-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…browser environment
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 with CodeRabbit:
SupportNeed help? Create a ticket on our support 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: ConsolidateBufferassignments for modern environments.Assigning to both
window.BufferandglobalThis.Bufferis 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 to global to avoid ReferenceError.
Right now, if both window and globalThis are undefined, the code unconditionally does return global, which will throw a ReferenceError in many browser contexts (where global is not defined). You should explicitly check for typeof 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, the
fallback to return global is not guarded, which can cause a ReferenceError if
global is undefined. Modify the code to check if typeof global !== "undefined"
before returning global, and if neither globalThis nor global is defined, throw
a 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 |
|
@alumni I agree with your point. This PR only addresses the issue of running under WebWorker. |
|
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 |
gioboa
left a comment
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.
Thanks @dasoncheng for your help
…browser environment (typeorm#11495)

…browser environment
Description of change
Pull-Request Checklist
masterbranchFixes #00000Summary by CodeRabbit