|
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' |
9 | 2 | import type { Multiaddr } from '@multiformats/multiaddr'
|
10 | 3 |
|
11 | 4 | export function all (multiaddrs: Multiaddr[]): Multiaddr[] {
|
12 | 5 | 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) |
21 | 7 | })
|
22 | 8 | }
|
23 | 9 |
|
24 | 10 | export function wss (multiaddrs: Multiaddr[]): Multiaddr[] {
|
25 | 11 | 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) |
33 | 13 | })
|
34 | 14 | }
|
35 | 15 |
|
36 | 16 | export function dnsWss (multiaddrs: Multiaddr[]): Multiaddr[] {
|
37 | 17 | 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) |
46 | 19 | })
|
47 | 20 | }
|
48 | 21 |
|
49 | 22 | export function dnsWsOrWss (multiaddrs: Multiaddr[]): Multiaddr[] {
|
50 | 23 | 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)) |
65 | 25 | })
|
66 | 26 | }
|
0 commit comments