rwebtransport 0.1.0
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/Cargo.lock +666 -0
- package/Cargo.toml +31 -0
- package/LICENSE +202 -0
- package/NOTICE +41 -0
- package/README.md +550 -0
- package/crates/echo-server/Cargo.toml +20 -0
- package/crates/echo-server/src/main.rs +666 -0
- package/crates/native/Cargo.toml +23 -0
- package/crates/native/src/config.rs +129 -0
- package/crates/native/src/driver.rs +463 -0
- package/crates/native/src/lib.rs +821 -0
- package/crates/native/src/server.rs +718 -0
- package/crates/native/src/session.rs +1191 -0
- package/crates/native/src/tls.rs +96 -0
- package/crates/wtcore/Cargo.toml +15 -0
- package/crates/wtcore/src/h3.rs +226 -0
- package/crates/wtcore/src/lib.rs +9 -0
- package/dist/datagrams.d.ts +16 -0
- package/dist/errors.d.ts +10 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +1512 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1473 -0
- package/dist/index.mjs.map +1 -0
- package/dist/loader.d.ts +2 -0
- package/dist/native.d.ts +226 -0
- package/dist/server.d.ts +44 -0
- package/dist/streams.d.ts +14 -0
- package/dist/types.d.ts +24 -0
- package/dist/webtransport.d.ts +20 -0
- package/package.json +79 -0
- package/prebuilds/win32-x64/rwebtransport.node +0 -0
- package/scripts/bench.mjs +180 -0
- package/scripts/build.js +67 -0
- package/scripts/postinstall.js +67 -0
- package/scripts/setup-unix.sh +129 -0
- package/scripts/setup-windows.ps1 +229 -0
- package/vendor/quiche/AGENTS.md +75 -0
- package/vendor/quiche/COPYING +23 -0
- package/vendor/quiche/Cargo.lock +1212 -0
- package/vendor/quiche/Cargo.toml +215 -0
- package/vendor/quiche/README.md +466 -0
- package/vendor/quiche/examples/Makefile +46 -0
- package/vendor/quiche/examples/README.md +20 -0
- package/vendor/quiche/examples/cert-big.crt +90 -0
- package/vendor/quiche/examples/cert.crt +18 -0
- package/vendor/quiche/examples/cert.key +28 -0
- package/vendor/quiche/examples/client.c +343 -0
- package/vendor/quiche/examples/client.rs +281 -0
- package/vendor/quiche/examples/gen-certs.sh +15 -0
- package/vendor/quiche/examples/http3-client.c +473 -0
- package/vendor/quiche/examples/http3-client.rs +349 -0
- package/vendor/quiche/examples/http3-server.c +614 -0
- package/vendor/quiche/examples/http3-server.rs +680 -0
- package/vendor/quiche/examples/qpack-decode.rs +87 -0
- package/vendor/quiche/examples/qpack-encode.rs +88 -0
- package/vendor/quiche/examples/rootca.crt +21 -0
- package/vendor/quiche/examples/server.c +529 -0
- package/vendor/quiche/examples/server.rs +529 -0
- package/vendor/quiche/include/quiche.h +1282 -0
- package/vendor/quiche/quiche.svg +1 -0
- package/vendor/quiche/src/buffers.rs +105 -0
- package/vendor/quiche/src/build.rs +66 -0
- package/vendor/quiche/src/cid.rs +1079 -0
- package/vendor/quiche/src/crypto/boringssl.rs +373 -0
- package/vendor/quiche/src/crypto/mod.rs +671 -0
- package/vendor/quiche/src/dgram.rs +118 -0
- package/vendor/quiche/src/error.rs +263 -0
- package/vendor/quiche/src/ffi.rs +2290 -0
- package/vendor/quiche/src/flowcontrol.rs +246 -0
- package/vendor/quiche/src/frame.rs +2172 -0
- package/vendor/quiche/src/h3/AGENTS.md +54 -0
- package/vendor/quiche/src/h3/ffi.rs +442 -0
- package/vendor/quiche/src/h3/frame.rs +1404 -0
- package/vendor/quiche/src/h3/mod.rs +8223 -0
- package/vendor/quiche/src/h3/qpack/decoder.rs +288 -0
- package/vendor/quiche/src/h3/qpack/encoder.rs +235 -0
- package/vendor/quiche/src/h3/qpack/mod.rs +202 -0
- package/vendor/quiche/src/h3/qpack/static_table.rs +323 -0
- package/vendor/quiche/src/h3/stream.rs +1595 -0
- package/vendor/quiche/src/lib.rs +9556 -0
- package/vendor/quiche/src/minmax.rs +446 -0
- package/vendor/quiche/src/packet.rs +2334 -0
- package/vendor/quiche/src/path.rs +1374 -0
- package/vendor/quiche/src/pmtud.rs +652 -0
- package/vendor/quiche/src/rand.rs +64 -0
- package/vendor/quiche/src/range_buf.rs +186 -0
- package/vendor/quiche/src/ranges.rs +755 -0
- package/vendor/quiche/src/recovery/AGENTS.md +80 -0
- package/vendor/quiche/src/recovery/bandwidth.rs +565 -0
- package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -0
- package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -0
- package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -0
- package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -0
- package/vendor/quiche/src/recovery/congestion/mod.rs +361 -0
- package/vendor/quiche/src/recovery/congestion/prr.rs +238 -0
- package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -0
- package/vendor/quiche/src/recovery/congestion/reno.rs +291 -0
- package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -0
- package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -0
- package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -0
- package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -0
- package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -0
- package/vendor/quiche/src/recovery/mod.rs +3086 -0
- package/vendor/quiche/src/recovery/rtt.rs +152 -0
- package/vendor/quiche/src/stream/mod.rs +2210 -0
- package/vendor/quiche/src/stream/recv_buf.rs +1236 -0
- package/vendor/quiche/src/stream/send_buf.rs +849 -0
- package/vendor/quiche/src/test_utils.rs +681 -0
- package/vendor/quiche/src/tests.rs +12922 -0
- package/vendor/quiche/src/tls/boringssl.rs +375 -0
- package/vendor/quiche/src/tls/mod.rs +1245 -0
- package/vendor/quiche/src/transport_params.rs +628 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1473 @@
|
|
|
1
|
+
// src/loader.ts
|
|
2
|
+
import { createRequire } from "module";
|
|
3
|
+
import { execFileSync } from "child_process";
|
|
4
|
+
import { existsSync } from "fs";
|
|
5
|
+
import { dirname, join } from "path";
|
|
6
|
+
import { fileURLToPath } from "url";
|
|
7
|
+
var require2 = createRequire(import.meta.url);
|
|
8
|
+
var moduleDir = dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
var PACKAGE_ROOT = join(moduleDir, "..");
|
|
10
|
+
function assertSupportedRuntime() {
|
|
11
|
+
const major = Number(process.versions.node.split(".")[0]);
|
|
12
|
+
if (major !== 24 && major !== 26) {
|
|
13
|
+
throw new Error(
|
|
14
|
+
`rwebtransport supports Node 24 and Node 26 only; this is Node ${process.versions.node}.`
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function prebuiltPath() {
|
|
19
|
+
return join(
|
|
20
|
+
PACKAGE_ROOT,
|
|
21
|
+
"prebuilds",
|
|
22
|
+
`${process.platform}-${process.arch}`,
|
|
23
|
+
"rwebtransport.node"
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
var cached;
|
|
27
|
+
function loadNative() {
|
|
28
|
+
if (cached) return cached;
|
|
29
|
+
assertSupportedRuntime();
|
|
30
|
+
const prebuilt = prebuiltPath();
|
|
31
|
+
if (existsSync(prebuilt)) {
|
|
32
|
+
cached = require2(prebuilt);
|
|
33
|
+
return cached;
|
|
34
|
+
}
|
|
35
|
+
const builder = join(PACKAGE_ROOT, "scripts", "build.js");
|
|
36
|
+
if (!existsSync(builder)) {
|
|
37
|
+
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).`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
process.stderr.write(
|
|
42
|
+
"rwebtransport: no prebuilt binary for this platform; compiling the native addon (this happens once)...\n"
|
|
43
|
+
);
|
|
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.`);
|
|
47
|
+
}
|
|
48
|
+
cached = require2(prebuilt);
|
|
49
|
+
return cached;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/errors.ts
|
|
53
|
+
var WebTransportError = class _WebTransportError extends Error {
|
|
54
|
+
/** Whether the error originated from a stream or the session as a whole. */
|
|
55
|
+
source;
|
|
56
|
+
/** The application error code, if the error came from a stream reset. */
|
|
57
|
+
streamErrorCode;
|
|
58
|
+
/**
|
|
59
|
+
* Creates a new {@link WebTransportError}.
|
|
60
|
+
*
|
|
61
|
+
* @param message - Human-readable description of the failure. When omitted,
|
|
62
|
+
* the message defaults to the string `'WebTransport error'`.
|
|
63
|
+
* @param options - Optional {@link WebTransportErrorOptions}; defaults to an
|
|
64
|
+
* empty object so that both fields fall back to their own defaults.
|
|
65
|
+
*
|
|
66
|
+
* @remarks
|
|
67
|
+
* Sets {@link Error.name} to `'WebTransportError'`, resolves
|
|
68
|
+
* {@link WebTransportError.source} (defaulting to `'session'`) and
|
|
69
|
+
* {@link WebTransportError.streamErrorCode} (defaulting to `null`), then
|
|
70
|
+
* restores the prototype chain with {@link Object.setPrototypeOf} so
|
|
71
|
+
* `instanceof` checks succeed under downlevel compilation.
|
|
72
|
+
*/
|
|
73
|
+
constructor(message, options = {}) {
|
|
74
|
+
super(message ?? "WebTransport error");
|
|
75
|
+
this.name = "WebTransportError";
|
|
76
|
+
this.source = options.source ?? "session";
|
|
77
|
+
this.streamErrorCode = options.streamErrorCode ?? null;
|
|
78
|
+
Object.setPrototypeOf(this, _WebTransportError.prototype);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// src/native.ts
|
|
83
|
+
function deferred() {
|
|
84
|
+
let resolve;
|
|
85
|
+
let reject;
|
|
86
|
+
const promise = new Promise((res, rej) => {
|
|
87
|
+
resolve = res;
|
|
88
|
+
reject = rej;
|
|
89
|
+
});
|
|
90
|
+
return { promise, resolve, reject };
|
|
91
|
+
}
|
|
92
|
+
var SessionCore = class {
|
|
93
|
+
/**
|
|
94
|
+
* The command transport, undefined until {@link SessionCore.attach} runs.
|
|
95
|
+
* While undefined, outbound commands are dropped or rejected.
|
|
96
|
+
*/
|
|
97
|
+
transport;
|
|
98
|
+
/** Monotonic source of request ids handed to the transport. */
|
|
99
|
+
nextRequestId = 1;
|
|
100
|
+
/** Pending {@link SessionCore.openStream} requests, keyed by request id. */
|
|
101
|
+
opens = /* @__PURE__ */ new Map();
|
|
102
|
+
/** Pending {@link SessionCore.write} requests, keyed by request id. */
|
|
103
|
+
writes = /* @__PURE__ */ new Map();
|
|
104
|
+
/** Pending {@link SessionCore.sendDatagram} requests, keyed by request id. */
|
|
105
|
+
datagramAcks = /* @__PURE__ */ new Map();
|
|
106
|
+
/** Active receive sinks, keyed by stream id. */
|
|
107
|
+
receives = /* @__PURE__ */ new Map();
|
|
108
|
+
/** Active send sinks, keyed by stream id. */
|
|
109
|
+
sends = /* @__PURE__ */ new Map();
|
|
110
|
+
/** Handler for peer-initiated streams, if one has been set. */
|
|
111
|
+
incoming;
|
|
112
|
+
/** Sink for inbound datagrams, if one has been set. */
|
|
113
|
+
datagramSink;
|
|
114
|
+
/** True once the session has reached its terminal (closed) state. */
|
|
115
|
+
closedState = false;
|
|
116
|
+
/** True once the session has been established (`ready`/`serverReady`). */
|
|
117
|
+
readyState = false;
|
|
118
|
+
/**
|
|
119
|
+
* Resolves when the session is established, rejects if it fails before then.
|
|
120
|
+
* Backs the public `WebTransport.ready` promise.
|
|
121
|
+
*/
|
|
122
|
+
ready = deferred();
|
|
123
|
+
/**
|
|
124
|
+
* Resolves with close info on a clean shutdown, rejects on an error or on a
|
|
125
|
+
* close that happens before the session is ready. Backs `WebTransport.closed`.
|
|
126
|
+
*/
|
|
127
|
+
closed = deferred();
|
|
128
|
+
/**
|
|
129
|
+
* Attach no-op catch handlers to {@link SessionCore.ready} and
|
|
130
|
+
* {@link SessionCore.closed} so that a session whose promises the user never
|
|
131
|
+
* observes does not trigger Node's unhandled-rejection warning.
|
|
132
|
+
*/
|
|
133
|
+
constructor() {
|
|
134
|
+
void this.closed.promise.catch(() => {
|
|
135
|
+
});
|
|
136
|
+
void this.ready.promise.catch(() => {
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Attach the transport once the underlying native handle exists.
|
|
141
|
+
*
|
|
142
|
+
* @param transport - The command sink for this session (client or server side).
|
|
143
|
+
*/
|
|
144
|
+
attach(transport) {
|
|
145
|
+
this.transport = transport;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Fail a session that could not be set up at all (e.g. client connect threw).
|
|
149
|
+
*
|
|
150
|
+
* @param message - Human-readable description of the setup failure.
|
|
151
|
+
* @remarks
|
|
152
|
+
* The failure is delivered on a microtask so the caller has a chance to
|
|
153
|
+
* attach `.ready`/`.closed` handlers before they reject.
|
|
154
|
+
*/
|
|
155
|
+
failSetup(message) {
|
|
156
|
+
queueMicrotask(
|
|
157
|
+
() => this.finish(null, new WebTransportError(message, { source: "session" }))
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* @returns The next request id, post-incrementing the internal counter.
|
|
162
|
+
*/
|
|
163
|
+
nextId() {
|
|
164
|
+
return this.nextRequestId++;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Open an outbound stream and await its assigned id.
|
|
168
|
+
*
|
|
169
|
+
* @param bidi - True for a bidirectional stream, false for unidirectional.
|
|
170
|
+
* @returns A promise for the new stream's id.
|
|
171
|
+
* @remarks
|
|
172
|
+
* Rejects immediately (without touching the transport) if the session is not
|
|
173
|
+
* usable (unattached or closed).
|
|
174
|
+
*/
|
|
175
|
+
openStream(bidi) {
|
|
176
|
+
if (!this.usable()) return Promise.reject(this.deadError());
|
|
177
|
+
const requestId = this.nextId();
|
|
178
|
+
const d = deferred();
|
|
179
|
+
this.opens.set(requestId, d);
|
|
180
|
+
this.transport.openStream(bidi, requestId);
|
|
181
|
+
return d.promise;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Write a chunk to a stream; resolves once the bytes are flushed into quiche
|
|
185
|
+
* (the write backpressure signal).
|
|
186
|
+
*
|
|
187
|
+
* @param streamId - The target stream id.
|
|
188
|
+
* @param chunk - The bytes to send.
|
|
189
|
+
* @returns A promise that resolves when the write is acknowledged.
|
|
190
|
+
* @remarks
|
|
191
|
+
* Rejects immediately if the session is not usable.
|
|
192
|
+
*/
|
|
193
|
+
write(streamId, chunk) {
|
|
194
|
+
if (!this.usable()) return Promise.reject(this.deadError());
|
|
195
|
+
const requestId = this.nextId();
|
|
196
|
+
const d = deferred();
|
|
197
|
+
this.writes.set(requestId, d);
|
|
198
|
+
this.transport.write(streamId, chunk, requestId);
|
|
199
|
+
return d.promise;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Finish (half-close) the send side of a stream. No-op if not attached.
|
|
203
|
+
*
|
|
204
|
+
* @param streamId - The stream to finish.
|
|
205
|
+
*/
|
|
206
|
+
finStream(streamId) {
|
|
207
|
+
this.transport?.fin(streamId);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Reset the send side of a stream. No-op if not attached.
|
|
211
|
+
*
|
|
212
|
+
* @param streamId - The stream to reset.
|
|
213
|
+
* @param code - The application error code; coerced to an unsigned 32-bit value.
|
|
214
|
+
*/
|
|
215
|
+
resetStream(streamId, code) {
|
|
216
|
+
this.transport?.reset(streamId, code >>> 0);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Ask the peer to stop sending on a stream. No-op if not attached.
|
|
220
|
+
*
|
|
221
|
+
* @param streamId - The stream whose inbound half should be stopped.
|
|
222
|
+
* @param code - The application error code; coerced to an unsigned 32-bit value.
|
|
223
|
+
*/
|
|
224
|
+
stopSending(streamId, code) {
|
|
225
|
+
this.transport?.stopSending(streamId, code >>> 0);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Toggle read backpressure on a stream. No-op if not attached.
|
|
229
|
+
*
|
|
230
|
+
* @param streamId - The receive stream.
|
|
231
|
+
* @param paused - True to pause native draining (flow-control the peer), false to resume.
|
|
232
|
+
*/
|
|
233
|
+
setPaused(streamId, paused) {
|
|
234
|
+
this.transport?.setPaused(streamId, paused);
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Send a datagram.
|
|
238
|
+
*
|
|
239
|
+
* @param chunk - The datagram payload.
|
|
240
|
+
* @returns A promise resolving to whether the datagram was actually sent;
|
|
241
|
+
* resolves to false (rather than rejecting) when the session is not usable.
|
|
242
|
+
*/
|
|
243
|
+
sendDatagram(chunk) {
|
|
244
|
+
if (!this.usable()) return Promise.resolve(false);
|
|
245
|
+
const requestId = this.nextId();
|
|
246
|
+
const d = deferred();
|
|
247
|
+
this.datagramAcks.set(requestId, d);
|
|
248
|
+
this.transport.sendDatagram(chunk, requestId);
|
|
249
|
+
return d.promise;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* @returns The maximum datagram payload size in bytes, or 0 if not attached.
|
|
253
|
+
*/
|
|
254
|
+
maxDatagramSize() {
|
|
255
|
+
return this.transport ? this.transport.maxDatagramSize() : 0;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Gracefully close the session. No-op if already closed or not attached.
|
|
259
|
+
*
|
|
260
|
+
* @param code - The WebTransport close code; coerced to an unsigned 32-bit value.
|
|
261
|
+
* @param reason - The close reason, UTF-8 encoded before it is sent.
|
|
262
|
+
*/
|
|
263
|
+
close(code, reason) {
|
|
264
|
+
if (this.closedState || !this.transport) return;
|
|
265
|
+
this.transport.closeSession(code >>> 0, new TextEncoder().encode(reason));
|
|
266
|
+
}
|
|
267
|
+
/** Tear down the native resources for this session. No-op if not attached. */
|
|
268
|
+
shutdown() {
|
|
269
|
+
this.transport?.shutdown();
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Whether outbound operations can still be issued (attached, not closed).
|
|
273
|
+
*
|
|
274
|
+
* @returns True when a transport is attached and the session is not closed.
|
|
275
|
+
*/
|
|
276
|
+
usable() {
|
|
277
|
+
return this.transport !== void 0 && !this.closedState;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* @returns The error used to reject operations attempted on a dead session.
|
|
281
|
+
*/
|
|
282
|
+
deadError() {
|
|
283
|
+
return new WebTransportError("session is closed", { source: "session" });
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Register a sink to receive a stream's inbound data/fin/reset events.
|
|
287
|
+
*
|
|
288
|
+
* @param streamId - The stream to observe.
|
|
289
|
+
* @param sink - The receiver of that stream's inbound events.
|
|
290
|
+
*/
|
|
291
|
+
registerReceive(streamId, sink) {
|
|
292
|
+
this.receives.set(streamId, sink);
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Stop delivering inbound events for a stream.
|
|
296
|
+
*
|
|
297
|
+
* @param streamId - The stream to forget.
|
|
298
|
+
*/
|
|
299
|
+
unregisterReceive(streamId) {
|
|
300
|
+
this.receives.delete(streamId);
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Register a sink to receive a stream's STOP_SENDING notifications.
|
|
304
|
+
*
|
|
305
|
+
* @param streamId - The stream to observe.
|
|
306
|
+
* @param sink - The receiver of that stream's send-side control events.
|
|
307
|
+
*/
|
|
308
|
+
registerSend(streamId, sink) {
|
|
309
|
+
this.sends.set(streamId, sink);
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Stop delivering send-side control events for a stream.
|
|
313
|
+
*
|
|
314
|
+
* @param streamId - The stream to forget.
|
|
315
|
+
*/
|
|
316
|
+
unregisterSend(streamId) {
|
|
317
|
+
this.sends.delete(streamId);
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Set the handler invoked when the peer opens a stream. Replaces any prior handler.
|
|
321
|
+
*
|
|
322
|
+
* @param handler - The consumer of peer-initiated streams.
|
|
323
|
+
*/
|
|
324
|
+
setIncomingHandler(handler) {
|
|
325
|
+
this.incoming = handler;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Set the sink invoked for each inbound datagram. Replaces any prior sink.
|
|
329
|
+
*
|
|
330
|
+
* @param sink - The consumer of inbound datagram payloads.
|
|
331
|
+
*/
|
|
332
|
+
setDatagramSink(sink) {
|
|
333
|
+
this.datagramSink = sink;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Route one native event to the appropriate promise, sink, or handler.
|
|
337
|
+
*
|
|
338
|
+
* @param ev - The event delivered by the native addon.
|
|
339
|
+
* @remarks
|
|
340
|
+
* This is the single entry point the native `onEvent` callback funnels into.
|
|
341
|
+
* `ready`/`serverReady` mark the session established and resolve `ready`;
|
|
342
|
+
* `closed`/`error` drive {@link SessionCore.finish}; the `stream*` events look
|
|
343
|
+
* up the matching sink (silently ignored if none is registered); and the
|
|
344
|
+
* `*Ack`/`streamOpened` events settle and then remove the pending deferred
|
|
345
|
+
* for their `requestId`.
|
|
346
|
+
*/
|
|
347
|
+
dispatch(ev) {
|
|
348
|
+
switch (ev.type) {
|
|
349
|
+
case "ready" /* Ready */:
|
|
350
|
+
case "serverReady" /* ServerReady */: {
|
|
351
|
+
this.readyState = true;
|
|
352
|
+
this.ready.resolve();
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
case "closed" /* Closed */: {
|
|
356
|
+
const reason = new TextDecoder().decode(ev.reason);
|
|
357
|
+
this.finish({ closeCode: ev.code, reason }, void 0);
|
|
358
|
+
break;
|
|
359
|
+
}
|
|
360
|
+
case "error" /* Error */: {
|
|
361
|
+
const err = new WebTransportError(ev.message, { source: "session" });
|
|
362
|
+
this.finish(null, err);
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
case "datagram" /* Datagram */: {
|
|
366
|
+
this.datagramSink?.(ev.data);
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
case "stream" /* Stream */: {
|
|
370
|
+
if (ev.bidi) this.incoming?.onBidi(ev.streamId);
|
|
371
|
+
else this.incoming?.onUni(ev.streamId);
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
case "streamData" /* StreamData */: {
|
|
375
|
+
this.receives.get(ev.streamId)?.onData(ev.data);
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
case "streamFin" /* StreamFin */: {
|
|
379
|
+
this.receives.get(ev.streamId)?.onFin();
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
case "streamReset" /* StreamReset */: {
|
|
383
|
+
this.receives.get(ev.streamId)?.onReset(ev.code);
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
case "streamStopSending" /* StreamStopSending */: {
|
|
387
|
+
this.sends.get(ev.streamId)?.onStopSending(ev.code);
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
case "streamOpened" /* StreamOpened */: {
|
|
391
|
+
this.opens.get(ev.requestId)?.resolve(ev.streamId);
|
|
392
|
+
this.opens.delete(ev.requestId);
|
|
393
|
+
break;
|
|
394
|
+
}
|
|
395
|
+
case "writeAck" /* WriteAck */: {
|
|
396
|
+
this.writes.get(ev.requestId)?.resolve();
|
|
397
|
+
this.writes.delete(ev.requestId);
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
case "datagramAck" /* DatagramAck */: {
|
|
401
|
+
this.datagramAcks.get(ev.requestId)?.resolve(ev.sent);
|
|
402
|
+
this.datagramAcks.delete(ev.requestId);
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Terminal transition: resolve/reject everything and tear down.
|
|
409
|
+
*
|
|
410
|
+
* @param info - Close info for a clean close, or null when closing on error.
|
|
411
|
+
* @param error - The error that ended the session, or undefined for a clean close.
|
|
412
|
+
* @remarks
|
|
413
|
+
* Idempotent: returns immediately if the session already closed. Settles the
|
|
414
|
+
* lifecycle promises according to state: if the session never became ready,
|
|
415
|
+
* both `ready` and `closed` reject (a pre-ready close is a failed connect); if
|
|
416
|
+
* it was ready and `error` is set, only `closed` rejects; otherwise `closed`
|
|
417
|
+
* resolves with `info` (defaulting to code 0 / empty reason). It then rejects
|
|
418
|
+
* every pending open and write, resolves every pending datagram ack to false,
|
|
419
|
+
* signals a reset (code 0) to all receive sinks and STOP_SENDING (code 0) to
|
|
420
|
+
* all send sinks, clears the bookkeeping maps, and finally shuts down the
|
|
421
|
+
* transport.
|
|
422
|
+
*/
|
|
423
|
+
finish(info, error) {
|
|
424
|
+
if (this.closedState) return;
|
|
425
|
+
this.closedState = true;
|
|
426
|
+
if (!this.readyState) {
|
|
427
|
+
const err2 = error ?? new WebTransportError("session closed before it was established", {
|
|
428
|
+
source: "session"
|
|
429
|
+
});
|
|
430
|
+
this.ready.reject(err2);
|
|
431
|
+
this.closed.reject(err2);
|
|
432
|
+
} else if (error) {
|
|
433
|
+
this.closed.reject(error);
|
|
434
|
+
} else {
|
|
435
|
+
this.closed.resolve(info ?? { closeCode: 0, reason: "" });
|
|
436
|
+
}
|
|
437
|
+
const err = error ?? new WebTransportError("session closed", { source: "session" });
|
|
438
|
+
for (const d of this.opens.values()) d.reject(err);
|
|
439
|
+
for (const d of this.writes.values()) d.reject(err);
|
|
440
|
+
for (const d of this.datagramAcks.values()) d.resolve(false);
|
|
441
|
+
for (const sink of this.receives.values()) sink.onReset(0);
|
|
442
|
+
for (const sink of this.sends.values()) sink.onStopSending(0);
|
|
443
|
+
this.opens.clear();
|
|
444
|
+
this.writes.clear();
|
|
445
|
+
this.datagramAcks.clear();
|
|
446
|
+
this.receives.clear();
|
|
447
|
+
this.sends.clear();
|
|
448
|
+
this.transport?.shutdown();
|
|
449
|
+
}
|
|
450
|
+
/** @returns Whether the session has reached its terminal closed state. */
|
|
451
|
+
get isClosed() {
|
|
452
|
+
return this.closedState;
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
var ClientTransport = class {
|
|
456
|
+
/**
|
|
457
|
+
* @param native - The loaded native addon.
|
|
458
|
+
* @param handle - The client session handle these commands target.
|
|
459
|
+
*/
|
|
460
|
+
constructor(native, handle) {
|
|
461
|
+
this.native = native;
|
|
462
|
+
this.handle = handle;
|
|
463
|
+
}
|
|
464
|
+
native;
|
|
465
|
+
handle;
|
|
466
|
+
/**
|
|
467
|
+
* @param bidi - True for bidirectional, false for unidirectional.
|
|
468
|
+
* @param requestId - Id correlating the eventual `streamOpened` event.
|
|
469
|
+
*/
|
|
470
|
+
openStream(bidi, requestId) {
|
|
471
|
+
this.native.openStream(this.handle, bidi, requestId);
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* @param streamId - The target stream id.
|
|
475
|
+
* @param bytes - The payload.
|
|
476
|
+
* @param requestId - Id correlating the eventual `writeAck` event.
|
|
477
|
+
*/
|
|
478
|
+
write(streamId, bytes, requestId) {
|
|
479
|
+
this.native.writeStream(this.handle, streamId, bytes, requestId);
|
|
480
|
+
}
|
|
481
|
+
/** @param streamId - The stream to finish. */
|
|
482
|
+
fin(streamId) {
|
|
483
|
+
this.native.finStream(this.handle, streamId);
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* @param streamId - The stream to reset.
|
|
487
|
+
* @param code - The application error code.
|
|
488
|
+
*/
|
|
489
|
+
reset(streamId, code) {
|
|
490
|
+
this.native.resetStream(this.handle, streamId, code);
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* @param streamId - The stream whose inbound half should be stopped.
|
|
494
|
+
* @param code - The application error code.
|
|
495
|
+
*/
|
|
496
|
+
stopSending(streamId, code) {
|
|
497
|
+
this.native.stopSending(this.handle, streamId, code);
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* @param streamId - The receive stream.
|
|
501
|
+
* @param paused - True to pause draining, false to resume.
|
|
502
|
+
*/
|
|
503
|
+
setPaused(streamId, paused) {
|
|
504
|
+
this.native.setPaused(this.handle, streamId, paused);
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* @param bytes - The datagram payload.
|
|
508
|
+
* @param requestId - Id correlating the eventual `datagramAck` event.
|
|
509
|
+
*/
|
|
510
|
+
sendDatagram(bytes, requestId) {
|
|
511
|
+
this.native.sendDatagram(this.handle, bytes, requestId);
|
|
512
|
+
}
|
|
513
|
+
/** @returns The maximum datagram payload size, in bytes. */
|
|
514
|
+
maxDatagramSize() {
|
|
515
|
+
return this.native.maxDatagramSize(this.handle);
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* @param code - The WebTransport close code.
|
|
519
|
+
* @param reason - The UTF-8 encoded close reason.
|
|
520
|
+
*/
|
|
521
|
+
closeSession(code, reason) {
|
|
522
|
+
this.native.closeSession(this.handle, code, reason);
|
|
523
|
+
}
|
|
524
|
+
/** Tear down the session's native driver thread. */
|
|
525
|
+
shutdown() {
|
|
526
|
+
this.native.shutdown(this.handle);
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
function createClientSession(config) {
|
|
530
|
+
const native = loadNative();
|
|
531
|
+
const core = new SessionCore();
|
|
532
|
+
try {
|
|
533
|
+
const handle = native.connect(
|
|
534
|
+
config.url,
|
|
535
|
+
config.hashes,
|
|
536
|
+
config.insecure,
|
|
537
|
+
config.origin,
|
|
538
|
+
config.headerNames,
|
|
539
|
+
config.headerValues,
|
|
540
|
+
(ev) => core.dispatch(ev)
|
|
541
|
+
);
|
|
542
|
+
core.attach(new ClientTransport(native, handle));
|
|
543
|
+
} catch (e) {
|
|
544
|
+
core.failSetup(e instanceof Error ? e.message : String(e));
|
|
545
|
+
}
|
|
546
|
+
return core;
|
|
547
|
+
}
|
|
548
|
+
var ServerTransport = class {
|
|
549
|
+
/**
|
|
550
|
+
* @param native - The loaded native addon.
|
|
551
|
+
* @param handle - The server handle these commands target.
|
|
552
|
+
* @param session - The id of the session these commands act on.
|
|
553
|
+
*/
|
|
554
|
+
constructor(native, handle, session) {
|
|
555
|
+
this.native = native;
|
|
556
|
+
this.handle = handle;
|
|
557
|
+
this.session = session;
|
|
558
|
+
}
|
|
559
|
+
native;
|
|
560
|
+
handle;
|
|
561
|
+
session;
|
|
562
|
+
/**
|
|
563
|
+
* @param bidi - True for bidirectional, false for unidirectional.
|
|
564
|
+
* @param requestId - Id correlating the eventual `streamOpened` event.
|
|
565
|
+
*/
|
|
566
|
+
openStream(bidi, requestId) {
|
|
567
|
+
this.native.serverOpenStream(this.handle, this.session, bidi, requestId);
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* @param streamId - The target stream id.
|
|
571
|
+
* @param bytes - The payload.
|
|
572
|
+
* @param requestId - Id correlating the eventual `writeAck` event.
|
|
573
|
+
*/
|
|
574
|
+
write(streamId, bytes, requestId) {
|
|
575
|
+
this.native.serverWrite(this.handle, this.session, streamId, bytes, requestId);
|
|
576
|
+
}
|
|
577
|
+
/** @param streamId - The stream to finish. */
|
|
578
|
+
fin(streamId) {
|
|
579
|
+
this.native.serverFin(this.handle, this.session, streamId);
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* @param streamId - The stream to reset.
|
|
583
|
+
* @param code - The application error code.
|
|
584
|
+
*/
|
|
585
|
+
reset(streamId, code) {
|
|
586
|
+
this.native.serverReset(this.handle, this.session, streamId, code);
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* @param streamId - The stream whose inbound half should be stopped.
|
|
590
|
+
* @param code - The application error code.
|
|
591
|
+
*/
|
|
592
|
+
stopSending(streamId, code) {
|
|
593
|
+
this.native.serverStopSending(this.handle, this.session, streamId, code);
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* @param streamId - The receive stream.
|
|
597
|
+
* @param paused - True to pause draining, false to resume.
|
|
598
|
+
*/
|
|
599
|
+
setPaused(streamId, paused) {
|
|
600
|
+
this.native.serverSetPaused(this.handle, this.session, streamId, paused);
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* @param bytes - The datagram payload.
|
|
604
|
+
* @param requestId - Id correlating the eventual `datagramAck` event.
|
|
605
|
+
*/
|
|
606
|
+
sendDatagram(bytes, requestId) {
|
|
607
|
+
this.native.serverSendDatagram(this.handle, this.session, bytes, requestId);
|
|
608
|
+
}
|
|
609
|
+
/** @returns The maximum datagram payload size, in bytes. */
|
|
610
|
+
maxDatagramSize() {
|
|
611
|
+
return this.native.serverMaxDatagramSize(this.handle);
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* @param code - The WebTransport close code.
|
|
615
|
+
* @param reason - The UTF-8 encoded close reason.
|
|
616
|
+
*/
|
|
617
|
+
closeSession(code, reason) {
|
|
618
|
+
this.native.serverCloseSession(this.handle, this.session, code, reason);
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* No-op: the server driver owns the session lifecycle, so there is nothing to
|
|
622
|
+
* tear down per session on the JS side.
|
|
623
|
+
*/
|
|
624
|
+
shutdown() {
|
|
625
|
+
}
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
// src/datagrams.ts
|
|
629
|
+
var WebTransportDatagramDuplexStream = class {
|
|
630
|
+
/**
|
|
631
|
+
* The underlying session used to send datagrams, register the inbound sink,
|
|
632
|
+
* query the maximum datagram size, and observe session closure.
|
|
633
|
+
*
|
|
634
|
+
* @internal
|
|
635
|
+
*/
|
|
636
|
+
session;
|
|
637
|
+
/**
|
|
638
|
+
* Stream of inbound datagrams. Each chunk is one received datagram payload.
|
|
639
|
+
* Backed by a {@link CountQueuingStrategy} whose high water mark is the
|
|
640
|
+
* value of {@link incomingHighWaterMark} captured when this instance was
|
|
641
|
+
* constructed. The stream is closed automatically when the session ends.
|
|
642
|
+
*/
|
|
643
|
+
readable;
|
|
644
|
+
/**
|
|
645
|
+
* Stream for sending outbound datagrams. Writing a chunk forwards it to
|
|
646
|
+
* {@link SessionCore.sendDatagram}, whose boolean result (whether the
|
|
647
|
+
* datagram was actually transmitted rather than dropped) is discarded, so
|
|
648
|
+
* that `write()` resolves once the native transport reports back on the
|
|
649
|
+
* send attempt. Backed by a {@link CountQueuingStrategy} whose high water
|
|
650
|
+
* mark is the value of {@link outgoingHighWaterMark} captured when this
|
|
651
|
+
* instance was constructed.
|
|
652
|
+
*/
|
|
653
|
+
writable;
|
|
654
|
+
/**
|
|
655
|
+
* High water mark (in datagrams) for the inbound `readable` queue. Read
|
|
656
|
+
* back through {@link incomingHighWaterMark}. Only the value present at
|
|
657
|
+
* construction time is applied to the actual {@link readable} strategy;
|
|
658
|
+
* later changes update the reported number but do not resize the live
|
|
659
|
+
* queue.
|
|
660
|
+
*
|
|
661
|
+
* @defaultValue 64
|
|
662
|
+
* @internal
|
|
663
|
+
*/
|
|
664
|
+
incomingHwm = 64;
|
|
665
|
+
/**
|
|
666
|
+
* High water mark (in datagrams) for the outbound `writable` queue. Read
|
|
667
|
+
* back through {@link outgoingHighWaterMark}. Only the value present at
|
|
668
|
+
* construction time is applied to the actual {@link writable} strategy;
|
|
669
|
+
* later changes update the reported number but do not resize the live
|
|
670
|
+
* queue.
|
|
671
|
+
*
|
|
672
|
+
* @defaultValue 64
|
|
673
|
+
* @internal
|
|
674
|
+
*/
|
|
675
|
+
outgoingHwm = 64;
|
|
676
|
+
/**
|
|
677
|
+
* Max age (ms) an inbound datagram is retained before being dropped, or
|
|
678
|
+
* `null` for no limit.
|
|
679
|
+
*
|
|
680
|
+
* @remarks
|
|
681
|
+
* Present to match the WHATWG interface. This class stores the value but
|
|
682
|
+
* does not currently enforce it: inbound loss here is driven only by the
|
|
683
|
+
* `readable` queue being full, not by datagram age.
|
|
684
|
+
*
|
|
685
|
+
* @defaultValue null
|
|
686
|
+
*/
|
|
687
|
+
incomingMaxAge = null;
|
|
688
|
+
/**
|
|
689
|
+
* Max age (ms) an outbound datagram waits to be sent before being dropped,
|
|
690
|
+
* or `null` for no limit.
|
|
691
|
+
*
|
|
692
|
+
* @remarks
|
|
693
|
+
* Present to match the WHATWG interface. This class stores the value but
|
|
694
|
+
* does not currently enforce it; outbound loss is governed by the
|
|
695
|
+
* `writable` queue backpressure and the native QUIC layer.
|
|
696
|
+
*
|
|
697
|
+
* @defaultValue null
|
|
698
|
+
*/
|
|
699
|
+
outgoingMaxAge = null;
|
|
700
|
+
/**
|
|
701
|
+
* Build the datagram duplex stream over a session.
|
|
702
|
+
*
|
|
703
|
+
* @param session - The session that carries the datagrams. Its
|
|
704
|
+
* {@link SessionCore.setDatagramSink} is used to receive inbound datagrams,
|
|
705
|
+
* {@link SessionCore.sendDatagram} to send them, and its
|
|
706
|
+
* {@link SessionCore.closed} promise to know when to close the inbound
|
|
707
|
+
* stream.
|
|
708
|
+
*
|
|
709
|
+
* @remarks
|
|
710
|
+
* Wiring performed here, in order:
|
|
711
|
+
*
|
|
712
|
+
* 1. Creates {@link readable} and captures its controller from `start`.
|
|
713
|
+
* 2. Installs a datagram sink that enqueues each received datagram only
|
|
714
|
+
* while the controller's `desiredSize` is positive (treating an unknown
|
|
715
|
+
* `desiredSize` of `null` as `1`, i.e. room available); datagrams that
|
|
716
|
+
* arrive when the queue is full are dropped.
|
|
717
|
+
* 3. Subscribes to the session's `closed` promise so the inbound stream is
|
|
718
|
+
* closed on both clean close and error, preventing a pending `read()`
|
|
719
|
+
* from hanging forever. A double-close is swallowed.
|
|
720
|
+
* 4. Creates {@link writable} whose `write` forwards each chunk to
|
|
721
|
+
* `session.sendDatagram` and maps the boolean ack to `undefined`.
|
|
722
|
+
*/
|
|
723
|
+
constructor(session) {
|
|
724
|
+
this.session = session;
|
|
725
|
+
let rController;
|
|
726
|
+
this.readable = new ReadableStream(
|
|
727
|
+
{
|
|
728
|
+
start(c) {
|
|
729
|
+
rController = c;
|
|
730
|
+
}
|
|
731
|
+
},
|
|
732
|
+
new CountQueuingStrategy({ highWaterMark: this.incomingHwm })
|
|
733
|
+
);
|
|
734
|
+
session.setDatagramSink((data) => {
|
|
735
|
+
if ((rController.desiredSize ?? 1) > 0) {
|
|
736
|
+
rController.enqueue(data);
|
|
737
|
+
}
|
|
738
|
+
});
|
|
739
|
+
void session.closed.promise.then(
|
|
740
|
+
() => {
|
|
741
|
+
try {
|
|
742
|
+
rController.close();
|
|
743
|
+
} catch {
|
|
744
|
+
}
|
|
745
|
+
},
|
|
746
|
+
() => {
|
|
747
|
+
try {
|
|
748
|
+
rController.close();
|
|
749
|
+
} catch {
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
);
|
|
753
|
+
this.writable = new WritableStream(
|
|
754
|
+
{
|
|
755
|
+
write: (chunk) => session.sendDatagram(chunk).then(() => void 0)
|
|
756
|
+
},
|
|
757
|
+
new CountQueuingStrategy({ highWaterMark: this.outgoingHwm })
|
|
758
|
+
);
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* The largest datagram payload that currently fits in a single packet.
|
|
762
|
+
*
|
|
763
|
+
* @returns The maximum datagram payload size in bytes as reported by the
|
|
764
|
+
* native transport, or `0` when the session has no transport attached yet.
|
|
765
|
+
* The value can change over the life of the session as the path MTU is
|
|
766
|
+
* discovered.
|
|
767
|
+
*/
|
|
768
|
+
get maxDatagramSize() {
|
|
769
|
+
return this.session.maxDatagramSize();
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* The high water mark (in datagrams) reported for the inbound `readable`
|
|
773
|
+
* queue.
|
|
774
|
+
*
|
|
775
|
+
* @returns The most recently set incoming high water mark.
|
|
776
|
+
*/
|
|
777
|
+
get incomingHighWaterMark() {
|
|
778
|
+
return this.incomingHwm;
|
|
779
|
+
}
|
|
780
|
+
/**
|
|
781
|
+
* Set the reported inbound high water mark.
|
|
782
|
+
*
|
|
783
|
+
* @param value - Desired high water mark; coerced to an integer with
|
|
784
|
+
* `value | 0` and clamped to a minimum of `1`.
|
|
785
|
+
*
|
|
786
|
+
* @remarks
|
|
787
|
+
* This updates only the number returned by {@link incomingHighWaterMark}.
|
|
788
|
+
* The live {@link readable} queue keeps the strategy fixed at construction
|
|
789
|
+
* time, so changing this after construction does not resize it.
|
|
790
|
+
*/
|
|
791
|
+
set incomingHighWaterMark(value) {
|
|
792
|
+
this.incomingHwm = Math.max(1, value | 0);
|
|
793
|
+
}
|
|
794
|
+
/**
|
|
795
|
+
* The high water mark (in datagrams) reported for the outbound `writable`
|
|
796
|
+
* queue.
|
|
797
|
+
*
|
|
798
|
+
* @returns The most recently set outgoing high water mark.
|
|
799
|
+
*/
|
|
800
|
+
get outgoingHighWaterMark() {
|
|
801
|
+
return this.outgoingHwm;
|
|
802
|
+
}
|
|
803
|
+
/**
|
|
804
|
+
* Set the reported outbound high water mark.
|
|
805
|
+
*
|
|
806
|
+
* @param value - Desired high water mark; coerced to an integer with
|
|
807
|
+
* `value | 0` and clamped to a minimum of `1`.
|
|
808
|
+
*
|
|
809
|
+
* @remarks
|
|
810
|
+
* This updates only the number returned by {@link outgoingHighWaterMark}.
|
|
811
|
+
* The live {@link writable} queue keeps the strategy fixed at construction
|
|
812
|
+
* time, so changing this after construction does not resize it.
|
|
813
|
+
*/
|
|
814
|
+
set outgoingHighWaterMark(value) {
|
|
815
|
+
this.outgoingHwm = Math.max(1, value | 0);
|
|
816
|
+
}
|
|
817
|
+
};
|
|
818
|
+
|
|
819
|
+
// src/streams.ts
|
|
820
|
+
function errorCode(reason) {
|
|
821
|
+
if (reason instanceof WebTransportError && reason.streamErrorCode != null) {
|
|
822
|
+
return reason.streamErrorCode >>> 0;
|
|
823
|
+
}
|
|
824
|
+
return 0;
|
|
825
|
+
}
|
|
826
|
+
var WebTransportReceiveStream = class extends ReadableStream {
|
|
827
|
+
/**
|
|
828
|
+
* The QUIC stream id of the underlying stream. Stable for the lifetime of the
|
|
829
|
+
* stream and shared with the paired {@link WebTransportSendStream} when this is
|
|
830
|
+
* one half of a {@link WebTransportBidirectionalStream}.
|
|
831
|
+
*/
|
|
832
|
+
streamId;
|
|
833
|
+
/**
|
|
834
|
+
* Wire a `ReadableStream` up to a native receive stream.
|
|
835
|
+
*
|
|
836
|
+
* @param session - The session core that dispatches native events and issues
|
|
837
|
+
* flow-control and teardown commands for this stream.
|
|
838
|
+
* @param streamId - The QUIC stream id to receive from.
|
|
839
|
+
* @remarks Installs the `ReadableStream` underlying source and registers a
|
|
840
|
+
* {@link ReceiveSink} on `session` for `streamId`:
|
|
841
|
+
*
|
|
842
|
+
* - `pull` unpauses the native stream via `setPaused(streamId, false)`, so
|
|
843
|
+
* quiche resumes draining inbound bytes when the consumer wants more.
|
|
844
|
+
* - `cancel(reason)` sends `STOP_SENDING` to the peer with the code from
|
|
845
|
+
* {@link errorCode} and then unregisters the receive sink.
|
|
846
|
+
* - The sink's `onData` enqueues each chunk and, once the controller's
|
|
847
|
+
* `desiredSize` falls to zero or below (a nullish `desiredSize` is treated as
|
|
848
|
+
* `1`, so it does not pause), pauses the native stream to flow-control the
|
|
849
|
+
* peer.
|
|
850
|
+
* - The sink's `onFin` closes the controller (swallowing the error if it was
|
|
851
|
+
* already closed or errored) and unregisters.
|
|
852
|
+
* - The sink's `onReset(code)` errors the controller with a `'stream'`-sourced
|
|
853
|
+
* {@link WebTransportError} ('stream reset by peer') carrying the peer's code,
|
|
854
|
+
* then unregisters.
|
|
855
|
+
*
|
|
856
|
+
* A {@link CountQueuingStrategy} with a high-water mark of 32 chunks bounds the
|
|
857
|
+
* JS-side queue and thereby the pause threshold.
|
|
858
|
+
*/
|
|
859
|
+
constructor(session, streamId) {
|
|
860
|
+
let controller;
|
|
861
|
+
super(
|
|
862
|
+
{
|
|
863
|
+
start(c) {
|
|
864
|
+
controller = c;
|
|
865
|
+
},
|
|
866
|
+
pull() {
|
|
867
|
+
session.setPaused(streamId, false);
|
|
868
|
+
},
|
|
869
|
+
cancel(reason) {
|
|
870
|
+
session.stopSending(streamId, errorCode(reason));
|
|
871
|
+
session.unregisterReceive(streamId);
|
|
872
|
+
}
|
|
873
|
+
},
|
|
874
|
+
new CountQueuingStrategy({ highWaterMark: 32 })
|
|
875
|
+
);
|
|
876
|
+
session.registerReceive(streamId, {
|
|
877
|
+
onData(chunk) {
|
|
878
|
+
controller.enqueue(chunk);
|
|
879
|
+
if ((controller.desiredSize ?? 1) <= 0) {
|
|
880
|
+
session.setPaused(streamId, true);
|
|
881
|
+
}
|
|
882
|
+
},
|
|
883
|
+
onFin() {
|
|
884
|
+
try {
|
|
885
|
+
controller.close();
|
|
886
|
+
} catch {
|
|
887
|
+
}
|
|
888
|
+
session.unregisterReceive(streamId);
|
|
889
|
+
},
|
|
890
|
+
onReset(code) {
|
|
891
|
+
controller.error(
|
|
892
|
+
new WebTransportError("stream reset by peer", {
|
|
893
|
+
source: "stream",
|
|
894
|
+
streamErrorCode: code
|
|
895
|
+
})
|
|
896
|
+
);
|
|
897
|
+
session.unregisterReceive(streamId);
|
|
898
|
+
}
|
|
899
|
+
});
|
|
900
|
+
this.streamId = streamId;
|
|
901
|
+
}
|
|
902
|
+
};
|
|
903
|
+
var WebTransportSendStream = class extends WritableStream {
|
|
904
|
+
/**
|
|
905
|
+
* The QUIC stream id of the underlying stream. Stable for the lifetime of the
|
|
906
|
+
* stream and shared with the paired {@link WebTransportReceiveStream} when this is
|
|
907
|
+
* one half of a {@link WebTransportBidirectionalStream}.
|
|
908
|
+
*/
|
|
909
|
+
streamId;
|
|
910
|
+
/**
|
|
911
|
+
* Wire a `WritableStream` up to a native send stream.
|
|
912
|
+
*
|
|
913
|
+
* @param session - The session core that dispatches native events and issues
|
|
914
|
+
* write, FIN, reset, and teardown commands for this stream.
|
|
915
|
+
* @param streamId - The QUIC stream id to send on.
|
|
916
|
+
* @remarks Installs the `WritableStream` underlying sink and registers a
|
|
917
|
+
* {@link SendSink} on `session` for `streamId`:
|
|
918
|
+
*
|
|
919
|
+
* - `write(chunk)` forwards to `session.write` and returns its promise, which
|
|
920
|
+
* settles only once the bytes are accepted into quiche's send buffer; awaiting
|
|
921
|
+
* that promise is what applies end-to-end backpressure.
|
|
922
|
+
* - `close` sends a FIN via `finStream` and unregisters the send sink.
|
|
923
|
+
* - `abort(reason)` resets the stream via `resetStream` with the code from
|
|
924
|
+
* {@link errorCode} and unregisters the send sink.
|
|
925
|
+
* - The sink's `onStopSending(code)` errors the controller with a
|
|
926
|
+
* `'stream'`-sourced {@link WebTransportError} ('peer sent STOP_SENDING')
|
|
927
|
+
* carrying the peer's code (swallowing the error if the stream was already
|
|
928
|
+
* errored or closed) and unregisters.
|
|
929
|
+
*
|
|
930
|
+
* A {@link ByteLengthQueuingStrategy} with a high-water mark of 1 MiB
|
|
931
|
+
* (`1024 * 1024` bytes) bounds the JS-side write queue.
|
|
932
|
+
*/
|
|
933
|
+
constructor(session, streamId) {
|
|
934
|
+
let controller;
|
|
935
|
+
super(
|
|
936
|
+
{
|
|
937
|
+
start(c) {
|
|
938
|
+
controller = c;
|
|
939
|
+
},
|
|
940
|
+
write(chunk) {
|
|
941
|
+
return session.write(streamId, chunk);
|
|
942
|
+
},
|
|
943
|
+
close() {
|
|
944
|
+
session.finStream(streamId);
|
|
945
|
+
session.unregisterSend(streamId);
|
|
946
|
+
},
|
|
947
|
+
abort(reason) {
|
|
948
|
+
session.resetStream(streamId, errorCode(reason));
|
|
949
|
+
session.unregisterSend(streamId);
|
|
950
|
+
}
|
|
951
|
+
},
|
|
952
|
+
new ByteLengthQueuingStrategy({ highWaterMark: 1024 * 1024 })
|
|
953
|
+
);
|
|
954
|
+
session.registerSend(streamId, {
|
|
955
|
+
onStopSending(code) {
|
|
956
|
+
try {
|
|
957
|
+
controller.error(
|
|
958
|
+
new WebTransportError("peer sent STOP_SENDING", {
|
|
959
|
+
source: "stream",
|
|
960
|
+
streamErrorCode: code
|
|
961
|
+
})
|
|
962
|
+
);
|
|
963
|
+
} catch {
|
|
964
|
+
}
|
|
965
|
+
session.unregisterSend(streamId);
|
|
966
|
+
}
|
|
967
|
+
});
|
|
968
|
+
this.streamId = streamId;
|
|
969
|
+
}
|
|
970
|
+
};
|
|
971
|
+
var WebTransportBidirectionalStream = class {
|
|
972
|
+
/** The readable half, delivering bytes the peer sends on this stream. */
|
|
973
|
+
readable;
|
|
974
|
+
/** The writable half, sending bytes to the peer on this stream. */
|
|
975
|
+
writable;
|
|
976
|
+
/**
|
|
977
|
+
* Construct both halves over the same QUIC stream.
|
|
978
|
+
*
|
|
979
|
+
* @param session - The session core backing both halves.
|
|
980
|
+
* @param streamId - The QUIC stream id shared by the readable and writable halves.
|
|
981
|
+
*/
|
|
982
|
+
constructor(session, streamId) {
|
|
983
|
+
this.readable = new WebTransportReceiveStream(session, streamId);
|
|
984
|
+
this.writable = new WebTransportSendStream(session, streamId);
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
// src/webtransport.ts
|
|
989
|
+
function toBytes(src) {
|
|
990
|
+
if (src instanceof ArrayBuffer) return new Uint8Array(src);
|
|
991
|
+
return new Uint8Array(src.buffer, src.byteOffset, src.byteLength);
|
|
992
|
+
}
|
|
993
|
+
function buildConfig(url, options) {
|
|
994
|
+
if (typeof url !== "string" || !url.startsWith("https://")) {
|
|
995
|
+
throw new WebTransportError(`invalid WebTransport URL: ${url}`, { source: "session" });
|
|
996
|
+
}
|
|
997
|
+
const hashes = [];
|
|
998
|
+
for (const hash of options.serverCertificateHashes ?? []) {
|
|
999
|
+
const algorithm = hash.algorithm.toLowerCase();
|
|
1000
|
+
if (algorithm !== "sha-256") {
|
|
1001
|
+
throw new WebTransportError(
|
|
1002
|
+
`unsupported certificate hash algorithm: ${hash.algorithm} (only sha-256)`,
|
|
1003
|
+
{ source: "session" }
|
|
1004
|
+
);
|
|
1005
|
+
}
|
|
1006
|
+
const bytes = toBytes(hash.value);
|
|
1007
|
+
if (bytes.byteLength !== 32) {
|
|
1008
|
+
throw new WebTransportError("sha-256 certificate hash must be 32 bytes", {
|
|
1009
|
+
source: "session"
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
hashes.push(bytes);
|
|
1013
|
+
}
|
|
1014
|
+
const headerNames = [];
|
|
1015
|
+
const headerValues = [];
|
|
1016
|
+
for (const [name, value] of Object.entries(options.headers ?? {})) {
|
|
1017
|
+
headerNames.push(name);
|
|
1018
|
+
headerValues.push(value);
|
|
1019
|
+
}
|
|
1020
|
+
return {
|
|
1021
|
+
url,
|
|
1022
|
+
hashes,
|
|
1023
|
+
insecure: options.insecure ?? false,
|
|
1024
|
+
origin: options.origin ?? null,
|
|
1025
|
+
headerNames,
|
|
1026
|
+
headerValues
|
|
1027
|
+
};
|
|
1028
|
+
}
|
|
1029
|
+
var WebTransportSession = class {
|
|
1030
|
+
/**
|
|
1031
|
+
* The event dispatcher and command surface backing this session. Shared with
|
|
1032
|
+
* the datagram and stream wrappers so they can issue native commands and
|
|
1033
|
+
* register their inbound sinks. Protected so the server-side subclass can
|
|
1034
|
+
* reach it while it stays hidden from public API consumers.
|
|
1035
|
+
* @internal
|
|
1036
|
+
*/
|
|
1037
|
+
core;
|
|
1038
|
+
/**
|
|
1039
|
+
* The session's datagram transport: a {@link WebTransportDatagramDuplexStream}
|
|
1040
|
+
* whose `readable` yields inbound datagrams and `writable` sends them.
|
|
1041
|
+
* Datagrams are unreliable and unordered; ones that do not fit the queue are
|
|
1042
|
+
* dropped rather than buffered.
|
|
1043
|
+
*/
|
|
1044
|
+
datagrams;
|
|
1045
|
+
/**
|
|
1046
|
+
* A `ReadableStream` that yields a {@link WebTransportBidirectionalStream} for
|
|
1047
|
+
* every bidirectional stream the peer opens. Fed by the core's incoming
|
|
1048
|
+
* handler; it closes when the session closes cleanly and errors if the
|
|
1049
|
+
* session terminates abnormally.
|
|
1050
|
+
*/
|
|
1051
|
+
incomingBidirectionalStreams;
|
|
1052
|
+
/**
|
|
1053
|
+
* A `ReadableStream` that yields a receive-only
|
|
1054
|
+
* {@link WebTransportReceiveStream} for every unidirectional stream the peer
|
|
1055
|
+
* opens. Fed by the core's incoming handler; it closes on clean session close
|
|
1056
|
+
* and errors on abnormal termination.
|
|
1057
|
+
*/
|
|
1058
|
+
incomingUnidirectionalStreams;
|
|
1059
|
+
/**
|
|
1060
|
+
* Guards {@link WebTransportSession.close} so that repeated calls are no-ops
|
|
1061
|
+
* and the native close is issued at most once.
|
|
1062
|
+
* @internal
|
|
1063
|
+
*/
|
|
1064
|
+
closeCalled = false;
|
|
1065
|
+
/**
|
|
1066
|
+
* Wire up the session surface around an existing {@link SessionCore}.
|
|
1067
|
+
*
|
|
1068
|
+
* @param core - The event dispatcher and command core for this session. For
|
|
1069
|
+
* the client this is a freshly connected client session; for the server it is
|
|
1070
|
+
* a session-scoped core created by the server driver.
|
|
1071
|
+
* @remarks Creates the {@link WebTransportSession.datagrams} duplex, then
|
|
1072
|
+
* constructs the two incoming-stream `ReadableStream`s and captures their
|
|
1073
|
+
* controllers via the `start` callback. Registers an incoming handler on
|
|
1074
|
+
* `core` so each peer-opened bidi/uni stream is wrapped and enqueued;
|
|
1075
|
+
* `enqueue` is wrapped in try/catch because it throws once the consumer has
|
|
1076
|
+
* cancelled the reader, and that error must not escape native event dispatch.
|
|
1077
|
+
* Finally it observes `core.closed`: on clean close both controllers are
|
|
1078
|
+
* closed via {@link safeClose}, and on rejection both are errored with the
|
|
1079
|
+
* failure reason via {@link safeError}.
|
|
1080
|
+
*/
|
|
1081
|
+
constructor(core) {
|
|
1082
|
+
this.core = core;
|
|
1083
|
+
this.datagrams = new WebTransportDatagramDuplexStream(core);
|
|
1084
|
+
let bidiController;
|
|
1085
|
+
let uniController;
|
|
1086
|
+
this.incomingBidirectionalStreams = new ReadableStream({
|
|
1087
|
+
start: (c) => {
|
|
1088
|
+
bidiController = c;
|
|
1089
|
+
}
|
|
1090
|
+
});
|
|
1091
|
+
this.incomingUnidirectionalStreams = new ReadableStream({
|
|
1092
|
+
start: (c) => {
|
|
1093
|
+
uniController = c;
|
|
1094
|
+
}
|
|
1095
|
+
});
|
|
1096
|
+
core.setIncomingHandler({
|
|
1097
|
+
onBidi: (id) => {
|
|
1098
|
+
try {
|
|
1099
|
+
bidiController.enqueue(new WebTransportBidirectionalStream(core, id));
|
|
1100
|
+
} catch {
|
|
1101
|
+
}
|
|
1102
|
+
},
|
|
1103
|
+
onUni: (id) => {
|
|
1104
|
+
try {
|
|
1105
|
+
uniController.enqueue(new WebTransportReceiveStream(core, id));
|
|
1106
|
+
} catch {
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
});
|
|
1110
|
+
core.closed.promise.then(
|
|
1111
|
+
() => {
|
|
1112
|
+
safeClose(bidiController);
|
|
1113
|
+
safeClose(uniController);
|
|
1114
|
+
},
|
|
1115
|
+
(err) => {
|
|
1116
|
+
safeError(bidiController, err);
|
|
1117
|
+
safeError(uniController, err);
|
|
1118
|
+
}
|
|
1119
|
+
);
|
|
1120
|
+
}
|
|
1121
|
+
/**
|
|
1122
|
+
* A promise that resolves once the session handshake completes and the
|
|
1123
|
+
* session is established, and rejects if the session fails or closes before
|
|
1124
|
+
* it becomes ready.
|
|
1125
|
+
* @returns The core's `ready` promise.
|
|
1126
|
+
*/
|
|
1127
|
+
get ready() {
|
|
1128
|
+
return this.core.ready.promise;
|
|
1129
|
+
}
|
|
1130
|
+
/**
|
|
1131
|
+
* A promise that resolves with the {@link WebTransportCloseInfo} (close code
|
|
1132
|
+
* and reason) when the session ends cleanly, and rejects with a
|
|
1133
|
+
* {@link WebTransportError} on abnormal termination or if the session never
|
|
1134
|
+
* became ready.
|
|
1135
|
+
* @returns The core's `closed` promise.
|
|
1136
|
+
*/
|
|
1137
|
+
get closed() {
|
|
1138
|
+
return this.core.closed.promise;
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* Open a new outbound bidirectional stream.
|
|
1142
|
+
*
|
|
1143
|
+
* @returns A promise resolving to a {@link WebTransportBidirectionalStream}
|
|
1144
|
+
* (paired readable and writable halves) once the native side reports the new
|
|
1145
|
+
* stream id.
|
|
1146
|
+
* @throws WebTransportError (the returned promise rejects) if the session is
|
|
1147
|
+
* already closed or was never attached.
|
|
1148
|
+
*/
|
|
1149
|
+
async createBidirectionalStream() {
|
|
1150
|
+
const id = await this.core.openStream(true);
|
|
1151
|
+
return new WebTransportBidirectionalStream(this.core, id);
|
|
1152
|
+
}
|
|
1153
|
+
/**
|
|
1154
|
+
* Open a new outbound unidirectional (send-only) stream.
|
|
1155
|
+
*
|
|
1156
|
+
* @returns A promise resolving to a {@link WebTransportSendStream} once the
|
|
1157
|
+
* native side reports the new stream id.
|
|
1158
|
+
* @throws WebTransportError (the returned promise rejects) if the session is
|
|
1159
|
+
* already closed or was never attached.
|
|
1160
|
+
*/
|
|
1161
|
+
async createUnidirectionalStream() {
|
|
1162
|
+
const id = await this.core.openStream(false);
|
|
1163
|
+
return new WebTransportSendStream(this.core, id);
|
|
1164
|
+
}
|
|
1165
|
+
/**
|
|
1166
|
+
* Close the session gracefully, notifying the peer with an application code
|
|
1167
|
+
* and reason.
|
|
1168
|
+
*
|
|
1169
|
+
* @param closeInfo - Close options. `closeInfo.closeCode` defaults to `0` and
|
|
1170
|
+
* `closeInfo.reason` defaults to `''`; both are forwarded to the native
|
|
1171
|
+
* close.
|
|
1172
|
+
* @defaultValue `closeInfo` defaults to `{}` (close code `0`, empty reason).
|
|
1173
|
+
* @remarks Idempotent: guarded by {@link WebTransportSession.closeCalled} so
|
|
1174
|
+
* only the first call takes effect and later calls are no-ops. Delegates to
|
|
1175
|
+
* the core, which issues the native `closeSession` (a graceful
|
|
1176
|
+
* CLOSE_WEBTRANSPORT_SESSION capsule plus FIN, then QUIC close) only while the
|
|
1177
|
+
* session is still attached and not already closed.
|
|
1178
|
+
*/
|
|
1179
|
+
close(closeInfo = {}) {
|
|
1180
|
+
if (this.closeCalled) return;
|
|
1181
|
+
this.closeCalled = true;
|
|
1182
|
+
this.core.close(closeInfo.closeCode ?? 0, closeInfo.reason ?? "");
|
|
1183
|
+
}
|
|
1184
|
+
};
|
|
1185
|
+
var WebTransport = class extends WebTransportSession {
|
|
1186
|
+
/**
|
|
1187
|
+
* Create and begin connecting a WebTransport client session.
|
|
1188
|
+
*
|
|
1189
|
+
* @param url - The session URL; must be an `https://` URL.
|
|
1190
|
+
* @param options - Session options: certificate pinning, extra headers,
|
|
1191
|
+
* origin, and the Node-specific `insecure` flag.
|
|
1192
|
+
* @defaultValue `options` defaults to `{}`.
|
|
1193
|
+
* @throws WebTransportError synchronously if {@link buildConfig} rejects the
|
|
1194
|
+
* URL or a certificate hash. Asynchronous setup failures (DNS, bind,
|
|
1195
|
+
* handshake) instead reject {@link WebTransportSession.ready | ready} and
|
|
1196
|
+
* {@link WebTransportSession.closed | closed}.
|
|
1197
|
+
* @remarks Validates and normalizes inputs via {@link buildConfig}, then
|
|
1198
|
+
* calls {@link createClientSession} to spawn the native driver thread and
|
|
1199
|
+
* passes the resulting {@link SessionCore} to the base constructor.
|
|
1200
|
+
*/
|
|
1201
|
+
constructor(url, options = {}) {
|
|
1202
|
+
super(createClientSession(buildConfig(url, options)));
|
|
1203
|
+
}
|
|
1204
|
+
};
|
|
1205
|
+
function safeClose(controller) {
|
|
1206
|
+
try {
|
|
1207
|
+
controller.close();
|
|
1208
|
+
} catch {
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
function safeError(controller, reason) {
|
|
1212
|
+
try {
|
|
1213
|
+
controller.error(reason);
|
|
1214
|
+
} catch {
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
// src/server.ts
|
|
1219
|
+
function deferred2() {
|
|
1220
|
+
let resolve;
|
|
1221
|
+
let reject;
|
|
1222
|
+
const promise = new Promise((res, rej) => {
|
|
1223
|
+
resolve = res;
|
|
1224
|
+
reject = rej;
|
|
1225
|
+
});
|
|
1226
|
+
return { promise, resolve, reject };
|
|
1227
|
+
}
|
|
1228
|
+
var WebTransportServerSession = class extends WebTransportSession {
|
|
1229
|
+
/** The `:authority` of the client's CONNECT request. */
|
|
1230
|
+
authority;
|
|
1231
|
+
/** The `:path` of the client's CONNECT request. */
|
|
1232
|
+
path;
|
|
1233
|
+
/** The `origin` header, if the client sent one, otherwise `null`. */
|
|
1234
|
+
origin;
|
|
1235
|
+
/** Any additional (non-pseudo) request headers, keyed by header name. */
|
|
1236
|
+
headers;
|
|
1237
|
+
/**
|
|
1238
|
+
* The client's remote IP address, as reported by the transport when the
|
|
1239
|
+
* session was established. The same for every stream and datagram on this
|
|
1240
|
+
* session, since they all share the one QUIC connection.
|
|
1241
|
+
*/
|
|
1242
|
+
remoteAddress;
|
|
1243
|
+
/** The client's remote UDP port at session establishment. */
|
|
1244
|
+
remotePort;
|
|
1245
|
+
/**
|
|
1246
|
+
* Wrap an established session core together with the CONNECT metadata that
|
|
1247
|
+
* opened it. Called by {@link WebTransportServer} when a new session becomes
|
|
1248
|
+
* ready; not intended for direct construction by users.
|
|
1249
|
+
*
|
|
1250
|
+
* @param core - The dispatcher/command core backing this session, already
|
|
1251
|
+
* attached to a {@link ServerTransport}.
|
|
1252
|
+
* @param request - The CONNECT metadata (authority, path, origin, headers)
|
|
1253
|
+
* plus the client's remote address, copied onto this session's public fields.
|
|
1254
|
+
*/
|
|
1255
|
+
constructor(core, request) {
|
|
1256
|
+
super(core);
|
|
1257
|
+
this.authority = request.authority;
|
|
1258
|
+
this.path = request.path;
|
|
1259
|
+
this.origin = request.origin;
|
|
1260
|
+
this.headers = request.headers;
|
|
1261
|
+
this.remoteAddress = request.remoteAddress;
|
|
1262
|
+
this.remotePort = request.remotePort;
|
|
1263
|
+
}
|
|
1264
|
+
};
|
|
1265
|
+
var WebTransportServer = class {
|
|
1266
|
+
/** The loaded native addon, source of the `server*` command functions. */
|
|
1267
|
+
native;
|
|
1268
|
+
/** Opaque native server handle returned by `serverListen`. */
|
|
1269
|
+
handle;
|
|
1270
|
+
/** Live sessions keyed by their native session id, for event routing. */
|
|
1271
|
+
sessions = /* @__PURE__ */ new Map();
|
|
1272
|
+
/** Settled when the native side reports `listening`; backs {@link ready}. */
|
|
1273
|
+
readyD = deferred2();
|
|
1274
|
+
/**
|
|
1275
|
+
* Settled when the server stops (`serverClosed`) or fatally fails
|
|
1276
|
+
* (`serverError`); backs {@link closed}.
|
|
1277
|
+
*/
|
|
1278
|
+
closedD = deferred2();
|
|
1279
|
+
/** Controller for {@link incomingSessions}; captured in its `start`. */
|
|
1280
|
+
incomingController;
|
|
1281
|
+
/** The UDP port actually bound, learned from the `listening` event. */
|
|
1282
|
+
boundPort = 0;
|
|
1283
|
+
/** Guards {@link close} so shutdown is issued at most once. */
|
|
1284
|
+
closeCalled = false;
|
|
1285
|
+
/**
|
|
1286
|
+
* The sessions clients open, delivered as they are established. Each pull
|
|
1287
|
+
* yields a {@link WebTransportServerSession}. The stream is closed when the
|
|
1288
|
+
* server stops or errors.
|
|
1289
|
+
*/
|
|
1290
|
+
incomingSessions;
|
|
1291
|
+
/**
|
|
1292
|
+
* Load the native addon and start listening immediately.
|
|
1293
|
+
*
|
|
1294
|
+
* Sets up {@link incomingSessions} (capturing its controller), attaches
|
|
1295
|
+
* no-op catch handlers to the internal ready/closed promises so they never
|
|
1296
|
+
* surface as unhandled rejections, then calls the native `serverListen` with
|
|
1297
|
+
* the given certificate, key, host, port and reuse-port flag, wiring every
|
|
1298
|
+
* native event to {@link onEvent}.
|
|
1299
|
+
*
|
|
1300
|
+
* @param options - Bind address, port, certificate/key paths and reuse-port
|
|
1301
|
+
* flag. `host` defaults to `'0.0.0.0'` and `reusePort` defaults to `false`.
|
|
1302
|
+
* @see {@link ready} to await binding and {@link WebTransportServerOptions}.
|
|
1303
|
+
*/
|
|
1304
|
+
constructor(options) {
|
|
1305
|
+
this.native = loadNative();
|
|
1306
|
+
void this.readyD.promise.catch(() => {
|
|
1307
|
+
});
|
|
1308
|
+
void this.closedD.promise.catch(() => {
|
|
1309
|
+
});
|
|
1310
|
+
this.incomingSessions = new ReadableStream({
|
|
1311
|
+
start: (c) => {
|
|
1312
|
+
this.incomingController = c;
|
|
1313
|
+
}
|
|
1314
|
+
});
|
|
1315
|
+
this.handle = this.native.serverListen(
|
|
1316
|
+
options.cert,
|
|
1317
|
+
options.key,
|
|
1318
|
+
options.host ?? "0.0.0.0",
|
|
1319
|
+
options.port,
|
|
1320
|
+
options.reusePort ?? false,
|
|
1321
|
+
(ev) => this.onEvent(ev)
|
|
1322
|
+
);
|
|
1323
|
+
}
|
|
1324
|
+
/**
|
|
1325
|
+
* Resolves once the server is listening (the native `listening` event has
|
|
1326
|
+
* arrived); rejects if the server fails before it binds.
|
|
1327
|
+
*/
|
|
1328
|
+
get ready() {
|
|
1329
|
+
return this.readyD.promise;
|
|
1330
|
+
}
|
|
1331
|
+
/**
|
|
1332
|
+
* Resolves when the server has stopped cleanly (`serverClosed`), rejects on
|
|
1333
|
+
* a fatal server error (`serverError`).
|
|
1334
|
+
*/
|
|
1335
|
+
get closed() {
|
|
1336
|
+
return this.closedD.promise;
|
|
1337
|
+
}
|
|
1338
|
+
/** The UDP port the server is bound to (valid after {@link ready}). */
|
|
1339
|
+
get port() {
|
|
1340
|
+
return this.boundPort;
|
|
1341
|
+
}
|
|
1342
|
+
/**
|
|
1343
|
+
* Stop the server and all its sessions. Idempotent: repeated calls after the
|
|
1344
|
+
* first are ignored. Triggers the native `serverShutdown`, which eventually
|
|
1345
|
+
* drives a `serverClosed` event that settles {@link closed} and finishes any
|
|
1346
|
+
* remaining sessions.
|
|
1347
|
+
*/
|
|
1348
|
+
close() {
|
|
1349
|
+
if (this.closeCalled) return;
|
|
1350
|
+
this.closeCalled = true;
|
|
1351
|
+
this.native.serverShutdown(this.handle);
|
|
1352
|
+
}
|
|
1353
|
+
/**
|
|
1354
|
+
* Handle one native server event. Server-level events (`listening`,
|
|
1355
|
+
* `serverError`, `serverClosed`) settle the server's ready/closed promises
|
|
1356
|
+
* and, on stop or error, finish all sessions and close
|
|
1357
|
+
* {@link incomingSessions}. Session-scoped events carry a `session` id: a
|
|
1358
|
+
* `serverReady` creates and registers a new {@link SessionCore} (attached to
|
|
1359
|
+
* a {@link ServerTransport}), wraps it in a {@link WebTransportServerSession}
|
|
1360
|
+
* and enqueues it; every session-scoped event is then dispatched to the
|
|
1361
|
+
* matching core.
|
|
1362
|
+
*
|
|
1363
|
+
* @param ev - The event delivered by the native `serverListen` callback.
|
|
1364
|
+
* @remarks Enqueue into {@link incomingSessions} is guarded: if the consumer
|
|
1365
|
+
* has stopped reading, the thrown error is swallowed so it cannot escape
|
|
1366
|
+
* the native event dispatch. On `serverError`, the `WebTransportError` is
|
|
1367
|
+
* created with `source: 'session'`.
|
|
1368
|
+
*/
|
|
1369
|
+
onEvent(ev) {
|
|
1370
|
+
switch (ev.type) {
|
|
1371
|
+
case "listening" /* Listening */: {
|
|
1372
|
+
this.boundPort = ev.port;
|
|
1373
|
+
this.readyD.resolve();
|
|
1374
|
+
return;
|
|
1375
|
+
}
|
|
1376
|
+
case "serverError" /* ServerError */: {
|
|
1377
|
+
const err = new WebTransportError(ev.message, { source: "session" });
|
|
1378
|
+
this.readyD.reject(err);
|
|
1379
|
+
this.closedD.reject(err);
|
|
1380
|
+
this.finishAllSessions();
|
|
1381
|
+
safeCloseController(this.incomingController);
|
|
1382
|
+
return;
|
|
1383
|
+
}
|
|
1384
|
+
case "serverClosed" /* ServerClosed */: {
|
|
1385
|
+
this.closedD.resolve();
|
|
1386
|
+
this.finishAllSessions();
|
|
1387
|
+
safeCloseController(this.incomingController);
|
|
1388
|
+
return;
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
const sessionId = ev.session;
|
|
1392
|
+
if (ev.type === "serverReady" /* ServerReady */) {
|
|
1393
|
+
const core = new SessionCore();
|
|
1394
|
+
core.attach(new ServerTransport(this.native, this.handle, sessionId));
|
|
1395
|
+
this.sessions.set(sessionId, core);
|
|
1396
|
+
void core.closed.promise.finally(() => this.sessions.delete(sessionId));
|
|
1397
|
+
const session = new WebTransportServerSession(core, {
|
|
1398
|
+
authority: ev.authority,
|
|
1399
|
+
path: ev.path,
|
|
1400
|
+
origin: ev.origin,
|
|
1401
|
+
headers: ev.headers,
|
|
1402
|
+
remoteAddress: ev.remoteAddress,
|
|
1403
|
+
remotePort: ev.remotePort
|
|
1404
|
+
});
|
|
1405
|
+
try {
|
|
1406
|
+
this.incomingController.enqueue(session);
|
|
1407
|
+
} catch {
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
this.sessions.get(sessionId)?.dispatch(ev);
|
|
1411
|
+
}
|
|
1412
|
+
/**
|
|
1413
|
+
* Settle every live session when the server stops, so their `ready`/`closed`
|
|
1414
|
+
* promises never hang and the session map does not leak.
|
|
1415
|
+
*
|
|
1416
|
+
* @remarks Dispatches a synthetic clean `closed` event (code `0`, empty
|
|
1417
|
+
* reason, `remote: false`) to each session core, then clears the map. The
|
|
1418
|
+
* per-core `closed.finally` handler registered in {@link onEvent} would
|
|
1419
|
+
* also remove entries, but the map is cleared here unconditionally.
|
|
1420
|
+
*/
|
|
1421
|
+
finishAllSessions() {
|
|
1422
|
+
for (const core of this.sessions.values()) {
|
|
1423
|
+
core.dispatch({
|
|
1424
|
+
type: "closed" /* Closed */,
|
|
1425
|
+
code: 0,
|
|
1426
|
+
reason: new Uint8Array(),
|
|
1427
|
+
remote: false
|
|
1428
|
+
});
|
|
1429
|
+
}
|
|
1430
|
+
this.sessions.clear();
|
|
1431
|
+
}
|
|
1432
|
+
};
|
|
1433
|
+
function safeCloseController(controller) {
|
|
1434
|
+
try {
|
|
1435
|
+
controller.close();
|
|
1436
|
+
} catch {
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
export {
|
|
1440
|
+
WebTransport,
|
|
1441
|
+
WebTransportBidirectionalStream,
|
|
1442
|
+
WebTransportDatagramDuplexStream,
|
|
1443
|
+
WebTransportError,
|
|
1444
|
+
WebTransportReceiveStream,
|
|
1445
|
+
WebTransportSendStream,
|
|
1446
|
+
WebTransportServer,
|
|
1447
|
+
WebTransportServerSession,
|
|
1448
|
+
WebTransportSession
|
|
1449
|
+
};
|
|
1450
|
+
//! Locates and loads the rwebtransport native addon.
|
|
1451
|
+
//!
|
|
1452
|
+
//! Resolution order:
|
|
1453
|
+
//! 1. A prebuilt binary at `prebuilds/<platform>-<arch>/rwebtransport.node`.
|
|
1454
|
+
//! 2. If none is found, compile the Rust crate on the fly via `scripts/build.js`
|
|
1455
|
+
//! (requires a Rust toolchain + cmake + C/C++ compiler) and load the result.
|
|
1456
|
+
//! Only Node 24 and Node 26 are supported.
|
|
1457
|
+
//! The `WebTransportError` type, matching the W3C interface.
|
|
1458
|
+
//! Typed view of the native addon and the event dispatcher that turns its
|
|
1459
|
+
//! low-level, callback-based ABI into promises and per-stream sinks.
|
|
1460
|
+
//! `WebTransportDatagramDuplexStream`: unreliable, unordered datagrams.
|
|
1461
|
+
//! WebTransport stream classes, implemented as WHATWG Readable/Writable streams
|
|
1462
|
+
//! with end-to-end backpressure onto the native QUIC flow-control window.
|
|
1463
|
+
//! The `WebTransport` class: the W3C entry point.
|
|
1464
|
+
//! The `WebTransportServer` accepts WebTransport sessions from clients. Each
|
|
1465
|
+
//! established session is a {@link WebTransportServerSession}, which shares the
|
|
1466
|
+
//! full stream/datagram surface with the client `WebTransport`.
|
|
1467
|
+
//! rwebtransport: a fully-compatible WebTransport client and server for Node.js.
|
|
1468
|
+
//! ```ts
|
|
1469
|
+
//! import { WebTransport } from 'rwebtransport';
|
|
1470
|
+
//! const wt = new WebTransport('https://example.com:4433/echo');
|
|
1471
|
+
//! await wt.ready;
|
|
1472
|
+
//! ```
|
|
1473
|
+
//# sourceMappingURL=index.mjs.map
|