From a5cd8cfbe7d150659012879239ef2ef4ac3143c9 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Mon, 28 Oct 2024 18:18:08 +0000 Subject: [PATCH] fix: replace mafmt with @multiformats/multiaddr-matcher (#2791) Switches to the more modern library. --- packages/transport-websockets/package.json | 2 +- .../transport-websockets/src/constants.ts | 8 --- packages/transport-websockets/src/filters.ts | 50 ++----------------- 3 files changed, 6 insertions(+), 54 deletions(-) diff --git a/packages/transport-websockets/package.json b/packages/transport-websockets/package.json index 71cb91c6c0..9c75fae597 100644 --- a/packages/transport-websockets/package.json +++ b/packages/transport-websockets/package.json @@ -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", diff --git a/packages/transport-websockets/src/constants.ts b/packages/transport-websockets/src/constants.ts index 93b937c0d3..5bed2c6a62 100644 --- a/packages/transport-websockets/src/constants.ts +++ b/packages/transport-websockets/src/constants.ts @@ -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 diff --git a/packages/transport-websockets/src/filters.ts b/packages/transport-websockets/src/filters.ts index 8536e7c97a..3526cf477a 100644 --- a/packages/transport-websockets/src/filters.ts +++ b/packages/transport-websockets/src/filters.ts @@ -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)) }) }