The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type * as runtime from "@cloudflare/workers-types"; |
| 2 | import * as Output from "alchemy/Output"; |
| 3 | import { defaultProviderMode, type ProviderMode } from "alchemy/ProviderMode"; |
| 4 | import * as Data from "effect/Data"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Redacted from "effect/Redacted"; |
| 8 | import { Worker, WorkerEnvironment } from "../Workers/Worker.ts"; |
| 9 | import { Connect, type ConnectClient } from "./Connect.ts"; |
| 10 | import type { Connection } from "./Connection.ts"; |
| 11 | import { defaultPort, type DevOrigin, type Mtls, type Origin } from "./Connection.ts"; |
| 12 | import { type ConnectionLookup, isConnectionLookup } from "./ConnectionLookup.ts"; |
| 13 | |
| 14 | export const ConnectBinding = Layer.effect( |
| 15 | Connect, |
| 16 | Effect.gen(function* () { |
| 17 | const env = yield* WorkerEnvironment; |
| 18 | const host = yield* Worker; |
| 19 | |
| 20 | return Effect.fn(function* (connection: Connection | ConnectionLookup) { |
| 21 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 22 | yield* host.bind`${connection}`({ |
| 23 | bindings: [ |
| 24 | { |
| 25 | type: "hyperdrive", |
| 26 | name: connection.LogicalId, |
| 27 | id: connection.hyperdriveId as unknown as string, |
| 28 | }, |
| 29 | ], |
| 30 | hyperdrives: yield* getHyperdriveDevOriginForHost(connection, host), |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | const hd = Effect.sync( |
| 35 | () => (env as Record<string, runtime.Hyperdrive>)[connection.LogicalId]!, |
| 36 | ); |
| 37 | |
| 38 | return { |
| 39 | raw: hd, |
| 40 | connectionString: hd.pipe(Effect.map((hd) => Redacted.make(hd.connectionString))), |
| 41 | host: hd.pipe(Effect.map((hd) => hd.host)), |
| 42 | port: hd.pipe(Effect.map((hd) => hd.port)), |
| 43 | user: hd.pipe(Effect.map((hd) => hd.user)), |
| 44 | password: hd.pipe(Effect.map((hd) => Redacted.make(hd.password))), |
| 45 | database: hd.pipe(Effect.map((hd) => hd.database)), |
| 46 | } satisfies ConnectClient; |
| 47 | }); |
| 48 | }), |
| 49 | ); |
| 50 | |
| 51 | /** |
| 52 | * The `hyperdrives` dev channel for `connection` bound to `host`. Only the |
| 53 | * local worker provider reads it, so it is contributed only when the host |
| 54 | * runs locally — its registration-captured `Mode` (`Alchemy.remote()` → |
| 55 | * `"live"`) or the run default (`alchemy dev` → `"local"`), the same |
| 56 | * resolution the planner applies to the host. A live host (`alchemy deploy`, |
| 57 | * or a `remote()` worker in dev) never evaluates the dev origin, so an |
| 58 | * Access-protected origin without a `dev` override deploys. |
| 59 | */ |
| 60 | export const getHyperdriveDevOriginForHost = Effect.fn(function* ( |
| 61 | connection: Connection | ConnectionLookup, |
| 62 | host: { readonly Mode?: ProviderMode | undefined }, |
| 63 | ) { |
| 64 | const mode = host.Mode ?? (yield* defaultProviderMode); |
| 65 | return mode === "local" ? getHyperdriveDevOrigin(connection) : undefined; |
| 66 | }); |
| 67 | |
| 68 | /** |
| 69 | * A Hyperdrive origin that `alchemy dev` cannot reach from the local |
| 70 | * machine, bound without a `dev` override. |
| 71 | */ |
| 72 | export class HyperdriveDevOriginUnsupported extends Data.TaggedError( |
| 73 | "HyperdriveDevOriginUnsupported", |
| 74 | )<{ |
| 75 | message: string; |
| 76 | logicalId: string; |
| 77 | origin: "access" | "vpc" | "lookup"; |
| 78 | }> {} |
| 79 | |
| 80 | const toDevOrigin = (dev: DevOrigin): Required<DevOrigin> => ({ |
| 81 | scheme: dev.scheme, |
| 82 | host: dev.host, |
| 83 | port: dev.port ?? defaultPort(dev.scheme), |
| 84 | user: dev.user, |
| 85 | database: dev.database, |
| 86 | password: dev.password, |
| 87 | sslmode: dev.sslmode ?? "prefer", |
| 88 | }); |
| 89 | |
| 90 | export const getHyperdriveDevOrigin = (connection: Connection | ConnectionLookup) => |
| 91 | isConnectionLookup(connection) |
| 92 | ? getLookupDevOrigin(connection) |
| 93 | : getConnectionDevOrigin(connection); |
| 94 | |
| 95 | /** |
| 96 | * A looked-up configuration's origin password is never returned by the |
| 97 | * Cloudflare API, so only its `dev` origin can be emulated locally. |
| 98 | */ |
| 99 | const getLookupDevOrigin = (lookup: ConnectionLookup) => |
| 100 | Output.map( |
| 101 | Output.all( |
| 102 | lookup.hyperdriveId, |
| 103 | lookup.dev.pipe( |
| 104 | Output.mapEffect((dev: DevOrigin | undefined) => |
| 105 | dev |
| 106 | ? Effect.succeed(toDevOrigin(dev)) |
| 107 | : Effect.die( |
| 108 | new HyperdriveDevOriginUnsupported({ |
| 109 | logicalId: lookup.LogicalId, |
| 110 | origin: "lookup", |
| 111 | message: |
| 112 | "A Hyperdrive.lookup(...) configuration cannot be emulated in development mode without a `dev` origin: " + |
| 113 | "the Cloudflare API never returns the origin password of an existing configuration. " + |
| 114 | "Set the `dev` property to an origin reachable from your machine.", |
| 115 | }), |
| 116 | ), |
| 117 | ), |
| 118 | Output.asOutput, |
| 119 | ), |
| 120 | ), |
| 121 | ([id, origin]) => ({ [id]: origin }), |
| 122 | ) as unknown as Record<string, Required<DevOrigin>>; |
| 123 | |
| 124 | const getConnectionDevOrigin = (connection: Connection) => { |
| 125 | const origin = Output.mapEffect(([dev, origin, mtls]: [DevOrigin | undefined, Origin, Mtls]) => { |
| 126 | if (dev) { |
| 127 | return Effect.succeed(toDevOrigin(dev)); |
| 128 | } |
| 129 | if ("accessClientId" in origin) { |
| 130 | return Effect.die( |
| 131 | new HyperdriveDevOriginUnsupported({ |
| 132 | logicalId: connection.LogicalId, |
| 133 | origin: "access", |
| 134 | message: |
| 135 | `Hyperdrive instance ${connection.LogicalId} has an origin that requires Cloudflare Access. This is not supported in development mode. ` + |
| 136 | "Select a different origin or set the `dev` property to an origin that does not require Cloudflare Access.", |
| 137 | }), |
| 138 | ); |
| 139 | } |
| 140 | if ("serviceId" in origin) { |
| 141 | return Effect.die( |
| 142 | new HyperdriveDevOriginUnsupported({ |
| 143 | logicalId: connection.LogicalId, |
| 144 | origin: "vpc", |
| 145 | message: |
| 146 | `Hyperdrive instance ${connection.LogicalId} has an origin that is reached through a Workers VPC Service. This is not supported in development mode. ` + |
| 147 | "Set the `dev` property to an origin reachable from your machine.", |
| 148 | }), |
| 149 | ); |
| 150 | } |
| 151 | return Effect.succeed<Required<DevOrigin>>({ |
| 152 | scheme: origin.scheme, |
| 153 | host: origin.host, |
| 154 | port: origin.port ?? defaultPort(origin.scheme), |
| 155 | user: origin.user, |
| 156 | database: origin.database, |
| 157 | password: origin.password, |
| 158 | sslmode: mtls?.sslmode ?? "require", |
| 159 | }); |
| 160 | })(Output.all(connection.dev, connection.origin, connection.mtls)); |
| 161 | return Output.map( |
| 162 | Output.all(connection.hyperdriveId, Output.asOutput(origin)), |
| 163 | ([id, origin]) => ({ |
| 164 | [id]: origin, |
| 165 | }), |
| 166 | ) as unknown as Record<string, Required<DevOrigin>>; |
| 167 | }; |