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

main
ts 984 lines 37.4 KB
Raw
1 import * as NodeCrypto from "node:crypto";
2 import { createRequire } from "node:module";
3 /**
4 * Alchemy Worker source provider for Astro projects.
5 *
6 * The default export implements alchemy's `WorkerSourceModule` contract
7 * (`packages/cloudflare/src/Workers/Source.ts`) **structurally** —
8 * this package deliberately does not depend on `alchemy`, so the types
9 * below mirror the shapes of `SourceProvider` / `SourceContext` /
10 * `DevContext` / `SourceBuildOutput` / `SourceHash` / `SourceDevHandle`
11 * instead of importing them. The contract is structural on purpose:
12 * version skew degrades to a load-time `SourceProviderError`, not a
13 * crash.
14 *
15 * Semantics mirror alchemy's built-in Vite source
16 * (`Workers/Sources/Vite.ts`):
17 *
18 * - `build()` runs the programmatic Astro build via this package's
19 * `Framework` service, maps `serverModules` (entry first) to the
20 * bundle files, reads `clientDirectory` into an asset manifest, and
21 * computes a rebuild-free `input` hash over the project tree.
22 * - `hash()` recomputes only the `input` hash (never builds) — the memo
23 * fast path that lets an unchanged project skip the build entirely.
24 * - `dev()` starts `astro dev` with the `ssr` environment executing
25 * inside workerd (via `@alchemy.run/cloudflare-runtime/vite`) and
26 * returns a `server`-mode handle.
27 *
28 * The `input` hash includes this package's version so upgrading the
29 * integration (which can change build output) busts the memo even when
30 * project sources are unchanged.
31 */
32 import type { CloudflareVitePluginOptions } from "@alchemy.run/cloudflare-runtime/vite";
33 import * as Data from "effect/Data";
34 import * as Effect from "effect/Effect";
35 import * as FileSystem from "effect/FileSystem";
36 import * as Path from "effect/Path";
37 import type { PlatformError } from "effect/PlatformError";
38 import * as Redacted from "effect/Redacted";
39 import type * as Scope from "effect/Scope";
40 import { resolveBuildEnv, runBuildChild } from "../core/BuildChild.ts";
41 import * as FrameworkCore from "../core/index.ts";
42 import * as Astro from "./Astro.ts";
43 import cloudflareTarget from "./cloudflare.ts";
44
45 // ─────────────────────────────────────────────────────────────────────
46 // Structural mirrors of alchemy's source-provider contract
47 // (packages/cloudflare/src/Workers/Source.ts). Keep in sync.
48 // ─────────────────────────────────────────────────────────────────────
49
50 /** Mirror of alchemy's `SourceHash`. */
51 export interface SourceHash {
52 readonly bundle: string | undefined;
53 readonly assets: string | undefined;
54 readonly input: string | undefined;
55 readonly additionalWorkspaces: Array<string> | undefined;
56 }
57
58 /** Mirror of alchemy's `Bundle.BundleFile`. */
59 export interface BundleFile {
60 readonly path: string;
61 readonly content: string | Uint8Array;
62 readonly hash: string;
63 }
64
65 /** Mirror of alchemy's `Bundle.BundleOutput`. */
66 export interface BundleOutput {
67 readonly files: [BundleFile, ...Array<BundleFile>];
68 readonly hash: string;
69 }
70
71 /** Mirror of alchemy's `AssetReadResult` (Workers/Assets.ts). */
72 export interface AssetReadResult {
73 readonly directory: string;
74 readonly config: Record<string, unknown> | undefined;
75 readonly manifest: Record<string, { hash: string; size: number }>;
76 readonly _headers: string | undefined;
77 readonly _redirects: string | undefined;
78 readonly hash: string;
79 }
80
81 /** Mirror of alchemy's `SourceBuildOutput`. */
82 export interface SourceBuildOutput {
83 readonly bundle: BundleOutput | undefined;
84 readonly assets: AssetReadResult | undefined;
85 readonly hash: SourceHash;
86 }
87
88 /** Mirror of alchemy's `SourceContext` (the slots this source reads). */
89 export interface SourceContext {
90 readonly id: string;
91 readonly workerName: string;
92 readonly compatibility: {
93 readonly date: string;
94 readonly flags: Array<string>;
95 };
96 readonly stack: { readonly name: string; readonly stage: string };
97 readonly env: Record<string, unknown> | undefined;
98 /** Raw `props.assets` — a directory string or an asset-config object. */
99 readonly assets: string | Record<string, unknown> | undefined;
100 }
101
102 type PluginWorker = Exclude<CloudflareVitePluginOptions["worker"], undefined>;
103
104 /** Mirror of alchemy's `DevContext` (the slots this source reads). */
105 export interface DevContext extends SourceContext {
106 readonly worker: {
107 readonly bindings: PluginWorker["bindings"];
108 readonly durableObjectNamespaces: PluginWorker["durableObjectNamespaces"];
109 readonly hyperdrives: PluginWorker["hyperdrives"];
110 readonly queueConsumers: Effect.Effect<Exclude<PluginWorker["queueConsumers"], undefined>>;
111 readonly assets: PluginWorker["assets"] | undefined;
112 /**
113 * The Worker's other runtime settings (crons, Workflows, tail consumers,
114 * Cache API, `request.cf`, zone, Access), spread whole into the runtime
115 * Worker.
116 */
117 readonly options: Pick<
118 PluginWorker,
119 "workflows" | "crons" | "tails" | "streamingTails" | "cache" | "cf" | "zone" | "access"
120 >;
121 };
122 readonly runtimeContext: CloudflareVitePluginOptions["context"];
123 }
124
125 /** Mirror of alchemy's `SourceDevHandle` (`server` mode only). */
126 export interface SourceDevHandle {
127 readonly mode: "server";
128 readonly url: URL;
129 }
130
131 /**
132 * Structural twin of alchemy's `Cloudflare.Workers.SourceProviderError`
133 * — same `_tag` and fields, so `Effect.catchTag` on the alchemy side
134 * matches errors raised here.
135 */
136 export class SourceProviderError extends Data.TaggedError(
137 "Cloudflare.Workers.SourceProviderError",
138 )<{
139 readonly provider: string;
140 readonly message: string;
141 readonly cause?: unknown;
142 }> {}
143
144 export type SourceError = PlatformError | SourceProviderError;
145
146 type SourceServices = FileSystem.FileSystem | Path.Path;
147
148 /** Mirror of alchemy's `SourceProvider` (with this module's closed error/requirement channels). */
149 export interface SourceProvider {
150 readonly ownsAssets: boolean;
151 readonly devAssetsBinding?: boolean;
152 readonly build: (
153 ctx: SourceContext,
154 ) => Effect.Effect<SourceBuildOutput, SourceError, SourceServices>;
155 readonly hash: (
156 ctx: SourceContext,
157 previous: SourceHash | undefined,
158 ) => Effect.Effect<Partial<SourceHash>, SourceError, SourceServices>;
159 readonly dev: (
160 ctx: DevContext,
161 ) => Effect.Effect<SourceDevHandle, SourceError, SourceServices | Scope.Scope>;
162 }
163
164 // ─────────────────────────────────────────────────────────────────────
165 // Options
166 // ─────────────────────────────────────────────────────────────────────
167
168 /**
169 * Controls which files are hashed to decide whether a rebuild is
170 * needed. Mirrors alchemy's `MemoOptions` semantics: by default every
171 * non-gitignored file under the root is hashed, plus the nearest
172 * package-manager lockfile.
173 */
174 export interface MemoOptions {
175 /** Glob patterns of files to hash, relative to the directory. @default all files */
176 readonly include?: Array<string>;
177 /** Glob patterns to exclude. @default gitignore rules up to the repo root */
178 readonly exclude?: Array<string>;
179 /**
180 * Whether to include the nearest lockfile in the hash.
181 * @default true when both `include` and `exclude` are unset
182 */
183 readonly lockfile?: boolean;
184 }
185
186 /**
187 * Provider options carried on the Worker's `source` descriptor. Must be
188 * JSON-serializable and JSON-stable (they persist in state and
189 * participate in the Worker's metadata hash).
190 */
191 export interface AstroSourceOptions {
192 /** Astro project root. Defaults to the process working directory. */
193 readonly rootDir?: string;
194 /**
195 * The user's own Worker entry (the user-entry seam): a module that wraps
196 * the Astro handler — imported from
197 * `@alchemy.run/frontend-frameworks/astro/entrypoints/server` — and adds
198 * exports of its own (Durable Object classes, a `scheduled` handler). A
199 * path resolves against the root; a bare specifier resolves as a package
200 * import. When unset, the vendored Astro server entrypoint is deployed.
201 */
202 readonly main?: string;
203 /** Rebuild-scope configuration (see {@link MemoOptions}). */
204 readonly memo?: MemoOptions & {
205 /**
206 * Additional workspace directories to hash. `"auto"` (default)
207 * auto-detects workspaces from the build's module graph; an explicit
208 * array pins them.
209 */
210 readonly workspaces?: "auto" | Array<MemoOptions & { readonly cwd: string }>;
211 };
212 /**
213 * The name of the KV binding injected into Astro's session config
214 * when present on the Worker env.
215 * @default "SESSION"
216 */
217 readonly sessionKVBindingName?: string;
218 /**
219 * Zero-config sessions: default Astro's session driver to Cloudflare KV
220 * (binding `sessionKVBindingName`) when none is configured. `false`
221 * leaves the session config untouched.
222 * @default true
223 */
224 readonly sessions?: boolean;
225 /**
226 * Auto-inject an in-memory local KV namespace for the session binding: in
227 * dev on the dev worker, at build time on the workerd prerender worker
228 * (so session-touching pages can prerender before the Worker's real
229 * namespace exists). Disable once the Worker's own bindings deliver a KV
230 * binding with that name (e.g. when alchemy provisions the session
231 * namespace and passes it through the dev bindings).
232 * @default true
233 */
234 readonly sessionDevKV?: boolean;
235 /**
236 * Runtime used to prerender static pages.
237 * @default "workerd"
238 */
239 readonly prerenderEnvironment?: "workerd" | "node";
240 /**
241 * JSON-serializable subset of Astro config merged into the in-memory
242 * `AstroInlineConfig`. The project's `astro.config.*` file loads
243 * natively; astro merges this inline overlay OVER it (scalars here
244 * override the file). The file itself participates in the `input`
245 * memo hash like any other project file, so these options stay
246 * JSON-serializable while the config file stays fully expressive.
247 */
248 readonly astro?: {
249 readonly site?: string;
250 readonly base?: string;
251 readonly output?: "server" | "static";
252 readonly srcDir?: string;
253 readonly publicDir?: string;
254 readonly outDir?: string;
255 readonly trailingSlash?: "always" | "never" | "ignore";
256 };
257 /**
258 * Path to an alternate Astro config file, resolved against
259 * {@link rootDir} when relative. Defaults to astro's own config
260 * discovery (`astro.config.*` in the project root).
261 */
262 readonly config?: string;
263 }
264
265 const PROVIDER = "@alchemy.run/frontend-frameworks/astro/source";
266
267 const packageMeta: { version: string; root: string | undefined } = (() => {
268 try {
269 const require = createRequire(import.meta.url);
270 const path = require.resolve("../../package.json");
271 return {
272 version: (require(path) as { version: string }).version,
273 root: path.slice(0, path.length - "package.json".length - 1),
274 };
275 } catch {
276 return { version: "unknown", root: undefined };
277 }
278 })();
279
280 const packageVersion = packageMeta.version;
281
282 // ─────────────────────────────────────────────────────────────────────
283 // Hashing primitives (mirror alchemy's Util/sha256 + Command/Memo)
284 // ─────────────────────────────────────────────────────────────────────
285
286 const sha256Hex = (input: string | Uint8Array): Effect.Effect<string> =>
287 Effect.sync(() => NodeCrypto.createHash("sha256").update(input).digest("hex"));
288
289 /** Recursively sort object keys so JSON.stringify is order-stable. */
290 const stableValue = (value: unknown): unknown => {
291 if (Array.isArray(value)) {
292 return value.map(stableValue);
293 }
294 if (value !== null && typeof value === "object" && !ArrayBuffer.isView(value)) {
295 return Object.fromEntries(
296 Object.entries(value as Record<string, unknown>)
297 .sort(([a], [b]) => a.localeCompare(b))
298 .map(([key, nested]) => [key, stableValue(nested)]),
299 );
300 }
301 return value;
302 };
303
304 const sha256Object = (input: unknown): Effect.Effect<string> =>
305 Effect.flatMap(
306 Effect.sync(() => JSON.stringify(stableValue(input))),
307 sha256Hex,
308 );
309
310 // ─────────────────────────────────────────────────────────────────────
311 // Glob / gitignore matching (self-contained approximation of the
312 // glob + gitignore semantics alchemy's `hashDirectory` uses)
313 // ─────────────────────────────────────────────────────────────────────
314
315 const escapeRegex = (char: string): string => (/[\\^$.|+()[\]{}]/.test(char) ? `\\${char}` : char);
316
317 /**
318 * Convert a glob body (no negation/anchoring — handled by callers) to a
319 * regex source. Supports `**`, `*`, `?`; everything else is literal.
320 */
321 const globToRegexSource = (glob: string): string => {
322 let out = "";
323 let i = 0;
324 while (i < glob.length) {
325 const c = glob[i]!;
326 if (c === "*" && glob[i + 1] === "*") {
327 const prevBoundary = i === 0 || glob[i - 1] === "/";
328 let j = i + 2;
329 if (prevBoundary && glob[j] === "/") {
330 // `**/` segment — zero or more whole path segments.
331 out += "(?:[^/]+/)*";
332 i = j + 1;
333 continue;
334 }
335 out += ".*";
336 i = j;
337 continue;
338 }
339 if (c === "*") {
340 out += "[^/]*";
341 i++;
342 continue;
343 }
344 if (c === "?") {
345 out += "[^/]";
346 i++;
347 continue;
348 }
349 out += escapeRegex(c);
350 i++;
351 }
352 return out;
353 };
354
355 interface IgnoreRule {
356 readonly regex: RegExp;
357 readonly dirOnly: boolean;
358 readonly negated: boolean;
359 }
360
361 /** Compile one gitignore-style rule line. Returns `undefined` for blanks/comments. */
362 const compileIgnoreRule = (raw: string): IgnoreRule | undefined => {
363 let line = raw.trim();
364 if (line.length === 0 || line.startsWith("#")) {
365 return undefined;
366 }
367 let negated = false;
368 if (line.startsWith("!")) {
369 negated = true;
370 line = line.slice(1).trim();
371 if (line.length === 0) return undefined;
372 }
373 let dirOnly = false;
374 if (line.endsWith("/")) {
375 dirOnly = true;
376 line = line.slice(0, -1);
377 }
378 line = line.replaceAll("\\", "/");
379 // A slash anywhere (after stripping the trailing one) anchors the
380 // pattern to the ignore root; otherwise it matches at any depth.
381 const anchored = line.startsWith("/") || line.includes("/");
382 if (line.startsWith("/")) {
383 line = line.slice(1);
384 }
385 if (line.length === 0) return undefined;
386 const body = globToRegexSource(line);
387 const source = anchored ? `^${body}$` : `(?:^|/)${body}$`;
388 try {
389 return { regex: new RegExp(source), dirOnly, negated };
390 } catch {
391 return undefined;
392 }
393 };
394
395 const compileIgnoreRules = (rules: ReadonlyArray<string>): Array<IgnoreRule> =>
396 rules.map(compileIgnoreRule).filter((rule): rule is IgnoreRule => rule !== undefined);
397
398 /**
399 * Gitignore matching: the LAST matching rule wins; `dirOnly` rules only
400 * match directories. Paths are `/`-separated and relative (no leading
401 * slash).
402 */
403 const isIgnored = (
404 rules: ReadonlyArray<IgnoreRule>,
405 path: string,
406 isDirectory: boolean,
407 ): boolean => {
408 let ignored = false;
409 for (const rule of rules) {
410 if (rule.dirOnly && !isDirectory) continue;
411 if (rule.regex.test(path)) {
412 ignored = !rule.negated;
413 }
414 }
415 return ignored;
416 };
417
418 /** Compile positive glob patterns (memo include/exclude), anchored to the root. */
419 const compileGlobs = (globs: ReadonlyArray<string>): Array<RegExp> =>
420 globs.flatMap((glob) => {
421 try {
422 return [new RegExp(`^${globToRegexSource(glob.replace(/^\.\//, ""))}$`)];
423 } catch {
424 return [];
425 }
426 });
427
428 const matchesAny = (regexes: ReadonlyArray<RegExp>, path: string): boolean =>
429 regexes.some((regex) => regex.test(path));
430
431 // ─────────────────────────────────────────────────────────────────────
432 // Directory walking + input hashing (mirror of alchemy's hashDirectory
433 // / hashViteInput recipe: `${relative}:${hash}` per workspace, sorted,
434 // hashed together — machine-independent, never hashes absolute paths)
435 // ─────────────────────────────────────────────────────────────────────
436
437 const LOCKFILES = ["bun.lock", "bun.lockb", "package-lock.json", "pnpm-lock.yaml", "yarn.lock"];
438
439 const readGitIgnoreRules = (
440 cwd: string,
441 ): Effect.Effect<Array<string>, PlatformError, SourceServices> =>
442 Effect.gen(function* () {
443 const fs = yield* FileSystem.FileSystem;
444 const path = yield* Path.Path;
445 const rules = yield* fs.readFileString(path.join(cwd, ".gitignore")).pipe(
446 Effect.map((file) => file.split("\n")),
447 Effect.catchTag("PlatformError", () => Effect.succeed([] as Array<string>)),
448 );
449 const parent = path.dirname(cwd);
450 if (parent === cwd || (yield* fs.exists(path.join(cwd, ".git")))) {
451 return rules;
452 }
453 return [...(yield* readGitIgnoreRules(parent)), ...rules];
454 });
455
456 const findUp = (
457 cwd: string,
458 filenames: ReadonlyArray<string>,
459 ): Effect.Effect<string | undefined, PlatformError, SourceServices> =>
460 Effect.gen(function* () {
461 const fs = yield* FileSystem.FileSystem;
462 const path = yield* Path.Path;
463 for (const filename of filenames) {
464 const candidate = path.join(cwd, filename);
465 if (yield* fs.exists(candidate)) {
466 return candidate;
467 }
468 }
469 const parent = path.dirname(cwd);
470 if (parent === cwd) {
471 return undefined;
472 }
473 return yield* findUp(parent, filenames);
474 });
475
476 /**
477 * List files under `cwd` (relative, `/`-separated, sorted), pruning
478 * ignored directories during the walk so `node_modules`-scale trees are
479 * never traversed.
480 */
481 const listFiles = (
482 cwd: string,
483 options: {
484 readonly include: ReadonlyArray<RegExp> | undefined;
485 readonly ignoreRules: ReadonlyArray<IgnoreRule>;
486 readonly exclude: ReadonlyArray<RegExp>;
487 },
488 ): Effect.Effect<Array<string>, PlatformError, SourceServices> =>
489 Effect.gen(function* () {
490 const fs = yield* FileSystem.FileSystem;
491 const path = yield* Path.Path;
492 const files: Array<string> = [];
493 const walk = (relative: string): Effect.Effect<void, PlatformError> =>
494 Effect.gen(function* () {
495 const absolute = relative === "" ? cwd : path.join(cwd, relative);
496 const entries = yield* fs.readDirectory(absolute);
497 for (const entry of entries.sort()) {
498 const rel = relative === "" ? entry : `${relative}/${entry}`;
499 const stat = yield* fs
500 .stat(path.join(cwd, rel))
501 .pipe(Effect.catchTag("PlatformError", () => Effect.undefined));
502 if (stat === undefined) continue;
503 if (stat.type === "Directory") {
504 if (
505 isIgnored(options.ignoreRules, rel, true) ||
506 matchesAny(options.exclude, rel) ||
507 matchesAny(options.exclude, `${rel}/`)
508 ) {
509 continue;
510 }
511 yield* walk(rel);
512 continue;
513 }
514 if (stat.type !== "File") continue;
515 if (isIgnored(options.ignoreRules, rel, false)) continue;
516 if (matchesAny(options.exclude, rel)) continue;
517 if (options.include !== undefined && !matchesAny(options.include, rel)) {
518 continue;
519 }
520 files.push(rel);
521 }
522 });
523 yield* walk("");
524 return files.sort();
525 });
526
527 /**
528 * Deterministic content hash of a directory per the given memo options —
529 * the structural mirror of alchemy's `hashDirectory`.
530 */
531 const hashDirectory = (
532 cwd: string,
533 memo: MemoOptions | undefined,
534 ): Effect.Effect<string, PlatformError, SourceServices> =>
535 Effect.gen(function* () {
536 const fs = yield* FileSystem.FileSystem;
537 const path = yield* Path.Path;
538 const include = memo?.include ? compileGlobs(memo.include) : undefined;
539 const exclude = memo?.exclude ? compileGlobs(memo.exclude) : [];
540 // Gitignore-based exclusion applies whenever no explicit exclude
541 // globs are given (matching alchemy's resolveMemoOptions).
542 const ignoreRules = memo?.exclude
543 ? []
544 : compileIgnoreRules([".git/", ...(yield* readGitIgnoreRules(cwd))]);
545 const useLockfile = memo?.lockfile ?? !(memo?.include || memo?.exclude);
546 const files = yield* listFiles(cwd, { include, ignoreRules, exclude });
547 if (useLockfile) {
548 const lockfile = yield* findUp(cwd, LOCKFILES);
549 if (lockfile !== undefined) {
550 const rel = path.relative(cwd, lockfile).replaceAll("\\", "/");
551 if (!files.includes(rel)) {
552 files.push(rel);
553 }
554 }
555 }
556 const hashes = yield* Effect.forEach(
557 files,
558 (file) =>
559 fs.readFile(path.resolve(cwd, file)).pipe(
560 Effect.flatMap(sha256Hex),
561 Effect.map((hash) => `${file}:${hash}`),
562 ),
563 { concurrency: 16 },
564 );
565 return yield* sha256Object(hashes);
566 });
567
568 /**
569 * The `input` hash: root tree + workspace trees + this package's
570 * version. Mirrors alchemy's `hashViteInput` recipe (workspace hashes
571 * are `${relativePath}:${hash}`, sorted) with the provider version as
572 * extra material so integration upgrades bust the memo. A custom Worker
573 * entry (`main`) is build-affecting material too; it is only mixed in when
574 * set, so the default hash is unchanged.
575 */
576 const hashAstroInput = (
577 rootDir: string,
578 memo: AstroSourceOptions["memo"],
579 main: string | undefined,
580 additionalWorkspaces: Effect.Effect<Iterable<string>, PlatformError, SourceServices>,
581 ): Effect.Effect<
582 { hash: string; workspaces: Array<string> | undefined },
583 PlatformError,
584 SourceServices
585 > =>
586 Effect.gen(function* () {
587 const path = yield* Path.Path;
588 const salt = [`${PROVIDER}@${packageVersion}`, ...(main === undefined ? [] : [`main:${main}`])];
589 // Normalize workspace directories to a root-relative, `/`-separated
590 // form so the hash label is identical whether the workspace came
591 // from the build (absolute) or from persisted state (relative) —
592 // and machine-independent (never hashes absolute paths).
593 const toRelative = (cwd: string): string =>
594 (path.isAbsolute(cwd) ? path.relative(rootDir, cwd) : path.normalize(cwd)).replaceAll(
595 "\\",
596 "/",
597 );
598 const hashWorkspaceDirectory = (cwdRelative: string, options?: MemoOptions) =>
599 hashDirectory(path.resolve(rootDir, cwdRelative), options).pipe(
600 Effect.map((hash) => `${cwdRelative}:${hash}`),
601 );
602 const hashRoot = hashDirectory(rootDir, memo).pipe(Effect.map((hash) => `:${hash}`));
603 if (Array.isArray(memo?.workspaces)) {
604 const [root, ...workspaces] = yield* Effect.all(
605 [
606 hashRoot,
607 ...memo.workspaces.map(({ cwd, ...options }) =>
608 hashWorkspaceDirectory(toRelative(cwd), options),
609 ),
610 ],
611 { concurrency: "unbounded" },
612 );
613 const hash = yield* sha256Object([...salt, root, ...workspaces.sort()]);
614 return { hash, workspaces: undefined };
615 }
616 const [root, workspaces] = yield* Effect.all([hashRoot, additionalWorkspaces], {
617 concurrency: "unbounded",
618 });
619 // Exclude this integration package's own tree from auto-detected
620 // workspaces (it escapes the collector's node_modules filter when
621 // workspace-linked): its influence on the build is covered by the
622 // version salt, and hashing its build artifacts would churn.
623 const isOwnPackage = (cwd: string): boolean => {
624 if (packageMeta.root === undefined) return false;
625 const resolved = path.resolve(rootDir, cwd);
626 return resolved === packageMeta.root || resolved.startsWith(packageMeta.root + path.sep);
627 };
628 const relativeWorkspaces = [
629 ...new Set(
630 Array.from(workspaces)
631 .filter((cwd) => !isOwnPackage(cwd))
632 .map(toRelative),
633 ),
634 ].sort();
635 const workspaceHashes = yield* Effect.forEach(
636 relativeWorkspaces,
637 (cwd) => hashWorkspaceDirectory(cwd),
638 { concurrency: "unbounded" },
639 );
640 const hash = yield* sha256Object([...salt, root, ...workspaceHashes.sort()]);
641 return { hash, workspaces: relativeWorkspaces };
642 });
643
644 // ─────────────────────────────────────────────────────────────────────
645 // Asset reading (mirror of alchemy's `readAssets`: per-file sha256
646 // truncated to 32 hex chars — the hash format Cloudflare's asset-upload
647 // sessions expect — with `.assetsignore` / `_headers` / `_redirects`
648 // handling, and a manifest-shaped overall hash)
649 // ─────────────────────────────────────────────────────────────────────
650
651 const MAX_ASSET_SIZE = 1024 * 1024 * 25; // 25MB
652 const MAX_ASSET_COUNT = 20_000;
653
654 const maybeReadString = (file: string): Effect.Effect<string | undefined, never, SourceServices> =>
655 Effect.flatMap(FileSystem.FileSystem, (fs) =>
656 fs.readFileString(file).pipe(Effect.catchTag("PlatformError", () => Effect.undefined)),
657 );
658
659 const readClientAssets = (
660 directory: string,
661 config: Record<string, unknown> | undefined,
662 ): Effect.Effect<AssetReadResult, SourceError, SourceServices> =>
663 Effect.gen(function* () {
664 const fs = yield* FileSystem.FileSystem;
665 const path = yield* Path.Path;
666 const [ignoreFile, _headers, _redirects] = yield* Effect.all([
667 maybeReadString(path.join(directory, ".assetsignore")),
668 maybeReadString(path.join(directory, "_headers")),
669 maybeReadString(path.join(directory, "_redirects")),
670 ]);
671 const ignoreRules = compileIgnoreRules([
672 ".assetsignore",
673 "_headers",
674 "_redirects",
675 ...(ignoreFile?.split("\n") ?? []),
676 ]);
677 const files = yield* listFiles(directory, {
678 include: undefined,
679 ignoreRules,
680 exclude: [],
681 });
682 if (files.length > MAX_ASSET_COUNT) {
683 return yield* new SourceProviderError({
684 provider: PROVIDER,
685 message: `Too many assets in ${directory} (maximum is ${MAX_ASSET_COUNT}, found ${files.length})`,
686 });
687 }
688 const manifest: Record<string, { hash: string; size: number }> = {};
689 yield* Effect.forEach(
690 files,
691 Effect.fn(function* (name) {
692 const file = path.join(directory, name);
693 const stat = yield* fs.stat(file);
694 const size = Number(stat.size);
695 if (size > MAX_ASSET_SIZE) {
696 return yield* new SourceProviderError({
697 provider: PROVIDER,
698 message: `Asset ${name} is too large (${(size / 1024 / 1024).toFixed(1)} MB; the maximum is ${MAX_ASSET_SIZE / 1024 / 1024} MB)`,
699 });
700 }
701 const content = yield* fs.readFile(file);
702 const hash = (yield* sha256Hex(content)).slice(0, 32);
703 manifest[`/${name}`] = { hash, size };
704 }),
705 { concurrency: 16 },
706 );
707 const sortedManifest = Object.fromEntries(
708 Object.entries(manifest).sort(([a], [b]) => a.localeCompare(b)),
709 );
710 const hash = yield* sha256Object({
711 config,
712 manifest: sortedManifest,
713 _headers,
714 _redirects,
715 });
716 return {
717 directory,
718 config,
719 manifest: sortedManifest,
720 _headers,
721 _redirects,
722 hash,
723 };
724 });
725
726 // ─────────────────────────────────────────────────────────────────────
727 // The provider
728 // ─────────────────────────────────────────────────────────────────────
729
730 /**
731 * Feed the Worker's literal env values into `process.env` so `astro:env`
732 * server secrets (`getSecret`) resolve while Astro runs in the node process
733 * — config loading and node prerendering read env from `process.env`. This
734 * is the role upstream's `loadWranglerEnv` (wrangler `vars` + `.dev.vars`)
735 * plays at `astro:config:done`. Strings pass through, `Redacted<string>`s
736 * are unwrapped, numbers/booleans are stringified; everything else
737 * (Effects, resource bindings) is skipped — those are delivered as real
738 * bindings at runtime, not build-time values. Exported for testing.
739 */
740 export const applyWorkerEnvToProcess = (
741 env: Record<string, unknown> | undefined,
742 ): Effect.Effect<void> =>
743 Effect.sync(() => {
744 for (const [key, value] of Object.entries(env ?? {})) {
745 if (typeof value === "string") {
746 process.env[key] = value;
747 } else if (Redacted.isRedacted(value) && typeof Redacted.value(value) === "string") {
748 process.env[key] = Redacted.value(value) as string;
749 } else if (typeof value === "number" || typeof value === "boolean") {
750 process.env[key] = String(value);
751 }
752 }
753 });
754
755 const failWith =
756 (message: string) =>
757 (cause: unknown): SourceProviderError =>
758 new SourceProviderError({
759 provider: PROVIDER,
760 message: cause instanceof Error && cause.message ? `${message}: ${cause.message}` : message,
761 cause,
762 });
763
764 /** Extract the asset-routing config from raw `props.assets` (drop non-config keys). */
765 const assetConfigFromProps = (
766 assets: SourceContext["assets"],
767 ): Record<string, unknown> | undefined => {
768 if (assets === undefined || typeof assets === "string") {
769 return undefined;
770 }
771 const { directory: _directory, path: _path, hash: _hash, ...config } = assets;
772 return Object.keys(config).length > 0 ? config : undefined;
773 };
774
775 /**
776 * The production build runs in a disposable child process with
777 * `cwd = project root` (see `core/BuildChild.ts`): the natively-loaded
778 * `astro.config.*` executes user plugins/integrations that may read the
779 * cwd, mutate `process.env`, or `process.chdir` — none of which may touch
780 * the engine process. The shared `core/BuildChildRunner`
781 * entry imports this module in the child and calls the exported
782 * {@link buildInChild}.
783 */
784 export interface AstroBuildChildConfig {
785 readonly rootDir: string;
786 readonly main: string | undefined;
787 readonly compatibilityDate: string;
788 readonly compatibilityFlags: Array<string>;
789 readonly env: Record<string, string>;
790 readonly sessionKVBindingName: string | undefined;
791 readonly sessions: boolean | undefined;
792 readonly sessionDevKV: boolean | undefined;
793 readonly prerenderEnvironment: "workerd" | "node" | undefined;
794 readonly astro: AstroSourceOptions["astro"];
795 readonly config: string | undefined;
796 }
797
798 export const buildInChild = (config: AstroBuildChildConfig) =>
799 Effect.gen(function* () {
800 // Worker env → process.env, confined to this disposable child (astro's
801 // config loading and integrations read env at build time).
802 yield* Effect.sync(() => {
803 for (const [key, value] of Object.entries(config.env)) {
804 process.env[key] = value;
805 }
806 });
807 const astro = yield* FrameworkCore.Framework.pipe(
808 Effect.provide(
809 Astro.layer({
810 root: config.rootDir,
811 target: cloudflareTarget({
812 worker: {
813 ...(config.main === undefined ? {} : { main: config.main }),
814 compatibilityDate: config.compatibilityDate,
815 compatibilityFlags: config.compatibilityFlags,
816 },
817 sessionKVBindingName: config.sessionKVBindingName,
818 sessions: config.sessions,
819 sessionDevKV: config.sessionDevKV,
820 prerenderEnvironment: config.prerenderEnvironment,
821 }),
822 astro: config.astro,
823 config: config.config,
824 }),
825 ),
826 );
827 return yield* astro.build({ root: config.rootDir });
828 });
829
830 const makeAstroSourceProvider = (options: AstroSourceOptions): SourceProvider => {
831 /** Canonical absolute project root (a relative `rootDir` resolves from cwd). */
832 const resolveRoot = Effect.map(Path.Path, (path) =>
833 path.resolve(options.rootDir ?? process.cwd()),
834 );
835
836 const framework = (rootDir: string, vite: CloudflareVitePluginOptions) =>
837 FrameworkCore.Framework.pipe(
838 Effect.provide(
839 Astro.layer({
840 root: rootDir,
841 // This module IS the alchemy Cloudflare Worker source provider, so
842 // it constructs the Cloudflare deploy target directly (as a value)
843 // rather than resolving the default specifier.
844 target: cloudflareTarget({
845 worker: options.main === undefined ? vite : { ...vite, main: options.main },
846 sessionKVBindingName: options.sessionKVBindingName,
847 sessions: options.sessions,
848 sessionDevKV: options.sessionDevKV,
849 prerenderEnvironment: options.prerenderEnvironment,
850 }),
851 astro: options.astro,
852 config: options.config,
853 }),
854 ),
855 );
856
857 return {
858 ownsAssets: true,
859 // Dev runs the Worker in workerd with the Vite plugin's assets service.
860 devAssetsBinding: true,
861
862 build: (ctx) =>
863 Effect.gen(function* () {
864 const path = yield* Path.Path;
865 const rootDir = yield* resolveRoot;
866 const output = yield* runBuildChild({
867 module: import.meta.url,
868 rootDir,
869 framework: "astro",
870 config: {
871 rootDir,
872 main: options.main,
873 compatibilityDate: ctx.compatibility.date,
874 compatibilityFlags: ctx.compatibility.flags,
875 env: resolveBuildEnv(ctx.env),
876 sessionKVBindingName: options.sessionKVBindingName,
877 sessions: options.sessions,
878 sessionDevKV: options.sessionDevKV,
879 prerenderEnvironment: options.prerenderEnvironment,
880 astro: options.astro,
881 config: options.config,
882 } satisfies AstroBuildChildConfig,
883 }).pipe(Effect.mapError((error) => failWith("Astro build failed")(error.cause ?? error)));
884 const files = (output.serverModules ?? []).map((module): BundleFile => ({
885 path: module.name,
886 content: module.content,
887 hash: module.hash,
888 }));
889 const [bundleHash, assets, input] = yield* Effect.all(
890 [
891 sha256Object(files.map((file) => ({ path: file.path, hash: file.hash }))),
892 output.clientDirectory
893 ? readClientAssets(
894 path.resolve(rootDir, output.clientDirectory),
895 assetConfigFromProps(ctx.assets),
896 )
897 : Effect.undefined,
898 hashAstroInput(
899 rootDir,
900 options.memo,
901 options.main,
902 Effect.succeed(output.externalWorkspaces ?? []),
903 ),
904 ],
905 { concurrency: "unbounded" },
906 );
907 const bundle: BundleOutput | undefined =
908 files.length > 0
909 ? {
910 files: files as [BundleFile, ...Array<BundleFile>],
911 hash: bundleHash,
912 }
913 : undefined;
914 if (bundle === undefined && assets === undefined) {
915 return yield* new SourceProviderError({
916 provider: PROVIDER,
917 message: "Astro build produced neither assets nor server output",
918 });
919 }
920 return {
921 bundle,
922 assets,
923 hash: {
924 bundle: bundle?.hash,
925 assets: assets?.hash,
926 input: input.hash,
927 additionalWorkspaces: input.workspaces,
928 },
929 } satisfies SourceBuildOutput;
930 }),
931
932 hash: (_ctx, previous) =>
933 Effect.gen(function* () {
934 const rootDir = yield* resolveRoot;
935 const { hash, workspaces } = yield* hashAstroInput(
936 rootDir,
937 options.memo,
938 options.main,
939 Effect.succeed(previous?.additionalWorkspaces ?? []),
940 );
941 return { input: hash, additionalWorkspaces: workspaces };
942 }),
943
944 dev: (ctx) =>
945 Effect.gen(function* () {
946 const rootDir = yield* resolveRoot;
947 yield* applyWorkerEnvToProcess(ctx.env);
948 const queueConsumers = yield* ctx.worker.queueConsumers;
949 const astro = yield* framework(rootDir, {
950 compatibilityDate: ctx.compatibility.date,
951 compatibilityFlags: ctx.compatibility.flags,
952 worker: {
953 ...ctx.worker.options,
954 name: ctx.workerName,
955 bindings: ctx.worker.bindings,
956 durableObjectNamespaces: ctx.worker.durableObjectNamespaces,
957 hyperdrives: ctx.worker.hyperdrives,
958 queueConsumers,
959 assets: ctx.worker.assets,
960 },
961 context: ctx.runtimeContext,
962 });
963 const server = yield* astro
964 .dev({ root: rootDir, port: 0 })
965 .pipe(Effect.mapError(failWith("Astro dev server failed to start")));
966 return {
967 mode: "server",
968 url: new URL(server.url),
969 } satisfies SourceDevHandle;
970 }),
971 };
972 };
973
974 /**
975 * Mirror of alchemy's `WorkerSourceModule`: the default export's
976 * `make(options)` builds a {@link SourceProvider} from the descriptor's
977 * JSON-serializable options.
978 */
979 const make = (
980 options: unknown,
981 ): Effect.Effect<SourceProvider, SourceProviderError, SourceServices> =>
982 Effect.sync(() => makeAstroSourceProvider((options ?? {}) as AstroSourceOptions));
983
984 export default { make };