sailfork / t3code-github / .repos/ alchemy-effect/ packages/ frontend-frameworks/ src/ astro/ source.ts

import * as NodeCrypto from "node:crypto";
import { createRequire } from "node:module";
/**
 * Alchemy Worker source provider for Astro projects.
 *
 * The default export implements alchemy's `WorkerSourceModule` contract
 * (`packages/cloudflare/src/Workers/Source.ts`) **structurally** —
 * this package deliberately does not depend on `alchemy`, so the types
 * below mirror the shapes of `SourceProvider` / `SourceContext` /
 * `DevContext` / `SourceBuildOutput` / `SourceHash` / `SourceDevHandle`
 * instead of importing them. The contract is structural on purpose:
 * version skew degrades to a load-time `SourceProviderError`, not a
 * crash.
 *
 * Semantics mirror alchemy's built-in Vite source
 * (`Workers/Sources/Vite.ts`):
 *
 * - `build()` runs the programmatic Astro build via this package's
 *   `Framework` service, maps `serverModules` (entry first) to the
 *   bundle files, reads `clientDirectory` into an asset manifest, and
 *   computes a rebuild-free `input` hash over the project tree.
 * - `hash()` recomputes only the `input` hash (never builds) — the memo
 *   fast path that lets an unchanged project skip the build entirely.
 * - `dev()` starts `astro dev` with the `ssr` environment executing
 *   inside workerd (via `@alchemy.run/cloudflare-runtime/vite`) and
 *   returns a `server`-mode handle.
 *
 * The `input` hash includes this package's version so upgrading the
 * integration (which can change build output) busts the memo even when
 * project sources are unchanged.
 */
import type { CloudflareVitePluginOptions } from "@alchemy.run/cloudflare-runtime/vite";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import type { PlatformError } from "effect/PlatformError";
import * as Redacted from "effect/Redacted";
import type * as Scope from "effect/Scope";
import { resolveBuildEnv, runBuildChild } from "../core/BuildChild.ts";
import * as FrameworkCore from "../core/index.ts";
import * as Astro from "./Astro.ts";
import cloudflareTarget from "./cloudflare.ts";

// ─────────────────────────────────────────────────────────────────────
// Structural mirrors of alchemy's source-provider contract
// (packages/cloudflare/src/Workers/Source.ts). Keep in sync.
// ─────────────────────────────────────────────────────────────────────

/** Mirror of alchemy's `SourceHash`. */
export interface SourceHash {
  readonly bundle: string | undefined;
  readonly assets: string | undefined;
  readonly input: string | undefined;
  readonly additionalWorkspaces: Array<string> | undefined;
}

/** Mirror of alchemy's `Bundle.BundleFile`. */
export interface BundleFile {
  readonly path: string;
  readonly content: string | Uint8Array;
  readonly hash: string;
}

/** Mirror of alchemy's `Bundle.BundleOutput`. */
export interface BundleOutput {
  readonly files: [BundleFile, ...Array<BundleFile>];
  readonly hash: string;
}

/** Mirror of alchemy's `AssetReadResult` (Workers/Assets.ts). */
export interface AssetReadResult {
  readonly directory: string;
  readonly config: Record<string, unknown> | undefined;
  readonly manifest: Record<string, { hash: string; size: number }>;
  readonly _headers: string | undefined;
  readonly _redirects: string | undefined;
  readonly hash: string;
}

/** Mirror of alchemy's `SourceBuildOutput`. */
export interface SourceBuildOutput {
  readonly bundle: BundleOutput | undefined;
  readonly assets: AssetReadResult | undefined;
  readonly hash: SourceHash;
}

/** Mirror of alchemy's `SourceContext` (the slots this source reads). */
export interface SourceContext {
  readonly id: string;
  readonly workerName: string;
  readonly compatibility: {
    readonly date: string;
    readonly flags: Array<string>;
  };
  readonly stack: { readonly name: string; readonly stage: string };
  readonly env: Record<string, unknown> | undefined;
  /** Raw `props.assets` — a directory string or an asset-config object. */
  readonly assets: string | Record<string, unknown> | undefined;
}

type PluginWorker = Exclude<CloudflareVitePluginOptions["worker"], undefined>;

/** Mirror of alchemy's `DevContext` (the slots this source reads). */
export interface DevContext extends SourceContext {
  readonly worker: {
    readonly bindings: PluginWorker["bindings"];
    readonly durableObjectNamespaces: PluginWorker["durableObjectNamespaces"];
    readonly hyperdrives: PluginWorker["hyperdrives"];
    readonly queueConsumers: Effect.Effect<Exclude<PluginWorker["queueConsumers"], undefined>>;
    readonly assets: PluginWorker["assets"] | undefined;
    /**
     * The Worker's other runtime settings (crons, Workflows, tail consumers,
     * Cache API, `request.cf`, zone, Access), spread whole into the runtime
     * Worker.
     */
    readonly options: Pick<
      PluginWorker,
      "workflows" | "crons" | "tails" | "streamingTails" | "cache" | "cf" | "zone" | "access"
    >;
  };
  readonly runtimeContext: CloudflareVitePluginOptions["context"];
}

/** Mirror of alchemy's `SourceDevHandle` (`server` mode only). */
export interface SourceDevHandle {
  readonly mode: "server";
  readonly url: URL;
}

/**
 * Structural twin of alchemy's `Cloudflare.Workers.SourceProviderError`
 * — same `_tag` and fields, so `Effect.catchTag` on the alchemy side
 * matches errors raised here.
 */
export class SourceProviderError extends Data.TaggedError(
  "Cloudflare.Workers.SourceProviderError",
)<{
  readonly provider: string;
  readonly message: string;
  readonly cause?: unknown;
}> {}

export type SourceError = PlatformError | SourceProviderError;

type SourceServices = FileSystem.FileSystem | Path.Path;

/** Mirror of alchemy's `SourceProvider` (with this module's closed error/requirement channels). */
export interface SourceProvider {
  readonly ownsAssets: boolean;
  readonly devAssetsBinding?: boolean;
  readonly build: (
    ctx: SourceContext,
  ) => Effect.Effect<SourceBuildOutput, SourceError, SourceServices>;
  readonly hash: (
    ctx: SourceContext,
    previous: SourceHash | undefined,
  ) => Effect.Effect<Partial<SourceHash>, SourceError, SourceServices>;
  readonly dev: (
    ctx: DevContext,
  ) => Effect.Effect<SourceDevHandle, SourceError, SourceServices | Scope.Scope>;
}

// ─────────────────────────────────────────────────────────────────────
// Options
// ─────────────────────────────────────────────────────────────────────

/**
 * Controls which files are hashed to decide whether a rebuild is
 * needed. Mirrors alchemy's `MemoOptions` semantics: by default every
 * non-gitignored file under the root is hashed, plus the nearest
 * package-manager lockfile.
 */
export interface MemoOptions {
  /** Glob patterns of files to hash, relative to the directory. @default all files */
  readonly include?: Array<string>;
  /** Glob patterns to exclude. @default gitignore rules up to the repo root */
  readonly exclude?: Array<string>;
  /**
   * Whether to include the nearest lockfile in the hash.
   * @default true when both `include` and `exclude` are unset
   */
  readonly lockfile?: boolean;
}

