env-runner 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -205,7 +205,8 @@ const response = await runner.fetch("/api");
205
205
  // prefer `manager.wsSrvxPlugin()` for cross-runtime proxying)
206
206
  runner.upgrade?.({ node: { req, socket, head } });
207
207
 
208
- // Wait for runner to be ready
208
+ // Wait for runner to be ready (rejects as soon as it closes, with the close
209
+ // reason as `error.cause`)
209
210
  await runner.waitForReady();
210
211
 
211
212
  // Bidirectional messaging
@@ -215,7 +216,12 @@ runner.onMessage((msg) => console.log(msg));
215
216
  // Request-response RPC
216
217
  const result = await runner.rpc<string>("transformHTML", "<html>...</html>");
217
218
 
218
- // Hot-reload entry module without restarting the worker
219
+ // Hot-reload entry module without restarting the worker (the entry is
220
+ // re-read under its own URL; modules it imports stay cached)
221
+ await runner.reloadModule();
222
+
223
+ // Add, replace or remove (`null`) virtual modules in one round trip, then reload
224
+ await runner.updateVirtualModules({ "#routes": `export default []`, "#old": null });
219
225
  await runner.reloadModule();
220
226
 
221
227
  // Invalidate a virtual module (re-runs a factory source), then reload
@@ -231,7 +237,7 @@ await runner.reloadModule();
231
237
  | Runner | Isolation | IPC mechanism |
232
238
  | ---------------------- | ------------------------------- | ---------------------------------- |
233
239
  | `NodeWorkerEnvRunner` | Worker thread | `workerData` / `parentPort` |
234
- | `NodeProcessEnvRunner` | Child process (`fork`) | `ENV_RUNNER_DATA` / `process.send` |
240
+ | `NodeProcessEnvRunner` | Child process (`fork`) | `process.send` IPC channel |
235
241
  | `BunProcessEnvRunner` | Bun or Node.js process | `Bun.spawn` IPC or `fork()` |
236
242
  | `DenoProcessEnvRunner` | Deno process | `deno run` with IPC channel |
237
243
  | `SelfEnvRunner` | In-process | In-memory channel |
@@ -241,7 +247,7 @@ await runner.reloadModule();
241
247
 
242
248
  #### Virtual Modules
243
249
 
244
- The Node.js runners (`NodeWorkerEnvRunner`, `NodeProcessEnvRunner`, and the runners built on top of them), `BunProcessEnvRunner`, `DenoProcessEnvRunner` (Deno >= 2.x), and `MiniflareEnvRunner` can serve **virtual modules** from an in-memory `specifier => source` map passed via `data.virtual`. The entry (and its dependencies) can then `import` them as if they were real files:
250
+ The Node.js runners (`NodeWorkerEnvRunner`, `NodeProcessEnvRunner`, and the runners built on top of them), `BunProcessEnvRunner`, `DenoProcessEnvRunner` (Deno >= 2.8), and `MiniflareEnvRunner` can serve **virtual modules** from an in-memory `specifier => source` map passed via `data.virtual` (`SelfEnvRunner` cannot, and closes with an error when it is set). The entry (and its dependencies) can then `import` them as if they were real files:
245
251
 
246
252
  ```ts
247
253
  import { NodeWorkerEnvRunner } from "env-runner/runners/node-worker";
@@ -268,7 +274,7 @@ export default {
268
274
  };
269
275
  ```
270
276
 
271
- The **entry itself can be virtual** — set `data.entry` to one of the `data.virtual` keys to run an entry whose source lives in memory (it may import other virtual modules too):
277
+ The **entry itself can be virtual** — set `data.entry` to one of the `data.virtual` keys (exactly as written in the map) to run an entry whose source lives in memory (it may import other virtual modules too):
272
278
 
273
279
  ```ts
274
280
  await using runner = new NodeWorkerEnvRunner({
@@ -284,7 +290,45 @@ await using runner = new NodeWorkerEnvRunner({
284
290
  });
285
291
  ```
286
292
 
