@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 148 lines 5.29 KB
Raw
1 import type {
2 CreateTunnelCloudflaredError,
3 CreateTunnelCloudflaredRequest,
4 CreateTunnelCloudflaredResponse,
5 DeleteTunnelCloudflaredError,
6 DeleteTunnelCloudflaredResponse,
7 PatchTunnelCloudflaredError,
8 PatchTunnelCloudflaredRequest,
9 PatchTunnelCloudflaredResponse,
10 PutTunnelCloudflaredConfigurationError,
11 PutTunnelCloudflaredConfigurationRequest,
12 PutTunnelCloudflaredConfigurationResponse,
13 } from "@distilled.cloud/cloudflare/zero-trust";
14 import * as zeroTrust from "@distilled.cloud/cloudflare/zero-trust";
15 import * as Binding from "alchemy/Binding";
16 import type { RuntimeContext } from "alchemy/RuntimeContext";
17 import * as Effect from "effect/Effect";
18 import type { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
19 import type { Worker } from "../Workers/Worker.ts";
20 import { type TunnelAuth } from "./TunnelBinding.ts";
21
22 /**
23 * Binding that lets a Worker create, update, and delete Cloudflare Tunnels at
24 * runtime.
25 *
26 * Creates a scoped {@link AccountApiToken} with only the `Cloudflare Tunnel
27 * Write` permission and binds its outputs into the Worker (the token value as a
28 * `secret_text` binding) so runtime code can authenticate.
29 *
30 * @remarks
31 *
32 * `WriteTunnel` is a single identifier that is simultaneously the binding's
33 * Context tag, its type, and the callable — `yield* Cloudflare.Tunnel.WriteTunnel()`.
34 *
35 * ### Mutating tunnels at runtime
36 * **Example:** Bind the write client
37 * Bind once in the Init phase; every method is available on the returned client.
38 * ```typescript
39 * const tunnels = yield* Cloudflare.Tunnel.WriteTunnel();
40 * ```
41 *
42 * **Example:** Create a tunnel
43 * ```typescript
44 * const tunnel = yield* tunnels.create({ name: "on-demand-tunnel" });
45 * ```
46 *
47 * **Example:** Push ingress configuration
48 * ```typescript
49 * yield* tunnels.putConfiguration(tunnel.id!, {
50 * ingress: [
51 * { hostname: "app.example.com", service: "http://localhost:3000" },
52 * { service: "http_status:404" },
53 * ],
54 * });
55 * ```
56 *
57 * **Example:** Rename and delete a tunnel
58 * ```typescript
59 * yield* tunnels.update(tunnel.id!, { name: "renamed-tunnel" });
60 * yield* tunnels.delete(tunnel.id!);
61 * ```
62 *
63 * ### Runtime Layer
64 * Provide {@link WriteTunnelBinding} in the Worker's runtime layer.
65 * ```typescript
66 * Effect.provide(Cloudflare.Tunnel.WriteTunnelBinding)
67 * ```
68 *
69 * @binding
70 * @product Tunnels
71 * @category Cloudflare One (Zero Trust)
72 */
73 export interface WriteTunnel extends Binding.Service<
74 WriteTunnel,
75 "Cloudflare.Tunnel.WriteTunnel",
76 () => Effect.Effect<WriteTunnelClient, never, Worker | CloudflareEnvironment>
77 > {}
78
79 export const WriteTunnel = Binding.Service<WriteTunnel>("Cloudflare.Tunnel.WriteTunnel");
80
81 /** Create-tunnel request, minus the account id (supplied by the binding). */
82 export type CreateTunnelRequest = Omit<CreateTunnelCloudflaredRequest, "accountId">;
83
84 /** Update-tunnel request, minus the account id and tunnel id (positional). */
85 export type UpdateTunnelRequest = Omit<PatchTunnelCloudflaredRequest, "accountId" | "tunnelId">;
86
87 /** Tunnel configuration body, minus the account id and tunnel id (positional). */
88 export type ConfigurationBody = NonNullable<PutTunnelCloudflaredConfigurationRequest["config"]>;
89
90 /**
91 * Mutating tunnel operations. Backed by the `Cloudflare Tunnel Write`
92 * permission group.
93 */
94 export interface WriteTunnelClient {
95 /** Create a new tunnel. */
96 create(
97 request: CreateTunnelRequest,
98 ): Effect.Effect<CreateTunnelCloudflaredResponse, CreateTunnelCloudflaredError, RuntimeContext>;
99 /** Update a tunnel's mutable fields (name, secret). */
100 update(
101 tunnelId: string,
102 request: UpdateTunnelRequest,
103 ): Effect.Effect<PatchTunnelCloudflaredResponse, PatchTunnelCloudflaredError, RuntimeContext>;
104 /** Delete a tunnel by id. */
105 delete(
106 tunnelId: string,
107 ): Effect.Effect<DeleteTunnelCloudflaredResponse, DeleteTunnelCloudflaredError, RuntimeContext>;
108 /** Replace the remotely-managed configuration (ingress rules) for a tunnel. */
109 putConfiguration(
110 tunnelId: string,
111 config: ConfigurationBody,
112 ): Effect.Effect<
113 PutTunnelCloudflaredConfigurationResponse,
114 PutTunnelCloudflaredConfigurationError,
115 RuntimeContext
116 >;
117 }
118
119 /** Build the write client over an injectable {@link TunnelAuth}. */
120 export const writeClient = (auth: TunnelAuth): WriteTunnelClient => {
121 const { authorize } = auth;
122 return {
123 create: Effect.fn("Cloudflare.Tunnel.create")(function* (request) {
124 const accountId = yield* auth.accountId;
125 return yield* authorize(zeroTrust.createTunnelCloudflared({ accountId, ...request }));
126 }),
127 update: Effect.fn("Cloudflare.Tunnel.update")(function* (tunnelId, request) {
128 const accountId = yield* auth.accountId;
129 return yield* authorize(
130 zeroTrust.patchTunnelCloudflared({ accountId, tunnelId, ...request }),
131 );
132 }),
133 delete: Effect.fn("Cloudflare.Tunnel.delete")(function* (tunnelId) {
134 const accountId = yield* auth.accountId;
135 return yield* authorize(zeroTrust.deleteTunnelCloudflared({ accountId, tunnelId }));
136 }),
137 putConfiguration: Effect.fn("Cloudflare.Tunnel.putConfiguration")(function* (tunnelId, config) {
138 const accountId = yield* auth.accountId;
139 return yield* authorize(
140 zeroTrust.putTunnelCloudflaredConfiguration({
141 accountId,
142 tunnelId,
143 config,
144 }),
145 );
146 }),
147 };
148 };