diff --git a/package.json b/package.json index 9a6fd7b..12a3494 100644 --- a/package.json +++ b/package.json @@ -60,4 +60,4 @@ "Greenkeeper ", "Richard Littauer " ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 21a5a6f..b7acf9e 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,7 @@ const connect = require('pull-ws/client') const mafmt = require('mafmt') const includes = require('lodash.includes') const Connection = require('interface-connection').Connection + const maToUrl = require('./ma-to-url') const debug = require('debug') const log = debug('libp2p:websockets:dialer') diff --git a/src/ma-to-url.js b/src/ma-to-url.js index 86beff6..8612069 100644 --- a/src/ma-to-url.js +++ b/src/ma-to-url.js @@ -1,21 +1,36 @@ 'use strict' -const multiaddr = require('multiaddr') +const debug = require('debug') +const log = debug('libp2p:websockets:dialer') function maToUrl (ma) { const maStrSplit = ma.toString().split('/') - const proto = ma.protos()[2].name - if (!(proto === 'ws' || proto === 'wss')) { - throw new Error('invalid multiaddr' + ma.toString()) + 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 url = ma.protos()[2].name + '://' + maStrSplit[2] - - if (!multiaddr.isName(ma)) { - url += ':' + maStrSplit[4] + 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 } diff --git a/test/compliance.node.js b/test/compliance.node.js index 1487ebe..3f31f97 100644 --- a/test/compliance.node.js +++ b/test/compliance.node.js @@ -11,9 +11,9 @@ describe('compliance', () => { let ws = new WS() const addrs = [ multiaddr('/ip4/127.0.0.1/tcp/9091/ws'), - multiaddr('/ip4/127.0.0.1/tcp/9092/wss') - // multiaddr('/dns4/awesome-dns-server.com/tcp/9092/ws'), - // multiaddr('/dns4/awesome-dns-server.com/tcp/9092/wss') + multiaddr('/ip4/127.0.0.1/tcp/9092/wss'), + multiaddr('/dns4/ipfs.io/tcp/9092/ws'), + multiaddr('/dns4/ipfs.io/tcp/9092/wss') ] callback(null, ws, addrs) }, diff --git a/test/node.js b/test/node.js index dc8a17a..19875fb 100644 --- a/test/node.js +++ b/test/node.js @@ -10,6 +10,7 @@ const pull = require('pull-stream') const goodbye = require('pull-goodbye') const WS = require('../src') +const maToUrl = require('../src/ma-to-url') require('./compliance.node') @@ -425,7 +426,27 @@ 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) => {}) - it('listener - emits error on the other end is terminated abruptly', (done) => {}) + it('dialer - emits error on the other end is terminated abruptly', (done) => { + }) + it('listener - emits error on the other end is terminated abruptly', (done) => { + }) })