287
- Each source may also be a **factory** `() => string | Promise<string>` instead of a literal string — useful for lazily computed or asynchronously loaded sources:
293
+ Keys can also be **file paths**: absolute paths (including Windows `C:\...`) or `file://` URLs. Such a module behaves like a file at that path, whether or not its directory exists on disk:
294
+
295
+ - it is matched by resolved URL, so any relative or absolute import that resolves to it is served from the map, whether the importer is a virtual module or a real file. A key equal to a real file's path therefore **overrides that file** for every importer.
296
+ - it runs under its real `file:` URL, so `import.meta.url`, `import.meta.dirname` and `import.meta.filename` point at the key.
297
+ - its own imports (relative files, bare packages) resolve from the key's directory.
298
+ - as `data.entry`, it may be written either way (`/app/x.mjs` for a `file:///app/x.mjs` key, or the reverse). It is still run from the map on load and across `reloadModule()`, even when a real file exists there.
299
+
300
+ ```ts
301
+ import { join, resolve } from "node:path";
302
+
303
+ const dir = resolve("src/generated"); // doesn't need to exist
304
+
305
+ await using runner = new NodeWorkerEnvRunner({
306
+ name: "my-app",
307
+ data: {
308
+ entry: join(dir, "entry.mjs"),
309
+ virtual: {
310
+ [join(dir, "entry.mjs")]: `import { body } from "./body.mjs";
311
+ export default { fetch: () => new Response(body) };`,
312
+ [join(dir, "body.mjs")]: `export const body = "Hello from " + import.meta.filename;`,
313
+ // Overrides the real `src/config.mjs` for all of its importers
314
+ [resolve("src/config.mjs")]: `export default { mode: "virtual" };`,
315
+ },
316
+ },
317
+ });
318
+ ```
319
+
320
+ Other keys (`#name`, bare names) only match an import specifier equal to the key, and relative imports inside them resolve from the working directory. Having no file, they get a runtime-specific id, shown by `import.meta.url` and stack traces: `virtual:#config` on Node.js and Deno, `file:///%23config` on Bun (`file:///env-runner-virtual:%23util.mjs` for keys with an extension), while on miniflare `import.meta.url` is undefined and stack traces show the key itself. A `?query` appended to an import is ignored for matching (`#config.json?raw` matches `#config.json`), but gives a separate module instance. Path keys are fully supported on the Node.js, Bun and Deno runners, with these exceptions:
321
+
322
+ - On Bun (`BunProcessEnvRunner`, and the Node.js runners when the host runtime is Bun), runtime plugins only see a specifier whose last `.` is followed by a letter, typically a file extension. A key without an extension (`#config`, `/app/entry`) therefore only matches an import spelled exactly like the key: no appended `?query`, and no relative or `file://` import of an extensionless path key.
323
+ - `MiniflareEnvRunner` supports relative imports and overrides, and treats `file://` keys and imports like their path, but `import.meta.url` is undefined.
324
+
325
+ On both, a virtual `data.entry` must be written exactly like its key.
326
+
327
+ Two path keys naming the same file (`/app/x.mjs` and `file:///app/x.mjs`) can't both be served, so the runner logs a warning naming both. Keep a single key per file.
328
+
329
+ Keys match **every importer** in the worker, dependencies included, like an import map entry. A bare key such as `react` replaces that package everywhere (useful to alias or stub it), and a `#name` key also replaces a dependency's own `#name` [subpath import](https://nodejs.org/api/packages.html#subpath-imports). There is no warning for this, since overriding a package is often the point, so give your own modules distinctive names (`#app/config` rather than `#utils`).
330
+
331
+ Each source may also be a **factory** returning a source (or a promise of one) — useful for lazily computed or asynchronously loaded sources:
288
332
 
289
333
  ```ts
290
334
  await using runner = new NodeWorkerEnvRunner({
@@ -299,9 +343,9 @@ await using runner = new NodeWorkerEnvRunner({
299
343
  });
300
344
  ```
301
345
 
302
- Factories are invoked once on the host (before the worker is spawned), so the worker always receives plain strings — functions can't cross the `workerData`/`JSON` boundary, and Node's synchronous load hook can't await. For the same reason, **all** factories are resolved eagerly at startup (in parallel), not lazily on first import — so keep them cheap, or use plain strings for sources that don't need computation. Maps containing only strings skip this step entirely.
346
+ Factories are invoked once on the host (before the worker is spawned), so the worker always receives resolved sources — functions can't cross the `workerData`/`JSON` boundary, and Node's synchronous load hook can't await. For the same reason, **all** factories are resolved eagerly at startup (in parallel), not lazily on first import — so keep them cheap, or use plain sources for modules that don't need computation. Maps without factories skip this step entirely.
303
347
 
304
- To refresh a single virtual module without restarting the worker, call `invalidateModule(specifier)`: a factory-valued source is re-run on the host and the module is invalidated in the worker so its **next import evaluates fresh**. Virtual modules that import the invalidated one (directly or transitively) are invalidated along with it, so the fresh module is picked up even through intermediate virtual importers. Already-imported modules keep their instances, so pair it with `reloadModule()` to re-import the entry graph:
348
+ To refresh a single virtual module without restarting the worker, call `invalidateModule(specifier)`: a factory-valued source is re-run on the host and the module is invalidated in the worker so its **next import evaluates fresh**. Virtual modules that import the invalidated one (directly or transitively) are invalidated along with it, so the fresh module is picked up even through intermediate virtual importers. On Node.js, Bun and Deno this follows the imports that were actually resolved, and on miniflare the import specifiers of the virtual sources, including relative ones between path keys. On Node.js and Deno it also covers **real files** on the way from the entry, such as a `./lib.mjs` importing `#config`: they are re-evaluated on the next reload too, while files that don't depend on the module stay cached. CommonJS files are never re-evaluated. On Bun and miniflare, a real file other than the entry keeps the old module. Already-imported modules keep their instances, so pair it with `reloadModule()` to re-import the entry graph:
305
349
 
