| 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 { Repos } from "./shared.ts"; |
| 6 | import { artifactsRoutes } from "./routes.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Effect-native Worker fixture for the Artifacts namespace binding. Yielding |
| 10 | * `Cloudflare.Artifacts.ReadWriteNamespace(Repos)` during Init registers the native |
| 11 | * `{ type: "artifacts" }` binding and returns the Effect-native |
| 12 | * {@link ReadWriteNamespaceClient}. Routes are shared with the async worker via |
| 13 | * `routes.ts`. |
| 14 | */ |
| 15 | export default class ArtifactsEffectWorker extends Cloudflare.Worker<ArtifactsEffectWorker>()( |
| 16 | "ArtifactsEffectWorker", |
| 17 | { main: import.meta.url }, |
| 18 | Effect.gen(function* () { |
| 19 | const repos = yield* Repos; |
| 20 | const client = yield* Cloudflare.Artifacts.ReadWriteNamespace(repos); |
| 21 | return { |
| 22 | fetch: Effect.gen(function* () { |
| 23 | const request = yield* HttpServerRequest; |
| 24 | const url = new URL(request.url, "http://x"); |
| 25 | const handled = yield* artifactsRoutes(client, url); |
| 26 | return handled ?? HttpServerResponse.text("Not Found", { status: 404 }); |
| 27 | }), |
| 28 | }; |
| 29 | }).pipe(Effect.provide(Cloudflare.Artifacts.ReadWriteNamespaceBinding)), |
| 30 | ) {} |