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

Commit 434d44c

Browse files
authored
fix: Only filter by wss not dns (#218)
Browsers can dial IP addresses just fine. There's no need for this to be only DNS addresses.
1 parent e54590f commit 434d44c

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/filters.ts

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ export function all (multiaddrs: Multiaddr[]) {
2121
})
2222
}
2323

24+
export function wss (multiaddrs: Multiaddr[]) {
25+
return multiaddrs.filter((ma) => {
26+
if (ma.protoCodes().includes(CODE_CIRCUIT)) {
27+
return false
28+
}
29+
30+
const testMa = ma.decapsulateCode(CODE_P2P)
31+
32+
return mafmt.WebSocketsSecure.matches(testMa)
33+
})
34+
}
35+
2436
export function dnsWss (multiaddrs: Multiaddr[]) {
2537
return multiaddrs.filter((ma) => {
2638
if (ma.protoCodes().includes(CODE_CIRCUIT)) {

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class WebSockets implements Transport {
133133

134134
// Browser
135135
if (isBrowser || isWebWorker) {
136-
return filters.dnsWss(multiaddrs)
136+
return filters.wss(multiaddrs)
137137
}
138138

139139
return filters.all(multiaddrs)

test/browser.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('libp2p-websockets', () => {
4040
expect(res[0].subarray()).to.equalBytes(data)
4141
})
4242

43-
it('should filter out no DNS websocket addresses', function () {
43+
it('should filter out no wss websocket addresses', function () {
4444
const ma1 = multiaddr('/ip4/127.0.0.1/tcp/80/ws')
4545
const ma2 = multiaddr('/ip4/127.0.0.1/tcp/443/wss')
4646
const ma3 = multiaddr('/ip6/::1/tcp/80/ws')
@@ -49,7 +49,8 @@ describe('libp2p-websockets', () => {
4949
const valid = ws.filter([ma1, ma2, ma3, ma4])
5050

5151
if (isBrowser || isWebWorker) {
52-
expect(valid.length).to.equal(0)
52+
expect(valid.length).to.equal(2)
53+
expect(valid).to.deep.equal([ma2, ma4])
5354
} else {
5455
expect(valid.length).to.equal(4)
5556
}

0 commit comments

Comments
 (0)