Hotfix: bind fetch to globalThis to avoid 'Illegal invocation' - #9
Merged
Conversation
Storing the native browser fetch as a bare reference on an instance field (VaultClient.fetchImpl) or as a default parameter loses its Window receiver. At call time the browser sees `this !== Window` and throws TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation. jsdom's fetch is permissive and didn't catch this. Bind `fetch` to `globalThis` in all four default paths (VaultClient constructor, discoverAuthServer, registerClient, beginOAuth, completeOAuth). Add a regression test that installs a this-aware fetch shim mimicking the browser's check — instantiates VaultClient without an explicit fetchImpl and confirms vaultInfo() resolves. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
6 tasks
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.
Summary
fetchrequiresthis === Window. Storing a barefetchreference onVaultClient.fetchImpl(or as a default param in oauth/discovery helpers) loses that binding — at call-timethisis the VaultClient instance and the browser throwsTypeError: Failed to execute 'fetch' on 'Window': Illegal invocation.fetchis permissive and didn't enforce the invariant, so existing tests passed while the browser broke.fetch.bind(globalThis)in all four default paths —VaultClientconstructor,discoverAuthServer,registerClient,beginOAuth,completeOAuth.this !== globalThis, instantiatesVaultClientwithout an explicitfetchImpl, and assertsvaultInfo()resolves. Verified it fails without the fix.Test plan
bun run lintcleanbun run typecheckcleanbun run test— 101 tests pass (new regression included)bun run buildsucceeds🤖 Generated with Claude Code