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

Commit

Permalink
fix: support multiaddr 7 (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Sep 30, 2019
1 parent 5a9434b commit 996c532
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"dirty-chai": "^2.0.1",
"interface-transport": "~0.3.7",
"multiaddr": "^6.0.6",
"multiaddr7": "npm:multiaddr@^7.0.0",
"pull-goodbye": "0.0.2",
"pull-stream": "^3.6.9"
},
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ class WebSockets {
return false
}

if (ma.protoNames().includes('ipfs')) {
if (typeof ma.decapsulateCode === 'function') {
ma = ma.decapsulateCode(421) // multiaddr 7
} else if (ma.protoNames().includes('ipfs')) {
ma = ma.decapsulate('ipfs')
}

Expand Down
4 changes: 0 additions & 4 deletions src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ module.exports = (options, handler) => {
callback = callback || noop
listeningMultiaddr = ma

if (ma.protoNames().includes('ipfs')) {
ma = ma.decapsulate('ipfs')
}

listener._listen(ma.toOptions(), callback)
}

Expand Down
42 changes: 39 additions & 3 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const multiaddr = require('multiaddr')
const multiaddr7 = require('multiaddr7')
const pull = require('pull-stream')
const goodbye = require('pull-goodbye')

Expand Down Expand Up @@ -96,7 +97,7 @@ describe('listen', () => {
})

it('getAddrs on port 0 listen', (done) => {
const addr = multiaddr(`/ip4/127.0.0.1/tcp/0/ws`)
const addr = multiaddr('/ip4/127.0.0.1/tcp/0/ws')
const listener = ws.createListener((conn) => {
})
listener.listen(addr, () => {
Expand All @@ -110,7 +111,7 @@ describe('listen', () => {
})

it('getAddrs from listening on 0.0.0.0', (done) => {
const addr = multiaddr(`/ip4/0.0.0.0/tcp/9003/ws`)
const addr = multiaddr('/ip4/0.0.0.0/tcp/9003/ws')
const listener = ws.createListener((conn) => {
})
listener.listen(addr, () => {
Expand All @@ -123,7 +124,7 @@ describe('listen', () => {
})

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 addr = multiaddr('/ip4/0.0.0.0/tcp/0/ws')
const listener = ws.createListener((conn) => {
})
listener.listen(addr, () => {
Expand All @@ -150,6 +151,21 @@ describe('listen', () => {
})
})
})

it('getAddrs preserves p2p Id', (done) => {
const ma = multiaddr7('/ip4/127.0.0.1/tcp/9090/ws/p2p/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)
})
})
})
})

describe('ip6', () => {
Expand Down Expand Up @@ -198,6 +214,16 @@ describe('listen', () => {
listener.close(done)
})
})

it('listen on addr with /p2p/QmHASH', (done) => {
const ma = multiaddr7('/ip6/::1/tcp/9091/ws/p2p/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')

const listener = ws.createListener((conn) => { })

listener.listen(ma, () => {
listener.close(done)
})
})
})
})

Expand Down Expand Up @@ -343,6 +369,16 @@ describe('filter addrs', () => {
expect(valid[1]).to.deep.equal(ma2)
})

it('should filter correct ipv4 addresses with p2p id', function () {
const ma1 = multiaddr7('/ip4/127.0.0.1/tcp/80/ws/p2p/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
const ma2 = multiaddr7('/ip4/127.0.0.1/tcp/80/wss/p2p/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')

const valid = ws.filter([ma1, ma2])
expect(valid.length).to.equal(2)
expect(valid[0]).to.deep.equal(ma1)
expect(valid[1]).to.deep.equal(ma2)
})

it('should filter correct ipv6 address', function () {
const ma1 = multiaddr('/ip6/::1/tcp/80/ws')
const ma2 = multiaddr('/ip6/::1/tcp/443/wss')
Expand Down

0 comments on commit 996c532

Please sign in to comment.