feat(unenv-preset): add native node:worker_threads module support#12043
feat(unenv-preset): add native node:worker_threads module support#12043petebacondarwin merged 22 commits intomainfrom
Conversation
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
🦋 Changeset detectedLatest commit: 3a9da95 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
create-cloudflare
@cloudflare/kv-asset-handler
miniflare
@cloudflare/pages-shared
@cloudflare/unenv-preset
@cloudflare/vite-plugin
@cloudflare/vitest-pool-workers
@cloudflare/workers-editor-shared
@cloudflare/workers-utils
wrangler
commit: |
|
Claude finished @petebacondarwin's task —— View job Changeset ReviewReviewing changeset files...
✅ All changesets look good The changeset
|
95b868a to
949c436
Compare
… flag The node:worker_threads module was previously importing MessageChannel and MessagePort from globalThis, which meant it only worked when the expose_global_message_channel compatibility flag was enabled. This change introduces a new internal module (cloudflare-internal:messagechannel) that exposes MessageChannel and MessagePort for use by built-in modules, independent of the global flag. Changes: - Add MessageChannelModule C++ class in messagechannel.h - Register cloudflare-internal:messagechannel as an internal module - Update node:worker_threads to import from the internal module - Add TypeScript type definitions for the internal module - Remove expose_global_message_channel from worker_threads tests This unblocks cloudflare/workers-sdk#12043 which needs worker_threads to work without requiring the global flag.
|
Blocked on cloudflare/workerd#6069 |
a5bfd5b to
ce12ada
Compare
This landed in v1.20260214.0 and |
vicb
left a comment
There was a problem hiding this comment.
I that the thing you changed in worked recently?
So we need a peer dep bump?
Otherwise LGTM
Oops! Good spot @vicb - I forgot that it was not enough to update workerd in general, we need to bump the peer dep. Will add that now. |
|
This is the change:
|
…y all workerd versions) Co-Authored-By: [email protected] <[email protected]>
… differences Co-Authored-By: [email protected] <[email protected]>
…mpatibility Co-Authored-By: [email protected] <[email protected]>
…reads feature Co-Authored-By: [email protected] <[email protected]>
- Both workerd and unenv export MessageChannel, MessagePort, Worker, BroadcastChannel - Use getRuntimeFlagValue() to check if native workerd is enabled - Test behavioral differences (Worker/BroadcastChannel throw in native workerd) Co-Authored-By: [email protected] <[email protected]>
- Native workerd exports MessageChannel/MessagePort as globalThis refs which may be undefined - Split type assertions for native vs unenv stub implementations - Only test MessageChannel in unenv stub mode Co-Authored-By: [email protected] <[email protected]>
This actually now fails because the MessageChannel in the `node:worker_threads` module is pulled from the `global.MessageChannel` and that is only available if `expose_global_message_channel` compat flag is on...
> Make node:worker_threads independent of expose_global_message_channel flag by @petebacondarwin in cloudflare/workerd#6069
5d7060a to
2a78e21
Compare
> Make node:worker_threads independent of expose_global_message_channel flag by @petebacondarwin in cloudflare/workerd#6069
> Make node:worker_threads independent of expose_global_message_channel flag by @petebacondarwin in cloudflare/workerd#6069
> Make node:worker_threads independent of expose_global_message_channel flag by @petebacondarwin in cloudflare/workerd#6069
Adds support for the native
node:worker_threadsmodule from workerd when both theenable_nodejs_worker_threads_moduleandexperimentalcompatibility flags are enabled. This follows the same pattern as other experimental modules (repl).Implementation Comparison
MessageChannelglobalThis.MessageChannel(native Web API)MessagePortglobalThis.MessagePort(native Web API)BroadcastChannelERR_METHOD_NOT_IMPLEMENTEDWorkerERR_METHOD_NOT_IMPLEMENTEDgetEnvironmentData/setEnvironmentDataisMainThread,parentPort,threadId,workerData,resourceLimitsSHARE_ENVpostMessageToThreadERR_METHOD_NOT_IMPLEMENTEDisInternalThreadfalsefalseBreaking change analysis: The main behavioral differences are that
WorkerandBroadcastChannelconstructors now throwERR_METHOD_NOT_IMPLEMENTEDinstead of returning no-op instances. This is more correct behavior and uses proper Node.js error codes.Implementation Notes
getRuntimeFlagValue("enable_nodejs_worker_threads_module")to detect native implementation at runtime (not export sniffing)Human Review Checklist
getWorkerThreadsOverrides()function follows the same pattern as other experimental modules (requiresexperimentalflag)enable_nodejs_worker_threads_module/disable_nodejs_worker_threads_module)getRuntimeFlagValue()instead of sniffing exportsLink to Devin run