@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 494 lines 20.8 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import {
3 WorkerBundle,
4 type WorkerBundleOptions,
5 } from "@alchemy.run/cloudflare/Workers/Sources/Rolldown";
6 import { describe, expect, layer } from "@alchemy.run/test";
7 import * as Test from "@alchemy.run/test/Alchemy";
8 import * as NodeServices from "@effect/platform-node/NodeServices";
9 import * as Alchemy from "alchemy";
10 import * as Effect from "effect/Effect";
11 import * as FileSystem from "effect/FileSystem";
12 import * as HttpClient from "effect/http/HttpClient";
13 import * as Path from "effect/Path";
14 import * as Schedule from "effect/Schedule";
15 import * as Stream from "effect/Stream";
16 import RequireNodeBuiltinsWorker from "./fixtures/require-node-builtins/worker.ts";
17
18 const decode = (content: string | Uint8Array<ArrayBufferLike>) =>
19 typeof content === "string" ? content : new TextDecoder().decode(content as Uint8Array);
20
21 /**
22 * Write `files` (paths relative to a fresh temp directory) and return
23 * the temp directory's absolute path.
24 */
25 const writeFixture = Effect.fn(function* (files: Record<string, string>) {
26 const fs = yield* FileSystem.FileSystem;
27 const path = yield* Path.Path;
28 const root = yield* fs.makeTempDirectory({ prefix: "alchemy-worker-bundle-" });
29 for (const [name, content] of Object.entries(files)) {
30 const file = path.join(root, name);
31 yield* fs.makeDirectory(path.dirname(file), { recursive: true });
32 yield* fs.writeFileString(file, content);
33 }
34 return root;
35 });
36
37 layer(NodeServices.layer)("WorkerBundle", (it) => {
38 // Regression test for #880: CJS dependencies (like `pg`) that
39 // `require("events")` must have those requires converted into ESM imports
40 // of the workerd-provided Node builtins. Left unconverted, rolldown emits
41 // a throwing `require` fallback and the Worker fails Cloudflare startup
42 // validation with "Calling `require` for \"events\" in an environment
43 // that doesn't expose the `require` function".
44 it.effect(
45 "converts CJS requires of Node builtins into ESM imports under nodejs_compat",
46 () =>
47 Effect.gen(function* () {
48 const path = yield* Path.Path;
49 const root = yield* writeFixture({
50 // Anchors findCwdForBundle so bundle output stays in the temp dir.
51 "package.json": "{}",
52 "dep.cjs": [
53 `const { EventEmitter } = require("events");`,
54 `const util = require("node:util");`,
55 `module.exports = {`,
56 ` Thing: class extends EventEmitter {`,
57 ` describe() { return util.format("thing %s", "one"); }`,
58 ` },`,
59 `};`,
60 ].join("\n"),
61 "worker.mjs": [
62 `import { Thing } from "./dep.cjs";`,
63 `export default {`,
64 ` fetch: () => new Response(new Thing().describe()),`,
65 `};`,
66 ].join("\n"),
67 });
68
69 const bundler = yield* WorkerBundle;
70 const output = yield* bundler.build({
71 id: "worker-bundle-require-events",
72 main: path.join(root, "worker.mjs"),
73 compatibility: { date: "2026-03-17", flags: ["nodejs_compat"] },
74 entry: { kind: "external" },
75 stack: { name: "worker-bundle-test", stage: "test" },
76 extraOptions: undefined,
77 });
78
79 const entry = decode(output.files[0].content);
80 // The CJS requires were rewritten to imports of the builtins.
81 expect(entry).toMatch(/from\s*["'](?:node:)?events["']/);
82 expect(entry).toMatch(/from\s*["']node:util["']/);
83 // No chunk retains rolldown's throwing `require` fallback.
84 for (const file of output.files) {
85 if (!file.path.endsWith(".js")) continue;
86 expect(decode(file.content)).not.toContain("Calling `require` for");
87 }
88 }),
89 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] },
90 );
91 // #2729: an import with an extension rolldown can't load is emitted as its
92 // own module when it matches the Worker's `rules`, instead of failing.
93 it.effect(
94 "emits imports matching rules as separate modules",
95 () =>
96 Effect.gen(function* () {
97 const path = yield* Path.Path;
98 const root = yield* writeFixture({
99 "package.json": "{}",
100 "src/font.ttf": "FONT_BYTES",
101 "src/skip.ttf": "SKIP_BYTES",
102 "shared/logo.png": "LOGO_BYTES",
103 "src/notes.md": "# NOTES",
104 "src/worker.mjs": `import font from "./font.ttf"; import logo from "../shared/logo.png"; import notes from "./notes.md"; export default { fetch: () => new Response(font + logo + notes) };`,
105 });
106 const bundler = yield* WorkerBundle;
107 const build = (rules: WorkerBundleOptions["rules"]) =>
108 bundler.build({
109 id: "worker-bundle-rules",
110 main: path.join(root, "src/worker.mjs"),
111 compatibility: { date: "2026-03-17", flags: [] },
112 entry: { kind: "external" },
113 stack: { name: "worker-bundle-test", stage: "test" },
114 extraOptions: undefined,
115 rules,
116 });
117
118 const output = yield* build([
119 { globs: ["**/*.ttf", `${root}/shared/*.png`] },
120 { type: "Text", globs: ["**/*.md"] },
121 ]);
122 const entry = decode(output.files[0].content);
123 // `font.ttf` is in the entry's directory; `logo.png` is outside it
124 // and matches an absolute glob.
125 for (const [ext, bytes] of [
126 [".ttf", "FONT_BYTES"],
127 [".png", "LOGO_BYTES"],
128 ]) {
129 const file = output.files.find((file) => file.path.endsWith(ext));
130 expect(file && decode(file.content)).toBe(bytes);
131 expect(entry).toContain(path.basename(file!.path));
132 }
133 // A `Text` rule over an extension uploads would read as Data gets
134 // the `.txt` extension the upload types it by.
135 const notes = output.files.find((file) => /notes.*\.txt$/.test(file.path));
136 expect(notes && decode(notes.content)).toBe("# NOTES");
137 expect(entry).toContain(path.basename(notes!.path));
138
139 // `!` globs exclude, as in the prebuilt walk: the excluded import is
140 // left to rolldown. Its text parses as JavaScript, so the build fails
141 // on the missing default export, and the hint still names the rule.
142 const excluded = yield* Effect.flip(
143 build([{ globs: ["**/*.ttf", "**/*.png", "**/*.md", `!${root}/src/font.ttf`] }]),
144 );
145 expect(excluded.message).toContain('add rules: [{ type: "Data", globs: ["**/*.ttf"] }]');
146
147 // Without rules, the error names the rule that would import them.
148 const error = yield* Effect.flip(build(undefined));
149 expect(error.message).toContain('{ type: "Text", globs: ["**/*.md"] }');
150
151 // `../` globs match files outside the entry's directory.
152 const relative = yield* build([
153 { globs: ["**/*.ttf", "../shared/*.png"] },
154 { type: "Text", globs: ["**/*.md"] },
155 ]);
156 expect(relative.files.some((file) => file.path.endsWith(".png"))).toBe(true);
157 }),
158 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] },
159 );
160 it.effect(
161 "applies input aliases before Node compatibility for imports and requires",
162 () =>
163 Effect.gen(function* () {
164 const path = yield* Path.Path;
165 const root = yield* writeFixture({
166 "package.json": "{}",
167 "stub.mjs": `export const createRequire = () => "ALIASED_MODULE"; export default "ALIASED_PACKAGE";`,
168 "worker.mjs": `
169 import { createRequire } from "module";
170 import { createRequire as nodeCreateRequire } from "node:module";
171 import ts from "typescript";
172 import execa from "execa";
173 import readable from "readable-stream";
174 const bare = require("module");
175 const prefixed = require("node:module");
176 export default { fetch: () => Response.json([
177 createRequire(undefined), nodeCreateRequire(undefined),
178 bare.createRequire(undefined), prefixed.createRequire(undefined),
179 ts, execa, readable, BUILD_MARKER,
180 ]) };
181 `,
182 });
183 const bundler = yield* WorkerBundle;
184 const output = yield* bundler.build({
185 id: "worker-bundle-alias",
186 main: path.join(root, "worker.mjs"),
187 compatibility: { date: "2025-04-01", flags: ["nodejs_compat"] },
188 entry: { kind: "external" },
189 stack: { name: "worker-bundle-test", stage: "test" },
190 extraOptions: {
191 input: {
192 resolve: {
193 alias: Object.fromEntries(
194 ["module", "node:module", "typescript", "execa", "readable-stream"].map(
195 (name) => [name, path.join(root, "stub.mjs")],
196 ),
197 ),
198 },
199 transform: { define: { BUILD_MARKER: JSON.stringify("INPUT_OPTIONS_MARKER") } },
200 },
201 },
202 });
203 const entry = decode(output.files[0].content);
204 expect(entry).toContain("ALIASED_MODULE");
205 expect(entry).toContain("ALIASED_PACKAGE");
206 expect(entry).toContain("INPUT_OPTIONS_MARKER");
207 expect(entry).not.toMatch(/from\s*["'](?:node:)?module["']/);
208 expect(entry).not.toContain("Calling `require` for");
209 }),
210 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] },
211 );
212 it.effect(
213 "preserves native alias matching, fallback arrays, and disabled modules",
214 () =>
215 Effect.gen(function* () {
216 const path = yield* Path.Path;
217 const root = yield* writeFixture({
218 "package.json": "{}",
219 "stub.mjs": `export default "NATIVE_ALIAS_MARKER";`,
220 "stubs/promises.mjs": `export default "SUBPATH_ALIAS_MARKER";`,
221 "worker.mjs": `
222 import module from "module";
223 import promises from "node:fs/promises";
224 import ignored from "node:inspector";
225 import { createRequire } from "node:module";
226 export default { fetch: () => Response.json([module, promises, ignored, typeof createRequire]) };
227 `,
228 });
229 const bundler = yield* WorkerBundle;
230 for (const fsAlias of ["node:fs", "node:fs/*"]) {
231 const output = yield* bundler.build({
232 id: "worker-bundle-native-alias",
233 main: path.join(root, "worker.mjs"),
234 compatibility: { date: "2025-04-01", flags: ["nodejs_compat"] },
235 entry: { kind: "external" },
236 stack: { name: "worker-bundle-test", stage: "test" },
237 extraOptions: {
238 input: {
239 resolve: {
240 alias: {
241 module$: [path.join(root, "missing.mjs"), path.join(root, "stub.mjs")],
242 [fsAlias]: path.join(root, fsAlias.includes("*") ? "stubs/*" : "stubs"),
243 "node:inspector": false,
244 "node:module": [],
245 },
246 },
247 },
248 },
249 });
250 const entry = decode(output.files[0].content);
251 expect(entry).toContain("NATIVE_ALIAS_MARKER");
252 expect(entry).toContain("SUBPATH_ALIAS_MARKER");
253 expect(entry).toMatch(/from\s*["']node:module["']/);
254 expect(entry).not.toMatch(/from\s*["'](?:module|node:fs\/promises|node:inspector)["']/);
255 }
256 }),
257 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] },
258 );
259
260 it.effect(
261 "uses input plugins and aliases when watching",
262 () =>
263 Effect.gen(function* () {
264 const path = yield* Path.Path;
265 const root = yield* writeFixture({
266 "package.json": "{}",
267 "stub.mjs": `export const createRequire = () => "WATCH_ALIAS_MARKER";`,
268 "worker.mjs": `import { createRequire } from "node:module"; import value from "virtual:custom"; export default { fetch: () => new Response(createRequire(undefined) + value) };`,
269 });
270 const bundler = yield* WorkerBundle;
271 const events = yield* bundler
272 .watch({
273 id: "worker-bundle-watch-alias",
274 main: path.join(root, "worker.mjs"),
275 compatibility: { date: "2025-04-01", flags: ["nodejs_compat"] },
276 entry: { kind: "external" },
277 stack: { name: "worker-bundle-test", stage: "test" },
278 extraOptions: {
279 input: {
280 resolve: { alias: { "node:module": path.join(root, "stub.mjs") } },
281 plugins: [
282 {
283 name: "custom-input-plugin",
284 resolveId(id) {
285 if (id === "virtual:custom") return "\u0000custom";
286 },
287 load(id) {
288 if (id === "\u0000custom") return `export default "INPUT_PLUGIN_MARKER";`;
289 },
290 },
291 ],
292 },
293 },
294 })
295 .pipe(
296 Stream.filter((event) => event._tag !== "Start"),
297 Stream.take(1),
298 Stream.runCollect,
299 );
300 const event = events[0]!;
301 expect(event._tag).toBe("Success");
302 if (event._tag === "Success") {
303 const entry = decode(event.output.files[0].content);
304 expect(entry).toContain("WATCH_ALIAS_MARKER");
305 expect(entry).toContain("INPUT_PLUGIN_MARKER");
306 expect(entry).not.toMatch(/from\s*["']node:module["']/);
307 }
308 }),
309 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] },
310 );
311 // `?worker` imports (WorkerModulePlugin.ts): the target is bundled into
312 // one self-contained module and imported as a STRING, ready for a
313 // `WorkerLoader`. Nested `?worker` imports inside the target work too.
314 it.effect(
315 "bundles a ?worker import into a string, nested imports included",
316 () =>
317 Effect.gen(function* () {
318 const path = yield* Path.Path;
319 const root = yield* writeFixture({
320 "package.json": "{}",
321 "stub.mjs": `export const createRequire = () => "NESTED_ALIAS_MARKER";`,
322 "grandchild.mjs": [
323 `import { createRequire } from "node:module";`,
324 `export default { fetch: () => new Response("GRANDCHILD_MARKER" + createRequire(undefined)) };`,
325 ].join("\n"),
326 "child.mjs": [
327 `import grandchild from "./grandchild.mjs?worker";`,
328 `export const MARKER = "CHILD_MARKER_";`,
329 `export default { fetch: () => new Response(MARKER + grandchild) };`,
330 ].join("\n"),
331 "worker.mjs": [
332 `import child from "./child.mjs?worker";`,
333 `export default {`,
334 ` fetch: () => new Response(typeof child + ":" + child.length),`,
335 `};`,
336 ].join("\n"),
337 });
338 const bundler = yield* WorkerBundle;
339 const output = yield* bundler.build({
340 id: "worker-bundle-worker-module",
341 main: path.join(root, "worker.mjs"),
342 compatibility: { date: "2026-03-17", flags: ["nodejs_compat"] },
343 entry: { kind: "external" },
344 stack: { name: "worker-bundle-test", stage: "test" },
345 extraOptions: {
346 input: { resolve: { alias: { "node:module": path.join(root, "stub.mjs") } } },
347 },
348 });
349 const entry = decode(output.files[0].content);
350 // The child's code is embedded as a JSON string literal …
351 expect(entry).toContain("CHILD_MARKER_");
352 // … which itself embeds the grandchild's code (one more level).
353 expect(entry).toContain("GRANDCHILD_MARKER");
354 expect(entry).toContain("NESTED_ALIAS_MARKER");
355 // `child` is a string in the parent, not a module namespace.
356 expect(entry).not.toContain("import child from");
357 }),
358 { tags: ["unit", "provider:cloudflare", "provider:cloudflare:worker", "local"] },
359 );
360 });
361
362 describe(
363 "integration",
364 { tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"] },
365 () => {
366 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
367 providers: Cloudflare.providers(),
368 });
369
370 const Stack = Alchemy.Stack(
371 "NodeBuiltinRequireTestStack",
372 { providers: Cloudflare.providers(), state: Cloudflare.state() },
373 Effect.gen(function* () {
374 const worker = yield* RequireNodeBuiltinsWorker;
375 const path = yield* Path.Path;
376 const stub = yield* path
377 .fromFileUrl(new URL("./fixtures/module-alias/stub.mjs", import.meta.url))
378 .pipe(Effect.orDie);
379 const aliased = yield* Cloudflare.Worker("AliasedWorker", {
380 main: new URL("./fixtures/module-alias/worker.mjs", import.meta.url).href,
381 compatibility: {
382 date: "2025-04-01",
383 flags: ["nodejs_compat", "nodejs_compat_populate_process_env"],
384 },
385 build: {
386 input: {
387 resolve: {
388 alias: Object.fromEntries(
389 ["module", "node:module", "typescript", "execa", "readable-stream"].map(
390 (name) => [name, stub],
391 ),
392 ),
393 },
394 },
395 },
396 });
397 const moduleRules = yield* Cloudflare.Worker("ModuleRulesWorker", {
398 main: new URL("./fixtures/module-rules/worker.mjs", import.meta.url).href,
399 compatibility: { date: "2025-04-01" },
400 rules: [{ globs: ["**/*.ttf"] }, { type: "Text", globs: ["**/*.md"] }],
401 });
402 return {
403 url: worker.url.as<string>(),
404 aliasUrl: aliased.url.as<string>(),
405 moduleRulesUrl: moduleRules.url.as<string>(),
406 };
407 }),
408 );
409
410 const stack = beforeAll(destroy(Stack).pipe(Effect.andThen(deploy(Stack))));
411 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
412
413 test(
414 "worker aliases override Node builtins without breaking other requires",
415 Effect.gen(function* () {
416 const { aliasUrl } = yield* stack;
417 const client = yield* HttpClient.HttpClient;
418 const body = yield* client.get(aliasUrl).pipe(
419 Effect.flatMap((response) => response.json),
420 Effect.retry({ schedule: Schedule.spaced("1 second"), times: 8 }),
421 );
422 expect(body).toEqual({
423 bare: "aliased:module",
424 prefixed: "aliased:module",
425 cjsBare: "aliased:module",
426 cjsPrefixed: "aliased:module",
427 typescript: "aliased:package",
428 execa: "aliased:package",
429 readable: "aliased:package",
430 event: "builtin:ok",
431 });
432 }),
433 );
434
435 // #2729: a bundled Worker imports a file rolldown can't load (`.ttf`)
436 // through `rules`, next to the built-in `.bin` and `.txt` rules. The
437 // font's bytes cover every value, so any text decoding along the way
438 // (bundle, upload, runtime) would change them.
439 test(
440 "bundled worker serves imports matched by rules as modules of their type",
441 Effect.gen(function* () {
442 const { moduleRulesUrl } = yield* stack;
443 const client = yield* HttpClient.HttpClient;
444 const body = yield* client.get(moduleRulesUrl).pipe(
445 Effect.flatMap((response) => response.json),
446 Effect.retry({ schedule: Schedule.spaced("1 second"), times: 8 }),
447 );
448 const hex = (bytes: Uint8Array) => Buffer.from(bytes).toString("hex");
449 const fs = yield* FileSystem.FileSystem;
450 const fixture = (name: string) =>
451 fs.readFile(new URL(`./fixtures/module-rules/${name}`, import.meta.url).pathname);
452 expect(body).toEqual({
453 font: { type: "[object ArrayBuffer]", hex: hex(yield* fixture("font.ttf")) },
454 data: hex(yield* fixture("data.bin")),
455 notice: "module-rules-notice\n",
456 readme: "module-rules-readme\n",
457 });
458 }),
459 );
460
461 // Regression test for #880: the deploy itself is the primary assertion —
462 // a bundle that keeps rolldown's throwing `require` fallback for
463 // `require("events")` fails Cloudflare startup validation at upload time
464 // and `beforeAll(deploy(Stack))` fails the suite.
465 test(
466 "worker with a CJS dep requiring node builtins deploys and serves",
467 Effect.gen(function* () {
468 const { url } = yield* stack;
469 const client = yield* HttpClient.HttpClient;
470
471 const get = client.get(`${url}/?value=pong`).pipe(
472 Effect.flatMap((res) => res.text),
473 Effect.retry({ schedule: Schedule.exponential("500 millis"), times: 5 }),
474 Effect.orDie,
475 );
476
477 // Fresh workers.dev URLs can serve placeholder 200s while propagating,
478 // so anchor the readiness poll on the fixture's marker, not the status.
479 const body = yield* get.pipe(
480 Effect.repeat({
481 schedule: Schedule.exponential("500 millis"),
482 until: (b) => b.includes("require-node-builtins:"),
483 times: 10,
484 }),
485 );
486
487 // The converted builtins really work at runtime: the EventEmitter
488 // round-tripped the request's value and util.format made the marker.
489 expect(body).toBe("require-node-builtins:pong");
490 }),
491 { timeout: 180_000 },
492 );
493 },
494 );