-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: replace mafmt with @multiformats/multiaddr-matcher (#2791)
Switches to the more modern library.
- Loading branch information
1 parent
2bbb5e1
commit a5cd8cf
Showing
3 changed files
with
6 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
} |