This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathfilters.ts
66 lines (53 loc) · 1.67 KB
/
filters.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import * as mafmt from '@multiformats/mafmt'
import {
CODE_CIRCUIT,
CODE_P2P,
CODE_TCP,
CODE_WS,
CODE_WSS
} from './constants.js'
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)
})
}
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)
})
}
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))
})
}
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))
})
}