Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork6.4k
fix(platform[web worker]): improve globalThis variable retrieval for …#11495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Conversation
…browser environment
coderabbitaibot commentedMay 24, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
WalkthroughThe control flow of the Changes
Sequence Diagram(s)sequenceDiagram participant Code as Application Code participant PT as PlatformTools participant W as window participant GT as globalThis participant G as global Code->>PT: call getGlobalVariable() alt window is defined PT->>W: return window else globalThis is defined PT->>GT: return globalThis else PT->>G: return global end Poem
Note ⚡️ AI Code Reviews for VS Code, Cursor, WindsurfCodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback. 🪧 TipsChatThere are 3 ways to chat withCodeRabbit:
SupportNeed help? Create a ticket on oursupport page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/platform/BrowserPlatformTools.template (1)
167-169
:ConsolidateBuffer
assignments for modern environments.Assigning to both
window.Buffer
andglobalThis.Buffer
is redundant when they refer to the same object (in most browsers). Consider unifying this into a single block that targetsglobalThis
(with a fallback for legacy). Also, in the Node case you’re usingrequire("buffer/")
—the standard import isrequire("buffer")
.Optional refactor:
- if (typeof window !== "undefined") {- window.Buffer = Buffer;- }- if (typeof globalThis !== "undefined") {- globalThis.Buffer = Buffer;- }+ const _g = (typeof globalThis !== "undefined" && globalThis) ||+ (typeof window !== "undefined" && window);+ if (_g) {+ _g.Buffer = Buffer;+ }And update the Node branch to:
- if (typeof global !== "undefined" && typeof require !== "undefined") {- global.Buffer = require("buffer/").Buffer;- }+ if (typeof global !== "undefined" && typeof require !== "undefined") {+ global.Buffer = require("buffer").Buffer;+ }
if (typeof globalThis !== "undefined") { | ||
return globalThis | ||
} | ||
// NativeScript uses global, not window | ||
return global |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
🛠️ Refactor suggestion
Guard the fallback toglobal
to avoid ReferenceError.
Right now, if bothwindow
andglobalThis
are undefined, the code unconditionally doesreturn global
, which will throw aReferenceError
in many browser contexts (whereglobal
is not defined). You should explicitly check fortypeof global !== "undefined"
before returning it and provide a clear error if no global object is found.
Proposed diff:
- if (typeof globalThis !== "undefined") {- return globalThis- }- // NativeScript uses global, not window- return global+ if (typeof globalThis !== "undefined") {+ return globalThis+ }+ if (typeof global !== "undefined") {+ return global+ }+ throw new Error("PlatformTools.getGlobalVariable(): no global object available")
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if (typeof globalThis !== "undefined") { | |
return globalThis | |
} | |
// NativeScript uses global, not window | |
return global | |
if (typeof globalThis !== "undefined") { | |
return globalThis | |
} | |
if (typeof global !== "undefined") { | |
return global | |
} | |
throw new Error("PlatformTools.getGlobalVariable(): no global object available") |
🤖 Prompt for AI Agents
In src/platform/BrowserPlatformTools.template around lines 24 to 28, thefallback to return global is not guarded, which can cause a ReferenceError ifglobal is undefined. Modify the code to check if typeof global !== "undefined"before returning global, and if neither globalThis nor global is defined, throwa clear error indicating no global object is available.
Hi@gioboa ,MDN: globalThis is the doc, could you please take a look at this fix for web worker, thanks! |
commit: |
Nowadays Honestly, I don't see a reason to keep the |
coveralls commentedMay 26, 2025
@alumni I agree with your point. This PR only addresses the issue of running under WebWorker. |
alumni commentedJul 10, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Would be great to have this fixed for NodeJs Workers as well before merging the PR. Also, some cleanup would be nice, i.e. replacing |
Uh oh!
There was an error while loading.Please reload this page.
…browser environment
Description of change
Pull-Request Checklist
master
branchFixes #00000
Summary by CodeRabbit