@browsercore/quic 0.1.3
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/.github/workflows/ci.yml +17 -0
- package/.github/workflows/publish.yml +49 -0
- package/CODING_STANDARDS.md +205 -0
- package/LICENSE +21 -0
- package/dist/connection.d.ts +104 -0
- package/dist/connection.d.ts.map +1 -0
- package/dist/connection.js +334 -0
- package/dist/connection.js.map +1 -0
- package/dist/errors.d.ts +98 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +130 -0
- package/dist/errors.js.map +1 -0
- package/dist/frame/frame.d.ts +22 -0
- package/dist/frame/frame.d.ts.map +1 -0
- package/dist/frame/frame.js +365 -0
- package/dist/frame/frame.js.map +1 -0
- package/dist/frame/varint.d.ts +32 -0
- package/dist/frame/varint.d.ts.map +1 -0
- package/dist/frame/varint.js +119 -0
- package/dist/frame/varint.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/packet/packet.d.ts +71 -0
- package/dist/packet/packet.d.ts.map +1 -0
- package/dist/packet/packet.js +150 -0
- package/dist/packet/packet.js.map +1 -0
- package/dist/stream/stream.d.ts +66 -0
- package/dist/stream/stream.d.ts.map +1 -0
- package/dist/stream/stream.js +617 -0
- package/dist/stream/stream.js.map +1 -0
- package/dist/types.d.ts +344 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +127 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +16 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +36 -0
- package/dist/utils.js.map +1 -0
- package/package.json +43 -0
- package/src/connection.ts +374 -0
- package/src/errors.ts +153 -0
- package/src/frame/frame.ts +423 -0
- package/src/frame/varint.ts +124 -0
- package/src/index.ts +70 -0
- package/src/packet/packet.ts +210 -0
- package/src/stream/stream.ts +746 -0
- package/src/types.ts +441 -0
- package/src/utils.ts +38 -0
- package/tests/connection.test.ts +106 -0
- package/tests/fake-transport.ts +107 -0
- package/tests/stream.test.ts +152 -0
- package/tests/wire.test.ts +342 -0
- package/tsconfig.json +30 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QUIC packet header parser and serializer (RFC 9000 §17).
|
|
3
|
+
*
|
|
4
|
+
* Pure wire-format logic — no I/O, no encryption. Two header forms:
|
|
5
|
+
*
|
|
6
|
+
* Long header (first bit = 1): used during the handshake.
|
|
7
|
+
* Header form (1) | Fixed bit (1) | Long packet type (2) | Type-specific (2)
|
|
8
|
+
* | Version (32) | DCID len (8) | DCID (0..255) | SCID len (8) | SCID (0..255)
|
|
9
|
+
* [+ type-specific fields: Token length/token (Initial), Packet number length, Payload]
|
|
10
|
+
*
|
|
11
|
+
* Short header (first bit = 0): 1-RTT data after the handshake.
|
|
12
|
+
* Header form (1) | Fixed bit (1) | Spin bit (1) | Reserved (2) | Key phase (1)
|
|
13
|
+
* | Packet number length (2) | DCID | Packet number (8/16/24/32 bits) | Protected payload
|
|
14
|
+
*
|
|
15
|
+
* The "packet number length" in both forms occupies the low 2 bits of the
|
|
16
|
+
* first byte (long) or the byte after the DCID (short) and tells the caller how
|
|
17
|
+
* many bytes of packet number follow.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type { ConnectionId, LongPacketTypeValue } from "../types.js";
|
|
21
|
+
import {
|
|
22
|
+
HEADER_FORM_LONG,
|
|
23
|
+
HEADER_FORM_SHORT,
|
|
24
|
+
LONG_PACKET_TYPE_MASK,
|
|
25
|
+
} from "../types.js";
|
|
26
|
+
import { decodeVarint, encodeVarint } from "../frame/varint.js";
|
|
27
|
+
import { concatAll } from "../utils.js";
|
|
28
|
+
|
|
29
|
+
export {
|
|
30
|
+
LongPacketType,
|
|
31
|
+
type LongPacketTypeValue,
|
|
32
|
+
} from "../types.js";
|
|
33
|
+
export type { ConnectionId } from "../types.js";
|
|
34
|
+
|
|
35
|
+
/** Parsed long-header fields (pre-decryption). */
|
|
36
|
+
export interface LongHeader {
|
|
37
|
+
readonly form: typeof HEADER_FORM_LONG;
|
|
38
|
+
readonly type: LongPacketTypeValue;
|
|
39
|
+
readonly version: number;
|
|
40
|
+
readonly dcid: ConnectionId;
|
|
41
|
+
readonly scid: ConnectionId;
|
|
42
|
+
/** Number of bytes the packet number occupies (1–4). */
|
|
43
|
+
readonly packetNumberLength: number;
|
|
44
|
+
/** Raw bytes consumed by the header (everything before the payload). */
|
|
45
|
+
readonly headerLength: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Parsed short-header (1-RTT) fields (pre-decryption). */
|
|
49
|
+
export interface ShortHeader {
|
|
50
|
+
readonly form: typeof HEADER_FORM_SHORT;
|
|
51
|
+
readonly spinBit: boolean;
|
|
52
|
+
readonly keyPhase: boolean;
|
|
53
|
+
readonly dcid: ConnectionId;
|
|
54
|
+
/** Number of bytes the packet number occupies (1–4). */
|
|
55
|
+
readonly packetNumberLength: number;
|
|
56
|
+
readonly headerLength: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type PacketHeader = LongHeader | ShortHeader;
|
|
60
|
+
|
|
61
|
+
/** Serialize a long header (without payload / packet number — caller appends those). */
|
|
62
|
+
export function serializeLongHeader(
|
|
63
|
+
type: LongPacketTypeValue,
|
|
64
|
+
version: number,
|
|
65
|
+
dcid: ConnectionId,
|
|
66
|
+
scid: ConnectionId,
|
|
67
|
+
packetNumberLength: number,
|
|
68
|
+
extra: Uint8Array = new Uint8Array(0),
|
|
69
|
+
): Uint8Array {
|
|
70
|
+
const first = (HEADER_FORM_LONG << 7) | (1 << 6) | ((type & LONG_PACKET_TYPE_MASK) << 4) | (packetNumberLength - 1);
|
|
71
|
+
const versionBytes = new Uint8Array(4);
|
|
72
|
+
new DataView(versionBytes.buffer).setUint32(0, version);
|
|
73
|
+
return concatAll([
|
|
74
|
+
new Uint8Array([first]),
|
|
75
|
+
versionBytes,
|
|
76
|
+
new Uint8Array([dcid.length]),
|
|
77
|
+
dcid,
|
|
78
|
+
new Uint8Array([scid.length]),
|
|
79
|
+
scid,
|
|
80
|
+
extra,
|
|
81
|
+
]);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Serialize a short (1-RTT) header (without payload / packet number). */
|
|
85
|
+
export function serializeShortHeader(
|
|
86
|
+
dcid: ConnectionId,
|
|
87
|
+
packetNumberLength: number,
|
|
88
|
+
spinBit: boolean,
|
|
89
|
+
keyPhase: boolean,
|
|
90
|
+
): Uint8Array {
|
|
91
|
+
let first = HEADER_FORM_SHORT << 7;
|
|
92
|
+
if (spinBit) first |= 1 << 5;
|
|
93
|
+
if (keyPhase) first |= 1 << 2;
|
|
94
|
+
first |= packetNumberLength - 1;
|
|
95
|
+
return concatAll([new Uint8Array([first]), dcid]);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Parse a packet header from the start of a buffer. Returns the header and the
|
|
100
|
+
* number of header bytes consumed. Throws PacketParseError on malformed input.
|
|
101
|
+
*
|
|
102
|
+
* Note: this parses the *unprotected* header. In practice the packet number and
|
|
103
|
+
* payload are encrypted; the caller strips header protection before parsing the
|
|
104
|
+
* full packet number. This function parses everything up to (not including) the
|
|
105
|
+
* packet number.
|
|
106
|
+
*/
|
|
107
|
+
export function parsePacketHeader(buf: Uint8Array): PacketHeader {
|
|
108
|
+
if (buf.length < 1) {
|
|
109
|
+
throw new RangeError("Buffer too short for packet header");
|
|
110
|
+
}
|
|
111
|
+
const first = buf[0]!;
|
|
112
|
+
const form = (first >> 7) & 0x01;
|
|
113
|
+
|
|
114
|
+
if (form === HEADER_FORM_LONG) {
|
|
115
|
+
return parseLongHeader(first, buf);
|
|
116
|
+
}
|
|
117
|
+
return parseShortHeader(first, buf);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function parseLongHeader(first: number, buf: Uint8Array): LongHeader {
|
|
121
|
+
const type = (first >> 4) & LONG_PACKET_TYPE_MASK;
|
|
122
|
+
const packetNumberLength = (first & 0x03) + 1;
|
|
123
|
+
// Minimum long header: 1 (first) + 4 (version) + 1 (dcid len) + 0 + 1 (scid len) + 0 = 7.
|
|
124
|
+
if (buf.length < 7) {
|
|
125
|
+
throw new RangeError("Buffer too short for long header");
|
|
126
|
+
}
|
|
127
|
+
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
128
|
+
const version = view.getUint32(1);
|
|
129
|
+
const dcidLen = buf[5]!;
|
|
130
|
+
if (buf.length < 6 + dcidLen + 1) {
|
|
131
|
+
throw new RangeError("Buffer too short for DCID");
|
|
132
|
+
}
|
|
133
|
+
const dcid = buf.subarray(6, 6 + dcidLen);
|
|
134
|
+
const scidLen = buf[6 + dcidLen]!;
|
|
135
|
+
if (buf.length < 6 + dcidLen + 1 + scidLen) {
|
|
136
|
+
throw new RangeError("Buffer too short for SCID");
|
|
137
|
+
}
|
|
138
|
+
const scid = buf.subarray(6 + dcidLen + 1, 6 + dcidLen + 1 + scidLen);
|
|
139
|
+
const headerLength = 6 + dcidLen + 1 + scidLen;
|
|
140
|
+
return {
|
|
141
|
+
form: HEADER_FORM_LONG,
|
|
142
|
+
type: type as LongPacketTypeValue,
|
|
143
|
+
version,
|
|
144
|
+
dcid,
|
|
145
|
+
scid,
|
|
146
|
+
packetNumberLength,
|
|
147
|
+
headerLength,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function parseShortHeader(first: number, _buf: Uint8Array): ShortHeader {
|
|
152
|
+
const spinBit = ((first >> 5) & 0x01) === 1;
|
|
153
|
+
const keyPhase = ((first >> 2) & 0x01) === 1;
|
|
154
|
+
const packetNumberLength = (first & 0x03) + 1;
|
|
155
|
+
// DCID length is variable and not on the wire for short headers — the
|
|
156
|
+
// caller must know it (from the handshake). We record header length as just
|
|
157
|
+
// the first byte; the DCID is decoded by the connection layer.
|
|
158
|
+
return {
|
|
159
|
+
form: HEADER_FORM_SHORT,
|
|
160
|
+
spinBit,
|
|
161
|
+
keyPhase,
|
|
162
|
+
dcid: new Uint8Array(0),
|
|
163
|
+
packetNumberLength,
|
|
164
|
+
headerLength: 1,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Decode a truncated packet number to its full value given the expected next
|
|
170
|
+
* packet number (RFC 9000 §17.1). The peer sends the least-significant
|
|
171
|
+
* `encodedBits` bits; we pick the candidate within ±2^(n-1) of `largestPn`
|
|
172
|
+
* that matches those bits.
|
|
173
|
+
*/
|
|
174
|
+
export function decodePacketNumber(
|
|
175
|
+
largestPn: bigint,
|
|
176
|
+
truncated: bigint,
|
|
177
|
+
encodedBits: number,
|
|
178
|
+
): bigint {
|
|
179
|
+
const pnNbits = BigInt(encodedBits);
|
|
180
|
+
const expectedPn = largestPn + 1n;
|
|
181
|
+
const pnWin = 1n << pnNbits;
|
|
182
|
+
const pnHwin = pnWin / 2n;
|
|
183
|
+
const pnMask = pnWin - 1n;
|
|
184
|
+
const candidate = (expectedPn & ~pnMask) | truncated;
|
|
185
|
+
if (candidate <= expectedPn - pnHwin && candidate < (1n << 62n) - pnWin) {
|
|
186
|
+
return candidate + pnWin;
|
|
187
|
+
}
|
|
188
|
+
if (candidate > expectedPn + pnHwin && candidate >= pnWin) {
|
|
189
|
+
return candidate - pnWin;
|
|
190
|
+
}
|
|
191
|
+
return candidate;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Encode a packet number to its truncated wire form (low `encodedBits` bits). */
|
|
195
|
+
export function encodePacketNumber(pn: bigint, encodedBits: number): bigint {
|
|
196
|
+
const mask = (1n << BigInt(encodedBits)) - 1n;
|
|
197
|
+
return pn & mask;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Read a packet number of the given byte length from a buffer at offset. */
|
|
201
|
+
export function readPacketNumber(buf: Uint8Array, offset: number, length: number): bigint {
|
|
202
|
+
let value = 0n;
|
|
203
|
+
for (let i = 0; i < length; i++) {
|
|
204
|
+
value = (value << 8n) | BigInt(buf[offset + i]!);
|
|
205
|
+
}
|
|
206
|
+
return value;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
void decodeVarint;
|
|
210
|
+
void encodeVarint;
|