@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,71 @@
|
|
|
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
|
+
import type { ConnectionId, LongPacketTypeValue } from "../types.js";
|
|
20
|
+
import { HEADER_FORM_LONG, HEADER_FORM_SHORT } from "../types.js";
|
|
21
|
+
export { LongPacketType, type LongPacketTypeValue, } from "../types.js";
|
|
22
|
+
export type { ConnectionId } from "../types.js";
|
|
23
|
+
/** Parsed long-header fields (pre-decryption). */
|
|
24
|
+
export interface LongHeader {
|
|
25
|
+
readonly form: typeof HEADER_FORM_LONG;
|
|
26
|
+
readonly type: LongPacketTypeValue;
|
|
27
|
+
readonly version: number;
|
|
28
|
+
readonly dcid: ConnectionId;
|
|
29
|
+
readonly scid: ConnectionId;
|
|
30
|
+
/** Number of bytes the packet number occupies (1–4). */
|
|
31
|
+
readonly packetNumberLength: number;
|
|
32
|
+
/** Raw bytes consumed by the header (everything before the payload). */
|
|
33
|
+
readonly headerLength: number;
|
|
34
|
+
}
|
|
35
|
+
/** Parsed short-header (1-RTT) fields (pre-decryption). */
|
|
36
|
+
export interface ShortHeader {
|
|
37
|
+
readonly form: typeof HEADER_FORM_SHORT;
|
|
38
|
+
readonly spinBit: boolean;
|
|
39
|
+
readonly keyPhase: boolean;
|
|
40
|
+
readonly dcid: ConnectionId;
|
|
41
|
+
/** Number of bytes the packet number occupies (1–4). */
|
|
42
|
+
readonly packetNumberLength: number;
|
|
43
|
+
readonly headerLength: number;
|
|
44
|
+
}
|
|
45
|
+
export type PacketHeader = LongHeader | ShortHeader;
|
|
46
|
+
/** Serialize a long header (without payload / packet number — caller appends those). */
|
|
47
|
+
export declare function serializeLongHeader(type: LongPacketTypeValue, version: number, dcid: ConnectionId, scid: ConnectionId, packetNumberLength: number, extra?: Uint8Array): Uint8Array;
|
|
48
|
+
/** Serialize a short (1-RTT) header (without payload / packet number). */
|
|
49
|
+
export declare function serializeShortHeader(dcid: ConnectionId, packetNumberLength: number, spinBit: boolean, keyPhase: boolean): Uint8Array;
|
|
50
|
+
/**
|
|
51
|
+
* Parse a packet header from the start of a buffer. Returns the header and the
|
|
52
|
+
* number of header bytes consumed. Throws PacketParseError on malformed input.
|
|
53
|
+
*
|
|
54
|
+
* Note: this parses the *unprotected* header. In practice the packet number and
|
|
55
|
+
* payload are encrypted; the caller strips header protection before parsing the
|
|
56
|
+
* full packet number. This function parses everything up to (not including) the
|
|
57
|
+
* packet number.
|
|
58
|
+
*/
|
|
59
|
+
export declare function parsePacketHeader(buf: Uint8Array): PacketHeader;
|
|
60
|
+
/**
|
|
61
|
+
* Decode a truncated packet number to its full value given the expected next
|
|
62
|
+
* packet number (RFC 9000 §17.1). The peer sends the least-significant
|
|
63
|
+
* `encodedBits` bits; we pick the candidate within ±2^(n-1) of `largestPn`
|
|
64
|
+
* that matches those bits.
|
|
65
|
+
*/
|
|
66
|
+
export declare function decodePacketNumber(largestPn: bigint, truncated: bigint, encodedBits: number): bigint;
|
|
67
|
+
/** Encode a packet number to its truncated wire form (low `encodedBits` bits). */
|
|
68
|
+
export declare function encodePacketNumber(pn: bigint, encodedBits: number): bigint;
|
|
69
|
+
/** Read a packet number of the given byte length from a buffer at offset. */
|
|
70
|
+
export declare function readPacketNumber(buf: Uint8Array, offset: number, length: number): bigint;
|
|
71
|
+
//# sourceMappingURL=packet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"packet.d.ts","sourceRoot":"","sources":["../../src/packet/packet.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,EACH,gBAAgB,EAChB,iBAAiB,EAEpB,MAAM,aAAa,CAAC;AAIrB,OAAO,EACH,cAAc,EACd,KAAK,mBAAmB,GAC3B,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,kDAAkD;AAClD,MAAM,WAAW,UAAU;IACvB,QAAQ,CAAC,IAAI,EAAE,OAAO,gBAAgB,CAAC;IACvC,QAAQ,CAAC,IAAI,EAAE,mBAAmB,CAAC;IACnC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,wDAAwD;IACxD,QAAQ,CAAC,kBAAkB,EAAE,MAAM,CAAC;IACpC,wEAAwE;IACxE,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;CACjC;AAED,2DAA2D;AAC3D,MAAM,WAAW,WAAW;IACxB,QAAQ,CAAC,IAAI,EAAE,OAAO,iBAAiB,CAAC;IACxC,QAAQ,CAAC,OAAO,EAAE,OAA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|
+
*
|
|
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
|
+
import { HEADER_FORM_LONG, HEADER_FORM_SHORT, LONG_PACKET_TYPE_MASK, } from "../types.js";
|
|
20
|
+
import { decodeVarint, encodeVarint } from "../frame/varint.js";
|
|
21
|
+
import { concatAll } from "../utils.js";
|
|
22
|
+
export { LongPacketType, } from "../types.js";
|
|
23
|
+
/** Serialize a long header (without payload / packet number — caller appends those). */
|
|
24
|
+
export function serializeLongHeader(type, version, dcid, scid, packetNumberLength, extra = new Uint8Array(0)) {
|
|
25
|
+
const first = (HEADER_FORM_LONG << 7) | (1 << 6) | ((type & LONG_PACKET_TYPE_MASK) << 4) | (packetNumberLength - 1);
|
|
26
|
+
const versionBytes = new Uint8Array(4);
|
|
27
|
+
new DataView(versionBytes.buffer).setUint32(0, version);
|
|
28
|
+
return concatAll([
|
|
29
|
+
new Uint8Array([first]),
|
|
30
|
+
versionBytes,
|
|
31
|
+
new Uint8Array([dcid.length]),
|
|
32
|
+
dcid,
|
|
33
|
+
new Uint8Array([scid.length]),
|
|
34
|
+
scid,
|
|
35
|
+
extra,
|
|
36
|
+
]);
|
|
37
|
+
}
|
|
38
|
+
/** Serialize a short (1-RTT) header (without payload / packet number). */
|
|
39
|
+
export function serializeShortHeader(dcid, packetNumberLength, spinBit, keyPhase) {
|
|
40
|
+
let first = HEADER_FORM_SHORT << 7;
|
|
41
|
+
if (spinBit)
|
|
42
|
+
first |= 1 << 5;
|
|
43
|
+
if (keyPhase)
|
|
44
|
+
first |= 1 << 2;
|
|
45
|
+
first |= packetNumberLength - 1;
|
|
46
|
+
return concatAll([new Uint8Array([first]), dcid]);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Parse a packet header from the start of a buffer. Returns the header and the
|
|
50
|
+
* number of header bytes consumed. Throws PacketParseError on malformed input.
|
|
51
|
+
*
|
|
52
|
+
* Note: this parses the *unprotected* header. In practice the packet number and
|
|
53
|
+
* payload are encrypted; the caller strips header protection before parsing the
|
|
54
|
+
* full packet number. This function parses everything up to (not including) the
|
|
55
|
+
* packet number.
|
|
56
|
+
*/
|
|
57
|
+
export function parsePacketHeader(buf) {
|
|
58
|
+
if (buf.length < 1) {
|
|
59
|
+
throw new RangeError("Buffer too short for packet header");
|
|
60
|
+
}
|
|
61
|
+
const first = buf[0];
|
|
62
|
+
const form = (first >> 7) & 0x01;
|
|
63
|
+
if (form === HEADER_FORM_LONG) {
|
|
64
|
+
return parseLongHeader(first, buf);
|
|
65
|
+
}
|
|
66
|
+
return parseShortHeader(first, buf);
|
|
67
|
+
}
|
|
68
|
+
function parseLongHeader(first, buf) {
|
|
69
|
+
const type = (first >> 4) & LONG_PACKET_TYPE_MASK;
|
|
70
|
+
const packetNumberLength = (first & 0x03) + 1;
|
|
71
|
+
// Minimum long header: 1 (first) + 4 (version) + 1 (dcid len) + 0 + 1 (scid len) + 0 = 7.
|
|
72
|
+
if (buf.length < 7) {
|
|
73
|
+
throw new RangeError("Buffer too short for long header");
|
|
74
|
+
}
|
|
75
|
+
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
76
|
+
const version = view.getUint32(1);
|
|
77
|
+
const dcidLen = buf[5];
|
|
78
|
+
if (buf.length < 6 + dcidLen + 1) {
|
|
79
|
+
throw new RangeError("Buffer too short for DCID");
|
|
80
|
+
}
|
|
81
|
+
const dcid = buf.subarray(6, 6 + dcidLen);
|
|
82
|
+
const scidLen = buf[6 + dcidLen];
|
|
83
|
+
if (buf.length < 6 + dcidLen + 1 + scidLen) {
|
|
84
|
+
throw new RangeError("Buffer too short for SCID");
|
|
85
|
+
}
|
|
86
|
+
const scid = buf.subarray(6 + dcidLen + 1, 6 + dcidLen + 1 + scidLen);
|
|
87
|
+
const headerLength = 6 + dcidLen + 1 + scidLen;
|
|
88
|
+
return {
|
|
89
|
+
form: HEADER_FORM_LONG,
|
|
90
|
+
type: type,
|
|
91
|
+
version,
|
|
92
|
+
dcid,
|
|
93
|
+
scid,
|
|
94
|
+
packetNumberLength,
|
|
95
|
+
headerLength,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
function parseShortHeader(first, _buf) {
|
|
99
|
+
const spinBit = ((first >> 5) & 0x01) === 1;
|
|
100
|
+
const keyPhase = ((first >> 2) & 0x01) === 1;
|
|
101
|
+
const packetNumberLength = (first & 0x03) + 1;
|
|
102
|
+
// DCID length is variable and not on the wire for short headers — the
|
|
103
|
+
// caller must know it (from the handshake). We record header length as just
|
|
104
|
+
// the first byte; the DCID is decoded by the connection layer.
|
|
105
|
+
return {
|
|
106
|
+
form: HEADER_FORM_SHORT,
|
|
107
|
+
spinBit,
|
|
108
|
+
keyPhase,
|
|
109
|
+
dcid: new Uint8Array(0),
|
|
110
|
+
packetNumberLength,
|
|
111
|
+
headerLength: 1,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Decode a truncated packet number to its full value given the expected next
|
|
116
|
+
* packet number (RFC 9000 §17.1). The peer sends the least-significant
|
|
117
|
+
* `encodedBits` bits; we pick the candidate within ±2^(n-1) of `largestPn`
|
|
118
|
+
* that matches those bits.
|
|
119
|
+
*/
|
|
120
|
+
export function decodePacketNumber(largestPn, truncated, encodedBits) {
|
|
121
|
+
const pnNbits = BigInt(encodedBits);
|
|
122
|
+
const expectedPn = largestPn + 1n;
|
|
123
|
+
const pnWin = 1n << pnNbits;
|
|
124
|
+
const pnHwin = pnWin / 2n;
|
|
125
|
+
const pnMask = pnWin - 1n;
|
|
126
|
+
const candidate = (expectedPn & ~pnMask) | truncated;
|
|
127
|
+
if (candidate <= expectedPn - pnHwin && candidate < (1n << 62n) - pnWin) {
|
|
128
|
+
return candidate + pnWin;
|
|
129
|
+
}
|
|
130
|
+
if (candidate > expectedPn + pnHwin && candidate >= pnWin) {
|
|
131
|
+
return candidate - pnWin;
|
|
132
|
+
}
|
|
133
|
+
return candidate;
|
|
134
|
+
}
|
|
135
|
+
/** Encode a packet number to its truncated wire form (low `encodedBits` bits). */
|
|
136
|
+
export function encodePacketNumber(pn, encodedBits) {
|
|
137
|
+
const mask = (1n << BigInt(encodedBits)) - 1n;
|
|
138
|
+
return pn & mask;
|
|
139
|
+
}
|
|
140
|
+
/** Read a packet number of the given byte length from a buffer at offset. */
|
|
141
|
+
export function readPacketNumber(buf, offset, length) {
|
|
142
|
+
let value = 0n;
|
|
143
|
+
for (let i = 0; i < length; i++) {
|
|
144
|
+
value = (value << 8n) | BigInt(buf[offset + i]);
|
|
145
|
+
}
|
|
146
|
+
return value;
|
|
147
|
+
}
|
|
148
|
+
void decodeVarint;
|
|
149
|
+
void encodeVarint;
|
|
150
|
+
//# sourceMappingURL=packet.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"packet.js","sourceRoot":"","sources":["../../src/packet/packet.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAGH,OAAO,EACH,gBAAgB,EAChB,iBAAiB,EACjB,qBAAqB,GACxB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,aAA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;IACD,OAAO,KAAK,CAAC;AACjB,CAAC;AAED,KAAK,YAAY,CAAC;AAClB,KAAK,YAAY,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QUIC stream state machine + flow control + receive reassembly (RFC 9000 §2, §4).
|
|
3
|
+
*
|
|
4
|
+
* Each QUIC stream is a reliable, ordered byte stream. The connection receives
|
|
5
|
+
* STREAM frames (offset, data, fin) out of order — datagrams are not ordered —
|
|
6
|
+
* so a stream reassembles by offset into the byte stream `read()` consumes.
|
|
7
|
+
* Flow control is enforced on both sides: we never send more than the peer's
|
|
8
|
+
* MAX_STREAM_DATA / MAX_DATA windows allow, and we replenish the peer's send
|
|
9
|
+
* credit with MAX_STREAM_DATA / MAX_DATA frames as we consume.
|
|
10
|
+
*
|
|
11
|
+
* Honest limitations:
|
|
12
|
+
* - Send-side backpressure is real: `write()` resolves once bytes are buffered
|
|
13
|
+
* at the QUIC layer, and the connection drains the send queue only as far as
|
|
14
|
+
* the peer's flow-control window allows, queueing the rest. When the window
|
|
15
|
+
* is exhausted we tell the peer with DATA_BLOCKED / STREAM_DATA_BLOCKED.
|
|
16
|
+
* - There is no congestion controller and no liveness ping in this layer —
|
|
17
|
+
* ACK frames are relayed but not paced. Connection establishment (the TLS
|
|
18
|
+
* handshake) and packet protection are out of scope; `connectQuic()` returns
|
|
19
|
+
* a connection that moves frames over the transport directly.
|
|
20
|
+
*/
|
|
21
|
+
import { EventEmitter } from "node:events";
|
|
22
|
+
import type { QuicFrame, QuicStream, QuicTransportParameters } from "../types.js";
|
|
23
|
+
/** A handle the stream manager exposes to the connection for sending + dispatch. */
|
|
24
|
+
export interface StreamManager {
|
|
25
|
+
/** Open a new locally-initiated stream of the given type. */
|
|
26
|
+
openStream(bidirectional: boolean): QuicStream;
|
|
27
|
+
/** Wait for the next peer-initiated stream of the given type. */
|
|
28
|
+
acceptStream(bidirectional: boolean): Promise<QuicStream>;
|
|
29
|
+
/** Dispatch a decoded frame to the relevant stream / connection. */
|
|
30
|
+
dispatch(frame: QuicFrame): void;
|
|
31
|
+
/**
|
|
32
|
+
* Drain pending sends into STREAM frames, respecting flow control. Calls
|
|
33
|
+
* `emit` for each frame that should be packed into an outbound packet.
|
|
34
|
+
* `maxPayload` bounds the total bytes of STREAM data to emit this pass.
|
|
35
|
+
*/
|
|
36
|
+
flushSends(maxPayload: number, emit: (frame: QuicFrame) => void): void;
|
|
37
|
+
/** True if there are streams with data/FIN pending to send. */
|
|
38
|
+
readonly hasPendingSends: boolean;
|
|
39
|
+
/** Reject every in-flight reader/writer and close all streams. */
|
|
40
|
+
abortAll(error: Error): void;
|
|
41
|
+
/** Begin connection close: emit a CONNECTION_CLOSE and stop accepting. */
|
|
42
|
+
close(errorCode: bigint, reason: string): void;
|
|
43
|
+
/** The local transport parameters we advertise. */
|
|
44
|
+
readonly localParameters: QuicTransportParameters;
|
|
45
|
+
}
|
|
46
|
+
/** Dependencies the stream manager needs from the connection. */
|
|
47
|
+
export interface StreamManagerDeps {
|
|
48
|
+
/** Send a frame to be packed into an outbound packet. */
|
|
49
|
+
sendFrame: (frame: QuicFrame) => void;
|
|
50
|
+
/** Local transport parameters to advertise. */
|
|
51
|
+
localParameters: QuicTransportParameters;
|
|
52
|
+
/** Peer transport parameters (for their receive windows). */
|
|
53
|
+
peerParameters: QuicTransportParameters;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Create a stream manager. `deps.sendFrame` is the callback for frame I/O — the
|
|
57
|
+
* manager never touches the transport directly.
|
|
58
|
+
*
|
|
59
|
+
* The returned object is also an {@link EventEmitter} emitting connection-level
|
|
60
|
+
* signals the connection layer reacts to:
|
|
61
|
+
* - "incomingStream", stream — a peer-initiated stream the app can accept
|
|
62
|
+
* - "connectionClose", {errorCode, reason} — peer sent CONNECTION_CLOSE
|
|
63
|
+
* - "maxData", maximum — peer grew our connection send window
|
|
64
|
+
*/
|
|
65
|
+
export declare function createStreamManager(deps: StreamManagerDeps): StreamManager & EventEmitter;
|
|
66
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/stream/stream.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,KAAK,EAER,SAAS,EACT,UAAU,EACV,uBAAuB,EAI1B,MAAM,aAAa,CAAC;AAkVrB,oFAAoF;AACpF,MAAM,WAAW,aAAa;IAC1B,6DAA6D;IAC7D,UAAU,CAAC,aAAa,EAAE,OAAO,GAAG,UAAU,CAAC;IAC/C,iEAAiE;IACjE,YAAY,CAAC,aAAa,EAAE,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAC1D,oEAAoE;IACpE,QAAQ,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;IACjC;;;;OAIG;IACH,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,GAAG,IAAI,CAAC;IACvE,+DAA+D;IAC/D,QAAQ,CAAC,eAAe,EAAE,OAAO,CAAC;IAClC,kEAAkE;IAClE,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IAC7B,0EAA0E;IAC1E,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/C,mDAAmD;IACnD,QAAQ,CAAC,eAAe,EAAE,uBAAuB,CAAC;CACrD;AAED,iEAAiE;AACjE,MAAM,WAAW,iBAAiB;IAC9B,yDAAyD;IACzD,SAAS,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,CAAC;IACtC,+CAA+C;IAC/C,eAAe,EAAE,uBAAuB,CAAC;IACzC,6DAA6D;IAC7D,cAAc,EAAE,uBAAuB,CAAC;CAC3C;AAED;;;;;;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