Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace mafmt with @multiformats/multiaddr-matcher #2791

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
})
}
Loading