@browsercore/quic 0.1.3 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +122 -0
- package/dist/connection.d.ts +33 -13
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +160 -77
- package/dist/connection.js.map +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/frame/frame.d.ts +1 -2
- package/dist/frame/frame.d.ts.map +1 -1
- package/dist/frame/frame.js +26 -8
- package/dist/frame/frame.js.map +1 -1
- package/dist/frame/varint.d.ts +10 -0
- package/dist/frame/varint.d.ts.map +1 -1
- package/dist/frame/varint.js +41 -14
- package/dist/frame/varint.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/packet/packet.d.ts +1 -2
- package/dist/packet/packet.d.ts.map +1 -1
- package/dist/packet/packet.js +19 -4
- package/dist/packet/packet.js.map +1 -1
- package/dist/stream/stream.d.ts +7 -1
- package/dist/stream/stream.d.ts.map +1 -1
- package/dist/stream/stream.js +127 -84
- package/dist/stream/stream.js.map +1 -1
- package/dist/transport-params.d.ts +38 -0
- package/dist/transport-params.d.ts.map +1 -0
- package/dist/transport-params.js +124 -0
- package/dist/transport-params.js.map +1 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +2 -1
- package/dist/utils.js.map +1 -1
- package/package.json +51 -40
- package/.github/workflows/ci.yml +0 -17
- package/.github/workflows/publish.yml +0 -49
- package/CODING_STANDARDS.md +0 -205
- package/src/connection.ts +0 -374
- package/src/errors.ts +0 -153
- package/src/frame/frame.ts +0 -423
- package/src/frame/varint.ts +0 -124
- package/src/index.ts +0 -70
- package/src/packet/packet.ts +0 -210
- package/src/stream/stream.ts +0 -746
- package/src/types.ts +0 -441
- package/src/utils.ts +0 -38
- package/tests/connection.test.ts +0 -106
- package/tests/fake-transport.ts +0 -107
- package/tests/stream.test.ts +0 -152
- package/tests/wire.test.ts +0 -342
- package/tsconfig.json +0 -30
package/src/connection.ts
DELETED
|
@@ -1,374 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* QUIC connection lifecycle + datagram read loop (RFC 9000 §5, §12).
|
|
3
|
-
*
|
|
4
|
-
* Wires packet header parsing/serialization, the frame layer, and the stream
|
|
5
|
-
* manager over an injected {@link DatagramTransport} (UDP). The connection owns
|
|
6
|
-
* the read loop: it turns inbound datagrams into frames, dispatches each to the
|
|
7
|
-
* stream manager, and drains pending stream sends back into outbound packets.
|
|
8
|
-
*
|
|
9
|
-
* Honest limitations:
|
|
10
|
-
* - The TLS 1.3 handshake and packet protection (header protection + AEAD
|
|
11
|
-
* payload encryption) are out of scope. `connectQuic()` returns a connection
|
|
12
|
-
* that moves *unprotected* frames over the transport directly — enough to
|
|
13
|
-
* drive the data plane and to be tested with a fake datagram transport. A
|
|
14
|
-
* production build layers protection + the handshake on top of this.
|
|
15
|
-
* - No congestion controller, no connection migration, no PATH_CHALLENGE /
|
|
16
|
-
* PATH_RESPONSE beyond frame relay, and no liveness PING.
|
|
17
|
-
*
|
|
18
|
-
* Concurrency model: the read loop is a single async task pulling datagrams from
|
|
19
|
-
* the transport. Outbound frames are produced synchronously by the stream
|
|
20
|
-
* manager and packed into packets; writes resolve once bytes are buffered.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import type { EventEmitter } from "node:events";
|
|
24
|
-
import type {
|
|
25
|
-
ConnectionId,
|
|
26
|
-
QuicConnection,
|
|
27
|
-
QuicFrame,
|
|
28
|
-
QuicOptions,
|
|
29
|
-
QuicStream,
|
|
30
|
-
QuicTransportParameters,
|
|
31
|
-
UdpAddress,
|
|
32
|
-
} from "./types.js";
|
|
33
|
-
import { QuicFrameType } from "./types.js";
|
|
34
|
-
import { parsePacketHeader, serializeShortHeader } from "./packet/packet.js";
|
|
35
|
-
import type { PacketHeader } from "./packet/packet.js";
|
|
36
|
-
import { readFrames, serializeFrame } from "./frame/frame.js";
|
|
37
|
-
import { ConnectionClosedError } from "./errors.js";
|
|
38
|
-
import { createStreamManager, type StreamManager } from "./stream/stream.js";
|
|
39
|
-
import { concatAll } from "./utils.js";
|
|
40
|
-
|
|
41
|
-
/** Byte type alias matching the `Uint8Array<ArrayBufferLike>` wire signatures. */
|
|
42
|
-
type Bytes = Uint8Array<ArrayBufferLike>;
|
|
43
|
-
|
|
44
|
-
/** Largest UDP payload we will pack into a single datagram (RFC 9000 §14). */
|
|
45
|
-
const MAX_DATAGRAM_PAYLOAD = 1200;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Concrete QUIC connection. The public surface matches the fixed
|
|
49
|
-
* `QuicConnection` interface; internal state is kept on the instance.
|
|
50
|
-
*/
|
|
51
|
-
export class QuicConnectionImpl implements QuicConnection {
|
|
52
|
-
public readonly id: string;
|
|
53
|
-
|
|
54
|
-
/** The underlying datagram (UDP) transport. */
|
|
55
|
-
private readonly transport: QuicOptions["transport"];
|
|
56
|
-
/** The peer's UDP address. */
|
|
57
|
-
private readonly peer: UdpAddress;
|
|
58
|
-
/** Stream manager (also an EventEmitter for connection-level signals). */
|
|
59
|
-
private readonly manager: StreamManager & EventEmitter;
|
|
60
|
-
/** Our current destination connection id (the one we put on outbound packets). */
|
|
61
|
-
private readonly dcid: ConnectionId;
|
|
62
|
-
|
|
63
|
-
/** Set once the connection begins graceful shutdown. */
|
|
64
|
-
private closing = false;
|
|
65
|
-
/** Set once the connection is fully torn down. */
|
|
66
|
-
private closed = false;
|
|
67
|
-
/** Buffered outbound frames waiting to be packed into the next packet. */
|
|
68
|
-
private readonly outboundFrames: QuicFrame[] = [];
|
|
69
|
-
|
|
70
|
-
public constructor(
|
|
71
|
-
id: string,
|
|
72
|
-
options: QuicOptions,
|
|
73
|
-
manager: StreamManager & EventEmitter,
|
|
74
|
-
dcid: ConnectionId,
|
|
75
|
-
) {
|
|
76
|
-
this.id = id;
|
|
77
|
-
this.transport = options.transport;
|
|
78
|
-
this.peer = options.peer;
|
|
79
|
-
this.manager = manager;
|
|
80
|
-
this.dcid = dcid;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// --- public QuicConnection surface ------------------------------------------
|
|
84
|
-
|
|
85
|
-
public async openBidirectionalStream(): Promise<QuicStream> {
|
|
86
|
-
this._ensureOpen();
|
|
87
|
-
return this.manager.openStream(true);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
public async acceptBidirectionalStream(): Promise<QuicStream> {
|
|
91
|
-
this._ensureOpen();
|
|
92
|
-
return this.manager.acceptStream(true);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
public async openUnidirectionalStream(): Promise<QuicStream> {
|
|
96
|
-
this._ensureOpen();
|
|
97
|
-
return this.manager.openStream(false);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
public async acceptUnidirectionalStream(): Promise<QuicStream> {
|
|
101
|
-
this._ensureOpen();
|
|
102
|
-
return this.manager.acceptStream(false);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
public async close(errorCode: bigint, reason: string): Promise<void> {
|
|
106
|
-
if (this.closed) return;
|
|
107
|
-
this.closing = true;
|
|
108
|
-
this.manager.close(errorCode, reason);
|
|
109
|
-
// Pack + flush the CONNECTION_CLOSE frame, then tear down.
|
|
110
|
-
await this._flush();
|
|
111
|
-
await this._teardown({ kind: "client_close" });
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// --- frame I/O -------------------------------------------------------------
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Enqueue a frame for the next outbound packet. Control frames from the
|
|
118
|
-
* stream manager flow through here so they are serialized + sent by the
|
|
119
|
-
* connection's packetizer.
|
|
120
|
-
*/
|
|
121
|
-
public sendFrame(frame: QuicFrame): void {
|
|
122
|
-
this.outboundFrames.push(frame);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Pack all buffered outbound frames into a short-header packet and send it.
|
|
127
|
-
* Splits into multiple datagrams if the payload exceeds MAX_DATAGRAM_PAYLOAD.
|
|
128
|
-
*/
|
|
129
|
-
private async _flush(): Promise<void> {
|
|
130
|
-
if (this.outboundFrames.length === 0) return;
|
|
131
|
-
const frames = this.outboundFrames.splice(0, this.outboundFrames.length);
|
|
132
|
-
const payload = this._packFrames(frames);
|
|
133
|
-
let offset = 0;
|
|
134
|
-
while (offset < payload.length) {
|
|
135
|
-
const slice = payload.subarray(offset, offset + MAX_DATAGRAM_PAYLOAD) as Bytes;
|
|
136
|
-
const packet = this._wrapPacket(slice);
|
|
137
|
-
await this.transport.send(packet, this.peer);
|
|
138
|
-
offset += MAX_DATAGRAM_PAYLOAD;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/** Serialize frames into a single byte buffer (no packet header). */
|
|
143
|
-
private _packFrames(frames: QuicFrame[]): Bytes {
|
|
144
|
-
return concatAll(frames.map((f) => serializeFrame(f)));
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/** Wrap a serialized payload in a short (1-RTT) header + packet number. */
|
|
148
|
-
private _wrapPacket(payload: Bytes): Bytes {
|
|
149
|
-
const header = serializeShortHeader(this.dcid, 1, false, false);
|
|
150
|
-
// 1-byte packet number (relative offset 0) — placeholder for the
|
|
151
|
-
// protection layer, which assigns real packet numbers.
|
|
152
|
-
const packetNumber = new Uint8Array([0]);
|
|
153
|
-
return concatAll([header, packetNumber, payload]);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// --- read loop -------------------------------------------------------------
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Start the datagram read loop. Must be called once after construction.
|
|
160
|
-
* Runs until the transport closes or the connection tears down.
|
|
161
|
-
*/
|
|
162
|
-
public startReadLoop(): void {
|
|
163
|
-
// React to connection-level signals from the stream manager.
|
|
164
|
-
this.manager.on("connectionClose", ({ errorCode, reason }: { errorCode: bigint; reason: string }) => {
|
|
165
|
-
void errorCode;
|
|
166
|
-
void reason;
|
|
167
|
-
void this._onPeerClose(errorCode, reason);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
void this._readLoop();
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
private async _onPeerClose(errorCode: bigint, reason: string): Promise<void> {
|
|
174
|
-
if (this.closed) return;
|
|
175
|
-
this.closing = true;
|
|
176
|
-
this.manager.abortAll(new ConnectionClosedError(errorCode, reason));
|
|
177
|
-
await this._teardown({ kind: "remote_close" });
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/** Parse a single datagram into frames and dispatch them. */
|
|
181
|
-
private async _dispatchDatagram(data: Bytes): Promise<void> {
|
|
182
|
-
if (data.length === 0) return;
|
|
183
|
-
try {
|
|
184
|
-
const header = parsePacketHeader(data);
|
|
185
|
-
this._applyHeader(header);
|
|
186
|
-
// Skip the header + packet number to reach the frame payload. The
|
|
187
|
-
// protection layer would decrypt first in a full implementation.
|
|
188
|
-
const pnLength = header.packetNumberLength;
|
|
189
|
-
// Short headers carry a variable-length DCID after the first byte;
|
|
190
|
-
// its length is not on the wire, so the connection supplies it from
|
|
191
|
-
// the handshake state we already know (this.dcid).
|
|
192
|
-
const dcidLength = header.form === 0 ? this.dcid.length : 0;
|
|
193
|
-
const payloadStart = header.headerLength + dcidLength + pnLength;
|
|
194
|
-
const payload = data.subarray(payloadStart);
|
|
195
|
-
if (payload.length === 0) return;
|
|
196
|
-
|
|
197
|
-
const read = this._byteStreamReader(payload);
|
|
198
|
-
for await (const frame of readFrames(read)) {
|
|
199
|
-
this._handleFrame(frame);
|
|
200
|
-
}
|
|
201
|
-
} catch (err) {
|
|
202
|
-
// A parse error on one datagram is not necessarily fatal for the
|
|
203
|
-
// connection, but without a protection layer to frame it we close.
|
|
204
|
-
this._handleFatal(err instanceof Error ? err : new Error(String(err)));
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/** Pull the DCID / version from a parsed header into connection state. */
|
|
209
|
-
private _applyHeader(header: PacketHeader): void {
|
|
210
|
-
if (header.form === 1) {
|
|
211
|
-
// Long header: could carry a version negotiation. We ignore for now.
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
// Short header: the DCID on the wire is the route to the peer.
|
|
215
|
-
void header.dcid;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Build a pull-based byte reader over a fixed buffer. `readFrames` consumes
|
|
220
|
-
* bytes incrementally; this yields slices until the buffer is exhausted.
|
|
221
|
-
*/
|
|
222
|
-
private _byteStreamReader(buf: Bytes): () => Promise<Uint8Array | null> {
|
|
223
|
-
let offset = 0;
|
|
224
|
-
return async (): Promise<Uint8Array | null> => {
|
|
225
|
-
if (offset >= buf.length) return null;
|
|
226
|
-
const chunk = buf.subarray(offset);
|
|
227
|
-
offset = buf.length;
|
|
228
|
-
return chunk;
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/** Route a decoded frame: data-plane frames to the manager, the rest here. */
|
|
233
|
-
private _handleFrame(frame: QuicFrame): void {
|
|
234
|
-
switch (frame.type) {
|
|
235
|
-
case QuicFrameType.PADDING:
|
|
236
|
-
case QuicFrameType.PING:
|
|
237
|
-
return;
|
|
238
|
-
case QuicFrameType.ACK:
|
|
239
|
-
case QuicFrameType.ACK_ECN:
|
|
240
|
-
case QuicFrameType.CRYPTO:
|
|
241
|
-
case QuicFrameType.NEW_TOKEN:
|
|
242
|
-
case QuicFrameType.NEW_CONNECTION_ID:
|
|
243
|
-
case QuicFrameType.RETIRE_CONNECTION_ID:
|
|
244
|
-
case QuicFrameType.PATH_CHALLENGE:
|
|
245
|
-
case QuicFrameType.PATH_RESPONSE:
|
|
246
|
-
case QuicFrameType.HANDSHAKE_DONE:
|
|
247
|
-
// Connection / handshake layer concerns — relay only.
|
|
248
|
-
return;
|
|
249
|
-
default:
|
|
250
|
-
// Data-plane frame: dispatch to the stream manager, which may
|
|
251
|
-
// emit outbound control frames (MAX_DATA, MAX_STREAM_DATA, ...).
|
|
252
|
-
this._withOutbound(() => this.manager.dispatch(frame));
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/** Run `fn`, then flush any outbound frames it produced to the peer. */
|
|
258
|
-
private _withOutbound(fn: () => void): void {
|
|
259
|
-
const before = this.outboundFrames.length;
|
|
260
|
-
fn();
|
|
261
|
-
if (this.outboundFrames.length > before) {
|
|
262
|
-
void this._flush();
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/** Main read loop: pull datagrams, dispatch, drain sends. */
|
|
267
|
-
private async _readLoop(): Promise<void> {
|
|
268
|
-
try {
|
|
269
|
-
while (!this.closed) {
|
|
270
|
-
const { data } = await this.transport.recv();
|
|
271
|
-
await this._dispatchDatagram(data as Bytes);
|
|
272
|
-
// Drain any pending stream sends after handling each datagram.
|
|
273
|
-
this._drainSends();
|
|
274
|
-
}
|
|
275
|
-
} catch (err) {
|
|
276
|
-
if (!this.closed) {
|
|
277
|
-
this._handleFatal(err instanceof Error ? err : new Error(String(err)));
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/** Emit STREAM frames for streams with queued data, then flush. */
|
|
283
|
-
private _drainSends(): void {
|
|
284
|
-
if (!this.manager.hasPendingSends) return;
|
|
285
|
-
this.manager.flushSends(MAX_DATAGRAM_PAYLOAD, (frame) => this.sendFrame(frame));
|
|
286
|
-
void this._flush();
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/** Tear down the connection on a fatal transport / parse error. */
|
|
290
|
-
private _handleFatal(err: Error): void {
|
|
291
|
-
if (this.closed) return;
|
|
292
|
-
this.closing = true;
|
|
293
|
-
this.manager.abortAll(err);
|
|
294
|
-
this._teardown({ kind: "error", error: err });
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/** Mark closed and release the transport. */
|
|
298
|
-
private async _teardown(reason: { readonly kind: "client_close" } | { readonly kind: "remote_close" } | { readonly kind: "error"; readonly error: Error }): Promise<void> {
|
|
299
|
-
if (this.closed) return;
|
|
300
|
-
this.closed = true;
|
|
301
|
-
this.closing = true;
|
|
302
|
-
try {
|
|
303
|
-
await this.transport.close(reason);
|
|
304
|
-
} catch {
|
|
305
|
-
// best-effort
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
private _ensureOpen(): void {
|
|
310
|
-
if (this.closing || this.closed) {
|
|
311
|
-
throw new Error("connection is closing");
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
// ---------------------------------------------------------------------------
|
|
317
|
-
// helpers
|
|
318
|
-
// ---------------------------------------------------------------------------
|
|
319
|
-
|
|
320
|
-
/** Resolve the local transport parameters, filling in defaults. */
|
|
321
|
-
function resolveLocalParameters(options: QuicOptions): QuicTransportParameters {
|
|
322
|
-
const src = options.transportParameters;
|
|
323
|
-
const out: Partial<Record<keyof QuicTransportParameters, bigint | number>> = {};
|
|
324
|
-
if (src?.initialMaxData !== undefined) out.initialMaxData = src.initialMaxData;
|
|
325
|
-
if (src?.initialMaxStreamDataBidiLocal !== undefined) {
|
|
326
|
-
out.initialMaxStreamDataBidiLocal = src.initialMaxStreamDataBidiLocal;
|
|
327
|
-
}
|
|
328
|
-
if (src?.initialMaxStreamDataBidiRemote !== undefined) {
|
|
329
|
-
out.initialMaxStreamDataBidiRemote = src.initialMaxStreamDataBidiRemote;
|
|
330
|
-
}
|
|
331
|
-
if (src?.initialMaxStreamDataUni !== undefined) out.initialMaxStreamDataUni = src.initialMaxStreamDataUni;
|
|
332
|
-
if (src?.initialMaxStreamsBidi !== undefined) out.initialMaxStreamsBidi = src.initialMaxStreamsBidi;
|
|
333
|
-
if (src?.initialMaxStreamsUni !== undefined) out.initialMaxStreamsUni = src.initialMaxStreamsUni;
|
|
334
|
-
if (src?.maxIdleTimeoutMs !== undefined) out.maxIdleTimeoutMs = src.maxIdleTimeoutMs;
|
|
335
|
-
if (src?.maxUdpPayloadSize !== undefined) out.maxUdpPayloadSize = src.maxUdpPayloadSize;
|
|
336
|
-
if (src?.activeConnectionIdLimit !== undefined) out.activeConnectionIdLimit = src.activeConnectionIdLimit;
|
|
337
|
-
return out as QuicTransportParameters;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
// ---------------------------------------------------------------------------
|
|
341
|
-
// connectQuic
|
|
342
|
-
// ---------------------------------------------------------------------------
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* Establish a QUIC connection over an existing datagram transport.
|
|
346
|
-
*
|
|
347
|
-
* Creates the stream manager and the read loop. Because the TLS handshake and
|
|
348
|
-
* packet protection are out of scope, this returns a connection that moves
|
|
349
|
-
* unprotected frames — the data plane is fully functional and testable with a
|
|
350
|
-
* fake datagram transport.
|
|
351
|
-
*/
|
|
352
|
-
export async function connectQuic(options: QuicOptions): Promise<QuicConnection> {
|
|
353
|
-
const id = `quic_${Date.now().toString(36)}`;
|
|
354
|
-
|
|
355
|
-
// The stream manager is constructed before the connection, but its frames
|
|
356
|
-
// must reach the connection's packetizer. We bridge the two with a mutable
|
|
357
|
-
// router that we point at the connection once it exists.
|
|
358
|
-
const frameRouter: { send: (frame: QuicFrame) => void } = { send: () => {} };
|
|
359
|
-
|
|
360
|
-
const manager = createStreamManager({
|
|
361
|
-
sendFrame: (frame) => frameRouter.send(frame),
|
|
362
|
-
localParameters: resolveLocalParameters(options),
|
|
363
|
-
peerParameters: options.transportParameters ?? {},
|
|
364
|
-
});
|
|
365
|
-
|
|
366
|
-
const conn = new QuicConnectionImpl(id, options, manager, options.initialDcid);
|
|
367
|
-
frameRouter.send = (frame) => conn.sendFrame(frame);
|
|
368
|
-
|
|
369
|
-
// A larger send window may have queued stream data — the read loop drains it.
|
|
370
|
-
manager.on("maxData", () => { /* sends drain in the read loop */ });
|
|
371
|
-
|
|
372
|
-
conn.startReadLoop();
|
|
373
|
-
return conn;
|
|
374
|
-
}
|
package/src/errors.ts
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Typed errors for @browsercore/quic.
|
|
3
|
-
*
|
|
4
|
-
* Errors are part of the API — every failure mode is an explicit type so callers
|
|
5
|
-
* can match on `kind` instead of parsing messages.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/** Base class for all QUIC errors. */
|
|
9
|
-
export class QuicError extends Error {
|
|
10
|
-
public readonly kind = "QuicError" as const;
|
|
11
|
-
public override readonly cause: Error | undefined;
|
|
12
|
-
|
|
13
|
-
constructor(message: string, options?: { cause?: Error }) {
|
|
14
|
-
super(message, options);
|
|
15
|
-
this.name = new.target.name;
|
|
16
|
-
this.cause = options?.cause;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/** The peer closed the connection with a CONNECTION_CLOSE frame. */
|
|
21
|
-
export class ConnectionClosedError extends Error {
|
|
22
|
-
public readonly kind = "ConnectionClosedError" as const;
|
|
23
|
-
/** Application error code (application-layer close) or transport error code. */
|
|
24
|
-
public readonly errorCode: bigint;
|
|
25
|
-
/** Frame type that triggered the close, if any. */
|
|
26
|
-
public readonly frameType: bigint | undefined;
|
|
27
|
-
/** Human-readable reason phrase. */
|
|
28
|
-
public readonly reason: string;
|
|
29
|
-
public override readonly cause: Error | undefined;
|
|
30
|
-
|
|
31
|
-
constructor(
|
|
32
|
-
errorCode: bigint,
|
|
33
|
-
reason: string,
|
|
34
|
-
options?: { frameType?: bigint; cause?: Error },
|
|
35
|
-
) {
|
|
36
|
-
super(`CONNECTION_CLOSE: errorCode=${errorCode}, reason="${reason}"`);
|
|
37
|
-
this.name = "ConnectionClosedError";
|
|
38
|
-
this.errorCode = errorCode;
|
|
39
|
-
this.frameType = options?.frameType;
|
|
40
|
-
this.reason = reason;
|
|
41
|
-
this.cause = options?.cause;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** The peer closed a specific stream with a STOP_SENDING frame. */
|
|
46
|
-
export class StopSendingError extends Error {
|
|
47
|
-
public readonly kind = "StopSendingError" as const;
|
|
48
|
-
public readonly streamId: bigint;
|
|
49
|
-
public readonly errorCode: bigint;
|
|
50
|
-
public override readonly cause: Error | undefined;
|
|
51
|
-
|
|
52
|
-
constructor(streamId: bigint, errorCode: bigint, options?: { cause?: Error }) {
|
|
53
|
-
super(`STOP_SENDING on stream ${streamId}: errorCode=${errorCode}`);
|
|
54
|
-
this.name = "StopSendingError";
|
|
55
|
-
this.streamId = streamId;
|
|
56
|
-
this.errorCode = errorCode;
|
|
57
|
-
this.cause = options?.cause;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** The peer reset a specific stream with a RESET_STREAM frame. */
|
|
62
|
-
export class ResetStreamError extends Error {
|
|
63
|
-
public readonly kind = "ResetStreamError" as const;
|
|
64
|
-
public readonly streamId: bigint;
|
|
65
|
-
public readonly errorCode: bigint;
|
|
66
|
-
public readonly finalSize: bigint;
|
|
67
|
-
public override readonly cause: Error | undefined;
|
|
68
|
-
|
|
69
|
-
constructor(streamId: bigint, errorCode: bigint, finalSize: bigint, options?: { cause?: Error }) {
|
|
70
|
-
super(`RESET_STREAM on stream ${streamId}: errorCode=${errorCode}, finalSize=${finalSize}`);
|
|
71
|
-
this.name = "ResetStreamError";
|
|
72
|
-
this.streamId = streamId;
|
|
73
|
-
this.errorCode = errorCode;
|
|
74
|
-
this.finalSize = finalSize;
|
|
75
|
-
this.cause = options?.cause;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** A flow-control window was violated (send exceeded the peer's limit). */
|
|
80
|
-
export class FlowControlError extends Error {
|
|
81
|
-
public readonly kind = "FlowControlError" as const;
|
|
82
|
-
public readonly streamId: bigint | undefined;
|
|
83
|
-
public readonly limit: bigint;
|
|
84
|
-
public readonly attempted: bigint;
|
|
85
|
-
public override readonly cause: Error | undefined;
|
|
86
|
-
|
|
87
|
-
constructor(limit: bigint, attempted: bigint, streamId?: bigint, options?: { cause?: Error }) {
|
|
88
|
-
super(
|
|
89
|
-
`Flow control violation: attempted ${attempted} bytes against limit ${limit} (stream ${streamId ?? "connection"})`,
|
|
90
|
-
);
|
|
91
|
-
this.name = "FlowControlError";
|
|
92
|
-
this.streamId = streamId;
|
|
93
|
-
this.limit = limit;
|
|
94
|
-
this.attempted = attempted;
|
|
95
|
-
this.cause = options?.cause;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/** A packet could not be parsed from the wire. */
|
|
100
|
-
export class PacketParseError extends Error {
|
|
101
|
-
public readonly kind = "PacketParseError" as const;
|
|
102
|
-
public readonly offset: number;
|
|
103
|
-
public override readonly cause: Error | undefined;
|
|
104
|
-
|
|
105
|
-
constructor(offset: number, options?: { cause?: Error }) {
|
|
106
|
-
super(`Packet parse error at offset ${offset}`);
|
|
107
|
-
this.name = "PacketParseError";
|
|
108
|
-
this.offset = offset;
|
|
109
|
-
this.cause = options?.cause;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/** A QUIC frame could not be parsed. */
|
|
114
|
-
export class FrameParseError extends Error {
|
|
115
|
-
public readonly kind = "FrameParseError" as const;
|
|
116
|
-
public readonly offset: number;
|
|
117
|
-
public override readonly cause: Error | undefined;
|
|
118
|
-
|
|
119
|
-
constructor(offset: number, options?: { cause?: Error }) {
|
|
120
|
-
super(`Frame parse error at offset ${offset}`);
|
|
121
|
-
this.name = "FrameParseError";
|
|
122
|
-
this.offset = offset;
|
|
123
|
-
this.cause = options?.cause;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/** The peer violated the transport parameters we advertised. */
|
|
128
|
-
export class TransportParameterError extends Error {
|
|
129
|
-
public readonly kind = "TransportParameterError" as const;
|
|
130
|
-
public readonly parameter: number;
|
|
131
|
-
public override readonly cause: Error | undefined;
|
|
132
|
-
|
|
133
|
-
constructor(parameter: number, options?: { cause?: Error }) {
|
|
134
|
-
super(`Transport parameter error: parameter=0x${parameter.toString(16)}`);
|
|
135
|
-
this.name = "TransportParameterError";
|
|
136
|
-
this.parameter = parameter;
|
|
137
|
-
this.cause = options?.cause;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/** A connection could not be established within the configured timeout. */
|
|
142
|
-
export class HandshakeTimeoutError extends Error {
|
|
143
|
-
public readonly kind = "HandshakeTimeoutError" as const;
|
|
144
|
-
public readonly timeoutMs: number;
|
|
145
|
-
public override readonly cause: Error | undefined;
|
|
146
|
-
|
|
147
|
-
constructor(timeoutMs: number, options?: { cause?: Error }) {
|
|
148
|
-
super(`QUIC handshake not completed within ${timeoutMs}ms`);
|
|
149
|
-
this.name = "HandshakeTimeoutError";
|
|
150
|
-
this.timeoutMs = timeoutMs;
|
|
151
|
-
this.cause = options?.cause;
|
|
152
|
-
}
|
|
153
|
-
}
|