@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 129 lines 4.61 KB
Raw
1 import type {
2 GetTunnelCloudflaredConfigurationError,
3 GetTunnelCloudflaredConfigurationResponse,
4 GetTunnelCloudflaredError,
5 GetTunnelCloudflaredResponse,
6 GetTunnelCloudflaredTokenError,
7 GetTunnelCloudflaredTokenResponse,
8 ListTunnelCloudflaredsError,
9 ListTunnelCloudflaredsRequest,
10 ListTunnelCloudflaredsResponse,
11 } from "@distilled.cloud/cloudflare/zero-trust";
12 import * as zeroTrust from "@distilled.cloud/cloudflare/zero-trust";
13 import * as Binding from "alchemy/Binding";
14 import type { RuntimeContext } from "alchemy/RuntimeContext";
15 import * as Effect from "effect/Effect";
16 import type { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
17 import type { Worker } from "../Workers/Worker.ts";
18 import { type TunnelAuth } from "./TunnelBinding.ts";
19
20 /**
21 * Binding that lets a Worker read Cloudflare Tunnels at runtime.
22 *
23 * Creates a scoped {@link AccountApiToken} with only the `Cloudflare Tunnel
24 * Read` permission and binds its outputs into the Worker (the token value as a
25 * `secret_text` binding) so runtime code can authenticate.
26 *
27 * @remarks
28 *
29 * `ReadTunnel` is a single identifier that is simultaneously the binding's
30 * Context tag, its type, and the callable — `yield* Cloudflare.Tunnel.ReadTunnel()`.
31 *
32 * ### Reading tunnels at runtime
33 * **Example:** Bind the read client
34 * Bind once in the Init phase; every method is available on the returned client.
35 * ```typescript
36 * const tunnels = yield* Cloudflare.Tunnel.ReadTunnel();
37 * ```
38 *
39 * **Example:** List tunnels
40 * ```typescript
41 * const { result } = yield* tunnels.list({ isDeleted: false });
42 * ```
43 *
44 * **Example:** Fetch a tunnel and its connector token
45 * `getToken` returns the plaintext token used to run `cloudflared`.
46 * ```typescript
47 * const tunnel = yield* tunnels.get(tunnelId);
48 * const token = yield* tunnels.getToken(tunnelId);
49 * ```
50 *
51 * **Example:** Read the ingress configuration
52 * ```typescript
53 * const { config } = yield* tunnels.getConfiguration(tunnelId);
54 * ```
55 *
56 * ### Runtime Layer
57 * Provide {@link ReadTunnelBinding} in the Worker's runtime layer.
58 * ```typescript
59 * Effect.provide(Cloudflare.Tunnel.ReadTunnelBinding)
60 * ```
61 *
62 * @binding
63 * @product Tunnels
64 * @category Cloudflare One (Zero Trust)
65 */
66 export interface ReadTunnel extends Binding.Service<
67 ReadTunnel,
68 "Cloudflare.Tunnel.ReadTunnel",
69 () => Effect.Effect<ReadTunnelClient, never, Worker | CloudflareEnvironment>
70 > {}
71
72 export const ReadTunnel = Binding.Service<ReadTunnel>("Cloudflare.Tunnel.ReadTunnel");
73
74 /** List-tunnels request, minus the account id (supplied by the binding). */
75 export type ListTunnelsRequest = Omit<ListTunnelCloudflaredsRequest, "accountId">;
76
77 /**
78 * Read-only tunnel operations. Backed by the `Cloudflare Tunnel Read`
79 * permission group.
80 */
81 export interface ReadTunnelClient {
82 /** Fetch a single tunnel by id. */
83 get(
84 tunnelId: string,
85 ): Effect.Effect<GetTunnelCloudflaredResponse, GetTunnelCloudflaredError, RuntimeContext>;
86 /** List tunnels in the account. */
87 list(
88 request?: ListTunnelsRequest,
89 ): Effect.Effect<ListTunnelCloudflaredsResponse, ListTunnelCloudflaredsError, RuntimeContext>;
90 /** Fetch the connector token used to run the tunnel. */
91 getToken(
92 tunnelId: string,
93 ): Effect.Effect<
94 GetTunnelCloudflaredTokenResponse,
95 GetTunnelCloudflaredTokenError,
96 RuntimeContext
97 >;
98 /** Read the remotely-managed configuration (ingress rules) for a tunnel. */
99 getConfiguration(
100 tunnelId: string,
101 ): Effect.Effect<
102 GetTunnelCloudflaredConfigurationResponse,
103 GetTunnelCloudflaredConfigurationError,
104 RuntimeContext
105 >;
106 }
107
108 /** Build the read-only client over an injectable {@link TunnelAuth}. */
109 export const readClient = (auth: TunnelAuth): ReadTunnelClient => {
110 const { authorize } = auth;
111 return {
112 get: Effect.fn("Cloudflare.Tunnel.get")(function* (tunnelId) {
113 const accountId = yield* auth.accountId;
114 return yield* authorize(zeroTrust.getTunnelCloudflared({ accountId, tunnelId }));
115 }),
116 list: Effect.fn("Cloudflare.Tunnel.list")(function* (request) {
117 const accountId = yield* auth.accountId;
118 return yield* authorize(zeroTrust.listTunnelCloudflareds({ accountId, ...request }));
119 }),
120 getToken: Effect.fn("Cloudflare.Tunnel.getToken")(function* (tunnelId) {
121 const accountId = yield* auth.accountId;
122 return yield* authorize(zeroTrust.getTunnelCloudflaredToken({ accountId, tunnelId }));
123 }),
124 getConfiguration: Effect.fn("Cloudflare.Tunnel.getConfiguration")(function* (tunnelId) {
125 const accountId = yield* auth.accountId;
126 return yield* authorize(zeroTrust.getTunnelCloudflaredConfiguration({ accountId, tunnelId }));
127 }),
128 };
129 };