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

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

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

Synced Nov 21, 06:00 UTC

main
ts 506 lines 19.4 KB
Raw
1 import { stripVTControlCharacters } from "node:util";
2 import * as Artifacts from "alchemy/Artifacts";
3 import * as Bundle from "alchemy/Bundle/Bundle";
4 import { findCwdForBundle, resolveMainPath } from "alchemy/Bundle/TempRoot";
5 import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import { flow } from "effect/Function";
8 import type * as Path from "effect/Path";
9 import * as Stream from "effect/Stream";
10 import path from "pathe";
11 import picomatch from "picomatch";
12 import type * as rolldown from "rolldown";
13 import { isWorkflowExport } from "../../Workflows/Workflow.ts";
14 import { isDurableObjectExport } from "../DurableObject.ts";
15 import type { SourceContext, SourceProvider } from "../Source.ts";
16 import type { SqlMigrationSnapshot } from "../SqlMigrationsRuntime.ts";
17 import type { WorkerProps } from "../Worker.ts";
18 import type { WorkerExport } from "../WorkerRuntimeContext.ts";
19 import type { ModuleRule } from "./Prebuilt.ts";
20 import { bundleSource } from "./shared.ts";
21 import { workerModulePlugin } from "./WorkerModulePlugin.ts";
22
23 /**
24 * Bundler options for a Worker: Rolldown input/output overrides and
25 * {@link Bundle.BundleExtraOptions}, merged over Alchemy's defaults.
26 */
27 export interface WorkerBuildOptions extends Bundle.BundleConfig {
28 /**
29 * Rolldown output options merged over Alchemy's defaults. Use this to
30 * control chunking (`codeSplitting`), minification, etc.
31 */
32 output?: rolldown.OutputOptions;
33 /**
34 * Forwarded to rolldown's `preserveEntrySignatures` input option. Some
35 * `output.codeSplitting` configurations require relaxing it (e.g.
36 * `includeDependenciesRecursively: false` needs `"allow-extension"`).
37 * Workers must keep their entry exports, so never pass `false`.
38 */
39 preserveEntrySignatures?: rolldown.InputOptions["preserveEntrySignatures"];
40 }
41
42 export interface WorkerBundleOptions {
43 id: string;
44 main: string;
45 compatibility: {
46 date: string;
47 flags: string[];
48 };
49 entry:
50 | {
51 kind: "external";
52 }
53 | {
54 kind: "effect";
55 exports: Record<string, WorkerExport>;
56 };
57 stack: { name: string; stage: string };
58 extraOptions: WorkerBuildOptions | undefined;
59 rules?: readonly ModuleRule[] | undefined;
60 }
61
62 /**
63 * The bundler options for a Worker's props, as local dev records them in its
64 * hashed config (a change restarts the instance). Builds themselves go
65 * through the Worker's source provider (`resolveSource`), which derives the
66 * same options from its {@link SourceContext} in {@link makeRolldownSource}.
67 */
68 export const workerBundleOptions = (options: {
69 id: string;
70 props: Pick<WorkerProps, "main" | "isExternal" | "exports" | "build" | "rules">;
71 compatibility: WorkerBundleOptions["compatibility"];
72 stack: WorkerBundleOptions["stack"];
73 }): WorkerBundleOptions => ({
74 id: options.id,
75 main: options.props.main!,
76 compatibility: options.compatibility,
77 entry: options.props.isExternal
78 ? { kind: "external" }
79 : { kind: "effect", exports: options.props.exports ?? {} },
80 stack: options.stack,
81 extraOptions: options.props.build,
82 rules: options.props.rules,
83 });
84
85 /**
86 * Lower the Worker's `rules` to bundler module rules: an import whose
87 * resolved file matches a rule's glob is emitted as its own module, of the
88 * rule's type, instead of being loaded by rolldown. Globs match the path
89 * relative to the entry's directory (`../` included), or an absolute path;
90 * `!` globs exclude across all user rules, as in the prebuilt
91 * (`bundle: false`) walk. JavaScript, TypeScript and JSON are always bundled.
92 */
93 const toModuleRules = (main: string, rules: readonly ModuleRule[] | undefined) => {
94 const globs = rules?.flatMap((rule) => rule.globs) ?? [];
95 const exclude = globs.filter((glob) => glob.startsWith("!")).map((glob) => glob.slice(1));
96 const root = path.dirname(main);
97 const isExcluded = exclude.length ? picomatch(exclude, { dot: true }) : () => false;
98 // Absolute globs match any file; relative ones match from the entry's
99 // directory (`../shared/*.png` too), and `**` globs from the filesystem
100 // root for files outside it.
101 const targetsOf = (id: string) => {
102 const file = path.normalize(id);
103 const relative = path.relative(root, file);
104 const outside = relative.startsWith("../") || path.isAbsolute(relative);
105 return outside ? [file, relative, file.replace(/^\//, "")] : [file, relative];
106 };
107 const moduleRules = (rules ?? []).flatMap((rule) => {
108 const include = rule.globs.filter((glob) => !glob.startsWith("!"));
109 if (include.length === 0) return [];
110 const isIncluded = picomatch(include, { dot: true });
111 return [
112 {
113 type: rule.type,
114 test: (id: string) => {
115 if (/\.([cm]?[jt]sx?|json)$/.test(id)) return false;
116 const targets = targetsOf(id);
117 return (
118 targets.some((target) => isIncluded(target)) &&
119 !targets.some((target) => isExcluded(target))
120 );
121 },
122 },
123 ];
124 });
125 return moduleRules.length ? moduleRules : undefined;
126 };
127
128 /**
129 * Rolldown resolves imports to real paths, so an absolute glob under a
130 * symlink (macOS `/var` → `/private/var`) is matched by its real base.
131 */
132 const realGlobs = (rules: readonly ModuleRule[] | undefined) =>
133 Effect.gen(function* () {
134 if (rules === undefined) return rules;
135 const fs = yield* FileSystem.FileSystem;
136 const real = (glob: string) => {
137 const negated = glob.startsWith("!");
138 const body = negated ? glob.slice(1) : glob;
139 if (!path.isAbsolute(body)) return Effect.succeed(glob);
140 const { base } = picomatch.scan(body);
141 return fs.realPath(base).pipe(
142 Effect.map((resolved) => `${negated ? "!" : ""}${resolved}${body.slice(base.length)}`),
143 Effect.orElseSucceed(() => glob),
144 );
145 };
146 return yield* Effect.forEach(rules, (rule) =>
147 Effect.forEach(rule.globs, real).pipe(Effect.map((globs) => ({ ...rule, globs }))),
148 );
149 });
150
151 /**
152 * Point rolldown's failure to load an imported non-JavaScript file (a font,
153 * markdown, an image) at the `rules` that upload it as a module. A text file
154 * that happens to parse as JavaScript fails later, without a default export.
155 */
156 const withRulesHint = (error: Bundle.BundleError): Bundle.BundleError => {
157 const errors = (
158 error.cause as { errors?: Array<{ code?: string; id?: string; message?: string }> }
159 )?.errors;
160 const exts = (errors ?? []).flatMap((e) => {
161 const message = e.message ? stripVTControlCharacters(e.message) : "";
162 const file =
163 e.code === "UNLOADABLE_DEPENDENCY"
164 ? message.match(/Could not load (\S+)/)?.[1]
165 : e.code === "PARSE_ERROR"
166 ? e.id
167 : e.code === "MISSING_EXPORT"
168 ? message.match(/"default" is not exported by "([^"]+)"/)?.[1]
169 : undefined;
170 const ext = file && path.extname(file);
171 return ext && !/^\.([cm]?[jt]sx?|json|css)$/.test(ext) ? [ext] : [];
172 });
173 if (exts.length === 0) return error;
174 const unique = [...new Set(exts)];
175 const isText = (ext: string) => TEXT_EXTENSIONS.test(ext);
176 const rules = [
177 { type: "Text", exts: unique.filter(isText) },
178 { type: "Data", exts: unique.filter((ext) => !isText(ext)) },
179 ]
180 .filter((rule) => rule.exts.length)
181 .map(
182 (rule) =>
183 `{ type: "${rule.type}", globs: [${rule.exts.map((ext) => `"**/*${ext}"`).join(", ")}] }`,
184 );
185 return Bundle.BundleError.make({
186 message: `${error.message}\nTo import these files as Worker modules, add rules: [${rules.join(", ")}] to the Worker.`,
187 cause: error.cause,
188 });
189 };
190
191 /** Extensions the hint suggests importing as `Text` rather than `Data`. */
192 const TEXT_EXTENSIONS =
193 /^\.(md|markdown|mdx|csv|tsv|ya?ml|toml|ini|xml|svg|graphql|gql|glsl|wgsl|vert|frag|prompt)$/;
194
195 /**
196 * Rebuild any `builtin:esm-external-require` plugin instance with OUR copy of
197 * rolldown.
198 *
199 * A separately installed or bundled `@alchemy.run/cloudflare-runtime/rolldown`
200 * can resolve a physically different copy of rolldown than the one Alchemy
201 * invokes. Builtin plugins — here `builtin:esm-external-require`, which
202 * rewrites CJS `require`s of Node builtins into ESM imports under
203 * `nodejs_compat` — are `BuiltinPlugin` class instances that rolldown
204 * recognizes with an `instanceof` check. An instance constructed by the
205 * foreign rolldown copy fails that check and is silently treated as a
206 * hookless JS plugin, so CJS requires keep rolldown's throwing `require`
207 * shim and the Worker fails Cloudflare startup validation (#880).
208 * Reconstructing the builtin from its `_options` with the rolldown copy we
209 * actually invoke makes the identity check pass regardless of which copy the
210 * plugin package resolved.
211 */
212 const configureCloudflarePlugins = (
213 plugins: Array<rolldown.Plugin | null>,
214 esmExternalRequirePlugin: (typeof import("rolldown/plugins"))["esmExternalRequirePlugin"],
215 aliases: NonNullable<rolldown.InputOptions["resolve"]>["alias"],
216 ): Array<rolldown.Plugin | null> => {
217 const aliasKeys = Object.entries(aliases ?? {})
218 .filter(([, value]) => !Array.isArray(value) || value.length > 0)
219 .map(([key]) => key);
220 const isAliased = (id: string) =>
221 aliasKeys.some((key) => {
222 if (key.endsWith("$")) return id === key.slice(0, -1);
223 const wildcard = key.indexOf("*");
224 if (wildcard !== -1) {
225 const prefix = key.slice(0, wildcard);
226 const suffix = key.slice(wildcard + 1);
227 return (
228 id.length >= prefix.length + suffix.length && id.startsWith(prefix) && id.endsWith(suffix)
229 );
230 }
231 return id === key || id.startsWith(`${key}/`) || id.startsWith(`${key}\\`);
232 });
233 return plugins.map((plugin) => {
234 if (
235 typeof plugin === "object" &&
236 plugin !== null &&
237 "name" in plugin &&
238 plugin.name === "builtin:esm-external-require" &&
239 "_options" in plugin
240 ) {
241 const options = plugin._options as Parameters<typeof esmExternalRequirePlugin>[0];
242 return esmExternalRequirePlugin(
243 options && {
244 ...options,
245 external: options.external.filter((id) => typeof id !== "string" || !isAliased(id)),
246 },
247 ) as rolldown.Plugin;
248 }
249 // Let Rolldown resolve user aliases before compatibility polyfills.
250 if (plugin?.resolveId && aliasKeys.length > 0) {
251 const hook = plugin.resolveId;
252 const handler = typeof hook === "function" ? hook : hook.handler;
253 return {
254 ...plugin,
255 resolveId: {
256 ...(typeof hook === "function" ? {} : hook),
257 handler(source, importer, options) {
258 if (isAliased(source)) return null;
259 return handler.call(this, source, importer, options);
260 },
261 },
262 };
263 }
264 return plugin;
265 });
266 };
267
268 export const WorkerBundle = Effect.gen(function* () {
269 const context = yield* Effect.context<FileSystem.FileSystem | Path.Path>();
270 const virtualEntryPlugin = yield* Bundle.virtualEntryPlugin;
271
272 const makeOptions = Effect.fn(function* (options: WorkerBundleOptions) {
273 // Loaded lazily so importing the Cloudflare provider (or the CLI, whose
274 // command tree reaches this module) never loads rolldown's native
275 // binding — only actually bundling a Worker does (#562).
276 const [{ default: cloudflareRolldown }, { esmExternalRequirePlugin }] = yield* Effect.promise(
277 () =>
278 Promise.all([
279 import("@alchemy.run/cloudflare-runtime/rolldown"),
280 import("rolldown/plugins"),
281 ]),
282 );
283 const realMain = yield* sanitizeMain(options.main);
284 const rules = yield* realGlobs(options.rules);
285 const cwd = yield* findCwdForBundle(realMain).pipe(
286 Effect.mapError((cause) =>
287 Bundle.BundleError.make({
288 message: `Failed to find cwd for bundle: ${realMain}`,
289 cause,
290 }),
291 ),
292 Effect.provide(context),
293 );
294 // The Cloudflare-flavored plugin set, shared by the parent build and
295 // by every nested `?worker` module build (see WorkerModulePlugin.ts).
296 const overrides = options.extraOptions?.input;
297 // Nested `?worker` builds keep only their entry chunk (see
298 // WorkerModulePlugin.ts), so `rules` apply to the parent build alone: a
299 // matching import inside a nested worker fails to load rather than
300 // pointing at a module that is never uploaded.
301 const cloudflarePlugins = (moduleRules?: ReturnType<typeof toModuleRules>) =>
302 configureCloudflarePlugins(
303 cloudflareRolldown({
304 compatibilityDate: options.compatibility.date,
305 compatibilityFlags: options.compatibility.flags,
306 moduleRules,
307 }),
308 esmExternalRequirePlugin,
309 overrides?.resolve?.alias,
310 );
311 const workerModules: rolldown.Plugin = workerModulePlugin({
312 loadRolldown: () => import("rolldown"),
313 nested: async () => ({
314 external: ["lightningcss", "fsevents"],
315 cwd,
316 ...overrides,
317 plugins: [overrides?.plugins, cloudflarePlugins(), workerModules],
318 checks: {
319 unresolvedImport: false,
320 ineffectiveDynamicImport: false,
321 ...overrides?.checks,
322 },
323 }),
324 });
325 const inputOptions: rolldown.InputOptions = {
326 preserveEntrySignatures: options.extraOptions?.preserveEntrySignatures,
327 // Forever-devtool native modules that vite/chokidar reference behind
328 // runtime guards. Rolldown resolves before tree-shaking, so the dead
329 // `require('../pkg')` (lightningcss < 1.32) and `require('fsevents')`
330 // (darwin-only) trip [UNRESOLVED_IMPORT] before DCE can prune them.
331 // See rolldown/tsdown#212.
332 external: ["lightningcss", "fsevents"],
333 cwd,
334 ...overrides,
335 input: realMain,
336 plugins: [
337 overrides?.plugins,
338 cloudflarePlugins(toModuleRules(realMain, rules)),
339 workerModules,
340 options.entry.kind === "effect"
341 ? [virtualEntryPlugin(makeEffectVirtualEntry(options.entry.exports, options.stack))]
342 : undefined,
343 ],
344 checks: {
345 // Suppress unresolved import warnings for unrelated AWS packages
346 unresolvedImport: false,
347 // The shared platform boundary keeps guarded Node/Bun adapter imports.
348 ineffectiveDynamicImport: false,
349 ...overrides?.checks,
350 },
351 };
352 const outputOptions: rolldown.OutputOptions = {
353 format: "esm",
354 sourcemap: "hidden",
355 minify: true,
356 keepNames: true,
357 // Rolldown's default chunking can split top-level initializer modules
358 // (e.g. Drizzle `pgTable` schemas) away from the classes they read,
359 // and workerd then evaluates a reader before its imported binding is
360 // initialized — the script fails Cloudflare startup validation with
361 // `ScriptStartupError: Cannot access '<minified>' before
362 // initialization` (#749). `strictExecutionOrder` wraps cross-chunk
363 // modules so evaluation follows ESM semantics regardless of how the
364 // graph was chunked. See DrizzleSchemaChunks.test.ts.
365 strictExecutionOrder: true,
366 ...options.extraOptions?.output,
367 };
368 return { inputOptions, outputOptions, extraOptions: options.extraOptions };
369 });
370
371 const sanitizeMain = (main: string) =>
372 resolveMainPath(main).pipe(
373 Effect.mapError((cause) =>
374 Bundle.BundleError.make({
375 message: `Failed to find real path for bundle: ${main}`,
376 cause,
377 }),
378 ),
379 Effect.provide(context),
380 );
381
382 return {
383 build: flow(
384 makeOptions,
385 Effect.flatMap((resolved) =>
386 Bundle.build(resolved.inputOptions, resolved.outputOptions, resolved.extraOptions),
387 ),
388 Effect.mapError(withRulesHint),
389 ),
390 watch: flow(
391 makeOptions,
392 Stream.fromEffect,
393 Stream.flatMap((resolved) =>
394 Bundle.watch(resolved.inputOptions, resolved.outputOptions, resolved.extraOptions),
395 ),
396 Stream.map((event) =>
397 event._tag === "Error" ? { ...event, error: withRulesHint(event.error) } : event,
398 ),
399 ),
400 };
401 });
402
403 /**
404 * Export a bridge class under `name`. A name that isn't an identifier, like a
405 * Domain model's `Billing.AccountAggregate`, is exported by alias.
406 */
407 const exportClass = (name: string, base: string, local: string) =>
408 /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name)
409 ? `export class ${name} extends ${base} {}`
410 : `const ${local} = class extends ${base} {};\nexport { ${local} as ${JSON.stringify(name)} };`;
411
412 export const makeEffectVirtualEntry = (
413 exports: Record<string, WorkerExport>,
414 stack: { name: string; stage: string },
415 ) => {
416 const doClasses: string[] = [];
417 const wfClasses: string[] = [];
418 const migrations: Record<string, SqlMigrationSnapshot> = {};
419 for (const [className, entry] of Object.entries(exports)) {
420 if (isDurableObjectExport(entry)) {
421 doClasses.push(className);
422 } else if (isWorkflowExport(entry)) {
423 wfClasses.push(className);
424 } else if (entry.kind === "sqlMigrations") {
425 migrations[className] = entry.snapshot;
426 }
427 }
428 const hasMigrations = Object.keys(migrations).length > 0;
429 const hasDoClasses = doClasses.length > 0;
430 const hasWfClasses = wfClasses.length > 0;
431 return (importPath: string) => `
432 import * as Effect from "effect/Effect";
433
434 import { env, DurableObject, WorkerEntrypoint${hasWfClasses ? ", WorkflowEntrypoint" : ""} } from "cloudflare:workers";
435 import { makeDurableObjectBridge, makeWorkerBridge${hasWfClasses ? ", makeWorkflowBridge" : ""}${hasMigrations ? ", withSqlMigrations" : ""} } from "@alchemy.run/cloudflare/Bridge";
436 import { makeEntrypointLayer } from "alchemy/Runtime";
437
438 import entrypoint from ${JSON.stringify(importPath)};
439
440 const meta = {
441 entrypoint: ${hasMigrations ? `withSqlMigrations(entrypoint, ${JSON.stringify(migrations)})` : "entrypoint"},
442 stack: {
443 name: ${JSON.stringify(stack.name)},
444 stage: ${JSON.stringify(stack.stage)},
445 },
446 };
447
448 export default makeWorkerBridge(WorkerEntrypoint, meta);
449
450 // export class proxy stubs for Durable Objects and Workflows
451 ${[
452 ...(hasDoClasses
453 ? [
454 "const DurableObjectBridge = makeDurableObjectBridge(DurableObject, meta);",
455 ...doClasses.map((id, i) =>
456 exportClass(id, `DurableObjectBridge(${JSON.stringify(id)})`, `DurableObjectClass${i}`),
457 ),
458 ]
459 : []),
460 ...(hasWfClasses
461 ? [
462 "const WorkflowBridgeFn = makeWorkflowBridge(WorkflowEntrypoint, meta);",
463 ...wfClasses.map((id, i) =>
464 exportClass(id, `WorkflowBridgeFn(${JSON.stringify(id)})`, `WorkflowClass${i}`),
465 ),
466 ]
467 : []),
468 ].join("\n")}
469 `;
470 };
471
472 /**
473 * The default source provider: bundle `props.main` with rolldown.
474 *
475 * `hash()` deliberately builds — recomputing "without building" is
476 * impossible for rolldown without a source-tree memo hash, and today's
477 * semantics accept that. The build routes through `Artifacts.cached`
478 * under the same key as `build()`, so a diff that had to build shares
479 * its output with the reconcile in the same run.
480 */
481 export const makeRolldownSource = (options: {
482 main: string;
483 rules: readonly ModuleRule[] | undefined;
484 }): SourceProvider => {
485 const bundleOptions = (ctx: SourceContext): WorkerBundleOptions => ({
486 id: ctx.id,
487 main: options.main,
488 compatibility: ctx.compatibility,
489 entry: ctx.entry,
490 stack: ctx.stack,
491 extraOptions: ctx.extraOptions,
492 rules: options.rules,
493 });
494 return bundleSource({
495 build: (ctx) =>
496 Effect.gen(function* () {
497 const bundler = yield* WorkerBundle;
498 return yield* bundler.build(bundleOptions(ctx));
499 }).pipe(Artifacts.cached("build")),
500 watch: (ctx) =>
501 Effect.gen(function* () {
502 const bundler = yield* WorkerBundle;
503 return bundler.watch(bundleOptions(ctx));
504 }),
505 });
506 };