fix(js-sdk): detect Request inputs by shape, not instanceof, in the Node fetch bridge#1610
Open
himself65 wants to merge 1 commit into
Open
fix(js-sdk): detect Request inputs by shape, not instanceof, in the Node fetch bridge#1610himself65 wants to merge 1 commit into
himself65 wants to merge 1 commit into
Conversation
…ode fetch bridge A Request minted by a different Request class than the current globalThis.Request fails the brand checks in toUndiciRequestInput and limitConcurrency. Runtimes and tools replace globalThis.Request the same way they replace globalThis.fetch (test environments, instrumentation, server shims such as @hono/node-server) — and two copies of one shim can coexist in a process, each installing its own class, so a Request built by one class is checked against the other and disowned. The miss is fatal on the undici path: the foreign Request is passed to undici's fetch verbatim, fails undici's own brand check too, gets coerced to a URL string, and every API call crashes with 'Failed to parse URL from [object Request]' (observed in production-like apps embedding the SDK next to @hono/node-server: sandbox create/list died on every call). Detect Requests by shape instead (url + method both present) and destructure them into the plain (url, init) pair undici accepts; the in-flight limiter now also honors the abort signal of a foreign-class Request while it waits for a slot. Both new tests reproduce the real-world event with sibling subclasses of the native Request plus a stubbed global, and fail without the fix.
|
We require contributors to sign our Contributor License Agreement, and we don't have @himself65 on file. You can sign our CLA at https://e2b.dev/docs/cla . Once you've signed, post a comment here that says '@cla-bot check' |
🦋 Changeset detectedLatest commit: c58a1e8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Author
|
@cla-bot check |
|
The cla-bot has been summoned, and re-checked this pull request! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Symptom
In an app embedding the JS SDK, every control-plane call (
Sandbox.create,Sandbox.list, …) crashed with:Root cause
toUndiciRequestInput(and the in-flight limiter's signal extraction) decide Request-ness withinput instanceof Request. That brand check misses any Request minted by a differentRequestclass — and replaced/duplicated Request classes are a real thing in Node processes: runtimes and tools swapglobalThis.Requestexactly like they swapglobalThis.fetch(test environments, instrumentation, server shims).src/undici.tsalready late-binds the global fetch for this very reason ("msw, instrumentation may replaceglobalThis.fetch") — this PR extends the same reasoning toRequest.The concrete case we debugged: the host app served HTTP with
@hono/node-server, which installs a lightweightclass Request extends GlobalRequestshim ontoglobalThisat import — and the dependency tree contained multiple copies of that shim (different versions via different packages), each with its own class. A Request built by one copy's class failsinstanceofagainst the other copy's, so the SDK passed it verbatim into the npm-undicifetch, whose own brand check also can't recognize it → it coerces the object to a URL string →new URL('[object Request]')throws. Sandbox creation and listing died on every call; which copy "wins" the global is import-order-dependent, so the failure appears and disappears with unrelated dependency changes.Fix
Treat anything carrying
url+methodas a Request (isRequestLikeinsrc/utils.ts) and destructure it into the plain(url, init)pair undici accepts — the existing destructuring already produces exactly that. Two call sites:toUndiciRequestInput(src/undici.ts) — the crash above;limitConcurrency(src/api/inflight.ts) — a foreign-class Request's abort signal was silently ignored while the request waited for a slot.No behavior change for native Requests (the
instanceoffast path still short-circuits), strings, orURLobjects (nomethod, so the shape check can't match them).Tests
Both new tests reproduce the real-world event faithfully with sibling subclasses of the native Request (instances of one are fully functional Requests but fail
instanceofagainst the other) plusvi.stubGlobal('Request', …):tests/undici.test.ts— a foreign-class Request must reach the (injected fake) undici fetch destructured as(url, init), not verbatim;tests/api/inflight.test.ts— an already-aborted foreign-class Request must reject withAbortErrorwithout consuming a slot.Red/green verified: with
src/reverted, exactly these two tests fail; with the fix,vitest run tests/undici.test.ts tests/api/inflight.test.ts= 11/11, andpnpm build(tsc + tsdown) +pnpm lintare clean. Changeset included (e2b: patch).🤖 Generated with Claude Code