@libp2p/webtransport 3.0.3 → 3.0.4-4ef9c79c
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/README.md +2 -0
- package/dist/index.min.js +17 -14
- package/dist/src/index.d.ts +0 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +106 -311
- 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 +9 -8
- package/src/index.ts +127 -343
- 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
- package/dist/typedoc-urls.json +0 -6
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Duplex, Source } from 'it-stream-types'
|
|
2
|
+
|
|
3
|
+
// Duplex that does nothing. Needed to fulfill the interface
|
|
4
|
+
export function inertDuplex (): Duplex<any, any, any> {
|
|
5
|
+
return {
|
|
6
|
+
source: {
|
|
7
|
+
[Symbol.asyncIterator] () {
|
|
8
|
+
return {
|
|
9
|
+
async next () {
|
|
10
|
+
// This will never resolve
|
|
11
|
+
return new Promise(() => { })
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
sink: async (source: Source<any>) => {
|
|
17
|
+
// This will never resolve
|
|
18
|
+
return new Promise(() => { })
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Determines if `maybeSubset` is a subset of `set`. This means that all byte
|
|
5
|
+
* arrays in `maybeSubset` are present in `set`.
|
|
6
|
+
*/
|
|
7
|
+
export function isSubset (set: Uint8Array[], maybeSubset: Uint8Array[]): boolean {
|
|
8
|
+
const intersection = maybeSubset.filter(byteArray => {
|
|
9
|
+
return Boolean(set.find((otherByteArray: Uint8Array) => uint8ArrayEquals(byteArray, otherByteArray)))
|
|
10
|
+
})
|
|
11
|
+
return (intersection.length === maybeSubset.length)
|
|
12
|
+
}
|
|
@@ -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
|
+
}
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"WebTransportComponents": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webtransport.WebTransportComponents.html",
|
|
3
|
-
"WebTransportInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_webtransport.WebTransportInit.html",
|
|
4
|
-
"isSubset": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webtransport.isSubset.html",
|
|
5
|
-
"webTransport": "https://libp2p.github.io/js-libp2p/functions/_libp2p_webtransport.webTransport.html"
|
|
6
|
-
}
|