mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-06-13 06:31:35 +00:00
refactor: async with multiaddr conn (#92)
BREAKING CHANGE: Switch to using async/await and async iterators. The transport and connection interfaces have changed. See the README for new usage.
This commit is contained in:
@ -7,75 +7,64 @@ const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
|
||||
const multiaddr = require('multiaddr')
|
||||
const pull = require('pull-stream')
|
||||
const goodbye = require('pull-goodbye')
|
||||
const pipe = require('it-pipe')
|
||||
const goodbye = require('it-goodbye')
|
||||
const { collect, take } = require('streaming-iterables')
|
||||
|
||||
const WS = require('../src')
|
||||
|
||||
const mockUpgrader = {
|
||||
upgradeInbound: maConn => maConn,
|
||||
upgradeOutbound: maConn => maConn
|
||||
}
|
||||
|
||||
describe('libp2p-websockets', () => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9095/ws')
|
||||
let ws
|
||||
let conn
|
||||
|
||||
beforeEach((done) => {
|
||||
ws = new WS()
|
||||
expect(ws).to.exist()
|
||||
conn = ws.dial(ma, (err, res) => {
|
||||
expect(err).to.not.exist()
|
||||
done()
|
||||
})
|
||||
beforeEach(async () => {
|
||||
ws = new WS({ upgrader: mockUpgrader })
|
||||
conn = await ws.dial(ma)
|
||||
})
|
||||
|
||||
it('echo', (done) => {
|
||||
const message = 'Hello World!'
|
||||
it('echo', async () => {
|
||||
const message = Buffer.from('Hello World!')
|
||||
const s = goodbye({ source: [message], sink: collect })
|
||||
|
||||
const s = goodbye({
|
||||
source: pull.values([message]),
|
||||
sink: pull.collect((err, results) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(results).to.eql([message])
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
pull(s, conn, s)
|
||||
const results = await pipe(s, conn, s)
|
||||
expect(results).to.eql([message])
|
||||
})
|
||||
|
||||
describe('stress', () => {
|
||||
it('one big write', (done) => {
|
||||
it('one big write', async () => {
|
||||
const rawMessage = Buffer.allocUnsafe(1000000).fill('a')
|
||||
|
||||
const s = goodbye({
|
||||
source: pull.values([rawMessage]),
|
||||
sink: pull.collect((err, results) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(results).to.eql([rawMessage])
|
||||
done()
|
||||
})
|
||||
})
|
||||
pull(s, conn, s)
|
||||
const s = goodbye({ source: [rawMessage], sink: collect })
|
||||
|
||||
const results = await pipe(s, conn, s)
|
||||
expect(results).to.eql([rawMessage])
|
||||
})
|
||||
|
||||
it('many writes', function (done) {
|
||||
it('many writes', async function () {
|
||||
this.timeout(10000)
|
||||
const s = goodbye({
|
||||
source: pull(
|
||||
pull.infinite(),
|
||||
pull.take(1000),
|
||||
pull.map((val) => Buffer.from(val.toString()))
|
||||
source: pipe(
|
||||
{
|
||||
[Symbol.iterator] () { return this },
|
||||
next: () => ({ done: false, value: Buffer.from(Math.random().toString()) })
|
||||
},
|
||||
take(20000)
|
||||
),
|
||||
sink: pull.collect((err, result) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(result).to.have.length(1000)
|
||||
done()
|
||||
})
|
||||
sink: collect
|
||||
})
|
||||
|
||||
pull(s, conn, s)
|
||||
const result = await pipe(s, conn, s)
|
||||
expect(result).to.have.length(20000)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('.createServer throws in browser', () => {
|
||||
expect(new WS().createListener).to.throw()
|
||||
it('.createServer throws in browser', () => {
|
||||
expect(new WS({ upgrader: mockUpgrader }).createListener).to.throw()
|
||||
})
|
||||
})
|
||||
|
@ -3,22 +3,61 @@
|
||||
|
||||
const tests = require('interface-transport')
|
||||
const multiaddr = require('multiaddr')
|
||||
const http = require('http')
|
||||
const WS = require('../src')
|
||||
|
||||
describe('compliance', () => {
|
||||
describe('interface-transport compliance', () => {
|
||||
tests({
|
||||
setup (callback) {
|
||||
const ws = new WS()
|
||||
async setup ({ upgrader }) { // eslint-disable-line require-await
|
||||
const ws = new WS({ upgrader })
|
||||
const addrs = [
|
||||
multiaddr('/ip4/127.0.0.1/tcp/9091/ws'),
|
||||
multiaddr('/ip4/127.0.0.1/tcp/9092/wss'),
|
||||
multiaddr('/ip4/127.0.0.1/tcp/9092/ws'),
|
||||
multiaddr('/dns4/ipfs.io/tcp/9092/ws'),
|
||||
multiaddr('/dns4/ipfs.io/tcp/9092/wss')
|
||||
]
|
||||
callback(null, ws, addrs)
|
||||
|
||||
let delayMs = 0
|
||||
const delayedCreateListener = (options, handler) => {
|
||||
if (typeof options === 'function') {
|
||||
handler = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
options = options || {}
|
||||
|
||||
// A server that will delay the upgrade event by delayMs
|
||||
options.server = new Proxy(http.createServer(), {
|
||||
get (server, prop) {
|
||||
if (prop === 'on') {
|
||||
return (event, handler) => {
|
||||
server.on(event, (...args) => {
|
||||
if (event !== 'upgrade' || !delayMs) {
|
||||
return handler(...args)
|
||||
}
|
||||
setTimeout(() => handler(...args), delayMs)
|
||||
})
|
||||
}
|
||||
}
|
||||
return server[prop]
|
||||
}
|
||||
})
|
||||
|
||||
return ws.createListener(options, handler)
|
||||
}
|
||||
|
||||
const wsProxy = new Proxy(ws, {
|
||||
get: (_, prop) => prop === 'createListener' ? delayedCreateListener : ws[prop]
|
||||
})
|
||||
|
||||
// Used by the dial tests to simulate a delayed connect
|
||||
const connector = {
|
||||
delay (ms) { delayMs = ms },
|
||||
restore () { delayMs = 0 }
|
||||
}
|
||||
|
||||
return { transport: wsProxy, addrs, connector }
|
||||
},
|
||||
teardown (callback) {
|
||||
callback()
|
||||
}
|
||||
async teardown () {}
|
||||
})
|
||||
})
|
||||
|
13
test/fixtures/certificate.pem
vendored
Normal file
13
test/fixtures/certificate.pem
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIICATCCAWoCCQDPufXH86n2QzANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJu
|
||||
bzETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0
|
||||
cyBQdHkgTHRkMB4XDTEyMDEwMTE0NDQwMFoXDTIwMDMxOTE0NDQwMFowRTELMAkG
|
||||
A1UEBhMCbm8xEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0
|
||||
IFdpZGdpdHMgUHR5IEx0ZDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAtrQ7
|
||||
+r//2iV/B6F+4boH0XqFn7alcV9lpjvAmwRXNKnxAoa0f97AjYPGNLKrjpkNXXhB
|
||||
JROIdbRbZnCNeC5fzX1a+JCo7KStzBXuGSZr27TtFmcV4H+9gIRIcNHtZmJLnxbJ
|
||||
sIhkGR8yVYdmJZe4eT5ldk1zoB1adgPF1hZhCBMCAwEAATANBgkqhkiG9w0BAQUF
|
||||
AAOBgQCeWBEHYJ4mCB5McwSSUox0T+/mJ4W48L/ZUE4LtRhHasU9hiW92xZkTa7E
|
||||
QLcoJKQiWfiLX2ysAro0NX4+V8iqLziMqvswnPzz5nezaOLE/9U/QvH3l8qqNkXu
|
||||
rNbsW1h/IO6FV8avWFYVFoutUwOaZ809k7iMh2F2JMgXQ5EymQ==
|
||||
-----END CERTIFICATE-----
|
15
test/fixtures/key.pem
vendored
Normal file
15
test/fixtures/key.pem
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIICXAIBAAKBgQC2tDv6v//aJX8HoX7hugfReoWftqVxX2WmO8CbBFc0qfEChrR/
|
||||
3sCNg8Y0squOmQ1deEElE4h1tFtmcI14Ll/NfVr4kKjspK3MFe4ZJmvbtO0WZxXg
|
||||
f72AhEhw0e1mYkufFsmwiGQZHzJVh2Yll7h5PmV2TXOgHVp2A8XWFmEIEwIDAQAB
|
||||
AoGAAlVY8sHi/aE+9xT77twWX3mGHV0SzdjfDnly40fx6S1Gc7bOtVdd9DC7pk6l
|
||||
3ENeJVR02IlgU8iC5lMHq4JEHPE272jtPrLlrpWLTGmHEqoVFv9AITPqUDLhB9Kk
|
||||
Hjl7h8NYBKbr2JHKICr3DIPKOT+RnXVb1PD4EORbJ3ooYmkCQQDfknUnVxPgxUGs
|
||||
ouABw1WJIOVgcCY/IFt4Ihf6VWTsxBgzTJKxn3HtgvE0oqTH7V480XoH0QxHhjLq
|
||||
DrgobWU9AkEA0TRJ8/ouXGnFEPAXjWr9GdPQRZ1Use2MrFjneH2+Sxc0CmYtwwqL
|
||||
Kr5kS6mqJrxprJeluSjBd+3/ElxURrEXjwJAUvmlN1OPEhXDmRHd92mKnlkyKEeX
|
||||
OkiFCiIFKih1S5Y/sRJTQ0781nyJjtJqO7UyC3pnQu1oFEePL+UEniRztQJAMfav
|
||||
AtnpYKDSM+1jcp7uu9BemYGtzKDTTAYfoiNF42EzSJiGrWJDQn4eLgPjY0T0aAf/
|
||||
yGz3Z9ErbhMm/Ysl+QJBAL4kBxRT8gM4ByJw4sdOvSeCCANFq8fhbgm8pGWlCPb5
|
||||
JGmX3/GHFM8x2tbWMGpyZP1DLtiNEFz7eCGktWK5rqE=
|
||||
-----END RSA PRIVATE KEY-----
|
436
test/node.js
436
test/node.js
@ -2,21 +2,30 @@
|
||||
/* eslint max-nested-callbacks: ["error", 6] */
|
||||
'use strict'
|
||||
|
||||
const https = require('https')
|
||||
const fs = require('fs')
|
||||
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
const multiaddr = require('multiaddr')
|
||||
const pull = require('pull-stream')
|
||||
const goodbye = require('pull-goodbye')
|
||||
const goodbye = require('it-goodbye')
|
||||
const { collect } = require('streaming-iterables')
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
const WS = require('../src')
|
||||
|
||||
require('./compliance.node')
|
||||
|
||||
const mockUpgrader = {
|
||||
upgradeInbound: maConn => maConn,
|
||||
upgradeOutbound: maConn => maConn
|
||||
}
|
||||
|
||||
describe('instantiate the transport', () => {
|
||||
it('create', () => {
|
||||
const ws = new WS()
|
||||
const ws = new WS({ upgrader: mockUpgrader })
|
||||
expect(ws).to.exist()
|
||||
})
|
||||
})
|
||||
@ -27,22 +36,21 @@ describe('listen', () => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
|
||||
beforeEach(() => {
|
||||
ws = new WS()
|
||||
ws = new WS({ upgrader: mockUpgrader })
|
||||
})
|
||||
|
||||
it('listen, check for callback', (done) => {
|
||||
it('listen, check for promise', async () => {
|
||||
const listener = ws.createListener((conn) => { })
|
||||
|
||||
listener.listen(ma, () => {
|
||||
listener.close(done)
|
||||
})
|
||||
await listener.listen(ma)
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('listen, check for listening event', (done) => {
|
||||
const listener = ws.createListener((conn) => { })
|
||||
|
||||
listener.on('listening', () => {
|
||||
listener.close(done)
|
||||
listener.on('listening', async () => {
|
||||
await listener.close()
|
||||
done()
|
||||
})
|
||||
|
||||
listener.listen(ma)
|
||||
@ -59,14 +67,12 @@ describe('listen', () => {
|
||||
listener.listen(ma)
|
||||
})
|
||||
|
||||
it('listen on addr with /ipfs/QmHASH', (done) => {
|
||||
it('listen on addr with /ipfs/QmHASH', async () => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
|
||||
const listener = ws.createListener((conn) => { })
|
||||
|
||||
listener.listen(ma, () => {
|
||||
listener.close(done)
|
||||
})
|
||||
await listener.listen(ma)
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it.skip('close listener with connections, through timeout', (done) => {
|
||||
@ -82,73 +88,53 @@ describe('listen', () => {
|
||||
// TODO 0.0.0.0 not supported yet
|
||||
})
|
||||
|
||||
it('getAddrs', (done) => {
|
||||
const listener = ws.createListener((conn) => {
|
||||
})
|
||||
listener.listen(ma, () => {
|
||||
listener.getAddrs((err, addrs) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(addrs.length).to.equal(1)
|
||||
expect(addrs[0]).to.deep.equal(ma)
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
it('getAddrs', async () => {
|
||||
const listener = ws.createListener((conn) => { })
|
||||
await listener.listen(ma)
|
||||
const addrs = await listener.getAddrs()
|
||||
expect(addrs.length).to.equal(1)
|
||||
expect(addrs[0]).to.deep.equal(ma)
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('getAddrs on port 0 listen', (done) => {
|
||||
const addr = multiaddr(`/ip4/127.0.0.1/tcp/0/ws`)
|
||||
const listener = ws.createListener((conn) => {
|
||||
})
|
||||
listener.listen(addr, () => {
|
||||
listener.getAddrs((err, addrs) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(addrs.length).to.equal(1)
|
||||
expect(addrs.map((a) => a.toOptions().port)).to.not.include('0')
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
it('getAddrs on port 0 listen', async () => {
|
||||
const addr = multiaddr('/ip4/127.0.0.1/tcp/0/ws')
|
||||
const listener = ws.createListener((conn) => { })
|
||||
await listener.listen(addr)
|
||||
const addrs = await listener.getAddrs()
|
||||
expect(addrs.length).to.equal(1)
|
||||
expect(addrs.map((a) => a.toOptions().port)).to.not.include('0')
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('getAddrs from listening on 0.0.0.0', (done) => {
|
||||
const addr = multiaddr(`/ip4/0.0.0.0/tcp/9003/ws`)
|
||||
const listener = ws.createListener((conn) => {
|
||||
})
|
||||
listener.listen(addr, () => {
|
||||
listener.getAddrs((err, addrs) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(addrs.map((a) => a.toOptions().host)).to.not.include('0.0.0.0')
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
it('getAddrs from listening on 0.0.0.0', async () => {
|
||||
const addr = multiaddr('/ip4/0.0.0.0/tcp/9003/ws')
|
||||
const listener = ws.createListener((conn) => { })
|
||||
await listener.listen(addr)
|
||||
const addrs = await listener.getAddrs()
|
||||
expect(addrs.map((a) => a.toOptions().host)).to.not.include('0.0.0.0')
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('getAddrs from listening on 0.0.0.0 and port 0', (done) => {
|
||||
const addr = multiaddr(`/ip4/0.0.0.0/tcp/0/ws`)
|
||||
const listener = ws.createListener((conn) => {
|
||||
})
|
||||
listener.listen(addr, () => {
|
||||
listener.getAddrs((err, addrs) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(addrs.map((a) => a.toOptions().host)).to.not.include('0.0.0.0')
|
||||
expect(addrs.map((a) => a.toOptions().port)).to.not.include('0')
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
it('getAddrs from listening on 0.0.0.0 and port 0', async () => {
|
||||
const addr = multiaddr('/ip4/0.0.0.0/tcp/0/ws')
|
||||
const listener = ws.createListener((conn) => { })
|
||||
await listener.listen(addr)
|
||||
const addrs = await listener.getAddrs()
|
||||
expect(addrs.map((a) => a.toOptions().host)).to.not.include('0.0.0.0')
|
||||
expect(addrs.map((a) => a.toOptions().port)).to.not.include('0')
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('getAddrs preserves IPFS Id', (done) => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
|
||||
it('getAddrs preserves p2p Id', async () => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/p2p/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
const listener = ws.createListener((conn) => { })
|
||||
|
||||
listener.listen(ma, () => {
|
||||
listener.getAddrs((err, addrs) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(addrs.length).to.equal(1)
|
||||
expect(addrs[0]).to.deep.equal(ma)
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
await listener.listen(ma)
|
||||
const addrs = await listener.getAddrs()
|
||||
expect(addrs.length).to.equal(1)
|
||||
expect(addrs[0]).to.deep.equal(ma)
|
||||
await listener.close()
|
||||
})
|
||||
})
|
||||
|
||||
@ -157,22 +143,21 @@ describe('listen', () => {
|
||||
const ma = multiaddr('/ip6/::1/tcp/9091/ws')
|
||||
|
||||
beforeEach(() => {
|
||||
ws = new WS()
|
||||
ws = new WS({ upgrader: mockUpgrader })
|
||||
})
|
||||
|
||||
it('listen, check for callback', (done) => {
|
||||
it('listen, check for promise', async () => {
|
||||
const listener = ws.createListener((conn) => { })
|
||||
|
||||
listener.listen(ma, () => {
|
||||
listener.close(done)
|
||||
})
|
||||
await listener.listen(ma)
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('listen, check for listening event', (done) => {
|
||||
const listener = ws.createListener((conn) => { })
|
||||
|
||||
listener.on('listening', () => {
|
||||
listener.close(done)
|
||||
listener.on('listening', async () => {
|
||||
await listener.close()
|
||||
done()
|
||||
})
|
||||
|
||||
listener.listen(ma)
|
||||
@ -189,14 +174,11 @@ describe('listen', () => {
|
||||
listener.listen(ma)
|
||||
})
|
||||
|
||||
it('listen on addr with /ipfs/QmHASH', (done) => {
|
||||
it('listen on addr with /ipfs/QmHASH', async () => {
|
||||
const ma = multiaddr('/ip6/::1/tcp/9091/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
|
||||
const listener = ws.createListener((conn) => { })
|
||||
|
||||
listener.listen(ma, () => {
|
||||
listener.close(done)
|
||||
})
|
||||
await listener.listen(ma)
|
||||
await listener.close()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -207,49 +189,86 @@ describe('dial', () => {
|
||||
let listener
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9091/ws')
|
||||
|
||||
beforeEach((done) => {
|
||||
ws = new WS()
|
||||
listener = ws.createListener((conn) => {
|
||||
pull(conn, conn)
|
||||
})
|
||||
listener.listen(ma, done)
|
||||
beforeEach(() => {
|
||||
ws = new WS({ upgrader: mockUpgrader })
|
||||
listener = ws.createListener(conn => pipe(conn, conn))
|
||||
return listener.listen(ma)
|
||||
})
|
||||
|
||||
afterEach((done) => {
|
||||
listener.close(done)
|
||||
afterEach(() => listener.close())
|
||||
|
||||
it('dial', async () => {
|
||||
const conn = await ws.dial(ma)
|
||||
const s = goodbye({ source: ['hey'], sink: collect })
|
||||
|
||||
const result = await pipe(s, conn, s)
|
||||
|
||||
expect(result).to.be.eql([Buffer.from('hey')])
|
||||
})
|
||||
|
||||
it('dial', (done) => {
|
||||
const conn = ws.dial(ma)
|
||||
it('dial with p2p Id', async () => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9091/ws/p2p/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
const conn = await ws.dial(ma)
|
||||
const s = goodbye({ source: ['hey'], sink: collect })
|
||||
|
||||
const s = goodbye({
|
||||
source: pull.values(['hey']),
|
||||
sink: pull.collect((err, result) => {
|
||||
expect(err).to.not.exist()
|
||||
const result = await pipe(s, conn, s)
|
||||
|
||||
expect(result).to.be.eql(['hey'])
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
pull(s, conn, s)
|
||||
expect(result).to.be.eql([Buffer.from('hey')])
|
||||
})
|
||||
|
||||
it('dial with IPFS Id', (done) => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9091/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
const conn = ws.dial(ma)
|
||||
it('should resolve port 0', async () => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/0/ws')
|
||||
const ws = new WS({ upgrader: mockUpgrader })
|
||||
|
||||
const s = goodbye({
|
||||
source: pull.values(['hey']),
|
||||
sink: pull.collect((err, result) => {
|
||||
expect(err).to.not.exist()
|
||||
// Create a Promise that resolves when a connection is handled
|
||||
let handled
|
||||
const handlerPromise = new Promise(resolve => { handled = resolve })
|
||||
const handler = conn => handled(conn)
|
||||
|
||||
expect(result).to.be.eql(['hey'])
|
||||
done()
|
||||
})
|
||||
})
|
||||
const listener = ws.createListener(handler)
|
||||
|
||||
pull(s, conn, s)
|
||||
// Listen on the multiaddr
|
||||
await listener.listen(ma)
|
||||
|
||||
const localAddrs = listener.getAddrs()
|
||||
expect(localAddrs.length).to.equal(1)
|
||||
|
||||
// Dial to that address
|
||||
await ws.dial(localAddrs[0])
|
||||
|
||||
// Wait for the incoming dial to be handled
|
||||
await handlerPromise
|
||||
|
||||
// close the listener
|
||||
await listener.close()
|
||||
})
|
||||
})
|
||||
|
||||
describe('ip4 with wss', () => {
|
||||
let ws
|
||||
let listener
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9091/wss')
|
||||
|
||||
const server = https.createServer({
|
||||
cert: fs.readFileSync('./test/fixtures/certificate.pem'),
|
||||
key: fs.readFileSync('./test/fixtures/key.pem')
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
ws = new WS({ upgrader: mockUpgrader })
|
||||
listener = ws.createListener({ server }, conn => pipe(conn, conn))
|
||||
return listener.listen(ma)
|
||||
})
|
||||
|
||||
afterEach(() => listener.close())
|
||||
|
||||
it('dial', async () => {
|
||||
const conn = await ws.dial(ma, { websocket: { rejectUnauthorized: false } })
|
||||
const s = goodbye({ source: ['hey'], sink: collect })
|
||||
|
||||
const result = await pipe(s, conn, s)
|
||||
|
||||
expect(result).to.be.eql([Buffer.from('hey')])
|
||||
})
|
||||
})
|
||||
|
||||
@ -258,49 +277,34 @@ describe('dial', () => {
|
||||
let listener
|
||||
const ma = multiaddr('/ip6/::1/tcp/9091')
|
||||
|
||||
beforeEach((done) => {
|
||||
ws = new WS()
|
||||
listener = ws.createListener((conn) => {
|
||||
pull(conn, conn)
|
||||
})
|
||||
listener.listen(ma, done)
|
||||
beforeEach(() => {
|
||||
ws = new WS({ upgrader: mockUpgrader })
|
||||
listener = ws.createListener(conn => pipe(conn, conn))
|
||||
return listener.listen(ma)
|
||||
})
|
||||
|
||||
afterEach((done) => {
|
||||
listener.close(done)
|
||||
afterEach(() => listener.close())
|
||||
|
||||
it('dial', async () => {
|
||||
const conn = await ws.dial(ma)
|
||||
const s = goodbye({ source: ['hey'], sink: collect })
|
||||
|
||||
const result = await pipe(s, conn, s)
|
||||
|
||||
expect(result).to.be.eql([Buffer.from('hey')])
|
||||
})
|
||||
|
||||
it('dial', (done) => {
|
||||
const conn = ws.dial(ma)
|
||||
it('dial with p2p Id', async () => {
|
||||
const ma = multiaddr('/ip6/::1/tcp/9091/ws/p2p/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
const conn = await ws.dial(ma)
|
||||
|
||||
const s = goodbye({
|
||||
source: pull.values(['hey']),
|
||||
sink: pull.collect((err, result) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
expect(result).to.be.eql(['hey'])
|
||||
done()
|
||||
})
|
||||
source: ['hey'],
|
||||
sink: collect
|
||||
})
|
||||
|
||||
pull(s, conn, s)
|
||||
})
|
||||
|
||||
it('dial with IPFS Id', (done) => {
|
||||
const ma = multiaddr('/ip6/::1/tcp/9091/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
const conn = ws.dial(ma)
|
||||
|
||||
const s = goodbye({
|
||||
source: pull.values(['hey']),
|
||||
sink: pull.collect((err, result) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
expect(result).to.be.eql(['hey'])
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
pull(s, conn, s)
|
||||
const result = await pipe(s, conn, s)
|
||||
expect(result).to.be.eql([Buffer.from('hey')])
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -309,7 +313,7 @@ describe('filter addrs', () => {
|
||||
let ws
|
||||
|
||||
before(() => {
|
||||
ws = new WS()
|
||||
ws = new WS({ upgrader: mockUpgrader })
|
||||
})
|
||||
|
||||
describe('filter valid addrs for this transport', function () {
|
||||
@ -439,125 +443,3 @@ describe('filter addrs', () => {
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
describe('valid Connection', () => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9092/ws')
|
||||
|
||||
it('get observed addrs', (done) => {
|
||||
let dialerObsAddrs
|
||||
let listenerObsAddrs
|
||||
|
||||
const ws = new WS()
|
||||
|
||||
const listener = ws.createListener((conn) => {
|
||||
expect(conn).to.exist()
|
||||
|
||||
conn.getObservedAddrs((err, addrs) => {
|
||||
expect(err).to.not.exist()
|
||||
dialerObsAddrs = addrs
|
||||
})
|
||||
|
||||
pull(conn, conn)
|
||||
})
|
||||
|
||||
listener.listen(ma, () => {
|
||||
const conn = ws.dial(ma)
|
||||
|
||||
pull(
|
||||
pull.empty(),
|
||||
conn,
|
||||
pull.onEnd(onEnd)
|
||||
)
|
||||
|
||||
function onEnd () {
|
||||
conn.getObservedAddrs((err, addrs) => {
|
||||
expect(err).to.not.exist()
|
||||
listenerObsAddrs = addrs
|
||||
|
||||
listener.close(onClose)
|
||||
|
||||
function onClose () {
|
||||
expect(listenerObsAddrs[0]).to.deep.equal(ma)
|
||||
expect(dialerObsAddrs.length).to.equal(0)
|
||||
done()
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('get Peer Info', (done) => {
|
||||
const ws = new WS()
|
||||
|
||||
const listener = ws.createListener((conn) => {
|
||||
expect(conn).to.exist()
|
||||
|
||||
conn.getPeerInfo((err, peerInfo) => {
|
||||
expect(err).to.exist()
|
||||
})
|
||||
|
||||
pull(conn, conn)
|
||||
})
|
||||
|
||||
listener.listen(ma, () => {
|
||||
const conn = ws.dial(ma)
|
||||
|
||||
pull(
|
||||
pull.empty(),
|
||||
conn,
|
||||
pull.onEnd(onEnd)
|
||||
)
|
||||
|
||||
function onEnd () {
|
||||
conn.getPeerInfo((err, peerInfo) => {
|
||||
expect(err).to.exist()
|
||||
listener.close(done)
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it('set Peer Info', (done) => {
|
||||
const ws = new WS()
|
||||
|
||||
const listener = ws.createListener((conn) => {
|
||||
expect(conn).to.exist()
|
||||
conn.setPeerInfo('a')
|
||||
|
||||
conn.getPeerInfo((err, peerInfo) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(peerInfo).to.equal('a')
|
||||
})
|
||||
|
||||
pull(conn, conn)
|
||||
})
|
||||
|
||||
listener.listen(ma, onListen)
|
||||
|
||||
function onListen () {
|
||||
const conn = ws.dial(ma)
|
||||
conn.setPeerInfo('b')
|
||||
|
||||
pull(
|
||||
pull.empty(),
|
||||
conn,
|
||||
pull.onEnd(onEnd)
|
||||
)
|
||||
|
||||
function onEnd () {
|
||||
conn.getPeerInfo((err, peerInfo) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(peerInfo).to.equal('b')
|
||||
listener.close(done)
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe.skip('turbolence', () => {
|
||||
it('dialer - emits error on the other end is terminated abruptly', (done) => {
|
||||
})
|
||||
it('listener - emits error on the other end is terminated abruptly', (done) => {
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user