@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, 06:00 UTC

main
ts 181 lines 7.14 KB
Raw
1 import crypto from "node:crypto";
2 import zlib from "node:zlib";
3 /**
4 * Probe fixture: what does THIS workerd's `node:zlib` offer for a
5 * synchronous, exact-span inflate, and what does each path cost? (See
6 * `packages/git/src/Protocol/Zlib.ts`.) `?n=<count>` runs each path that many times so
7 * the CLIENT can measure wall time — workerd freezes `performance.now()`
8 * during synchronous work, so the worker cannot time itself.
9 */
10 import * as Cloudflare from "@alchemy.run/cloudflare";
11 import * as Effect from "effect/Effect";
12 import { HttpServerRequest } from "effect/http/HttpServerRequest";
13 import * as HttpServerResponse from "effect/http/HttpServerResponse";
14
15 const streamInflate = (z: Uint8Array) =>
16 new Promise<number>((resolve, reject) => {
17 const engine = zlib.createInflate();
18 let out = 0;
19 engine.on("data", (chunk: Uint8Array) => {
20 out += chunk.length;
21 });
22 engine.on("end", () => resolve(out));
23 engine.on("error", reject);
24 engine.end(z);
25 });
26
27 export default class ZlibProbeWorker extends Cloudflare.Worker<ZlibProbeWorker>()(
28 "ZlibProbeWorker",
29 { main: import.meta.url },
30 Effect.gen(function* () {
31 return {
32 fetch: Effect.gen(function* () {
33 const request = yield* HttpServerRequest;
34 const url = new URL(request.url, "http://x");
35 const n = Number(url.searchParams.get("n") ?? "0");
36 const which = url.searchParams.get("path") ?? "probe";
37 const content = new TextEncoder().encode("hello ".repeat(500));
38 const z = new Uint8Array(zlib.deflateSync(content));
39 const withTrailer = new Uint8Array(z.length + 7);
40 withTrailer.set(z, 0);
41 withTrailer.set([1, 2, 3, 4, 5, 6, 7], z.length);
42
43 if (which === "probe") {
44 const engine = zlib.createInflate() as unknown as {
45 _processChunk?: unknown;
46 close?: () => void;
47 };
48 const hasProcessChunk = typeof engine._processChunk === "function";
49 engine.close?.();
50 let info: unknown;
51 let infoError: string | undefined;
52 try {
53 const r = zlib.inflateSync(withTrailer, {
54 info: true,
55 } as never) as unknown as {
56 buffer: Uint8Array;
57 engine: { bytesWritten?: number };
58 };
59 info = {
60 outLen: r.buffer?.length,
61 bytesWritten: r.engine?.bytesWritten,
62 expectedConsumed: z.length,
63 };
64 } catch (e) {
65 infoError = String(e);
66 }
67 return HttpServerResponse.jsonUnsafe({
68 hasProcessChunk,
69 info,
70 infoError,
71 contentLen: content.length,
72 });
73 }
74 let ok = 0;
75 let error: string | undefined;
76 try {
77 if (which === "processChunk") {
78 for (let i = 0; i < n; i++) {
79 const engine = zlib.createInflate() as unknown as {
80 _processChunk: (chunk: Uint8Array, flush: number) => Uint8Array;
81 bytesWritten?: number;
82 close?: () => void;
83 };
84 const out = engine._processChunk(withTrailer, zlib.constants.Z_SYNC_FLUSH);
85 if (out.length === content.length && engine.bytesWritten === z.length) ok++;
86 engine.close?.();
87 }
88 } else if (which === "processChunkReuse") {
89 // ONE engine for all entries: _processChunk, read the consumed
90 // delta from the cumulative bytesWritten, then reset().
91 const engine = zlib.createInflate() as unknown as {
92 _processChunk: (chunk: Uint8Array, flush: number) => Uint8Array;
93 bytesWritten: number;
94 reset: () => void;
95 close?: () => void;
96 };
97 let consumedBefore = 0;
98 for (let i = 0; i < n; i++) {
99 const out = engine._processChunk(withTrailer, zlib.constants.Z_SYNC_FLUSH);
100 const consumed = engine.bytesWritten - consumedBefore;
101 consumedBefore = engine.bytesWritten;
102 if (out.length === content.length && consumed === z.length) ok++;
103 engine.reset();
104 }
105 engine.close?.();
106 } else if (which === "info") {
107 for (let i = 0; i < n; i++) {
108 const r = zlib.inflateSync(withTrailer, {
109 info: true,
110 } as never) as unknown as {
111 buffer: Uint8Array;
112 engine: { bytesWritten?: number };
113 };
114 if (r.buffer.length === content.length && r.engine.bytesWritten === z.length) ok++;
115 }
116 } else if (which === "plain") {
117 for (let i = 0; i < n; i++) {
118 if (zlib.inflateSync(z).length === content.length) ok++;
119 }
120 } else if (which === "sha1") {
121 for (let i = 0; i < n; i++) {
122 const h = crypto.createHash("sha1");
123 h.update(content);
124 if (h.digest().length === 20) ok++;
125 }
126 } else if (which === "deflate6") {
127 const big = new Uint8Array(30 * 1024);
128 for (let i = 0; i < big.length; i++) big[i] = (i * 31) & 0xff;
129 for (let i = 0; i < n; i++) {
130 if (zlib.deflateSync(big, { level: 6 }).length > 0) ok++;
131 }
132 } else if (which === "deflate1") {
133 const big = new Uint8Array(30 * 1024);
134 for (let i = 0; i < big.length; i++) big[i] = (i * 31) & 0xff;
135 for (let i = 0; i < n; i++) {
136 if (zlib.deflateSync(big, { level: 1 }).length > 0) ok++;
137 }
138 } else if (which === "copy") {
139 for (let i = 0; i < n; i++) {
140 if (Uint8Array.from(content.subarray(1)).length === content.length - 1) ok++;
141 }
142 } else if (which === "jsloop") {
143 // A delta-apply-shaped loop: byte copies with small varint reads.
144 const out = new Uint8Array(content.length);
145 for (let i = 0; i < n; i++) {
146 let at = 0;
147 for (let j = 0; j < content.length; j += 16) {
148 const len = Math.min(16, content.length - j);
149 out.set(content.subarray(j, j + len), at);
150 at += len;
151 }
152 if (at === content.length) ok++;
153 }
154 } else if (which === "effect") {
155 ok = yield* Effect.gen(function* () {
156 let k = 0;
157 for (let i = 0; i < n; i++) {
158 const v = yield* Effect.gen(function* () {
159 let acc = 0;
160 for (let j = 0; j < 20; j++) acc += yield* Effect.sync(() => j);
161 return acc;
162 });
163 if (v === 190) k++;
164 }
165 return k;
166 });
167 } else if (which === "stream") {
168 ok = yield* Effect.promise(async () => {
169 let k = 0;
170 for (let i = 0; i < n; i++) if ((await streamInflate(z)) === content.length) k++;
171 return k;
172 });
173 }
174 } catch (e) {
175 error = String(e);
176 }
177 return HttpServerResponse.jsonUnsafe({ path: which, n, ok, error });
178 }),
179 };
180 }),
181 ) {}