@browsercore/http1 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jens Verneuer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE
package/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # @browsercore/http1
2
+
3
+ An HTTP/1.1 client over any duplex byte stream.
4
+
5
+ ## Responsibility
6
+
7
+ Serialize requests, parse responses, and manage the HTTP/1.1 protocol state
8
+ machine: keep-alive, chunked transfer-encoding, content-encoding compression,
9
+ redirect following, and a cookie-header integration seam. The package knows
10
+ nothing about the underlying transport — it could be plain TCP, TLS, a pipe, or
11
+ a test double.
12
+
13
+ ## What it does NOT know about
14
+
15
+ - TLS / DTLS
16
+ - TCP, DNS, or sockets
17
+ - HTTP/2 or HTTP/3
18
+ - Browser fingerprints
19
+
20
+ ## Public API
21
+
22
+ ```ts
23
+ import { connectHttp1, RedirectLimitError } from "@browsercore/http1";
24
+
25
+ const conn = await connectHttp1({ transport });
26
+ const res = await conn.request({
27
+ method: "GET",
28
+ url: "/index.html",
29
+ headers: new Map([["host", "example.com"]]),
30
+ body: { kind: "empty" },
31
+ });
32
+
33
+ console.log(res.statusCode, res.body);
34
+ await conn.close();
35
+ ```
36
+
37
+ ## Types
38
+
39
+ | Export | Kind | Purpose |
40
+ | --- | --- | --- |
41
+ | `Http1Connection` | interface | Public contract higher layers depend on |
42
+ | `connectHttp1()` | function | Wrap a transport with HTTP/1.1 |
43
+ | `HttpRequest` | interface | Serializable request |
44
+ | `HttpResponse` | interface | Parsed response |
45
+ | `HttpBodyKind` | discriminated union | Empty / bytes / streaming body |
46
+ | `Http1ConnectionState` | discriminated union | `idle \| in_flight \| closing \| closed` |
47
+ | `Http1Error` | class | Base typed error |
48
+ | `RedirectLimitError` | class | Redirect chain exceeded |
49
+ | `InvalidResponseError` | class | Unparseable response bytes |
50
+ | `ChunkEncodingError` | class | Malformed chunked encoding |
51
+
52
+ ## Dependency graph
53
+
54
+ ```
55
+ @browsercore/http1
56
+ └─ @browsercore/transport
57
+ └─ node:net / node:dns / node:crypto
58
+ ```
59
+
60
+ No other `@browsercore/*` packages are imported.
@@ -0,0 +1,78 @@
1
+ /**
2
+ * HTTP/1.1 connection implementation.
3
+ *
4
+ * Wires the message parser/serializer over a `@browsercore/transport` duplex byte
5
+ * stream. Handles keep-alive via serial request/response on a single connection.
6
+ */
7
+ import type { Http1CloseReason, Http1Connection, Http1ConnectionId, Http1ConnectionState, Http1Options, HttpRequest, HttpResponse } from "./types.js";
8
+ /** Concrete HTTP/1.1 connection. */
9
+ export declare class Http1ConnectionImpl implements Http1Connection {
10
+ private readonly _options;
11
+ readonly id: Http1ConnectionId;
12
+ state: Http1ConnectionState;
13
+ /**
14
+ * Incoming bytes not yet consumed by a parsed response. Drained by
15
+ * `bytesConsumed` after every parse — this is what makes keep-alive work.
16
+ */
17
+ private _buffer;
18
+ /** Resolvers awaiting the next `data` event from the transport. */
19
+ private _dataWaiters;
20
+ /** Resolvers awaiting connection close (used by `close()` to drain in-flight). */
21
+ private _closeWaiters;
22
+ /** Set once the transport has closed unexpectedly (remote close / error). */
23
+ private _transportClosed;
24
+ constructor(id: Http1ConnectionId, _options: Http1Options);
25
+ request(req: HttpRequest): Promise<HttpResponse>;
26
+ close(reason?: Http1CloseReason): Promise<void>;
27
+ private _isClosedOrClosing;
28
+ /** Read bytes from the transport until a complete response is available. */
29
+ private _readResponse;
30
+ /**
31
+ * Attempt to parse a complete response from the current buffer.
32
+ * Returns `undefined` if more bytes are needed.
33
+ */
34
+ private _tryParse;
35
+ /**
36
+ * Parse the first `totalLength` bytes, drain them from the buffer, then
37
+ * decode transfer-encoding (chunked) and content-encoding in the correct
38
+ * order: transfer-encoding first (it's the outer framing), then
39
+ * content-encoding on the reassembled bytes.
40
+ */
41
+ private _parseAndDrain;
42
+ /**
43
+ * Apply transfer-encoding (chunked) decoding then content-encoding
44
+ * decompression to a parsed response's body. `parseResponse` is kept pure
45
+ * (wire-format only); this is where protocol semantics get applied.
46
+ */
47
+ private _decodeBody;
48
+ /** Read one chunk from the transport, or `undefined` once it has closed. */
49
+ private _readChunk;
50
+ private _appendBuffer;
51
+ private _ensureOpen;
52
+ private _transition;
53
+ /** Derive the current pending count from the state. */
54
+ private _pendingCount;
55
+ /**
56
+ * Build the cookie-matching URL for a request.
57
+ *
58
+ * http1 is scheme-agnostic — TLS lives below the transport, so the protocol
59
+ * is not known here. We derive `host` from the `host` header and `path` from
60
+ * the request target, and default `protocol` to `https:` as a best-effort
61
+ * (this stack is built for TLS). The seam is optional and only invoked when
62
+ * a caller supplies an interceptor.
63
+ */
64
+ private _cookieUrl;
65
+ /**
66
+ * Merge cookies returned by the interceptor into a request. A bare string
67
+ * is treated as the `Cookie` header value; a map is merged name->value.
68
+ */
69
+ private _injectCookies;
70
+ }
71
+ /**
72
+ * Establish an HTTP/1.1 connection over an existing transport.
73
+ *
74
+ * The transport is assumed to be already connected — this function only wraps
75
+ * it with the HTTP/1.1 protocol state machine.
76
+ */
77
+ export declare function connectHttp1(options: Http1Options): Promise<Http1Connection>;
78
+ //# sourceMappingURL=connection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACR,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EACjB,oBAAoB,EACpB,YAAY,EACZ,WAAW,EACX,YAAY,EACf,MAAM,YAAY,CAAC;AAKpB,oCAAoC;AACpC,qBAAa,mBAAoB,YAAW,eAAe;IAqBnD,OAAO,CAAC,QAAQ,CAAC,QAAQ;IApB7B,SAAgB,EAAE,EAAE,iBAAiB,CAAC;IAC/B,KAAK,EAAE,oBAAoB,CAAqB;IAEvD;;;OAGG;IACH,OAAO,CAAC,OAAO,CAAiC;IAEhD,mEAAmE;IACnE,OAAO,CAAC,YAAY,CAAyC;IAE7D,kFAAkF;IAClF,OAAO,CAAC,aAAa,CAAyB;IAE9C,6EAA6E;IAC7E,OAAO,CAAC,gBAAgB,CAAS;gBAG7B,EAAE,EAAE,iBAAiB,EACJ,QAAQ,EAAE,YAAY;IAoB9B,OAAO,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,YAAY,CAAC;IAkChD,KAAK,CAAC,MAAM,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC;IAiB5D,OAAO,CAAC,kBAAkB;IAK1B,4EAA4E;YAC9D,aAAa;IAsB3B;;;OAGG;YACW,SAAS;IA2BvB;;;;;OAKG;YACW,cAAc;IAM5B;;;;OAIG;YACW,WAAW;IAsBzB,4EAA4E;YAC9D,UAAU;IAYxB,OAAO,CAAC,aAAa;IAQrB,OAAO,CAAC,WAAW;IAenB,OAAO,CAAC,WAAW;IAInB,uDAAuD;IACvD,OAAO,CAAC,aAAa;IAMrB;;;;;;;;OAQG;IACH,OAAO,CAAC,UAAU;IASlB;;;OAGG;IACH,OAAO,CAAC,cAAc;CAiBzB;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,eAAe,CAAC,CAGlF"}
@@ -0,0 +1,423 @@
1
+ /**
2
+ * HTTP/1.1 connection implementation.
3
+ *
4
+ * Wires the message parser/serializer over a `@browsercore/transport` duplex byte
5
+ * stream. Handles keep-alive via serial request/response on a single connection.
6
+ */
7
+ import { parseResponse, serializeRequest, parseChunkedEncoding } from "./message.js";
8
+ import { decompressBody } from "./decompress.js";
9
+ import { assertNever, createId } from "./utils.js";
10
+ /** Concrete HTTP/1.1 connection. */
11
+ export class Http1ConnectionImpl {
12
+ _options;
13
+ id;
14
+ state = { state: "idle" };
15
+ /**
16
+ * Incoming bytes not yet consumed by a parsed response. Drained by
17
+ * `bytesConsumed` after every parse — this is what makes keep-alive work.
18
+ */
19
+ _buffer = new Uint8Array(0);
20
+ /** Resolvers awaiting the next `data` event from the transport. */
21
+ _dataWaiters = [];
22
+ /** Resolvers awaiting connection close (used by `close()` to drain in-flight). */
23
+ _closeWaiters = [];
24
+ /** Set once the transport has closed unexpectedly (remote close / error). */
25
+ _transportClosed = false;
26
+ constructor(id, _options) {
27
+ this._options = _options;
28
+ this.id = id;
29
+ this._options.transport.on("data", (chunk) => {
30
+ this._appendBuffer(chunk);
31
+ const waiter = this._dataWaiters.shift();
32
+ if (waiter !== undefined) {
33
+ waiter(chunk);
34
+ }
35
+ });
36
+ this._options.transport.on("close", () => {
37
+ this._transportClosed = true;
38
+ // Wake any pending reads so they can observe the closed transport.
39
+ while (this._dataWaiters.length > 0) {
40
+ const waiter = this._dataWaiters.shift();
41
+ if (waiter !== undefined)
42
+ waiter(new Uint8Array(0));
43
+ }
44
+ });
45
+ }
46
+ async request(req) {
47
+ this._ensureOpen();
48
+ this._transition({ state: "in_flight", pending: this._pendingCount() + 1 });
49
+ try {
50
+ // Cookie seam (optional): inject request cookies before serializing.
51
+ const interceptor = this._options.cookieInterceptor;
52
+ let wireReq = req;
53
+ if (interceptor?.addCookies !== undefined) {
54
+ wireReq = this._injectCookies(req, interceptor.addCookies(this._cookieUrl(req)));
55
+ }
56
+ const wire = serializeRequest(wireReq);
57
+ await this._options.transport.write(wire);
58
+ const response = await this._readResponse();
59
+ // Cookie seam (optional): collect response Set-Cookie headers.
60
+ if (interceptor?.storeCookies !== undefined) {
61
+ interceptor.storeCookies(this._cookieUrl(wireReq), collectSetCookie(response.headers));
62
+ }
63
+ return response;
64
+ }
65
+ finally {
66
+ const pending = this._pendingCount() - 1;
67
+ if (pending === 0) {
68
+ this._transition({ state: "idle" });
69
+ if (this._closeWaiters.length > 0) {
70
+ for (const waiter of this._closeWaiters)
71
+ waiter();
72
+ this._closeWaiters = [];
73
+ }
74
+ }
75
+ else {
76
+ this._transition({ state: "in_flight", pending });
77
+ }
78
+ }
79
+ }
80
+ async close(reason) {
81
+ const effectiveReason = reason ?? { kind: "client_close" };
82
+ if (this._isClosedOrClosing())
83
+ return;
84
+ // If requests are in flight, wait for them to drain before closing.
85
+ if (this._pendingCount() > 0) {
86
+ this._transition({ state: "closing" });
87
+ await new Promise((resolve) => {
88
+ this._closeWaiters.push(resolve);
89
+ });
90
+ }
91
+ if (this._isClosedOrClosing())
92
+ return;
93
+ this._transition({ state: "closed", reason: effectiveReason });
94
+ await this._options.transport.close();
95
+ }
96
+ _isClosedOrClosing() {
97
+ const s = this.state.state;
98
+ return s === "closed" || s === "closing";
99
+ }
100
+ /** Read bytes from the transport until a complete response is available. */
101
+ async _readResponse() {
102
+ while (true) {
103
+ const parsed = await this._tryParse();
104
+ if (parsed !== undefined) {
105
+ return parsed;
106
+ }
107
+ const chunk = await this._readChunk();
108
+ if (chunk === undefined) {
109
+ // Transport closed mid-response — drain what we have.
110
+ if (this._buffer.length > 0) {
111
+ const { response } = parseResponse(this._buffer);
112
+ this._buffer = this._buffer.slice(0, 0);
113
+ return this._decodeBody(response);
114
+ }
115
+ throw new Error("transport closed before response received");
116
+ }
117
+ // The chunk was already appended to the buffer by the "data" event
118
+ // handler, which also woke this waiter. Nothing more to do here.
119
+ void chunk;
120
+ }
121
+ }
122
+ /**
123
+ * Attempt to parse a complete response from the current buffer.
124
+ * Returns `undefined` if more bytes are needed.
125
+ */
126
+ async _tryParse() {
127
+ const headerEnd = findHeaderEnd(this._buffer);
128
+ if (headerEnd === -1)
129
+ return undefined;
130
+ const bodyStart = headerEnd + 4;
131
+ const headerText = decodeAscii(this._buffer, 0, headerEnd);
132
+ const contentLength = extractContentLength(headerText);
133
+ const isChunked = isChunkedEncoding(headerText);
134
+ if (contentLength !== undefined) {
135
+ const totalLength = bodyStart + contentLength;
136
+ if (this._buffer.length < totalLength)
137
+ return undefined;
138
+ return await this._parseAndDrain(totalLength);
139
+ }
140
+ if (isChunked) {
141
+ const bodyEnd = findChunkedBodyEnd(this._buffer, bodyStart);
142
+ if (bodyEnd === -1)
143
+ return undefined;
144
+ return await this._parseAndDrain(bodyEnd);
145
+ }
146
+ // No content-length and not chunked — body runs until transport close.
147
+ if (!this._transportClosed)
148
+ return undefined;
149
+ return await this._parseAndDrain(this._buffer.length);
150
+ }
151
+ /**
152
+ * Parse the first `totalLength` bytes, drain them from the buffer, then
153
+ * decode transfer-encoding (chunked) and content-encoding in the correct
154
+ * order: transfer-encoding first (it's the outer framing), then
155
+ * content-encoding on the reassembled bytes.
156
+ */
157
+ async _parseAndDrain(totalLength) {
158
+ const { response, bytesConsumed } = parseResponse(this._buffer.slice(0, totalLength));
159
+ this._buffer = this._buffer.slice(bytesConsumed);
160
+ return this._decodeBody(response);
161
+ }
162
+ /**
163
+ * Apply transfer-encoding (chunked) decoding then content-encoding
164
+ * decompression to a parsed response's body. `parseResponse` is kept pure
165
+ * (wire-format only); this is where protocol semantics get applied.
166
+ */
167
+ async _decodeBody(response) {
168
+ const headers = response.headers;
169
+ let body = response.body;
170
+ const isChunked = headers.get("transfer-encoding");
171
+ if (isChunked !== undefined && isChunked.toLowerCase().includes("chunked")) {
172
+ body = await materialize(parseChunkedEncoding(chunkIterable(body)));
173
+ }
174
+ const contentEncoding = headers.get("content-encoding");
175
+ if (contentEncoding !== undefined) {
176
+ body = decompressBody(body, contentEncoding);
177
+ }
178
+ return {
179
+ statusCode: response.statusCode,
180
+ statusText: response.statusText,
181
+ headers,
182
+ body,
183
+ };
184
+ }
185
+ /** Read one chunk from the transport, or `undefined` once it has closed. */
186
+ async _readChunk() {
187
+ return new Promise((resolve) => {
188
+ this._dataWaiters.push((chunk) => {
189
+ if (this._transportClosed && chunk.length === 0) {
190
+ resolve(undefined);
191
+ }
192
+ else {
193
+ resolve(chunk);
194
+ }
195
+ });
196
+ });
197
+ }
198
+ _appendBuffer(chunk) {
199
+ if (chunk.length === 0)
200
+ return;
201
+ const next = new Uint8Array(this._buffer.length + chunk.length);
202
+ next.set(this._buffer, 0);
203
+ next.set(chunk, this._buffer.length);
204
+ this._buffer = next;
205
+ }
206
+ _ensureOpen() {
207
+ const s = this.state;
208
+ switch (s.state) {
209
+ case "idle":
210
+ case "in_flight":
211
+ return;
212
+ case "closing":
213
+ throw new Error("connection is closing — no new requests allowed");
214
+ case "closed":
215
+ throw new Error(`connection is closed: ${describeCloseReason(s.reason)}`);
216
+ default:
217
+ assertNever(s);
218
+ }
219
+ }
220
+ _transition(next) {
221
+ this.state = next;
222
+ }
223
+ /** Derive the current pending count from the state. */
224
+ _pendingCount() {
225
+ const s = this.state;
226
+ if (s.state === "in_flight")
227
+ return s.pending;
228
+ return 0;
229
+ }
230
+ /**
231
+ * Build the cookie-matching URL for a request.
232
+ *
233
+ * http1 is scheme-agnostic — TLS lives below the transport, so the protocol
234
+ * is not known here. We derive `host` from the `host` header and `path` from
235
+ * the request target, and default `protocol` to `https:` as a best-effort
236
+ * (this stack is built for TLS). The seam is optional and only invoked when
237
+ * a caller supplies an interceptor.
238
+ */
239
+ _cookieUrl(req) {
240
+ const host = req.headers.get("host") ?? "";
241
+ return {
242
+ host: host.split(":")[0] ?? host,
243
+ path: new URL(`http://${host}${req.url}`).pathname,
244
+ protocol: "https:",
245
+ };
246
+ }
247
+ /**
248
+ * Merge cookies returned by the interceptor into a request. A bare string
249
+ * is treated as the `Cookie` header value; a map is merged name->value.
250
+ */
251
+ _injectCookies(req, cookies) {
252
+ if (typeof cookies === "string") {
253
+ if (cookies === "")
254
+ return req;
255
+ const headers = new Map(req.headers);
256
+ headers.set("cookie", cookies);
257
+ return { ...req, headers };
258
+ }
259
+ if (cookies.size === 0)
260
+ return req;
261
+ const headers = new Map(req.headers);
262
+ for (const [name, value] of cookies) {
263
+ headers.set(name, value);
264
+ }
265
+ return { ...req, headers };
266
+ }
267
+ }
268
+ /**
269
+ * Establish an HTTP/1.1 connection over an existing transport.
270
+ *
271
+ * The transport is assumed to be already connected — this function only wraps
272
+ * it with the HTTP/1.1 protocol state machine.
273
+ */
274
+ export async function connectHttp1(options) {
275
+ const id = createId("http1");
276
+ return new Http1ConnectionImpl(id, options);
277
+ }
278
+ // --- Byte-level helpers --------------------------------------------------
279
+ /** Find the offset of the `\r\n\r\n` header terminator, or -1 if not present. */
280
+ function findHeaderEnd(buf) {
281
+ for (let i = 0; i + 3 < buf.length; i++) {
282
+ if (buf[i] === 0x0d &&
283
+ buf[i + 1] === 0x0a &&
284
+ buf[i + 2] === 0x0d &&
285
+ buf[i + 3] === 0x0a) {
286
+ return i;
287
+ }
288
+ }
289
+ return -1;
290
+ }
291
+ /** Decode a slice of bytes as ASCII without going through `Buffer`. */
292
+ function decodeAscii(buf, start, end) {
293
+ let out = "";
294
+ for (let i = start; i < end; i++) {
295
+ out += String.fromCharCode(buf[i]);
296
+ }
297
+ return out;
298
+ }
299
+ /** Extract the `content-length` header value, or `undefined` if absent. */
300
+ function extractContentLength(headerText) {
301
+ const match = /(?:^|\n)content-length:\s*(\d+)\r?/i.exec(headerText);
302
+ if (match === null)
303
+ return undefined;
304
+ const value = match[1];
305
+ return value === undefined ? undefined : Number(value);
306
+ }
307
+ /** Whether the response uses chunked transfer-encoding. */
308
+ function isChunkedEncoding(headerText) {
309
+ const match = /(?:^|\n)transfer-encoding:\s*([^\r\n]+)/i.exec(headerText);
310
+ if (match === null)
311
+ return false;
312
+ const value = match[1];
313
+ return value !== undefined && value.toLowerCase().includes("chunked");
314
+ }
315
+ /**
316
+ * Find the offset just past the end of a chunked body in `buf`, starting the
317
+ * scan at `bodyStart`. Returns -1 if the terminating chunk has not arrived.
318
+ */
319
+ function findChunkedBodyEnd(buf, bodyStart) {
320
+ let offset = bodyStart;
321
+ while (offset < buf.length) {
322
+ // Find the end of the chunk-size line.
323
+ let lineEnd = -1;
324
+ for (let i = offset; i + 1 < buf.length; i++) {
325
+ if (buf[i] === 0x0d && buf[i + 1] === 0x0a) {
326
+ lineEnd = i;
327
+ break;
328
+ }
329
+ }
330
+ if (lineEnd === -1)
331
+ return -1;
332
+ const sizeLine = decodeAscii(buf, offset, lineEnd);
333
+ // exec returns null (not undefined) when there is no match.
334
+ if (!/^[0-9a-fA-F]+(?:;[^\r\n]*)?$/.test(sizeLine))
335
+ return -1;
336
+ const size = Number.parseInt(sizeLine, 16);
337
+ const dataStart = lineEnd + 2;
338
+ if (size === 0) {
339
+ // last-chunk = "1*("0") [ chunk-ext ] CRLF" — no chunk-data, no
340
+ // trailing data CRLF. dataStart points just past the "0\r\n".
341
+ // trailer-part follows: zero or more header-field lines, then a
342
+ // final blank line. consumeTrailers returns the offset just past
343
+ // that final blank line, or -1 if the buffer doesn't hold it all.
344
+ const trailerEnd = consumeTrailers(buf, dataStart);
345
+ return trailerEnd === -1 ? -1 : trailerEnd;
346
+ }
347
+ const dataEnd = dataStart + size;
348
+ const chunkEnd = dataEnd + 2; // trailing \r\n after chunk data
349
+ if (chunkEnd > buf.length)
350
+ return -1;
351
+ if (buf[dataEnd] !== 0x0d || buf[dataEnd + 1] !== 0x0a)
352
+ return -1;
353
+ offset = chunkEnd;
354
+ }
355
+ return -1;
356
+ }
357
+ /**
358
+ * Consume the trailer section (zero or more header-field lines + a final blank
359
+ * line) starting at `start`. Returns the offset just past the terminating blank
360
+ * line, or -1 if the buffer does not yet hold the full trailer section.
361
+ *
362
+ * A blank line is a `CRLF` at `lineStart` — i.e. two consecutive CRLFs with no
363
+ * header content between them. This mirrors the trailer handling in
364
+ * `parseChunkedEncoding`.
365
+ */
366
+ function consumeTrailers(buf, start) {
367
+ let lineStart = start;
368
+ for (let i = start; i + 1 < buf.length; i++) {
369
+ if (buf[i] !== 0x0d || buf[i + 1] !== 0x0a)
370
+ continue;
371
+ if (i === lineStart)
372
+ return i + 2; // blank line — end of trailers
373
+ lineStart = i + 2;
374
+ }
375
+ return -1;
376
+ }
377
+ /** Human-readable description of a close reason — for error messages. */
378
+ function describeCloseReason(reason) {
379
+ switch (reason.kind) {
380
+ case "client_close":
381
+ return "client closed";
382
+ case "remote_close":
383
+ return "remote closed";
384
+ case "error":
385
+ return `error: ${reason.error.message}`;
386
+ case "redirect_jump":
387
+ return `redirect to ${reason.to}`;
388
+ default:
389
+ assertNever(reason);
390
+ }
391
+ }
392
+ // --- Body decoding helpers -----------------------------------------------
393
+ /** Yield the bytes of a single buffer as an `AsyncIterable` (one chunk). */
394
+ async function* chunkIterable(buf) {
395
+ if (buf.length > 0)
396
+ yield buf;
397
+ }
398
+ /** Collect all chunks of an async byte stream into one contiguous buffer. */
399
+ async function materialize(stream) {
400
+ const chunks = [];
401
+ let total = 0;
402
+ for await (const chunk of stream) {
403
+ chunks.push(chunk);
404
+ total += chunk.length;
405
+ }
406
+ const out = new Uint8Array(total);
407
+ let offset = 0;
408
+ for (const chunk of chunks) {
409
+ out.set(chunk, offset);
410
+ offset += chunk.length;
411
+ }
412
+ return out;
413
+ }
414
+ /** Collect all `set-cookie` header values (case-insensitive) in wire order. */
415
+ function collectSetCookie(headers) {
416
+ const out = [];
417
+ for (const [name, value] of headers) {
418
+ if (name === "set-cookie")
419
+ out.push(value);
420
+ }
421
+ return out;
422
+ }
423
+ //# sourceMappingURL=connection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connection.js","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAWH,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACrF,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEnD,oCAAoC;AACpC,MAAM,OAAO,mBAAmB;IAqBP;IApBL,EAAE,CAAoB;IAC/B,KAAK,GAAyB,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IAEvD;;;OAGG;IACK,OAAO,GAAe,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;IAEhD,mEAAmE;IAC3D,YAAY,GAAsC,EAAE,CAAC;IAE7D,kFAAkF;IAC1E,aAAa,GAAsB,EAAE,CAAC;IAE9C,6EAA6E;IACrE,gBAAgB,GAAG,KAAK,CAAC;IAEjC,YACI,EAAqB,EACJ,QAAsB;QAAtB,aAAQ,GAAR,QAAQ,CAAc;QAEvC,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAiB,EAAQ,EAAE;YAC3D,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;YAC1B,MAAM,MAAM,GAAG,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;YACzC,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;gBACvB,MAAM,CAAC,KAAK,CAAC,CAAC;YAClB,CAAC;QACL,CAAC,CAAC,CAAC;QACH,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,GAAS,EAAE;YAC3C,IAAI,CAAC,gBAAgB,GAAG,IAAI,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E;AAC5E,KAAK,SAAS,CAAC,CAAC,aAAa,CAAC,GAAe;IACzC,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC;QAAE,MAAM,GAAG,CAAC;AAClC,CAAC;AAED,6EAA6E;AAC7E,KAAK,UAAU,WAAW,CAAC,MAAiC;IACxD,MAAM,MAAM,GAAiB,EAAE,CAAC;IAChC,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC/B,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACnB,KAAK,IAAI,KAAK,CAAC,MAAM,CAAC;IAC1B,CAAC;IACD,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;IAClC,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QACzB,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC;IAC3B,CAAC;IACD,OAAO,GAAG,CAAC;AACf,CAAC;AAED,+EAA+E;AAC/E,SAAS,gBAAgB,CAAC,OAAoC;IAC1D,MAAM,GAAG,GAAa,EAAE,CAAC;IACzB,KAAK,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,OAAO,EAAE,CAAC;QAClC,IAAI,IAAI,KAAK,YAAY;YAAE,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC/C,CAAC;IACD,OAAO,GAAG,CAAC;AACf,CAAC"}
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Content-encoding decompression for HTTP/1.1 responses.
3
+ *
4
+ * Pure function — operates on the full buffered body bytes (after any
5
+ * transfer-encoding has been removed). Uses `node:zlib` for gzip, deflate,
6
+ * and brotli. Kept out of `message.ts` so the message parser stays a pure
7
+ * wire-format function with no dependency on zlib.
8
+ */
9
+ /** Content-encoding values we can decode — literal union, never bare `string`. */
10
+ export type ContentEncoding = "gzip" | "deflate" | "br";
11
+ /** Whether `value` is a content-encoding we know how to decode. */
12
+ export declare function isSupportedContentEncoding(value: string): value is ContentEncoding;
13
+ /**
14
+ * Decompress a body according to a `content-encoding` header value.
15
+ *
16
+ * For `deflate`, servers disagree on framing: some send a zlib-wrapped stream
17
+ * (what the RFC calls for), some send raw deflate. We try zlib first and fall
18
+ * back to raw inflate, matching the de-facto browser tolerance.
19
+ *
20
+ * @throws {ContentEncodingError} on an unsupported value or corrupt stream.
21
+ */
22
+ export declare function decompressBody(body: Uint8Array, encoding: string): Uint8Array;
23
+ //# sourceMappingURL=decompress.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decompress.d.ts","sourceRoot":"","sources":["../src/decompress.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,kFAAkF;AAClF,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;AAIxD,mEAAmE;AACnE,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,eAAe,CAElF;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,GAAG,UAAU,CAqB7E"}