@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, 07:15 UTC

main
ts 62 lines 2.49 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Effect from "effect/Effect";
3 import { HttpServerRequest } from "effect/http/HttpServerRequest";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5 import * as Layer from "effect/Layer";
6 import * as EffectStream from "effect/Stream";
7 import { PageView, PageViews } from "./pipelines-stream.ts";
8
9 /**
10 * Effect-native Worker exercising the typed Pipelines producers over the
11 * native `pipelines` binding: `/send` (WriteStream), `/sink` (StreamSink)
12 * and `/invalid` (a record the stream's Effect Schema rejects).
13 */
14 export default class PipelinesWorker extends Cloudflare.Worker<PipelinesWorker>()(
15 "PipelinesWorker",
16 { main: import.meta.url },
17 Effect.gen(function* () {
18 const views = yield* Cloudflare.Basin.WriteStream(PageViews);
19 const sink = yield* Cloudflare.Basin.StreamSink(PageViews);
20
21 return {
22 fetch: Effect.gen(function* () {
23 const request = yield* HttpServerRequest;
24 const url = new URL(request.originalUrl, "http://x");
25 if (url.pathname === "/send") {
26 yield* views
27 .send([
28 new PageView({ url: "/a", at: new Date(), tags: ["x"] }),
29 new PageView({ url: "/b", at: new Date(), tags: [], user: { id: "u1" } }),
30 ])
31 .pipe(Effect.orDie);
32 return yield* HttpServerResponse.json({ sent: 2 });
33 }
34 if (url.pathname === "/sink") {
35 const count = Number(url.searchParams.get("count") ?? "300");
36 yield* EffectStream.range(1, count).pipe(
37 EffectStream.map((i) => new PageView({ url: `/p/${i}`, at: new Date(), tags: [] })),
38 EffectStream.run(sink),
39 Effect.orDie,
40 );
41 return yield* HttpServerResponse.json({ sent: count });
42 }
43 if (url.pathname === "/invalid") {
44 const result = yield* views
45 .send([{ url: "/c", at: "not-a-date", tags: [] } as unknown as PageView])
46 .pipe(
47 Effect.as({ reason: "none" as string, index: -1 }),
48 Effect.catchTag("StreamSendError", (e) =>
49 Effect.succeed({ reason: e.reason ?? "unknown", index: e.index ?? -1 }),
50 ),
51 );
52 return yield* HttpServerResponse.json(result);
53 }
54 return yield* HttpServerResponse.json({ ok: true });
55 }),
56 };
57 }).pipe(
58 Effect.provide(
59 Layer.mergeAll(Cloudflare.Basin.WriteStreamBinding, Cloudflare.Basin.StreamSinkBinding),
60 ),
61 ),
62 ) {}