@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 141 lines 5.26 KB
Raw
1 /**
2 * Request-body gunzip (DESIGN §4, §7).
3 *
4 * git's remote-curl **gzips POST bodies** (`Content-Encoding: gzip`) whenever
5 * the buffered body is under `http.postBuffer` (default 1 MiB) — i.e.
6 * essentially every fetch negotiation request. Workers do not auto-decompress
7 * request bodies, so the server must branch on `Content-Encoding: gzip` and
8 * pipe through `DecompressionStream("gzip")`, sniffing the `1f 8b` magic
9 * defensively (a raw pkt-line body always starts with an ASCII hex digit, so
10 * the sniff can never false-positive).
11 *
12 * This module is the only place `DecompressionStream` is used; the pack-entry
13 * zlib boundary lives in `Zlib.ts`.
14 */
15 import * as Effect from "effect/Effect";
16 import * as Pull from "effect/Pull";
17 import * as Schema from "effect/Schema";
18 import * as Stream from "effect/Stream";
19 import { concatBytes } from "./ObjectCodec.ts";
20
21 /**
22 * Error raised when a request body declared (or sniffed) as gzip fails to
23 * decompress, or when the upstream body stream fails while being piped
24 * through the decompressor.
25 */
26 export class GzipBodyError extends Schema.TaggedError<GzipBodyError>()("GzipBodyError", {
27 reason: Schema.String,
28 }) {}
29
30 /**
31 * Returns `true` when the bytes start with the gzip magic `1f 8b`.
32 */
33 export const sniffGzip = (bytes: Uint8Array): boolean =>
34 bytes.length >= 2 && bytes[0] === 0x1f && bytes[1] === 0x8b;
35
36 /**
37 * Returns `true` when a `Content-Encoding` header value declares gzip
38 * (handles `gzip`, `x-gzip`, and comma-separated lists).
39 */
40 export const isGzipContentEncoding = (value: string | undefined): boolean =>
41 value !== undefined &&
42 value.split(",").some((token) => {
43 const t = token.trim().toLowerCase();
44 return t === "gzip" || t === "x-gzip";
45 });
46
47 const errorReason = (error: unknown): string =>
48 error instanceof Error ? error.message : String(error);
49
50 /**
51 * Pipes a byte stream through `DecompressionStream("gzip")`.
52 *
53 * Upstream failures propagate through the web-stream pipe and surface as
54 * {@link GzipBodyError} (the typed upstream error is flattened — acceptable
55 * because the caller treats any body failure the same way).
56 */
57 export const gunzipStream = <E>(
58 input: Stream.Stream<Uint8Array, E>,
59 ): Stream.Stream<Uint8Array, GzipBodyError> =>
60 Stream.fromReadableStream({
61 evaluate: () =>
62 Stream.toReadableStream(input).pipeThrough(
63 // dom.d.ts types DecompressionStream's writable as BufferSource; the
64 // runtime accepts Uint8Array chunks — bridge the variance mismatch.
65 new DecompressionStream("gzip") as unknown as ReadableWritablePair<Uint8Array, Uint8Array>,
66 ) as ReadableStream<Uint8Array>,
67 onError: (error) => GzipBodyError.make({ reason: `gunzip failed: ${errorReason(error)}` }),
68 });
69
70 /**
71 * One-shot gunzip of a fully buffered body.
72 */
73 export const gunzipBuffer = (data: Uint8Array): Effect.Effect<Uint8Array, GzipBodyError> =>
74 gunzipStream(Stream.succeed(data)).pipe(
75 Stream.runCollect,
76 Effect.map((chunks) => concatBytes(chunks)),
77 );
78
79 /**
80 * Decodes a fully buffered git request body: gunzips when `Content-Encoding`
81 * says gzip **or** the body carries the `1f 8b` magic (defensive sniff —
82 * pkt-line bodies always start with an ASCII hex digit, so this can never
83 * misfire); otherwise returns the bytes unchanged.
84 */
85 export const decodeGitBodyBuffer = (
86 data: Uint8Array,
87 contentEncoding: string | undefined,
88 ): Effect.Effect<Uint8Array, GzipBodyError> =>
89 isGzipContentEncoding(contentEncoding) || sniffGzip(data)
90 ? gunzipBuffer(data)
91 : Effect.succeed(data);
92
93 /**
94 * Decodes a streaming git request body: when `Content-Encoding` declares
95 * gzip the whole stream pipes through the decompressor; otherwise the first
96 * bytes are peeked and the `1f 8b` sniff decides. Pass-through preserves the
97 * upstream error type; the gunzip path surfaces failures as
98 * {@link GzipBodyError}.
99 */
100 export const decodeGitBodyStream = <E>(
101 input: Stream.Stream<Uint8Array, E>,
102 contentEncoding: string | undefined,
103 ): Stream.Stream<Uint8Array, E | GzipBodyError> => {
104 if (isGzipContentEncoding(contentEncoding)) {
105 return gunzipStream(input);
106 }
107 return Stream.unwrap(
108 Effect.gen(function* () {
109 const pull = yield* Stream.toPull(input);
110 const head: Array<Uint8Array> = [];
111 let length = 0;
112 let ended = false;
113 while (length < 2 && !ended) {
114 yield* pull.pipe(
115 Effect.map((chunks) => {
116 for (const chunk of chunks) {
117 head.push(chunk);
118 length += chunk.length;
119 }
120 }),
121 Pull.catchDone(() =>
122 Effect.sync(() => {
123 ended = true;
124 }),
125 ),
126 );
127 }
128 const headBytes = concatBytes(head, length);
129 const remainder: Stream.Stream<Uint8Array, E> = ended
130 ? Stream.empty
131 : // `toPull`'s error channel is already Done-free, so ExcludeDone<E> = E;
132 // TS cannot reduce the conditional for a generic E, hence the cast.
133 (pull.pipe(Effect.succeed, Stream.fromPull) as Stream.Stream<Uint8Array, E>);
134 const full: Stream.Stream<Uint8Array, E> =
135 length === 0 ? remainder : Stream.concat(Stream.succeed(headBytes), remainder);
136 return sniffGzip(headBytes)
137 ? (gunzipStream(full) as Stream.Stream<Uint8Array, E | GzipBodyError>)
138 : full;
139 }),
140 );
141 };