Skip to content

fix: bind default fetch to globalThis for Cloudflare Workers#3

Merged
alexpate merged 1 commit into
mainfrom
fix/bind-default-fetch
Jul 7, 2026
Merged

fix: bind default fetch to globalThis for Cloudflare Workers#3
alexpate merged 1 commit into
mainfrom
fix/bind-default-fetch

Conversation

@alexpate

@alexpate alexpate commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

What

PassmintHttpClient stored the default fetch bare (options.fetch ?? globalThis.fetch) and invoked it as a method (this.fetchImpl(url, init)). Cloudflare's workerd requires fetch to be called with its own global as the receiver, so every SDK request inside a Worker threw TypeError: Illegal invocation unless the caller passed a custom fetch in 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 fetch is not receiver-sensitive — calling it with a foreign this works fine, so the suite passed everywhere except real Workers runtimes.

Regression test

Added a test whose fetch stub reproduces workerd's strictness (throws Illegal invocation when this isn't globalThis/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.create call crashed. The demo currently works around it with the documented fetch option and carries a TODO(sdk feedback) comment pointing here — that shim gets deleted once this ships as a patch release (changeset included).

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>
@alexpate alexpate merged commit 2f7dabd into main Jul 7, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant