@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 61 lines 2.24 KB
Raw
1 // Async (non-Effect) Worker that drives a Stream binding the way real users
2 // do — upload / details / list / delete over simple routes. Used by
3 // Stream.local.test.ts both against the local video-store simulator and
4 // (piped through `Alchemy.remote()`) against the real Stream service via the
5 // remote-bindings proxy.
6 import type * as cf from "@cloudflare/workers-types";
7
8 interface Env {
9 STREAM: cf.StreamBinding;
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 === "/upload") {
17 // The local simulator (like Miniflare's stream plugin) accepts a
18 // ReadableStream of the video bytes in place of a URL; the published
19 // types only declare the URL overload.
20 const upload = env.STREAM.upload as unknown as (
21 input: ReadableStream | string,
22 params?: cf.StreamUrlUploadParams,
23 ) => Promise<cf.StreamVideo>;
24 const video = await upload(request.body as unknown as ReadableStream, {
25 meta: { title: "alchemy-local-test" },
26 creator: "alchemy",
27 });
28 return Response.json(video);
29 }
30 if (url.pathname === "/details") {
31 const video = await env.STREAM.video(url.searchParams.get("id")!).details();
32 return Response.json(video);
33 }
34 if (url.pathname === "/list") {
35 const videos = await env.STREAM.videos.list();
36 return Response.json(videos);
37 }
38 if (url.pathname === "/delete") {
39 await env.STREAM.video(url.searchParams.get("id")!).delete();
40 return Response.json({ deleted: true });
41 }
42 return new Response("not found", { status: 404 });
43 } catch (e) {
44 // Expected binding errors (e.g. "Video not found") come back as a 200
45 // JSON envelope so the test can assert on them without tripping the
46 // readiness retry; unexpected errors carry the stack for debugging.
47 const error = e as {
48 code?: number;
49 statusCode?: number;
50 message?: string;
51 stack?: string;
52 };
53 return Response.json({
54 error: true,
55 code: error.code,
56 statusCode: error.statusCode,
57 message: error.message ?? String(e),
58 });
59 }
60 },
61 };