@libp2p/webtransport 3.0.10-4db2f5f5 → 3.0.10-d9159dd5
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/utils/parse-multiaddr.d.ts +3 -2
- package/dist/src/utils/parse-multiaddr.d.ts.map +1 -1
- package/dist/src/utils/parse-multiaddr.js +31 -65
- package/dist/src/utils/parse-multiaddr.js.map +1 -1
- package/package.json +6 -5
- package/src/utils/parse-multiaddr.ts +41 -66
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { type Multiaddr } from '@multiformats/multiaddr';
|
|
2
2
|
import type { PeerId } from '@libp2p/interface/peer-id';
|
|
3
3
|
import type { MultihashDigest } from 'multiformats/hashes/interface';
|
|
4
|
-
export
|
|
4
|
+
export interface ParsedMultiaddr {
|
|
5
5
|
url: string;
|
|
6
6
|
certhashes: MultihashDigest[];
|
|
7
7
|
remotePeer?: PeerId;
|
|
8
|
-
}
|
|
8
|
+
}
|
|
9
|
+
export declare function parseMultiaddr(ma: Multiaddr): ParsedMultiaddr;
|
|
9
10
|
//# sourceMappingURL=parse-multiaddr.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-multiaddr.d.ts","sourceRoot":"","sources":["../../../src/utils/parse-multiaddr.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"parse-multiaddr.d.ts","sourceRoot":"","sources":["../../../src/utils/parse-multiaddr.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,SAAS,EAAa,MAAM,yBAAyB,CAAA;AAGnE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AASpE,MAAM,WAAW,eAAe;IAC9B,GAAG,EAAE,MAAM,CAAA;IACX,UAAU,EAAE,eAAe,EAAE,CAAA;IAC7B,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,wBAAgB,cAAc,CAAE,EAAE,EAAE,SAAS,GAAG,eAAe,CAoC9D"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors';
|
|
1
2
|
import { peerIdFromString } from '@libp2p/peer-id';
|
|
2
3
|
import { protocols } from '@multiformats/multiaddr';
|
|
4
|
+
import { WebTransport } from '@multiformats/multiaddr-matcher';
|
|
3
5
|
import { bases, digest } from 'multiformats/basics';
|
|
4
6
|
// @ts-expect-error - Not easy to combine these types.
|
|
5
7
|
const multibaseDecoder = Object.values(bases).map(b => b.decoder).reduce((d, b) => d.or(b));
|
|
@@ -7,71 +9,35 @@ function decodeCerthashStr(s) {
|
|
|
7
9
|
return digest.decode(multibaseDecoder.decode(s));
|
|
8
10
|
}
|
|
9
11
|
export function parseMultiaddr(ma) {
|
|
12
|
+
if (!WebTransport.matches(ma)) {
|
|
13
|
+
throw new CodeError('Invalid multiaddr, was not a WebTransport address', 'ERR_INVALID_MULTIADDR');
|
|
14
|
+
}
|
|
10
15
|
const parts = ma.stringTuples();
|
|
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
|
-
url: `${state.url}${value ?? ''}`,
|
|
38
|
-
seenHost: true
|
|
39
|
-
};
|
|
40
|
-
case protocols('quic').code:
|
|
41
|
-
case protocols('quic-v1').code:
|
|
42
|
-
case protocols('webtransport').code:
|
|
43
|
-
if (!state.seenHost || !state.seenPort) {
|
|
44
|
-
throw new Error("Invalid multiaddr, Didn't see host and port, but saw quic/webtransport");
|
|
45
|
-
}
|
|
46
|
-
return state;
|
|
47
|
-
case protocols('udp').code:
|
|
48
|
-
if (state.seenPort) {
|
|
49
|
-
throw new Error('Invalid multiaddr, saw port but already saw the port');
|
|
50
|
-
}
|
|
51
|
-
return {
|
|
52
|
-
...state,
|
|
53
|
-
url: `${state.url}:${value ?? ''}`,
|
|
54
|
-
seenPort: true
|
|
55
|
-
};
|
|
56
|
-
case protocols('certhash').code:
|
|
57
|
-
if (!state.seenHost || !state.seenPort) {
|
|
58
|
-
throw new Error('Invalid multiaddr, saw the certhash before seeing the host and port');
|
|
59
|
-
}
|
|
60
|
-
return {
|
|
61
|
-
...state,
|
|
62
|
-
certhashes: state.certhashes.concat([decodeCerthashStr(value ?? '')])
|
|
63
|
-
};
|
|
64
|
-
case protocols('p2p').code:
|
|
65
|
-
return {
|
|
66
|
-
...state,
|
|
67
|
-
remotePeer: peerIdFromString(value ?? '')
|
|
68
|
-
};
|
|
69
|
-
default:
|
|
70
|
-
throw new Error(`unexpected component in multiaddr: ${proto} ${protocols(proto).name} ${value ?? ''} `);
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
// All webtransport urls are https
|
|
74
|
-
{ url: 'https://', seenHost: false, seenPort: false, certhashes: [] });
|
|
75
|
-
return { url, certhashes, remotePeer };
|
|
16
|
+
const certhashes = parts
|
|
17
|
+
.filter(([name, _]) => name === protocols('certhash').code)
|
|
18
|
+
.map(([_, value]) => decodeCerthashStr(value ?? ''));
|
|
19
|
+
// only take the first peer id in the multiaddr as it may be a relay
|
|
20
|
+
const remotePeer = parts
|
|
21
|
+
.filter(([name, _]) => name === protocols('p2p').code)
|
|
22
|
+
.map(([_, value]) => peerIdFromString(value ?? ''))[0];
|
|
23
|
+
const opts = ma.toOptions();
|
|
24
|
+
let host = opts.host;
|
|
25
|
+
if (opts.family === 6 && host?.includes(':')) {
|
|
26
|
+
/**
|
|
27
|
+
* This resolves cases where `new WebTransport()` fails to construct because of an invalid URL being passed.
|
|
28
|
+
*
|
|
29
|
+
* `new URL('https://::1:4001/blah')` will throw a `TypeError: Failed to construct 'URL': Invalid URL`
|
|
30
|
+
* `new URL('https://[::1]:4001/blah')` is valid and will not.
|
|
31
|
+
*
|
|
32
|
+
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
|
|
33
|
+
*/
|
|
34
|
+
host = `[${host}]`;
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
// All webtransport urls are https
|
|
38
|
+
url: `https://${host}:${opts.port}`,
|
|
39
|
+
certhashes,
|
|
40
|
+
remotePeer
|
|
41
|
+
};
|
|
76
42
|
}
|
|
77
43
|
//# sourceMappingURL=parse-multiaddr.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-multiaddr.js","sourceRoot":"","sources":["../../../src/utils/parse-multiaddr.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAkB,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACnE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAInD,sDAAsD;AACtD,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAA;AAE3F,SAAS,iBAAiB,CAAE,CAAS;IACnC,OAAO,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;AAClD,CAAC;
|
|
1
|
+
{"version":3,"file":"parse-multiaddr.js","sourceRoot":"","sources":["../../../src/utils/parse-multiaddr.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAA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AE,CAAC,IAAI,KAAK,SAAS,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC;SACrD,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,gBAAgB,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAExD,MAAM,IAAI,GAAG,EAAE,CAAC,SAAS,EAAE,CAAA;IAC3B,IAAI,IAAI,GAAG,IAAI,CAAC,IAAI,CAAA;IAEpB,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,IAAI,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC,EAAE;QAC5C;;;;;;;WAOG;QACH,IAAI,GAAG,IAAI,IAAI,GAAG,CAAA;KACnB;IAED,OAAO;QACL,kCAAkC;QAClC,GAAG,EAAE,WAAW,IAAI,IAAI,IAAI,CAAC,IAAI,EAAE;QACnC,UAAU;QACV,UAAU;KACX,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/webtransport",
|
|
3
|
-
"version": "3.0.10-
|
|
3
|
+
"version": "3.0.10-d9159dd5",
|
|
4
4
|
"description": "JavaScript implementation of the WebTransport module that libp2p uses and that implements the interface-transport spec",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/transport-webtransport#readme",
|
|
@@ -61,10 +61,11 @@
|
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
63
|
"@chainsafe/libp2p-noise": "^13.0.0",
|
|
64
|
-
"@libp2p/interface": "0.1.2-
|
|
65
|
-
"@libp2p/logger": "3.0.2-
|
|
66
|
-
"@libp2p/peer-id": "3.0.2-
|
|
64
|
+
"@libp2p/interface": "0.1.2-d9159dd5",
|
|
65
|
+
"@libp2p/logger": "3.0.2-d9159dd5",
|
|
66
|
+
"@libp2p/peer-id": "3.0.2-d9159dd5",
|
|
67
67
|
"@multiformats/multiaddr": "^12.1.5",
|
|
68
|
+
"@multiformats/multiaddr-matcher": "^1.0.1",
|
|
68
69
|
"it-stream-types": "^2.0.1",
|
|
69
70
|
"multiformats": "^12.0.1",
|
|
70
71
|
"uint8arraylist": "^2.4.3",
|
|
@@ -72,7 +73,7 @@
|
|
|
72
73
|
},
|
|
73
74
|
"devDependencies": {
|
|
74
75
|
"aegir": "^40.0.8",
|
|
75
|
-
"libp2p": "0.46.10-
|
|
76
|
+
"libp2p": "0.46.10-d9159dd5",
|
|
76
77
|
"p-defer": "^4.0.0"
|
|
77
78
|
},
|
|
78
79
|
"browser": {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { CodeError } from '@libp2p/interface/errors'
|
|
1
2
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
2
3
|
import { type Multiaddr, protocols } from '@multiformats/multiaddr'
|
|
4
|
+
import { WebTransport } from '@multiformats/multiaddr-matcher'
|
|
3
5
|
import { bases, digest } from 'multiformats/basics'
|
|
4
6
|
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
5
7
|
import type { MultihashDigest } from 'multiformats/hashes/interface'
|
|
@@ -11,73 +13,46 @@ function decodeCerthashStr (s: string): MultihashDigest {
|
|
|
11
13
|
return digest.decode(multibaseDecoder.decode(s))
|
|
12
14
|
}
|
|
13
15
|
|
|
14
|
-
export
|
|
16
|
+
export interface ParsedMultiaddr {
|
|
17
|
+
url: string
|
|
18
|
+
certhashes: MultihashDigest[]
|
|
19
|
+
remotePeer?: PeerId
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function parseMultiaddr (ma: Multiaddr): ParsedMultiaddr {
|
|
23
|
+
if (!WebTransport.matches(ma)) {
|
|
24
|
+
throw new CodeError('Invalid multiaddr, was not a WebTransport address', 'ERR_INVALID_MULTIADDR')
|
|
25
|
+
}
|
|
26
|
+
|
|
15
27
|
const parts = ma.stringTuples()
|
|
28
|
+
const certhashes = parts
|
|
29
|
+
.filter(([name, _]) => name === protocols('certhash').code)
|
|
30
|
+
.map(([_, value]) => decodeCerthashStr(value ?? ''))
|
|
31
|
+
|
|
32
|
+
// only take the first peer id in the multiaddr as it may be a relay
|
|
33
|
+
const remotePeer = parts
|
|
34
|
+
.filter(([name, _]) => name === protocols('p2p').code)
|
|
35
|
+
.map(([_, value]) => peerIdFromString(value ?? ''))[0]
|
|
36
|
+
|
|
37
|
+
const opts = ma.toOptions()
|
|
38
|
+
let host = opts.host
|
|
16
39
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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: [] })
|
|
40
|
+
if (opts.family === 6 && host?.includes(':')) {
|
|
41
|
+
/**
|
|
42
|
+
* This resolves cases where `new WebTransport()` fails to construct because of an invalid URL being passed.
|
|
43
|
+
*
|
|
44
|
+
* `new URL('https://::1:4001/blah')` will throw a `TypeError: Failed to construct 'URL': Invalid URL`
|
|
45
|
+
* `new URL('https://[::1]:4001/blah')` is valid and will not.
|
|
46
|
+
*
|
|
47
|
+
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
|
|
48
|
+
*/
|
|
49
|
+
host = `[${host}]`
|
|
50
|
+
}
|
|
81
51
|
|
|
82
|
-
return {
|
|
52
|
+
return {
|
|
53
|
+
// All webtransport urls are https
|
|
54
|
+
url: `https://${host}:${opts.port}`,
|
|
55
|
+
certhashes,
|
|
56
|
+
remotePeer
|
|
57
|
+
}
|
|
83
58
|
}
|