rwebtransport 0.2.3 → 0.2.5

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 (96) hide show
  1. package/Cargo.lock +153 -226
  2. package/Cargo.toml +5 -5
  3. package/README.md +4 -4
  4. package/crates/echo-server/src/bin/gen-test-cert.rs +1 -1
  5. package/crates/native/Cargo.toml +1 -1
  6. package/crates/native/src/config.rs +13 -1
  7. package/crates/native/src/driver.rs +249 -91
  8. package/crates/native/src/lib.rs +9 -10
  9. package/crates/native/src/parser_memory.rs +1 -0
  10. package/crates/native/src/server.rs +197 -182
  11. package/crates/native/src/session.rs +329 -177
  12. package/crates/native/src/test_support.rs +2 -2
  13. package/crates/wtcore/src/h3.rs +166 -10
  14. package/dist/index.js +92 -45
  15. package/dist/index.js.map +2 -2
  16. package/dist/index.mjs +92 -45
  17. package/dist/index.mjs.map +2 -2
  18. package/dist/native.d.ts +9 -4
  19. package/package.json +14 -11
  20. package/prebuilds/darwin-arm64/rwebtransport.node +0 -0
  21. package/prebuilds/linux-arm64/rwebtransport.node +0 -0
  22. package/prebuilds/linux-x64/rwebtransport.node +0 -0
  23. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  24. package/scripts/build-linux-prebuilt.sh +46 -0
  25. package/scripts/build.js +10 -3
  26. package/vendor/quiche/AGENTS.md +10 -0
  27. package/vendor/quiche/Cargo.lock +239 -248
  28. package/vendor/quiche/Cargo.toml +23 -10
  29. package/vendor/quiche/examples/Makefile +4 -4
  30. package/vendor/quiche/examples/client.rs +10 -28
  31. package/vendor/quiche/examples/http3-client.rs +20 -34
  32. package/vendor/quiche/examples/http3-server.rs +47 -78
  33. package/vendor/quiche/examples/server.rs +19 -49
  34. package/vendor/quiche/include/quiche.h +35 -4
  35. package/vendor/quiche/src/build.rs +17 -0
  36. package/vendor/quiche/src/cid.rs +129 -61
  37. package/vendor/quiche/src/crypto/boringssl.rs +98 -86
  38. package/vendor/quiche/src/crypto/mod.rs +64 -77
  39. package/vendor/quiche/src/dgram.rs +2 -3
  40. package/vendor/quiche/src/error.rs +22 -20
  41. package/vendor/quiche/src/ffi.rs +558 -280
  42. package/vendor/quiche/src/flowcontrol.rs +0 -7
  43. package/vendor/quiche/src/frame.rs +134 -135
  44. package/vendor/quiche/src/h3/ffi.rs +73 -60
  45. package/vendor/quiche/src/h3/frame.rs +70 -90
  46. package/vendor/quiche/src/h3/mod.rs +1024 -593
  47. package/vendor/quiche/src/h3/qpack/decoder.rs +248 -34
  48. package/vendor/quiche/src/h3/qpack/dynamic_table.rs +859 -0
  49. package/vendor/quiche/src/h3/qpack/encoder.rs +14 -17
  50. package/vendor/quiche/src/h3/qpack/mod.rs +48 -0
  51. package/vendor/quiche/src/h3/qpack/static_table.rs +86 -73
  52. package/vendor/quiche/src/h3/stream.rs +1009 -194
  53. package/vendor/quiche/src/lib.rs +1346 -1195
  54. package/vendor/quiche/src/minmax.rs +2 -6
  55. package/vendor/quiche/src/packet.rs +420 -552
  56. package/vendor/quiche/src/path.rs +202 -72
  57. package/vendor/quiche/src/pmtud.rs +18 -16
  58. package/vendor/quiche/src/ranges.rs +184 -82
  59. package/vendor/quiche/src/recovery/AGENTS.md +5 -2
  60. package/vendor/quiche/src/recovery/bandwidth.rs +59 -40
  61. package/vendor/quiche/src/recovery/bytes_in_flight.rs +2 -2
  62. package/vendor/quiche/src/recovery/congestion/cubic.rs +38 -47
  63. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +54 -60
  64. package/vendor/quiche/src/recovery/congestion/hystart.rs +6 -10
  65. package/vendor/quiche/src/recovery/congestion/mod.rs +110 -42
  66. package/vendor/quiche/src/recovery/congestion/prr.rs +4 -1
  67. package/vendor/quiche/src/recovery/congestion/recovery.rs +91 -95
  68. package/vendor/quiche/src/recovery/congestion/reno.rs +17 -12
  69. package/vendor/quiche/src/recovery/congestion/test_sender.rs +8 -27
  70. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +159 -247
  71. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +9 -14
  72. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +18 -15
  73. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +43 -28
  74. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +349 -76
  75. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +147 -195
  76. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +29 -27
  77. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min.rs +180 -0
  78. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/global_min_tests.rs +221 -0
  79. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm.rs +395 -0
  80. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/hmm_tests.rs +473 -0
  81. package/vendor/quiche/src/recovery/gcongestion/bbr2/rtt_jump_detector/mod.rs +175 -0
  82. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +33 -26
  83. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +107 -60
  84. package/vendor/quiche/src/recovery/gcongestion/mod.rs +64 -11
  85. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +34 -25
  86. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +81 -86
  87. package/vendor/quiche/src/recovery/mod.rs +414 -173
  88. package/vendor/quiche/src/recovery/rtt.rs +8 -5
  89. package/vendor/quiche/src/stream/mod.rs +864 -133
  90. package/vendor/quiche/src/stream/recv_buf.rs +471 -172
  91. package/vendor/quiche/src/stream/send_buf.rs +338 -81
  92. package/vendor/quiche/src/test_utils.rs +78 -64
  93. package/vendor/quiche/src/tests.rs +2559 -1335
  94. package/vendor/quiche/src/tls/boringssl.rs +60 -63
  95. package/vendor/quiche/src/tls/mod.rs +146 -193
  96. package/vendor/quiche/src/transport_params.rs +87 -103
