Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
fix: address parsing (#57)
Browse files Browse the repository at this point in the history
* feat: dns support for WS

* fix: address parsing

* test: adding ma-to-url tests
  • Loading branch information
dryajov authored and daviddias committed Mar 23, 2017
1 parent f872518 commit 9fbbe3f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@
"Greenkeeper <[email protected]>",
"Richard Littauer <[email protected]>"
]
}
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
31 changes: 23 additions & 8 deletions src/ma-to-url.js
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions test/compliance.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
25 changes: 23 additions & 2 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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) => {
})
})

0 comments on commit 9fbbe3f

Please sign in to comment.