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

Commit 93ef7c3

Browse files
vasco-santosjacobheun
authored andcommittedJan 24, 2019
fix: ipv6 naming with multiaddr-to-uri package (#81)
* chore: use multiaddr-to-uri package * fix: ipv6 naming with multiaddr-to-uri
1 parent 24541d3 commit 93ef7c3

File tree

4 files changed

+222
-181
lines changed

4 files changed

+222
-181
lines changed
 

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"debug": "^4.1.1",
4545
"interface-connection": "~0.3.2",
4646
"mafmt": "^6.0.4",
47+
"multiaddr-to-uri": "^4.0.1",
4748
"pull-ws": "hugomrdias/pull-ws#fix/bundle-size"
4849
},
4950
"devDependencies": {

‎src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const mafmt = require('mafmt')
55
const withIs = require('class-is')
66
const Connection = require('interface-connection').Connection
77

8-
const maToUrl = require('./ma-to-url')
8+
const toUri = require('multiaddr-to-uri')
99
const debug = require('debug')
1010
const log = debug('libp2p:websockets:dialer')
1111

@@ -18,9 +18,9 @@ class WebSockets {
1818
options = {}
1919
}
2020

21-
callback = callback || function () {}
21+
callback = callback || function () { }
2222

23-
const url = maToUrl(ma)
23+
const url = toUri(ma)
2424
log('dialing %s', url)
2525
const socket = connect(url, {
2626
binary: true,

‎src/ma-to-url.js

-37
This file was deleted.

‎test/node.js

+218-141
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-env mocha */
2-
/* eslint max-nested-callbacks: ["error", 5] */
2+
/* eslint max-nested-callbacks: ["error", 6] */
33
'use strict'
44

55
const chai = require('chai')
@@ -11,7 +11,6 @@ const pull = require('pull-stream')
1111
const goodbye = require('pull-goodbye')
1212

1313
const WS = require('../src')
14-
const maToUrl = require('../src/ma-to-url')
1514

1615
require('./compliance.node')
1716

@@ -23,190 +22,286 @@ describe('instantiate the transport', () => {
2322
})
2423

2524
describe('listen', () => {
26-
let ws
27-
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
25+
describe('ip4', () => {
26+
let ws
27+
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
2828

29-
beforeEach(() => {
30-
ws = new WS()
31-
})
29+
beforeEach(() => {
30+
ws = new WS()
31+
})
3232

33-
it('listen, check for callback', (done) => {
34-
const listener = ws.createListener((conn) => {})
33+
it('listen, check for callback', (done) => {
34+
const listener = ws.createListener((conn) => { })
3535

36-
listener.listen(ma, () => {
37-
listener.close(done)
36+
listener.listen(ma, () => {
37+
listener.close(done)
38+
})
3839
})
39-
})
4040

41-
it('listen, check for listening event', (done) => {
42-
const listener = ws.createListener((conn) => {})
41+
it('listen, check for listening event', (done) => {
42+
const listener = ws.createListener((conn) => { })
4343

44-
listener.on('listening', () => {
45-
listener.close(done)
44+
listener.on('listening', () => {
45+
listener.close(done)
46+
})
47+
48+
listener.listen(ma)
4649
})
4750

48-
listener.listen(ma)
49-
})
51+
it('listen, check for the close event', (done) => {
52+
const listener = ws.createListener((conn) => { })
5053

51-
it('listen, check for the close event', (done) => {
52-
const listener = ws.createListener((conn) => {})
54+
listener.on('listening', () => {
55+
listener.on('close', done)
56+
listener.close()
57+
})
5358

54-
listener.on('listening', () => {
55-
listener.on('close', done)
56-
listener.close()
59+
listener.listen(ma)
5760
})
5861

59-
listener.listen(ma)
60-
})
62+
it('listen on addr with /ipfs/QmHASH', (done) => {
63+
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
6164

62-
it('listen on addr with /ipfs/QmHASH', (done) => {
63-
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
65+
const listener = ws.createListener((conn) => { })
6466

65-
const listener = ws.createListener((conn) => {})
67+
listener.listen(ma, () => {
68+
listener.close(done)
69+
})
70+
})
6671

67-
listener.listen(ma, () => {
68-
listener.close(done)
72+
it.skip('close listener with connections, through timeout', (done) => {
73+
// TODO `ws` closes all anyway, we need to make it not close
74+
// first - https://github.com/diasdavid/simple-websocket-server
6975
})
70-
})
7176

72-
it.skip('close listener with connections, through timeout', (done) => {
73-
// TODO `ws` closes all anyway, we need to make it not close
74-
// first - https://github.com/diasdavid/simple-websocket-server
75-
})
77+
it.skip('listen on port 0', (done) => {
78+
// TODO port 0 not supported yet
79+
})
7680

77-
it.skip('listen on port 0', (done) => {
78-
// TODO port 0 not supported yet
79-
})
80-
it.skip('listen on IPv6 addr', (done) => {
81-
// TODO IPv6 not supported yet
82-
})
81+
it.skip('listen on any Interface', (done) => {
82+
// TODO 0.0.0.0 not supported yet
83+
})
8384

84-
it.skip('listen on any Interface', (done) => {
85-
// TODO 0.0.0.0 not supported yet
86-
})
85+
it('getAddrs', (done) => {
86+
const listener = ws.createListener((conn) => {
87+
})
88+
listener.listen(ma, () => {
89+
listener.getAddrs((err, addrs) => {
90+
expect(err).to.not.exist()
91+
expect(addrs.length).to.equal(1)
92+
expect(addrs[0]).to.deep.equal(ma)
93+
listener.close(done)
94+
})
95+
})
96+
})
8797

88-
it('getAddrs', (done) => {
89-
const listener = ws.createListener((conn) => {
98+
it('getAddrs on port 0 listen', (done) => {
99+
const addr = multiaddr(`/ip4/127.0.0.1/tcp/0/ws`)
100+
const listener = ws.createListener((conn) => {
101+
})
102+
listener.listen(addr, () => {
103+
listener.getAddrs((err, addrs) => {
104+
expect(err).to.not.exist()
105+
expect(addrs.length).to.equal(1)
106+
expect(addrs.map((a) => a.toOptions().port)).to.not.include('0')
107+
listener.close(done)
108+
})
109+
})
90110
})
91-
listener.listen(ma, () => {
92-
listener.getAddrs((err, addrs) => {
93-
expect(err).to.not.exist()
94-
expect(addrs.length).to.equal(1)
95-
expect(addrs[0]).to.deep.equal(ma)
96-
listener.close(done)
111+
112+
it('getAddrs from listening on 0.0.0.0', (done) => {
113+
const addr = multiaddr(`/ip4/0.0.0.0/tcp/9003/ws`)
114+
const listener = ws.createListener((conn) => {
115+
})
116+
listener.listen(addr, () => {
117+
listener.getAddrs((err, addrs) => {
118+
expect(err).to.not.exist()
119+
expect(addrs.map((a) => a.toOptions().host)).to.not.include('0.0.0.0')
120+
listener.close(done)
121+
})
97122
})
98123
})
99-
})
100124

101-
it('getAddrs on port 0 listen', (done) => {
102-
const addr = multiaddr(`/ip4/127.0.0.1/tcp/0/ws`)
103-
const listener = ws.createListener((conn) => {
125+
it('getAddrs from listening on 0.0.0.0 and port 0', (done) => {
126+
const addr = multiaddr(`/ip4/0.0.0.0/tcp/0/ws`)
127+
const listener = ws.createListener((conn) => {
128+
})
129+
listener.listen(addr, () => {
130+
listener.getAddrs((err, addrs) => {
131+
expect(err).to.not.exist()
132+
expect(addrs.map((a) => a.toOptions().host)).to.not.include('0.0.0.0')
133+
expect(addrs.map((a) => a.toOptions().port)).to.not.include('0')
134+
listener.close(done)
135+
})
136+
})
104137
})
105-
listener.listen(addr, () => {
106-
listener.getAddrs((err, addrs) => {
107-
expect(err).to.not.exist()
108-
expect(addrs.length).to.equal(1)
109-
expect(addrs.map((a) => a.toOptions().port)).to.not.include('0')
110-
listener.close(done)
138+
139+
it('getAddrs preserves IPFS Id', (done) => {
140+
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
141+
142+
const listener = ws.createListener((conn) => { })
143+
144+
listener.listen(ma, () => {
145+
listener.getAddrs((err, addrs) => {
146+
expect(err).to.not.exist()
147+
expect(addrs.length).to.equal(1)
148+
expect(addrs[0]).to.deep.equal(ma)
149+
listener.close(done)
150+
})
111151
})
112152
})
113153
})
114154

115-
it('getAddrs from listening on 0.0.0.0', (done) => {
116-
const addr = multiaddr(`/ip4/0.0.0.0/tcp/9003/ws`)
117-
const listener = ws.createListener((conn) => {
155+
describe('ip6', () => {
156+
let ws
157+
const ma = multiaddr('/ip6/::1/tcp/9091/ws')
158+
159+
beforeEach(() => {
160+
ws = new WS()
118161
})
119-
listener.listen(addr, () => {
120-
listener.getAddrs((err, addrs) => {
121-
expect(err).to.not.exist()
122-
expect(addrs.map((a) => a.toOptions().host)).to.not.include('0.0.0.0')
162+
163+
it('listen, check for callback', (done) => {
164+
const listener = ws.createListener((conn) => { })
165+
166+
listener.listen(ma, () => {
123167
listener.close(done)
124168
})
125169
})
126-
})
127170

128-
it('getAddrs from listening on 0.0.0.0 and port 0', (done) => {
129-
const addr = multiaddr(`/ip4/0.0.0.0/tcp/0/ws`)
130-
const listener = ws.createListener((conn) => {
131-
})
132-
listener.listen(addr, () => {
133-
listener.getAddrs((err, addrs) => {
134-
expect(err).to.not.exist()
135-
expect(addrs.map((a) => a.toOptions().host)).to.not.include('0.0.0.0')
136-
expect(addrs.map((a) => a.toOptions().port)).to.not.include('0')
171+
it('listen, check for listening event', (done) => {
172+
const listener = ws.createListener((conn) => { })
173+
174+
listener.on('listening', () => {
137175
listener.close(done)
138176
})
177+
178+
listener.listen(ma)
139179
})
140-
})
141180

142-
it('getAddrs preserves IPFS Id', (done) => {
143-
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
181+
it('listen, check for the close event', (done) => {
182+
const listener = ws.createListener((conn) => { })
144183

145-
const listener = ws.createListener((conn) => {})
184+
listener.on('listening', () => {
185+
listener.on('close', done)
186+
listener.close()
187+
})
146188

147-
listener.listen(ma, () => {
148-
listener.getAddrs((err, addrs) => {
149-
expect(err).to.not.exist()
150-
expect(addrs.length).to.equal(1)
151-
expect(addrs[0]).to.deep.equal(ma)
189+
listener.listen(ma)
190+
})
191+
192+
it('listen on addr with /ipfs/QmHASH', (done) => {
193+
const ma = multiaddr('/ip6/::1/tcp/9091/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
194+
195+
const listener = ws.createListener((conn) => { })
196+
197+
listener.listen(ma, () => {
152198
listener.close(done)
153199
})
154200
})
155201
})
156202
})
157203

158204
describe('dial', () => {
159-
let ws
160-
let listener
161-
const ma = multiaddr('/ip4/127.0.0.1/tcp/9091/ws')
162-
163-
beforeEach((done) => {
164-
ws = new WS()
165-
listener = ws.createListener((conn) => {
166-
pull(conn, conn)
205+
describe('ip4', () => {
206+
let ws
207+
let listener
208+
const ma = multiaddr('/ip4/127.0.0.1/tcp/9091/ws')
209+
210+
beforeEach((done) => {
211+
ws = new WS()
212+
listener = ws.createListener((conn) => {
213+
pull(conn, conn)
214+
})
215+
listener.listen(ma, done)
167216
})
168-
listener.listen(ma, done)
169-
})
170217

171-
afterEach((done) => {
172-
listener.close(done)
173-
})
218+
afterEach((done) => {
219+
listener.close(done)
220+
})
174221

175-
it('dial on IPv4', (done) => {
176-
const conn = ws.dial(ma)
222+
it('dial', (done) => {
223+
const conn = ws.dial(ma)
177224

178-
const s = goodbye({
179-
source: pull.values(['hey']),
180-
sink: pull.collect((err, result) => {
181-
expect(err).to.not.exist()
225+
const s = goodbye({
226+
source: pull.values(['hey']),
227+
sink: pull.collect((err, result) => {
228+
expect(err).to.not.exist()
182229

183-
expect(result).to.be.eql(['hey'])
184-
done()
230+
expect(result).to.be.eql(['hey'])
231+
done()
232+
})
185233
})
234+
235+
pull(s, conn, s)
186236
})
187237

188-
pull(s, conn, s)
189-
})
238+
it('dial with IPFS Id', (done) => {
239+
const ma = multiaddr('/ip4/127.0.0.1/tcp/9091/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
240+
const conn = ws.dial(ma)
241+
242+
const s = goodbye({
243+
source: pull.values(['hey']),
244+
sink: pull.collect((err, result) => {
245+
expect(err).to.not.exist()
190246

191-
it.skip('dial on IPv6', (done) => {
192-
// TODO IPv6 not supported yet
247+
expect(result).to.be.eql(['hey'])
248+
done()
249+
})
250+
})
251+
252+
pull(s, conn, s)
253+
})
193254
})
194255

195-
it('dial on IPv4 with IPFS Id', (done) => {
196-
const ma = multiaddr('/ip4/127.0.0.1/tcp/9091/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
197-
const conn = ws.dial(ma)
256+
describe('ip6', () => {
257+
let ws
258+
let listener
259+
const ma = multiaddr('/ip6/::1/tcp/9091')
198260

199-
const s = goodbye({
200-
source: pull.values(['hey']),
201-
sink: pull.collect((err, result) => {
202-
expect(err).to.not.exist()
261+
beforeEach((done) => {
262+
ws = new WS()
263+
listener = ws.createListener((conn) => {
264+
pull(conn, conn)
265+
})
266+
listener.listen(ma, done)
267+
})
203268

204-
expect(result).to.be.eql(['hey'])
205-
done()
269+
afterEach((done) => {
270+
listener.close(done)
271+
})
272+
273+
it('dial', (done) => {
274+
const conn = ws.dial(ma)
275+
276+
const s = goodbye({
277+
source: pull.values(['hey']),
278+
sink: pull.collect((err, result) => {
279+
expect(err).to.not.exist()
280+
281+
expect(result).to.be.eql(['hey'])
282+
done()
283+
})
206284
})
285+
286+
pull(s, conn, s)
207287
})
208288

209-
pull(s, conn, s)
289+
it('dial with IPFS Id', (done) => {
290+
const ma = multiaddr('/ip6/::1/tcp/9091/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
291+
const conn = ws.dial(ma)
292+
293+
const s = goodbye({
294+
source: pull.values(['hey']),
295+
sink: pull.collect((err, result) => {
296+
expect(err).to.not.exist()
297+
298+
expect(result).to.be.eql(['hey'])
299+
done()
300+
})
301+
})
302+
303+
pull(s, conn, s)
304+
})
210305
})
211306
})
212307

@@ -460,24 +555,6 @@ describe('valid Connection', () => {
460555
})
461556
})
462557

463-
describe('ma-to-url test', function () {
464-
it('should convert ipv4 ma to url', function () {
465-
expect(maToUrl(multiaddr('/ip4/127.0.0.1/ws'))).to.equal('ws://127.0.0.1')
466-
})
467-
468-
it('should convert ipv4 ma with port to url', function () {
469-
expect(maToUrl(multiaddr('/ip4/127.0.0.1/tcp/80/ws'))).to.equal('ws://127.0.0.1:80')
470-
})
471-
472-
it('should convert dns ma to url', function () {
473-
expect(maToUrl(multiaddr('/dns4/ipfs.io/ws'))).to.equal('ws://ipfs.io')
474-
})
475-
476-
it('should convert dns ma with port to url', function () {
477-
expect(maToUrl(multiaddr('/dns4/ipfs.io/tcp/80/ws'))).to.equal('ws://ipfs.io:80')
478-
})
479-
})
480-
481558
describe.skip('turbolence', () => {
482559
it('dialer - emits error on the other end is terminated abruptly', (done) => {
483560
})

0 commit comments

Comments
 (0)
This repository has been archived.