@libp2p/webtransport 5.0.51 → 6.0.0-55b7e5fea

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.
Files changed (48) hide show
  1. package/dist/index.min.js +11 -12
  2. package/dist/index.min.js.map +4 -4
  3. package/dist/src/index.d.ts +1 -4
  4. package/dist/src/index.d.ts.map +1 -1
  5. package/dist/src/index.js +32 -63
  6. package/dist/src/index.js.map +1 -1
  7. package/dist/src/listener.d.ts +0 -1
  8. package/dist/src/listener.d.ts.map +1 -1
  9. package/dist/src/listener.js.map +1 -1
  10. package/dist/src/muxer.d.ts +2 -5
  11. package/dist/src/muxer.d.ts.map +1 -1
  12. package/dist/src/muxer.js +44 -74
  13. package/dist/src/muxer.js.map +1 -1
  14. package/dist/src/session-to-conn.d.ts +11 -0
  15. package/dist/src/session-to-conn.d.ts.map +1 -0
  16. package/dist/src/session-to-conn.js +35 -0
  17. package/dist/src/session-to-conn.js.map +1 -0
  18. package/dist/src/stream.d.ts +21 -2
  19. package/dist/src/stream.d.ts.map +1 -1
  20. package/dist/src/stream.js +75 -47
  21. package/dist/src/stream.js.map +1 -1
  22. package/dist/src/utils/parse-multiaddr.d.ts +1 -1
  23. package/dist/src/utils/parse-multiaddr.d.ts.map +1 -1
  24. package/dist/src/utils/parse-multiaddr.js +17 -11
  25. package/dist/src/utils/parse-multiaddr.js.map +1 -1
  26. package/dist/src/utils/webtransport-message-stream.d.ts +18 -0
  27. package/dist/src/utils/webtransport-message-stream.d.ts.map +1 -0
  28. package/dist/src/utils/webtransport-message-stream.js +49 -0
  29. package/dist/src/utils/webtransport-message-stream.js.map +1 -0
  30. package/package.json +20 -21
  31. package/src/index.ts +36 -74
  32. package/src/listener.ts +0 -1
  33. package/src/muxer.ts +60 -84
  34. package/src/session-to-conn.ts +50 -0
  35. package/src/stream.ts +88 -55
  36. package/src/utils/parse-multiaddr.ts +20 -12
  37. package/src/utils/webtransport-message-stream.ts +69 -0
  38. package/dist/src/constants.d.ts +0 -2
  39. package/dist/src/constants.d.ts.map +0 -1
  40. package/dist/src/constants.js +0 -2
  41. package/dist/src/constants.js.map +0 -1
  42. package/dist/src/utils/inert-duplex.d.ts +0 -3
  43. package/dist/src/utils/inert-duplex.d.ts.map +0 -1
  44. package/dist/src/utils/inert-duplex.js +0 -20
  45. package/dist/src/utils/inert-duplex.js.map +0 -1
  46. package/dist/typedoc-urls.json +0 -14
  47. package/src/constants.ts +0 -1
  48. package/src/utils/inert-duplex.ts +0 -21
@@ -1,67 +1,96 @@
1
- import { AbstractStream } from '@libp2p/utils/abstract-stream';
1
+ import { AbstractStream } from '@libp2p/utils';
2
2
  import { raceSignal } from 'race-signal';