306
350
  ```ts
307
351
  await runner.invalidateModule("#config"); // re-runs the factory, busts the module
@@ -310,9 +354,29 @@ await runner.reloadModule(); // re-imports the entry, picking up the fresh modul
310
354
 
311
355
  When fetching through `RunnerManager` or `EnvServer`, the reload is automatic: `invalidateModule()` marks the manager dirty and the next `fetch()` reloads the entry once before serving (concurrent fetches share the reload), so no explicit `reloadModule()` call is needed.
312
356
 
313
- The module format is derived from the specifier extension: `.ts`/`.mts` sources are served as **TypeScript** and `.json` sources as **JSON modules**; everything else is plain JavaScript ESM:
357
+ To change the map itself while the runner is running, call `updateVirtualModules(changes)`. A source (string or factory) **adds or replaces** a key, and `null` **removes** it. All changes of one call are applied together in a single round trip to the worker:
314
358
 
315
359
  ```ts
360
+ await runner.updateVirtualModules({
361
+ "#routes": `export default ["/", "/about"]`, // add or replace
362
+ [resolve("src/generated/api.mjs")]: () => generateApi(), // factories run on the host
363
+ "#legacy": null, // remove
364
+ });
365
+ await runner.reloadModule(); // or let RunnerManager/EnvServer reload on the next fetch
366
+ ```
367
+
368
+ Changed and removed keys are invalidated like `invalidateModule()` does, together with the modules importing them, so the next `reloadModule()` sees the new map:
369
+
370
+ - An **added** key resolves from then on, path keys included. An importer that failed to import it, or that loaded the real file it now overrides, picks it up once it is re-evaluated: the reloaded entry, virtual importers, and on Node.js and Deno also real files between them. A runner started without `data.virtual` registers its virtual modules on the first update, and real files it loaded before aren't tracked as importers.
371
+ - A **removed** key falls through to normal resolution: the real file it overrode, or a "not found" error. On Bun, a removed key without a file extension (see the Bun notes above) fails to load instead of falling through, and on miniflare an unresolvable bare specifier gets an empty module, as usual there.
372
+
373
+ Calls are applied one at a time in call order (a later call never loses to a slower factory of an earlier one), and `reloadModule()` waits for pending ones. A call made before the runner is ready waits for it. `invalidateModule(specifier)` is the same as updating the key with its current source. The runner keeps its own copy of the map, never changing your `data.virtual`. `RunnerManager.updateVirtualModules()` marks the manager dirty like `invalidateModule()`, and `EnvServer` also keeps the changes for the runners it creates later (`reload()`, watch mode). Changes made before the server started are only recorded, and the first runner starts with them.
374
+
375
+ Each module has a **format**. By default it follows the key's extension, like Node.js does for files: `.cjs` is CommonJS, `.ts`/`.mts` TypeScript, `.cts` CommonJS TypeScript, `.json` JSON, `.jsx`/`.tsx` JSX and `.wasm` WebAssembly. Any other string is an ES module, and any other `Uint8Array` raw bytes. To set the format explicitly, for example on a key without an extension, pass `{ source, format }`:
376
+
377
+ ```ts
378
+ import { readFileSync } from "node:fs";
379
+
316
380
  await using runner = new NodeWorkerEnvRunner({
317
381
  name: "my-app",
318
382
  data: {
@@ -320,31 +384,60 @@ await using runner = new NodeWorkerEnvRunner({
320
384
  virtual: {
321
385
  "#entry.ts": `
322
386
  import { getGreeting } from "#util.ts";
323
- import config from "#config.json";
324
- const handler: () => Response = () => new Response(getGreeting(config.name));
387
+ import config from "#config";
388
+ import legacy from "#legacy.cjs";
389
+ import logo from "#logo";
390
+ import add from "#add.wasm";
391
+ const { exports } = await WebAssembly.instantiate(add);
392
+ const handler: () => Response = () =>
393
+ new Response(\`\${getGreeting(config.name)} \${legacy.answer} \${logo.length} \${exports.add(1, 2)}\`);
325
394
  export default { fetch: handler };
326
395
  `,
327
396
  "#util.ts": `export function getGreeting(name: string): string {
328
397
  return \`Hello, \${name}!\`;
329
398
  }`,
330
- "#config.json": JSON.stringify({ name: "virtual" }),
399
+ "#config": { source: JSON.stringify({ name: "virtual" }), format: "json" },
400
+ "#legacy.cjs": `module.exports = { answer: 42 };`,
401
+ "#logo": readFileSync("logo.png"), // a Uint8Array: `bytes`
402
+ "#add.wasm": readFileSync("add.wasm"),
331
403
  },
332
404
  },
333
405
  });
334
406
  ```
335
407
 
336
- - **TypeScript** is type-stripped by Node's native [type stripping](https://nodejs.org/api/typescript.html#type-stripping) (Node.js >= 22.18 / 23.6 — erasable syntax only) and by Bun's `ts` loader. On Deno, custom load hooks bypass its native type stripping, so sources are pre-stripped with [`module.stripTypeScriptTypes`](https://docs.deno.com/api/node/module/~/Module.stripTypeScriptTypes) (Deno >= 2.8.2); on older Deno without it, virtual `.ts`/`.mts` sources **throw at registration** — pass pre-transpiled JavaScript instead. On miniflare, sources are likewise pre-stripped with `module.stripTypeScriptTypes` on the host (workerd does not parse TypeScript).
408
+ | Format | Default for | Source | Importing it gives |
409
+ | --------------------- | -------------------------- | ------------ | ------------------------------------------------- |
410
+ | `module` | other string sources | string | the ES module |
411
+ | `commonjs` | `.cjs` | string | `module.exports` as default export, named exports |
412
+ | `module-typescript` | `.ts`, `.mts` | string | the ES module, types stripped |
413
+ | `commonjs-typescript` | `.cts` | string | like `commonjs`, types stripped |
414
+ | `json` | `.json` | string | the parsed value as default export |
415
+ | `jsx`, `tsx` | `.jsx`, `.tsx` | string | the ES module (Bun only) |
416
+ | `text` | — | string | the string as default export |
417
+ | `bytes` | other `Uint8Array` sources | `Uint8Array` | a `Uint8Array` as default export |
418
+ | `wasm` | `.wasm` | `Uint8Array` | a compiled `WebAssembly.Module` as default export |
419
+
420
+ The code formats are named like Node's [load formats](https://nodejs.org/api/module.html#loadurl-context-nextload), and `text` and `bytes` like the `with { type }` import attributes proposed for them (import them without attributes, though). An unknown format, or a source that doesn't fit its format (a `Uint8Array` for `json`, a string for `wasm`, bytes that aren't valid WebAssembly), closes the runner at startup with an error naming the key, or rejects `updateVirtualModules()` before anything changes.
421
+
422
+ - **TypeScript** is type-stripped by Node's native [type stripping](https://nodejs.org/api/typescript.html#type-stripping) (Node.js >= 22.18 / 23.6 — erasable syntax only) and by Bun's `ts` loader. On Deno, custom load hooks bypass its native type stripping, so sources are pre-stripped with [`module.stripTypeScriptTypes`](https://docs.deno.com/api/node/module/~/Module.stripTypeScriptTypes) (Deno >= 2.8.2); on older Deno without it, virtual TypeScript sources **throw at registration** — pass pre-transpiled JavaScript instead. On miniflare, sources are likewise pre-stripped with `module.stripTypeScriptTypes` on the host (workerd does not parse TypeScript).
337
423
  - **JSON** sources expose the parsed value as the default export on all runtimes. The `with { type: "json" }` import attribute is optional on Node.js and Bun; on Deno and miniflare it must be **omitted** (static imports carrying an import attribute bypass `registerHooks` resolution on Deno, and workerd rejects import attributes outright).
424
+ - **CommonJS** is loaded natively by Node.js and workerd (miniflare). Bun and Deno only parse in-memory sources as ES modules, so there the source runs inside an ES module wrapper, as strict-mode code. Named exports are detected like Node.js does ([cjs-module-lexer](https://github.com/nodejs/cjs-module-lexer)), but re-exports (`module.exports = require("./other.cjs")`) only add named exports on Node.js. `require()` resolves packages, real files and other virtual modules, with these exceptions:
425
+ - On Deno, `require()` only reaches real files and packages, not virtual modules (Deno reads required files from disk), and requiring an ES module crashes Deno 2.9 while module hooks are registered (a Deno bug).
426
+ - On miniflare, a virtual module that is `require()`d keeps its first instance when it changes (`invalidateModule()`, updates): only `import` specifiers are rewritten. Import it instead, or restart the runner.
427
+ - On Node.js, after a module re-exported by CommonJS (`module.exports = require("./dep.cjs")`) changes, Node reads it as an empty, circular module (this happens with real files too after deleting them from `require.cache`). Assign it first (`const dep = require("./dep.cjs"); module.exports = dep;`).
428
+ - **Text and bytes** can't be imported from memory natively on every runtime, so they are served as ES modules where needed. `bytes` gives each module instance its own `Uint8Array`. Bytes survive every transport, including the process runners' JSON IPC (as base64) and `updateVirtualModules()`.
429
+ - **WebAssembly** default-exports a compiled [`WebAssembly.Module`](https://developer.mozilla.org/docs/WebAssembly/Reference/JavaScript_interface/Module) on every runtime, so instantiate it yourself with `WebAssembly.instantiate(module, imports)`. This follows workerd, which compiles `.wasm` modules ahead of time and disallows compiling Wasm at runtime. Node.js and Deno's own `.wasm` imports instantiate the module instead ([Wasm ESM integration](https://github.com/WebAssembly/esm-integration)), and Bun's give a file path, so those aren't used.
430
+ - **JSX** is only supported on Bun, by its `jsx`/`tsx` loaders (configure the JSX runtime with `tsconfig.json` or pragma comments like `/** @jsxImportSource preact */`). Node.js, Deno and miniflare can't load JSX from memory and fail with an error naming the key: pre-transpile it to JavaScript, and pass `{ source, format: "module" }` to keep a `.jsx`/`.tsx` key.
338
431
 
339
- Virtual modules are registered inside the worker, before the entry is imported. On Node.js (>= 22.15 / 23.5) and Deno (>= 2.x) this uses [ESM customization hooks](https://nodejs.org/api/module.html#moduleregisterhooksoptions) (`module.registerHooks`); on Bun (which does not implement `registerHooks`) it uses [`Bun.plugin()`](https://bun.com/docs/runtime/plugins) virtual modules instead. The source string is treated as an ES module, and virtual specifiers (including a virtual entry) resolve across `reloadModule()`. On runtimes supporting neither mechanism, a warning is logged and registration is skipped. When the worker shuts down gracefully the registration is unregistered again (the `registerHooks` registration is deregistered; on Bun, which has no plugin-removal API, the in-memory source map is detached so fresh loads and reloads stop resolving).
432
+ Virtual modules are registered inside the worker, before the entry is imported. On Node.js (>= 22.15 / 23.5) and Deno (>= 2.8) this uses [ESM customization hooks](https://nodejs.org/api/module.html#moduleregisterhooksoptions) (`module.registerHooks`); on Bun (which does not implement `registerHooks`) it uses a [`Bun.plugin()`](https://bun.com/docs/runtime/plugins) runtime plugin instead, also for the Node.js runners when the host runtime is Bun. Each source is served in its format, and virtual specifiers (including a virtual entry) resolve across `reloadModule()`. On runtimes supporting neither mechanism, a warning is logged and registration is skipped. When the worker shuts down gracefully the registration is unregistered again (the `registerHooks` registration is deregistered; on Bun, which has no plugin-removal API, the registration is detached so fresh loads and reloads stop resolving, and an overridden real file loads from disk again).
340
433
 
341
- On `MiniflareEnvRunner` there is no in-worker registration: the runner's module fallback service serves virtual specifiers to workerd directly (taking precedence over disk files and the `transformRequest` pipeline, so a virtual key overrides a real file with the same path). One limitation: named `exports` (Durable Objects / WorkerEntrypoints) cannot be combined with a **virtual entry** — the wrapper would need a static re-export that miniflare cannot resolve at startup — and the runner fails fast with a clear error in that case.
434
+ On `MiniflareEnvRunner` there is no in-worker registration: the runner's module fallback service serves virtual specifiers to workerd directly (taking precedence over disk files and the `transformRequest` pipeline, so a virtual key overrides a real file with the same path). Named `exports` (Durable Objects / WorkerEntrypoints) also work with virtual entries. One limitation on miniflare v4: a **real** entry with auto-detected named exports can't import virtual modules, because miniflare's module locator reads its imports from disk at startup. Use a virtual entry, a separate `exports` module or miniflare v5 instead.
342
435
 
343
436
  #### Miniflare Runner
344
437
 
345
438
  Run your app in the Cloudflare Workers runtime using [miniflare](https://github.com/cloudflare/workers-sdk/tree/main/packages/miniflare).
346
439
 
347
- `env-runner` declares no peer dependencies — install `miniflare` yourself and pass it to the runner (see [Runtime dependencies](#runtime-dependencies)):
440
+ `env-runner` declares no peer dependencies — install `miniflare` (v4 or v5) yourself and pass it to the runner (see [Runtime dependencies](#runtime-dependencies)):
348
441
 
349
442
  ```bash
350
443
  npm install miniflare
@@ -431,9 +524,9 @@ await using runner = new MiniflareEnvRunner({
431
524
 
432
525
  A missing `wranglerConfigPath` file warns (an inline config is still applied). When `wrangler` is itself a string path, that path wins and `wranglerConfigPath` is ignored.
433
526
 
434
- The runner hosts a single fetch-only worker, so config entries it can't run are **dropped**: `assets`, `services`, `queues.consumers`, `workflows`, `tail_consumers`/`streaming_tail_consumers`, and Durable Object bindings to another script (`script_name`). Durable Object bindings to classes exported by your entry are kept — including bindings whose `script_name` is the worker's own `name` (the inline config's `name` when set, else the file's; with `wranglerEnv` suffixed `-<env>` unless the env section sets a `name`, e.g. `my-worker-staging`), which are local in `wrangler dev` too — and merged with [auto-detected exports](#auto-detected-exports). Pass any of the dropped options via `miniflareOptions` to opt back in.
527
+ The runner hosts a single fetch-only worker, so config entries it can't run are **dropped**: `assets`, `services`, `queues.consumers`, `workflows`, `tail_consumers`/`streaming_tail_consumers`, and Durable Object bindings to another script (`script_name`). Durable Object bindings to local classes (exported by your entry or an [exports module](#exports-module)) are kept — including bindings whose `script_name` is the worker's own `name` (the inline config's `name` when set, else the file's; with `wranglerEnv` suffixed `-<env>` unless the env section sets a `name`, e.g. `my-worker-staging`), which are local in `wrangler dev` too — and merged with [auto-detected exports](#auto-detected-exports). Pass any of the dropped options via `miniflareOptions` to opt back in.
435
528
 
436
- Whenever `wrangler` is enabled (`true`, a path, or an inline config), local state (KV, D1, R2, Durable Objects, ...) persists under `<dir>/.wrangler/state/v3` — the same place `wrangler dev` uses, so both share data. `<dir>` is the directory of the loaded config file, else of the requested config path (`wrangler` string or `wranglerConfigPath`, even if the file is missing), else the current working directory (e.g. inline-only configs, or `wrangler: true` with no file found). Set `miniflareOptions.defaultPersistRoot` (or any `*Persist` option, e.g. `kvPersist: false`) to opt out.
529
+ Whenever `wrangler` is enabled (`true`, a path, or an inline config), local state (KV, D1, R2, Durable Objects, ...) persists under `<dir>/.wrangler/state/v3` — the same place `wrangler dev` uses, so both share data. `<dir>` is the directory of the loaded config file, else of the requested config path (`wrangler` string or `wranglerConfigPath`, even if the file is missing), else the current working directory (e.g. inline-only configs, or `wrangler: true` with no file found). Set `miniflareOptions.defaultPersistRoot` (or any `*Persist` option, e.g. `kvPersist: false`; on miniflare v5, `resourcePersistencePath`) to opt out.
437
530
 
438
531
  Pass the [`wrangler`](https://www.npmjs.com/package/wrangler) package as `wranglerModule` — the imported module or a specifier — for full fidelity: TOML, config validation, and every binding type.
439
532
 
@@ -491,7 +584,7 @@ When `transformRequest` is provided:
491
584
 
492
585
  - The `unsafeModuleFallbackService` calls it with the resolved file path before falling back to raw disk reads
493
586
  - Module rules for `.ts`, `.tsx`, `.jsx`, and `.mts` are added automatically
494
- - Static `export *` re-exports are skipped in the wrapper to avoid miniflare's ModuleLocator pre-walking the import tree
587
+ - The wrapper never statically re-exports the entry (`export *`), to avoid miniflare's ModuleLocator pre-walking its import tree
495
588
 
496
589
  The callback should return `{ code: string }` for transformed modules, or `null`/`undefined` to fall back to the default raw file read.
497
590
 
@@ -529,6 +622,29 @@ await using runner = new MiniflareEnvRunner({
529
622
 
530
623
  Auto-wired bindings are merged with Durable Object bindings from `miniflareOptions` and a wrangler config: exports whose class is already bound (or whose binding name is taken) are skipped. Set `exports: false` to disable auto-detection entirely.
531
624
 
625
+ #### Exports Module
626
+
627
+ To load named exports from a separate module, set `exports` to its absolute path or a `data.virtual` key (a relative path resolves from the entry's directory, not the working directory). The wrapper re-exports it with `export *`, so re-exports and exported aliases work.
628
+
629
+ In this mode nothing is auto-detected or auto-wired: configure the bindings with `wrangler` or `miniflareOptions`. The entry's own `export class` declarations are **not** re-exported either, so re-export them from the exports module if they are bound.
630
+
631
+ ```ts
632
+ const runner = new MiniflareEnvRunner({
633
+ name: "app",
634
+ miniflare,
635
+ data: {
636
+ entry: "/path/to/server.mjs",
637
+ virtual: {
638
+ "#server-exports": 'export { Counter } from "/path/to/counter.mjs";',
639
+ },
640
+ },
641
+ exports: "#server-exports",
642
+ miniflareOptions: { durableObjects: { COUNTER: "Counter" } },
643
+ });
644
+ ```
645
+
646
+ In both modes, named exports are registered when the worker starts. Recreate the runner when their implementation or export list changes; `reloadModule()` only reloads the request entry.
647
+
532
648
  #### Error Capture
533
649
 
534
650
  By default, the runner wraps the user's `fetch` handler in a try/catch that returns structured JSON error responses with preserved stack traces:
@@ -569,6 +685,8 @@ const runner2 = new MiniflareEnvRunner({
569
685
  // Fully destroy: runner.dispose() or MiniflareEnvRunner.disposeAll()
570
686
  ```
571
687
 
688
+ An instance is only reused by runners with the same virtual module sources. Once `invalidateModule()` or `updateVirtualModules()` changes them, the instance leaves the cache (runners attached to it keep using it), and later runners start a fresh one.
689
+
572
690
  #### Vercel Runner
573
691
 
574
692
  Simulates a Vercel deployment environment with automatic header injection (`x-vercel-deployment-url`, `x-vercel-forwarded-for`, forwarding headers) and global context.
@@ -776,6 +894,23 @@ await using runner = new NodeProcessEnvRunner({
776
894
  });
777
895
  ```
778
896
 
897
+ Process runners (`NodeProcessEnvRunner`, `BunProcessEnvRunner`, `DenoProcessEnvRunner`) deliver `data` over IPC, so its size (e.g. large virtual modules) is not bound by environment variable limits. A custom process worker requests it once its message listener is attached:
898
+
899
+ ```ts
900
+ // custom-worker.ts
901
+ const data = await new Promise((resolve) => {
902
+ const onMessage = (message) => {
903
+ if (message?.event === "init-data") {
904
+ process.off("message", onMessage);
905
+ resolve(JSON.parse(message.data)); // `data` is sent as a JSON string
906
+ }
907
+ };
908
+ process.on("message", onMessage);
909
+ process.send({ event: "request-init-data" });
910
+ });
911
+ // ... start a server, then report it with `process.send({ address: { host, port } })`
912
+ ```
913
+
779
914
  ## Development
780
915
 
781
916
  <details>
@@ -1,21 +1,21 @@
1
- import { EnvRunner, RunnerMessageListener, WorkerAddress, WorkerHooks } from "./types.mjs";
1
+ import { EnvRunner, ResolvedVirtualModule, RunnerMessageListener, VirtualModuleUpdates, VirtualModules, WorkerAddress, WorkerHooks } from "./types.mjs";
2
2
  import { IncomingMessage } from "node:http";
3
3
  import { Socket } from "node:net";
4
- /** Factories run once on the host, before the worker spawns. */
5
- export type VirtualModuleSource = string | (() => string | Promise<string>);
6
- /** Virtual modules as a `specifier => source` map. */
7
- export type VirtualModules = Record<string, VirtualModuleSource>;
8
4
  export interface EnvRunnerData {
9
5
  name?: string;
10
6
  /**
11
7
  * Virtual modules importable from the entry, e.g.
12
- * `{ "#virtual-import": "export const foo = 1" }`. Factory sources run once
13
- * on the host before spawn. Not supported by the `self` runner.
8
+ * `{ "#virtual-import": "export const foo = 1" }`. A source is a string, a
9
+ * `Uint8Array` or `{ source, format }` (format by extension by default), or
10
+ * a factory returning one, which runs on the host before spawn. Change them
11
+ * at runtime with `updateVirtualModules()`. Not supported by the `self`
12
+ * runner (it closes with an error).
14
13
  */
15
14
  virtual?: VirtualModules;
16
15
  [key: string]: unknown;
17
16
  }
18
17
  export declare abstract class BaseEnvRunner implements EnvRunner, AsyncDisposable {
18
+ #private;
19
19
  closed: boolean;
20
20
  protected _name: string;
21
21
  protected _workerEntry: string;
@@ -25,7 +25,10 @@ export declare abstract class BaseEnvRunner implements EnvRunner, AsyncDisposabl
25
25
  protected _address?: WorkerAddress;
26
26
  protected _messageListeners: Set<(data: unknown) => void>;
27
27
  protected _pendingRequests: Set<(cause?: unknown) => void>;
28
+ protected _closeCause?: unknown;
28
29
  protected _virtualResolved?: Promise<void>;
30
+ protected _virtualUpdates: Promise<void>;
31
+ protected _processData?: string;
29
32
  constructor(opts: {
30
33
  name: string;
31
34
  workerEntry: string;
@@ -45,14 +48,26 @@ export declare abstract class BaseEnvRunner implements EnvRunner, AsyncDisposabl
45
48
  abstract sendMessage(message: unknown): void;
46
49
  onMessage(listener: RunnerMessageListener): void;
47
50
  offMessage(listener: RunnerMessageListener): void;
51
+ /** Rejects on timeout, and as soon as the runner closes (with the close cause). */
48
52
  waitForReady(timeout?: number): Promise<void>;
49
53
  rpc<T = unknown>(name: string, data?: unknown, opts?: {
50
54
  timeout?: number;
51
55
  }): Promise<T>;
56
+ /** Re-import the entry, after any pending `updateVirtualModules()` call. */
52
57
  reloadModule(timeout?: number): Promise<void>;
53
58
  /**
54
- * Invalidate a virtual module so the next `reloadModule()` re-evaluates it.
55
- * Factory sources are re-run on the host. Rejects for unknown specifiers.
59
+ * Set (add or replace) and remove (`null`) virtual modules in one round trip.
60
+ * Factory sources run on the host. Changed and removed keys, and the modules
61
+ * importing them, evaluate fresh on the next `reloadModule()`; a removed key
62
+ * falls through to normal resolution. Calls apply in order, waiting for the
63
+ * runner to become ready. The runner keeps its own copy of the map in sync,
64
+ * never changing the caller's `data.virtual`.
65
+ */
66
+ updateVirtualModules(changes: VirtualModuleUpdates, timeout?: number): Promise<void>;
67
+ /**
68
+ * Invalidate a virtual module so the next `reloadModule()` re-evaluates it:
69
+ * `updateVirtualModules()` with its current source, so a factory re-runs.
70
+ * Rejects for unknown specifiers.
56
71
  */
57
72
  invalidateModule(specifier: string, timeout?: number): Promise<void>;
58
73
  close(cause?: unknown): Promise<void>;
@@ -62,6 +77,17 @@ export declare abstract class BaseEnvRunner implements EnvRunner, AsyncDisposabl
62
77
  /** Placeholder origin for relative inputs; requests go to the worker address regardless. */
63
78
  protected _resolveFetchInput(input: string | URL | Request): string | URL | Request;
64
79
  protected _handleMessage(message: any): void;
80
+ /**
81
+ * Env for a process worker. Also snapshots the runner data, which goes over
82
+ * IPC on request (see `common/process-data.ts`), virtual module bytes as
83
+ * base64; throws if not JSON-serializable.
84
+ */
85
+ protected _processEnv(): NodeJS.ProcessEnv;
86
+ /**
87
+ * Process worker messages: answer the `request-init-data` handshake with the
88
+ * runner data (internal, not forwarded to listeners), handle everything else.
89
+ */
90
+ protected _handleProcessMessage(message: any): void;
65
91
  /**
66
92
  * Send a message and await the matching response. Rejects on timeout, on an
67
93
  * `error` response, and as soon as the runner closes.
@@ -74,15 +100,27 @@ export declare abstract class BaseEnvRunner implements EnvRunner, AsyncDisposabl
74
100
  }): Promise<T>;
75
101
  /**
76
102
  * Resolve factory `data.virtual` sources before spawn (functions can't cross
77
- * the worker boundary; the load hook can't await). `undefined` when there is
78
- * no factory, so subclasses can spawn synchronously.
103
+ * the worker boundary; the load hook can't await), and validate every
104
+ * module. `undefined` when there is no factory and nothing is invalid, so
105
+ * subclasses can spawn synchronously.
79
106
  */
80
107
  protected _resolveVirtualData(): Promise<void> | undefined;
81
- /** Re-run a factory virtual source and sync `data.virtual`; `undefined` if not a factory. */
82
- protected _refreshVirtualSource(specifier: string): Promise<string | undefined>;
108
+ /**
109
+ * Queue a virtual module update. Updates apply one at a time in call order
110
+ * (`changes` is read when its turn comes), each after the initial factory
111
+ * resolution: until it settles, `_data.virtual` aliases the factory map.
112
+ */
113
+ protected _enqueueVirtualUpdate(changes: () => VirtualModuleUpdates, timeout: number): Promise<void>;
114
+ /**
115
+ * Apply resolved changes (`null` removes) to the running worker in one round
116
+ * trip, bytes as base64 (the message may be JSON). Overridden by runners
117
+ * serving virtual modules from the host.
118
+ */
119
+ protected _applyVirtualUpdates(changes: Record<string, ResolvedVirtualModule | null>, timeout: number): Promise<void>;
83
120
  /**
84
121
  * Run `init` once `data.virtual` is resolved (synchronously without factories).
85
- * A failing factory closes the runner with the error as cause.
122
+ * A failing factory (or deferred `init`, e.g. a spawn error) closes the runner
123
+ * with the error as cause.
86
124
  */
87
125
  protected _initWithVirtualData(init: () => void): void;
88
126
  protected _closeSocket(): Promise<void>;