@ac-kit/format-varint 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/README.md +8 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/quic/encode-quic-varint.d.ts +19 -0
- package/dist/quic/encode-quic-varint.d.ts.map +1 -0
- package/dist/quic/encode-quic-varint.js +25 -0
- package/dist/quic/encode-quic-varint.js.map +1 -0
- package/dist/quic/quic-varint-byte-length.d.ts +22 -0
- package/dist/quic/quic-varint-byte-length.d.ts.map +1 -0
- package/dist/quic/quic-varint-byte-length.js +48 -0
- package/dist/quic/quic-varint-byte-length.js.map +1 -0
- package/dist/quic/read-big-quic-varint.d.ts +20 -0
- package/dist/quic/read-big-quic-varint.d.ts.map +1 -0
- package/dist/quic/read-big-quic-varint.js +40 -0
- package/dist/quic/read-big-quic-varint.js.map +1 -0
- package/dist/quic/read-quic-varint.d.ts +20 -0
- package/dist/quic/read-quic-varint.d.ts.map +1 -0
- package/dist/quic/read-quic-varint.js +37 -0
- package/dist/quic/read-quic-varint.js.map +1 -0
- package/dist/quic/write-quic-varint.d.ts +16 -0
- package/dist/quic/write-quic-varint.d.ts.map +1 -0
- package/dist/quic/write-quic-varint.js +51 -0
- package/dist/quic/write-quic-varint.js.map +1 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
# @ac-kit/format-varint
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 5075202: HTTP/3, over a multiplexed connection contract.
|
|
8
|
+
|
|
9
|
+
- `net-core`: `IMultiplexedConnection` and `IMultiplexedStream` — a secure
|
|
10
|
+
connection carrying many streams, each a `Transport` that either side can
|
|
11
|
+
reset or stop on its own — with `MultiplexedStreamHandlers` and the errors a
|
|
12
|
+
reset, a stop or the connection's end report. `Connector` is now generic over
|
|
13
|
+
what it establishes, like `IListener`.
|
|
14
|
+
- `async`: `staggeredRace`, RFC 8305 connection racing for any candidates.
|
|
15
|
+
- `format-varint`: QUIC variable-length integers (RFC 9000 §16) —
|
|
16
|
+
`readQuicVarint`, `readBigQuicVarint` (the whole 62-bit range, as a
|
|
17
|
+
`bigint`), `writeQuicVarint`, `encodeQuicVarint`, `quicVarintByteLength` and
|
|
18
|
+
`quicVarintEncodedLength`.
|
|
19
|
+
- `format-http`: `QpackDecoder` (RFC 9204, dynamic table and blocked streams)
|
|
20
|
+
and `QpackEncoder` (static table and literals); `Http3FrameDecoder` and
|
|
21
|
+
`encodeHttp3Frame` for HTTP/3 frames (RFC 9114), with DATA streamed in
|
|
22
|
+
pieces; `Http3Settings`, `Http3StreamType`, `Http3Error` and
|
|
23
|
+
`Http3ErrorCode`. `parseAltSvc` and `tryParseAltSvc` read the `Alt-Svc`
|
|
24
|
+
field (RFC 7838), `AltSvcSyntaxError` reporting a malformed one.
|
|
25
|
+
- `net-http`: `Http3ServerConnection` and `Http3ClientConnection`, with
|
|
26
|
+
trailers, GOAWAY and extended `CONNECT` tunnels (RFC 9220). `HttpServer`
|
|
27
|
+
serves HTTP/3 on multiplexed listeners with its `http3` option; `HttpAgent`
|
|
28
|
+
reaches `https:` origins over HTTP/3 with its own: those advertising it
|
|
29
|
+
through `Alt-Svc`, raced against TCP and falling back to it, or every one
|
|
30
|
+
with `priorKnowledge`.
|
|
31
|
+
- `net-quic`: first release. `QuicListener` and `createQuicConnector` over a
|
|
32
|
+
native quiche binding; the binding of each platform is its own optional
|
|
33
|
+
package, `@ac-kit/net-quic-<platform>`. A client's UDP socket is connected,
|
|
34
|
+
so a refused port fails the attempt at once.
|
|
35
|
+
- `node`: `DgramSocket.connect`, `disconnect`, `remoteAddress` and
|
|
36
|
+
`sendConnected`, for a UDP socket bound to one remote endpoint.
|
|
37
|
+
- `integration-test-util`: `createSelfSignedCertificate` takes the names the
|
|
38
|
+
certificate is valid for.
|
|
39
|
+
- `net-websocket`: documented over HTTP/3, which needed no change.
|
|
40
|
+
|
|
3
41
|
## 0.1.4
|
|
4
42
|
|
|
5
43
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -23,6 +23,13 @@ interchangeable: LEB128 puts the _least_ significant group first, and source
|
|
|
23
23
|
maps use a base-64 alphabet with the sign in the low bit. Every export here
|
|
24
24
|
names its member, so there is no ambiguous `encodeVarint`.
|
|
25
25
|
|
|
26
|
+
**QUIC** variable-length integers (RFC 9000 §16) have no continuation bit: the
|
|
27
|
+
two high bits of the first byte give the length, 1, 2, 4 or 8 bytes, and the
|
|
28
|
+
value follows big-endian. QUIC, HTTP/3 and HTTP datagram capsules use them.
|
|
29
|
+
`quicVarintEncodedLength(firstByte)` tells a streaming parser how many bytes to
|
|
30
|
+
wait for before calling `readQuicVarint`. A longer form than needed is accepted,
|
|
31
|
+
as QUIC allows it; values stop at `Number.MAX_SAFE_INTEGER` rather than 2^62 - 1.
|
|
32
|
+
|
|
26
33
|
## Reading one value, or reading a stream
|
|
27
34
|
|
|
28
35
|
Two shapes, because the two situations are genuinely different:
|
|
@@ -50,7 +57,7 @@ says so: it throws `RangeError` naming `readBigVlq`, and reserves
|
|
|
50
57
|
|
|
51
58
|
## Canonical form
|
|
52
59
|
|
|
53
|
-
An overlong encoding — a leading `0x80`, whose payload group is zero — is
|
|
60
|
+
An overlong VLQ encoding — a leading `0x80`, whose payload group is zero — is
|
|
54
61
|
rejected. X.690 §8.19.2 requires the fewest possible octets, and accepting the
|
|
55
62
|
padded form would let two different byte sequences mean the same number.
|
|
56
63
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
export * from "./errors.js";
|
|
2
|
+
export * from "./quic/encode-quic-varint.js";
|
|
3
|
+
export * from "./quic/quic-varint-byte-length.js";
|
|
4
|
+
export * from "./quic/read-big-quic-varint.js";
|
|
5
|
+
export * from "./quic/read-quic-varint.js";
|
|
6
|
+
export * from "./quic/write-quic-varint.js";
|
|
2
7
|
export * from "./vlq/big-vlq-decoder.js";
|
|
3
8
|
export * from "./vlq/big-vlq-encoder.js";
|
|
4
9
|
export * from "./vlq/decode-big-vlq.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,mCAAmC,CAAC;AAClD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
export * from "./errors.js";
|
|
2
|
+
export * from "./quic/encode-quic-varint.js";
|
|
3
|
+
export * from "./quic/quic-varint-byte-length.js";
|
|
4
|
+
export * from "./quic/read-big-quic-varint.js";
|
|
5
|
+
export * from "./quic/read-quic-varint.js";
|
|
6
|
+
export * from "./quic/write-quic-varint.js";
|
|
2
7
|
export * from "./vlq/big-vlq-decoder.js";
|
|
3
8
|
export * from "./vlq/big-vlq-encoder.js";
|
|
4
9
|
export * from "./vlq/decode-big-vlq.js";
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,mCAAmC,CAAC;AAClD,cAAc,gCAAgC,CAAC;AAC/C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC;AACxC,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encodes a non-negative integer as a QUIC variable-length integer (RFC 9000
|
|
3
|
+
* §16): a two-bit length prefix in the first byte, then the value big-endian in
|
|
4
|
+
* 1, 2, 4 or 8 bytes. Always the shortest form.
|
|
5
|
+
*
|
|
6
|
+
* Used throughout QUIC, HTTP/3 (RFC 9114) and HTTP datagrams and capsules (RFC
|
|
7
|
+
* 9297). Unlike the continuation-bit members of this package, the length is
|
|
8
|
+
* known from the first byte.
|
|
9
|
+
*
|
|
10
|
+
* Time complexity: O(1).
|
|
11
|
+
*
|
|
12
|
+
* @param value - A non-negative safe integer. QUIC allows up to 2^62 - 1; this
|
|
13
|
+
* function stops at `Number.MAX_SAFE_INTEGER`.
|
|
14
|
+
* @returns A fresh buffer of 1, 2, 4 or 8 bytes.
|
|
15
|
+
* @throws {RangeError} When `value` is negative, fractional, or beyond
|
|
16
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function encodeQuicVarint(value: number): Uint8Array<ArrayBuffer>;
|
|
19
|
+
//# sourceMappingURL=encode-quic-varint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode-quic-varint.d.ts","sourceRoot":"","sources":["../../src/quic/encode-quic-varint.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAAC,WAAW,CAAC,CAKvE"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { quicVarintByteLength } from "./quic-varint-byte-length.js";
|
|
2
|
+
import { writeQuicVarint } from "./write-quic-varint.js";
|
|
3
|
+
/**
|
|
4
|
+
* Encodes a non-negative integer as a QUIC variable-length integer (RFC 9000
|
|
5
|
+
* §16): a two-bit length prefix in the first byte, then the value big-endian in
|
|
6
|
+
* 1, 2, 4 or 8 bytes. Always the shortest form.
|
|
7
|
+
*
|
|
8
|
+
* Used throughout QUIC, HTTP/3 (RFC 9114) and HTTP datagrams and capsules (RFC
|
|
9
|
+
* 9297). Unlike the continuation-bit members of this package, the length is
|
|
10
|
+
* known from the first byte.
|
|
11
|
+
*
|
|
12
|
+
* Time complexity: O(1).
|
|
13
|
+
*
|
|
14
|
+
* @param value - A non-negative safe integer. QUIC allows up to 2^62 - 1; this
|
|
15
|
+
* function stops at `Number.MAX_SAFE_INTEGER`.
|
|
16
|
+
* @returns A fresh buffer of 1, 2, 4 or 8 bytes.
|
|
17
|
+
* @throws {RangeError} When `value` is negative, fractional, or beyond
|
|
18
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
19
|
+
*/
|
|
20
|
+
export function encodeQuicVarint(value) {
|
|
21
|
+
const out = new Uint8Array(quicVarintByteLength(value));
|
|
22
|
+
writeQuicVarint(out, 0, value);
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=encode-quic-varint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encode-quic-varint.js","sourceRoot":"","sources":["../../src/quic/encode-quic-varint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACpE,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC7C,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,oBAAoB,CAAC,KAAK,CAAC,CAAC,CAAC;IACxD,eAAe,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC;IAE/B,OAAO,GAAG,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How many bytes {@link encodeQuicVarint} will produce for `value`: the shortest
|
|
3
|
+
* of the four QUIC lengths that holds it.
|
|
4
|
+
*
|
|
5
|
+
* @param value - A non-negative safe integer.
|
|
6
|
+
* @returns 1, 2, 4 or 8.
|
|
7
|
+
* @throws {RangeError} When `value` is negative, fractional, or beyond
|
|
8
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function quicVarintByteLength(value: number): 1 | 2 | 4 | 8;
|
|
11
|
+
/**
|
|
12
|
+
* How many bytes the QUIC variable-length integer starting with `firstByte`
|
|
13
|
+
* takes, read from its two most significant bits.
|
|
14
|
+
*
|
|
15
|
+
* Lets a streaming parser check that a whole value is buffered before reading
|
|
16
|
+
* it, without a cursor or an exception.
|
|
17
|
+
*
|
|
18
|
+
* @param firstByte - The encoding's first byte, 0 to 255.
|
|
19
|
+
* @returns 1, 2, 4 or 8.
|
|
20
|
+
*/
|
|
21
|
+
export declare function quicVarintEncodedLength(firstByte: number): 1 | 2 | 4 | 8;
|
|
22
|
+
//# sourceMappingURL=quic-varint-byte-length.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quic-varint-byte-length.d.ts","sourceRoot":"","sources":["../../src/quic/quic-varint-byte-length.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAgBjE;AAED;;;;;;;;;GASG;AACH,wBAAgB,uBAAuB,CAAC,SAAS,EAAE,MAAM,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAYxE"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How many bytes {@link encodeQuicVarint} will produce for `value`: the shortest
|
|
3
|
+
* of the four QUIC lengths that holds it.
|
|
4
|
+
*
|
|
5
|
+
* @param value - A non-negative safe integer.
|
|
6
|
+
* @returns 1, 2, 4 or 8.
|
|
7
|
+
* @throws {RangeError} When `value` is negative, fractional, or beyond
|
|
8
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
9
|
+
*/
|
|
10
|
+
export function quicVarintByteLength(value) {
|
|
11
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
12
|
+
throw new RangeError(`quicVarintByteLength: value must be a non-negative safe integer, got ${value}`);
|
|
13
|
+
}
|
|
14
|
+
if (value < 0x40) {
|
|
15
|
+
return 1;
|
|
16
|
+
}
|
|
17
|
+
if (value < 0x4000) {
|
|
18
|
+
return 2;
|
|
19
|
+
}
|
|
20
|
+
if (value < 1073741824) {
|
|
21
|
+
return 4;
|
|
22
|
+
}
|
|
23
|
+
return 8;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* How many bytes the QUIC variable-length integer starting with `firstByte`
|
|
27
|
+
* takes, read from its two most significant bits.
|
|
28
|
+
*
|
|
29
|
+
* Lets a streaming parser check that a whole value is buffered before reading
|
|
30
|
+
* it, without a cursor or an exception.
|
|
31
|
+
*
|
|
32
|
+
* @param firstByte - The encoding's first byte, 0 to 255.
|
|
33
|
+
* @returns 1, 2, 4 or 8.
|
|
34
|
+
*/
|
|
35
|
+
export function quicVarintEncodedLength(firstByte) {
|
|
36
|
+
// The two-bit prefix is the base-2 logarithm of the length.
|
|
37
|
+
switch (firstByte >>> 6) {
|
|
38
|
+
case 0:
|
|
39
|
+
return 1;
|
|
40
|
+
case 1:
|
|
41
|
+
return 2;
|
|
42
|
+
case 2:
|
|
43
|
+
return 4;
|
|
44
|
+
default:
|
|
45
|
+
return 8;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=quic-varint-byte-length.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quic-varint-byte-length.js","sourceRoot":"","sources":["../../src/quic/quic-varint-byte-length.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAAa;IACjD,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QAC/C,MAAM,IAAI,UAAU,CACnB,wEAAwE,KAAK,EAAE,CAC/E,CAAC;IACH,CAAC;IACD,IAAI,KAAK,GAAG,IAAI,EAAE,CAAC;QAClB,OAAO,CAAC,CAAC;IACV,CAAC;IACD,IAAI,KAAK,GAAG,MAAM,EAAE,CAAC;QACpB,OAAO,CAAC,CAAC;IACV,CAAC;IACD,IAAI,KAAK,GAAG,UAAW,EAAE,CAAC;QACzB,OAAO,CAAC,CAAC;IACV,CAAC;IACD,OAAO,CAAC,CAAC;AACV,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,uBAAuB,CAAC,SAAiB;IACxD,4DAA4D;IAC5D,QAAQ,SAAS,KAAK,CAAC,EAAE,CAAC;QACzB,KAAK,CAAC;YACL,OAAO,CAAC,CAAC;QACV,KAAK,CAAC;YACL,OAAO,CAAC,CAAC;QACV,KAAK,CAAC;YACL,OAAO,CAAC,CAAC;QACV;YACC,OAAO,CAAC,CAAC;IACX,CAAC;AACF,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ByteReader } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Reads one QUIC variable-length integer from `reader` as a `bigint`, advancing
|
|
4
|
+
* it past the bytes consumed — the full-range counterpart of
|
|
5
|
+
* {@link readQuicVarint}, up to 2^62 - 1.
|
|
6
|
+
*
|
|
7
|
+
* QUIC and HTTP/3 grease values (`0x1f * N + 0x21`) reach that range, so a
|
|
8
|
+
* reader that must skip unknown identifiers rather than fail on them needs it.
|
|
9
|
+
* Prefer {@link readQuicVarint} where the value is known to fit a `number`;
|
|
10
|
+
* `bigint` arithmetic is slower and allocates.
|
|
11
|
+
*
|
|
12
|
+
* Time complexity: O(1).
|
|
13
|
+
*
|
|
14
|
+
* @param reader - Cursor positioned at the first byte of the encoding.
|
|
15
|
+
* @returns The decoded value, from 0 to 2^62 - 1.
|
|
16
|
+
* @throws {VarintIncompleteError} When fewer bytes remain than the length
|
|
17
|
+
* prefix announces.
|
|
18
|
+
*/
|
|
19
|
+
export declare function readBigQuicVarint(reader: ByteReader): bigint;
|
|
20
|
+
//# sourceMappingURL=read-big-quic-varint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-big-quic-varint.d.ts","sourceRoot":"","sources":["../../src/quic/read-big-quic-varint.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAK/C;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CA0B5D"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { VarintIncompleteError } from "../errors.js";
|
|
2
|
+
import { quicVarintEncodedLength } from "./quic-varint-byte-length.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads one QUIC variable-length integer from `reader` as a `bigint`, advancing
|
|
5
|
+
* it past the bytes consumed — the full-range counterpart of
|
|
6
|
+
* {@link readQuicVarint}, up to 2^62 - 1.
|
|
7
|
+
*
|
|
8
|
+
* QUIC and HTTP/3 grease values (`0x1f * N + 0x21`) reach that range, so a
|
|
9
|
+
* reader that must skip unknown identifiers rather than fail on them needs it.
|
|
10
|
+
* Prefer {@link readQuicVarint} where the value is known to fit a `number`;
|
|
11
|
+
* `bigint` arithmetic is slower and allocates.
|
|
12
|
+
*
|
|
13
|
+
* Time complexity: O(1).
|
|
14
|
+
*
|
|
15
|
+
* @param reader - Cursor positioned at the first byte of the encoding.
|
|
16
|
+
* @returns The decoded value, from 0 to 2^62 - 1.
|
|
17
|
+
* @throws {VarintIncompleteError} When fewer bytes remain than the length
|
|
18
|
+
* prefix announces.
|
|
19
|
+
*/
|
|
20
|
+
export function readBigQuicVarint(reader) {
|
|
21
|
+
if (reader.atEnd) {
|
|
22
|
+
throw new VarintIncompleteError("readBigQuicVarint: no bytes to read");
|
|
23
|
+
}
|
|
24
|
+
const length = quicVarintEncodedLength(reader.peekByte());
|
|
25
|
+
if (reader.remaining < length) {
|
|
26
|
+
throw new VarintIncompleteError(`readBigQuicVarint: ${length}-byte encoding, ${reader.remaining} bytes left`);
|
|
27
|
+
}
|
|
28
|
+
// Two 32-bit halves keep the arithmetic in `number` until the last step.
|
|
29
|
+
let high = 0;
|
|
30
|
+
let low = reader.readByte() & 0x3f;
|
|
31
|
+
for (let index = 1; index < length; index++) {
|
|
32
|
+
if (index === length - 4) {
|
|
33
|
+
high = low;
|
|
34
|
+
low = 0;
|
|
35
|
+
}
|
|
36
|
+
low = low * 256 + reader.readByte();
|
|
37
|
+
}
|
|
38
|
+
return length === 8 ? (BigInt(high) << 32n) | BigInt(low) : BigInt(low);
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=read-big-quic-varint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-big-quic-varint.js","sourceRoot":"","sources":["../../src/quic/read-big-quic-varint.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAEvE;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAkB;IACnD,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;QAClB,MAAM,IAAI,qBAAqB,CAAC,qCAAqC,CAAC,CAAC;IACxE,CAAC;IAED,MAAM,MAAM,GAAG,uBAAuB,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;IAE1D,IAAI,MAAM,CAAC,SAAS,GAAG,MAAM,EAAE,CAAC;QAC/B,MAAM,IAAI,qBAAqB,CAC9B,sBAAsB,MAAM,mBAAmB,MAAM,CAAC,SAAS,aAAa,CAC5E,CAAC;IACH,CAAC;IAED,yEAAyE;IACzE,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,GAAG,GAAG,MAAM,CAAC,QAAQ,EAAE,GAAG,IAAI,CAAC;IAEnC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC7C,IAAI,KAAK,KAAK,MAAM,GAAG,CAAC,EAAE,CAAC;YAC1B,IAAI,GAAG,GAAG,CAAC;YACX,GAAG,GAAG,CAAC,CAAC;QACT,CAAC;QACD,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC;IACrC,CAAC;IAED,OAAO,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AACzE,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ByteReader } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Reads one QUIC variable-length integer from `reader`, advancing it past the
|
|
4
|
+
* bytes consumed.
|
|
5
|
+
*
|
|
6
|
+
* Accepts a longer encoding than needed (`0x4025` is 37): QUIC allows it
|
|
7
|
+
* outside of frame types, and checking minimality is the protocol layer's job
|
|
8
|
+
* where it applies.
|
|
9
|
+
*
|
|
10
|
+
* Time complexity: O(1).
|
|
11
|
+
*
|
|
12
|
+
* @param reader - Cursor positioned at the first byte of the encoding.
|
|
13
|
+
* @returns The decoded value, from 0 to `Number.MAX_SAFE_INTEGER`.
|
|
14
|
+
* @throws {VarintIncompleteError} When fewer bytes remain than the length
|
|
15
|
+
* prefix announces.
|
|
16
|
+
* @throws {RangeError} When the value is beyond `Number.MAX_SAFE_INTEGER`; QUIC
|
|
17
|
+
* allows up to 2^62 - 1, so such an encoding is well-formed.
|
|
18
|
+
*/
|
|
19
|
+
export declare function readQuicVarint(reader: ByteReader): number;
|
|
20
|
+
//# sourceMappingURL=read-quic-varint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-quic-varint.d.ts","sourceRoot":"","sources":["../../src/quic/read-quic-varint.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAK/C;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CA0BzD"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { VarintIncompleteError } from "../errors.js";
|
|
2
|
+
import { quicVarintEncodedLength } from "./quic-varint-byte-length.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads one QUIC variable-length integer from `reader`, advancing it past the
|
|
5
|
+
* bytes consumed.
|
|
6
|
+
*
|
|
7
|
+
* Accepts a longer encoding than needed (`0x4025` is 37): QUIC allows it
|
|
8
|
+
* outside of frame types, and checking minimality is the protocol layer's job
|
|
9
|
+
* where it applies.
|
|
10
|
+
*
|
|
11
|
+
* Time complexity: O(1).
|
|
12
|
+
*
|
|
13
|
+
* @param reader - Cursor positioned at the first byte of the encoding.
|
|
14
|
+
* @returns The decoded value, from 0 to `Number.MAX_SAFE_INTEGER`.
|
|
15
|
+
* @throws {VarintIncompleteError} When fewer bytes remain than the length
|
|
16
|
+
* prefix announces.
|
|
17
|
+
* @throws {RangeError} When the value is beyond `Number.MAX_SAFE_INTEGER`; QUIC
|
|
18
|
+
* allows up to 2^62 - 1, so such an encoding is well-formed.
|
|
19
|
+
*/
|
|
20
|
+
export function readQuicVarint(reader) {
|
|
21
|
+
if (reader.atEnd) {
|
|
22
|
+
throw new VarintIncompleteError("readQuicVarint: no bytes to read");
|
|
23
|
+
}
|
|
24
|
+
const length = quicVarintEncodedLength(reader.peekByte());
|
|
25
|
+
if (reader.remaining < length) {
|
|
26
|
+
throw new VarintIncompleteError(`readQuicVarint: ${length}-byte encoding, ${reader.remaining} bytes left`);
|
|
27
|
+
}
|
|
28
|
+
let value = reader.readByte() & 0x3f;
|
|
29
|
+
for (let index = 1; index < length; index++) {
|
|
30
|
+
value = value * 256 + reader.readByte();
|
|
31
|
+
}
|
|
32
|
+
if (!Number.isSafeInteger(value)) {
|
|
33
|
+
throw new RangeError("readQuicVarint: value exceeds Number.MAX_SAFE_INTEGER");
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=read-quic-varint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-quic-varint.js","sourceRoot":"","sources":["../../src/quic/read-quic-varint.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAEvE;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,cAAc,CAAC,MAAkB;IAChD,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;QAClB,MAAM,IAAI,qBAAqB,CAAC,kCAAkC,CAAC,CAAC;IACrE,CAAC;IAED,MAAM,MAAM,GAAG,uBAAuB,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;IAE1D,IAAI,MAAM,CAAC,SAAS,GAAG,MAAM,EAAE,CAAC;QAC/B,MAAM,IAAI,qBAAqB,CAC9B,mBAAmB,MAAM,mBAAmB,MAAM,CAAC,SAAS,aAAa,CACzE,CAAC;IACH,CAAC;IAED,IAAI,KAAK,GAAG,MAAM,CAAC,QAAQ,EAAE,GAAG,IAAI,CAAC;IAErC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC7C,KAAK,GAAG,KAAK,GAAG,GAAG,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC;IACzC,CAAC;IAED,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC;QAClC,MAAM,IAAI,UAAU,CACnB,uDAAuD,CACvD,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes `value` into `target` at `offset` as a QUIC variable-length integer,
|
|
3
|
+
* in the shortest form that holds it.
|
|
4
|
+
*
|
|
5
|
+
* The in-place counterpart of {@link encodeQuicVarint}, for a caller assembling
|
|
6
|
+
* a frame without allocating per value.
|
|
7
|
+
*
|
|
8
|
+
* @param target - Destination, which must hold {@link quicVarintByteLength}
|
|
9
|
+
* bytes from `offset`.
|
|
10
|
+
* @param offset - Where to start writing.
|
|
11
|
+
* @param value - A non-negative safe integer.
|
|
12
|
+
* @returns The offset just past the bytes written.
|
|
13
|
+
* @throws {RangeError} When `value` is out of range, or `target` is too short.
|
|
14
|
+
*/
|
|
15
|
+
export declare function writeQuicVarint(target: Uint8Array, offset: number, value: number): number;
|
|
16
|
+
//# sourceMappingURL=write-quic-varint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write-quic-varint.d.ts","sourceRoot":"","sources":["../../src/quic/write-quic-varint.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,eAAe,CAC9B,MAAM,EAAE,UAAU,EAClB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,GACX,MAAM,CAuCR"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { quicVarintByteLength } from "./quic-varint-byte-length.js";
|
|
2
|
+
/**
|
|
3
|
+
* Writes `value` into `target` at `offset` as a QUIC variable-length integer,
|
|
4
|
+
* in the shortest form that holds it.
|
|
5
|
+
*
|
|
6
|
+
* The in-place counterpart of {@link encodeQuicVarint}, for a caller assembling
|
|
7
|
+
* a frame without allocating per value.
|
|
8
|
+
*
|
|
9
|
+
* @param target - Destination, which must hold {@link quicVarintByteLength}
|
|
10
|
+
* bytes from `offset`.
|
|
11
|
+
* @param offset - Where to start writing.
|
|
12
|
+
* @param value - A non-negative safe integer.
|
|
13
|
+
* @returns The offset just past the bytes written.
|
|
14
|
+
* @throws {RangeError} When `value` is out of range, or `target` is too short.
|
|
15
|
+
*/
|
|
16
|
+
export function writeQuicVarint(target, offset, value) {
|
|
17
|
+
const length = quicVarintByteLength(value);
|
|
18
|
+
if (offset < 0 || offset + length > target.length) {
|
|
19
|
+
throw new RangeError(`writeQuicVarint: ${length} bytes at offset ${offset} do not fit in ${target.length}`);
|
|
20
|
+
}
|
|
21
|
+
switch (length) {
|
|
22
|
+
case 1:
|
|
23
|
+
target[offset] = value;
|
|
24
|
+
break;
|
|
25
|
+
case 2:
|
|
26
|
+
target[offset] = 0x40 | (value >>> 8);
|
|
27
|
+
target[offset + 1] = value & 0xff;
|
|
28
|
+
break;
|
|
29
|
+
case 4:
|
|
30
|
+
target[offset] = 0x80 | (value >>> 24);
|
|
31
|
+
target[offset + 1] = (value >>> 16) & 0xff;
|
|
32
|
+
target[offset + 2] = (value >>> 8) & 0xff;
|
|
33
|
+
target[offset + 3] = value & 0xff;
|
|
34
|
+
break;
|
|
35
|
+
default: {
|
|
36
|
+
// Above 2^32 bitwise operators truncate: split into two 32-bit halves.
|
|
37
|
+
const high = Math.floor(value / 4294967296);
|
|
38
|
+
const low = value >>> 0;
|
|
39
|
+
target[offset] = 0xc0 | (high >>> 24);
|
|
40
|
+
target[offset + 1] = (high >>> 16) & 0xff;
|
|
41
|
+
target[offset + 2] = (high >>> 8) & 0xff;
|
|
42
|
+
target[offset + 3] = high & 0xff;
|
|
43
|
+
target[offset + 4] = low >>> 24;
|
|
44
|
+
target[offset + 5] = (low >>> 16) & 0xff;
|
|
45
|
+
target[offset + 6] = (low >>> 8) & 0xff;
|
|
46
|
+
target[offset + 7] = low & 0xff;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return offset + length;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=write-quic-varint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write-quic-varint.js","sourceRoot":"","sources":["../../src/quic/write-quic-varint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAEpE;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,eAAe,CAC9B,MAAkB,EAClB,MAAc,EACd,KAAa;IAEb,MAAM,MAAM,GAAG,oBAAoB,CAAC,KAAK,CAAC,CAAC;IAE3C,IAAI,MAAM,GAAG,CAAC,IAAI,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;QACnD,MAAM,IAAI,UAAU,CACnB,oBAAoB,MAAM,oBAAoB,MAAM,kBAAkB,MAAM,CAAC,MAAM,EAAE,CACrF,CAAC;IACH,CAAC;IAED,QAAQ,MAAM,EAAE,CAAC;QAChB,KAAK,CAAC;YACL,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC;YACvB,MAAM;QACP,KAAK,CAAC;YACL,MAAM,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC;YACtC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC;YAClC,MAAM;QACP,KAAK,CAAC;YACL,MAAM,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,CAAC,KAAK,KAAK,EAAE,CAAC,CAAC;YACvC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,KAAK,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;YAC3C,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,KAAK,KAAK,CAAC,CAAC,GAAG,IAAI,CAAC;YAC1C,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC;YAClC,MAAM;QACP,SAAS,CAAC;YACT,uEAAuE;YACvE,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,UAAa,CAAC,CAAC;YAC/C,MAAM,GAAG,GAAG,KAAK,KAAK,CAAC,CAAC;YACxB,MAAM,CAAC,MAAM,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,KAAK,EAAE,CAAC,CAAC;YACtC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,IAAI,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;YAC1C,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,IAAI,KAAK,CAAC,CAAC,GAAG,IAAI,CAAC;YACzC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,IAAI,GAAG,IAAI,CAAC;YACjC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,GAAG,KAAK,EAAE,CAAC;YAChC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;YACzC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,IAAI,CAAC;YACxC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,GAAG,GAAG,IAAI,CAAC;QACjC,CAAC;IACF,CAAC;IAED,OAAO,MAAM,GAAG,MAAM,CAAC;AACxB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ac-kit/format-varint",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Variable-length integer encodings: LEB128, VLQ and the other self-delimiting integer formats.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"encoding",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"@ac-kit/core": "^0.5.0",
|
|
49
|
-
"@ac-kit/format-core": "^0.1.
|
|
49
|
+
"@ac-kit/format-core": "^0.1.6"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@ac-kit/tsconfig": "0.1.0",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"@ac-kit/vitest-config": "0.1.1",
|
|
55
55
|
"@types/node": "^26.5.1",
|
|
56
56
|
"@typescript/native": "typescript@7.0.2",
|
|
57
|
-
"rimraf": "6.1.
|
|
57
|
+
"rimraf": "6.1.4",
|
|
58
58
|
"typedoc": "0.28.20",
|
|
59
59
|
"typescript": "@typescript/typescript6@6.0.2"
|
|
60
60
|
},
|