mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-26 04:11:04 +00:00
* fix: use new libp2p-crypto
* fix: remove node globals and reduce size
- adds buffer require
- adds globalThis where needed
- streaming-iterables was remove and new utils created, this will be consolidated in `ipfs-utils` and backported here to normalize all these iterator helper functions
- latency-monitor was copied inside the repo
- iso-random-stream is now used instead of node crypto
- the test `should ignore self on discovery` was moved to node only
Size: 172.97KB
47.03KB below the 220KB limit.
`aegir build --node false` and `aegir test -t browser --node false` now work 🎉
* fix: fix require path
* fix: feedback
* fix: update deps and bundle size
* chore: bump interfaces
* chore: update size
96 lines
2.5 KiB
JavaScript
96 lines
2.5 KiB
JavaScript
/* eslint-env mocha */
|
|
'use strict'
|
|
|
|
const { Buffer } = require('buffer')
|
|
const chai = require('chai')
|
|
const dirtyChai = require('dirty-chai')
|
|
chai.use(dirtyChai)
|
|
const expect = chai.expect
|
|
const duplexPair = require('it-pair/duplex')
|
|
const pipe = require('it-pipe')
|
|
const { collect } = require('streaming-iterables')
|
|
|
|
const Protector = require('../../src/pnet')
|
|
const Errors = Protector.errors
|
|
const generate = Protector.generate
|
|
|
|
const swarmKeyBuffer = Buffer.alloc(95)
|
|
const wrongSwarmKeyBuffer = Buffer.alloc(95)
|
|
|
|
// Write new psk files to the buffers
|
|
generate(swarmKeyBuffer)
|
|
generate(wrongSwarmKeyBuffer)
|
|
|
|
describe('private network', () => {
|
|
it('should accept a valid psk buffer', () => {
|
|
const protector = new Protector(swarmKeyBuffer)
|
|
|
|
expect(protector.tag).to.equal('/key/swarm/psk/1.0.0/')
|
|
expect(protector.psk.byteLength).to.equal(32)
|
|
})
|
|
|
|
it('should protect a simple connection', async () => {
|
|
const [inbound, outbound] = duplexPair()
|
|
const protector = new Protector(swarmKeyBuffer)
|
|
|
|
const [aToB, bToA] = await Promise.all([
|
|
protector.protect(inbound),
|
|
protector.protect(outbound)
|
|
])
|
|
|
|
pipe(
|
|
[Buffer.from('hello world'), Buffer.from('doo dah')],
|
|
aToB
|
|
)
|
|
|
|
const output = await pipe(
|
|
bToA,
|
|
source => (async function * () {
|
|
for await (const chunk of source) {
|
|
yield chunk.slice()
|
|
}
|
|
})(),
|
|
collect
|
|
)
|
|
|
|
expect(output).to.eql([Buffer.from('hello world'), Buffer.from('doo dah')])
|
|
})
|
|
|
|
it('should not be able to share correct data with different keys', async () => {
|
|
const [inbound, outbound] = duplexPair()
|
|
const protector = new Protector(swarmKeyBuffer)
|
|
const protectorB = new Protector(wrongSwarmKeyBuffer)
|
|
|
|
const [aToB, bToA] = await Promise.all([
|
|
protector.protect(inbound),
|
|
protectorB.protect(outbound)
|
|
])
|
|
|
|
pipe(
|
|
[Buffer.from('hello world'), Buffer.from('doo dah')],
|
|
aToB
|
|
)
|
|
|
|
const output = await pipe(
|
|
bToA,
|
|
collect
|
|
)
|
|
|
|
expect(output).to.not.eql([Buffer.from('hello world'), Buffer.from('doo dah')])
|
|
})
|
|
|
|
describe('invalid psks', () => {
|
|
it('should not accept a bad psk', () => {
|
|
expect(() => {
|
|
return new Protector(Buffer.from('not-a-key'))
|
|
}).to.throw(Errors.INVALID_PSK)
|
|
})
|
|
|
|
it('should not accept a psk of incorrect length', () => {
|
|
expect(() => {
|
|
return new Protector(Buffer.from('/key/swarm/psk/1.0.0/\n/base16/\ndffb7e'))
|
|
}).to.throw(Errors.INVALID_PSK)
|
|
})
|
|
})
|
|
})
|