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

import * as NodeCrypto from "node:crypto";
import { createRequire } from "node:module";
import type {
  BindingHook,
  BindingServices,
  HyperdriveOrigin,
  Assets as RuntimeAssets,
  DurableObjectNamespace as RuntimeDurableObject,
  QueueConsumer as RuntimeQueueConsumer,
  RuntimeServices,
  RuntimeWorker,
} from "@alchemy.run/cloudflare-runtime/core";
import type * as Context from "effect/Context";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import type { PlatformError } from "effect/PlatformError";
import type * as Scope from "effect/Scope";
import * as Semaphore from "effect/Semaphore";
import { resolveBuildEnv, runBuildChild } from "../core/BuildChild.ts";
import * as FrameworkCore from "../core/index.ts";
import { makeWakuCloudflareTarget } from "./cloudflare.ts";
import { layer as wakuFrameworkLayer } from "./Waku.ts";

// ─────────────────────────────────────────────────────────────────────────────
// Structural mirror of alchemy's Worker source-provider contract.
//
// This module is loaded dynamically by alchemy's `loadSource` (see
// `packages/cloudflare/src/Workers/Source.ts` — `WorkerSourceModule`,
// `SourceProvider`, `SourceContext`, `DevContext`, `SourceBuildOutput`). The
// contract is deliberately implemented STRUCTURALLY — no alchemy import — so
// version skew degrades to a load-time `SourceProviderError` instead of a
// crash. Keep these shapes in sync with the alchemy interfaces.
// ─────────────────────────────────────────────────────────────────────────────

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

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

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

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

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

/** Mirror of alchemy `Cloudflare/Workers/Source.SourceContext`. */
export interface SourceContext {
  readonly id: string;
  readonly workerName: string;
  readonly compatibility: {
    readonly date: string;
    readonly flags: Array<string>;
  };
  readonly entry:
    | { readonly kind: "external" }
    | { readonly kind: "effect"; readonly exports: Record<string, unknown> };
  readonly stack: { readonly name: string; readonly stage: string };
  readonly env: Record<string, unknown> | undefined;
  readonly extraOptions: unknown;
  /** Raw `props.assets` (routing config; sources that own assets merge it in). */
  readonly assets: unknown;
}

/** Mirror of alchemy `Cloudflare/Workers/Source.DevContext`. */
export interface DevContext extends SourceContext {
  readonly worker: {
    readonly bindings: Array<BindingHook<BindingServices>>;
    readonly durableObjectNamespaces: Array<RuntimeDurableObject & { uniqueKey: string }>;
    readonly hyperdrives: Record<string, Required<HyperdriveOrigin>>;
    readonly queueConsumers: Effect.Effect<Array<RuntimeQueueConsumer>>;
    readonly assets: RuntimeAssets | 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<
      RuntimeWorker,
      "workflows" | "crons" | "tails" | "streamingTails" | "cache" | "cf" | "zone" | "access"
    >;
  };
  readonly runtimeContext: Context.Context<RuntimeServices>;
}

/** The `server`-mode arm of alchemy's `SourceDevHandle` (waku dev is a vite dev server). */
export interface ServerDevHandle {
  readonly mode: "server";
  readonly url: URL;
}

/**
 * Tag-compatible mirror of alchemy's
 * `Cloudflare.Workers.SourceProviderError` — same `_tag`, same 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 WakuSourceError = SourceProviderError | PlatformError;

export type SourceRequirements = FileSystem.FileSystem | Path.Path;

/** Mirror of alchemy `Cloudflare/Workers/Source.SourceProvider` (narrowed E/R). */
export interface SourceProvider {
  readonly ownsAssets: boolean;
  readonly devAssetsBinding?: boolean;
  readonly build: (
    ctx: SourceContext,
  ) => Effect.Effect<SourceBuildOutput, WakuSourceError, SourceRequirements>;
  readonly hash: (
    ctx: SourceContext,
    previous: SourceHash | undefined,
  ) => Effect.Effect<Partial<SourceHash>, WakuSourceError, SourceRequirements>;
  readonly dev: (
    ctx: DevContext,
  ) => Effect.Effect<ServerDevHandle, WakuSourceError, SourceRequirements | Scope.Scope>;
}

/**
 * Serializable options of the waku source descriptor
 * (`{ provider: "@alchemy.run/frontend-frameworks/waku/source", options }` on alchemy's
 * `WorkerProps.source`). MUST stay plain JSON data — the descriptor persists
 * in alchemy state and participates in the Worker metadata hash.
 */
export interface WakuSourceOptions {
  /** Project root. Defaults to the process working directory. */
  readonly rootDir?: string | undefined;
  /**
   * The user's own worker entry (the user-entry seam,
   * `DeployTarget.entry`): a module that wraps waku's handler — imported
   * from `virtual:waku/server-entry` — and re-exports extras (Durable
   * Object classes, additional handlers). Relative paths resolve against
   * the root. When unset, waku's own rsc server entry is deployed.
   */
  readonly main?: string | undefined;
  /** Waku `srcDir` (relative to the root). @default "src" */
  readonly srcDir?: string | undefined;
  /**
   * Waku `distDir` (relative to the root). If `waku.config.ts` customizes
   * `distDir`, mirror it here (or add it to `memo.exclude`) so the build
   * output stays excluded from the rebuild-deciding input hash.
   * @default "dist"
   */
  readonly distDir?: string | undefined;
  /** Waku `basePath`. @default "/" */
  readonly basePath?: string | undefined;
  /** Controls which files feed the rebuild-deciding `input` hash. */
  readonly memo?: WakuMemoOptions | undefined;
}

const wakuConfigFor = (options: WakuSourceOptions) => ({
  ...(options.srcDir !== undefined ? { srcDir: options.srcDir } : undefined),
  ...(options.distDir !== undefined ? { distDir: options.distDir } : undefined),
  ...(options.basePath !== undefined ? { basePath: options.basePath } : undefined),
});

