@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/frame/frame.ts
DELETED
|
@@ -1,423 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* QUIC frame parser and serializer (RFC 9000 §12).
|
|
3
|
-
*
|
|
4
|
-
* Pure wire-format logic — no I/O. Frames are written into packets; the frame
|
|
5
|
-
* layer does not own the packet or the transport. Each frame is encoded as its
|
|
6
|
-
* type varint followed by type-specific fields. The reader functions consume
|
|
7
|
-
* bytes from a pull-based source so they can be driven by a packet reassembly
|
|
8
|
-
* buffer.
|
|
9
|
-
*
|
|
10
|
-
* STREAM frames (0x08–0x0f) are encoded with their off/len/fin flags folded
|
|
11
|
-
* into the type byte; the reader/writer handle that transparently.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import type { QuicFrame, QuicFrameTypeValue } from "../types.js";
|
|
15
|
-
import { QuicFrameType, STREAM_OFF_BIT, STREAM_LEN_BIT, STREAM_FIN_BIT } from "../types.js";
|
|
16
|
-
import { decodeVarint, encodeVarint } from "./varint.js";
|
|
17
|
-
import { assertNever, concat, concatAll } from "../utils.js";
|
|
18
|
-
|
|
19
|
-
export {
|
|
20
|
-
QuicFrameType,
|
|
21
|
-
type QuicFrame,
|
|
22
|
-
type QuicFrameTypeValue,
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/** Serialize a single QUIC frame to wire bytes (type varint + fields). */
|
|
26
|
-
export function serializeFrame(frame: QuicFrame): Uint8Array {
|
|
27
|
-
switch (frame.type) {
|
|
28
|
-
case QuicFrameType.PADDING:
|
|
29
|
-
return new Uint8Array([QuicFrameType.PADDING]);
|
|
30
|
-
case QuicFrameType.PING:
|
|
31
|
-
return new Uint8Array([QuicFrameType.PING]);
|
|
32
|
-
|
|
33
|
-
case QuicFrameType.ACK:
|
|
34
|
-
case QuicFrameType.ACK_ECN: {
|
|
35
|
-
const body = concatAll([
|
|
36
|
-
encodeVarint(frame.largestAck),
|
|
37
|
-
encodeVarint(frame.ackDelay),
|
|
38
|
-
encodeVarint(frame.ackRangeCount),
|
|
39
|
-
encodeVarint(frame.firstAckRange),
|
|
40
|
-
...frame.ackRanges.flatMap((r) => [encodeVarint(r.gap), encodeVarint(r.ackRangeLength)]),
|
|
41
|
-
]);
|
|
42
|
-
if (frame.type === QuicFrameType.ACK_ECN && frame.ecnCounts) {
|
|
43
|
-
return concatAll([
|
|
44
|
-
encodeVarint(BigInt(QuicFrameType.ACK_ECN)),
|
|
45
|
-
body,
|
|
46
|
-
encodeVarint(frame.ecnCounts.ect0),
|
|
47
|
-
encodeVarint(frame.ecnCounts.ect1),
|
|
48
|
-
encodeVarint(frame.ecnCounts.ce),
|
|
49
|
-
]);
|
|
50
|
-
}
|
|
51
|
-
return concatAll([encodeVarint(BigInt(QuicFrameType.ACK)), body]);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
case QuicFrameType.RESET_STREAM:
|
|
55
|
-
return concatAll([
|
|
56
|
-
encodeVarint(BigInt(frame.type)),
|
|
57
|
-
encodeVarint(frame.streamId),
|
|
58
|
-
encodeVarint(frame.errorCode),
|
|
59
|
-
encodeVarint(frame.finalSize),
|
|
60
|
-
]);
|
|
61
|
-
|
|
62
|
-
case QuicFrameType.STOP_SENDING:
|
|
63
|
-
return concatAll([
|
|
64
|
-
encodeVarint(BigInt(frame.type)),
|
|
65
|
-
encodeVarint(frame.streamId),
|
|
66
|
-
encodeVarint(frame.errorCode),
|
|
67
|
-
]);
|
|
68
|
-
|
|
69
|
-
case QuicFrameType.CRYPTO:
|
|
70
|
-
return concatAll([
|
|
71
|
-
encodeVarint(BigInt(frame.type)),
|
|
72
|
-
encodeVarint(frame.offset),
|
|
73
|
-
encodeVarint(BigInt(frame.data.length)),
|
|
74
|
-
frame.data,
|
|
75
|
-
]);
|
|
76
|
-
|
|
77
|
-
case QuicFrameType.NEW_TOKEN:
|
|
78
|
-
return concatAll([
|
|
79
|
-
encodeVarint(BigInt(frame.type)),
|
|
80
|
-
encodeVarint(BigInt(frame.token.length)),
|
|
81
|
-
frame.token,
|
|
82
|
-
]);
|
|
83
|
-
|
|
84
|
-
case QuicFrameType.STREAM: {
|
|
85
|
-
// Reconstruct the type byte with off/len/fin flags.
|
|
86
|
-
let typeByte = QuicFrameType.STREAM;
|
|
87
|
-
if (frame.offset > 0n) typeByte |= STREAM_OFF_BIT;
|
|
88
|
-
// Length is always present in this encoding (simplification).
|
|
89
|
-
typeByte |= STREAM_LEN_BIT;
|
|
90
|
-
if (frame.fin) typeByte |= STREAM_FIN_BIT;
|
|
91
|
-
const length = BigInt(frame.data.length);
|
|
92
|
-
return concatAll([
|
|
93
|
-
encodeVarint(BigInt(typeByte)),
|
|
94
|
-
encodeVarint(frame.streamId),
|
|
95
|
-
frame.offset > 0n ? encodeVarint(frame.offset) : new Uint8Array(0),
|
|
96
|
-
encodeVarint(length),
|
|
97
|
-
frame.data,
|
|
98
|
-
]);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
case QuicFrameType.MAX_DATA:
|
|
102
|
-
return concatAll([encodeVarint(BigInt(frame.type)), encodeVarint(frame.maximum)]);
|
|
103
|
-
|
|
104
|
-
case QuicFrameType.MAX_STREAM_DATA:
|
|
105
|
-
return concatAll([
|
|
106
|
-
encodeVarint(BigInt(frame.type)),
|
|
107
|
-
encodeVarint(frame.streamId),
|
|
108
|
-
encodeVarint(frame.maximum),
|
|
109
|
-
]);
|
|
110
|
-
|
|
111
|
-
case QuicFrameType.MAX_STREAMS_BIDI:
|
|
112
|
-
case QuicFrameType.MAX_STREAMS_UNI:
|
|
113
|
-
return concatAll([encodeVarint(BigInt(frame.type)), encodeVarint(frame.maximum)]);
|
|
114
|
-
|
|
115
|
-
case QuicFrameType.DATA_BLOCKED:
|
|
116
|
-
return concatAll([encodeVarint(BigInt(frame.type)), encodeVarint(frame.limit)]);
|
|
117
|
-
|
|
118
|
-
case QuicFrameType.STREAM_DATA_BLOCKED:
|
|
119
|
-
return concatAll([
|
|
120
|
-
encodeVarint(BigInt(frame.type)),
|
|
121
|
-
encodeVarint(frame.streamId),
|
|
122
|
-
encodeVarint(frame.limit),
|
|
123
|
-
]);
|
|
124
|
-
|
|
125
|
-
case QuicFrameType.STREAMS_BLOCKED_BIDI:
|
|
126
|
-
case QuicFrameType.STREAMS_BLOCKED_UNI:
|
|
127
|
-
return concatAll([encodeVarint(BigInt(frame.type)), encodeVarint(frame.limit)]);
|
|
128
|
-
|
|
129
|
-
case QuicFrameType.NEW_CONNECTION_ID:
|
|
130
|
-
return concatAll([
|
|
131
|
-
encodeVarint(BigInt(frame.type)),
|
|
132
|
-
encodeVarint(frame.sequenceNumber),
|
|
133
|
-
encodeVarint(frame.retirePriorTo),
|
|
134
|
-
encodeVarint(BigInt(frame.connectionId.length)),
|
|
135
|
-
frame.connectionId,
|
|
136
|
-
frame.statelessResetToken,
|
|
137
|
-
]);
|
|
138
|
-
|
|
139
|
-
case QuicFrameType.RETIRE_CONNECTION_ID:
|
|
140
|
-
return concatAll([encodeVarint(BigInt(frame.type)), encodeVarint(frame.sequenceNumber)]);
|
|
141
|
-
|
|
142
|
-
case QuicFrameType.PATH_CHALLENGE:
|
|
143
|
-
case QuicFrameType.PATH_RESPONSE:
|
|
144
|
-
return concatAll([encodeVarint(BigInt(frame.type)), frame.data]);
|
|
145
|
-
|
|
146
|
-
case QuicFrameType.CONNECTION_CLOSE:
|
|
147
|
-
case QuicFrameType.CONNECTION_CLOSE_APP: {
|
|
148
|
-
const reasonBytes = new TextEncoder().encode(frame.reason) as Uint8Array<ArrayBuffer>;
|
|
149
|
-
return concatAll([
|
|
150
|
-
encodeVarint(BigInt(frame.type)),
|
|
151
|
-
encodeVarint(frame.errorCode),
|
|
152
|
-
frame.frameType !== undefined
|
|
153
|
-
? encodeVarint(frame.frameType)
|
|
154
|
-
: new Uint8Array(0),
|
|
155
|
-
encodeVarint(BigInt(reasonBytes.length)),
|
|
156
|
-
reasonBytes,
|
|
157
|
-
]);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
case QuicFrameType.HANDSHAKE_DONE:
|
|
161
|
-
return new Uint8Array([QuicFrameType.HANDSHAKE_DONE]);
|
|
162
|
-
|
|
163
|
-
default:
|
|
164
|
-
return assertNever(frame);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/** Read frames from a pull-based byte source until it reports end-of-packet. */
|
|
169
|
-
export function readFrames(
|
|
170
|
-
read: () => Promise<Uint8Array | null>,
|
|
171
|
-
): AsyncGenerator<QuicFrame, void, unknown> {
|
|
172
|
-
return {
|
|
173
|
-
[Symbol.asyncIterator]() {
|
|
174
|
-
let buffer: Uint8Array<ArrayBufferLike> = new Uint8Array(0);
|
|
175
|
-
let done = false;
|
|
176
|
-
|
|
177
|
-
async function fill(minBytes: number): Promise<boolean> {
|
|
178
|
-
while (buffer.length < minBytes && !done) {
|
|
179
|
-
const chunk = await read();
|
|
180
|
-
if (chunk === null) {
|
|
181
|
-
done = true;
|
|
182
|
-
break;
|
|
183
|
-
}
|
|
184
|
-
buffer = concat(buffer, chunk);
|
|
185
|
-
}
|
|
186
|
-
return buffer.length >= minBytes;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
async function readVarintFromBuffer(): Promise<bigint> {
|
|
190
|
-
// Ensure at least one byte to read the length prefix.
|
|
191
|
-
if (!(await fill(1))) {
|
|
192
|
-
throw new Error("unexpected end of frame data");
|
|
193
|
-
}
|
|
194
|
-
const first = buffer[0]!;
|
|
195
|
-
const length = 1 << ((first >> 6) & 0x03);
|
|
196
|
-
if (!(await fill(length))) {
|
|
197
|
-
throw new Error("unexpected end of frame data");
|
|
198
|
-
}
|
|
199
|
-
const { value, length: consumed } = decodeVarint(buffer);
|
|
200
|
-
buffer = buffer.subarray(consumed);
|
|
201
|
-
return value;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
async function readBytes(count: bigint): Promise<Uint8Array> {
|
|
205
|
-
const n = Number(count);
|
|
206
|
-
if (!(await fill(n))) {
|
|
207
|
-
throw new Error("unexpected end of frame data");
|
|
208
|
-
}
|
|
209
|
-
const out = buffer.subarray(0, n);
|
|
210
|
-
buffer = buffer.subarray(n);
|
|
211
|
-
return out;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
async function parseOne(): Promise<QuicFrame> {
|
|
215
|
-
const type = await readVarintFromBuffer();
|
|
216
|
-
return decodeFrame(type, readVarintFromBuffer, readBytes);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return {
|
|
220
|
-
next(): Promise<IteratorResult<QuicFrame>> {
|
|
221
|
-
return (async () => {
|
|
222
|
-
// Peek: is there any data left?
|
|
223
|
-
if (!(await fill(1))) {
|
|
224
|
-
return { done: true, value: undefined };
|
|
225
|
-
}
|
|
226
|
-
const frame = await parseOne();
|
|
227
|
-
return { done: false, value: frame };
|
|
228
|
-
})();
|
|
229
|
-
},
|
|
230
|
-
};
|
|
231
|
-
},
|
|
232
|
-
} as AsyncGenerator<QuicFrame>;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/** Decode a single frame given its type value and byte-reading helpers. */
|
|
236
|
-
export async function decodeFrame(
|
|
237
|
-
type: bigint,
|
|
238
|
-
readVarint: () => Promise<bigint>,
|
|
239
|
-
readBytes: (count: bigint) => Promise<Uint8Array>,
|
|
240
|
-
): Promise<QuicFrame> {
|
|
241
|
-
switch (type) {
|
|
242
|
-
case BigInt(QuicFrameType.PADDING):
|
|
243
|
-
return { type: QuicFrameType.PADDING };
|
|
244
|
-
case BigInt(QuicFrameType.PING):
|
|
245
|
-
return { type: QuicFrameType.PING };
|
|
246
|
-
|
|
247
|
-
case BigInt(QuicFrameType.ACK): {
|
|
248
|
-
const largestAck = await readVarint();
|
|
249
|
-
const ackDelay = await readVarint();
|
|
250
|
-
const ackRangeCount = await readVarint();
|
|
251
|
-
const firstAckRange = await readVarint();
|
|
252
|
-
const ackRanges: Array<{ gap: bigint; ackRangeLength: bigint }> = [];
|
|
253
|
-
for (let i = 0n; i < ackRangeCount; i++) {
|
|
254
|
-
const gap = await readVarint();
|
|
255
|
-
const ackRangeLength = await readVarint();
|
|
256
|
-
ackRanges.push({ gap, ackRangeLength });
|
|
257
|
-
}
|
|
258
|
-
return {
|
|
259
|
-
type: QuicFrameType.ACK,
|
|
260
|
-
largestAck,
|
|
261
|
-
ackDelay,
|
|
262
|
-
ackRangeCount,
|
|
263
|
-
firstAckRange,
|
|
264
|
-
ackRanges,
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
case BigInt(QuicFrameType.ACK_ECN): {
|
|
269
|
-
const largestAck = await readVarint();
|
|
270
|
-
const ackDelay = await readVarint();
|
|
271
|
-
const ackRangeCount = await readVarint();
|
|
272
|
-
const firstAckRange = await readVarint();
|
|
273
|
-
const ackRanges: Array<{ gap: bigint; ackRangeLength: bigint }> = [];
|
|
274
|
-
for (let i = 0n; i < ackRangeCount; i++) {
|
|
275
|
-
const gap = await readVarint();
|
|
276
|
-
const ackRangeLength = await readVarint();
|
|
277
|
-
ackRanges.push({ gap, ackRangeLength });
|
|
278
|
-
}
|
|
279
|
-
const ect0 = await readVarint();
|
|
280
|
-
const ect1 = await readVarint();
|
|
281
|
-
const ce = await readVarint();
|
|
282
|
-
return {
|
|
283
|
-
type: QuicFrameType.ACK_ECN,
|
|
284
|
-
largestAck,
|
|
285
|
-
ackDelay,
|
|
286
|
-
ackRangeCount,
|
|
287
|
-
firstAckRange,
|
|
288
|
-
ackRanges,
|
|
289
|
-
ecnCounts: { ect0, ect1, ce },
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
case BigInt(QuicFrameType.RESET_STREAM): {
|
|
294
|
-
const streamId = await readVarint();
|
|
295
|
-
const errorCode = await readVarint();
|
|
296
|
-
const finalSize = await readVarint();
|
|
297
|
-
return { type: QuicFrameType.RESET_STREAM, streamId, errorCode, finalSize };
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
case BigInt(QuicFrameType.STOP_SENDING): {
|
|
301
|
-
const streamId = await readVarint();
|
|
302
|
-
const errorCode = await readVarint();
|
|
303
|
-
return { type: QuicFrameType.STOP_SENDING, streamId, errorCode };
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
case BigInt(QuicFrameType.CRYPTO): {
|
|
307
|
-
const offset = await readVarint();
|
|
308
|
-
const length = await readVarint();
|
|
309
|
-
const data = await readBytes(length);
|
|
310
|
-
return { type: QuicFrameType.CRYPTO, offset, data };
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
case BigInt(QuicFrameType.NEW_TOKEN): {
|
|
314
|
-
const length = await readVarint();
|
|
315
|
-
const token = await readBytes(length);
|
|
316
|
-
return { type: QuicFrameType.NEW_TOKEN, token };
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
// STREAM frames: type byte 0x08..0x0f.
|
|
320
|
-
case 0x08n:
|
|
321
|
-
case 0x09n:
|
|
322
|
-
case 0x0an:
|
|
323
|
-
case 0x0bn:
|
|
324
|
-
case 0x0cn:
|
|
325
|
-
case 0x0dn:
|
|
326
|
-
case 0x0en:
|
|
327
|
-
case 0x0fn: {
|
|
328
|
-
const typeByte = Number(type);
|
|
329
|
-
const streamId = await readVarint();
|
|
330
|
-
const offset = typeByte & STREAM_OFF_BIT ? await readVarint() : 0n;
|
|
331
|
-
const length = typeByte & STREAM_LEN_BIT ? await readVarint() : await readVarint();
|
|
332
|
-
const data = await readBytes(length);
|
|
333
|
-
const fin = (typeByte & STREAM_FIN_BIT) !== 0;
|
|
334
|
-
return { type: QuicFrameType.STREAM, streamId, offset, data, fin };
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
case BigInt(QuicFrameType.MAX_DATA):
|
|
338
|
-
return { type: QuicFrameType.MAX_DATA, maximum: await readVarint() };
|
|
339
|
-
|
|
340
|
-
case BigInt(QuicFrameType.MAX_STREAM_DATA): {
|
|
341
|
-
const streamId = await readVarint();
|
|
342
|
-
const maximum = await readVarint();
|
|
343
|
-
return { type: QuicFrameType.MAX_STREAM_DATA, streamId, maximum };
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
case BigInt(QuicFrameType.MAX_STREAMS_BIDI):
|
|
347
|
-
return { type: QuicFrameType.MAX_STREAMS_BIDI, maximum: await readVarint() };
|
|
348
|
-
|
|
349
|
-
case BigInt(QuicFrameType.MAX_STREAMS_UNI):
|
|
350
|
-
return { type: QuicFrameType.MAX_STREAMS_UNI, maximum: await readVarint() };
|
|
351
|
-
|
|
352
|
-
case BigInt(QuicFrameType.DATA_BLOCKED):
|
|
353
|
-
return { type: QuicFrameType.DATA_BLOCKED, limit: await readVarint() };
|
|
354
|
-
|
|
355
|
-
case BigInt(QuicFrameType.STREAM_DATA_BLOCKED): {
|
|
356
|
-
const streamId = await readVarint();
|
|
357
|
-
const limit = await readVarint();
|
|
358
|
-
return { type: QuicFrameType.STREAM_DATA_BLOCKED, streamId, limit };
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
case BigInt(QuicFrameType.STREAMS_BLOCKED_BIDI):
|
|
362
|
-
return { type: QuicFrameType.STREAMS_BLOCKED_BIDI, limit: await readVarint() };
|
|
363
|
-
|
|
364
|
-
case BigInt(QuicFrameType.STREAMS_BLOCKED_UNI):
|
|
365
|
-
return { type: QuicFrameType.STREAMS_BLOCKED_UNI, limit: await readVarint() };
|
|
366
|
-
|
|
367
|
-
case BigInt(QuicFrameType.NEW_CONNECTION_ID): {
|
|
368
|
-
const sequenceNumber = await readVarint();
|
|
369
|
-
const retirePriorTo = await readVarint();
|
|
370
|
-
const length = await readVarint();
|
|
371
|
-
const connectionId = await readBytes(length);
|
|
372
|
-
const statelessResetToken = await readBytes(16n);
|
|
373
|
-
return {
|
|
374
|
-
type: QuicFrameType.NEW_CONNECTION_ID,
|
|
375
|
-
sequenceNumber,
|
|
376
|
-
retirePriorTo,
|
|
377
|
-
connectionId,
|
|
378
|
-
statelessResetToken,
|
|
379
|
-
};
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
case BigInt(QuicFrameType.RETIRE_CONNECTION_ID):
|
|
383
|
-
return {
|
|
384
|
-
type: QuicFrameType.RETIRE_CONNECTION_ID,
|
|
385
|
-
sequenceNumber: await readVarint(),
|
|
386
|
-
};
|
|
387
|
-
|
|
388
|
-
case BigInt(QuicFrameType.PATH_CHALLENGE):
|
|
389
|
-
return { type: QuicFrameType.PATH_CHALLENGE, data: await readBytes(8n) };
|
|
390
|
-
|
|
391
|
-
case BigInt(QuicFrameType.PATH_RESPONSE):
|
|
392
|
-
return { type: QuicFrameType.PATH_RESPONSE, data: await readBytes(8n) };
|
|
393
|
-
|
|
394
|
-
case BigInt(QuicFrameType.CONNECTION_CLOSE):
|
|
395
|
-
case BigInt(QuicFrameType.CONNECTION_CLOSE_APP): {
|
|
396
|
-
const errorCode = await readVarint();
|
|
397
|
-
// Frame type field: present only if there are more bytes. We read a
|
|
398
|
-
// varint frame-type then a length-prefixed reason; if the remaining
|
|
399
|
-
// data doesn't parse cleanly, fall back.
|
|
400
|
-
const frameType = await readVarint();
|
|
401
|
-
const reasonLength = await readVarint();
|
|
402
|
-
const reasonBytes = await readBytes(reasonLength);
|
|
403
|
-
const reason = new TextDecoder().decode(reasonBytes);
|
|
404
|
-
return {
|
|
405
|
-
type: type === BigInt(QuicFrameType.CONNECTION_CLOSE)
|
|
406
|
-
? QuicFrameType.CONNECTION_CLOSE
|
|
407
|
-
: QuicFrameType.CONNECTION_CLOSE_APP,
|
|
408
|
-
errorCode,
|
|
409
|
-
frameType,
|
|
410
|
-
reason,
|
|
411
|
-
};
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
case BigInt(QuicFrameType.HANDSHAKE_DONE):
|
|
415
|
-
return { type: QuicFrameType.HANDSHAKE_DONE };
|
|
416
|
-
|
|
417
|
-
default:
|
|
418
|
-
// Unknown frame types: per RFC 9000, frames of unknown type in
|
|
419
|
-
// non-1-RTT packets are a connection error; in 1-RTT packets they
|
|
420
|
-
// MAY be ignored. We surface a generic frame so callers can decide.
|
|
421
|
-
return { type: QuicFrameType.PADDING };
|
|
422
|
-
}
|
|
423
|
-
}
|
package/src/frame/varint.ts
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* QUIC variable-length integer encoding (RFC 9000 §16).
|
|
3
|
-
*
|
|
4
|
-
* Two-bit prefix selects the length: 1, 2, 4, or 8 bytes. Used for stream ids,
|
|
5
|
-
* frame types, frame lengths, packet numbers, transport-parameter ids/values,
|
|
6
|
-
* and push ids throughout QUIC (and HTTP/3).
|
|
7
|
-
*
|
|
8
|
-
* Pure wire-format logic — no I/O.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { assertNever } from "../utils.js";
|
|
12
|
-
|
|
13
|
-
/** Maximum value encodable in a QUIC varint (2^62 - 1). */
|
|
14
|
-
export const VARINT_MAX = (1n << 62n) - 1n;
|
|
15
|
-
|
|
16
|
-
/** The length prefix mask in the first byte's top two bits, per encoded length. */
|
|
17
|
-
function prefixMask(length: 1 | 2 | 4 | 8): number {
|
|
18
|
-
switch (length) {
|
|
19
|
-
case 1:
|
|
20
|
-
return 0x00; // 00
|
|
21
|
-
case 2:
|
|
22
|
-
return 0x40; // 01
|
|
23
|
-
case 4:
|
|
24
|
-
return 0x80; // 10
|
|
25
|
-
case 8:
|
|
26
|
-
return 0xc0; // 11
|
|
27
|
-
default:
|
|
28
|
-
return assertNever(length);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** Return the number of bytes needed to encode `value` as a varint. */
|
|
33
|
-
export function getVarintEncodedLength(value: bigint): 1 | 2 | 4 | 8 {
|
|
34
|
-
if (value < 0n) throw new RangeError(`varint cannot be negative: ${value}`);
|
|
35
|
-
if (value > VARINT_MAX) throw new RangeError(`varint overflow: ${value}`);
|
|
36
|
-
if (value < (1n << 6n)) return 1;
|
|
37
|
-
if (value < (1n << 14n)) return 2;
|
|
38
|
-
if (value < (1n << 30n)) return 4;
|
|
39
|
-
return 8;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** Encode a varint to its wire representation. */
|
|
43
|
-
export function encodeVarint(value: bigint): Uint8Array {
|
|
44
|
-
const length = getVarintEncodedLength(value);
|
|
45
|
-
const out = new Uint8Array(length);
|
|
46
|
-
switch (length) {
|
|
47
|
-
case 1:
|
|
48
|
-
out[0] = Number(value);
|
|
49
|
-
break;
|
|
50
|
-
case 2:
|
|
51
|
-
out[0] = prefixMask(2) | Number((value >> 8n) & 0xffn);
|
|
52
|
-
out[1] = Number(value & 0xffn);
|
|
53
|
-
break;
|
|
54
|
-
case 4:
|
|
55
|
-
out[0] = prefixMask(4) | Number((value >> 24n) & 0xffn);
|
|
56
|
-
out[1] = Number((value >> 16n) & 0xffn);
|
|
57
|
-
out[2] = Number((value >> 8n) & 0xffn);
|
|
58
|
-
out[3] = Number(value & 0xffn);
|
|
59
|
-
break;
|
|
60
|
-
case 8: {
|
|
61
|
-
// Byte 0: 2-bit prefix (11) + top 6 value bits; bytes 1-7: the rest.
|
|
62
|
-
out[0] = prefixMask(8) | Number((value >> 56n) & 0x3fn);
|
|
63
|
-
out[1] = Number((value >> 48n) & 0xffn);
|
|
64
|
-
out[2] = Number((value >> 40n) & 0xffn);
|
|
65
|
-
out[3] = Number((value >> 32n) & 0xffn);
|
|
66
|
-
out[4] = Number((value >> 24n) & 0xffn);
|
|
67
|
-
out[5] = Number((value >> 16n) & 0xffn);
|
|
68
|
-
out[6] = Number((value >> 8n) & 0xffn);
|
|
69
|
-
out[7] = Number(value & 0xffn);
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
72
|
-
default:
|
|
73
|
-
assertNever(length);
|
|
74
|
-
}
|
|
75
|
-
return out;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Decode a varint from the start of `buf`. Returns the value and the number of
|
|
80
|
-
* bytes consumed. Throws RangeError if the buffer is too short.
|
|
81
|
-
*/
|
|
82
|
-
export function decodeVarint(buf: Uint8Array, offset = 0): { readonly value: bigint; readonly length: number } {
|
|
83
|
-
if (offset >= buf.length) {
|
|
84
|
-
throw new RangeError(`Buffer too short for varint at offset ${offset}`);
|
|
85
|
-
}
|
|
86
|
-
const first = buf[offset]!;
|
|
87
|
-
const length = 1 << ((first >> 6) & 0x03); // 1,2,4,8
|
|
88
|
-
if (buf.length < offset + length) {
|
|
89
|
-
throw new RangeError(`Buffer too short for ${length}-byte varint at offset ${offset}`);
|
|
90
|
-
}
|
|
91
|
-
// Clear the top 2 prefix bits to recover the first 6 payload bits.
|
|
92
|
-
let value = BigInt(first & 0x3f);
|
|
93
|
-
for (let i = 1; i < length; i++) {
|
|
94
|
-
value = (value << 8n) | BigInt(buf[offset + i]!);
|
|
95
|
-
}
|
|
96
|
-
return { value, length };
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Read a varint from an async byte source, pulling exactly as many bytes as
|
|
101
|
-
* needed. Used by frame readers that consume a stream incrementally.
|
|
102
|
-
*/
|
|
103
|
-
export async function readVarint(read: () => Promise<Uint8Array>): Promise<{
|
|
104
|
-
readonly value: bigint;
|
|
105
|
-
readonly bytes: Uint8Array;
|
|
106
|
-
}> {
|
|
107
|
-
// Read the first byte to learn the length.
|
|
108
|
-
const firstChunk = await read();
|
|
109
|
-
const first = firstChunk[0]!;
|
|
110
|
-
const length = 1 << ((first >> 6) & 0x03);
|
|
111
|
-
// Collect any remaining bytes of the varint.
|
|
112
|
-
const collected: Uint8Array[] = [firstChunk];
|
|
113
|
-
let have = firstChunk.length;
|
|
114
|
-
while (have < length) {
|
|
115
|
-
const chunk = await read();
|
|
116
|
-
collected.push(chunk);
|
|
117
|
-
have += chunk.length;
|
|
118
|
-
}
|
|
119
|
-
const all = concatAll(collected);
|
|
120
|
-
const { value } = decodeVarint(all);
|
|
121
|
-
return { value, bytes: all };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
import { concatAll } from "../utils.js";
|
package/src/index.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @browsercore/quic — public API surface.
|
|
3
|
-
*
|
|
4
|
-
* QUIC transport (RFC 9000) over a datagram (UDP) transport. No knowledge of
|
|
5
|
-
* HTTP/3, TLS handshake semantics, or sockets — it composes exclusively over an
|
|
6
|
-
* injected {@link DatagramTransport} and `@browsercore/crypto`. Higher layers
|
|
7
|
-
* (http3) compose through {@link QuicConnection}.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
export { connectQuic, QuicConnectionImpl } from "./connection.js";
|
|
11
|
-
|
|
12
|
-
export {
|
|
13
|
-
ConnectionClosedError,
|
|
14
|
-
FlowControlError,
|
|
15
|
-
FrameParseError,
|
|
16
|
-
HandshakeTimeoutError,
|
|
17
|
-
PacketParseError,
|
|
18
|
-
QuicError,
|
|
19
|
-
ResetStreamError,
|
|
20
|
-
StopSendingError,
|
|
21
|
-
TransportParameterError,
|
|
22
|
-
} from "./errors.js";
|
|
23
|
-
|
|
24
|
-
export {
|
|
25
|
-
LongPacketType,
|
|
26
|
-
QuicFrameType,
|
|
27
|
-
TransportParameter,
|
|
28
|
-
type LongPacketTypeValue,
|
|
29
|
-
type QuicFrame,
|
|
30
|
-
type QuicFrameTypeValue,
|
|
31
|
-
} from "./types.js";
|
|
32
|
-
|
|
33
|
-
export {
|
|
34
|
-
EMPTY_CONNECTION_ID,
|
|
35
|
-
HEADER_FORM_LONG,
|
|
36
|
-
HEADER_FORM_SHORT,
|
|
37
|
-
MIN_MAX_UDP_PAYLOAD_SIZE,
|
|
38
|
-
STREAM_FIN_BIT,
|
|
39
|
-
STREAM_LEN_BIT,
|
|
40
|
-
STREAM_OFF_BIT,
|
|
41
|
-
firstStreamId,
|
|
42
|
-
makeStreamId,
|
|
43
|
-
nextStreamId,
|
|
44
|
-
streamIdIsBidirectional,
|
|
45
|
-
streamIdIsClientInitiated,
|
|
46
|
-
} from "./types.js";
|
|
47
|
-
|
|
48
|
-
export { VARINT_MAX } from "./frame/varint.js";
|
|
49
|
-
|
|
50
|
-
export {
|
|
51
|
-
type BaseQuicFrame,
|
|
52
|
-
type ConnectionId,
|
|
53
|
-
type DatagramCloseReason,
|
|
54
|
-
type DatagramTransport,
|
|
55
|
-
type QuicConnection,
|
|
56
|
-
type QuicOptions,
|
|
57
|
-
type QuicStream,
|
|
58
|
-
type QuicTransportParameters,
|
|
59
|
-
type StreamId,
|
|
60
|
-
type StreamState,
|
|
61
|
-
type StreamCloseReason,
|
|
62
|
-
type UdpAddress,
|
|
63
|
-
} from "./types.js";
|
|
64
|
-
|
|
65
|
-
export { decodeVarint, encodeVarint, getVarintEncodedLength } from "./frame/varint.js";
|
|
66
|
-
export { decodeFrame, readFrames, serializeFrame } from "./frame/frame.js";
|
|
67
|
-
export { createStreamManager } from "./stream/stream.js";
|
|
68
|
-
export type { StreamManager } from "./stream/stream.js";
|
|
69
|
-
|
|
70
|
-
export { assertNever, concat, concatAll, hex } from "./utils.js";
|