1import * as Cloudflare from "@/Cloudflare/index.ts";
2import * as Effect from "effect/Effect";
3import { HttpServerRequest } from "effect/http/HttpServerRequest";
4import * as HttpServerResponse from "effect/http/HttpServerResponse";
5import { Repos } from "./shared.ts";
6import { 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 */
15export 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) {}