/**
 * Input-hash scoping, mirroring the semantics of alchemy's `MemoOptions`
 * (`Command/Memo.ts`): by default every non-gitignored file under the root is
 * hashed plus the nearest package-manager lockfile; explicit `include` /
 * `exclude` globs narrow the scope.
 *
 * The glob support is a self-contained subset (`**`, `*`, `?`; gitignore-style
 * anchoring). Negated (`!`) gitignore rules are ignored (files they would
 * re-include stay excluded from the hash).
 */
export interface WakuMemoOptions {
  /** Glob patterns of files to hash, relative to the root. @default all files */
  readonly include?: Array<string> | undefined;
  /** Glob patterns to exclude. @default gitignore rules from the root up to the repo root */
  readonly exclude?: Array<string> | undefined;
  /**
   * Whether to include the nearest package-manager lockfile in the hash.
   * @default true when both `include` and `exclude` are unset; false otherwise
   */
  readonly lockfile?: boolean | undefined;
}

// ─────────────────────────────────────────────────────────────────────────────
// Hashing primitives
// ─────────────────────────────────────────────────────────────────────────────

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

const stableValue = (value: unknown): unknown => {
  if (Array.isArray(value)) {
    return value.map(stableValue);
  }
  if (value !== null && typeof value === "object" && value.constructor === Object) {
    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 = (value: unknown): string => sha256(JSON.stringify(stableValue(value)));

/** The integration's own version — included in the input hash so upgrading the
 * adapter busts the build memo even when project sources are unchanged. */
const PACKAGE_VERSION: string = (
  createRequire(import.meta.url)("../../package.json") as { version: string }
).version;

// ─────────────────────────────────────────────────────────────────────────────
// Minimal gitignore-style matching (documented subset, zero dependencies)
// ─────────────────────────────────────────────────────────────────────────────

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

const globBodyToRegExpSource = (glob: string): string => {
  let source = "";
  let i = 0;
  while (i < glob.length) {
    const char = glob[i]!;
    if (char === "*") {
      if (glob[i + 1] === "*") {
        if (glob[i + 2] === "/") {
          source += "(?:[^/]+/)*";
          i += 3;
        } else {
          source += ".*";
          i += 2;
        }
      } else {
        source += "[^/]*";
        i += 1;
      }
    } else if (char === "?") {
      source += "[^/]";
      i += 1;
    } else {
      source += escapeRegExpChar(char);
      i += 1;
    }
  }
  return source;
};

/**
 * Compile one gitignore-style rule into a RegExp over root-relative posix
 * paths. The RegExp matches the path itself and everything under it (so a
 * rule matching a directory also matches its files). Returns `undefined` for
 * blank lines, comments, and negated rules (unsupported).
 */
const compileIgnoreRule = (raw: string): RegExp | undefined => {
  let rule = raw.trim();
  if (rule.length === 0 || rule.startsWith("#") || rule.startsWith("!")) {
    return undefined;
  }
  if (rule.endsWith("/")) {
    rule = rule.slice(0, -1);
  }
  if (rule.length === 0) {
    return undefined;
  }
  // Anchored when the rule contains a `/` (the trailing directory `/` was
  // already stripped above); otherwise it matches a basename at any depth.
  const anchored = rule.includes("/");
  if (rule.startsWith("/")) {
    rule = rule.slice(1);
  }
  const body = globBodyToRegExpSource(rule);
  const prefix = anchored ? "^" : "^(?:.*/)?";
  return new RegExp(`${prefix}${body}(?:/.*)?

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	.repos/alchemy-effect/packages/frontend-frameworks/src/waku/source.ts at main · sailfork/t3code-github · Sailfork
	
		
); }; const compileIgnoreRules = (rules: ReadonlyArray<string>): Array<RegExp> => rules.map(compileIgnoreRule).filter((regex): regex is RegExp => regex !== undefined); const compileIncludeGlobs = (globs: ReadonlyArray<string>): Array<RegExp> => globs.map((glob) => new RegExp(`^${globBodyToRegExpSource(glob)} .repos/alchemy-effect/packages/frontend-frameworks/src/waku/source.ts at main · sailfork/t3code-github · Sailfork
)); const matchesAny = (regexes: ReadonlyArray<RegExp>, path: string): boolean => regexes.some((regex) => regex.test(path)); // ───────────────────────────────────────────────────────────────────────────── // Input-tree hashing (the rebuild-free change signal, mirroring the semantics // of alchemy's `hashViteInput` / `hashDirectory`) // ───────────────────────────────────────────────────────────────────────────── const LOCKFILE_NAMES = [ "bun.lock", "bun.lockb", "package-lock.json", "pnpm-lock.yaml", "yarn.lock", ]; const findUp = Effect.fn(function* (startDir: string, filenames: ReadonlyArray<string>) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; let dir = startDir; for (;;) { for (const filename of filenames) { const candidate = path.join(dir, filename); if (yield* fs.exists(candidate)) { return candidate; } } const parent = path.dirname(dir); if (parent === dir) { return undefined; } dir = parent; } }); /** Collect `.gitignore` rules from `rootDir` up to the repo root (the first * directory containing `.git`), outermost first — same walk as alchemy's * `Command/Memo.ts`. Rules apply relative to `rootDir` (simplification). */ const readGitIgnoreRules = Effect.fn(function* (rootDir: string) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const chain: Array<Array<string>> = []; let dir = rootDir; for (;;) { const gitignore = path.join(dir, ".gitignore"); if (yield* fs.exists(gitignore)) { const content = yield* fs.readFileString(gitignore); chain.unshift(content.split("\n")); } if (yield* fs.exists(path.join(dir, ".git"))) { break; } const parent = path.dirname(dir); if (parent === dir) { break; } dir = parent; } return chain.flat(); }); interface ResolvedMemo { readonly include: Array<RegExp> | undefined; readonly exclude: Array<RegExp>; readonly lockfile: boolean; } const resolveMemo = Effect.fn(function* ( rootDir: string, distDir: string, memo: WakuMemoOptions | undefined, ) { const exclude = memo?.exclude !== undefined ? compileIgnoreRules(memo.exclude) : compileIgnoreRules(yield* readGitIgnoreRules(rootDir)); return { include: memo?.include !== undefined ? compileIncludeGlobs(memo.include) : undefined, exclude: [ // Always excluded regardless of gitignore: dependency trees, VCS // metadata, and the build output waku itself writes (hashing `distDir` // would make every build bust its own memo). ...compileIgnoreRules(["node_modules", ".git", `/${distDir}`]), ...exclude, ], lockfile: memo?.lockfile ?? !(memo?.include || memo?.exclude), } satisfies ResolvedMemo; }); /** Deterministic, machine-independent content hash of one directory tree: * sorted `[relativePosixPath, sha256(content)]` pairs (never absolute paths). */ const hashDirectory = Effect.fn(function* (rootDir: string, memo: ResolvedMemo) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const files: Array<string> = []; const walk: (dir: string, rel: string) => Effect.Effect<void, PlatformError> = Effect.fn( function* (dir: string, rel: string) { const entries = (yield* fs.readDirectory(dir)).sort(); for (const entry of entries) { const relPath = rel === "" ? entry : `${rel}/${entry}`; if (matchesAny(memo.exclude, relPath)) { continue; } const absPath = path.join(dir, entry); const stat = yield* fs.stat(absPath); if (stat.type === "Directory") { yield* walk(absPath, relPath); } else if (stat.type === "File") { if (memo.include !== undefined && !matchesAny(memo.include, relPath)) { continue; } files.push(relPath); } } }, ); yield* walk(rootDir, ""); if (memo.lockfile) { const lockfile = yield* findUp(rootDir, LOCKFILE_NAMES); if (lockfile !== undefined) { const relative = path.relative(rootDir, lockfile).replaceAll("\\", "/"); if (!files.includes(relative)) { files.push(relative); } } } const hashes = yield* Effect.forEach( files.sort(), Effect.fn(function* (file) { const content = yield* fs.readFile(path.join(rootDir, file)); return `${file}:${sha256(content)}`; }), { concurrency: 16 }, ); return sha256Object(hashes); }); /** * The `input` hash slot: the project tree (+ workspaces + lockfile), the * integration's own package version, and the build-affecting waku options. * Deterministic for an unchanged tree and stable across `rootDir` moves. */ const hashWakuInput = Effect.fn(function* (params: { rootDir: string; distDir: string; memo: WakuMemoOptions | undefined; options: WakuSourceOptions; /** Workspace directories, absolute. */ workspaces: Iterable<string>; }) { const path = yield* Path.Path; const memo = yield* resolveMemo(params.rootDir, params.distDir, params.memo); const workspaceMemo = yield* resolveMemo(params.rootDir, params.distDir, undefined); const [root, ...workspaceHashes] = yield* Effect.all( [ hashDirectory(params.rootDir, memo), ...Array.from(params.workspaces).map((workspace) => Effect.gen(function* () { const resolved = path.resolve(params.rootDir, workspace); const rules = yield* readGitIgnoreRules(resolved); const hash = yield* hashDirectory(resolved, { ...workspaceMemo, exclude: [...workspaceMemo.exclude, ...compileIgnoreRules(rules)], }); return `${path.relative(params.rootDir, resolved).replaceAll("\\", "/")}:${hash}`; }), ), ], { concurrency: 4 }, ); const hash = sha256Object({ version: PACKAGE_VERSION, root, workspaces: workspaceHashes.sort(), options: { // Build-affecting: a changed user entry must bust the memo. main: params.options.main, srcDir: params.options.srcDir, distDir: params.options.distDir, basePath: params.options.basePath, }, }); const workspaces = Array.from(params.workspaces).map((workspace) => path.relative(params.rootDir, path.resolve(params.rootDir, workspace)).replaceAll("\\", "/"), ); return { hash, workspaces }; }); // ───────────────────────────────────────────────────────────────────────────── // Client-asset reading (mirror of alchemy `Cloudflare/Workers/Assets.readAssets`) // ───────────────────────────────────────────────────────────────────────────── const SPECIAL_ASSET_FILES = [".assetsignore", "_headers", "_redirects"]; const maybeReadString = Effect.fn(function* (file: string) { const fs = yield* FileSystem.FileSystem; return yield* fs.readFileString(file).pipe( Effect.catchIf( (error) => error._tag === "PlatformError" && error.reason._tag === "NotFound", () => Effect.undefined, ), ); }); /** * Read the client output directory into the manifest shape alchemy's * `putWorker`/`uploadAssets` consume: per-file `sha256(content).slice(0, 32)` * + byte size, keyed by `/`-prefixed posix path; the aggregate hash covers * only upload-affecting inputs (manifest, config, `_headers`, `_redirects` — * never the absolute directory path). */ const readClientAssets = Effect.fn(function* ( directory: string, config: Record<string, unknown> | undefined, ) { const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const [entries, ignoreFile, _headers, _redirects] = yield* Effect.all([ fs.readDirectory(directory, { recursive: true }), maybeReadString(path.join(directory, ".assetsignore")), maybeReadString(path.join(directory, "_headers")), maybeReadString(path.join(directory, "_redirects")), ]); const ignores = compileIgnoreRules([...SPECIAL_ASSET_FILES, ...(ignoreFile?.split("\n") ?? [])]); const manifest = new Map<string, { hash: string; size: number }>(); yield* Effect.forEach( entries, Effect.fn(function* (name) { const relPath = name.replaceAll("\\", "/"); if (matchesAny(ignores, relPath)) { return; } const file = path.join(directory, name); const stat = yield* fs.stat(file); if (stat.type !== "File") { return; } const content = yield* fs.readFile(file); manifest.set(relPath.startsWith("/") ? relPath : `/${relPath}`, { hash: sha256(content).slice(0, 32), size: Number(stat.size), }); }), { concurrency: 16 }, ); const sortedManifest = Object.fromEntries( Array.from(manifest.entries()).sort((a, b) => a[0].localeCompare(b[0])), ); return { directory, config, manifest: sortedManifest, _headers, _redirects, hash: sha256Object({ config, manifest: sortedManifest, _headers, _redirects, }), } satisfies AssetReadResult; }); // ───────────────────────────────────────────────────────────────────────────── // The provider // ───────────────────────────────────────────────────────────────────────────── const PROVIDER = "@alchemy.run/frontend-frameworks/waku/source"; export interface WakuLikeSourceIntegration { readonly provider: string; readonly framework: string; readonly displayName: string; readonly buildModule: string; readonly layer: (options: { root: string; target: ReturnType<typeof makeWakuCloudflareTarget>; }) => Layer.Layer<FrameworkCore.Framework, never, SourceRequirements>; } /** * Waku assumes `process.cwd()` is the project root in several places (the * html-shell plugin's `index.html` input resolves against the cwd, and * rolldown resolves relative inputs from the cwd), exactly like its CLI, * which always runs from the project. * * The production **build** therefore runs in a disposable child process * whose working directory IS the project root (see `core/BuildChild.ts`) — * no in-process `chdir`, no lock, unbounded build concurrency. The shared `core/BuildChildRunner` * entry imports this module in the child and calls the exported * {@link buildInChild}. * * The **dev server** cannot cross a process boundary (its inputs — * `ctx.worker.bindings` hooks and `ctx.runtimeContext` — are live objects), * so only its startup window still chdirs in-process, serialized through * {@link cwdLock}; the cwd is restored as soon as the server is listening. */ const cwdLock = Semaphore.makeUnsafe(1); const withRootCwd = <A, E, R>( rootDir: string, effect: Effect.Effect<A, E, R>, ): Effect.Effect<A, E, R> => Semaphore.withPermits( cwdLock, 1, )( Effect.acquireUseRelease( Effect.sync(() => { const previous = process.cwd(); process.chdir(rootDir); return previous; }), () => effect, (previous) => Effect.sync(() => process.chdir(previous)), ), ); /** JSON config the build child reconstructs the waku framework from. */ export interface WakuBuildChildConfig { readonly rootDir: string; readonly compatibilityDate: string; readonly compatibilityFlags: Array<string>; readonly main: string | undefined; readonly waku: { readonly srcDir?: string; readonly distDir?: string; readonly basePath?: string; }; } export const buildInChild = (config: WakuBuildChildConfig) => Effect.gen(function* () { const waku = yield* FrameworkCore.Framework; return yield* waku.build({ root: config.rootDir }); }).pipe( Effect.provide( wakuFrameworkLayer({ root: config.rootDir, waku: config.waku, target: makeWakuCloudflareTarget({ compatibilityDate: config.compatibilityDate, compatibilityFlags: config.compatibilityFlags, ...(config.main !== undefined ? { main: config.main } : undefined), }), }), ), ); /** Routing config from the raw `props.assets` (directory/hash keys are * path-level concerns owned by alchemy; only the routing config passes through). */ const assetsConfigOf = (assets: unknown): Record<string, unknown> | undefined => { if (assets === undefined || assets === null || typeof assets !== "object") { return undefined; } const { directory: _directory, hash: _hash, ...config } = assets as Record<string, unknown>; return Object.keys(config).length > 0 ? config : undefined; }; export const makeWakuSourceProvider = ( options: WakuSourceOptions, integration: WakuLikeSourceIntegration = { provider: PROVIDER, framework: "waku", displayName: "Waku", buildModule: import.meta.url, layer: ({ root, target }) => wakuFrameworkLayer({ root, waku: wakuConfigFor(options), target }), }, ): SourceProvider => { const rootDirOf = (path: Path.Path): string => options.rootDir !== undefined ? path.resolve(options.rootDir) : process.cwd(); const distDir = options.distDir ?? "dist"; const wakuConfig = wakuConfigFor(options); return { // Waku's assets are a build product (`dist/public`), not a props-level // directory — the provider reads and diffs them itself. ownsAssets: true, // Dev runs the Worker in workerd with the Vite plugin's assets service. devAssetsBinding: true, build: Effect.fn(function* (ctx) { const path = yield* Path.Path; const rootDir = rootDirOf(path); // The build runs in a child process with cwd = rootDir (waku resolves // inputs relative to the cwd); `buildInChild` reconstructs the // framework + cloudflare target from this JSON config on the far side. const output = yield* runBuildChild({ module: integration.buildModule, rootDir, framework: integration.framework, env: resolveBuildEnv(ctx.env), config: { rootDir, compatibilityDate: ctx.compatibility.date, compatibilityFlags: ctx.compatibility.flags, // The user-entry seam: surfaced as `DeployTarget.entry` and made // the vite plugin's `main` (resolved against the root by // `makeWakuPluginOptions`). main: options.main, waku: wakuConfig, } satisfies WakuBuildChildConfig, }).pipe( Effect.mapError( (error) => new SourceProviderError({ provider: integration.provider, message: `${integration.displayName} build failed`, cause: error.cause ?? error, }), ), ); if (output.serverModules === undefined || output.serverModules.length === 0) { return yield* new SourceProviderError({ provider: integration.provider, message: `${integration.displayName} build produced no server modules`, }); } const [entry, ...rest] = output.serverModules; const files: [BundleFile, ...Array<BundleFile>] = [entry!, ...rest].map((file) => ({ path: file.name.replaceAll("\\", "/"), content: file.content, hash: file.hash, })) as [BundleFile, ...Array<BundleFile>]; const bundle: BundleOutput = { files, hash: sha256Object(files.map((file) => [file.path, file.hash])), }; const [assets, input] = yield* Effect.all( [ output.clientDirectory !== undefined ? readClientAssets(output.clientDirectory, assetsConfigOf(ctx.assets)) : Effect.undefined, hashWakuInput({ rootDir, distDir, memo: options.memo, options, workspaces: output.externalWorkspaces, }), ], { concurrency: "unbounded" }, ); return { bundle, assets, hash: { bundle: bundle.hash, assets: assets?.hash, input: input.hash, additionalWorkspaces: input.workspaces, }, } satisfies SourceBuildOutput; }), // Rebuild-free: re-hash the project tree (+ previously-discovered // workspaces) — the same recipe as alchemy's vite source. `previous` is // only used for the auxiliary workspace list, never to skip recomputation. hash: Effect.fn(function* (_ctx, previous) { const path = yield* Path.Path; const rootDir = rootDirOf(path); const input = yield* hashWakuInput({ rootDir, distDir, memo: options.memo, options, workspaces: (previous?.additionalWorkspaces ?? []).map((workspace) => path.resolve(rootDir, workspace), ), }); return { input: input.hash, additionalWorkspaces: input.workspaces }; }), // Waku dev is a vite dev server with the cloudflare plugin embedded in // waku's own plugin stack (the rsc environment runs in workerd against // the host-provided worker wiring) — a `server`-mode dev handle. dev: Effect.fn(function* (ctx) { const path = yield* Path.Path; const rootDir = rootDirOf(path); const queueConsumers = yield* ctx.worker.queueConsumers; const framework = integration.layer({ root: rootDir, target: makeWakuCloudflareTarget({ compatibilityDate: ctx.compatibility.date, compatibilityFlags: ctx.compatibility.flags, // Dev serves the wrapped user entry too, so DO classes exported // from it exist in dev. ...(options.main !== undefined ? { main: options.main } : undefined), 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, }), }); // The dev server *starts* under the project cwd (waku resolves its // html shell and relative inputs from the cwd at startup); the cwd is // restored once the server is listening. const server = yield* withRootCwd( rootDir, Effect.gen(function* () { const waku = yield* FrameworkCore.Framework; return yield* waku.dev({ root: rootDir }); }).pipe( Effect.provide(framework), Effect.mapError( (cause) => new SourceProviderError({ provider: integration.provider, message: `Failed to start the ${integration.framework} dev server`, cause, }), ), ), ); return { mode: "server", url: new URL(server.url), } satisfies ServerDevHandle; }), }; }; /** * The dynamic-module contract alchemy's `loadSource` expects: the default * export's `make(options)` builds a `SourceProvider` from the descriptor's * JSON-serializable options. */ const sourceModule = { make: (options: unknown): Effect.Effect<SourceProvider, SourceProviderError> => { if (options !== undefined && (typeof options !== "object" || options === null)) { return Effect.fail( new SourceProviderError({ provider: PROVIDER, message: `Invalid options for ${PROVIDER}: expected an object, got ${typeof options}`, }), ); } return Effect.succeed(makeWakuSourceProvider((options ?? {}) as WakuSourceOptions)); }, }; export default sourceModule;
.repos/alchemy-effect/packages/frontend-frameworks/src/waku/source.ts at main · sailfork/t3code-github · Sailfork
);\n};\n\nconst compileIgnoreRules = (rules: ReadonlyArray\u003cstring>): Array\u003cRegExp> =>\n rules.map(compileIgnoreRule).filter((regex): regex is RegExp => regex !== undefined);\n\nconst compileIncludeGlobs = (globs: ReadonlyArray\u003cstring>): Array\u003cRegExp> =>\n globs.map((glob) => new RegExp(`^${globBodyToRegExpSource(glob)} .repos/alchemy-effect/packages/frontend-frameworks/src/waku/source.ts at main · sailfork/t3code-github · Sailfork
));\n\nconst matchesAny = (regexes: ReadonlyArray\u003cRegExp>, path: string): boolean =>\n regexes.some((regex) => regex.test(path));\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Input-tree hashing (the rebuild-free change signal, mirroring the semantics\n// of alchemy's `hashViteInput` / `hashDirectory`)\n// ─────────────────────────────────────────────────────────────────────────────\n\nconst LOCKFILE_NAMES = [\n \"bun.lock\",\n \"bun.lockb\",\n \"package-lock.json\",\n \"pnpm-lock.yaml\",\n \"yarn.lock\",\n];\n\nconst findUp = Effect.fn(function* (startDir: string, filenames: ReadonlyArray\u003cstring>) {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n let dir = startDir;\n for (;;) {\n for (const filename of filenames) {\n const candidate = path.join(dir, filename);\n if (yield* fs.exists(candidate)) {\n return candidate;\n }\n }\n const parent = path.dirname(dir);\n if (parent === dir) {\n return undefined;\n }\n dir = parent;\n }\n});\n\n/** Collect `.gitignore` rules from `rootDir` up to the repo root (the first\n * directory containing `.git`), outermost first — same walk as alchemy's\n * `Command/Memo.ts`. Rules apply relative to `rootDir` (simplification). */\nconst readGitIgnoreRules = Effect.fn(function* (rootDir: string) {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n const chain: Array\u003cArray\u003cstring>> = [];\n let dir = rootDir;\n for (;;) {\n const gitignore = path.join(dir, \".gitignore\");\n if (yield* fs.exists(gitignore)) {\n const content = yield* fs.readFileString(gitignore);\n chain.unshift(content.split(\"\\n\"));\n }\n if (yield* fs.exists(path.join(dir, \".git\"))) {\n break;\n }\n const parent = path.dirname(dir);\n if (parent === dir) {\n break;\n }\n dir = parent;\n }\n return chain.flat();\n});\n\ninterface ResolvedMemo {\n readonly include: Array\u003cRegExp> | undefined;\n readonly exclude: Array\u003cRegExp>;\n readonly lockfile: boolean;\n}\n\nconst resolveMemo = Effect.fn(function* (\n rootDir: string,\n distDir: string,\n memo: WakuMemoOptions | undefined,\n) {\n const exclude =\n memo?.exclude !== undefined\n ? compileIgnoreRules(memo.exclude)\n : compileIgnoreRules(yield* readGitIgnoreRules(rootDir));\n return {\n include: memo?.include !== undefined ? compileIncludeGlobs(memo.include) : undefined,\n exclude: [\n // Always excluded regardless of gitignore: dependency trees, VCS\n // metadata, and the build output waku itself writes (hashing `distDir`\n // would make every build bust its own memo).\n ...compileIgnoreRules([\"node_modules\", \".git\", `/${distDir}`]),\n ...exclude,\n ],\n lockfile: memo?.lockfile ?? !(memo?.include || memo?.exclude),\n } satisfies ResolvedMemo;\n});\n\n/** Deterministic, machine-independent content hash of one directory tree:\n * sorted `[relativePosixPath, sha256(content)]` pairs (never absolute paths). */\nconst hashDirectory = Effect.fn(function* (rootDir: string, memo: ResolvedMemo) {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n\n const files: Array\u003cstring> = [];\n const walk: (dir: string, rel: string) => Effect.Effect\u003cvoid, PlatformError> = Effect.fn(\n function* (dir: string, rel: string) {\n const entries = (yield* fs.readDirectory(dir)).sort();\n for (const entry of entries) {\n const relPath = rel === \"\" ? entry : `${rel}/${entry}`;\n if (matchesAny(memo.exclude, relPath)) {\n continue;\n }\n const absPath = path.join(dir, entry);\n const stat = yield* fs.stat(absPath);\n if (stat.type === \"Directory\") {\n yield* walk(absPath, relPath);\n } else if (stat.type === \"File\") {\n if (memo.include !== undefined && !matchesAny(memo.include, relPath)) {\n continue;\n }\n files.push(relPath);\n }\n }\n },\n );\n yield* walk(rootDir, \"\");\n\n if (memo.lockfile) {\n const lockfile = yield* findUp(rootDir, LOCKFILE_NAMES);\n if (lockfile !== undefined) {\n const relative = path.relative(rootDir, lockfile).replaceAll(\"\\\\\", \"/\");\n if (!files.includes(relative)) {\n files.push(relative);\n }\n }\n }\n\n const hashes = yield* Effect.forEach(\n files.sort(),\n Effect.fn(function* (file) {\n const content = yield* fs.readFile(path.join(rootDir, file));\n return `${file}:${sha256(content)}`;\n }),\n { concurrency: 16 },\n );\n return sha256Object(hashes);\n});\n\n/**\n * The `input` hash slot: the project tree (+ workspaces + lockfile), the\n * integration's own package version, and the build-affecting waku options.\n * Deterministic for an unchanged tree and stable across `rootDir` moves.\n */\nconst hashWakuInput = Effect.fn(function* (params: {\n rootDir: string;\n distDir: string;\n memo: WakuMemoOptions | undefined;\n options: WakuSourceOptions;\n /** Workspace directories, absolute. */\n workspaces: Iterable\u003cstring>;\n}) {\n const path = yield* Path.Path;\n const memo = yield* resolveMemo(params.rootDir, params.distDir, params.memo);\n const workspaceMemo = yield* resolveMemo(params.rootDir, params.distDir, undefined);\n const [root, ...workspaceHashes] = yield* Effect.all(\n [\n hashDirectory(params.rootDir, memo),\n ...Array.from(params.workspaces).map((workspace) =>\n Effect.gen(function* () {\n const resolved = path.resolve(params.rootDir, workspace);\n const rules = yield* readGitIgnoreRules(resolved);\n const hash = yield* hashDirectory(resolved, {\n ...workspaceMemo,\n exclude: [...workspaceMemo.exclude, ...compileIgnoreRules(rules)],\n });\n return `${path.relative(params.rootDir, resolved).replaceAll(\"\\\\\", \"/\")}:${hash}`;\n }),\n ),\n ],\n { concurrency: 4 },\n );\n const hash = sha256Object({\n version: PACKAGE_VERSION,\n root,\n workspaces: workspaceHashes.sort(),\n options: {\n // Build-affecting: a changed user entry must bust the memo.\n main: params.options.main,\n srcDir: params.options.srcDir,\n distDir: params.options.distDir,\n basePath: params.options.basePath,\n },\n });\n const workspaces = Array.from(params.workspaces).map((workspace) =>\n path.relative(params.rootDir, path.resolve(params.rootDir, workspace)).replaceAll(\"\\\\\", \"/\"),\n );\n return { hash, workspaces };\n});\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Client-asset reading (mirror of alchemy `Cloudflare/Workers/Assets.readAssets`)\n// ─────────────────────────────────────────────────────────────────────────────\n\nconst SPECIAL_ASSET_FILES = [\".assetsignore\", \"_headers\", \"_redirects\"];\n\nconst maybeReadString = Effect.fn(function* (file: string) {\n const fs = yield* FileSystem.FileSystem;\n return yield* fs.readFileString(file).pipe(\n Effect.catchIf(\n (error) => error._tag === \"PlatformError\" && error.reason._tag === \"NotFound\",\n () => Effect.undefined,\n ),\n );\n});\n\n/**\n * Read the client output directory into the manifest shape alchemy's\n * `putWorker`/`uploadAssets` consume: per-file `sha256(content).slice(0, 32)`\n * + byte size, keyed by `/`-prefixed posix path; the aggregate hash covers\n * only upload-affecting inputs (manifest, config, `_headers`, `_redirects` —\n * never the absolute directory path).\n */\nconst readClientAssets = Effect.fn(function* (\n directory: string,\n config: Record\u003cstring, unknown> | undefined,\n) {\n const fs = yield* FileSystem.FileSystem;\n const path = yield* Path.Path;\n const [entries, ignoreFile, _headers, _redirects] = yield* Effect.all([\n fs.readDirectory(directory, { recursive: true }),\n maybeReadString(path.join(directory, \".assetsignore\")),\n maybeReadString(path.join(directory, \"_headers\")),\n maybeReadString(path.join(directory, \"_redirects\")),\n ]);\n const ignores = compileIgnoreRules([...SPECIAL_ASSET_FILES, ...(ignoreFile?.split(\"\\n\") ?? [])]);\n const manifest = new Map\u003cstring, { hash: string; size: number }>();\n yield* Effect.forEach(\n entries,\n Effect.fn(function* (name) {\n const relPath = name.replaceAll(\"\\\\\", \"/\");\n if (matchesAny(ignores, relPath)) {\n return;\n }\n const file = path.join(directory, name);\n const stat = yield* fs.stat(file);\n if (stat.type !== \"File\") {\n return;\n }\n const content = yield* fs.readFile(file);\n manifest.set(relPath.startsWith(\"/\") ? relPath : `/${relPath}`, {\n hash: sha256(content).slice(0, 32),\n size: Number(stat.size),\n });\n }),\n { concurrency: 16 },\n );\n const sortedManifest = Object.fromEntries(\n Array.from(manifest.entries()).sort((a, b) => a[0].localeCompare(b[0])),\n );\n return {\n directory,\n config,\n manifest: sortedManifest,\n _headers,\n _redirects,\n hash: sha256Object({\n config,\n manifest: sortedManifest,\n _headers,\n _redirects,\n }),\n } satisfies AssetReadResult;\n});\n\n// ─────────────────────────────────────────────────────────────────────────────\n// The provider\n// ─────────────────────────────────────────────────────────────────────────────\n\nconst PROVIDER = \"@alchemy.run/frontend-frameworks/waku/source\";\n\nexport interface WakuLikeSourceIntegration {\n readonly provider: string;\n readonly framework: string;\n readonly displayName: string;\n readonly buildModule: string;\n readonly layer: (options: {\n root: string;\n target: ReturnType\u003ctypeof makeWakuCloudflareTarget>;\n }) => Layer.Layer\u003cFrameworkCore.Framework, never, SourceRequirements>;\n}\n\n/**\n * Waku assumes `process.cwd()` is the project root in several places (the\n * html-shell plugin's `index.html` input resolves against the cwd, and\n * rolldown resolves relative inputs from the cwd), exactly like its CLI,\n * which always runs from the project.\n *\n * The production **build** therefore runs in a disposable child process\n * whose working directory IS the project root (see `core/BuildChild.ts`) —\n * no in-process `chdir`, no lock, unbounded build concurrency. The shared `core/BuildChildRunner`\n * entry imports this module in the child and calls the exported\n * {@link buildInChild}.\n *\n * The **dev server** cannot cross a process boundary (its inputs —\n * `ctx.worker.bindings` hooks and `ctx.runtimeContext` — are live objects),\n * so only its startup window still chdirs in-process, serialized through\n * {@link cwdLock}; the cwd is restored as soon as the server is listening.\n */\nconst cwdLock = Semaphore.makeUnsafe(1);\n\nconst withRootCwd = \u003cA, E, R>(\n rootDir: string,\n effect: Effect.Effect\u003cA, E, R>,\n): Effect.Effect\u003cA, E, R> =>\n Semaphore.withPermits(\n cwdLock,\n 1,\n )(\n Effect.acquireUseRelease(\n Effect.sync(() => {\n const previous = process.cwd();\n process.chdir(rootDir);\n return previous;\n }),\n () => effect,\n (previous) => Effect.sync(() => process.chdir(previous)),\n ),\n );\n\n/** JSON config the build child reconstructs the waku framework from. */\nexport interface WakuBuildChildConfig {\n readonly rootDir: string;\n readonly compatibilityDate: string;\n readonly compatibilityFlags: Array\u003cstring>;\n readonly main: string | undefined;\n readonly waku: {\n readonly srcDir?: string;\n readonly distDir?: string;\n readonly basePath?: string;\n };\n}\n\nexport const buildInChild = (config: WakuBuildChildConfig) =>\n Effect.gen(function* () {\n const waku = yield* FrameworkCore.Framework;\n return yield* waku.build({ root: config.rootDir });\n }).pipe(\n Effect.provide(\n wakuFrameworkLayer({\n root: config.rootDir,\n waku: config.waku,\n target: makeWakuCloudflareTarget({\n compatibilityDate: config.compatibilityDate,\n compatibilityFlags: config.compatibilityFlags,\n ...(config.main !== undefined ? { main: config.main } : undefined),\n }),\n }),\n ),\n );\n\n/** Routing config from the raw `props.assets` (directory/hash keys are\n * path-level concerns owned by alchemy; only the routing config passes through). */\nconst assetsConfigOf = (assets: unknown): Record\u003cstring, unknown> | undefined => {\n if (assets === undefined || assets === null || typeof assets !== \"object\") {\n return undefined;\n }\n const { directory: _directory, hash: _hash, ...config } = assets as Record\u003cstring, unknown>;\n return Object.keys(config).length > 0 ? config : undefined;\n};\n\nexport const makeWakuSourceProvider = (\n options: WakuSourceOptions,\n integration: WakuLikeSourceIntegration = {\n provider: PROVIDER,\n framework: \"waku\",\n displayName: \"Waku\",\n buildModule: import.meta.url,\n layer: ({ root, target }) => wakuFrameworkLayer({ root, waku: wakuConfigFor(options), target }),\n },\n): SourceProvider => {\n const rootDirOf = (path: Path.Path): string =>\n options.rootDir !== undefined ? path.resolve(options.rootDir) : process.cwd();\n const distDir = options.distDir ?? \"dist\";\n const wakuConfig = wakuConfigFor(options);\n\n return {\n // Waku's assets are a build product (`dist/public`), not a props-level\n // directory — the provider reads and diffs them itself.\n ownsAssets: true,\n // Dev runs the Worker in workerd with the Vite plugin's assets service.\n devAssetsBinding: true,\n\n build: Effect.fn(function* (ctx) {\n const path = yield* Path.Path;\n const rootDir = rootDirOf(path);\n // The build runs in a child process with cwd = rootDir (waku resolves\n // inputs relative to the cwd); `buildInChild` reconstructs the\n // framework + cloudflare target from this JSON config on the far side.\n const output = yield* runBuildChild({\n module: integration.buildModule,\n rootDir,\n framework: integration.framework,\n env: resolveBuildEnv(ctx.env),\n config: {\n rootDir,\n compatibilityDate: ctx.compatibility.date,\n compatibilityFlags: ctx.compatibility.flags,\n // The user-entry seam: surfaced as `DeployTarget.entry` and made\n // the vite plugin's `main` (resolved against the root by\n // `makeWakuPluginOptions`).\n main: options.main,\n waku: wakuConfig,\n } satisfies WakuBuildChildConfig,\n }).pipe(\n Effect.mapError(\n (error) =>\n new SourceProviderError({\n provider: integration.provider,\n message: `${integration.displayName} build failed`,\n cause: error.cause ?? error,\n }),\n ),\n );\n\n if (output.serverModules === undefined || output.serverModules.length === 0) {\n return yield* new SourceProviderError({\n provider: integration.provider,\n message: `${integration.displayName} build produced no server modules`,\n });\n }\n const [entry, ...rest] = output.serverModules;\n const files: [BundleFile, ...Array\u003cBundleFile>] = [entry!, ...rest].map((file) => ({\n path: file.name.replaceAll(\"\\\\\", \"/\"),\n content: file.content,\n hash: file.hash,\n })) as [BundleFile, ...Array\u003cBundleFile>];\n const bundle: BundleOutput = {\n files,\n hash: sha256Object(files.map((file) => [file.path, file.hash])),\n };\n\n const [assets, input] = yield* Effect.all(\n [\n output.clientDirectory !== undefined\n ? readClientAssets(output.clientDirectory, assetsConfigOf(ctx.assets))\n : Effect.undefined,\n hashWakuInput({\n rootDir,\n distDir,\n memo: options.memo,\n options,\n workspaces: output.externalWorkspaces,\n }),\n ],\n { concurrency: \"unbounded\" },\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 // Rebuild-free: re-hash the project tree (+ previously-discovered\n // workspaces) — the same recipe as alchemy's vite source. `previous` is\n // only used for the auxiliary workspace list, never to skip recomputation.\n hash: Effect.fn(function* (_ctx, previous) {\n const path = yield* Path.Path;\n const rootDir = rootDirOf(path);\n const input = yield* hashWakuInput({\n rootDir,\n distDir,\n memo: options.memo,\n options,\n workspaces: (previous?.additionalWorkspaces ?? []).map((workspace) =>\n path.resolve(rootDir, workspace),\n ),\n });\n return { input: input.hash, additionalWorkspaces: input.workspaces };\n }),\n\n // Waku dev is a vite dev server with the cloudflare plugin embedded in\n // waku's own plugin stack (the rsc environment runs in workerd against\n // the host-provided worker wiring) — a `server`-mode dev handle.\n dev: Effect.fn(function* (ctx) {\n const path = yield* Path.Path;\n const rootDir = rootDirOf(path);\n const queueConsumers = yield* ctx.worker.queueConsumers;\n const framework = integration.layer({\n root: rootDir,\n target: makeWakuCloudflareTarget({\n compatibilityDate: ctx.compatibility.date,\n compatibilityFlags: ctx.compatibility.flags,\n // Dev serves the wrapped user entry too, so DO classes exported\n // from it exist in dev.\n ...(options.main !== undefined ? { main: options.main } : undefined),\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 });\n // The dev server *starts* under the project cwd (waku resolves its\n // html shell and relative inputs from the cwd at startup); the cwd is\n // restored once the server is listening.\n const server = yield* withRootCwd(\n rootDir,\n Effect.gen(function* () {\n const waku = yield* FrameworkCore.Framework;\n return yield* waku.dev({ root: rootDir });\n }).pipe(\n Effect.provide(framework),\n Effect.mapError(\n (cause) =>\n new SourceProviderError({\n provider: integration.provider,\n message: `Failed to start the ${integration.framework} dev server`,\n cause,\n }),\n ),\n ),\n );\n return {\n mode: \"server\",\n url: new URL(server.url),\n } satisfies ServerDevHandle;\n }),\n };\n};\n\n/**\n * The dynamic-module contract alchemy's `loadSource` expects: the default\n * export's `make(options)` builds a `SourceProvider` from the descriptor's\n * JSON-serializable options.\n */\nconst sourceModule = {\n make: (options: unknown): Effect.Effect\u003cSourceProvider, SourceProviderError> => {\n if (options !== undefined && (typeof options !== \"object\" || options === null)) {\n return Effect.fail(\n new SourceProviderError({\n provider: PROVIDER,\n message: `Invalid options for ${PROVIDER}: expected an object, got ${typeof options}`,\n }),\n );\n }\n return Effect.succeed(makeWakuSourceProvider((options ?? {}) as WakuSourceOptions));\n },\n};\n\nexport default sourceModule;\n","preview":null,"history":null,"version":"dafd7a914f79f25c585336bf37a1ae27cdd2cce3"},"/_session?shell=1":null}