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

main
ts 99 lines 3.69 KB
Raw
1 import type * as cf from "@cloudflare/workers-types";
2 import * as Effect from "effect/Effect";
3 import * as Stream from "effect/Stream";
4 import * as Binding from "../Workers/Binding.ts";
5 import { makeBindingLayer } from "../Workers/BindingLayer.ts";
6 import {
7 Images,
8 ImagesError,
9 type ImagesClient,
10 type ImageTransformationResult,
11 type ImageTransformer,
12 } from "./Images.ts";
13
14 /** The binding value produced by calling {@link Images} (declared on `env` or `yield*`-ed). */
15 export type ImagesBinding = Binding.Binding<Images["key"], ImagesClient, Images>;
16
17 /**
18 * The layer that provides the Effect-native interface for the Cloudflare
19 * Workers Images binding.
20 *
21 * Provide it on the Worker effect (`Effect.provide(Cloudflare.Images.ImagesBinding)`)
22 * so that yielding an {@link Images} binding attaches the native `images`
23 * binding to the surrounding Worker at deploy time and, at runtime, resolves to
24 * the Effect-native {@link ImagesClient} (wrapping the raw `cf.ImagesBinding` so every
25 * `info` / `input(...).transform(...).output(...)` call returns an `Effect`).
26 */
27 export const ImagesBinding = makeBindingLayer<Images, cf.ImagesBinding, ImagesClient>(
28 Images,
29 (raw) =>
30 ({
31 raw,
32 info: (stream, options) =>
33 Effect.gen(function* () {
34 const binding = yield* raw;
35 const readable = yield* toCfReadable(stream);
36 return yield* tryPromise(() => binding.info(readable, options));
37 }),
38 input: (stream, options) =>
39 Effect.gen(function* () {
40 const binding = yield* raw;
41 const readable = yield* toCfReadable(stream);
42 return wrapTransformer(binding.input(readable, options));
43 }),
44 }) satisfies ImagesClient,
45 );
46
47 /**
48 * Wrap a runtime `ImageTransformer` as the Effect-native chainable client.
49 * `transform`/`draw` stay pure; `output` crosses into the runtime.
50 */
51 const wrapTransformer = (raw: cf.ImageTransformer): ImageTransformer => ({
52 raw,
53 transform: (transform) => wrapTransformer(raw.transform(transform)),
54 draw: <E, R>(
55 image: Stream.Stream<Uint8Array, E, R> | ImageTransformer,
56 options?: cf.ImageDrawOptions,
57 ): Effect.Effect<ImageTransformer, never, R> => {
58 if (isTransformerClient(image)) {
59 return Effect.succeed(wrapTransformer(raw.draw(image.raw, options)));
60 }
61 return toCfReadable(image).pipe(
62 Effect.map((readable) => wrapTransformer(raw.draw(readable, options))),
63 );
64 },
65 output: (options) => tryPromise(() => raw.output(options)).pipe(Effect.map(wrapResult)),
66 });
67
68 /** Wrap a runtime `ImageTransformationResult` as the Effect-native result client. */
69 const wrapResult = (raw: cf.ImageTransformationResult): ImageTransformationResult => ({
70 raw,
71 response: Effect.sync(() => raw.response()),
72 contentType: Effect.sync(() => raw.contentType()),
73 image: (options) => Effect.sync(() => raw.image(options)),
74 });
75
76 /**
77 * Convert an Effect `Stream<Uint8Array>` into the `cf.ReadableStream<Uint8Array>`
78 * shape the Images runtime binding expects (identical at runtime).
79 */
80 const toCfReadable = <E, R>(
81 stream: Stream.Stream<Uint8Array, E, R>,
82 ): Effect.Effect<cf.ReadableStream<Uint8Array>, never, R> =>
83 Stream.toReadableStreamEffect(stream).pipe(
84 Effect.map((s) => s as unknown as cf.ReadableStream<Uint8Array>),
85 );
86
87 const tryPromise = <T>(fn: () => Promise<T>): Effect.Effect<T, ImagesError> =>
88 Effect.tryPromise({
89 try: fn,
90 catch: (error: any) =>
91 new ImagesError({
92 message: error?.message ?? "Unknown error",
93 code: typeof error?.code === "number" ? error.code : undefined,
94 cause: error,
95 }),
96 });
97
98 const isTransformerClient = (image: unknown): image is ImageTransformer =>
99 typeof image === "object" && image !== null && "raw" in image;