@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 105 lines 4.31 KB
Raw
1 import {
2 makeDeployTarget,
3 type DeployTarget,
4 type DeployTargetServer,
5 } from "@alchemy.run/frontend-frameworks/core";
6 import * as Effect from "effect/Effect";
7 import { cast } from "effect/Function";
8 import * as Path from "effect/Path";
9 import { moduleTypeFromExtension, type MiniflareModule } from "../miniflare/miniflare-module.ts";
10 import * as Miniflare from "../miniflare/miniflare.ts";
11 import type * as Options from "./Options.ts";
12
13 /**
14 * The e2e harness's Cloudflare deploy target: a framework-core `DeployTarget`
15 * value whose `serve` boots miniflare over a `BuildOutput` (the
16 * preview-parity engine the harness has always used for `e2e preview` / the
17 * Playwright `live` mode). Serving built output is *architecturally* the
18 * target's concern — the implementation lives in the harness for now because
19 * miniflare is a harness dependency.
20 */
21 export interface CloudflareTarget extends DeployTarget<Options.CloudflareTargetOptions> {
22 /** Always available on the harness's cloudflare target. */
23 readonly serve: NonNullable<DeployTarget["serve"]>;
24 }
25
26 /**
27 * Miniflare requires a script even for an assets-only deploy, so when a
28 * `BuildOutput` carries no server modules (and the preview config hand-rolls
29 * none), this stub stands in behind `routerConfig.has_user_worker: false`.
30 * Every real request must be answered by the asset layer — a request reaching
31 * the stub means the routing is misconfigured, so it 500s loudly.
32 */
33 const ASSETS_ONLY_STUB: MiniflareModule = {
34 type: "ESModule",
35 path: "__assets-only-stub.js",
36 contents: `export default { fetch: () => new Response("assets-only build: no user worker — a request reached the stub, so asset routing is misconfigured", { status: 500 }) }`,
37 };
38
39 export const makeCloudflareTarget = (config: Options.CloudflareTargetOptions): CloudflareTarget =>
40 makeDeployTarget({
41 platform: "cloudflare",
42 config,
43 bundle: {
44 conditions: ["workerd", "worker", "module", "browser"],
45 external: ["cloudflare:"],
46 },
47 serve: (context) =>
48 Effect.gen(function* () {
49 const path = yield* Path.Path;
50 const preview = config.preview ?? {};
51 const build = context.output;
52 const modules = build.serverModules?.flatMap(
53 (module): MiniflareModule | Array<MiniflareModule> => {
54 const type = moduleTypeFromExtension(path.extname(module.name));
55 if (type === "SourceMap") {
56 return [];
57 }
58 return {
59 path: module.name,
60 type,
61 contents: module.content as string | Uint8Array<ArrayBuffer> | undefined,
62 };
63 },
64 );
65 // Assets-only build: no server modules in the BuildOutput and none
66 // hand-rolled in the preview config. The harness synthesizes the
67 // stub script miniflare requires and routes every request to the
68 // asset layer (`has_user_worker: false`) — fixtures don't hand-roll
69 // stubs.
70 const resolvedModules = modules ?? preview.modules;
71 const assetsOnly = resolvedModules === undefined || resolvedModules.length === 0;
72 const instance = yield* Effect.acquireDisposable(
73 Effect.promise(
74 async () =>
75 await Miniflare.createMiniflare({
76 ...preview,
77 assets:
78 preview.assets && build.clientDirectory
79 ? {
80 ...preview.assets,
81 directory: build.clientDirectory,
82 ...(assetsOnly
83 ? {
84 routerConfig: {
85 ...preview.assets.routerConfig,
86 has_user_worker: false,
87 },
88 }
89 : undefined),
90 }
91 : undefined,
92 modules: assetsOnly ? [ASSETS_ONLY_STUB] : resolvedModules,
93 }),
94 ),
95 );
96 return {
97 url: instance.url.toString(),
98 // miniflare's Response type differs nominally from the DOM's; the
99 // harness has always cast across this boundary.
100 fetch: cast<Miniflare.MiniflareInstance["fetch"], DeployTargetServer["fetch"]>(
101 instance.fetch.bind(instance),
102 ),
103 };
104 }),
105 });