3
- import { Uint8ArrayList } from 'uint8arraylist';
4
- class WebTransportStream extends AbstractStream {
3
+ export class WebTransportStream extends AbstractStream {
5
4
  writer;
6
5
  reader;
7
6
  constructor(init) {
8
7
  super(init);
9
- this.writer = init.bidiStream.writable.getWriter();
10
- this.reader = init.bidiStream.readable.getReader();
8
+ this.writer = init.stream.writable.getWriter();
9
+ this.reader = init.stream.readable.getReader();
10
+ void this.writer.closed
11
+ .then(() => {
12
+ this.log('writer closed gracefully');
13
+ })
14
+ .catch((err) => {
15
+ // chrome/ff send different messages
16
+ if (err.message.includes('STOP_SENDING') || err.message.includes('StopSending')) {
17
+ // err.code === 0 so we may be able to use this to detect remote close
18
+ // read instead?
19
+ this.onRemoteCloseRead();
20
+ }
21
+ else if (err.message.includes('RESET_STREAM') || err.message.includes('Reset')) {
22
+ this.onRemoteReset();
23
+ }
24
+ else {
25
+ this.log('writer close promise rejected - %e', err);
26
+ }
27
+ });
28
+ this.readData();
29
+ }
30
+ readData() {
11
31
  Promise.resolve()
12
32
  .then(async () => {
13
33
  while (true) {
14
34
  const result = await this.reader.read();
35
+ if (result.value != null) {
36
+ this.onData(result.value);
37
+ }
15
38
  if (result.done) {
16
- init.log('remote closed write');
39
+ this.log('remote closed write');
40
+ this.onRemoteCloseWrite();
17
41
  return;
18
42
  }
19
- if (result.value != null) {
20
- this.sourcePush(new Uint8ArrayList(result.value));
43
+ if (this.readStatus === 'paused') {
44
+ break;
21
45
  }
22
46
  }
23
47
  })
24
48
  .catch(err => {
25
- init.log.error('error reading from stream', err);
26
49
  this.abort(err);
27
50
  })
28
51
  .finally(() => {
29
- this.remoteCloseWrite();
30
- });
31
- void this.writer.closed
32
- .then(() => {
33
- init.log('writer closed');
34
- })
35
- .catch((err) => {
36
- init.log('writer close promise rejected', err);
37
- })
38
- .finally(() => {
39
- this.remoteCloseRead();
52
+ this.reader.releaseLock();
40
53
  });
41
54
  }
42
- sendNewStream(options) {
43
- // this is a no-op
44
- }
45
- async sendData(buf, options) {
46
- for (const chunk of buf) {
47
- this.log('sendData waiting for writer to be ready');
48
- await raceSignal(this.writer.ready, options?.signal);
49
- // the streams spec recommends not waiting for data to be sent
50
- // https://streams.spec.whatwg.org/#example-manual-write-dont-await
51
- this.writer.write(chunk)
55
+ sendData(data) {
56
+ // the streams spec recommends not waiting for data to be sent
57
+ // https://streams.spec.whatwg.org/#example-manual-write-dont-await
58
+ for (const buf of data) {
59
+ this.writer.write(buf)
52
60
  .catch(err => {
53
- this.log.error('error sending stream data', err);
61
+ this.abort(err);
62
+ });
63
+ }
64
+ this.log.trace('desired size after sending %d bytes is %d bytes', data.byteLength, this.writer.desiredSize);
65
+ // null means the stream has errored - https://streams.spec.whatwg.org/#writable-stream-default-writer-get-desired-size
66
+ if (this.writer.desiredSize == null) {
67
+ return {
68
+ sentBytes: data.byteLength,
69
+ canSendMore: false
70
+ };
71
+ }
72
+ const canSendMore = this.writer.desiredSize > 0;
73
+ if (!canSendMore) {
74
+ this.writer.ready.then(() => {
75
+ this.safeDispatchEvent('drain');
76
+ }, (err) => {
77
+ this.abort(err);
54
78
  });
55
79
  }
80
+ return {
81
+ sentBytes: data.byteLength,
82
+ canSendMore
83
+ };
56
84
  }
57
- async sendReset(options) {
58
- this.log('sendReset aborting writer');
59
- await raceSignal(this.writer.abort(), options?.signal);
60
- this.log('sendReset aborted writer');
85
+ sendReset(err) {
86
+ this.writer.abort(err)
87
+ .catch(err => {
88
+ this.log.error('error aborting writer - %e', err);
89
+ });
61
90
  }
62
91
  async sendCloseWrite(options) {
63
92
  this.log('sendCloseWrite closing writer');
64
- await raceSignal(this.writer.close(), options?.signal);
93
+ await raceSignal(this.writer.close().catch(() => { }), options?.signal);
65
94
  this.log('sendCloseWrite closed writer');
66
95
  }
67
96
  async sendCloseRead(options) {
@@ -69,21 +98,20 @@ class WebTransportStream extends AbstractStream {
69
98
  await raceSignal(this.reader.cancel(), options?.signal);
70
99
  this.log('sendCloseRead cancelled reader');
71
100
  }
101
+ sendPause() {
102
+ }
103
+ sendResume() {
104
+ this.readData();
105
+ }
72
106
  }
73
- export async function webtransportBiDiStreamToStream(bidiStream, streamId, direction, activeStreams, onStreamEnd, log) {
74
- const stream = new WebTransportStream({
75
- bidiStream,
107
+ export function webtransportBiDiStreamToStream(stream, streamId, direction, log, options) {
108
+ return new WebTransportStream({
109
+ ...options,
110
+ stream,
76
111
  id: streamId,
77
112
  direction,
78
113
  log: log.newScope(`${direction}:${streamId}`),
79
- onEnd: () => {
80
- const index = activeStreams.findIndex(s => s === stream);
81
- if (index !== -1) {
82
- activeStreams.splice(index, 1);
83
- }
84
- onStreamEnd?.(stream);
85
- }
114
+ protocol: ''
86
115
  });
87
- return stream;
88
116
  }
89
117
  //# sourceMappingURL=stream.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAA;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAQ/C,MAAM,kBAAmB,SAAQ,cAAc;IAC5B,MAAM,CAAyC;IAC/C,MAAM,CAAyC;IAEhE,YAAa,IAA4B;QACvC,KA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AAE;KACb,CAAC,CAAA;AACJ,CAAC"}
@@ -4,7 +4,7 @@ import type { MultihashDigest } from 'multiformats/hashes/interface';
4
4
  export interface ParsedMultiaddr {
5
5
  url: string;
6
6
  certhashes: MultihashDigest[];
7
- remotePeer?: PeerId;
7
+ remotePeer: PeerId;
8
8
  }
9
9
  export declare function parseMultiaddr(ma: Multiaddr): ParsedMultiaddr;
10
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":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,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
+ {"version":3,"file":"parse-multiaddr.d.ts","sourceRoot":"","sources":["../../../src/utils/parse-multiaddr.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,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,EAAE,MAAM,CAAA;CACnB;AAED,wBAAgB,cAAc,CAAE,EAAE,EAAE,SAAS,GAAG,eAAe,CA4C9D"}
@@ -1,6 +1,6 @@
1
1
  import { InvalidMultiaddrError } from '@libp2p/interface';
2
2
  import { peerIdFromString } from '@libp2p/peer-id';
3
- import { protocols } from '@multiformats/multiaddr';
3
+ import { getNetConfig } from '@libp2p/utils';
4
4
  import { WebTransport } from '@multiformats/multiaddr-matcher';
5
5
  import { bases, digest } from 'multiformats/basics';
6
6
  // @ts-expect-error - Not easy to combine these types.
@@ -12,17 +12,23 @@ export function parseMultiaddr(ma) {
12
12
  if (!WebTransport.matches(ma)) {
13
13
  throw new InvalidMultiaddrError('Invalid multiaddr, was not a WebTransport address');
14
14
  }
15
- const parts = ma.stringTuples();
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();
15
+ const certhashes = [];
16
+ let remotePeer;
17
+ for (const components of ma.getComponents()) {
18
+ if (components.name === 'certhash') {
19
+ certhashes.push(decodeCerthashStr(components.value ?? ''));
20
+ }
21
+ // only take the first peer id in the multiaddr as it may be a relay
22
+ if (components.name === 'p2p' && remotePeer == null) {
23
+ remotePeer = peerIdFromString(components.value ?? '');
24
+ }
25
+ }
26
+ if (remotePeer == null) {
27
+ throw new InvalidMultiaddrError('Remote peer must be present in multiaddr');
28
+ }
29
+ const opts = getNetConfig(ma);
24
30
  let host = opts.host;
25
- if (opts.family === 6 && host?.includes(':')) {
31
+ if (opts.type === 'ip6' && host.includes(':')) {
26
32
  /**
27
33
  * This resolves cases where `new WebTransport()` fails to construct because of an invalid URL being passed.
28
34
  *
@@ -1 +1 @@
1
- {"version":3,"file":"parse-multiaddr.js","sourceRoot":"","sources":["../../../src/utils/parse-multiaddr.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAA;AACzD,OAAO,EAAE,gBA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AAC"}
@@ -0,0 +1,18 @@
1
+ import { AbstractMessageStream } from '@libp2p/utils';
2
+ import type { AbortOptions } from '@libp2p/interface';
3
+ import type { MessageStreamInit, SendResult } from '@libp2p/utils';
4
+ import type { Uint8ArrayList } from 'uint8arraylist';
5
+ export interface WebTransportMessageStreamInit extends MessageStreamInit {
6
+ stream: WebTransportBidirectionalStream;
7
+ }
8
+ export declare class WebTransportMessageStream extends AbstractMessageStream {
9
+ private writer;
10
+ private reader;
11
+ constructor(init: WebTransportMessageStreamInit);
12
+ close(options?: AbortOptions): Promise<void>;
13
+ sendData(data: Uint8ArrayList): SendResult;
14
+ sendReset(err: Error): void;
15
+ sendPause(): void;
16
+ sendResume(): void;
17
+ }
18
+ //# sourceMappingURL=webtransport-message-stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webtransport-message-stream.d.ts","sourceRoot":"","sources":["../../../src/utils/webtransport-message-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AAErD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAClE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,MAAM,WAAW,6BAA8B,SAAQ,iBAAiB;IACtE,MAAM,EAAE,+BAA+B,CAAA;CACxC;AAED,qBAAa,yBAA0B,SAAQ,qBAAqB;IAClE,OAAO,CAAC,MAAM,CAAkC;IAChD,OAAO,CAAC,MAAM,CAAkC;gBAEnC,IAAI,EAAE,6BAA6B;IAwB1C,KAAK,CAAE,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;IAInD,QAAQ,CAAE,IAAI,EAAE,cAAc,GAAG,UAAU;IAY3C,SAAS,CAAE,GAAG,EAAE,KAAK,GAAG,IAAI;IAO5B,SAAS,IAAK,IAAI;IAIlB,UAAU,IAAK,IAAI;CAGpB"}
@@ -0,0 +1,49 @@
1
+ import { AbstractMessageStream } from '@libp2p/utils';
2
+ import { raceSignal } from 'race-signal';
3
+ export class WebTransportMessageStream extends AbstractMessageStream {
4
+ writer;
5
+ reader;
6
+ constructor(init) {
7
+ super(init);
8
+ this.writer = init.stream.writable.getWriter();
9
+ this.reader = init.stream.readable.getReader();
10
+ Promise.resolve().then(async () => {
11
+ while (true) {
12
+ const { done, value } = await this.reader.read();
13
+ if (value != null) {
14
+ this.onData(value);
15
+ }
16
+ if (done) {
17
+ break;
18
+ }
19
+ }
20
+ })
21
+ .catch(err => {
22
+ this.abort(err);
23
+ });
24
+ }
25
+ async close(options) {
26
+ await raceSignal(this.writer.close(), options?.signal);
27
+ }
28
+ sendData(data) {
29
+ this.writer.write(data.subarray())
30
+ .catch(err => {
31
+ this.abort(err);
32
+ });
33
+ return {
34
+ sentBytes: data.byteLength,
35
+ canSendMore: true
36
+ };
37
+ }
38
+ sendReset(err) {
39
+ this.writer.abort(err)
40
+ .catch(err => {
41
+ this.log.error('could not send reset - %e', err);
42
+ });
43
+ }
44
+ sendPause() {
45
+ }
46
+ sendResume() {
47
+ }
48
+ }
49
+ //# sourceMappingURL=webtransport-message-stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webtransport-message-stream.js","sourceRoot":"","sources":["../../../src/utils/webtransport-message-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AASxC,MAAM,OAAO,yBAA0B,SAAQ,qBAAqB;IAC1D,MAAM,CAAkC;IACxC,MAAM,CAAkC;IAEhD,YAAa,IAAmC;QAC9C,KAAK,CAAC,IAAI,CAAC,CAAA;QAEX,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAA;QAC9C,IAAI,CAAC,MAA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C;IAED,SAAS;IAET,CAAC;IAED,UAAU;IAEV,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/webtransport",
3
- "version": "5.0.51",
3
+ "version": "6.0.0-55b7e5fea",
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/main/packages/transport-webtransport#readme",
@@ -40,36 +40,35 @@
40
40
  "build": "aegir build",
41
41
  "test": "aegir test -t browser -t webworker",
42
42
  "test:chrome": "aegir test -t browser --cov",
43
- "test:chrome-webworker": "aegir test -t webworker"
43
+ "test:chrome-webworker": "aegir test -t webworker",
44
+ "test:firefox": "aegir test -t browser -- --browser firefox"
44
45
  },
45
46
  "dependencies": {
46
- "@chainsafe/libp2p-noise": "^16.1.3",
47
- "@libp2p/interface": "^2.11.0",
48
- "@libp2p/peer-id": "^5.1.9",
49
- "@libp2p/utils": "^6.7.2",
50
- "@multiformats/multiaddr": "^12.4.4",
51
- "@multiformats/multiaddr-matcher": "^2.0.0",
52
- "it-stream-types": "^2.0.2",
47
+ "@libp2p/interface": "3.0.0-55b7e5fea",
48
+ "@libp2p/noise": "1.0.0-55b7e5fea",
49
+ "@libp2p/peer-id": "6.0.0-55b7e5fea",
50
+ "@libp2p/utils": "7.0.0-55b7e5fea",
51
+ "@multiformats/multiaddr": "^13.0.1",
52
+ "@multiformats/multiaddr-matcher": "^3.0.1",
53
53
  "multiformats": "^13.3.6",
54
54
  "progress-events": "^1.0.1",
55
- "race-signal": "^1.1.3",
55
+ "race-signal": "^2.0.0",
56
56
  "uint8arraylist": "^2.4.8",
57
57
  "uint8arrays": "^5.1.0"
58
58
  },
59
59
  "devDependencies": {
60
- "@libp2p/crypto": "^5.1.8",
61
- "@libp2p/daemon-client": "^9.0.6",
62
- "@libp2p/logger": "^5.2.0",
63
- "@libp2p/ping": "^2.0.37",
64
- "@noble/hashes": "^1.8.0",
65
- "aegir": "^47.0.14",
66
- "execa": "^9.5.3",
60
+ "@libp2p/crypto": "5.1.9-55b7e5fea",
61
+ "@libp2p/daemon-client": "10.0.0-55b7e5fea",
62
+ "@libp2p/logger": "6.0.0-55b7e5fea",
63
+ "@libp2p/ping": "3.0.0-55b7e5fea",
64
+ "@noble/hashes": "^2.0.1",
65
+ "aegir": "^47.0.22",
66
+ "execa": "^9.6.0",
67
67
  "go-libp2p": "^1.6.0",
68
- "it-map": "^3.1.3",
69
- "it-to-buffer": "^4.0.9",
70
- "libp2p": "^2.10.0",
68
+ "it-all": "^3.0.9",
69
+ "libp2p": "3.0.0-55b7e5fea",
71
70
  "p-defer": "^4.0.1",
72
- "p-wait-for": "^5.0.2",
71
+ "p-event": "^7.0.0",
73
72
  "sinon-ts": "^2.0.0"
74
73
  },
75
74
  "browser": {
package/src/index.ts CHANGED
@@ -3,20 +3,18 @@
3
3
  *
4
4
  * A [libp2p transport](https://docs.libp2p.io/concepts/transports/overview/) based on [WebTransport](https://www.w3.org/TR/webtransport/).
5
5
  *
6
- * >
7
6
  * > ⚠️ **Note**
8
7
  * >
9
8
  * > This WebTransport implementation currently only allows dialing to other nodes. It does not yet allow listening for incoming dials. This feature requires QUIC support to land in Node JS first.
10
9
  * >
11
10
  * > QUIC support in Node JS is actively being worked on. You can keep an eye on the progress by watching the [related issues on the Node JS issue tracker](https://github.com/nodejs/node/labels/quic)
12
- * >
13
11
  *
14
12
  * @example
15
13
  *
16
14
  * ```TypeScript
17
15
  * import { createLibp2p } from 'libp2p'
18
16
  * import { webTransport } from '@libp2p/webtransport'
19
- * import { noise } from '@chainsafe/libp2p-noise'
17
+ * import { noise } from '@libp2p/noise'
20
18
  *
21
19
  * const node = await createLibp2p({
22
20
  * transports: [
@@ -29,24 +27,21 @@
29
27
  * ```
30
28
  */
31
29
 
32
- import { noise } from '@chainsafe/libp2p-noise'
33
30
  import { InvalidCryptoExchangeError, InvalidParametersError, serviceCapabilities, transportSymbol } from '@libp2p/interface'
31
+ import { noise } from '@libp2p/noise'
34
32
  import { WebTransport as WebTransportMatcher } from '@multiformats/multiaddr-matcher'
35
33
  import { CustomProgressEvent } from 'progress-events'
36
- import { raceSignal } from 'race-signal'
37
- import { MAX_INBOUND_STREAMS } from './constants.js'
38
34
  import createListener from './listener.js'
39
35
  import { webtransportMuxer } from './muxer.js'
40
- import { inertDuplex } from './utils/inert-duplex.js'
36
+ import { toMultiaddrConnection } from './session-to-conn.ts'
41
37
  import { isSubset } from './utils/is-subset.js'
42
38
  import { parseMultiaddr } from './utils/parse-multiaddr.js'
39
+ import { WebTransportMessageStream } from './utils/webtransport-message-stream.ts'
43
40
  import WebTransport from './webtransport.js'
44
41
  import type { Upgrader, Transport, CreateListenerOptions, DialTransportOptions, Listener, ComponentLogger, Logger, Connection, MultiaddrConnection, CounterGroup, Metrics, PeerId, OutboundConnectionUpgradeEvents, PrivateKey } from '@libp2p/interface'
45
42
  import type { Multiaddr } from '@multiformats/multiaddr'
46
- import type { Source } from 'it-stream-types'
47
43
  import type { MultihashDigest } from 'multiformats/hashes/interface'
48
44
  import type { ProgressEvent } from 'progress-events'
49
- import type { Uint8ArrayList } from 'uint8arraylist'
50
45
 
51
46
  /**
52
47
  * PEM format server certificate and private key
@@ -63,7 +58,6 @@ interface WebTransportSessionCleanup {
63
58
  }
64
59
 
65
60
  export interface WebTransportInit {
66
- maxInboundStreams?: number
67
61
  certificates?: WebTransportCertificate[]
68
62
  }
69
63
 
@@ -88,6 +82,7 @@ export type WebTransportDialEvents =
88
82
 
89
83
  interface AuthenticateWebTransportOptions extends DialTransportOptions<WebTransportDialEvents> {
90
84
  wt: WebTransport
85
+ maConn: MultiaddrConnection
91
86
  remotePeer?: PeerId
92
87
  certhashes: Array<MultihashDigest<number>>
93
88
  }
@@ -103,7 +98,6 @@ class WebTransportTransport implements Transport<WebTransportDialEvents> {
103
98
  this.components = components
104
99
  this.config = {
105
100
  ...init,
106
- maxInboundStreams: init.maxInboundStreams ?? MAX_INBOUND_STREAMS,
107
101
  certificates: init.certificates ?? []
108
102
  }
109
103
 
@@ -202,36 +196,32 @@ class WebTransportTransport implements Transport<WebTransportDialEvents> {
202
196
  cleanUpWTSession('remote_close')
203
197
  })
204
198
 
205
- authenticated = await raceSignal(this.authenticateWebTransport({ wt, remotePeer, certhashes, ...options }), options.signal)
206
-
207
- if (!authenticated) {
208
- throw new InvalidCryptoExchangeError('Failed to authenticate webtransport')
209
- }
210
-
211
199
  this.metrics?.dialerEvents.increment({ open: true })
212
200
 
213
- maConn = {
214
- close: async () => {
215
- this.log('closing webtransport')
216
- cleanUpWTSession('close')
217
- },
218
- abort: (err: Error) => {
219
- this.log('aborting webtransport due to passed err', err)
220
- cleanUpWTSession('abort')
221
- },
201
+ maConn = toMultiaddrConnection({
222
202
  remoteAddr: ma,
223
- timeline: {
224
- open: Date.now()
225
- },
226
- log: this.components.logger.forComponent('libp2p:webtransport:maconn'),
227
- // This connection is never used directly since webtransport supports native streams.
228
- ...inertDuplex()
203
+ cleanUpWTSession,
204
+ direction: 'outbound',
205
+ log: this.components.logger.forComponent('libp2p:webtransport:connection')
206
+ })
207
+
208
+ authenticated = await this.authenticateWebTransport({
209
+ wt,
210
+ maConn,
211
+ remotePeer,
212
+ certhashes,
213
+ ...options
214
+ })
215
+
216
+ if (!authenticated) {
217
+ throw new InvalidCryptoExchangeError('Failed to authenticate webtransport')
229
218
  }
230
219
 
231
220
  return await options.upgrader.upgradeOutbound(maConn, {
232
221
  ...options,
233
222
  skipEncryption: true,
234
- muxerFactory: webtransportMuxer(wt, wt.incomingBidirectionalStreams.getReader(), this.log, this.config),
223
+ remotePeer,
224
+ muxerFactory: webtransportMuxer(wt),
235
225
  skipProtection: true
236
226
  })
237
227
  } catch (err: any) {
@@ -253,61 +243,34 @@ class WebTransportTransport implements Transport<WebTransportDialEvents> {
253
243
  }
254
244
  }
255
245
 
256
- async authenticateWebTransport ({ wt, remotePeer, certhashes, onProgress, signal }: AuthenticateWebTransportOptions): Promise<boolean> {
257
- signal?.throwIfAborted()
258
-
246
+ async authenticateWebTransport ({ wt, maConn, remotePeer, certhashes, onProgress, signal }: AuthenticateWebTransportOptions): Promise<boolean> {
259
247
  onProgress?.(new CustomProgressEvent('webtransport:open-authentication-stream'))
260
248
  const stream = await wt.createBidirectionalStream()
261
- const writer = stream.writable.getWriter()
262
- const reader = stream.readable.getReader()
263
-
264
- const duplex = {
265
- source: (async function * () {
266
- while (true) {
267
- const val = await reader.read()
268
-
269
- if (val.value != null) {
270
- yield val.value
271
- }
272
-
273
- if (val.done) {
274
- break
275
- }
276
- }
277
- })(),
278
- sink: async (source: Source<Uint8Array | Uint8ArrayList>) => {
279
- for await (const chunk of source) {
280
- await raceSignal(writer.ready, signal)
281
-
282
- const buf = chunk instanceof Uint8Array ? chunk : chunk.subarray()
249
+ signal?.throwIfAborted()
283
250
 
284
- writer.write(buf).catch(err => {
285
- this.log.error('could not write chunk during authentication of WebTransport stream', err)
286
- })
287
- }
288
- }
289
- }
251
+ const messages = new WebTransportMessageStream({
252
+ stream,
253
+ log: maConn.log.newScope('muxer')
254
+ })
290
255
 
291
256
  const n = noise()(this.components)
292
257
 
293
258
  onProgress?.(new CustomProgressEvent('webtransport:secure-outbound-connection'))
294
- const { remoteExtensions } = await n.secureOutbound(duplex, {
259
+ const { remoteExtensions } = await n.secureOutbound(messages, {
295
260
  signal,
296
261
  remotePeer,
297
262
  skipStreamMuxerNegotiation: true
298
263
  })
299
264
 
300
265
  onProgress?.(new CustomProgressEvent('webtransport:close-authentication-stream'))
301
- // We're done with this authentication stream
302
- writer.close().catch((err: Error) => {
303
- this.log.error(`Failed to close authentication stream writer: ${err.message}`)
304
- })
305
266
 
306
- reader.cancel().catch((err: Error) => {
307
- this.log.error(`Failed to close authentication stream reader: ${err.message}`)
267
+ // We're done with this authentication stream
268
+ await messages.close({
269
+ signal
308
270
  })
309
271
 
310
- // Verify the certhashes we used when dialing are a subset of the certhashes relayed by the remote peer
272
+ // Verify the certhashes we used when dialing are a subset of the certhashes
273
+ // relayed by the remote peer
311
274
  if (!isSubset(remoteExtensions?.webtransportCerthashes ?? [], certhashes.map(ch => ch.bytes))) {
312
275
  throw new InvalidParametersError("Our certhashes are not a subset of the remote's reported certhashes")
313
276
  }
@@ -318,8 +281,7 @@ class WebTransportTransport implements Transport<WebTransportDialEvents> {
318
281
  createListener (options: CreateListenerOptions): Listener {
319
282
  return createListener(this.components, {
320
283
  ...options,
321
- certificates: this.config.certificates,
322
- maxInboundStreams: this.config.maxInboundStreams
284
+ certificates: this.config.certificates
323
285
  })
324
286
  }
325
287
 
package/src/listener.ts CHANGED
@@ -11,7 +11,6 @@ export interface WebTransportListenerInit extends CreateListenerOptions {
11
11
  handler?(conn: Connection): void
12
12
  upgrader: Upgrader
13
13
  certificates?: WebTransportCertificate[]
14
- maxInboundStreams?: number
15
14
  }
16
15
 
17
16
  export default function createListener (components: WebTransportListenerComponents, options: WebTransportListenerInit): Listener {