| 1 | import * as Cloudflare from "@/Cloudflare"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | |
| 6 | /** |
| 7 | * Effect-native Worker fixture for the Cloudflare Images binding. Yielding |
| 8 | * `Cloudflare.Images.Images(...)` during Init attaches the binding to this Worker and |
| 9 | * returns the runtime client in one step — no separate `.bind(...)`. The |
| 10 | * worker forwards the request body (typed as `Stream.Stream<Uint8Array>`) |
| 11 | * straight into `images.info(...)`, proving the Effect-native client converts |
| 12 | * an Effect Stream into the runtime ReadableStream the binding expects. |
| 13 | */ |
| 14 | export default class ImagesEffectWorker extends Cloudflare.Worker<ImagesEffectWorker>()( |
| 15 | "ImagesEffectWorker", |
| 16 | { |
| 17 | main: import.meta.url, |
| 18 | }, |
| 19 | Effect.gen(function* () { |
| 20 | const images = yield* Cloudflare.Images.Images("PIPELINE"); |
| 21 | |
| 22 | return { |
| 23 | fetch: Effect.gen(function* () { |
| 24 | const request = yield* HttpServerRequest; |
| 25 | const info = yield* images.info(request.stream).pipe(Effect.orDie); |
| 26 | return yield* HttpServerResponse.json({ mode: "effect", ...info }); |
| 27 | }), |
| 28 | }; |
| 29 | }).pipe(Effect.provide(Cloudflare.Images.ImagesBinding)), |
| 30 | ) {} |