mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-04-25 21:32:28 +00:00
fix: ipv6 naming with multiaddr-to-uri package (#81)
* chore: use multiaddr-to-uri package * fix: ipv6 naming with multiaddr-to-uri
This commit is contained in:
parent
24541d371f
commit
93ef7c3e70
@ -44,6 +44,7 @@
|
|||||||
"debug": "^4.1.1",
|
"debug": "^4.1.1",
|
||||||
"interface-connection": "~0.3.2",
|
"interface-connection": "~0.3.2",
|
||||||
"mafmt": "^6.0.4",
|
"mafmt": "^6.0.4",
|
||||||
|
"multiaddr-to-uri": "^4.0.1",
|
||||||
"pull-ws": "hugomrdias/pull-ws#fix/bundle-size"
|
"pull-ws": "hugomrdias/pull-ws#fix/bundle-size"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -5,7 +5,7 @@ const mafmt = require('mafmt')
|
|||||||
const withIs = require('class-is')
|
const withIs = require('class-is')
|
||||||
const Connection = require('interface-connection').Connection
|
const Connection = require('interface-connection').Connection
|
||||||
|
|
||||||
const maToUrl = require('./ma-to-url')
|
const toUri = require('multiaddr-to-uri')
|
||||||
const debug = require('debug')
|
const debug = require('debug')
|
||||||
const log = debug('libp2p:websockets:dialer')
|
const log = debug('libp2p:websockets:dialer')
|
||||||
|
|
||||||
@ -18,9 +18,9 @@ class WebSockets {
|
|||||||
options = {}
|
options = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
callback = callback || function () {}
|
callback = callback || function () { }
|
||||||
|
|
||||||
const url = maToUrl(ma)
|
const url = toUri(ma)
|
||||||
log('dialing %s', url)
|
log('dialing %s', url)
|
||||||
const socket = connect(url, {
|
const socket = connect(url, {
|
||||||
binary: true,
|
binary: true,
|
||||||
|
@ -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
|
|
145
test/node.js
145
test/node.js
@ -1,5 +1,5 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
/* eslint max-nested-callbacks: ["error", 5] */
|
/* eslint max-nested-callbacks: ["error", 6] */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const chai = require('chai')
|
const chai = require('chai')
|
||||||
@ -11,7 +11,6 @@ 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')
|
||||||
|
|
||||||
@ -23,6 +22,7 @@ describe('instantiate the transport', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('listen', () => {
|
describe('listen', () => {
|
||||||
|
describe('ip4', () => {
|
||||||
let ws
|
let ws
|
||||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||||
|
|
||||||
@ -31,7 +31,7 @@ describe('listen', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('listen, check for callback', (done) => {
|
it('listen, check for callback', (done) => {
|
||||||
const listener = ws.createListener((conn) => {})
|
const listener = ws.createListener((conn) => { })
|
||||||
|
|
||||||
listener.listen(ma, () => {
|
listener.listen(ma, () => {
|
||||||
listener.close(done)
|
listener.close(done)
|
||||||
@ -39,7 +39,7 @@ describe('listen', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('listen, check for listening event', (done) => {
|
it('listen, check for listening event', (done) => {
|
||||||
const listener = ws.createListener((conn) => {})
|
const listener = ws.createListener((conn) => { })
|
||||||
|
|
||||||
listener.on('listening', () => {
|
listener.on('listening', () => {
|
||||||
listener.close(done)
|
listener.close(done)
|
||||||
@ -49,7 +49,7 @@ describe('listen', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('listen, check for the close event', (done) => {
|
it('listen, check for the close event', (done) => {
|
||||||
const listener = ws.createListener((conn) => {})
|
const listener = ws.createListener((conn) => { })
|
||||||
|
|
||||||
listener.on('listening', () => {
|
listener.on('listening', () => {
|
||||||
listener.on('close', done)
|
listener.on('close', done)
|
||||||
@ -62,7 +62,7 @@ describe('listen', () => {
|
|||||||
it('listen on addr with /ipfs/QmHASH', (done) => {
|
it('listen on addr with /ipfs/QmHASH', (done) => {
|
||||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
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.listen(ma, () => {
|
||||||
listener.close(done)
|
listener.close(done)
|
||||||
@ -77,9 +77,6 @@ describe('listen', () => {
|
|||||||
it.skip('listen on port 0', (done) => {
|
it.skip('listen on port 0', (done) => {
|
||||||
// TODO port 0 not supported yet
|
// 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) => {
|
it.skip('listen on any Interface', (done) => {
|
||||||
// TODO 0.0.0.0 not supported yet
|
// TODO 0.0.0.0 not supported yet
|
||||||
@ -142,7 +139,7 @@ describe('listen', () => {
|
|||||||
it('getAddrs preserves IPFS Id', (done) => {
|
it('getAddrs preserves IPFS Id', (done) => {
|
||||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
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.listen(ma, () => {
|
||||||
listener.getAddrs((err, addrs) => {
|
listener.getAddrs((err, addrs) => {
|
||||||
@ -153,9 +150,59 @@ describe('listen', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('ip6', () => {
|
||||||
|
let ws
|
||||||
|
const ma = multiaddr('/ip6/::1/tcp/9091/ws')
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
ws = new WS()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('listen, check for callback', (done) => {
|
||||||
|
const listener = ws.createListener((conn) => { })
|
||||||
|
|
||||||
|
listener.listen(ma, () => {
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('listen, check for listening event', (done) => {
|
||||||
|
const listener = ws.createListener((conn) => { })
|
||||||
|
|
||||||
|
listener.on('listening', () => {
|
||||||
|
listener.close(done)
|
||||||
|
})
|
||||||
|
|
||||||
|
listener.listen(ma)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('listen, check for the close event', (done) => {
|
||||||
|
const listener = ws.createListener((conn) => { })
|
||||||
|
|
||||||
|
listener.on('listening', () => {
|
||||||
|
listener.on('close', done)
|
||||||
|
listener.close()
|
||||||
|
})
|
||||||
|
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('dial', () => {
|
describe('dial', () => {
|
||||||
|
describe('ip4', () => {
|
||||||
let ws
|
let ws
|
||||||
let listener
|
let listener
|
||||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9091/ws')
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/9091/ws')
|
||||||
@ -172,7 +219,7 @@ describe('dial', () => {
|
|||||||
listener.close(done)
|
listener.close(done)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('dial on IPv4', (done) => {
|
it('dial', (done) => {
|
||||||
const conn = ws.dial(ma)
|
const conn = ws.dial(ma)
|
||||||
|
|
||||||
const s = goodbye({
|
const s = goodbye({
|
||||||
@ -188,11 +235,7 @@ describe('dial', () => {
|
|||||||
pull(s, conn, s)
|
pull(s, conn, s)
|
||||||
})
|
})
|
||||||
|
|
||||||
it.skip('dial on IPv6', (done) => {
|
it('dial with IPFS Id', (done) => {
|
||||||
// TODO IPv6 not supported yet
|
|
||||||
})
|
|
||||||
|
|
||||||
it('dial on IPv4 with IPFS Id', (done) => {
|
|
||||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9091/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/9091/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||||
const conn = ws.dial(ma)
|
const conn = ws.dial(ma)
|
||||||
|
|
||||||
@ -208,6 +251,58 @@ describe('dial', () => {
|
|||||||
|
|
||||||
pull(s, conn, s)
|
pull(s, conn, s)
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('ip6', () => {
|
||||||
|
let ws
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('filter addrs', () => {
|
describe('filter addrs', () => {
|
||||||
@ -460,24 +555,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', () => {
|
describe.skip('turbolence', () => {
|
||||||
it('dialer - emits error on the other end is terminated abruptly', (done) => {
|
it('dialer - emits error on the other end is terminated abruptly', (done) => {
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user