package/dist/index.mjs CHANGED
@@ -15,37 +15,58 @@ function assertSupportedRuntime() {
15
15
  );
16
16
  }
17
17
  }
18
- function prebuiltPath() {
19
- return join(
20
- PACKAGE_ROOT,
21
- "prebuilds",
22
- `${process.platform}-${process.arch}`,
23
- "rwebtransport.node"
24
- );
18
+ function prebuiltPath(name = "rwebtransport.node") {
19
+ return join(PACKAGE_ROOT, "prebuilds", `${process.platform}-${process.arch}`, name);
25
20
  }
26
21
  var cached;
27
22
  function loadNative() {
28
23
  if (cached) return cached;
29
24
  assertSupportedRuntime();
30
25
  const prebuilt = prebuiltPath();
26
+ let prebuildError;
27
+ let output = prebuilt;
31
28
  if (existsSync(prebuilt)) {
32
- cached = require2(prebuilt);
33
- return cached;
29
+ try {
30
+ cached = require2(prebuilt);
31
+ return cached;
32
+ } catch (error) {
33
+ if (!(error instanceof Error) || error.code !== "ERR_DLOPEN_FAILED") {
34
+ throw error;
35
+ }
36
+ prebuildError = error;
37
+ output = prebuiltPath("rwebtransport-local.node");
38
+ }
39
+ }
40
+ if (output !== prebuilt && existsSync(output)) {
41
+ try {
42
+ cached = require2(output);
43
+ return cached;
44
+ } catch (error) {
45
+ if (!(error instanceof Error) || error.code !== "ERR_DLOPEN_FAILED") {
46
+ throw error;
47
+ }
48
+ }
34
49
  }
35
50
  const builder = join(PACKAGE_ROOT, "scripts", "build.js");
36
51
  if (!existsSync(builder)) {
37
52
  throw new Error(
38
- `rwebtransport: no prebuilt binary at ${prebuilt} and no build script to compile one. Reinstall the package or build it manually (npm run build:rust).`
53
+ `rwebtransport: no usable native binary at ${prebuilt} and no build script to compile one. Reinstall the package or build it manually (npm run build:rust).`,
54
+ { cause: prebuildError }
39
55
  );
40
56
  }
41
57
  process.stderr.write(
42
- "rwebtransport: no prebuilt binary for this platform; compiling the native addon (this happens once)...\n"
58
+ "rwebtransport: no usable native binary for this platform; compiling the native addon...\n"
43
59
  );
44
- execFileSync(process.execPath, [builder], { cwd: PACKAGE_ROOT, stdio: "inherit" });
45
- if (!existsSync(prebuilt)) {
46
- throw new Error(`rwebtransport: build completed but ${prebuilt} was not produced.`);
60
+ const args = output === prebuilt ? [builder] : [builder, "--fallback"];
61
+ try {
62
+ execFileSync(process.execPath, args, { cwd: PACKAGE_ROOT, stdio: "inherit" });
63
+ } catch (error) {
64
+ const detail = prebuildError instanceof Error ? ` (${prebuildError.message})` : "";
65
+ throw new Error(`rwebtransport: unable to build a native addon for this system${detail}`, {
66
+ cause: error
67
+ });
47
68
  }
48
- cached = require2(prebuilt);
69
+ cached = require2(output);
49
70
  return cached;
50
71
  }
51
72
 
@@ -129,6 +150,7 @@ var SessionCore = class {
129
150
  cachedProtocol = "";
130
151
  /** The CONNECT response headers, cached from the client ready event. */
131
152
  cachedResponseHeaders = [];
153
+ advertisedDatagramSize;
132
154
  /**
133
155
  * Resolves when the session is established, rejects if it fails before then.
134
156
  * Backs the public `WebTransport.ready` promise.
@@ -234,14 +256,18 @@ var SessionCore = class {
234
256
  */
235
257
  resetStream(streamId, code) {
236
258
  this.transport?.reset(streamId, code >>> 0);
259
+ this.rejectStreamWrites(
260
+ streamId,
261
+ new WebTransportError("stream aborted", {
262
+ source: "stream",
263
+ streamErrorCode: code >>> 0
264
+ })
265
+ );
266
+ }
267
+ rejectStreamWrites(streamId, error) {
237
268
  for (const [requestId, write] of this.writes) {
238
269
  if (write.streamId === streamId) {
239
- write.deferred.reject(
240
- new WebTransportError("stream aborted", {
241
- source: "stream",
242
- streamErrorCode: code >>> 0
243
- })
244
- );
270
+ write.deferred.reject(error);
245
271
  this.writes.delete(requestId);
246
272
  }
247
273
  }
@@ -283,7 +309,8 @@ var SessionCore = class {
283
309
  * @returns The maximum datagram payload size in bytes, or 0 if not attached.
284
310
  */
285
311
  maxDatagramSize() {
286
- return this.transport ? this.transport.maxDatagramSize() : 0;
312
+ if (this.closedState) return 0;
313
+ return this.advertisedDatagramSize ?? this.transport?.maxDatagramSize?.() ?? 0;
287
314
  }
288
315
  /**
289
316
  * Gracefully close the session. No-op if already closed or not attached.
@@ -463,6 +490,10 @@ var SessionCore = class {
463
490
  this.datagramSink?.(ev.data);
464
491
  break;
465
492
  }
493
+ case "datagramSize" /* DatagramSize */: {
494
+ this.advertisedDatagramSize = ev.size;
495
+ break;
496
+ }
466
497
  case "stream" /* Stream */: {
467
498
  if (ev.bidi) this.incoming?.onBidi(ev.streamId);
468
499
  else this.incoming?.onUni(ev.streamId);
@@ -481,6 +512,13 @@ var SessionCore = class {
481
512
  break;
482
513
  }
483
514
  case "streamStopSending" /* StreamStopSending */: {
515
+ this.rejectStreamWrites(
516
+ ev.streamId,
517
+ new WebTransportError("peer sent STOP_SENDING", {
518
+ source: "stream",
519
+ streamErrorCode: ev.code
520
+ })
521
+ );
484
522
  this.sends.get(ev.streamId)?.onStopSending(ev.code);
485
523
  break;
486
524
  }
@@ -490,8 +528,13 @@ var SessionCore = class {
490
528
  break;
491
529
  }
492
530
  case "writeAck" /* WriteAck */: {
493
- this.writes.get(ev.requestId)?.deferred.resolve();
531
+ const write = this.writes.get(ev.requestId);
494
532
  this.writes.delete(ev.requestId);
533
+ if (ev.accepted) write?.deferred.resolve();
534
+ else
535
+ write?.deferred.reject(
536
+ new WebTransportError("stream is closed", { source: "stream" })
537
+ );
495
538
  break;
496
539
  }
497
540
  case "datagramAck" /* DatagramAck */: {
@@ -772,10 +815,6 @@ var ServerTransport = class {
772
815
  sendDatagram(bytes, requestId) {
773
816
  this.native.serverSendDatagram(this.handle, this.session, bytes, requestId);
774
817
  }
775
- /** @returns The maximum datagram payload size, in bytes. */
776
- maxDatagramSize() {
777
- return this.native.serverMaxDatagramSize(this.handle);
778
- }
779
818
  /**
780
819
  * @param code - The WebTransport close code.
781
820
  * @param reason - The UTF-8 encoded close reason.
@@ -896,9 +935,8 @@ var WebTransportDatagramDuplexStream = class {
896
935
  *
897
936
  * 1. Creates {@link readable} and captures its controller from `start`.
898
937
  * 2. Installs a datagram sink that enqueues each received datagram only
899
- * while the controller's `desiredSize` is positive (treating an unknown
900
- * `desiredSize` of `null` as `1`, i.e. room available); datagrams that
901
- * arrive when the queue is full are dropped.
938
+ * while the controller's `desiredSize` is positive. Datagrams that
939
+ * arrive after cancellation or when the queue is full are dropped.
902
940
  * 3. Subscribes to the session's `closed` promise so the inbound stream is
903
941
  * closed on both clean close and error, preventing a pending `read()`
904
942
  * from hanging forever. A double-close is swallowed.
@@ -908,16 +946,20 @@ var WebTransportDatagramDuplexStream = class {
908
946
  constructor(session) {
909
947
  this.session = session;
910
948
  let rController;
949
+ let receiving = true;
911
950
  this.readable = new ReadableStream(
912
951
  {
913
952
  start(c) {
914
953
  rController = c;
954
+ },
955
+ cancel() {
956
+ receiving = false;
915
957
  }
916
958
  },
917
959
  new CountQueuingStrategy({ highWaterMark: this.incomingHwm })
918
960
  );
919
961
  session.setDatagramSink((data) => {
920
- if ((rController.desiredSize ?? 1) > 0) {
962
+ if (receiving && (rController.desiredSize ?? 0) > 0) {
921
963
  rController.enqueue(data);
922
964
  } else {
923
965
  session.recordDroppedIncomingDatagram();
@@ -1026,46 +1068,48 @@ var WebTransportReceiveStream = class extends ReadableStream {
1026
1068
  * @remarks Installs the `ReadableStream` underlying source and registers a
1027
1069
  * {@link ReceiveSink} on `session` for `streamId`:
1028
1070
  *
1029
- * - `pull` unpauses the native stream via `setPaused(streamId, false)`, so
1030
- * quiche resumes draining inbound bytes when the consumer wants more.
1071
+ * - `pull` grants the native stream credit for one inbound chunk.
1031
1072
  * - `cancel(reason)` sends `STOP_SENDING` to the peer with the code from
1032
1073
  * {@link errorCode} and then unregisters the receive sink.
1033
- * - The sink's `onData` enqueues each chunk and, once the controller's
1034
- * `desiredSize` falls to zero or below (a nullish `desiredSize` is treated as
1035
- * `1`, so it does not pause), pauses the native stream to flow-control the
1036
- * peer.
1037
1074
  * - The sink's `onFin` closes the controller (swallowing the error if it was
1038
1075
  * already closed or errored) and unregisters.
1039
1076
  * - The sink's `onReset(code)` errors the controller with a `'stream'`-sourced
1040
1077
  * {@link WebTransportError} ('stream reset by peer') carrying the peer's code,
1041
1078
  * then unregisters.
1042
1079
  *
1043
- * A {@link CountQueuingStrategy} with a high-water mark of 32 chunks bounds the
1044
- * JS-side queue and thereby the pause threshold.
1080
+ * A zero high-water mark leaves unread payload in native flow control.
1045
1081
  */
1046
1082
  constructor(session, streamId) {
1047
1083
  let controller;
1084
+ let resolvePull;
1085
+ const finishPull = () => {
1086
+ const resolve = resolvePull;
1087
+ resolvePull = void 0;
1088
+ resolve?.();
1089
+ };
1048
1090
  super(
1049
1091
  {
1050
1092
  start(c) {
1051
1093
  controller = c;
1052
1094
  },
1053
1095
  pull() {
1096
+ const { promise, resolve } = Promise.withResolvers();
1097
+ resolvePull = resolve;
1054
1098
  session.setPaused(streamId, false);
1099
+ return promise;
1055
1100
  },
1056
1101
  cancel(reason) {
1057
1102
  session.stopSending(streamId, errorCode(reason));
1058
1103
  session.unregisterReceive(streamId);
1104
+ finishPull();
1059
1105
  }
1060
1106
  },
1061
- new CountQueuingStrategy({ highWaterMark: 32 })
1107
+ { highWaterMark: 0 }
1062
1108
  );
1063
1109
  session.registerReceive(streamId, {
1064
1110
  onData(chunk) {
1065
1111
  controller.enqueue(chunk);
1066
- if ((controller.desiredSize ?? 1) <= 0) {
1067
- session.setPaused(streamId, true);
1068
- }
1112
+ finishPull();
1069
1113
  },
1070
1114
  onFin() {
1071
1115
  try {
@@ -1073,6 +1117,7 @@ var WebTransportReceiveStream = class extends ReadableStream {
1073
1117
  } catch {
1074
1118
  }
1075
1119
  session.unregisterReceive(streamId);
1120
+ finishPull();
1076
1121
  },
1077
1122
  onReset(code) {
1078
1123
  controller.error(
@@ -1082,6 +1127,7 @@ var WebTransportReceiveStream = class extends ReadableStream {
1082
1127
  })
1083
1128
  );
1084
1129
  session.unregisterReceive(streamId);
1130
+ finishPull();
1085
1131
  },
1086
1132
  onSessionClose(error) {
1087
1133
  try {
@@ -1089,6 +1135,7 @@ var WebTransportReceiveStream = class extends ReadableStream {
1089
1135
  } catch {
1090
1136
  }
1091
1137
  session.unregisterReceive(streamId);
1138
+ finishPull();
1092
1139
  }
1093
1140
  });
1094
1141
  this.streamId = streamId;
@@ -1251,7 +1298,7 @@ function buildConfig(url, options) {
1251
1298
  const headerNames = [];
1252
1299
  const headerValues = [];
1253
1300
  for (const [name, value] of Object.entries(options.headers ?? {})) {
1254
- headerNames.push(name);
1301
+ headerNames.push(name.toLowerCase());
1255
1302
  headerValues.push(value);
1256
1303
  }
1257
1304
  return {
@@ -1675,7 +1722,7 @@ var WebTransportServer = class {
1675
1722
  const responseHeaderNames = [];
1676
1723
  const responseHeaderValues = [];
1677
1724
  for (const [name, value] of Object.entries(options.responseHeaders ?? {})) {
1678
- responseHeaderNames.push(name);
1725
+ responseHeaderNames.push(name.toLowerCase());
1679
1726
  responseHeaderValues.push(value);
1680
1727
  }
1681
1728
  this.handle = this.native.serverListen(