@libp2p/webtransport 3.0.3-32212959 → 3.0.3-5e85154b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.min.js +9 -9
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +5 -268
- package/dist/src/index.js.map +1 -1
- package/dist/src/stream.d.ts +3 -0
- package/dist/src/stream.d.ts.map +1 -0
- package/dist/src/stream.js +158 -0
- package/dist/src/stream.js.map +1 -0
- package/dist/src/utils/inert-duplex.d.ts +3 -0
- package/dist/src/utils/inert-duplex.d.ts.map +1 -0
- package/dist/src/utils/inert-duplex.js +20 -0
- package/dist/src/utils/inert-duplex.js.map +1 -0
- package/dist/src/utils/is-subset.d.ts +6 -0
- package/dist/src/utils/is-subset.d.ts.map +1 -0
- package/dist/src/utils/is-subset.js +12 -0
- package/dist/src/utils/is-subset.js.map +1 -0
- package/dist/src/utils/parse-multiaddr.d.ts +9 -0
- package/dist/src/utils/parse-multiaddr.d.ts.map +1 -0
- package/dist/src/utils/parse-multiaddr.js +77 -0
- package/dist/src/utils/parse-multiaddr.js.map +1 -0
- package/package.json +7 -6
- package/src/index.ts +7 -300
- package/src/stream.ts +183 -0
- package/src/utils/inert-duplex.ts +21 -0
- package/src/utils/is-subset.ts +12 -0
- package/src/utils/parse-multiaddr.ts +83 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { peerIdFromString } from '@libp2p/peer-id'
|
|
2
|
+
import { type Multiaddr, protocols } from '@multiformats/multiaddr'
|
|
3
|
+
import { bases, digest } from 'multiformats/basics'
|
|
4
|
+
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
5
|
+
import type { MultihashDigest } from 'multiformats/hashes/interface'
|
|
6
|
+
|
|
7
|
+
// @ts-expect-error - Not easy to combine these types.
|
|
8
|
+
const multibaseDecoder = Object.values(bases).map(b => b.decoder).reduce((d, b) => d.or(b))
|
|
9
|
+
|
|
10
|
+
function decodeCerthashStr (s: string): MultihashDigest {
|
|
11
|
+
return digest.decode(multibaseDecoder.decode(s))
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function parseMultiaddr (ma: Multiaddr): { url: string, certhashes: MultihashDigest[], remotePeer?: PeerId } {
|
|
15
|
+
const parts = ma.stringTuples()
|
|
16
|
+
|
|
17
|
+
// This is simpler to have inline than extract into a separate function
|
|
18
|
+
// eslint-disable-next-line complexity
|
|
19
|
+
const { url, certhashes, remotePeer } = parts.reduce((state: { url: string, certhashes: MultihashDigest[], seenHost: boolean, seenPort: boolean, remotePeer?: PeerId }, [proto, value]) => {
|
|
20
|
+
switch (proto) {
|
|
21
|
+
case protocols('ip6').code:
|
|
22
|
+
// @ts-expect-error - ts error on switch fallthrough
|
|
23
|
+
case protocols('dns6').code:
|
|
24
|
+
if (value?.includes(':') === true) {
|
|
25
|
+
/**
|
|
26
|
+
* This resolves cases where `new globalThis.WebTransport` fails to construct because of an invalid URL being passed.
|
|
27
|
+
*
|
|
28
|
+
* `new URL('https://::1:4001/blah')` will throw a `TypeError: Failed to construct 'URL': Invalid URL`
|
|
29
|
+
* `new URL('https://[::1]:4001/blah')` is valid and will not.
|
|
30
|
+
*
|
|
31
|
+
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
|
|
32
|
+
*/
|
|
33
|
+
value = `[${value}]`
|
|
34
|
+
}
|
|
35
|
+
// eslint-disable-next-line no-fallthrough
|
|
36
|
+
case protocols('ip4').code:
|
|
37
|
+
case protocols('dns4').code:
|
|
38
|
+
if (state.seenHost || state.seenPort) {
|
|
39
|
+
throw new Error('Invalid multiaddr, saw host and already saw the host or port')
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
...state,
|
|
43
|
+
url: `${state.url}${value ?? ''}`,
|
|
44
|
+
seenHost: true
|
|
45
|
+
}
|
|
46
|
+
case protocols('quic').code:
|
|
47
|
+
case protocols('quic-v1').code:
|
|
48
|
+
case protocols('webtransport').code:
|
|
49
|
+
if (!state.seenHost || !state.seenPort) {
|
|
50
|
+
throw new Error("Invalid multiaddr, Didn't see host and port, but saw quic/webtransport")
|
|
51
|
+
}
|
|
52
|
+
return state
|
|
53
|
+
case protocols('udp').code:
|
|
54
|
+
if (state.seenPort) {
|
|
55
|
+
throw new Error('Invalid multiaddr, saw port but already saw the port')
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
...state,
|
|
59
|
+
url: `${state.url}:${value ?? ''}`,
|
|
60
|
+
seenPort: true
|
|
61
|
+
}
|
|
62
|
+
case protocols('certhash').code:
|
|
63
|
+
if (!state.seenHost || !state.seenPort) {
|
|
64
|
+
throw new Error('Invalid multiaddr, saw the certhash before seeing the host and port')
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
...state,
|
|
68
|
+
certhashes: state.certhashes.concat([decodeCerthashStr(value ?? '')])
|
|
69
|
+
}
|
|
70
|
+
case protocols('p2p').code:
|
|
71
|
+
return {
|
|
72
|
+
...state,
|
|
73
|
+
remotePeer: peerIdFromString(value ?? '')
|
|
74
|
+
}
|
|
75
|
+
default:
|
|
76
|
+
throw new Error(`unexpected component in multiaddr: ${proto} ${protocols(proto).name} ${value ?? ''} `)
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
// All webtransport urls are https
|
|
80
|
+
{ url: 'https://', seenHost: false, seenPort: false, certhashes: [] })
|
|
81
|
+
|
|
82
|
+
return { url, certhashes, remotePeer }
|
|
83
|
+
}
|