/**
 * Provider options carried on the Worker's `source` descriptor. Must be
 * JSON-serializable and JSON-stable (they persist in state and
 * participate in the Worker's metadata hash).
 */
export interface AstroSourceOptions {
  /** Astro project root. Defaults to the process working directory. */
  readonly rootDir?: string;
  /**
   * The user's own Worker entry (the user-entry seam): a module that wraps
   * the Astro handler — imported from
   * `@alchemy.run/frontend-frameworks/astro/entrypoints/server` — and adds
   * exports of its own (Durable Object classes, a `scheduled` handler). A
   * path resolves against the root; a bare specifier resolves as a package
   * import. When unset, the vendored Astro server entrypoint is deployed.
   */
  readonly main?: string;
  /** Rebuild-scope configuration (see {@link MemoOptions}). */
  readonly memo?: MemoOptions & {
    /**
     * Additional workspace directories to hash. `"auto"` (default)
     * auto-detects workspaces from the build's module graph; an explicit
     * array pins them.
     */
    readonly workspaces?: "auto" | Array<MemoOptions & { readonly cwd: string }>;
  };
  /**
   * The name of the KV binding injected into Astro's session config
   * when present on the Worker env.
   * @default "SESSION"
   */
  readonly sessionKVBindingName?: string;
  /**
   * Zero-config sessions: default Astro's session driver to Cloudflare KV
   * (binding `sessionKVBindingName`) when none is configured. `false`
   * leaves the session config untouched.
   * @default true
   */
  readonly sessions?: boolean;
  /**
   * Auto-inject an in-memory local KV namespace for the session binding: in
   * dev on the dev worker, at build time on the workerd prerender worker
   * (so session-touching pages can prerender before the Worker's real
   * namespace exists). Disable once the Worker's own bindings deliver a KV
   * binding with that name (e.g. when alchemy provisions the session
   * namespace and passes it through the dev bindings).
   * @default true
   */
  readonly sessionDevKV?: boolean;
  /**
   * Runtime used to prerender static pages.
   * @default "workerd"
   */
  readonly prerenderEnvironment?: "workerd" | "node";
  /**
   * JSON-serializable subset of Astro config merged into the in-memory
   * `AstroInlineConfig`. The project's `astro.config.*` file loads
   * natively; astro merges this inline overlay OVER it (scalars here
   * override the file). The file itself participates in the `input`
   * memo hash like any other project file, so these options stay
   * JSON-serializable while the config file stays fully expressive.
   */
  readonly astro?: {
    readonly site?: string;
    readonly base?: string;
    readonly output?: "server" | "static";
    readonly srcDir?: string;
    readonly publicDir?: string;
    readonly outDir?: string;
    readonly trailingSlash?: "always" | "never" | "ignore";
  };
  /**
   * Path to an alternate Astro config file, resolved against
   * {@link rootDir} when relative. Defaults to astro's own config
   * discovery (`astro.config.*` in the project root).
   */
  readonly config?: string;
}

const PROVIDER = "@alchemy.run/frontend-frameworks/astro/source";

const packageMeta: { version: string; root: string | undefined } = (() => {
  try {
    const require = createRequire(import.meta.url);
    const path = require.resolve("../../package.json");
    return {
      version: (require(path) as { version: string }).version,
      root: path.slice(0, path.length - "package.json".length - 1),
    };
  } catch {
    return { version: "unknown", root: undefined };
  }
})();

const packageVersion = packageMeta.version;

// ─────────────────────────────────────────────────────────────────────
// Hashing primitives (mirror alchemy's Util/sha256 + Command/Memo)
// ─────────────────────────────────────────────────────────────────────

const sha256Hex = (input: string | Uint8Array): Effect.Effect<string> =>
  Effect.sync(() => NodeCrypto.createHash("sha256").update(input).digest("hex"));

/** Recursively sort object keys so JSON.stringify is order-stable. */
const stableValue = (value: unknown): unknown => {
  if (Array.isArray(value)) {
    return value.map(stableValue);
  }
  if (value !== null && typeof value === "object" && !ArrayBuffer.isView(value)) {
    return Object.fromEntries(
      Object.entries(value as Record<string, unknown>)
        .sort(([a], [b]) => a.localeCompare(b))
        .map(([key, nested]) => [key, stableValue(nested)]),
    );
  }
  return value;
};

const sha256Object = (input: unknown): Effect.Effect<string> =>
  Effect.flatMap(
    Effect.sync(() => JSON.stringify(stableValue(input))),
    sha256Hex,
  );

// ─────────────────────────────────────────────────────────────────────
// Glob / gitignore matching (self-contained approximation of the
// glob + gitignore semantics alchemy's `hashDirectory` uses)
// ─────────────────────────────────────────────────────────────────────

const escapeRegex = (char: string): string => (/[\\^$.|+()[\]{}]/.test(char) ? `\\${char}` : char);

/**
 * Convert a glob body (no negation/anchoring — handled by callers) to a
 * regex source. Supports `**`, `*`, `?`; everything else is literal.
 */
const globToRegexSource = (glob: string): string => {
  let out = "";
  let i = 0;
  while (i < glob.length) {
    const c = glob[i]!;
    if (c === "*" && glob[i + 1] === "*") {
      const prevBoundary = i === 0 || glob[i - 1] === "/";
      let j = i + 2;
      if (prevBoundary && glob[j] === "/") {
        // `**/` segment — zero or more whole path segments.
        out += "(?:[^/]+/)*";
        i = j + 1;
        continue;
      }
      out += ".*";
      i = j;
      continue;
    }
    if (c === "*") {
      out += "[^/]*";
      i++;
      continue;
    }
    if (c === "?") {
      out += "[^/]";
      i++;
      continue;
    }
    out += escapeRegex(c);
    i++;
  }
  return out;
};

interface IgnoreRule {
  readonly regex: RegExp;
  readonly dirOnly: boolean;
  readonly negated: boolean;
}

