fix(wasm): update workerData polyfill and usage - #2474
Conversation
Summary of ChangesHello @xusd320, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refines the handling of WebAssembly (WASM) worker thread metadata and polyfills to align more closely with Node.js 'worker_threads' conventions. It standardizes the use of 'threadId' over 'workerId' and implements a more robust, proxy-based 'workerData' mechanism within the polyfills, ensuring consistent and accurate worker thread emulation across the codebase. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request is a great improvement, refactoring the worker thread metadata handling to standardize on threadId and using a Proxy for workerData to better emulate Node.js worker threads. The changes are well-executed and improve the robustness and maintainability of the codebase. I've identified a couple of areas for improvement: a potential bug with a stale threadId value in the polyfill and a minor inconsistency in its usage. Addressing these will make the implementation even more solid.
| ); | ||
|
|
||
| export const isMainThread = false; | ||
| export const threadId = workerData?.threadId || 0; |
There was a problem hiding this comment.
The threadId is calculated only once when this module is imported. At that time, self.workerData is likely not yet available, causing threadId to be permanently initialized to 0. This is a potential bug as the value will be stale.
Since nodePolyFills.ts already defines a dynamic getter for threadId on the final polyfill object, this export is both buggy and redundant. I recommend removing it to prevent potential issues if this module is imported directly elsewhere.
| // @ts-ignore | ||
| return self.workerData?.workerId || 0; | ||
| return self.workerData?.threadId || 0; |
There was a problem hiding this comment.
For consistency with the other changes in this PR that refactor access to workerData through the new proxy, this getter should also use workerThreads.workerData. This also has the benefit of removing the need for @ts-ignore and makes the code cleaner.
| // @ts-ignore | |
| return self.workerData?.workerId || 0; | |
| return self.workerData?.threadId || 0; | |
| return workerThreads.workerData?.threadId || 0; |
This pull request refactors how worker thread metadata is handled in the loader and polyfill code, standardizing the use of
threadIdinstead ofworkerIdand improving compatibility with Node.js worker threads APIs. The changes also introduce a more robust proxy-based implementation for accessingworkerDatain the polyfills, ensuring consistent access patterns across the codebase.Worker thread metadata standardization and refactoring:
workerIdwiththreadIdin theLoaderRunnerMetainterface, worker initialization, and throughout the loader worker pool and worker files for consistency with Node.js conventions. [1] [2] [3] [4]next.js, likely to pull in upstream changes or fixes.Polyfill improvements and compatibility:
workerThreadsPolyfill.tsto export a proxy-basedworkerDataobject, ensuring dynamic property access and better emulation of Node.js worker threads. Also, added athreadIdexport based on the proxiedworkerData.fsPolyfill,fsPromisesPolyfill,nodePolyFills) to use the newworkerDataproxy for accessing worker metadata, replacing direct access toself.workerData. [1] [2] [3] [4]These changes improve the maintainability and accuracy of the worker thread emulation, making the codebase more robust and consistent with Node.js APIs.