fix(auth): guard process.env access for browser/Vite compatibility#483
Open
peteroyce wants to merge 1 commit intodeepgram:mainfrom
Open
fix(auth): guard process.env access for browser/Vite compatibility#483peteroyce wants to merge 1 commit intodeepgram:mainfrom
peteroyce wants to merge 1 commit intodeepgram:mainfrom
Conversation
Direct access to process.env throws ReferenceError in environments where the global process object does not exist, such as browser builds with Vite, Webpack, or Parcel without a process polyfill. Wrap both process.env accesses in HeaderAuthProvider with a typeof process !== "undefined" guard so the SDK works seamlessly in browser, Node.js, and edge runtimes without requiring a polyfill. Closes deepgram#349
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
HeaderAuthProviderdirectly accessesprocess.envwithout checking whether the globalprocessobject exists. In browser environments (Vite, Webpack without aprocesspolyfill, Parcel, Cloudflare Workers, Deno), this throws aReferenceErrorat runtime:Root Cause
Optional chaining (
process.env?.KEY) only guards againstenvbeingnull/undefinedon an existingprocessobject. Ifprocessitself is not defined as a global, the expression throws before optional chaining can help.Fix
Wrap both
process.envaccesses withtypeof process !== "undefined"guards:typeofchecks never throw regardless of the runtime environment, making this safe in Node.js, browsers, edge runtimes, and any environment that doesn't defineprocess.Testing
Existing behaviour is unchanged in Node.js. In browser environments the SDK now gracefully falls back to
undefinedwhenDEEPGRAM_API_KEYcannot be read fromprocess.env, instead of crashing.Closes #349