The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type * as cf from "@cloudflare/workers-types"; |
| 2 | import type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import type * as Binding from "../Workers/Binding.ts"; |
| 5 | import { makeBindingLayer } from "../Workers/BindingLayer.ts"; |
| 6 | import { Stream, StreamError, type StreamClient, type StreamVideoClient } from "./Stream.ts"; |
| 7 | |
| 8 | /** The binding value produced by calling {@link Stream} (declared on `env` or `yield*`-ed). */ |
| 9 | export type StreamBinding = Binding.Binding<Stream["key"], StreamClient, Stream>; |
| 10 | |
| 11 | /** |
| 12 | * The layer that provides the Effect-native interface for the Cloudflare |
| 13 | * Stream binding. |
| 14 | * |
| 15 | * Provide it on the Worker effect (`Effect.provide(Cloudflare.Stream.StreamBinding)`) |
| 16 | * so that yielding a {@link Stream} binding attaches the native `stream` |
| 17 | * binding to the surrounding Worker at deploy time and, at runtime, resolves to |
| 18 | * the Effect-native {@link StreamClient} (wrapping the raw `cf.StreamBinding` |
| 19 | * handle so every operation returns an `Effect`). |
| 20 | */ |
| 21 | export const StreamBinding = makeBindingLayer<Stream, cf.StreamBinding, StreamClient>( |
| 22 | Stream, |
| 23 | (raw): StreamClient => { |
| 24 | const call = <T>( |
| 25 | fn: (binding: cf.StreamBinding) => Promise<T>, |
| 26 | ): Effect.Effect<T, StreamError, RuntimeContext> => |
| 27 | raw.pipe(Effect.flatMap((binding) => tryPromise(() => fn(binding)))); |
| 28 | |
| 29 | const video = (id: string): StreamVideoClient => ({ |
| 30 | id, |
| 31 | details: () => call((binding) => binding.video(id).details()), |
| 32 | update: (params) => call((binding) => binding.video(id).update(params)), |
| 33 | delete: () => call((binding) => binding.video(id).delete()), |
| 34 | generateToken: () => call((binding) => binding.video(id).generateToken()), |
| 35 | downloads: { |
| 36 | generate: (downloadType) => |
| 37 | call((binding) => binding.video(id).downloads.generate(downloadType)), |
| 38 | get: () => call((binding) => binding.video(id).downloads.get()), |
| 39 | delete: (downloadType) => |
| 40 | call((binding) => binding.video(id).downloads.delete(downloadType)), |
| 41 | }, |
| 42 | captions: { |
| 43 | upload: (language, input) => |
| 44 | call((binding) => |
| 45 | binding.video(id).captions.upload(language, input as unknown as cf.ReadableStream), |
| 46 | ), |
| 47 | generate: (language) => call((binding) => binding.video(id).captions.generate(language)), |
| 48 | list: (language) => call((binding) => binding.video(id).captions.list(language)), |
| 49 | delete: (language) => call((binding) => binding.video(id).captions.delete(language)), |
| 50 | }, |
| 51 | }); |
| 52 | |
| 53 | return { |
| 54 | raw, |
| 55 | upload: (url, params) => call((binding) => binding.upload(url, params)), |
| 56 | createDirectUpload: (params) => call((binding) => binding.createDirectUpload(params)), |
| 57 | video, |
| 58 | videos: { |
| 59 | list: (params) => call((binding) => binding.videos.list(params)), |
| 60 | }, |
| 61 | watermarks: { |
| 62 | generate: (input, params) => |
| 63 | call((binding) => |
| 64 | // The runtime accepts a stream or a URL string for the first |
| 65 | // argument; the published types declare the two as overloads. |
| 66 | binding.watermarks.generate(input as unknown as cf.ReadableStream, params), |
| 67 | ), |
| 68 | list: () => call((binding) => binding.watermarks.list()), |
| 69 | get: (watermarkId) => call((binding) => binding.watermarks.get(watermarkId)), |
| 70 | delete: (watermarkId) => call((binding) => binding.watermarks.delete(watermarkId)), |
| 71 | }, |
| 72 | } satisfies StreamClient; |
| 73 | }, |
| 74 | ); |
| 75 | |
| 76 | const tryPromise = <T>(fn: () => Promise<T>): Effect.Effect<T, StreamError> => |
| 77 | Effect.tryPromise({ |
| 78 | try: fn, |
| 79 | catch: (error: any) => |
| 80 | new StreamError({ |
| 81 | message: error?.message ?? "Unknown Cloudflare Stream error", |
| 82 | code: typeof error?.code === "number" ? error.code : undefined, |
| 83 | statusCode: typeof error?.statusCode === "number" ? error.statusCode : undefined, |
| 84 | cause: error, |
| 85 | }), |
| 86 | }); |