Skip to content

Commit a5cd8cf

Browse files
authored
fix: replace mafmt with @multiformats/multiaddr-matcher (#2791)
Switches to the more modern library.
1 parent 2bbb5e1 commit a5cd8cf

File tree

3 files changed

+6
-54
lines changed

3 files changed

+6
-54
lines changed

packages/transport-websockets/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@
7676
"dependencies": {
7777
"@libp2p/interface": "^2.2.0",
7878
"@libp2p/utils": "^6.1.3",
79-
"@multiformats/mafmt": "^12.1.6",
8079
"@multiformats/multiaddr": "^12.2.3",
80+
"@multiformats/multiaddr-matcher": "^1.4.0",
8181
"@multiformats/multiaddr-to-uri": "^10.0.1",
8282
"@types/ws": "^8.5.10",
8383
"it-ws": "^6.1.1",
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
1-
// p2p multi-address code
2-
export const CODE_P2P = 421
3-
export const CODE_CIRCUIT = 290
4-
5-
export const CODE_TCP = 6
6-
export const CODE_WS = 477
7-
export const CODE_WSS = 478
8-
91
// Time to wait for a connection to close gracefully before destroying it manually
102
export const CLOSE_TIMEOUT = 500
Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,26 @@
1-
import * as mafmt from '@multiformats/mafmt'
2-
import {
3-
CODE_CIRCUIT,
4-
CODE_P2P,
5-
CODE_TCP,
6-
CODE_WS,
7-
CODE_WSS
8-
} from './constants.js'
1+
import { WebSocketsSecure, WebSockets, DNS } from '@multiformats/multiaddr-matcher'
92
import type { Multiaddr } from '@multiformats/multiaddr'
103

114
export function all (multiaddrs: Multiaddr[]): Multiaddr[] {
125
return multiaddrs.filter((ma) => {
13-
if (ma.protoCodes().includes(CODE_CIRCUIT)) {
14-
return false
15-
}
16-
17-
const testMa = ma.decapsulateCode(CODE_P2P)
18-
19-
return mafmt.WebSockets.matches(testMa) ||
20-
mafmt.WebSocketsSecure.matches(testMa)
6+
return WebSocketsSecure.exactMatch(ma) || WebSockets.exactMatch(ma)
217
})
228
}
239

2410
export function wss (multiaddrs: Multiaddr[]): Multiaddr[] {
2511
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)
12+
return WebSocketsSecure.exactMatch(ma)
3313
})
3414
}
3515

3616
export function dnsWss (multiaddrs: Multiaddr[]): Multiaddr[] {
3717
return multiaddrs.filter((ma) => {
38-
if (ma.protoCodes().includes(CODE_CIRCUIT)) {
39-
return false
40-
}
41-
42-
const testMa = ma.decapsulateCode(CODE_P2P)
43-
44-
return mafmt.WebSocketsSecure.matches(testMa) &&
45-
mafmt.DNS.matches(testMa.decapsulateCode(CODE_TCP).decapsulateCode(CODE_WSS))
18+
return DNS.matches(ma) && WebSocketsSecure.exactMatch(ma)
4619
})
4720
}
4821

4922
export function dnsWsOrWss (multiaddrs: Multiaddr[]): Multiaddr[] {
5023
return multiaddrs.filter((ma) => {
51-
if (ma.protoCodes().includes(CODE_CIRCUIT)) {
52-
return false
53-
}
54-
55-
const testMa = ma.decapsulateCode(CODE_P2P)
56-
57-
// WS
58-
if (mafmt.WebSockets.matches(testMa)) {
59-
return mafmt.DNS.matches(testMa.decapsulateCode(CODE_TCP).decapsulateCode(CODE_WS))
60-
}
61-
62-
// WSS
63-
return mafmt.WebSocketsSecure.matches(testMa) &&
64-
mafmt.DNS.matches(testMa.decapsulateCode(CODE_TCP).decapsulateCode(CODE_WSS))
24+
return DNS.matches(ma) && (WebSocketsSecure.exactMatch(ma) || WebSockets.exactMatch(ma))
6525
})
6626
}

0 commit comments

Comments
 (0)