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

main
ts 97 lines 3.05 KB
Raw
1 import type * as cf from "@cloudflare/workers-types";
2 import * as Namespace from "alchemy/Namespace";
3 import type { RuntimeContext } from "alchemy/RuntimeContext";
4 import * as Effect from "effect/Effect";
5 import { Worker, WorkerExecutionContext, type CachePurgeError } from "./Worker.ts";
6
7 export interface CacheOptions {
8 /**
9 * Whether Workers Cache checks the cache before invoking the Worker.
10 * @default true
11 */
12 enabled?: boolean;
13 /**
14 * Share cached responses across Worker versions. By default the cache is
15 * scoped to a single version, so every deploy starts cold.
16 * @default false
17 */
18 crossVersionCache?: boolean;
19 }
20
21 /**
22 * Runtime client for Workers Cache, returned by `yield* Cloudflare.cache()`.
23 */
24 export interface CacheClient {
25 /**
26 * Purge cached responses by `Cache-Tag`, path prefix, or everything.
27 */
28 purge(
29 options: cf.CachePurgeOptions,
30 ): Effect.Effect<cf.CachePurgeResult, CachePurgeError, RuntimeContext>;
31 }
32
33 /**
34 * Enable [Workers Cache](https://blog.cloudflare.com/workers-cache/) on the
35 * surrounding Worker and get the runtime cache client.
36 *
37 * Yielding `cache()` in an Effect-native Worker's init phase turns the cache
38 * on at deploy time (the equivalent of setting `cache: { enabled: true }` on
39 * the Worker's props) and returns a client whose `purge` drives the runtime
40 * `ctx.cache` API from your handlers.
41 *
42 * What gets cached is controlled by standard response headers —
43 * `Cache-Control` (including `stale-while-revalidate`), `Cache-Tag` for
44 * tag-based purging, and `Vary` for content negotiation.
45 *
46 * For async (non-Effect) Workers, set the `cache` prop on the Worker
47 * instead.
48 *
49 * **Example:** Example
50 * ```typescript
51 * Effect.gen(function* () {
52 * // init: enable Workers Cache on this Worker
53 * const { purge } = yield* Cloudflare.cache();
54 *
55 * return {
56 * fetch: Effect.gen(function* () {
57 * const request = yield* HttpServerRequest;
58 * if (request.url.startsWith("/invalidate")) {
59 * yield* purge({ tags: ["products"] });
60 * return HttpServerResponse.text("purged");
61 * }
62 * return HttpServerResponse.text("hello", {
63 * headers: {
64 * "Cache-Control": "public, max-age=300",
65 * "Cache-Tag": "products",
66 * },
67 * });
68 * }),
69 * };
70 * })
71 * ```
72 *
73 * @binding
74 * @product Workers
75 * @category Workers & Compute
76 */
77 export const cache = (options?: CacheOptions): Effect.Effect<CacheClient, never, Worker> =>
78 Effect.gen(function* () {
79 const host = yield* Worker;
80 const exec = yield* WorkerExecutionContext;
81 if (!globalThis.__ALCHEMY_RUNTIME__) {
82 yield* Namespace.push(
83 host.LogicalId,
84 host.bind("Cache", {
85 cache: {
86 enabled: options?.enabled ?? true,
87 crossVersionCache: options?.crossVersionCache,
88 },
89 }),
90 );
91 }
92 return {
93 // `exec` is the init-phase deferred context; purge resolves the live
94 // per-event context from the calling handler's fiber.
95 purge: (purgeOptions) => exec.cache.purge(purgeOptions),
96 };
97 });