worker: add support for Web Workers - #64894
Conversation
|
Review requested:
|
|
I'm not sure if I should break the WPT additions into their own commit / PR for ease of reviewing? You should be able to collapse the Finally, I haven't added dedicated tests for this outside of the WPT, which should cover it. |
|
The web platform tests in this PR really ought to be separated out into a separate commit to make reviewing this easier. 1300+ files changes with 54k+ lines changed is very difficult to review in a single commit. |
Broken up! c73f2fe has the actual changes |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #64894 +/- ##
==========================================
+ Coverage 90.14% 90.29% +0.14%
==========================================
Files 746 763 +17
Lines 242849 248612 +5763
Branches 45771 46800 +1029
==========================================
+ Hits 218906 224473 +5567
- Misses 15438 15569 +131
- Partials 8505 8570 +65
🚀 New features to boost your workflow:
|
|
This is gargantuan. Can you add a review guide and/or split into chunks? |
|
I have:
|
It's only extremely large due to the added WPT tests. You can make it easier to review by
|
3372c74 to
4cfa0b4
Compare
|
Right now this implementation converts I can change the ESM loader for closer adherence to the spec, but iirc last time I suggested to do that we were concerned about how loader threads would handle blob URLs. @nodejs/loaders opinions? |
Signed-off-by: Aviv Keller <me@aviv.sh>
Signed-off-by: Aviv Keller <me@aviv.sh>
Signed-off-by: Filip Skokan <panva.ip@gmail.com>
|
@panva I changed the web crypto add it to the protoype rather than the instance, can you TAL and make sure it's still what you want? |
LGTM |
Fixes: #43583
Adds support for the Web Worker API as defined by the HTML Standard:
https://html.spec.whatwg.org/multipage/workers.html
The implementation trys to follow the specification as close as Node.js allows, so note the following differences:
SharedWorkeris not implemented. Its lifetime and sharing model depend on origins and browsing contexts, concepts that do not exist in Node.js.Worker scripts are loaded synchronously from the local filesystem rather than fetched over the network. As a result:
new Worker()andimportScripts()accept onlyfile:,data:, andblob:URLs.NotSupportedError.NetworkError(per the spec, this is emitted as anerrorevent).nosniff, and HTTP MIME type validation are not applicable. MIME type validation is performed only fordata:andblob:URLs.WorkerOptions.credentialsis validated for API compatibility but otherwise has no effect, since no network request is made.Node.js has no origin model. Consequently, concepts such as same-origin and cross-origin do not exist, and
location.originisnullforfile:workers.close()terminates the worker immediately instead of following the specification's "closing flag" algorithm. Code remaining in the current task afterclose()is therefore not executed.The worker global is the normal Node.js global object with
DedicatedWorkerGlobalScopeinserted into its prototype chain rather than the inverse (a fresh global created from the interface). Additionally, classicfile:workers are executed through the CommonJS/ESM loaders rather than as classic scripts, so top-level declarations do not become global properties. Classicdata:andblob:workers continue to execute as classic scripts.ErrorEvents dispatched toWorkerinstances includemessageanderror, but notfilename,lineno, orcolno. Unhandled worker errors are also not propagated further. These are a result of theworker_threadsimplementation that is underneath the web workers implemantion.The following
WorkerGlobalScopeevents are never dispatched:languagechange,online, andoffline, since these concepts do not exist in Node.js.rejectionhandledandunhandledrejection, since Node.js exposes equivalent process-level events but does not implement thePromiseRejectionEventinterface or the per-rejectionpreventDefault()behavior required by the HTML Standard.On the main thread, relative worker script URLs are resolved against the current working directory because there is no document base URL. Within a worker, relative URLs resolve against the worker's own URL, matching the specification.
AI Disclaimer: I used slight AI help to resolve issues that came up during me validating the WPT tests.
TODO before merge: