| 1 | import * as Cloudflare from "@/Cloudflare/index.ts"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | import { HyperdriveConnection } from "./connection.ts"; |
| 6 | import { connectionRoutes } from "./routes.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Effect-native Worker that binds the shared Hyperdrive Connection via |
| 10 | * `Cloudflare.Hyperdrive.Connect(connection)` and exposes the client over |
| 11 | * `fetch` through the shared {@link connectionRoutes}. The binding is |
| 12 | * provided via `Effect.provide(Cloudflare.Hyperdrive.ConnectBinding)`. |
| 13 | */ |
| 14 | export default class HyperdriveEffectWorker extends Cloudflare.Worker<HyperdriveEffectWorker>()( |
| 15 | "HyperdriveEffectWorker", |
| 16 | { main: import.meta.url }, |
| 17 | Effect.gen(function* () { |
| 18 | const { connection } = yield* HyperdriveConnection; |
| 19 | const hd = yield* Cloudflare.Hyperdrive.Connect(connection); |
| 20 | return { |
| 21 | fetch: Effect.gen(function* () { |
| 22 | const request = yield* HttpServerRequest; |
| 23 | const url = new URL(request.url, "http://x"); |
| 24 | const handled = yield* connectionRoutes(hd, url); |
| 25 | return handled ?? HttpServerResponse.text("Not Found", { status: 404 }); |
| 26 | }), |
| 27 | }; |
| 28 | }).pipe(Effect.provide(Cloudflare.Hyperdrive.ConnectBinding)), |
| 29 | ) {} |