Repository navigation
[vitest-pool-workers]: the module fallback service makes one sequential HTTP round-trip per node_modules import — has pre-injecting resolved modules been considered?
#16957
workers-devprod
started this conversation in
Feature Requests
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Moved from #14283. Originally opened by tm0h on 2026-06-12.
Summary
Under
@cloudflare/vitest-pool-workers, everynode_modulesmodule thatworkerdcan't resolve natively is fetched over HTTP from the Node-side Module Fallback
Service, one module per round-trip. Because
workerddiscovers a module'sdependencies only after it has compiled that module, these round-trips are
inherently sequential. For a test worker with a CJS-heavy transitive dependency
tree, this scales as O(number of modules) sequential HTTP round-trips before the
test body even runs — which is structurally slow regardless of how fast each
individual round-trip is.
This is effectively the unfinished half of #5395
("Improve Workers Vitest integration performance"). That issue named two levers for
the fallback-service cost: the number of requests (~800 at the time) and the
per-request cost ("each HTTP request starts a new OS thread and opens a new TCP
connection… use a thread pool with connection reuse"). cloudflare/workerd#6115
("reuse HTTP connections in fallback service to prevent port exhaustion") implemented
the connection-reuse half — it stops port exhaustion (
Fixes#7954)and reuses connections — and #5395 was then closed with the maintainer note that it was
"in large part addressed by #6115."
The half that remains is the number of sequential round-trips itself. Connection
reuse lowers per-request transport overhead but does not reduce how many requests are
made, nor that they are issued one after another. For CJS-heavy dependency trees that
count is the structural bottleneck.
This issue argues from the mechanism rather than from a specific benchmark, and asks
whether pre-injecting resolved dependencies into
workerd's module graph — addressingthe request-count lever directly — is a direction the team would consider.
Environment
@cloudflare/vitest-pool-workers0.16.4vitest4.1.6wrangler4.93.0miniflare4.20260508.0(transitively pinned by vitest-pool-workers)workerd1.20260508.1(transitively pinned via miniflare)20The mechanism
When
workerdencounters animport/requireit can't resolve from its own moduleregistry, it issues an HTTP request to the fallback service that the pool runs on the
Node side; Node (via Vite) resolves the specifier, reads the file, and returns the
module source. Two properties of this make CJS-heavy trees expensive:
One round-trip per module. Each unresolved specifier — including every internal
relative
requireinside a package — is its own request/response.The round-trips are sequential, not batchable.
workerdwalks the module graphlazily: it only learns that a module needs
require('./internal-helper')after ithas fetched and compiled the parent. So there is no complete list of "everything this
worker needs" to fetch up front — modules are discovered, and therefore fetched, one
at a time in dependency order.
A single CJS package can fan out to hundreds of these requests on its own — e.g.
lodash's entry point pulls in a large tree of internal files, each via its ownrelative
require, each a separate sequential round-trip.The consequence: the dominant cost of a vitest-pool-workers run on a CJS-heavy project
is module loading over the fallback channel, not the test logic itself. The total
is essentially
(number of modules) × (per-round-trip latency), serialized.Why caching / connection-level fixes don't address it
We explored the obvious mitigations; sharing the lessons so they can be skipped:
overhead but leaves the count of sequential round-trips unchanged — the requests
still happen one after another.
(method, specifier, referrer)helps littlewithin a single run, because modules are requested from largely unique
(specifier, referrer)pairs, so intra-run hit rate is low.round-trips, but in our exploration it traded one problem for another: bundling pulls
Node-builtin/polyfill bloat into otherwise-tiny packages and can regress them badly,
and a fallback-side intercept only catches top-level specifiers (sub-path/internal
requires still miss). It was not a net win.
The common thread: the bottleneck is the volume of sequential round-trips, so the
effective fix is to remove the round-trips, not to make them cheaper or fewer-by-
bundling.
Proposed direction (open to alternatives)
Pre-inject the resolved
node_modulesmodules intoworkerd's module graph beforetests run — register them in Miniflare's
modulesconfig soworkerdresolves themnatively and the fallback service is never consulted for
node_modules.We'd suggest injecting the individually-resolved files as-is (no bundling):
in-process and up front, and strips out only the sequential HTTP layer.
side-effect semantics exactly.
workerd's existing CJS handling by injecting each file with its correctmodule type — no transpilation needed.
The natural place looks like wherever the pool assembles the Miniflare worker's
modulesarray, extending it with the pre-resolved dependency set.Open questions for maintainers
workerdparse/compile themodulesarray eagerly at startup, or lazily onfirst import? This determines whether injecting the full dependency set (including
unused files) is free or costly, and thus whether injection should be scoped to the
statically-reachable import graph.
step (cleaner, more workflow)?
node_moduleschanges (content hash / version check).pkg/sub) — enumerating entry points frompackage.jsonexports.we're not seeing (e.g. correctness around dynamic
require, conditional exports)?Impact
This affects any test suite with a CJS-heavy transitive dependency tree: module loading
over the fallback channel becomes the dominant cost of the run, growing with dependency
count rather than with the amount of code under test.
References
Fixes🐛 BUG: Error running tests in when usingsingleWorker: false#7954); did not reduce the round-trip count.singleWorker: false#7954 — Error running tests withsingleWorker: false(port-exhaustion crash; fixed by chore: sort wrangler deps, update lockfile #6115).node_modules/.vite/deps/) — already performs CJS→ESM conversion and tree-shaking; a possible source of pre-resolved modules, though it targets browser/SSR conditions rather thanworkerd.jamesopstad commented on 2026-06-22:
Hi @tm0h. Once there is server-side support for bundled dev in Vite, we're going to explore if we can use that in vitest-pool-workers. That would resolve this issue as the dependencies would bundled in a way that is much closer to production.
All reactions