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

Commit ee47570

Browse files
authoredJun 8, 2021
fix: listener get addrs with wss (#130)
1 parent 237fa15 commit ee47570

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed
 

‎src/listener.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const EventEmitter = require('events')
44
const os = require('os')
5-
const { Multiaddr } = require('multiaddr')
5+
const { Multiaddr, protocols } = require('multiaddr')
66
const { createServer } = require('it-ws')
77
const debug = require('debug')
88
const log = debug('libp2p:websockets:listener')
@@ -63,12 +63,14 @@ module.exports = ({ handler, upgrader }, options = {}) => {
6363
}
6464

6565
const ipfsId = listeningMultiaddr.getPeerId()
66+
const protos = listeningMultiaddr.protos()
6667

6768
// Because TCP will only return the IPv6 version
6869
// we need to capture from the passed multiaddr
69-
if (listeningMultiaddr.toString().indexOf('ip4') !== -1) {
70+
if (protos.some(proto => proto.code === protocols('ip4').code)) {
71+
const wsProto = protos.some(proto => proto.code === protocols('ws').code) ? '/ws' : '/wss'
7072
let m = listeningMultiaddr.decapsulate('tcp')
71-
m = m.encapsulate('/tcp/' + address.port + '/ws')
73+
m = m.encapsulate('/tcp/' + address.port + wsProto)
7274
if (listeningMultiaddr.getPeerId()) {
7375
m = m.encapsulate('/p2p/' + ipfsId)
7476
}

‎test/node.js

+7
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,13 @@ describe('dial', () => {
311311

312312
afterEach(() => listener.close())
313313

314+
it('should listen on wss address', () => {
315+
const addrs = listener.getAddrs()
316+
317+
expect(addrs).to.have.lengthOf(1)
318+
expect(ma.equals(addrs[0])).to.eql(true)
319+
})
320+
314321
it('dial', async () => {
315322
const conn = await ws.dial(ma, { websocket: { rejectUnauthorized: false } })
316323
const s = goodbye({ source: ['hey'], sink: collect })

0 commit comments

Comments
 (0)
This repository has been archived.