-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathprotocols-table.ts
98 lines (90 loc) · 2.38 KB
/
protocols-table.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import type { Protocol } from './index.js'
const V = -1
export const names: Record<string, Protocol> = {}
export const codes: Record<number, Protocol> = {}
export const table: Array<[number, number, string, boolean?, boolean?]> = [
[4, 32, 'ip4'],
[6, 16, 'tcp'],
[33, 16, 'dccp'],
[41, 128, 'ip6'],
[42, V, 'ip6zone'],
[43, 8, 'ipcidr'],
[53, V, 'dns', true],
[54, V, 'dns4', true],
[55, V, 'dns6', true],
[56, V, 'dnsaddr', true],
[132, 16, 'sctp'],
[273, 16, 'udp'],
[275, 0, 'p2p-webrtc-star'],
[276, 0, 'p2p-webrtc-direct'],
[277, 0, 'p2p-stardust'],
[280, 0, 'webrtc-direct'],
[281, 0, 'webrtc'],
[290, 0, 'p2p-circuit'],
[301, 0, 'udt'],
[302, 0, 'utp'],
[400, V, 'unix', false, true],
// `ipfs` is added before `p2p` for legacy support.
// All text representations will default to `p2p`, but `ipfs` will
// still be supported
[421, V, 'ipfs'],
// `p2p` is the preferred name for 421, and is now the default
[421, V, 'p2p'],
[443, 0, 'https'],
[444, 96, 'onion'],
[445, 296, 'onion3'],
[446, V, 'garlic64'],
[448, 0, 'tls'],
[449, V, 'sni'],
[460, 0, 'quic'],
[461, 0, 'quic-v1'],
[465, 0, 'webtransport'],
[466, V, 'certhash'],
[477, 0, 'ws'],
[478, 0, 'wss'],
[479, 0, 'p2p-websocket-star'],
[480, 0, 'http'],
[777, V, 'memory'],
[1883, 0, 'mqtt']
]
// populate tables
table.forEach(row => {
const proto = createProtocol(...row)
codes[proto.code] = proto
names[proto.name] = proto
})
export function createProtocol (code: number, size: number, name: string, resolvable?: any, path?: any): Protocol {
return {
code,
size,
name,
resolvable: Boolean(resolvable),
path: Boolean(path)
}
}
/**
* For the passed proto string or number, return a {@link Protocol}
*
* @example
*
* ```js
* import { protocol } from '@multiformats/multiaddr'
*
* console.info(protocol(4))
* // { code: 4, size: 32, name: 'ip4', resolvable: false, path: false }
* ```
*/
export function getProtocol (proto: number | string): Protocol {
if (typeof proto === 'number') {
if (codes[proto] != null) {
return codes[proto]
}
throw new Error(`no protocol with code: ${proto}`)
} else if (typeof proto === 'string') {
if (names[proto] != null) {
return names[proto]
}
throw new Error(`no protocol with name: ${proto}`)
}
throw new Error(`invalid protocol id type: ${typeof proto}`)
}