| 1 | import type * as Bundle from "@/Bundle/Bundle.ts"; |
| 2 | import type { |
| 3 | SourceProvider, |
| 4 | WorkerSourceModule, |
| 5 | } from "@/Cloudflare/Workers/Source.ts"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | import * as crypto from "node:crypto"; |
| 9 | |
| 10 | /** |
| 11 | * Minimal external source-provider fixture for the loadSource unit |
| 12 | * tests: builds a one-module bundle whose content embeds the |
| 13 | * descriptor's `marker` option. |
| 14 | */ |
| 15 | const make: WorkerSourceModule["make"] = (options) => |
| 16 | Effect.sync((): SourceProvider => { |
| 17 | const marker = (options as { marker?: string } | undefined)?.marker ?? ""; |
| 18 | const content = `export default { fetch: () => new Response(${JSON.stringify(marker)}) };`; |
| 19 | const bundle = Effect.sync((): Bundle.BundleOutput => { |
| 20 | const hash = crypto.createHash("sha256").update(content).digest("hex"); |
| 21 | return { |
| 22 | files: [{ path: "main.js", content, hash }], |
| 23 | hash, |
| 24 | }; |
| 25 | }); |
| 26 | return { |
| 27 | ownsAssets: false, |
| 28 | build: () => |
| 29 | bundle.pipe( |
| 30 | Effect.map((output) => ({ |
| 31 | bundle: output, |
| 32 | assets: undefined, |
| 33 | hash: { |
| 34 | bundle: output.hash, |
| 35 | assets: undefined, |
| 36 | input: undefined, |
| 37 | additionalWorkspaces: undefined, |
| 38 | }, |
| 39 | })), |
| 40 | ), |
| 41 | hash: () => |
| 42 | bundle.pipe(Effect.map((output) => ({ bundle: output.hash }))), |
| 43 | dev: () => |
| 44 | bundle.pipe( |
| 45 | Effect.map((output) => ({ |
| 46 | mode: "bundle" as const, |
| 47 | bundles: Stream.make({ |
| 48 | _tag: "Success", |
| 49 | output, |
| 50 | } as Bundle.BundleWatchEvent), |
| 51 | })), |
| 52 | ), |
| 53 | }; |
| 54 | }); |
| 55 | |
| 56 | export default { make } satisfies WorkerSourceModule; |