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

main
ts 53 lines 1.84 KB
Raw
1 // Async (non-Effect) Worker that drives an Images binding the way real users
2 // do — `info(...)` and `input(...).transform(...).output(...)` over the
3 // request body. Used by Images.local.test.ts both against the local
4 // Sharp-backed simulator and (piped through `Alchemy.remote()`) against the real
5 // Images service via the remote-bindings proxy.
6 import type * as cf from "@cloudflare/workers-types";
7
8 interface Env {
9 IMAGES: cf.ImagesBinding;
10 }
11
12 export default {
13 async fetch(request: Request, env: Env): Promise<Response> {
14 const url = new URL(request.url);
15 try {
16 if (url.pathname === "/info") {
17 const info = await env.IMAGES.info(
18 request.body as unknown as cf.ReadableStream<Uint8Array>,
19 );
20 return Response.json(info);
21 }
22 if (url.pathname === "/transform") {
23 let transformer = env.IMAGES.input(
24 request.body as unknown as cf.ReadableStream<Uint8Array>,
25 );
26 const width = url.searchParams.get("width");
27 if (width !== null) {
28 transformer = transformer.transform({ width: Number(width) });
29 }
30 const format = (url.searchParams.get("format") ?? "image/png") as Parameters<
31 typeof transformer.output
32 >[0]["format"];
33 try {
34 const result = await transformer.output({ format });
35 return result.response() as unknown as Response;
36 } catch (e) {
37 const error = e as { code?: number; message?: string };
38 return Response.json({
39 error: true,
40 code: error.code,
41 message: error.message,
42 });
43 }
44 }
45 return new Response("not found", { status: 404 });
46 } catch (e) {
47 const error = e as { stack?: string };
48 return new Response(`fixture error: ${error?.stack ?? String(e)}`, {
49 status: 500,
50 });
51 }
52 },
53 };