/** Compile one gitignore-style rule line. Returns `undefined` for blanks/comments. */
const compileIgnoreRule = (raw: string): IgnoreRule | undefined => {
  let line = raw.trim();
  if (line.length === 0 || line.startsWith("#")) {
    return undefined;
  }
  let negated = false;
  if (line.startsWith("!")) {
    negated = true;
    line = line.slice(1).trim();
    if (line.length === 0) return undefined;
  }
  let dirOnly = false;
  if (line.endsWith("/")) {
    dirOnly = true;
    line = line.slice(0, -1);
  }
  line = line.replaceAll("\\", "/");
  // A slash anywhere (after stripping the trailing one) anchors the
  // pattern to the ignore root; otherwise it matches at any depth.
  const anchored = line.startsWith("/") || line.includes("/");
  if (line.startsWith("/")) {
    line = line.slice(1);
  }
  if (line.length === 0) return undefined;
  const body = globToRegexSource(line);
  const source = anchored ? `^${body}

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	.repos/alchemy-effect/packages/frontend-frameworks/src/astro/source.ts at main · sailfork/t3code-github · Sailfork
	
		
: `(?:^|/)${body} .repos/alchemy-effect/packages/frontend-frameworks/src/astro/source.ts at main · sailfork/t3code-github · Sailfork
; try { return { regex: new RegExp(source), dirOnly, negated }; } catch { return undefined; } }; const compileIgnoreRules = (rules: ReadonlyArray<string>): Array<IgnoreRule> => rules.map(compileIgnoreRule).filter((rule): rule is IgnoreRule => rule !== undefined); /** * Gitignore matching: the LAST matching rule wins; `dirOnly` rules only * match directories. Paths are `/`-separated and relative (no leading * slash). */ const isIgnored = ( rules: ReadonlyArray<IgnoreRule>, path: string, isDirectory: boolean, ): boolean => { let ignored = false; for (const rule of rules) { if (rule.dirOnly && !isDirectory) continue; if (rule.regex.test(path)) { ignored = !rule.negated; } } return ignored; }; /** Compile positive glob patterns (memo include/exclude), anchored to the root. */ const compileGlobs = (globs: ReadonlyArray<string>): Array<RegExp> => globs.flatMap((glob) => { try { return [new RegExp(`^${globToRegexSource(glob.replace(/^\.\//, ""))} .repos/alchemy-effect/packages/frontend-frameworks/src/astro/source.ts at main · sailfork/t3code-github · Sailfork
)]; } catch { return []; } }); const matchesAny = (regexes: ReadonlyArray<RegExp>, path: string): boolean => regexes.some((regex) => regex.test(path)); // ───────────────────────────────────────────────────────────────────── // Directory walking + input hashing (mirror of alchemy's hashDirectory // / hashViteInput recipe: `${relative}:${hash}` per workspace, sorted, // hashed together — machine-independent, never hashes absolute paths) // ───────────────────────────────────────────────────────────────────── const LOCKFILES = ["bun.lock", "bun.lockb", "package-lock.json", "pnpm-lock.yaml", "yarn.lock"]; const readGitIgnoreRules = ( cwd: string, ): Effect.Effect<Array<string>, PlatformError, SourceServices> => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const rules = yield* fs.readFileString(path.join(cwd, ".gitignore")).pipe( Effect.map((file) => file.split("\n")), Effect.catchTag("PlatformError", () => Effect.succeed([] as Array<string>)), ); const parent = path.dirname(cwd); if (parent === cwd || (yield* fs.exists(path.join(cwd, ".git")))) { return rules; } return [...(yield* readGitIgnoreRules(parent)), ...rules]; }); const findUp = ( cwd: string, filenames: ReadonlyArray<string>, ): Effect.Effect<string | undefined, PlatformError, SourceServices> => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; for (const filename of filenames) { const candidate = path.join(cwd, filename); if (yield* fs.exists(candidate)) { return candidate; } } const parent = path.dirname(cwd); if (parent === cwd) { return undefined; } return yield* findUp(parent, filenames); }); /** * List files under `cwd` (relative, `/`-separated, sorted), pruning * ignored directories during the walk so `node_modules`-scale trees are * never traversed. */ const listFiles = ( cwd: string, options: { readonly include: ReadonlyArray<RegExp> | undefined; readonly ignoreRules: ReadonlyArray<IgnoreRule>; readonly exclude: ReadonlyArray<RegExp>; }, ): Effect.Effect<Array<string>, PlatformError, SourceServices> => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const files: Array<string> = []; const walk = (relative: string): Effect.Effect<void, PlatformError> => Effect.gen(function* () { const absolute = relative === "" ? cwd : path.join(cwd, relative); const entries = yield* fs.readDirectory(absolute); for (const entry of entries.sort()) { const rel = relative === "" ? entry : `${relative}/${entry}`; const stat = yield* fs .stat(path.join(cwd, rel)) .pipe(Effect.catchTag("PlatformError", () => Effect.undefined)); if (stat === undefined) continue; if (stat.type === "Directory") { if ( isIgnored(options.ignoreRules, rel, true) || matchesAny(options.exclude, rel) || matchesAny(options.exclude, `${rel}/`) ) { continue; } yield* walk(rel); continue; } if (stat.type !== "File") continue; if (isIgnored(options.ignoreRules, rel, false)) continue; if (matchesAny(options.exclude, rel)) continue; if (options.include !== undefined && !matchesAny(options.include, rel)) { continue; } files.push(rel); } }); yield* walk(""); return files.sort(); }); /** * Deterministic content hash of a directory per the given memo options — * the structural mirror of alchemy's `hashDirectory`. */ const hashDirectory = ( cwd: string, memo: MemoOptions | undefined, ): Effect.Effect<string, PlatformError, SourceServices> => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const include = memo?.include ? compileGlobs(memo.include) : undefined; const exclude = memo?.exclude ? compileGlobs(memo.exclude) : []; // Gitignore-based exclusion applies whenever no explicit exclude // globs are given (matching alchemy's resolveMemoOptions). const ignoreRules = memo?.exclude ? [] : compileIgnoreRules([".git/", ...(yield* readGitIgnoreRules(cwd))]); const useLockfile = memo?.lockfile ?? !(memo?.include || memo?.exclude); const files = yield* listFiles(cwd, { include, ignoreRules, exclude }); if (useLockfile) { const lockfile = yield* findUp(cwd, LOCKFILES); if (lockfile !== undefined) { const rel = path.relative(cwd, lockfile).replaceAll("\\", "/"); if (!files.includes(rel)) { files.push(rel); } } } const hashes = yield* Effect.forEach( files, (file) => fs.readFile(path.resolve(cwd, file)).pipe( Effect.flatMap(sha256Hex), Effect.map((hash) => `${file}:${hash}`), ), { concurrency: 16 }, ); return yield* sha256Object(hashes); }); /** * The `input` hash: root tree + workspace trees + this package's * version. Mirrors alchemy's `hashViteInput` recipe (workspace hashes * are `${relativePath}:${hash}`, sorted) with the provider version as * extra material so integration upgrades bust the memo. A custom Worker * entry (`main`) is build-affecting material too; it is only mixed in when * set, so the default hash is unchanged. */ const hashAstroInput = ( rootDir: string, memo: AstroSourceOptions["memo"], main: string | undefined, additionalWorkspaces: Effect.Effect<Iterable<string>, PlatformError, SourceServices>, ): Effect.Effect< { hash: string; workspaces: Array<string> | undefined }, PlatformError, SourceServices > => Effect.gen(function* () { const path = yield* Path.Path; const salt = [`${PROVIDER}@${packageVersion}`, ...(main === undefined ? [] : [`main:${main}`])]; // Normalize workspace directories to a root-relative, `/`-separated // form so the hash label is identical whether the workspace came // from the build (absolute) or from persisted state (relative) — // and machine-independent (never hashes absolute paths). const toRelative = (cwd: string): string => (path.isAbsolute(cwd) ? path.relative(rootDir, cwd) : path.normalize(cwd)).replaceAll( "\\", "/", ); const hashWorkspaceDirectory = (cwdRelative: string, options?: MemoOptions) => hashDirectory(path.resolve(rootDir, cwdRelative), options).pipe( Effect.map((hash) => `${cwdRelative}:${hash}`), ); const hashRoot = hashDirectory(rootDir, memo).pipe(Effect.map((hash) => `:${hash}`)); if (Array.isArray(memo?.workspaces)) { const [root, ...workspaces] = yield* Effect.all( [ hashRoot, ...memo.workspaces.map(({ cwd, ...options }) => hashWorkspaceDirectory(toRelative(cwd), options), ), ], { concurrency: "unbounded" }, ); const hash = yield* sha256Object([...salt, root, ...workspaces.sort()]); return { hash, workspaces: undefined }; } const [root, workspaces] = yield* Effect.all([hashRoot, additionalWorkspaces], { concurrency: "unbounded", }); // Exclude this integration package's own tree from auto-detected // workspaces (it escapes the collector's node_modules filter when // workspace-linked): its influence on the build is covered by the // version salt, and hashing its build artifacts would churn. const isOwnPackage = (cwd: string): boolean => { if (packageMeta.root === undefined) return false; const resolved = path.resolve(rootDir, cwd); return resolved === packageMeta.root || resolved.startsWith(packageMeta.root + path.sep); }; const relativeWorkspaces = [ ...new Set( Array.from(workspaces) .filter((cwd) => !isOwnPackage(cwd)) .map(toRelative), ), ].sort(); const workspaceHashes = yield* Effect.forEach( relativeWorkspaces, (cwd) => hashWorkspaceDirectory(cwd), { concurrency: "unbounded" }, ); const hash = yield* sha256Object([...salt, root, ...workspaceHashes.sort()]); return { hash, workspaces: relativeWorkspaces }; }); // ───────────────────────────────────────────────────────────────────── // Asset reading (mirror of alchemy's `readAssets`: per-file sha256 // truncated to 32 hex chars — the hash format Cloudflare's asset-upload // sessions expect — with `.assetsignore` / `_headers` / `_redirects` // handling, and a manifest-shaped overall hash) // ───────────────────────────────────────────────────────────────────── const MAX_ASSET_SIZE = 1024 * 1024 * 25; // 25MB const MAX_ASSET_COUNT = 20_000; const maybeReadString = (file: string): Effect.Effect<string | undefined, never, SourceServices> => Effect.flatMap(FileSystem.FileSystem, (fs) => fs.readFileString(file).pipe(Effect.catchTag("PlatformError", () => Effect.undefined)), ); const readClientAssets = ( directory: string, config: Record<string, unknown> | undefined, ): Effect.Effect<AssetReadResult, SourceError, SourceServices> => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const [ignoreFile, _headers, _redirects] = yield* Effect.all([ maybeReadString(path.join(directory, ".assetsignore")), maybeReadString(path.join(directory, "_headers")), maybeReadString(path.join(directory, "_redirects")), ]); const ignoreRules = compileIgnoreRules([ ".assetsignore", "_headers", "_redirects", ...(ignoreFile?.split("\n") ?? []), ]); const files = yield* listFiles(directory, { include: undefined, ignoreRules, exclude: [], }); if (files.length > MAX_ASSET_COUNT) { return yield* new SourceProviderError({ provider: PROVIDER, message: `Too many assets in ${directory} (maximum is ${MAX_ASSET_COUNT}, found ${files.length})`, }); } const manifest: Record<string, { hash: string; size: number }> = {}; yield* Effect.forEach( files, Effect.fn(function* (name) { const file = path.join(directory, name); const stat = yield* fs.stat(file); const size = Number(stat.size); if (size > MAX_ASSET_SIZE) { return yield* new SourceProviderError({ provider: PROVIDER, message: `Asset ${name} is too large (${(size / 1024 / 1024).toFixed(1)} MB; the maximum is ${MAX_ASSET_SIZE / 1024 / 1024} MB)`, }); } const content = yield* fs.readFile(file); const hash = (yield* sha256Hex(content)).slice(0, 32); manifest[`/${name}`] = { hash, size }; }), { concurrency: 16 }, ); const sortedManifest = Object.fromEntries( Object.entries(manifest).sort(([a], [b]) => a.localeCompare(b)), ); const hash = yield* sha256Object({ config, manifest: sortedManifest, _headers, _redirects, }); return { directory, config, manifest: sortedManifest, _headers, _redirects, hash, }; }); // ───────────────────────────────────────────────────────────────────── // The provider // ───────────────────────────────────────────────────────────────────── /** * Feed the Worker's literal env values into `process.env` so `astro:env` * server secrets (`getSecret`) resolve while Astro runs in the node process * — config loading and node prerendering read env from `process.env`. This * is the role upstream's `loadWranglerEnv` (wrangler `vars` + `.dev.vars`) * plays at `astro:config:done`. Strings pass through, `Redacted<string>`s * are unwrapped, numbers/booleans are stringified; everything else * (Effects, resource bindings) is skipped — those are delivered as real * bindings at runtime, not build-time values. Exported for testing. */ export const applyWorkerEnvToProcess = ( env: Record<string, unknown> | undefined, ): Effect.Effect<void> => Effect.sync(() => { for (const [key, value] of Object.entries(env ?? {})) { if (typeof value === "string") { process.env[key] = value; } else if (Redacted.isRedacted(value) && typeof Redacted.value(value) === "string") { process.env[key] = Redacted.value(value) as string; } else if (typeof value === "number" || typeof value === "boolean") { process.env[key] = String(value); } } }); const failWith = (message: string) => (cause: unknown): SourceProviderError => new SourceProviderError({ provider: PROVIDER, message: cause instanceof Error && cause.message ? `${message}: ${cause.message}` : message, cause, }); /** Extract the asset-routing config from raw `props.assets` (drop non-config keys). */ const assetConfigFromProps = ( assets: SourceContext["assets"], ): Record<string, unknown> | undefined => { if (assets === undefined || typeof assets === "string") { return undefined; } const { directory: _directory, path: _path, hash: _hash, ...config } = assets; return Object.keys(config).length > 0 ? config : undefined; }; /** * The production build runs in a disposable child process with * `cwd = project root` (see `core/BuildChild.ts`): the natively-loaded * `astro.config.*` executes user plugins/integrations that may read the * cwd, mutate `process.env`, or `process.chdir` — none of which may touch * the engine process. The shared `core/BuildChildRunner` * entry imports this module in the child and calls the exported * {@link buildInChild}. */ export interface AstroBuildChildConfig { readonly rootDir: string; readonly main: string | undefined; readonly compatibilityDate: string; readonly compatibilityFlags: Array<string>; readonly env: Record<string, string>; readonly sessionKVBindingName: string | undefined; readonly sessions: boolean | undefined; readonly sessionDevKV: boolean | undefined; readonly prerenderEnvironment: "workerd" | "node" | undefined; readonly astro: AstroSourceOptions["astro"]; readonly config: string | undefined; } export const buildInChild = (config: AstroBuildChildConfig) => Effect.gen(function* () { // Worker env → process.env, confined to this disposable child (astro's // config loading and integrations read env at build time). yield* Effect.sync(() => { for (const [key, value] of Object.entries(config.env)) { process.env[key] = value; } }); const astro = yield* FrameworkCore.Framework.pipe( Effect.provide( Astro.layer({ root: config.rootDir, target: cloudflareTarget({ worker: { ...(config.main === undefined ? {} : { main: config.main }), compatibilityDate: config.compatibilityDate, compatibilityFlags: config.compatibilityFlags, }, sessionKVBindingName: config.sessionKVBindingName, sessions: config.sessions, sessionDevKV: config.sessionDevKV, prerenderEnvironment: config.prerenderEnvironment, }), astro: config.astro, config: config.config, }), ), ); return yield* astro.build({ root: config.rootDir }); }); const makeAstroSourceProvider = (options: AstroSourceOptions): SourceProvider => { /** Canonical absolute project root (a relative `rootDir` resolves from cwd). */ const resolveRoot = Effect.map(Path.Path, (path) => path.resolve(options.rootDir ?? process.cwd()), ); const framework = (rootDir: string, vite: CloudflareVitePluginOptions) => FrameworkCore.Framework.pipe( Effect.provide( Astro.layer({ root: rootDir, // This module IS the alchemy Cloudflare Worker source provider, so // it constructs the Cloudflare deploy target directly (as a value) // rather than resolving the default specifier. target: cloudflareTarget({ worker: options.main === undefined ? vite : { ...vite, main: options.main }, sessionKVBindingName: options.sessionKVBindingName, sessions: options.sessions, sessionDevKV: options.sessionDevKV, prerenderEnvironment: options.prerenderEnvironment, }), astro: options.astro, config: options.config, }), ), ); return { ownsAssets: true, // Dev runs the Worker in workerd with the Vite plugin's assets service. devAssetsBinding: true, build: (ctx) => Effect.gen(function* () { const path = yield* Path.Path; const rootDir = yield* resolveRoot; const output = yield* runBuildChild({ module: import.meta.url, rootDir, framework: "astro", config: { rootDir, main: options.main, compatibilityDate: ctx.compatibility.date, compatibilityFlags: ctx.compatibility.flags, env: resolveBuildEnv(ctx.env), sessionKVBindingName: options.sessionKVBindingName, sessions: options.sessions, sessionDevKV: options.sessionDevKV, prerenderEnvironment: options.prerenderEnvironment, astro: options.astro, config: options.config, } satisfies AstroBuildChildConfig, }).pipe(Effect.mapError((error) => failWith("Astro build failed")(error.cause ?? error))); const files = (output.serverModules ?? []).map((module): BundleFile => ({ path: module.name, content: module.content, hash: module.hash, })); const [bundleHash, assets, input] = yield* Effect.all( [ sha256Object(files.map((file) => ({ path: file.path, hash: file.hash }))), output.clientDirectory ? readClientAssets( path.resolve(rootDir, output.clientDirectory), assetConfigFromProps(ctx.assets), ) : Effect.undefined, hashAstroInput( rootDir, options.memo, options.main, Effect.succeed(output.externalWorkspaces ?? []), ), ], { concurrency: "unbounded" }, ); const bundle: BundleOutput | undefined = files.length > 0 ? { files: files as [BundleFile, ...Array<BundleFile>], hash: bundleHash, } : undefined; if (bundle === undefined && assets === undefined) { return yield* new SourceProviderError({ provider: PROVIDER, message: "Astro build produced neither assets nor server output", }); } return { bundle, assets, hash: { bundle: bundle?.hash, assets: assets?.hash, input: input.hash, additionalWorkspaces: input.workspaces, }, } satisfies SourceBuildOutput; }), hash: (_ctx, previous) => Effect.gen(function* () { const rootDir = yield* resolveRoot; const { hash, workspaces } = yield* hashAstroInput( rootDir, options.memo, options.main, Effect.succeed(previous?.additionalWorkspaces ?? []), ); return { input: hash, additionalWorkspaces: workspaces }; }), dev: (ctx) => Effect.gen(function* () { const rootDir = yield* resolveRoot; yield* applyWorkerEnvToProcess(ctx.env); const queueConsumers = yield* ctx.worker.queueConsumers; const astro = yield* framework(rootDir, { compatibilityDate: ctx.compatibility.date, compatibilityFlags: ctx.compatibility.flags, worker: { ...ctx.worker.options, name: ctx.workerName, bindings: ctx.worker.bindings, durableObjectNamespaces: ctx.worker.durableObjectNamespaces, hyperdrives: ctx.worker.hyperdrives, queueConsumers, assets: ctx.worker.assets, }, context: ctx.runtimeContext, }); const server = yield* astro .dev({ root: rootDir, port: 0 }) .pipe(Effect.mapError(failWith("Astro dev server failed to start"))); return { mode: "server", url: new URL(server.url), } satisfies SourceDevHandle; }), }; }; /** * Mirror of alchemy's `WorkerSourceModule`: the default export's * `make(options)` builds a {@link SourceProvider} from the descriptor's * JSON-serializable options. */ const make = ( options: unknown, ): Effect.Effect<SourceProvider, SourceProviderError, SourceServices> => Effect.sync(() => makeAstroSourceProvider((options ?? {}) as AstroSourceOptions)); export default { make };
.repos/alchemy-effect/packages/frontend-frameworks/src/astro/source.ts at main · sailfork/t3code-github · Sailfork
: `(?:^|/)${body} .repos/alchemy-effect/packages/frontend-frameworks/src/astro/source.ts at main · sailfork/t3code-github · Sailfork
;\n try {\n return { regex: new RegExp(source), dirOnly, negated };\n } catch {\n return undefined;\n }\n};\n\nconst compileIgnoreRules = (rules: ReadonlyArray\u003cstring>): Array\u003cIgnoreRule> =>\n rules.map(compileIgnoreRule).filter((rule): rule is IgnoreRule => rule !== undefined);\n\n/**\n * Gitignore matching: the LAST matching rule wins; `dirOnly` rules only\n * match directories. Paths are `/`-separated and relative (no leading\n * slash).\n */\nconst isIgnored = (\n rules: ReadonlyArray\u003cIgnoreRule>,\n path: string,\n isDirectory: boolean,\n): boolean => {\n let ignored = false;\n for (const rule of rules) {\n if (rule.dirOnly && !isDirectory) continue;\n if (rule.regex.test(path)) {\n ignored = !rule.negated;\n }\n }\n return ignored;\n};\n\n/** Compile positive glob patterns (memo include/exclude), anchored to the root. */\nconst compileGlobs = (globs: ReadonlyArray\u003cstring>): Array\u003cRegExp> =>\n globs.flatMap((glob) => {\n try {\n return [new RegExp(`^${globToRegexSource(glob.replace(/^\\.\\//, \"\"))} .repos/alchemy-effect/packages/frontend-frameworks/src/astro/source.ts at main · sailfork/t3code-github · Sailfork
)];\n } catch {\n return [];\n }\n });\n\nconst matchesAny = (regexes: ReadonlyArray\u003cRegExp>, path: string): boolean =>\n regexes.some((regex) => regex.test(path));\n\n// ─────────────────────────────────────────────────────────────────────\n// Directory walking + input hashing (mirror of alchemy's hashDirectory\n// / hashViteInput recipe: `${relative}:${hash}` per workspace, sorted,\n// hashed together — machine-independent, never hashes absolute paths)\n// ─────────────────────────────────────────────────────────────────────\n\nconst LOCKFILES = [\"bun.lock\", \"bun.lockb\", \"package-lock.json\", \"pnpm-lock.yaml\", \"yarn.lock\"];\n\nconst readGitIgnoreRules = (\n cwd: string,\n): Effect.Effect\u003cArray\u003cstring>, PlatformError, SourceServices> =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n const rules = yield* fs.readFileString(path.join(cwd, \".gitignore\")).pipe(\n Effect.map((file) => file.split(\"\\n\")),\n Effect.catchTag(\"PlatformError\", () => Effect.succeed([] as Array\u003cstring>)),\n );\n const parent = path.dirname(cwd);\n if (parent === cwd || (yield* fs.exists(path.join(cwd, \".git\")))) {\n return rules;\n }\n return [...(yield* readGitIgnoreRules(parent)), ...rules];\n });\n\nconst findUp = (\n cwd: string,\n filenames: ReadonlyArray\u003cstring>,\n): Effect.Effect\u003cstring | undefined, PlatformError, SourceServices> =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n for (const filename of filenames) {\n const candidate = path.join(cwd, filename);\n if (yield* fs.exists(candidate)) {\n return candidate;\n }\n }\n const parent = path.dirname(cwd);\n if (parent === cwd) {\n return undefined;\n }\n return yield* findUp(parent, filenames);\n });\n\n/**\n * List files under `cwd` (relative, `/`-separated, sorted), pruning\n * ignored directories during the walk so `node_modules`-scale trees are\n * never traversed.\n */\nconst listFiles = (\n cwd: string,\n options: {\n readonly include: ReadonlyArray\u003cRegExp> | undefined;\n readonly ignoreRules: ReadonlyArray\u003cIgnoreRule>;\n readonly exclude: ReadonlyArray\u003cRegExp>;\n },\n): Effect.Effect\u003cArray\u003cstring>, PlatformError, SourceServices> =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n const files: Array\u003cstring> = [];\n const walk = (relative: string): Effect.Effect\u003cvoid, PlatformError> =>\n Effect.gen(function* () {\n const absolute = relative === \"\" ? cwd : path.join(cwd, relative);\n const entries = yield* fs.readDirectory(absolute);\n for (const entry of entries.sort()) {\n const rel = relative === \"\" ? entry : `${relative}/${entry}`;\n const stat = yield* fs\n .stat(path.join(cwd, rel))\n .pipe(Effect.catchTag(\"PlatformError\", () => Effect.undefined));\n if (stat === undefined) continue;\n if (stat.type === \"Directory\") {\n if (\n isIgnored(options.ignoreRules, rel, true) ||\n matchesAny(options.exclude, rel) ||\n matchesAny(options.exclude, `${rel}/`)\n ) {\n continue;\n }\n yield* walk(rel);\n continue;\n }\n if (stat.type !== \"File\") continue;\n if (isIgnored(options.ignoreRules, rel, false)) continue;\n if (matchesAny(options.exclude, rel)) continue;\n if (options.include !== undefined && !matchesAny(options.include, rel)) {\n continue;\n }\n files.push(rel);\n }\n });\n yield* walk(\"\");\n return files.sort();\n });\n\n/**\n * Deterministic content hash of a directory per the given memo options —\n * the structural mirror of alchemy's `hashDirectory`.\n */\nconst hashDirectory = (\n cwd: string,\n memo: MemoOptions | undefined,\n): Effect.Effect\u003cstring, PlatformError, SourceServices> =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n const include = memo?.include ? compileGlobs(memo.include) : undefined;\n const exclude = memo?.exclude ? compileGlobs(memo.exclude) : [];\n // Gitignore-based exclusion applies whenever no explicit exclude\n // globs are given (matching alchemy's resolveMemoOptions).\n const ignoreRules = memo?.exclude\n ? []\n : compileIgnoreRules([\".git/\", ...(yield* readGitIgnoreRules(cwd))]);\n const useLockfile = memo?.lockfile ?? !(memo?.include || memo?.exclude);\n const files = yield* listFiles(cwd, { include, ignoreRules, exclude });\n if (useLockfile) {\n const lockfile = yield* findUp(cwd, LOCKFILES);\n if (lockfile !== undefined) {\n const rel = path.relative(cwd, lockfile).replaceAll(\"\\\\\", \"/\");\n if (!files.includes(rel)) {\n files.push(rel);\n }\n }\n }\n const hashes = yield* Effect.forEach(\n files,\n (file) =>\n fs.readFile(path.resolve(cwd, file)).pipe(\n Effect.flatMap(sha256Hex),\n Effect.map((hash) => `${file}:${hash}`),\n ),\n { concurrency: 16 },\n );\n return yield* sha256Object(hashes);\n });\n\n/**\n * The `input` hash: root tree + workspace trees + this package's\n * version. Mirrors alchemy's `hashViteInput` recipe (workspace hashes\n * are `${relativePath}:${hash}`, sorted) with the provider version as\n * extra material so integration upgrades bust the memo. A custom Worker\n * entry (`main`) is build-affecting material too; it is only mixed in when\n * set, so the default hash is unchanged.\n */\nconst hashAstroInput = (\n rootDir: string,\n memo: AstroSourceOptions[\"memo\"],\n main: string | undefined,\n additionalWorkspaces: Effect.Effect\u003cIterable\u003cstring>, PlatformError, SourceServices>,\n): Effect.Effect\u003c\n { hash: string; workspaces: Array\u003cstring> | undefined },\n PlatformError,\n SourceServices\n> =>\n Effect.gen(function* () {\n const path = yield* Path.Path;\n const salt = [`${PROVIDER}@${packageVersion}`, ...(main === undefined ? [] : [`main:${main}`])];\n // Normalize workspace directories to a root-relative, `/`-separated\n // form so the hash label is identical whether the workspace came\n // from the build (absolute) or from persisted state (relative) —\n // and machine-independent (never hashes absolute paths).\n const toRelative = (cwd: string): string =>\n (path.isAbsolute(cwd) ? path.relative(rootDir, cwd) : path.normalize(cwd)).replaceAll(\n \"\\\\\",\n \"/\",\n );\n const hashWorkspaceDirectory = (cwdRelative: string, options?: MemoOptions) =>\n hashDirectory(path.resolve(rootDir, cwdRelative), options).pipe(\n Effect.map((hash) => `${cwdRelative}:${hash}`),\n );\n const hashRoot = hashDirectory(rootDir, memo).pipe(Effect.map((hash) => `:${hash}`));\n if (Array.isArray(memo?.workspaces)) {\n const [root, ...workspaces] = yield* Effect.all(\n [\n hashRoot,\n ...memo.workspaces.map(({ cwd, ...options }) =>\n hashWorkspaceDirectory(toRelative(cwd), options),\n ),\n ],\n { concurrency: \"unbounded\" },\n );\n const hash = yield* sha256Object([...salt, root, ...workspaces.sort()]);\n return { hash, workspaces: undefined };\n }\n const [root, workspaces] = yield* Effect.all([hashRoot, additionalWorkspaces], {\n concurrency: \"unbounded\",\n });\n // Exclude this integration package's own tree from auto-detected\n // workspaces (it escapes the collector's node_modules filter when\n // workspace-linked): its influence on the build is covered by the\n // version salt, and hashing its build artifacts would churn.\n const isOwnPackage = (cwd: string): boolean => {\n if (packageMeta.root === undefined) return false;\n const resolved = path.resolve(rootDir, cwd);\n return resolved === packageMeta.root || resolved.startsWith(packageMeta.root + path.sep);\n };\n const relativeWorkspaces = [\n ...new Set(\n Array.from(workspaces)\n .filter((cwd) => !isOwnPackage(cwd))\n .map(toRelative),\n ),\n ].sort();\n const workspaceHashes = yield* Effect.forEach(\n relativeWorkspaces,\n (cwd) => hashWorkspaceDirectory(cwd),\n { concurrency: \"unbounded\" },\n );\n const hash = yield* sha256Object([...salt, root, ...workspaceHashes.sort()]);\n return { hash, workspaces: relativeWorkspaces };\n });\n\n// ─────────────────────────────────────────────────────────────────────\n// Asset reading (mirror of alchemy's `readAssets`: per-file sha256\n// truncated to 32 hex chars — the hash format Cloudflare's asset-upload\n// sessions expect — with `.assetsignore` / `_headers` / `_redirects`\n// handling, and a manifest-shaped overall hash)\n// ─────────────────────────────────────────────────────────────────────\n\nconst MAX_ASSET_SIZE = 1024 * 1024 * 25; // 25MB\nconst MAX_ASSET_COUNT = 20_000;\n\nconst maybeReadString = (file: string): Effect.Effect\u003cstring | undefined, never, SourceServices> =>\n Effect.flatMap(FileSystem.FileSystem, (fs) =>\n fs.readFileString(file).pipe(Effect.catchTag(\"PlatformError\", () => Effect.undefined)),\n );\n\nconst readClientAssets = (\n directory: string,\n config: Record\u003cstring, unknown> | undefined,\n): Effect.Effect\u003cAssetReadResult, SourceError, SourceServices> =>\n Effect.gen(function* () {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n const [ignoreFile, _headers, _redirects] = yield* Effect.all([\n maybeReadString(path.join(directory, \".assetsignore\")),\n maybeReadString(path.join(directory, \"_headers\")),\n maybeReadString(path.join(directory, \"_redirects\")),\n ]);\n const ignoreRules = compileIgnoreRules([\n \".assetsignore\",\n \"_headers\",\n \"_redirects\",\n ...(ignoreFile?.split(\"\\n\") ?? []),\n ]);\n const files = yield* listFiles(directory, {\n include: undefined,\n ignoreRules,\n exclude: [],\n });\n if (files.length > MAX_ASSET_COUNT) {\n return yield* new SourceProviderError({\n provider: PROVIDER,\n message: `Too many assets in ${directory} (maximum is ${MAX_ASSET_COUNT}, found ${files.length})`,\n });\n }\n const manifest: Record\u003cstring, { hash: string; size: number }> = {};\n yield* Effect.forEach(\n files,\n Effect.fn(function* (name) {\n const file = path.join(directory, name);\n const stat = yield* fs.stat(file);\n const size = Number(stat.size);\n if (size > MAX_ASSET_SIZE) {\n return yield* new SourceProviderError({\n provider: PROVIDER,\n message: `Asset ${name} is too large (${(size / 1024 / 1024).toFixed(1)} MB; the maximum is ${MAX_ASSET_SIZE / 1024 / 1024} MB)`,\n });\n }\n const content = yield* fs.readFile(file);\n const hash = (yield* sha256Hex(content)).slice(0, 32);\n manifest[`/${name}`] = { hash, size };\n }),\n { concurrency: 16 },\n );\n const sortedManifest = Object.fromEntries(\n Object.entries(manifest).sort(([a], [b]) => a.localeCompare(b)),\n );\n const hash = yield* sha256Object({\n config,\n manifest: sortedManifest,\n _headers,\n _redirects,\n });\n return {\n directory,\n config,\n manifest: sortedManifest,\n _headers,\n _redirects,\n hash,\n };\n });\n\n// ─────────────────────────────────────────────────────────────────────\n// The provider\n// ─────────────────────────────────────────────────────────────────────\n\n/**\n * Feed the Worker's literal env values into `process.env` so `astro:env`\n * server secrets (`getSecret`) resolve while Astro runs in the node process\n * — config loading and node prerendering read env from `process.env`. This\n * is the role upstream's `loadWranglerEnv` (wrangler `vars` + `.dev.vars`)\n * plays at `astro:config:done`. Strings pass through, `Redacted\u003cstring>`s\n * are unwrapped, numbers/booleans are stringified; everything else\n * (Effects, resource bindings) is skipped — those are delivered as real\n * bindings at runtime, not build-time values. Exported for testing.\n */\nexport const applyWorkerEnvToProcess = (\n env: Record\u003cstring, unknown> | undefined,\n): Effect.Effect\u003cvoid> =>\n Effect.sync(() => {\n for (const [key, value] of Object.entries(env ?? {})) {\n if (typeof value === \"string\") {\n process.env[key] = value;\n } else if (Redacted.isRedacted(value) && typeof Redacted.value(value) === \"string\") {\n process.env[key] = Redacted.value(value) as string;\n } else if (typeof value === \"number\" || typeof value === \"boolean\") {\n process.env[key] = String(value);\n }\n }\n });\n\nconst failWith =\n (message: string) =>\n (cause: unknown): SourceProviderError =>\n new SourceProviderError({\n provider: PROVIDER,\n message: cause instanceof Error && cause.message ? `${message}: ${cause.message}` : message,\n cause,\n });\n\n/** Extract the asset-routing config from raw `props.assets` (drop non-config keys). */\nconst assetConfigFromProps = (\n assets: SourceContext[\"assets\"],\n): Record\u003cstring, unknown> | undefined => {\n if (assets === undefined || typeof assets === \"string\") {\n return undefined;\n }\n const { directory: _directory, path: _path, hash: _hash, ...config } = assets;\n return Object.keys(config).length > 0 ? config : undefined;\n};\n\n/**\n * The production build runs in a disposable child process with\n * `cwd = project root` (see `core/BuildChild.ts`): the natively-loaded\n * `astro.config.*` executes user plugins/integrations that may read the\n * cwd, mutate `process.env`, or `process.chdir` — none of which may touch\n * the engine process. The shared `core/BuildChildRunner`\n * entry imports this module in the child and calls the exported\n * {@link buildInChild}.\n */\nexport interface AstroBuildChildConfig {\n readonly rootDir: string;\n readonly main: string | undefined;\n readonly compatibilityDate: string;\n readonly compatibilityFlags: Array\u003cstring>;\n readonly env: Record\u003cstring, string>;\n readonly sessionKVBindingName: string | undefined;\n readonly sessions: boolean | undefined;\n readonly sessionDevKV: boolean | undefined;\n readonly prerenderEnvironment: \"workerd\" | \"node\" | undefined;\n readonly astro: AstroSourceOptions[\"astro\"];\n readonly config: string | undefined;\n}\n\nexport const buildInChild = (config: AstroBuildChildConfig) =>\n Effect.gen(function* () {\n // Worker env → process.env, confined to this disposable child (astro's\n // config loading and integrations read env at build time).\n yield* Effect.sync(() => {\n for (const [key, value] of Object.entries(config.env)) {\n process.env[key] = value;\n }\n });\n const astro = yield* FrameworkCore.Framework.pipe(\n Effect.provide(\n Astro.layer({\n root: config.rootDir,\n target: cloudflareTarget({\n worker: {\n ...(config.main === undefined ? {} : { main: config.main }),\n compatibilityDate: config.compatibilityDate,\n compatibilityFlags: config.compatibilityFlags,\n },\n sessionKVBindingName: config.sessionKVBindingName,\n sessions: config.sessions,\n sessionDevKV: config.sessionDevKV,\n prerenderEnvironment: config.prerenderEnvironment,\n }),\n astro: config.astro,\n config: config.config,\n }),\n ),\n );\n return yield* astro.build({ root: config.rootDir });\n });\n\nconst makeAstroSourceProvider = (options: AstroSourceOptions): SourceProvider => {\n /** Canonical absolute project root (a relative `rootDir` resolves from cwd). */\n const resolveRoot = Effect.map(Path.Path, (path) =>\n path.resolve(options.rootDir ?? process.cwd()),\n );\n\n const framework = (rootDir: string, vite: CloudflareVitePluginOptions) =>\n FrameworkCore.Framework.pipe(\n Effect.provide(\n Astro.layer({\n root: rootDir,\n // This module IS the alchemy Cloudflare Worker source provider, so\n // it constructs the Cloudflare deploy target directly (as a value)\n // rather than resolving the default specifier.\n target: cloudflareTarget({\n worker: options.main === undefined ? vite : { ...vite, main: options.main },\n sessionKVBindingName: options.sessionKVBindingName,\n sessions: options.sessions,\n sessionDevKV: options.sessionDevKV,\n prerenderEnvironment: options.prerenderEnvironment,\n }),\n astro: options.astro,\n config: options.config,\n }),\n ),\n );\n\n return {\n ownsAssets: true,\n // Dev runs the Worker in workerd with the Vite plugin's assets service.\n devAssetsBinding: true,\n\n build: (ctx) =>\n Effect.gen(function* () {\n const path = yield* Path.Path;\n const rootDir = yield* resolveRoot;\n const output = yield* runBuildChild({\n module: import.meta.url,\n rootDir,\n framework: \"astro\",\n config: {\n rootDir,\n main: options.main,\n compatibilityDate: ctx.compatibility.date,\n compatibilityFlags: ctx.compatibility.flags,\n env: resolveBuildEnv(ctx.env),\n sessionKVBindingName: options.sessionKVBindingName,\n sessions: options.sessions,\n sessionDevKV: options.sessionDevKV,\n prerenderEnvironment: options.prerenderEnvironment,\n astro: options.astro,\n config: options.config,\n } satisfies AstroBuildChildConfig,\n }).pipe(Effect.mapError((error) => failWith(\"Astro build failed\")(error.cause ?? error)));\n const files = (output.serverModules ?? []).map((module): BundleFile => ({\n path: module.name,\n content: module.content,\n hash: module.hash,\n }));\n const [bundleHash, assets, input] = yield* Effect.all(\n [\n sha256Object(files.map((file) => ({ path: file.path, hash: file.hash }))),\n output.clientDirectory\n ? readClientAssets(\n path.resolve(rootDir, output.clientDirectory),\n assetConfigFromProps(ctx.assets),\n )\n : Effect.undefined,\n hashAstroInput(\n rootDir,\n options.memo,\n options.main,\n Effect.succeed(output.externalWorkspaces ?? []),\n ),\n ],\n { concurrency: \"unbounded\" },\n );\n const bundle: BundleOutput | undefined =\n files.length > 0\n ? {\n files: files as [BundleFile, ...Array\u003cBundleFile>],\n hash: bundleHash,\n }\n : undefined;\n if (bundle === undefined && assets === undefined) {\n return yield* new SourceProviderError({\n provider: PROVIDER,\n message: \"Astro build produced neither assets nor server output\",\n });\n }\n return {\n bundle,\n assets,\n hash: {\n bundle: bundle?.hash,\n assets: assets?.hash,\n input: input.hash,\n additionalWorkspaces: input.workspaces,\n },\n } satisfies SourceBuildOutput;\n }),\n\n hash: (_ctx, previous) =>\n Effect.gen(function* () {\n const rootDir = yield* resolveRoot;\n const { hash, workspaces } = yield* hashAstroInput(\n rootDir,\n options.memo,\n options.main,\n Effect.succeed(previous?.additionalWorkspaces ?? []),\n );\n return { input: hash, additionalWorkspaces: workspaces };\n }),\n\n dev: (ctx) =>\n Effect.gen(function* () {\n const rootDir = yield* resolveRoot;\n yield* applyWorkerEnvToProcess(ctx.env);\n const queueConsumers = yield* ctx.worker.queueConsumers;\n const astro = yield* framework(rootDir, {\n compatibilityDate: ctx.compatibility.date,\n compatibilityFlags: ctx.compatibility.flags,\n worker: {\n ...ctx.worker.options,\n name: ctx.workerName,\n bindings: ctx.worker.bindings,\n durableObjectNamespaces: ctx.worker.durableObjectNamespaces,\n hyperdrives: ctx.worker.hyperdrives,\n queueConsumers,\n assets: ctx.worker.assets,\n },\n context: ctx.runtimeContext,\n });\n const server = yield* astro\n .dev({ root: rootDir, port: 0 })\n .pipe(Effect.mapError(failWith(\"Astro dev server failed to start\")));\n return {\n mode: \"server\",\n url: new URL(server.url),\n } satisfies SourceDevHandle;\n }),\n };\n};\n\n/**\n * Mirror of alchemy's `WorkerSourceModule`: the default export's\n * `make(options)` builds a {@link SourceProvider} from the descriptor's\n * JSON-serializable options.\n */\nconst make = (\n options: unknown,\n): Effect.Effect\u003cSourceProvider, SourceProviderError, SourceServices> =>\n Effect.sync(() => makeAstroSourceProvider((options ?? {}) as AstroSourceOptions));\n\nexport default { make };\n","preview":null,"history":null,"version":"6af5c75f3a549b87d4005b0d1aba24963b932dc2"},"/_session?shell=1":null}