| 1 | import type { AsyncWorkerEnv } from "./stack.ts"; |
| 2 | |
| 3 | /** |
| 4 | * Async (non-Effect) Worker fixture for the Cloudflare Hyperdrive binding |
| 5 | * declared via `env: { HD: connection }`. `InferEnv` maps the |
| 6 | * `Hyperdrive.Connection` resource to the native runtime `Hyperdrive` |
| 7 | * binding, so the handler reads `env.HD.host` / `.port` / `.user` / |
| 8 | * `.database` / `.connectionString` directly. |
| 9 | * |
| 10 | * The runtime connection string / password are valid only within the |
| 11 | * current invocation, so the handler reports their *shape* (non-empty, |
| 12 | * parseable URI) rather than echoing secret material over the wire. |
| 13 | */ |
| 14 | export default { |
| 15 | async fetch(request: Request, env: AsyncWorkerEnv): Promise<Response> { |
| 16 | const url = new URL(request.url); |
| 17 | |
| 18 | if (url.pathname === "/meta") { |
| 19 | const hd = env.HD; |
| 20 | const connectionString = hd.connectionString; |
| 21 | const parsed = new URL(connectionString); |
| 22 | return Response.json({ |
| 23 | mode: "async", |
| 24 | host: hd.host, |
| 25 | port: hd.port, |
| 26 | user: hd.user, |
| 27 | database: hd.database, |
| 28 | hasConnectionString: connectionString.length > 0, |
| 29 | hasPassword: hd.password.length > 0, |
| 30 | connectionStringProtocol: parsed.protocol, |
| 31 | connectionStringHost: parsed.hostname, |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | return new Response("Not Found", { status: 404 }); |
| 36 | }, |
| 37 | }; |