fix: bind default fetch to globalThis for Cloudflare Workers#3
Merged
Conversation
The client stored the bare globalThis.fetch and invoked it as a method (this.fetchImpl(...)). workerd requires fetch to be called with its own global as the receiver, so every request on Cloudflare Workers threw "TypeError: Illegal invocation" unless a custom fetch was passed in options. Node's undici is not receiver-sensitive, which is why the test suite never caught it. Found while building the tenthcup.coffee demo, which runs the SDK inside a Worker. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
What
PassmintHttpClientstored the default fetch bare (options.fetch ?? globalThis.fetch) and invoked it as a method (this.fetchImpl(url, init)). Cloudflare's workerd requiresfetchto be called with its own global as the receiver, so every SDK request inside a Worker threwTypeError: Illegal invocationunless the caller passed a customfetchin options. One-line fix: bind at assignment (globalThis.fetch?.bind(globalThis)), same approach as the Stripe/OpenAI SDKs.Why tests never caught it
Node's undici
fetchis not receiver-sensitive — calling it with a foreignthisworks fine, so the suite passed everywhere except real Workers runtimes.Regression test
Added a test whose fetch stub reproduces workerd's strictness (throws
Illegal invocationwhenthisisn'tglobalThis/undefined). Verified it fails against the previous code and passes with the fix.Found via
Building the tenthcup.coffee demo (React Router 7 on Cloudflare Workers): the very first
passes.createcall crashed. The demo currently works around it with the documentedfetchoption and carries aTODO(sdk feedback)comment pointing here — that shim gets deleted once this ships as a patch release (changeset included).