Skip to content

Commit

Permalink
fix: replace mafmt with @multiformats/multiaddr-matcher (#2791)
Browse files Browse the repository at this point in the history
Switches to the more modern library.
  • Loading branch information
achingbrain authored Oct 28, 2024
1 parent 2bbb5e1 commit a5cd8cf
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/transport-websockets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
"dependencies": {
"@libp2p/interface": "^2.2.0",
"@libp2p/utils": "^6.1.3",
"@multiformats/mafmt": "^12.1.6",
"@multiformats/multiaddr": "^12.2.3",
"@multiformats/multiaddr-matcher": "^1.4.0",
"@multiformats/multiaddr-to-uri": "^10.0.1",
"@types/ws": "^8.5.10",
"it-ws": "^6.1.1",
Expand Down
8 changes: 0 additions & 8 deletions packages/transport-websockets/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
// p2p multi-address code
export const CODE_P2P = 421
export const CODE_CIRCUIT = 290

export const CODE_TCP = 6
export const CODE_WS = 477
export const CODE_WSS = 478

// Time to wait for a connection to close gracefully before destroying it manually
export const CLOSE_TIMEOUT = 500
50 changes: 5 additions & 45 deletions packages/transport-websockets/src/filters.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,26 @@
import * as mafmt from '@multiformats/mafmt'
import {
CODE_CIRCUIT,
CODE_P2P,
CODE_TCP,
CODE_WS,
CODE_WSS
} from './constants.js'
import { WebSocketsSecure, WebSockets, DNS } from '@multiformats/multiaddr-matcher'
import type { Multiaddr } from '@multiformats/multiaddr'

export function all (multiaddrs: Multiaddr[]): Multiaddr[] {
return multiaddrs.filter((ma) => {
if (ma.protoCodes().includes(CODE_CIRCUIT)) {
return false
}

const testMa = ma.decapsulateCode(CODE_P2P)

return mafmt.WebSockets.matches(testMa) ||
mafmt.WebSocketsSecure.matches(testMa)
return WebSocketsSecure.exactMatch(ma) || WebSockets.exactMatch(ma)
})
}

export function wss (multiaddrs: Multiaddr[]): Multiaddr[] {
return multiaddrs.filter((ma) => {
if (ma.protoCodes().includes(CODE_CIRCUIT)) {
return false
}

const testMa = ma.decapsulateCode(CODE_P2P)

return mafmt.WebSocketsSecure.matches(testMa)
return WebSocketsSecure.exactMatch(ma)
})
}

export function dnsWss (multiaddrs: Multiaddr[]): Multiaddr[] {
return multiaddrs.filter((ma) => {
if (ma.protoCodes().includes(CODE_CIRCUIT)) {
return false
}

const testMa = ma.decapsulateCode(CODE_P2P)

return mafmt.WebSocketsSecure.matches(testMa) &&
mafmt.DNS.matches(testMa.decapsulateCode(CODE_TCP).decapsulateCode(CODE_WSS))
return DNS.matches(ma) && WebSocketsSecure.exactMatch(ma)
})
}

export function dnsWsOrWss (multiaddrs: Multiaddr[]): Multiaddr[] {
return multiaddrs.filter((ma) => {
if (ma.protoCodes().includes(CODE_CIRCUIT)) {
return false
}

const testMa = ma.decapsulateCode(CODE_P2P)

// WS
if (mafmt.WebSockets.matches(testMa)) {
return mafmt.DNS.matches(testMa.decapsulateCode(CODE_TCP).decapsulateCode(CODE_WS))
}

// WSS
return mafmt.WebSocketsSecure.matches(testMa) &&
mafmt.DNS.matches(testMa.decapsulateCode(CODE_TCP).decapsulateCode(CODE_WSS))
return DNS.matches(ma) && (WebSocketsSecure.exactMatch(ma) || WebSockets.exactMatch(ma))
})
}

0 comments on commit a5cd8cf

Please sign in to comment.