fix: address parsing (#57)

* feat: dns support for WS

* fix: address parsing

* test: adding ma-to-url tests
This commit is contained in:
Dmitriy Ryajov
2017-03-23 16:19:36 -07:00
committed by David Dias
parent f872518141
commit 9fbbe3f2a0
5 changed files with 52 additions and 15 deletions

View File

@ -4,6 +4,7 @@ const connect = require('pull-ws/client')
const mafmt = require('mafmt') const mafmt = require('mafmt')
const includes = require('lodash.includes') const includes = require('lodash.includes')
const Connection = require('interface-connection').Connection const Connection = require('interface-connection').Connection
const maToUrl = require('./ma-to-url') const maToUrl = require('./ma-to-url')
const debug = require('debug') const debug = require('debug')
const log = debug('libp2p:websockets:dialer') const log = debug('libp2p:websockets:dialer')

View File

@ -1,20 +1,35 @@
'use strict' 'use strict'
const multiaddr = require('multiaddr') const debug = require('debug')
const log = debug('libp2p:websockets:dialer')
function maToUrl (ma) { function maToUrl (ma) {
const maStrSplit = ma.toString().split('/') const maStrSplit = ma.toString().split('/')
const proto = ma.protos()[2].name
if (!(proto === 'ws' || proto === 'wss')) { let proto
throw new Error('invalid multiaddr' + ma.toString()) 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] let port
try {
if (!multiaddr.isName(ma)) { port = ma.stringTuples().filter((tuple) => {
url += ':' + maStrSplit[4] 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 return url
} }

View File

@ -11,9 +11,9 @@ describe('compliance', () => {
let ws = new WS() let ws = new WS()
const addrs = [ const addrs = [
multiaddr('/ip4/127.0.0.1/tcp/9091/ws'), 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/wss'),
// multiaddr('/dns4/awesome-dns-server.com/tcp/9092/ws'), multiaddr('/dns4/ipfs.io/tcp/9092/ws'),
// multiaddr('/dns4/awesome-dns-server.com/tcp/9092/wss') multiaddr('/dns4/ipfs.io/tcp/9092/wss')
] ]
callback(null, ws, addrs) callback(null, ws, addrs)
}, },

View File

@ -10,6 +10,7 @@ const pull = require('pull-stream')
const goodbye = require('pull-goodbye') const goodbye = require('pull-goodbye')
const WS = require('../src') const WS = require('../src')
const maToUrl = require('../src/ma-to-url')
require('./compliance.node') require('./compliance.node')
@ -425,7 +426,27 @@ describe('valid Connection', () => {
}) })
}) })
describe.skip('turbolence', () => { describe('ma-to-url test', function () {
it('dialer - emits error on the other end is terminated abruptly', (done) => {}) it('should convert ipv4 ma to url', function () {
it('listener - emits error on the other end is terminated abruptly', (done) => {}) 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) => {
})
}) })