@browsercore/quic 0.1.3 → 0.1.4
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 +13 -13
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +119 -73
- 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 +19 -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 +38 -14
- package/dist/frame/varint.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -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 +1 -1
- package/dist/stream/stream.d.ts.map +1 -1
- package/dist/stream/stream.js +124 -84
- package/dist/stream/stream.js.map +1 -1
- 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 +22 -9
- 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/types.ts
DELETED
|
@@ -1,441 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Domain types for @browsercore/quic.
|
|
3
|
-
*
|
|
4
|
-
* QUIC transport (RFC 9000) over a datagram (UDP) transport. This package owns
|
|
5
|
-
* NO knowledge of HTTP/3, TLS handshake semantics, or sockets — it composes
|
|
6
|
-
* exclusively over an injected {@link DatagramTransport} and
|
|
7
|
-
* `@browsercore/crypto`. Higher layers (http3) compose through
|
|
8
|
-
* {@link QuicConnection}.
|
|
9
|
-
*
|
|
10
|
-
* Key concepts that shape these types:
|
|
11
|
-
* - QUIC is datagram-based (UDP), not a byte stream. Packets carry frames;
|
|
12
|
-
* frames carry stream data. Reliability and ordering are per-stream, not
|
|
13
|
-
* per-connection.
|
|
14
|
-
* - Long headers (Initial, Handshake, 0-RTT, Retry) are used during the
|
|
15
|
-
* - handshake; short headers (1-RTT) are used for data after the handshake.
|
|
16
|
-
* - Connection ids route packets and survive NAT rebinding.
|
|
17
|
-
* - Streams are bidirectional or unidirectional, each with independent flow
|
|
18
|
-
* control. The 62-bit stream id's low 2 bits encode initiator + direction.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
// ---------------------------------------------------------------------------
|
|
22
|
-
// Datagram transport abstraction (injected — this package implements none of it)
|
|
23
|
-
// ---------------------------------------------------------------------------
|
|
24
|
-
|
|
25
|
-
/** A resolved UDP socket address. */
|
|
26
|
-
export interface UdpAddress {
|
|
27
|
-
readonly address: string;
|
|
28
|
-
readonly port: number;
|
|
29
|
-
readonly family: 4 | 6;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** Why a datagram transport was closed. */
|
|
33
|
-
export type DatagramCloseReason =
|
|
34
|
-
| { readonly kind: "client_close" }
|
|
35
|
-
| { readonly kind: "remote_close" }
|
|
36
|
-
| { readonly kind: "error"; readonly error: Error }
|
|
37
|
-
| { readonly kind: "timeout"; readonly afterMs: number };
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The UDP datagram transport abstraction QUIC requires. Implemented by a
|
|
41
|
-
* future UDP transport package (or a thin `node:dgram` adapter); injected here
|
|
42
|
-
* so QUIC stays testable with a fake datagram transport and has no dependency
|
|
43
|
-
* on socket internals.
|
|
44
|
-
*/
|
|
45
|
-
export interface DatagramTransport {
|
|
46
|
-
/** Opaque identifier for logging / correlation. */
|
|
47
|
-
readonly id: string;
|
|
48
|
-
/** Send a datagram to `address`. Resolves once handed to the kernel / buffered. */
|
|
49
|
-
send(data: Uint8Array, address: UdpAddress): Promise<void>;
|
|
50
|
-
/**
|
|
51
|
-
* Receive the next datagram. Resolves with the bytes and the sender's
|
|
52
|
-
* address, or rejects if the transport closes first.
|
|
53
|
-
*/
|
|
54
|
-
recv(): Promise<{ readonly data: Uint8Array; readonly from: UdpAddress }>;
|
|
55
|
-
/** Close the transport. */
|
|
56
|
-
close(reason?: DatagramCloseReason): Promise<void>;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// ---------------------------------------------------------------------------
|
|
60
|
-
// Connection ids (RFC 9000 §5.1)
|
|
61
|
-
// ---------------------------------------------------------------------------
|
|
62
|
-
|
|
63
|
-
/** A QUIC connection id (0–255 bytes, typically 0/8/16). */
|
|
64
|
-
export type ConnectionId = Uint8Array;
|
|
65
|
-
|
|
66
|
-
/** The zero-length connection id constant. */
|
|
67
|
-
export const EMPTY_CONNECTION_ID = new Uint8Array(0);
|
|
68
|
-
|
|
69
|
-
// ---------------------------------------------------------------------------
|
|
70
|
-
// QUIC packet types (RFC 9000 §17)
|
|
71
|
-
// ---------------------------------------------------------------------------
|
|
72
|
-
|
|
73
|
-
/** Long header packet types, encoded in the low 2 bits of the first byte. */
|
|
74
|
-
export const LongPacketType = {
|
|
75
|
-
INITIAL: 0b00,
|
|
76
|
-
ZERO_RTT: 0b01,
|
|
77
|
-
HANDSHAKE: 0b10,
|
|
78
|
-
RETRY: 0b11,
|
|
79
|
-
} as const;
|
|
80
|
-
|
|
81
|
-
export type LongPacketTypeValue = (typeof LongPacketType)[keyof typeof LongPacketType];
|
|
82
|
-
|
|
83
|
-
/** The fixed first-bit flag: 1 = long header, 0 = short header (1-RTT). */
|
|
84
|
-
export const HEADER_FORM_LONG = 1;
|
|
85
|
-
export const HEADER_FORM_SHORT = 0;
|
|
86
|
-
|
|
87
|
-
/** The bit mask for the long packet type in the first byte. */
|
|
88
|
-
export const LONG_PACKET_TYPE_MASK = 0x03;
|
|
89
|
-
|
|
90
|
-
// ---------------------------------------------------------------------------
|
|
91
|
-
// QUIC frame types (RFC 9000 §12.4) — encoded as a varint
|
|
92
|
-
// ---------------------------------------------------------------------------
|
|
93
|
-
|
|
94
|
-
/** QUIC frame type identifiers. */
|
|
95
|
-
export const QuicFrameType = {
|
|
96
|
-
PADDING: 0x00,
|
|
97
|
-
PING: 0x01,
|
|
98
|
-
ACK: 0x02, // 0x02 and 0x03 (with/without ECN)
|
|
99
|
-
ACK_ECN: 0x03,
|
|
100
|
-
RESET_STREAM: 0x04,
|
|
101
|
-
STOP_SENDING: 0x05,
|
|
102
|
-
CRYPTO: 0x06,
|
|
103
|
-
NEW_TOKEN: 0x07,
|
|
104
|
-
STREAM: 0x08, // 0x08..0x0f (off/len/fin bits)
|
|
105
|
-
MAX_DATA: 0x10,
|
|
106
|
-
MAX_STREAM_DATA: 0x11,
|
|
107
|
-
MAX_STREAMS_BIDI: 0x12,
|
|
108
|
-
MAX_STREAMS_UNI: 0x13,
|
|
109
|
-
DATA_BLOCKED: 0x14,
|
|
110
|
-
STREAM_DATA_BLOCKED: 0x15,
|
|
111
|
-
STREAMS_BLOCKED_BIDI: 0x16,
|
|
112
|
-
STREAMS_BLOCKED_UNI: 0x17,
|
|
113
|
-
NEW_CONNECTION_ID: 0x18,
|
|
114
|
-
RETIRE_CONNECTION_ID: 0x19,
|
|
115
|
-
PATH_CHALLENGE: 0x1a,
|
|
116
|
-
PATH_RESPONSE: 0x1b,
|
|
117
|
-
CONNECTION_CLOSE: 0x1c, // transport error
|
|
118
|
-
CONNECTION_CLOSE_APP: 0x1d, // application error
|
|
119
|
-
HANDSHAKE_DONE: 0x1e,
|
|
120
|
-
} as const;
|
|
121
|
-
|
|
122
|
-
export type QuicFrameTypeValue = (typeof QuicFrameType)[keyof typeof QuicFrameType];
|
|
123
|
-
|
|
124
|
-
/** The bit mask for the STREAM frame's offset/length/fin bits. */
|
|
125
|
-
export const STREAM_OFF_BIT = 0x04;
|
|
126
|
-
export const STREAM_LEN_BIT = 0x02;
|
|
127
|
-
export const STREAM_FIN_BIT = 0x01;
|
|
128
|
-
|
|
129
|
-
// ---------------------------------------------------------------------------
|
|
130
|
-
// QUIC transport parameters (RFC 9000 §18.2) — encoded as varint id + varint length + value
|
|
131
|
-
// ---------------------------------------------------------------------------
|
|
132
|
-
|
|
133
|
-
/** Transport parameter identifiers. */
|
|
134
|
-
export const TransportParameter = {
|
|
135
|
-
ORIGINAL_DESTINATION_CONNECTION_ID: 0x00,
|
|
136
|
-
MAX_IDLE_TIMEOUT: 0x01,
|
|
137
|
-
STATELESS_RESET_TOKEN: 0x02,
|
|
138
|
-
MAX_UDP_PAYLOAD_SIZE: 0x03,
|
|
139
|
-
INITIAL_MAX_DATA: 0x04,
|
|
140
|
-
INITIAL_MAX_STREAM_DATA_BIDI_LOCAL: 0x05,
|
|
141
|
-
INITIAL_MAX_STREAM_DATA_BIDI_REMOTE: 0x06,
|
|
142
|
-
INITIAL_MAX_STREAM_DATA_UNI: 0x07,
|
|
143
|
-
INITIAL_MAX_STREAMS_BIDI: 0x08,
|
|
144
|
-
INITIAL_MAX_STREAMS_UNI: 0x09,
|
|
145
|
-
ACK_DELAY_EXPONENT: 0x0a,
|
|
146
|
-
MAX_ACK_DELAY: 0x0b,
|
|
147
|
-
DISABLE_ACTIVE_MIGRATION: 0x0c,
|
|
148
|
-
PREFERRED_ADDRESS: 0x0d,
|
|
149
|
-
ACTIVE_CONNECTION_ID_LIMIT: 0x0e,
|
|
150
|
-
INITIAL_SOURCE_CONNECTION_ID: 0x0f,
|
|
151
|
-
RETIRE_CONNECTION_ID: 0x10, // actually RETIRE_PRIOR_TO = 0x10 is in TLS; see RFC
|
|
152
|
-
RETRY_SOURCE_CONNECTION_ID: 0x10, // (0x10 is not assigned; kept for completeness)
|
|
153
|
-
VERSION_NEGOTIATION: 0x11, // not a real param id; placeholder
|
|
154
|
-
} as const;
|
|
155
|
-
|
|
156
|
-
export type TransportParameterKey = (typeof TransportParameter)[keyof typeof TransportParameter];
|
|
157
|
-
|
|
158
|
-
/** The minimum max UDP payload size QUIC requires (RFC 9000 §14). */
|
|
159
|
-
export const MIN_MAX_UDP_PAYLOAD_SIZE = 1200;
|
|
160
|
-
|
|
161
|
-
// ---------------------------------------------------------------------------
|
|
162
|
-
// QUIC frames — discriminated union over `type`
|
|
163
|
-
// ---------------------------------------------------------------------------
|
|
164
|
-
|
|
165
|
-
/** The common QUIC frame shape — discriminated by `type`. */
|
|
166
|
-
export interface BaseQuicFrame {
|
|
167
|
-
readonly type: QuicFrameTypeValue;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export interface PaddingFrame extends BaseQuicFrame {
|
|
171
|
-
readonly type: typeof QuicFrameType.PADDING;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export interface PingFrame extends BaseQuicFrame {
|
|
175
|
-
readonly type: typeof QuicFrameType.PING;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export interface AckRange {
|
|
179
|
-
readonly gap: bigint;
|
|
180
|
-
readonly ackRangeLength: bigint;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
export interface AckFrame extends BaseQuicFrame {
|
|
184
|
-
readonly type: typeof QuicFrameType.ACK | typeof QuicFrameType.ACK_ECN;
|
|
185
|
-
readonly largestAck: bigint;
|
|
186
|
-
readonly ackDelay: bigint;
|
|
187
|
-
readonly ackRangeCount: bigint;
|
|
188
|
-
readonly firstAckRange: bigint;
|
|
189
|
-
readonly ackRanges: readonly AckRange[];
|
|
190
|
-
readonly ecnCounts?: { readonly ect0: bigint; readonly ect1: bigint; readonly ce: bigint };
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
export interface ResetStreamFrame extends BaseQuicFrame {
|
|
194
|
-
readonly type: typeof QuicFrameType.RESET_STREAM;
|
|
195
|
-
readonly streamId: bigint;
|
|
196
|
-
readonly errorCode: bigint;
|
|
197
|
-
readonly finalSize: bigint;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
export interface StopSendingFrame extends BaseQuicFrame {
|
|
201
|
-
readonly type: typeof QuicFrameType.STOP_SENDING;
|
|
202
|
-
readonly streamId: bigint;
|
|
203
|
-
readonly errorCode: bigint;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
export interface CryptoFrame extends BaseQuicFrame {
|
|
207
|
-
readonly type: typeof QuicFrameType.CRYPTO;
|
|
208
|
-
readonly offset: bigint;
|
|
209
|
-
readonly data: Uint8Array;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
export interface NewTokenFrame extends BaseQuicFrame {
|
|
213
|
-
readonly type: typeof QuicFrameType.NEW_TOKEN;
|
|
214
|
-
readonly token: Uint8Array;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
export interface StreamFrame extends BaseQuicFrame {
|
|
218
|
-
readonly type: typeof QuicFrameType.STREAM;
|
|
219
|
-
readonly streamId: bigint;
|
|
220
|
-
readonly offset: bigint;
|
|
221
|
-
readonly data: Uint8Array;
|
|
222
|
-
/** True if the FIN bit is set — this is the last byte on the stream. */
|
|
223
|
-
readonly fin: boolean;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
export interface MaxDataFrame extends BaseQuicFrame {
|
|
227
|
-
readonly type: typeof QuicFrameType.MAX_DATA;
|
|
228
|
-
readonly maximum: bigint;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export interface MaxStreamDataFrame extends BaseQuicFrame {
|
|
232
|
-
readonly type: typeof QuicFrameType.MAX_STREAM_DATA;
|
|
233
|
-
readonly streamId: bigint;
|
|
234
|
-
readonly maximum: bigint;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
export interface MaxStreamsFrame extends BaseQuicFrame {
|
|
238
|
-
readonly type:
|
|
239
|
-
| typeof QuicFrameType.MAX_STREAMS_BIDI
|
|
240
|
-
| typeof QuicFrameType.MAX_STREAMS_UNI;
|
|
241
|
-
readonly maximum: bigint;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
export interface DataBlockedFrame extends BaseQuicFrame {
|
|
245
|
-
readonly type: typeof QuicFrameType.DATA_BLOCKED;
|
|
246
|
-
readonly limit: bigint;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
export interface StreamDataBlockedFrame extends BaseQuicFrame {
|
|
250
|
-
readonly type: typeof QuicFrameType.STREAM_DATA_BLOCKED;
|
|
251
|
-
readonly streamId: bigint;
|
|
252
|
-
readonly limit: bigint;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
export interface StreamsBlockedFrame extends BaseQuicFrame {
|
|
256
|
-
readonly type:
|
|
257
|
-
| typeof QuicFrameType.STREAMS_BLOCKED_BIDI
|
|
258
|
-
| typeof QuicFrameType.STREAMS_BLOCKED_UNI;
|
|
259
|
-
readonly limit: bigint;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
export interface NewConnectionIdFrame extends BaseQuicFrame {
|
|
263
|
-
readonly type: typeof QuicFrameType.NEW_CONNECTION_ID;
|
|
264
|
-
readonly sequenceNumber: bigint;
|
|
265
|
-
readonly retirePriorTo: bigint;
|
|
266
|
-
readonly connectionId: ConnectionId;
|
|
267
|
-
readonly statelessResetToken: Uint8Array;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
export interface RetireConnectionIdFrame extends BaseQuicFrame {
|
|
271
|
-
readonly type: typeof QuicFrameType.RETIRE_CONNECTION_ID;
|
|
272
|
-
readonly sequenceNumber: bigint;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
export interface PathChallengeFrame extends BaseQuicFrame {
|
|
276
|
-
readonly type: typeof QuicFrameType.PATH_CHALLENGE;
|
|
277
|
-
readonly data: Uint8Array; // 8 bytes
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
export interface PathResponseFrame extends BaseQuicFrame {
|
|
281
|
-
readonly type: typeof QuicFrameType.PATH_RESPONSE;
|
|
282
|
-
readonly data: Uint8Array; // 8 bytes
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
export interface ConnectionCloseFrame extends BaseQuicFrame {
|
|
286
|
-
readonly type:
|
|
287
|
-
| typeof QuicFrameType.CONNECTION_CLOSE
|
|
288
|
-
| typeof QuicFrameType.CONNECTION_CLOSE_APP;
|
|
289
|
-
readonly errorCode: bigint;
|
|
290
|
-
readonly frameType: bigint | undefined;
|
|
291
|
-
readonly reason: string;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
export interface HandshakeDoneFrame extends BaseQuicFrame {
|
|
295
|
-
readonly type: typeof QuicFrameType.HANDSHAKE_DONE;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/** Every QUIC frame variant — exhaustive discriminated union. */
|
|
299
|
-
export type QuicFrame =
|
|
300
|
-
| PaddingFrame
|
|
301
|
-
| PingFrame
|
|
302
|
-
| AckFrame
|
|
303
|
-
| ResetStreamFrame
|
|
304
|
-
| StopSendingFrame
|
|
305
|
-
| CryptoFrame
|
|
306
|
-
| NewTokenFrame
|
|
307
|
-
| StreamFrame
|
|
308
|
-
| MaxDataFrame
|
|
309
|
-
| MaxStreamDataFrame
|
|
310
|
-
| MaxStreamsFrame
|
|
311
|
-
| DataBlockedFrame
|
|
312
|
-
| StreamDataBlockedFrame
|
|
313
|
-
| StreamsBlockedFrame
|
|
314
|
-
| NewConnectionIdFrame
|
|
315
|
-
| RetireConnectionIdFrame
|
|
316
|
-
| PathChallengeFrame
|
|
317
|
-
| PathResponseFrame
|
|
318
|
-
| ConnectionCloseFrame
|
|
319
|
-
| HandshakeDoneFrame;
|
|
320
|
-
|
|
321
|
-
// ---------------------------------------------------------------------------
|
|
322
|
-
// Stream model (RFC 9000 §2)
|
|
323
|
-
// ---------------------------------------------------------------------------
|
|
324
|
-
|
|
325
|
-
/** Stream id is a 62-bit unsigned integer; low 2 bits encode type. */
|
|
326
|
-
export type StreamId = bigint & { __brand: "StreamId" };
|
|
327
|
-
|
|
328
|
-
/** Create a StreamId from a raw value, validating the 62-bit range. */
|
|
329
|
-
export function makeStreamId(value: bigint): StreamId {
|
|
330
|
-
if (value < 0n || value > (1n << 62n) - 1n) {
|
|
331
|
-
throw new RangeError(`stream id out of range: ${value}`);
|
|
332
|
-
}
|
|
333
|
-
return value as StreamId;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/** The two least-significant bits of a stream id encode initiator + direction. */
|
|
337
|
-
export function streamIdIsClientInitiated(id: StreamId): boolean {
|
|
338
|
-
return (id & 1n) === 0n;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
export function streamIdIsBidirectional(id: StreamId): boolean {
|
|
342
|
-
return (id & 2n) === 0n;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Compute the first stream id for a given type (RFC 9000 §2.1). Client-
|
|
347
|
-
* initiated bidirectional streams start at 0; each subsequent stream of the
|
|
348
|
-
* same type increments by 4.
|
|
349
|
-
*/
|
|
350
|
-
export function firstStreamId(bidirectional: boolean, clientInitiated: boolean): StreamId {
|
|
351
|
-
// Low 2 bits encode direction (bit 1) and initiator (bit 0).
|
|
352
|
-
// Bidirectional=0b10 bit clear, unidirectional=set; client=bit clear, server=set.
|
|
353
|
-
const typeBits = (bidirectional ? 0n : 2n) | (clientInitiated ? 0n : 1n);
|
|
354
|
-
return makeStreamId(typeBits);
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
/** Compute the next valid stream id of the same type (increment by 4). */
|
|
358
|
-
export function nextStreamId(current: StreamId): StreamId {
|
|
359
|
-
return makeStreamId(current + 4n);
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
/** Lifecycle state of a QUIC stream. */
|
|
363
|
-
export type StreamState =
|
|
364
|
-
| { readonly state: "open" }
|
|
365
|
-
| { readonly state: "half_closed_local" }
|
|
366
|
-
| { readonly state: "half_closed_remote" }
|
|
367
|
-
| { readonly state: "closed"; readonly reason: StreamCloseReason };
|
|
368
|
-
|
|
369
|
-
/** Why a stream entered the `closed` state. */
|
|
370
|
-
export type StreamCloseReason =
|
|
371
|
-
| { readonly kind: "reset"; readonly errorCode: bigint }
|
|
372
|
-
| { readonly kind: "stop_sending"; readonly errorCode: bigint }
|
|
373
|
-
| { readonly kind: "normal" }
|
|
374
|
-
| { readonly kind: "connection_close" };
|
|
375
|
-
|
|
376
|
-
// ---------------------------------------------------------------------------
|
|
377
|
-
// QUIC stream + connection interfaces (the contract HTTP/3 consumes)
|
|
378
|
-
// ---------------------------------------------------------------------------
|
|
379
|
-
|
|
380
|
-
/** A bidirectional or unidirectional QUIC stream: a reliable, ordered byte stream. */
|
|
381
|
-
export interface QuicStream {
|
|
382
|
-
/** QUIC stream id (62-bit). */
|
|
383
|
-
readonly id: StreamId;
|
|
384
|
-
/** Write bytes to the stream. Resolves when handed to the QUIC layer / buffered. */
|
|
385
|
-
write(data: Uint8Array): Promise<void>;
|
|
386
|
-
/** Read the next chunk of bytes, or reject if the stream closes first. */
|
|
387
|
-
read(): Promise<Uint8Array>;
|
|
388
|
-
/** Close the stream (send FIN / RESET_STREAM). */
|
|
389
|
-
close(): Promise<void>;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* Public contract for a QUIC connection. This is the interface HTTP/3 depends
|
|
394
|
-
* on — implemented by this package's `QuicConnectionImpl`, consumed by
|
|
395
|
-
* `@browsercore/http3`.
|
|
396
|
-
*/
|
|
397
|
-
export interface QuicConnection {
|
|
398
|
-
/** Opaque identifier for logging / correlation. */
|
|
399
|
-
readonly id: string;
|
|
400
|
-
/** Open a new bidirectional stream (request/response). */
|
|
401
|
-
openBidirectionalStream(): Promise<QuicStream>;
|
|
402
|
-
/** Accept the next incoming bidirectional stream from the peer. */
|
|
403
|
-
acceptBidirectionalStream(): Promise<QuicStream>;
|
|
404
|
-
/** Open a new unidirectional stream (control / QPACK / push). */
|
|
405
|
-
openUnidirectionalStream(): Promise<QuicStream>;
|
|
406
|
-
/** Accept the next incoming unidirectional stream from the peer. */
|
|
407
|
-
acceptUnidirectionalStream(): Promise<QuicStream>;
|
|
408
|
-
/** Close the QUIC connection with an error code and reason. */
|
|
409
|
-
close(errorCode: bigint, reason: string): Promise<void>;
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
/** Options for {@link connectQuic}. */
|
|
413
|
-
export interface QuicOptions {
|
|
414
|
-
/** The underlying datagram (UDP) transport (already bound). */
|
|
415
|
-
readonly transport: DatagramTransport;
|
|
416
|
-
/** The peer's UDP address. */
|
|
417
|
-
readonly peer: UdpAddress;
|
|
418
|
-
/** Server name (SNI) for the handshake. */
|
|
419
|
-
readonly serverName: string;
|
|
420
|
-
/** Connection id to use for the handshake. */
|
|
421
|
-
readonly initialDcid: ConnectionId;
|
|
422
|
-
/** Our initial source connection id. */
|
|
423
|
-
readonly initialScid: ConnectionId;
|
|
424
|
-
/** Handshake timeout in milliseconds. Default 10_000. */
|
|
425
|
-
readonly handshakeTimeoutMs?: number;
|
|
426
|
-
/** Our transport parameters to advertise. */
|
|
427
|
-
readonly transportParameters?: QuicTransportParameters;
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
/** QUIC transport parameters the local endpoint advertises. */
|
|
431
|
-
export interface QuicTransportParameters {
|
|
432
|
-
readonly maxIdleTimeoutMs?: number;
|
|
433
|
-
readonly maxUdpPayloadSize?: number;
|
|
434
|
-
readonly initialMaxData?: bigint;
|
|
435
|
-
readonly initialMaxStreamDataBidiLocal?: bigint;
|
|
436
|
-
readonly initialMaxStreamDataBidiRemote?: bigint;
|
|
437
|
-
readonly initialMaxStreamDataUni?: bigint;
|
|
438
|
-
readonly initialMaxStreamsBidi?: bigint;
|
|
439
|
-
readonly initialMaxStreamsUni?: bigint;
|
|
440
|
-
readonly activeConnectionIdLimit?: number;
|
|
441
|
-
}
|
package/src/utils.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Small shared helpers for @browsercore/quic.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Exhaustiveness check for `switch`/`if-else` over discriminated unions.
|
|
7
|
-
* Call in the `default` branch: `default: assertNever(x)`.
|
|
8
|
-
* Adding a new union member forces every handler to compile-error until handled.
|
|
9
|
-
*/
|
|
10
|
-
export function assertNever(x: never): never {
|
|
11
|
-
throw new Error(`Unexpected value: ${JSON.stringify(x)}`);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/** Concatenate two byte arrays. */
|
|
15
|
-
export function concat(a: Uint8Array, b: Uint8Array): Uint8Array {
|
|
16
|
-
const out = new Uint8Array(a.length + b.length);
|
|
17
|
-
out.set(a, 0);
|
|
18
|
-
out.set(b, a.length);
|
|
19
|
-
return out;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** Concatenate many byte arrays into one. */
|
|
23
|
-
export function concatAll(parts: readonly Uint8Array[]): Uint8Array {
|
|
24
|
-
let total = 0;
|
|
25
|
-
for (const p of parts) total += p.length;
|
|
26
|
-
const out = new Uint8Array(total);
|
|
27
|
-
let offset = 0;
|
|
28
|
-
for (const p of parts) {
|
|
29
|
-
out.set(p, offset);
|
|
30
|
-
offset += p.length;
|
|
31
|
-
}
|
|
32
|
-
return out;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** Hex dump for debug logging of packet bytes. */
|
|
36
|
-
export function hex(bytes: Uint8Array): string {
|
|
37
|
-
return [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
38
|
-
}
|
package/tests/connection.test.ts
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Connection integration tests for @browsercore/quic.
|
|
3
|
-
*
|
|
4
|
-
* Drives a real `QuicConnectionImpl` over a fake datagram pair: a client
|
|
5
|
-
* connection and a scripted peer exchange datagrams. Because the TLS handshake
|
|
6
|
-
* and packet protection are out of scope, these tests move *unprotected* frames
|
|
7
|
-
* — enough to verify the read loop, stream open/accept, and frame dispatch.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { describe, it, expect, afterAll } from "vitest";
|
|
11
|
-
import { connectQuic } from "../src/connection.js";
|
|
12
|
-
import { QuicFrameType } from "../src/types.js";
|
|
13
|
-
import { serializeFrame } from "../src/frame/frame.js";
|
|
14
|
-
import { serializeShortHeader } from "../src/packet/packet.js";
|
|
15
|
-
import { concatAll } from "../src/utils.js";
|
|
16
|
-
import { createFakeDatagramPair, PEER_ADDR } from "./fake-transport.js";
|
|
17
|
-
|
|
18
|
-
/** Wrap unprotected frames in a short-header 1-RTT packet. */
|
|
19
|
-
function makePacket(frames: ReturnType<typeof serializeFrame>[]): Uint8Array {
|
|
20
|
-
const payload = concatAll(frames);
|
|
21
|
-
const dcid = new Uint8Array([0x01, 0x02, 0x03]);
|
|
22
|
-
const header = serializeShortHeader(dcid, 1, false, false);
|
|
23
|
-
const packetNumber = new Uint8Array([0]);
|
|
24
|
-
return concatAll([header, packetNumber, payload]);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** Give the event loop a few ticks to let the read loop process datagrams. */
|
|
28
|
-
const tick = (ms = 5) => new Promise<void>((resolve) => setTimeout(resolve, ms));
|
|
29
|
-
|
|
30
|
-
describe("connectQuic", () => {
|
|
31
|
-
it("returns a connection with an id and open streams", async () => {
|
|
32
|
-
const { client, server } = createFakeDatagramPair();
|
|
33
|
-
const conn = await connectQuic({
|
|
34
|
-
transport: client,
|
|
35
|
-
peer: PEER_ADDR,
|
|
36
|
-
serverName: "localhost",
|
|
37
|
-
initialDcid: new Uint8Array([0x01, 0x02, 0x03]),
|
|
38
|
-
initialScid: new Uint8Array([0x04, 0x05]),
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
expect(conn.id.startsWith("quic_")).toBe(true);
|
|
42
|
-
|
|
43
|
-
const stream = await conn.openBidirectionalStream();
|
|
44
|
-
expect(stream.id).toBe(0n);
|
|
45
|
-
|
|
46
|
-
await conn.close(0x00n, "done");
|
|
47
|
-
await tick();
|
|
48
|
-
void server;
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it("accepts a peer-opened bidirectional stream", async () => {
|
|
52
|
-
const { client, server } = createFakeDatagramPair();
|
|
53
|
-
const conn = await connectQuic({
|
|
54
|
-
transport: client,
|
|
55
|
-
peer: PEER_ADDR,
|
|
56
|
-
serverName: "localhost",
|
|
57
|
-
initialDcid: new Uint8Array([0x01, 0x02, 0x03]),
|
|
58
|
-
initialScid: new Uint8Array([0x04, 0x05]),
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
// Peer opens a server-initiated bidirectional stream (id 1) by sending
|
|
62
|
-
// a STREAM frame. Client-initiated bidi streams are even (0, 4, 8…);
|
|
63
|
-
// server-initiated bidi streams are odd (1, 5, 9…).
|
|
64
|
-
const streamFrame = serializeFrame({
|
|
65
|
-
type: QuicFrameType.STREAM,
|
|
66
|
-
streamId: 1n,
|
|
67
|
-
offset: 0n,
|
|
68
|
-
data: new Uint8Array([0xca, 0xfe]),
|
|
69
|
-
fin: true,
|
|
70
|
-
});
|
|
71
|
-
await server.send(makePacket([streamFrame]), PEER_ADDR);
|
|
72
|
-
await tick();
|
|
73
|
-
|
|
74
|
-
const accepted = await conn.acceptBidirectionalStream();
|
|
75
|
-
expect(accepted.id).toBe(1n);
|
|
76
|
-
|
|
77
|
-
const chunk = await accepted.read();
|
|
78
|
-
expect(Array.from(chunk)).toEqual([0xca, 0xfe]);
|
|
79
|
-
|
|
80
|
-
await conn.close(0x00n, "done");
|
|
81
|
-
await tick();
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it("dispatches a CONNECTION_CLOSE from the peer", async () => {
|
|
85
|
-
const { client, server } = createFakeDatagramPair();
|
|
86
|
-
const conn = await connectQuic({
|
|
87
|
-
transport: client,
|
|
88
|
-
peer: PEER_ADDR,
|
|
89
|
-
serverName: "localhost",
|
|
90
|
-
initialDcid: new Uint8Array([0x01, 0x02, 0x03]),
|
|
91
|
-
initialScid: new Uint8Array([0x04, 0x05]),
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
const closeFrame = serializeFrame({
|
|
95
|
-
type: QuicFrameType.CONNECTION_CLOSE,
|
|
96
|
-
errorCode: 0x00n,
|
|
97
|
-
frameType: undefined,
|
|
98
|
-
reason: "bye",
|
|
99
|
-
});
|
|
100
|
-
await server.send(makePacket([closeFrame]), PEER_ADDR);
|
|
101
|
-
await tick();
|
|
102
|
-
|
|
103
|
-
// After a peer close, opening a new stream must reject.
|
|
104
|
-
await expect(conn.openBidirectionalStream()).rejects.toThrow(/closing/);
|
|
105
|
-
});
|
|
106
|
-
});
|
package/tests/fake-transport.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A scriptable in-memory DatagramTransport for testing the QUIC connection
|
|
3
|
-
* without a real network. It implements the `DatagramTransport` interface from
|
|
4
|
-
* this package over internal datagram queues so a test can push datagrams the
|
|
5
|
-
* client "receives" and inspect datagrams the client "sent".
|
|
6
|
-
*
|
|
7
|
-
* It is NOT a real transport — it exists only to drive connection.test.ts.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import type { DatagramTransport, UdpAddress } from "../src/types.js";
|
|
11
|
-
|
|
12
|
-
/** A fixed IPv4 loopback address for tests. */
|
|
13
|
-
export const LOCAL_ADDR: UdpAddress = { address: "127.0.0.1", port: 4433, family: 4 };
|
|
14
|
-
export const PEER_ADDR: UdpAddress = { address: "127.0.0.1", port: 8443, family: 4 };
|
|
15
|
-
|
|
16
|
-
/** A single buffered datagram. */
|
|
17
|
-
interface Envelope {
|
|
18
|
-
readonly data: Uint8Array;
|
|
19
|
-
readonly from: UdpAddress;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* A pair of connected fake datagram transports: everything sent by one is
|
|
24
|
-
* receivable by the other. Used to connect a client to a scripted "peer".
|
|
25
|
-
*/
|
|
26
|
-
export function createFakeDatagramPair(): {
|
|
27
|
-
client: FakeDatagramTransport;
|
|
28
|
-
server: FakeDatagramTransport;
|
|
29
|
-
} {
|
|
30
|
-
const client = new FakeDatagramTransport("client", PEER_ADDR);
|
|
31
|
-
const server = new FakeDatagramTransport("server", LOCAL_ADDR);
|
|
32
|
-
client._peer = server;
|
|
33
|
-
server._peer = client;
|
|
34
|
-
return { client, server };
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export class FakeDatagramTransport implements DatagramTransport {
|
|
38
|
-
public readonly id: string;
|
|
39
|
-
private readonly _peer: FakeDatagramTransport | undefined;
|
|
40
|
-
private readonly _localAddr: UdpAddress;
|
|
41
|
-
private _closed = false;
|
|
42
|
-
/** Datagrams sent by this side, buffered for the peer to recv(). */
|
|
43
|
-
private readonly _outbox: Envelope[] = [];
|
|
44
|
-
/** Waiters blocked on a recv() when the outbox was empty. */
|
|
45
|
-
private _pendingRecv: ((env: Envelope) => void) | undefined;
|
|
46
|
-
private _pendingRecvReject: ((err: Error) => void) | undefined;
|
|
47
|
-
|
|
48
|
-
public constructor(id: string, localAddr: UdpAddress, peer?: FakeDatagramTransport) {
|
|
49
|
-
this.id = id;
|
|
50
|
-
this._localAddr = localAddr;
|
|
51
|
-
this._peer = peer;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public send(data: Uint8Array, _address: UdpAddress): Promise<void> {
|
|
55
|
-
if (this._closed) return Promise.reject(new Error(`transport ${this.id} is closed`));
|
|
56
|
-
const peer = this._peer;
|
|
57
|
-
if (peer !== undefined) {
|
|
58
|
-
peer._deliver(data);
|
|
59
|
-
return Promise.resolve();
|
|
60
|
-
}
|
|
61
|
-
// No peer: buffer for a test to drain via sentDatagrams().
|
|
62
|
-
this._outbox.push({ data, from: this._localAddr });
|
|
63
|
-
return Promise.resolve();
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
public recv(): Promise<{ readonly data: Uint8Array; readonly from: UdpAddress }> {
|
|
67
|
-
if (this._closed) return Promise.reject(new Error(`transport ${this.id} is closed`));
|
|
68
|
-
if (this._outbox.length > 0) {
|
|
69
|
-
const env = this._outbox.shift()!;
|
|
70
|
-
return Promise.resolve(env);
|
|
71
|
-
}
|
|
72
|
-
return new Promise<{ readonly data: Uint8Array; readonly from: UdpAddress }>((resolve, reject) => {
|
|
73
|
-
this._pendingRecv = resolve;
|
|
74
|
-
this._pendingRecvReject = reject;
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
public close(reason?: { readonly kind: string }): Promise<void> {
|
|
79
|
-
if (this._closed) return Promise.resolve();
|
|
80
|
-
this._closed = true;
|
|
81
|
-
const rejecter = this._pendingRecvReject;
|
|
82
|
-
if (rejecter !== undefined) {
|
|
83
|
-
this._pendingRecv = undefined;
|
|
84
|
-
this._pendingRecvReject = undefined;
|
|
85
|
-
rejecter(new Error(`transport closed: ${reason?.kind ?? "unknown"}`));
|
|
86
|
-
}
|
|
87
|
-
return Promise.resolve();
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/** True once close() has been called. */
|
|
91
|
-
public get isClosed(): boolean {
|
|
92
|
-
return this._closed;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/** Push a datagram into this side's recv queue (simulating data arriving). */
|
|
96
|
-
private _deliver(data: Uint8Array): void {
|
|
97
|
-
const env: Envelope = { data, from: this._peer?._localAddr ?? this._localAddr };
|
|
98
|
-
const pending = this._pendingRecv;
|
|
99
|
-
if (pending !== undefined) {
|
|
100
|
-
this._pendingRecv = undefined;
|
|
101
|
-
this._pendingRecvReject = undefined;
|
|
102
|
-
pending(env);
|
|
103
|
-
} else {
|
|
104
|
-
this._outbox.push(env);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
}
|