@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, 04:45 UTC

main
ts 100 lines 3.39 KB
Raw
1 import type { RuntimeContext } from "alchemy/RuntimeContext";
2 import type * as Effect from "effect/Effect";
3 import * as Binding from "./Binding.ts";
4 import type { VersionMetadataBinding } from "./VersionMetadataBinding.ts";
5
6 const TypeId = "Cloudflare.Workers.VersionMetadata" as const;
7 type TypeId = typeof TypeId;
8
9 /**
10 * Runtime value Cloudflare exposes for a `version_metadata` binding — the
11 * deployed Worker version's `id`, `tag`, and `timestamp`.
12 */
13 export interface WorkerVersionMetadata {
14 readonly id: string;
15 readonly tag: string;
16 readonly timestamp: string;
17 }
18
19 /**
20 * Effect-native accessor for the version metadata. The env binding only exists
21 * at the *exec* phase on the deployed Worker, so reading it is deferred behind
22 * an Effect that requires {@link RuntimeContext}. Yield it inside a handler to
23 * obtain the {@link WorkerVersionMetadata}.
24 */
25 export type VersionMetadataAccessor = Effect.Effect<WorkerVersionMetadata, never, RuntimeContext>;
26
27 /**
28 * A Cloudflare Workers Version Metadata binding — a Worker-only binding with no
29 * backing cloud resource. Cloudflare provides the deployed Worker version at
30 * runtime (`id`, `tag`, `timestamp`).
31 *
32 * `VersionMetadata` is a single value that is at once the `Binding.Service` tag,
33 * the callable that produces a {@link VersionMetadataBinding}, and the type.
34 * Declare it on a Worker's `env` (it flows through `InferEnv` →
35 * {@link WorkerVersionMetadata}) or `yield*` it inside an Effect-native Worker
36 * to attach the binding and obtain a deferred {@link VersionMetadataAccessor}.
37 *
38 * ### Effect-style Worker (recommended)
39 * **Example:** Read the deployed version from inside a handler
40 * ```typescript
41 * import * as Effect from "effect/Effect";
42 *
43 * Cloudflare.Worker(
44 * "VersionWorker",
45 * { main: import.meta.url },
46 * Effect.gen(function* () {
47 * // Attaches the binding to this Worker AND returns a deferred accessor.
48 * const versionMetadata = yield* Cloudflare.Workers.VersionMetadata();
49 *
50 * return {
51 * fetch: Effect.gen(function* () {
52 * const { id, tag, timestamp } = yield* versionMetadata;
53 * return Response.json({ id, tag, timestamp });
54 * }),
55 * };
56 * }).pipe(Effect.provide(Cloudflare.Workers.VersionMetadataBinding)),
57 * );
58 * ```
59 *
60 * ### Worker binding metadata
61 * **Example:** Example
62 * ```typescript
63 * export const Worker = Cloudflare.Worker("Worker", {
64 * main: "./src/worker.ts",
65 * env: { CF_VERSION_METADATA: Cloudflare.Workers.VersionMetadata() },
66 * });
67 *
68 * export type WorkerEnv = Cloudflare.InferEnv<typeof Worker>;
69 * // { CF_VERSION_METADATA: WorkerVersionMetadata }
70 * ```
71 *
72 * @see https://developers.cloudflare.com/workers/runtime-apis/bindings/version-metadata/
73 *
74 * @binding
75 * @product Workers
76 * @category Workers & Compute
77 */
78 export interface VersionMetadata extends Binding.Service<
79 VersionMetadata,
80 TypeId,
81 VersionMetadataAccessor
82 > {
83 /**
84 * @param name Binding name (logical id) — the `env` key it resolves to.
85 * @default "CF_VERSION_METADATA"
86 */
87 (name?: string): VersionMetadataBinding;
88 }
89
90 export const VersionMetadata = Binding.Service<VersionMetadata>({
91 id: TypeId,
92 defaultName: "CF_VERSION_METADATA",
93 toWorkerBinding: (binding) => ({
94 type: "version_metadata",
95 name: binding.name,
96 }),
97 });
98
99 export const isVersionMetadata = (value: unknown): value is VersionMetadataBinding =>
100 Binding.isBinding(value) && value.kind === TypeId;