From fe5e5c8fab272fe9f96b38cbfef9f33614b988d3 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Wed, 23 Jan 2019 16:52:50 +0000 Subject: [PATCH 1/2] chore: use multiaddr-to-uri package --- package.json | 1 + src/index.js | 4 ++-- src/ma-to-url.js | 37 ------------------------------------- test/node.js | 19 ------------------- 4 files changed, 3 insertions(+), 58 deletions(-) delete mode 100644 src/ma-to-url.js diff --git a/package.json b/package.json index 16bbed1..27354a6 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "debug": "^4.1.1", "interface-connection": "~0.3.2", "mafmt": "^6.0.4", + "multiaddr-to-uri": "^4.0.1", "pull-ws": "hugomrdias/pull-ws#fix/bundle-size" }, "devDependencies": { diff --git a/src/index.js b/src/index.js index d0ae4af..647c720 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,7 @@ const mafmt = require('mafmt') const withIs = require('class-is') const Connection = require('interface-connection').Connection -const maToUrl = require('./ma-to-url') +const toUri = require('multiaddr-to-uri') const debug = require('debug') const log = debug('libp2p:websockets:dialer') @@ -20,7 +20,7 @@ class WebSockets { callback = callback || function () {} - const url = maToUrl(ma) + const url = toUri(ma) log('dialing %s', url) const socket = connect(url, { binary: true, diff --git a/src/ma-to-url.js b/src/ma-to-url.js deleted file mode 100644 index 8612069..0000000 --- a/src/ma-to-url.js +++ /dev/null @@ -1,37 +0,0 @@ -'use strict' - -const debug = require('debug') -const log = debug('libp2p:websockets:dialer') - -function maToUrl (ma) { - const maStrSplit = ma.toString().split('/') - - let proto - try { - proto = ma.protoNames().filter((proto) => { - return proto === 'ws' || proto === 'wss' - })[0] - } catch (e) { - log(e) - throw new Error('Not a valid websocket address', e) - } - - let port - try { - port = ma.stringTuples().filter((tuple) => { - if (tuple[0] === ma.protos().filter((proto) => { - return proto.name === 'tcp' - })[0].code) { - return true - } - })[0][1] - } catch (e) { - log('No port, skipping') - } - - let url = `${proto}://${maStrSplit[2]}${(port && (port !== 80 || port !== 443) ? `:${port}` : '')}` - - return url -} - -module.exports = maToUrl diff --git a/test/node.js b/test/node.js index c8fc9eb..bcf2746 100644 --- a/test/node.js +++ b/test/node.js @@ -11,7 +11,6 @@ const pull = require('pull-stream') const goodbye = require('pull-goodbye') const WS = require('../src') -const maToUrl = require('../src/ma-to-url') require('./compliance.node') @@ -460,24 +459,6 @@ describe('valid Connection', () => { }) }) -describe('ma-to-url test', function () { - it('should convert ipv4 ma to url', function () { - expect(maToUrl(multiaddr('/ip4/127.0.0.1/ws'))).to.equal('ws://127.0.0.1') - }) - - it('should convert ipv4 ma with port to url', function () { - expect(maToUrl(multiaddr('/ip4/127.0.0.1/tcp/80/ws'))).to.equal('ws://127.0.0.1:80') - }) - - it('should convert dns ma to url', function () { - expect(maToUrl(multiaddr('/dns4/ipfs.io/ws'))).to.equal('ws://ipfs.io') - }) - - it('should convert dns ma with port to url', function () { - expect(maToUrl(multiaddr('/dns4/ipfs.io/tcp/80/ws'))).to.equal('ws://ipfs.io:80') - }) -}) - describe.skip('turbolence', () => { it('dialer - emits error on the other end is terminated abruptly', (done) => { }) From dd15f5a610dbd210f4d85796047b620ec9c9d753 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Wed, 23 Jan 2019 22:42:49 +0000 Subject: [PATCH 2/2] fix: ipv6 naming with multiaddr-to-uri --- src/index.js | 2 +- test/node.js | 340 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 219 insertions(+), 123 deletions(-) diff --git a/src/index.js b/src/index.js index 647c720..b3053b0 100644 --- a/src/index.js +++ b/src/index.js @@ -18,7 +18,7 @@ class WebSockets { options = {} } - callback = callback || function () {} + callback = callback || function () { } const url = toUri(ma) log('dialing %s', url) diff --git a/test/node.js b/test/node.js index bcf2746..f6ba702 100644 --- a/test/node.js +++ b/test/node.js @@ -1,5 +1,5 @@ /* eslint-env mocha */ -/* eslint max-nested-callbacks: ["error", 5] */ +/* eslint max-nested-callbacks: ["error", 6] */ 'use strict' const chai = require('chai') @@ -22,132 +22,179 @@ describe('instantiate the transport', () => { }) describe('listen', () => { - let ws - const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws') + describe('ip4', () => { + let ws + const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws') - beforeEach(() => { - ws = new WS() - }) + beforeEach(() => { + ws = new WS() + }) - it('listen, check for callback', (done) => { - const listener = ws.createListener((conn) => {}) + it('listen, check for callback', (done) => { + const listener = ws.createListener((conn) => { }) - listener.listen(ma, () => { - listener.close(done) + listener.listen(ma, () => { + listener.close(done) + }) }) - }) - it('listen, check for listening event', (done) => { - const listener = ws.createListener((conn) => {}) + it('listen, check for listening event', (done) => { + const listener = ws.createListener((conn) => { }) - listener.on('listening', () => { - listener.close(done) + listener.on('listening', () => { + listener.close(done) + }) + + listener.listen(ma) }) - listener.listen(ma) - }) + it('listen, check for the close event', (done) => { + const listener = ws.createListener((conn) => { }) - it('listen, check for the close event', (done) => { - const listener = ws.createListener((conn) => {}) + listener.on('listening', () => { + listener.on('close', done) + listener.close() + }) - listener.on('listening', () => { - listener.on('close', done) - listener.close() + listener.listen(ma) }) - listener.listen(ma) - }) + it('listen on addr with /ipfs/QmHASH', (done) => { + const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw') - it('listen on addr with /ipfs/QmHASH', (done) => { - const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw') + const listener = ws.createListener((conn) => { }) - const listener = ws.createListener((conn) => {}) + listener.listen(ma, () => { + listener.close(done) + }) + }) - listener.listen(ma, () => { - listener.close(done) + it.skip('close listener with connections, through timeout', (done) => { + // TODO `ws` closes all anyway, we need to make it not close + // first - https://github.com/diasdavid/simple-websocket-server }) - }) - it.skip('close listener with connections, through timeout', (done) => { - // TODO `ws` closes all anyway, we need to make it not close - // first - https://github.com/diasdavid/simple-websocket-server - }) + it.skip('listen on port 0', (done) => { + // TODO port 0 not supported yet + }) - it.skip('listen on port 0', (done) => { - // TODO port 0 not supported yet - }) - it.skip('listen on IPv6 addr', (done) => { - // TODO IPv6 not supported yet - }) + it.skip('listen on any Interface', (done) => { + // TODO 0.0.0.0 not supported yet + }) - it.skip('listen on any Interface', (done) => { - // 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', (done) => { - const listener = ws.createListener((conn) => { + 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) + }) + }) }) - 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 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 on port 0 listen', (done) => { - const addr = multiaddr(`/ip4/127.0.0.1/tcp/0/ws`) - const listener = ws.createListener((conn) => { + 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) + }) + }) }) - 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 preserves IPFS Id', (done) => { + const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/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) + }) }) }) }) - 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) => { + describe('ip6', () => { + let ws + const ma = multiaddr('/ip6/::1/tcp/9091/ws') + + beforeEach(() => { + ws = new WS() }) - 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') + + it('listen, check for callback', (done) => { + const listener = ws.createListener((conn) => { }) + + listener.listen(ma, () => { listener.close(done) }) }) - }) - 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') + it('listen, check for listening event', (done) => { + const listener = ws.createListener((conn) => { }) + + listener.on('listening', () => { listener.close(done) }) + + listener.listen(ma) }) - }) - it('getAddrs preserves IPFS Id', (done) => { - const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw') + it('listen, check for the close event', (done) => { + const listener = ws.createListener((conn) => { }) - const listener = ws.createListener((conn) => {}) + listener.on('listening', () => { + listener.on('close', done) + listener.close() + }) - 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.listen(ma) + }) + + it('listen on addr with /ipfs/QmHASH', (done) => { + const ma = multiaddr('/ip6/::1/tcp/9091/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw') + + const listener = ws.createListener((conn) => { }) + + listener.listen(ma, () => { listener.close(done) }) }) @@ -155,57 +202,106 @@ describe('listen', () => { }) describe('dial', () => { - let ws - 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) + describe('ip4', () => { + let ws + 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) }) - listener.listen(ma, done) - }) - afterEach((done) => { - listener.close(done) - }) + afterEach((done) => { + listener.close(done) + }) - it('dial on IPv4', (done) => { - const conn = ws.dial(ma) + it('dial', (done) => { + const conn = ws.dial(ma) - const s = goodbye({ - source: pull.values(['hey']), - sink: pull.collect((err, result) => { - expect(err).to.not.exist() + const s = goodbye({ + source: pull.values(['hey']), + sink: pull.collect((err, result) => { + expect(err).to.not.exist() - expect(result).to.be.eql(['hey']) - done() + expect(result).to.be.eql(['hey']) + done() + }) }) + + pull(s, conn, s) }) - pull(s, conn, s) - }) + it('dial with IPFS Id', (done) => { + const ma = multiaddr('/ip4/127.0.0.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() + }) + }) - it.skip('dial on IPv6', (done) => { - // TODO IPv6 not supported yet + pull(s, conn, s) + }) }) - it('dial on IPv4 with IPFS Id', (done) => { - const ma = multiaddr('/ip4/127.0.0.1/tcp/9091/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw') - const conn = ws.dial(ma) + describe('ip6', () => { + let ws + let listener + const ma = multiaddr('/ip6/::1/tcp/9091') - const s = goodbye({ - source: pull.values(['hey']), - sink: pull.collect((err, result) => { - expect(err).to.not.exist() + beforeEach((done) => { + ws = new WS() + listener = ws.createListener((conn) => { + pull(conn, conn) + }) + listener.listen(ma, done) + }) - expect(result).to.be.eql(['hey']) - done() + afterEach((done) => { + listener.close(done) + }) + + it('dial', (done) => { + 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) }) - 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) + }) }) })