@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 +21 -0
- package/README.md +60 -0
- package/dist/connection.d.ts +78 -0
- package/dist/connection.d.ts.map +1 -0
- package/dist/connection.js +423 -0
- package/dist/connection.js.map +1 -0
- package/dist/decompress.d.ts +23 -0
- package/dist/decompress.d.ts.map +1 -0
- package/dist/decompress.js +67 -0
- package/dist/decompress.js.map +1 -0
- package/dist/errors.d.ts +56 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +71 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -0
- package/dist/message.d.ts +59 -0
- package/dist/message.d.ts.map +1 -0
- package/dist/message.js +210 -0
- package/dist/message.js.map +1 -0
- package/dist/redirect.d.ts +49 -0
- package/dist/redirect.d.ts.map +1 -0
- package/dist/redirect.js +106 -0
- package/dist/redirect.js.map +1 -0
- package/dist/types.d.ts +127 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -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 +20 -0
- package/dist/utils.js.map +1 -0
- package/package.json +46 -0
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
import { gunzipSync, inflateSync, inflateRawSync, brotliDecompressSync } from "node:zlib";
|
|
10
|
+
import { ContentEncodingError } from "./errors.js";
|
|
11
|
+
const SUPPORTED_ENCODINGS = ["gzip", "deflate", "br"];
|
|
12
|
+
/** Whether `value` is a content-encoding we know how to decode. */
|
|
13
|
+
export function isSupportedContentEncoding(value) {
|
|
14
|
+
return SUPPORTED_ENCODINGS.includes(value);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Decompress a body according to a `content-encoding` header value.
|
|
18
|
+
*
|
|
19
|
+
* For `deflate`, servers disagree on framing: some send a zlib-wrapped stream
|
|
20
|
+
* (what the RFC calls for), some send raw deflate. We try zlib first and fall
|
|
21
|
+
* back to raw inflate, matching the de-facto browser tolerance.
|
|
22
|
+
*
|
|
23
|
+
* @throws {ContentEncodingError} on an unsupported value or corrupt stream.
|
|
24
|
+
*/
|
|
25
|
+
export function decompressBody(body, encoding) {
|
|
26
|
+
switch (encoding) {
|
|
27
|
+
case "gzip":
|
|
28
|
+
case "x-gzip":
|
|
29
|
+
return decompressWith((b) => gunzipSync(b), body, encoding);
|
|
30
|
+
case "deflate": {
|
|
31
|
+
// Try the RFC-mandated zlib-wrapped form first, then raw deflate.
|
|
32
|
+
try {
|
|
33
|
+
return decompressWith((b) => inflateSync(b), body, encoding);
|
|
34
|
+
}
|
|
35
|
+
catch (zlibErr) {
|
|
36
|
+
if (zlibErr instanceof ContentEncodingError) {
|
|
37
|
+
return decompressWith((b) => inflateRawSync(b), body, encoding);
|
|
38
|
+
}
|
|
39
|
+
throw zlibErr;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
case "br":
|
|
43
|
+
return decompressWith((b) => brotliDecompressSync(b), body, encoding);
|
|
44
|
+
default:
|
|
45
|
+
throw new ContentEncodingError(encoding);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** Run a zlib sync decoder, normalizing its Buffer output to a Uint8Array. */
|
|
49
|
+
function runDecoder(fn, body) {
|
|
50
|
+
const out = fn(body);
|
|
51
|
+
return out instanceof Uint8Array ? out : new Uint8Array(out);
|
|
52
|
+
}
|
|
53
|
+
/** Run a decoder and wrap any failure in a typed {@link ContentEncodingError}. */
|
|
54
|
+
function decompressWith(fn, body, encoding) {
|
|
55
|
+
try {
|
|
56
|
+
return runDecoder(fn, body);
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
// exactOptionalPropertyTypes: only set `cause` when we have one.
|
|
60
|
+
const cause = err instanceof Error ? err : undefined;
|
|
61
|
+
if (cause !== undefined) {
|
|
62
|
+
throw new ContentEncodingError(encoding, { cause });
|
|
63
|
+
}
|
|
64
|
+
throw new ContentEncodingError(encoding);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=decompress.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decompress.js","sourceRoot":"","sources":["../src/decompress.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAC1F,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAKnD,MAAM,mBAAmB,GAA+B,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;AAElF,mEAAmE;AACnE,MAAM,UAAU,0BAA0B,CAAC,KAAa;IACpD,OAAQ,mBAAyC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;AACtE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAAC,IAAgB,EAAE,QAAgB;IAC7D,QAAQ,QAAQ,EAAE,CAAC;QACf,KAAK,MAAM,CAAC;QACZ,KAAK,QAAQ;YACT,OAAO,cAAc,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;QAChE,KAAK,SAAS,CAAC,CAAC,CAAC;YACb,kEAAkE;YAClE,IAAI,CAAC;gBACD,OAAO,cAAc,CAAC,CAAC,CAAC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AAI,oBAAoB,CAAC,QAAQ,CAAC,CAAC;IAC7C,CAAC;AACL,CAAC"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed errors for @browsercore/http1.
|
|
3
|
+
*
|
|
4
|
+
* Errors are part of the API — every failure mode is an explicit type so callers
|
|
5
|
+
* can match on `kind` instead of parsing messages.
|
|
6
|
+
*/
|
|
7
|
+
/** Base class for all HTTP/1.1 errors. */
|
|
8
|
+
export declare class Http1Error extends Error {
|
|
9
|
+
readonly kind: "Http1Error";
|
|
10
|
+
readonly cause: Error | undefined;
|
|
11
|
+
constructor(message: string, options?: {
|
|
12
|
+
cause?: Error;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
/** Redirect chain exceeded {@link Http1Options.maxRedirects}. */
|
|
16
|
+
export declare class RedirectLimitError extends Error {
|
|
17
|
+
readonly kind: "RedirectLimitError";
|
|
18
|
+
readonly limit: number;
|
|
19
|
+
/** The URLs visited so far, in order — useful for debugging redirect loops. */
|
|
20
|
+
readonly trail: readonly string[];
|
|
21
|
+
readonly cause: Error | undefined;
|
|
22
|
+
constructor(limit: number, trail: readonly string[], options?: {
|
|
23
|
+
cause?: Error;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** The remote sent bytes that could not be parsed as a valid HTTP/1.1 response. */
|
|
27
|
+
export declare class InvalidResponseError extends Error {
|
|
28
|
+
readonly kind: "InvalidResponseError";
|
|
29
|
+
/** The raw bytes that failed to parse — truncated to a sane length for logging. */
|
|
30
|
+
readonly rawPreview: string;
|
|
31
|
+
readonly cause: Error | undefined;
|
|
32
|
+
constructor(rawPreview: string, options?: {
|
|
33
|
+
cause?: Error;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
/** The response used a `content-encoding` this client cannot decode. */
|
|
37
|
+
export declare class ContentEncodingError extends Error {
|
|
38
|
+
readonly kind: "ContentEncodingError";
|
|
39
|
+
/** The unsupported (or corrupt) content-encoding token. */
|
|
40
|
+
readonly encoding: string;
|
|
41
|
+
readonly cause: Error | undefined;
|
|
42
|
+
constructor(encoding: string, options?: {
|
|
43
|
+
cause?: Error;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/** A chunked transfer-encoding body was malformed. */
|
|
47
|
+
export declare class ChunkEncodingError extends Error {
|
|
48
|
+
readonly kind: "ChunkEncodingError";
|
|
49
|
+
/** Byte offset in the body stream where the malformed chunk was detected. */
|
|
50
|
+
readonly offset: number;
|
|
51
|
+
readonly cause: Error | undefined;
|
|
52
|
+
constructor(offset: number, options?: {
|
|
53
|
+
cause?: Error;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,0CAA0C;AAC1C,qBAAa,UAAW,SAAQ,KAAK;IACjC,SAAgB,IAAI,EAAG,YAAY,CAAU;IAC7C,SAAyB,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;gBAEtC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,KAAK,CAAA;KAAE;CAK3D;AAED,iEAAiE;AACjE,qBAAa,kBAAmB,SAAQ,KAAK;IACzC,SAAgB,IAAI,EAAG,oBAAoB,CAAU;IACrD,SAAgB,KAAK,EAAE,MAAM,CAAC;IAC9B,+EAA+E;IAC/E,SAAgB,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;IACzC,SAAyB,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;gBAEtC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,KAAK,CAAA;KAAE;CAOnF;AAED,mFAAmF;AACnF,qBAAa,oBAAqB,SAAQ,KAAK;IAC3C,SAAgB,IAAI,EAAG,sBAAsB,CAAU;IACvD,mFAAmF;IACnF,SAAgB,UAAU,EAAE,MAAM,CAAC;IACnC,SAAyB,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;gBAEtC,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,KAAK,CAAA;KAAE;CAM9D;AAED,wEAAwE;AACxE,qBAAa,oBAAqB,SAAQ,KAAK;IAC3C,SAAgB,IAAI,EAAG,sBAAsB,CAAU;IACvD,2DAA2D;IAC3D,SAAgB,QAAQ,EAAE,MAAM,CAAC;IACjC,SAAyB,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;gBAEtC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,KAAK,CAAA;KAAE;CAM5D;AAED,sDAAsD;AACtD,qBAAa,kBAAmB,SAAQ,KAAK;IACzC,SAAgB,IAAI,EAAG,oBAAoB,CAAU;IACrD,6EAA6E;IAC7E,SAAgB,MAAM,EAAE,MAAM,CAAC;IAC/B,SAAyB,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC;gBAEtC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,KAAK,CAAA;KAAE;CAM1D"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed errors for @browsercore/http1.
|
|
3
|
+
*
|
|
4
|
+
* Errors are part of the API — every failure mode is an explicit type so callers
|
|
5
|
+
* can match on `kind` instead of parsing messages.
|
|
6
|
+
*/
|
|
7
|
+
/** Base class for all HTTP/1.1 errors. */
|
|
8
|
+
export class Http1Error extends Error {
|
|
9
|
+
kind = "Http1Error";
|
|
10
|
+
cause;
|
|
11
|
+
constructor(message, options) {
|
|
12
|
+
super(message, options);
|
|
13
|
+
this.name = new.target.name;
|
|
14
|
+
this.cause = options?.cause;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** Redirect chain exceeded {@link Http1Options.maxRedirects}. */
|
|
18
|
+
export class RedirectLimitError extends Error {
|
|
19
|
+
kind = "RedirectLimitError";
|
|
20
|
+
limit;
|
|
21
|
+
/** The URLs visited so far, in order — useful for debugging redirect loops. */
|
|
22
|
+
trail;
|
|
23
|
+
cause;
|
|
24
|
+
constructor(limit, trail, options) {
|
|
25
|
+
super(`Redirect limit of ${limit} exceeded after: ${trail.join(" → ")}`);
|
|
26
|
+
this.name = "RedirectLimitError";
|
|
27
|
+
this.limit = limit;
|
|
28
|
+
this.trail = trail;
|
|
29
|
+
this.cause = options?.cause;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** The remote sent bytes that could not be parsed as a valid HTTP/1.1 response. */
|
|
33
|
+
export class InvalidResponseError extends Error {
|
|
34
|
+
kind = "InvalidResponseError";
|
|
35
|
+
/** The raw bytes that failed to parse — truncated to a sane length for logging. */
|
|
36
|
+
rawPreview;
|
|
37
|
+
cause;
|
|
38
|
+
constructor(rawPreview, options) {
|
|
39
|
+
super(`Invalid HTTP/1.1 response: ${rawPreview}`);
|
|
40
|
+
this.name = "InvalidResponseError";
|
|
41
|
+
this.rawPreview = rawPreview;
|
|
42
|
+
this.cause = options?.cause;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/** The response used a `content-encoding` this client cannot decode. */
|
|
46
|
+
export class ContentEncodingError extends Error {
|
|
47
|
+
kind = "ContentEncodingError";
|
|
48
|
+
/** The unsupported (or corrupt) content-encoding token. */
|
|
49
|
+
encoding;
|
|
50
|
+
cause;
|
|
51
|
+
constructor(encoding, options) {
|
|
52
|
+
super(`Unsupported or corrupt content-encoding: ${encoding}`);
|
|
53
|
+
this.name = "ContentEncodingError";
|
|
54
|
+
this.encoding = encoding;
|
|
55
|
+
this.cause = options?.cause;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** A chunked transfer-encoding body was malformed. */
|
|
59
|
+
export class ChunkEncodingError extends Error {
|
|
60
|
+
kind = "ChunkEncodingError";
|
|
61
|
+
/** Byte offset in the body stream where the malformed chunk was detected. */
|
|
62
|
+
offset;
|
|
63
|
+
cause;
|
|
64
|
+
constructor(offset, options) {
|
|
65
|
+
super(`Malformed chunked encoding at offset ${offset}`);
|
|
66
|
+
this.name = "ChunkEncodingError";
|
|
67
|
+
this.offset = offset;
|
|
68
|
+
this.cause = options?.cause;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,0CAA0C;AAC1C,MAAM,OAAO,UAAW,SAAQ,KAAK;IACjB,IAAI,GAAG,YAAqB,CAAC;IACpB,KAAK,CAAoB;IAElD,YAAY,OAAe,EAAE,OAA2B;QACpD,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC;QAC5B,IAAI,CAAC,KAAK,GAAG,OAAO,EAAE,KAAK,CAAC;IAChC,CAAC;CACJ;AAED,iEAAiE;AACjE,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IACzB,IAAI,GAAG,oBAA6B,CAAC;IACrC,KAAK,CAAS;IAC9B,+EAA+E;IAC/D,KAAK,CAAoB;IAChB,KAAK,CAAoB;IAElD,YAAY,KAAa,EAAE,KAAwB,EAAE,OAA2B;QAC5E,KAAK,CAAC,qBAAqB,KAAK,oBAAoB,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACzE,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACjC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,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DED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @browsercore/http1 — public API surface.
|
|
3
|
+
*
|
|
4
|
+
* HTTP/1.1 client over any duplex byte stream. No knowledge of TLS or TCP.
|
|
5
|
+
* Higher layers (cookies, fetch, profiles) compose exclusively through these exports.
|
|
6
|
+
*/
|
|
7
|
+
export { connectHttp1, Http1ConnectionImpl } from "./connection.js";
|
|
8
|
+
export { ChunkEncodingError, ContentEncodingError, Http1Error, InvalidResponseError, RedirectLimitError, } from "./errors.js";
|
|
9
|
+
export { type Headers, type ParseResponseResult, type StartLine, parseChunkedEncoding, parseResponse, serializeRequest, } from "./message.js";
|
|
10
|
+
export { decompressBody, isSupportedContentEncoding, type ContentEncoding } from "./decompress.js";
|
|
11
|
+
export { followRedirects, isRedirectStatus, resolveRedirectUrl, type FollowRedirectsOptions, type RedirectStatusCode, } from "./redirect.js";
|
|
12
|
+
export { type Http1CloseReason, type Http1Connection, type Http1ConnectionId, type Http1ConnectionState, type Http1Options, type HttpBodyKind, type HttpMethod, type HttpRequest, type HttpResponse, type CookieInterceptor, type CookieUrl, } from "./types.js";
|
|
13
|
+
export { assertNever } from "./utils.js";
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEpE,OAAO,EACH,kBAAkB,EAClB,oBAAoB,EACpB,UAAU,EACV,oBAAoB,EACpB,kBAAkB,GACrB,MAAM,aAAa,CAAC;AAErB,OAAO,EACH,KAAK,OAAO,EACZ,KAAK,mBAAmB,EACxB,KAAK,SAAS,EACd,oBAAoB,EACpB,aAAa,EACb,gBAAgB,GACnB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,cAAc,EAAE,0BAA0B,EAAE,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEnG,OAAO,EACH,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,KAAK,sBAAsB,EAC3B,KAAK,kBAAkB,GAC1B,MAAM,eAAe,CAAC;AAEvB,OAAO,EACH,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,KAAK,oBAAoB,EACzB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,SAAS,GACjB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @browsercore/http1 — public API surface.
|
|
3
|
+
*
|
|
4
|
+
* HTTP/1.1 client over any duplex byte stream. No knowledge of TLS or TCP.
|
|
5
|
+
* Higher layers (cookies, fetch, profiles) compose exclusively through these exports.
|
|
6
|
+
*/
|
|
7
|
+
export { connectHttp1, Http1ConnectionImpl } from "./connection.js";
|
|
8
|
+
export { ChunkEncodingError, ContentEncodingError, Http1Error, InvalidResponseError, RedirectLimitError, } from "./errors.js";
|
|
9
|
+
export { parseChunkedEncoding, parseResponse, serializeRequest, } from "./message.js";
|
|
10
|
+
export { decompressBody, isSupportedContentEncoding } from "./decompress.js";
|
|
11
|
+
export { followRedirects, isRedirectStatus, resolveRedirectUrl, } from "./redirect.js";
|
|
12
|
+
export {} from "./types.js";
|
|
13
|
+
export { assertNever } from "./utils.js";
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEpE,OAAO,EACH,kBAAkB,EAClB,oBAAoB,EACpB,UAAU,EACV,oBAAoB,EACpB,kBAAkB,GACrB,MAAM,aAAa,CAAC;AAErB,OAAO,EAIH,oBAAoB,EACpB,aAAa,EACb,gBAAgB,GACnB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,cAAc,EAAE,0BAA0B,EAAwB,MAAM,iBAAiB,CAAC;AAEnG,OAAO,EACH,eAAe,EACf,gBAAgB,EAChB,kBAAkB,GAGrB,MAAM,eAAe,CAAC;AAEvB,OAAO,EAYN,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP/1.1 message parser and serializer.
|
|
3
|
+
*
|
|
4
|
+
* Pure protocol logic — no I/O. Takes and returns `Uint8Array` so it can be
|
|
5
|
+
* tested in isolation and composed over any byte stream.
|
|
6
|
+
*/
|
|
7
|
+
import type { HttpRequest, HttpResponse } from "./types.js";
|
|
8
|
+
/** The start-line of an HTTP/1.1 message — discriminated by direction. */
|
|
9
|
+
export type StartLine = {
|
|
10
|
+
readonly kind: "request";
|
|
11
|
+
readonly method: string;
|
|
12
|
+
readonly target: string;
|
|
13
|
+
readonly version: string;
|
|
14
|
+
} | {
|
|
15
|
+
readonly kind: "status";
|
|
16
|
+
readonly version: string;
|
|
17
|
+
readonly statusCode: number;
|
|
18
|
+
readonly statusText: string;
|
|
19
|
+
};
|
|
20
|
+
/** Parsed headers — case-insensitive map. */
|
|
21
|
+
export type Headers = ReadonlyMap<string, string>;
|
|
22
|
+
/** Parse result for a response — includes bytes consumed so the caller can drain. */
|
|
23
|
+
export interface ParseResponseResult {
|
|
24
|
+
readonly response: HttpResponse;
|
|
25
|
+
readonly bytesConsumed: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Serialize an HTTP/1.1 request into wire bytes.
|
|
29
|
+
*
|
|
30
|
+
* Produces `request-line CRLF headers CRLF body` per RFC 7230. The body is
|
|
31
|
+
* appended verbatim — chunking and content-length are the caller's concern.
|
|
32
|
+
*/
|
|
33
|
+
export declare function serializeRequest(req: HttpRequest): Uint8Array;
|
|
34
|
+
/**
|
|
35
|
+
* Parse a raw HTTP/1.1 response from a buffer.
|
|
36
|
+
*
|
|
37
|
+
* Returns the parsed response plus the number of bytes consumed so the caller
|
|
38
|
+
* can drain the buffer before the next message (keep-alive).
|
|
39
|
+
*
|
|
40
|
+
* @throws {InvalidResponseError} if the bytes cannot be parsed as a response.
|
|
41
|
+
*/
|
|
42
|
+
export declare function parseResponse(buf: Uint8Array): ParseResponseResult;
|
|
43
|
+
/**
|
|
44
|
+
* Decode a chunked transfer-encoding body stream.
|
|
45
|
+
*
|
|
46
|
+
* Reads from an `AsyncIterable<Uint8Array>` of the raw body bytes (after the
|
|
47
|
+
* headers) and yields the decoded content bytes chunk by chunk. Parses the
|
|
48
|
+
* hex chunk-size line (with optional `;` extensions), verifies the per-chunk
|
|
49
|
+
* `CRLF`, and stops at the terminating zero chunk — consuming any optional
|
|
50
|
+
* trailers and the final blank line.
|
|
51
|
+
*
|
|
52
|
+
* Streaming: a chunk is yielded as soon as its full `size + CRLF` is available
|
|
53
|
+
* in the buffer, so a producer can pipe network bytes through without buffering
|
|
54
|
+
* the whole body.
|
|
55
|
+
*
|
|
56
|
+
* @throws {ChunkEncodingError} (with `offset`) if a chunk is malformed.
|
|
57
|
+
*/
|
|
58
|
+
export declare function parseChunkedEncoding(stream: AsyncIterable<Uint8Array>): AsyncGenerator<Uint8Array>;
|
|
59
|
+
//# sourceMappingURL=message.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message.d.ts","sourceRoot":"","sources":["../src/message.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAI5D,0EAA0E;AAC1E,MAAM,MAAM,SAAS,GACf;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACxG;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtH,6CAA6C;AAC7C,MAAM,MAAM,OAAO,GAAG,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAElD,qFAAqF;AACrF,MAAM,WAAW,mBAAmB;IAChC,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;IAChC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;CAClC;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,WAAW,GAAG,UAAU,CAwB7D;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,UAAU,GAAG,mBAAmB,CAsDlE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAuB,oBAAoB,CAAC,MAAM,EAAE,aAAa,CAAC,UAAU,CAAC,GAAG,cAAc,CAAC,UAAU,CAAC,CAmFzG"}
|
package/dist/message.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP/1.1 message parser and serializer.
|
|
3
|
+
*
|
|
4
|
+
* Pure protocol logic — no I/O. Takes and returns `Uint8Array` so it can be
|
|
5
|
+
* tested in isolation and composed over any byte stream.
|
|
6
|
+
*/
|
|
7
|
+
import { InvalidResponseError, ChunkEncodingError } from "./errors.js";
|
|
8
|
+
import { assertNever } from "./utils.js";
|
|
9
|
+
/**
|
|
10
|
+
* Serialize an HTTP/1.1 request into wire bytes.
|
|
11
|
+
*
|
|
12
|
+
* Produces `request-line CRLF headers CRLF body` per RFC 7230. The body is
|
|
13
|
+
* appended verbatim — chunking and content-length are the caller's concern.
|
|
14
|
+
*/
|
|
15
|
+
export function serializeRequest(req) {
|
|
16
|
+
const lines = [];
|
|
17
|
+
lines.push(`${req.method} ${req.url} HTTP/1.1`);
|
|
18
|
+
for (const [name, value] of req.headers) {
|
|
19
|
+
// node:http lowercases header names on the wire (RFC 7230 §3.2); match it.
|
|
20
|
+
lines.push(`${name.toLowerCase()}: ${value}`);
|
|
21
|
+
}
|
|
22
|
+
lines.push("");
|
|
23
|
+
lines.push("");
|
|
24
|
+
const headerBytes = Buffer.from(lines.join("\r\n"), "ascii");
|
|
25
|
+
switch (req.body.kind) {
|
|
26
|
+
case "empty":
|
|
27
|
+
return new Uint8Array(headerBytes);
|
|
28
|
+
case "bytes": {
|
|
29
|
+
const out = new Uint8Array(headerBytes.length + req.body.data.length);
|
|
30
|
+
out.set(new Uint8Array(headerBytes), 0);
|
|
31
|
+
out.set(req.body.data, headerBytes.length);
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
case "stream":
|
|
35
|
+
throw new Error("streaming body not implemented — see PLAN.md");
|
|
36
|
+
default:
|
|
37
|
+
assertNever(req.body);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Parse a raw HTTP/1.1 response from a buffer.
|
|
42
|
+
*
|
|
43
|
+
* Returns the parsed response plus the number of bytes consumed so the caller
|
|
44
|
+
* can drain the buffer before the next message (keep-alive).
|
|
45
|
+
*
|
|
46
|
+
* @throws {InvalidResponseError} if the bytes cannot be parsed as a response.
|
|
47
|
+
*/
|
|
48
|
+
export function parseResponse(buf) {
|
|
49
|
+
const text = Buffer.from(buf).toString("ascii");
|
|
50
|
+
const headerEnd = text.indexOf("\r\n\r\n");
|
|
51
|
+
if (headerEnd === -1) {
|
|
52
|
+
throw new InvalidResponseError(text.slice(0, 120));
|
|
53
|
+
}
|
|
54
|
+
const headerSection = text.slice(0, headerEnd);
|
|
55
|
+
const bodyStart = headerEnd + 4;
|
|
56
|
+
const lines = headerSection.split("\r\n");
|
|
57
|
+
const statusLine = lines[0];
|
|
58
|
+
if (statusLine === undefined) {
|
|
59
|
+
throw new InvalidResponseError(text.slice(0, 120));
|
|
60
|
+
}
|
|
61
|
+
const statusMatch = /^HTTP\/\d\.\d\s+(\d{3})\s+(.*)$/.exec(statusLine);
|
|
62
|
+
if (statusMatch === null) {
|
|
63
|
+
throw new InvalidResponseError(text.slice(0, 120));
|
|
64
|
+
}
|
|
65
|
+
const statusCodeStr = statusMatch[1] ?? "0";
|
|
66
|
+
const statusText = statusMatch[2] ?? "";
|
|
67
|
+
const statusCode = Number(statusCodeStr);
|
|
68
|
+
if (statusCode < 100 || statusCode > 999) {
|
|
69
|
+
throw new InvalidResponseError(text.slice(0, 120));
|
|
70
|
+
}
|
|
71
|
+
const headers = new Map();
|
|
72
|
+
for (let i = 1; i < lines.length; i++) {
|
|
73
|
+
const line = lines[i];
|
|
74
|
+
if (line === undefined)
|
|
75
|
+
continue;
|
|
76
|
+
const colon = line.indexOf(":");
|
|
77
|
+
if (colon === -1)
|
|
78
|
+
continue;
|
|
79
|
+
const name = line.slice(0, colon).trim().toLowerCase();
|
|
80
|
+
const value = line.slice(colon + 1).trim();
|
|
81
|
+
headers.set(name, value);
|
|
82
|
+
}
|
|
83
|
+
const contentLengthHeader = headers.get("content-length");
|
|
84
|
+
const transferEncoding = headers.get("transfer-encoding");
|
|
85
|
+
let body;
|
|
86
|
+
if (transferEncoding !== undefined && transferEncoding.includes("chunked")) {
|
|
87
|
+
// Chunked decoding is handled by parseChunkedEncoding — here we just
|
|
88
|
+
// return the raw body bytes for the caller to feed through that.
|
|
89
|
+
body = buf.slice(bodyStart);
|
|
90
|
+
}
|
|
91
|
+
else if (contentLengthHeader !== undefined) {
|
|
92
|
+
const contentLength = Number(contentLengthHeader);
|
|
93
|
+
body = buf.slice(bodyStart, bodyStart + contentLength);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
body = buf.slice(bodyStart);
|
|
97
|
+
}
|
|
98
|
+
const response = {
|
|
99
|
+
statusCode,
|
|
100
|
+
statusText,
|
|
101
|
+
headers,
|
|
102
|
+
body,
|
|
103
|
+
};
|
|
104
|
+
const bytesConsumed = bodyStart + body.length;
|
|
105
|
+
return { response, bytesConsumed };
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Decode a chunked transfer-encoding body stream.
|
|
109
|
+
*
|
|
110
|
+
* Reads from an `AsyncIterable<Uint8Array>` of the raw body bytes (after the
|
|
111
|
+
* headers) and yields the decoded content bytes chunk by chunk. Parses the
|
|
112
|
+
* hex chunk-size line (with optional `;` extensions), verifies the per-chunk
|
|
113
|
+
* `CRLF`, and stops at the terminating zero chunk — consuming any optional
|
|
114
|
+
* trailers and the final blank line.
|
|
115
|
+
*
|
|
116
|
+
* Streaming: a chunk is yielded as soon as its full `size + CRLF` is available
|
|
117
|
+
* in the buffer, so a producer can pipe network bytes through without buffering
|
|
118
|
+
* the whole body.
|
|
119
|
+
*
|
|
120
|
+
* @throws {ChunkEncodingError} (with `offset`) if a chunk is malformed.
|
|
121
|
+
*/
|
|
122
|
+
export async function* parseChunkedEncoding(stream) {
|
|
123
|
+
// Bytes buffered from the stream but not yet parsed into a complete chunk.
|
|
124
|
+
let buffer = new Uint8Array(0);
|
|
125
|
+
// Total body bytes consumed so far — reported as the error offset.
|
|
126
|
+
let consumed = 0;
|
|
127
|
+
const append = (chunk) => {
|
|
128
|
+
if (chunk.length === 0)
|
|
129
|
+
return;
|
|
130
|
+
const next = new Uint8Array(buffer.length + chunk.length);
|
|
131
|
+
next.set(buffer, 0);
|
|
132
|
+
next.set(chunk, buffer.length);
|
|
133
|
+
buffer = next;
|
|
134
|
+
};
|
|
135
|
+
// Find the next `CRLF` at or after `start`; return the index of the `CR`.
|
|
136
|
+
const findCrlf = (start) => {
|
|
137
|
+
for (let i = start; i + 1 < buffer.length; i++) {
|
|
138
|
+
if (buffer[i] === 0x0d && buffer[i + 1] === 0x0a)
|
|
139
|
+
return i;
|
|
140
|
+
}
|
|
141
|
+
return -1;
|
|
142
|
+
};
|
|
143
|
+
// Consume the trailer section (zero or more header lines + blank line).
|
|
144
|
+
// Returns the offset just past the terminating blank line, or -1 if the
|
|
145
|
+
// buffer does not yet hold the full trailer section.
|
|
146
|
+
const consumeTrailers = (start) => {
|
|
147
|
+
let lineStart = start;
|
|
148
|
+
for (let i = start; i + 1 < buffer.length; i++) {
|
|
149
|
+
if (buffer[i] !== 0x0d || buffer[i + 1] !== 0x0a)
|
|
150
|
+
continue;
|
|
151
|
+
if (i === lineStart) {
|
|
152
|
+
// Blank line — end of trailers.
|
|
153
|
+
return i + 2;
|
|
154
|
+
}
|
|
155
|
+
lineStart = i + 2;
|
|
156
|
+
}
|
|
157
|
+
return -1;
|
|
158
|
+
};
|
|
159
|
+
for await (const chunk of stream) {
|
|
160
|
+
append(chunk);
|
|
161
|
+
// Emit as many complete chunks as the buffer currently holds.
|
|
162
|
+
// Loop because a single pushed chunk may contain several full chunks.
|
|
163
|
+
drain: while (true) {
|
|
164
|
+
const lineEnd = findCrlf(0);
|
|
165
|
+
if (lineEnd === -1)
|
|
166
|
+
break drain; // Need more bytes for the size line.
|
|
167
|
+
const sizeLine = decodeAscii(buffer, 0, lineEnd);
|
|
168
|
+
// chunk-size [ ";" chunk-ext ] — hex digits then optional extension.
|
|
169
|
+
// exec returns null (not undefined) when there is no match.
|
|
170
|
+
if (!/^[0-9a-fA-F]+(?:;[^\r\n]*)?$/.test(sizeLine)) {
|
|
171
|
+
throw new ChunkEncodingError(consumed);
|
|
172
|
+
}
|
|
173
|
+
const size = Number.parseInt(sizeLine, 16);
|
|
174
|
+
const dataStart = lineEnd + 2;
|
|
175
|
+
if (size === 0) {
|
|
176
|
+
// Terminating chunk is just `0\r\n`; optional trailers + a final
|
|
177
|
+
// blank line follow. dataStart points past the `0\r\n`.
|
|
178
|
+
const trailerEnd = consumeTrailers(dataStart);
|
|
179
|
+
if (trailerEnd === -1)
|
|
180
|
+
break drain; // Need more bytes for trailers.
|
|
181
|
+
consumed += trailerEnd;
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const dataEnd = dataStart + size;
|
|
185
|
+
const chunkEnd = dataEnd + 2; // trailing CRLF after the chunk data
|
|
186
|
+
if (chunkEnd > buffer.length)
|
|
187
|
+
break drain; // Need more data bytes.
|
|
188
|
+
// The two bytes after the chunk data must be the required CRLF.
|
|
189
|
+
if (buffer[dataEnd] !== 0x0d || buffer[dataEnd + 1] !== 0x0a) {
|
|
190
|
+
throw new ChunkEncodingError(consumed + dataEnd);
|
|
191
|
+
}
|
|
192
|
+
yield buffer.slice(dataStart, dataEnd);
|
|
193
|
+
consumed += chunkEnd;
|
|
194
|
+
buffer = buffer.slice(chunkEnd);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
// Stream ended before the terminating zero chunk arrived.
|
|
198
|
+
throw new ChunkEncodingError(consumed);
|
|
199
|
+
}
|
|
200
|
+
/** Decode a slice of bytes as ASCII without going through `Buffer`. */
|
|
201
|
+
function decodeAscii(buf, start, end) {
|
|
202
|
+
let out = "";
|
|
203
|
+
for (let i = start; i < end; i++) {
|
|
204
|
+
out += String.fromCharCode(buf[i]);
|
|
205
|
+
}
|
|
206
|
+
return out;
|
|
207
|
+
}
|
|
208
|
+
void assertNever;
|
|
209
|
+
void InvalidResponseError;
|
|
210
|
+
//# sourceMappingURL=message.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message.js","sourceRoot":"","sources":["../src/message.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAgBzC;;;;;GAKG;AACH,M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I,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAE,CAAC,CAAC;IACxC,CAAC;IACD,OAAO,GAAG,CAAC;AACf,CAAC;AAED,KAAK,WAAW,CAAC;AACjB,KAAK,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP redirect following — protocol-level capability for @browsercore/http1.
|
|
3
|
+
*
|
|
4
|
+
* Design note: this logic is intentionally a STANDABLE exported function and is
|
|
5
|
+
* NOT wired into {@link Http1ConnectionImpl.request}. The @browsercore/fetch
|
|
6
|
+
* package implements its own redirect following (and connection-pool migration
|
|
7
|
+
* across origins) in `src/client.ts`. Auto-following here would double-follow
|
|
8
|
+
* against fetch. Instead, `followRedirects` gives a clean, tested capability
|
|
9
|
+
* that fetch (or any caller) can reuse, without changing current fetch behavior.
|
|
10
|
+
*
|
|
11
|
+
* Method/body rewrite follows RFC 7231:
|
|
12
|
+
* - 303 See Other -> GET, body stripped.
|
|
13
|
+
* - 307/308 -> method + body preserved.
|
|
14
|
+
* - 301/302 -> method + body preserved (RFC-correct; browsers
|
|
15
|
+
* historically GET-ify, but the spec does not).
|
|
16
|
+
*/
|
|
17
|
+
import type { Http1Connection, HttpRequest, HttpResponse } from "./types.js";
|
|
18
|
+
/** Status codes that trigger redirect handling. */
|
|
19
|
+
export declare const REDIRECT_STATUS_CODES: readonly [301, 302, 303, 307, 308];
|
|
20
|
+
/** Redirect status code union — exhaustive over REDIRECT_STATUS_CODES. */
|
|
21
|
+
export type RedirectStatusCode = (typeof REDIRECT_STATUS_CODES)[number];
|
|
22
|
+
/** Options for {@link followRedirects}. */
|
|
23
|
+
export interface FollowRedirectsOptions {
|
|
24
|
+
/** Maximum number of redirects to follow before raising. Default 20. */
|
|
25
|
+
readonly maxRedirects?: number;
|
|
26
|
+
}
|
|
27
|
+
/** Whether `status` is one of the redirect-triggering status codes. */
|
|
28
|
+
export declare function isRedirectStatus(status: number): status is RedirectStatusCode;
|
|
29
|
+
/**
|
|
30
|
+
* Resolve a possibly-relative `Location` header against the current absolute
|
|
31
|
+
* URL. A Location with its own scheme is returned as-is.
|
|
32
|
+
*/
|
|
33
|
+
export declare function resolveRedirectUrl(currentUrl: string, location: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Follow redirects for a request, returning the final response.
|
|
36
|
+
*
|
|
37
|
+
* The first request is dispatched as given; each redirect response is resolved
|
|
38
|
+
* against the URL of the request that produced it. The chain stops at the
|
|
39
|
+
* first non-redirect response, a redirect with no `Location`, or when the
|
|
40
|
+
* limit / a loop is detected.
|
|
41
|
+
*
|
|
42
|
+
* @param conn The connection to dispatch each hop on (same-origin redirects).
|
|
43
|
+
* @param req The initial request (carries the request target in `url`).
|
|
44
|
+
* @param currentUrl The absolute URL `req` was sent to — needed because
|
|
45
|
+
* `HttpRequest.url` is only the request target, not a full URL.
|
|
46
|
+
* @throws {RedirectLimitError} on exceeding `maxRedirects` or a repeated URL.
|
|
47
|
+
*/
|
|
48
|
+
export declare function followRedirects(conn: Http1Connection, req: HttpRequest, currentUrl: string, options?: FollowRedirectsOptions): Promise<HttpResponse>;
|
|
49
|
+
//# sourceMappingURL=redirect.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redirect.d.ts","sourceRoot":"","sources":["../src/redirect.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAG7E,mDAAmD;AACnD,eAAO,MAAM,qBAAqB,oCAAqC,CAAC;AAExE,0EAA0E;AAC1E,MAAM,MAAM,kBAAkB,GAAG,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,CAAC,CAAC;AAExE,2CAA2C;AAC3C,MAAM,WAAW,sBAAsB;IACnC,wEAAwE;IACxE,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;CAClC;AAED,uEAAuE;AACvE,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,IAAI,kBAAkB,CAE7E;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAE/E;AAkCD;;;;;;;;;;;;;GAaG;AACH,wBAAsB,eAAe,CACjC,IAAI,EAAE,eAAe,EACrB,GAAG,EAAE,WAAW,EAChB,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE,sBAA2B,GACrC,OAAO,CAAC,YAAY,CAAC,CAgCvB"}
|