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. |
| const kJavaScriptMIMETypes = new SafeSet([ | ||
| 'application/ecmascript', | ||
| 'application/javascript', | ||
| 'application/x-ecmascript', | ||
| 'application/x-javascript', | ||
| 'text/ecmascript', | ||
| 'text/javascript', | ||
| 'text/javascript1.0', | ||
| 'text/javascript1.1', | ||
| 'text/javascript1.2', | ||
| 'text/javascript1.3', | ||
| 'text/javascript1.4', | ||
| 'text/javascript1.5', | ||
| 'text/jscript', | ||
| 'text/livescript', | ||
| 'text/x-ecmascript', | ||
| 'text/x-javascript', | ||
| ]); |
There was a problem hiding this comment.
Undici keeps this map internally, but does not expose it, fwiw
There was a problem hiding this comment.
This wrapper is because the WPT tests import assuming they are running on a server (e.g. /absolute/path/to/import), so I rewrite them as relative.
|
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 |
Signed-off-by: Aviv Keller <me@aviv.sh>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #64894 +/- ##
==========================================
+ Coverage 90.14% 90.15% +0.01%
==========================================
Files 746 747 +1
Lines 242849 243813 +964
Branches 45771 45867 +96
==========================================
+ Hits 218906 219805 +899
- Misses 15438 15500 +62
- Partials 8505 8508 +3
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
please revert this file change:
Refs: #63938
Refs: nodejs/node-core-utils#1094
Refs: nodejs/node-core-utils#1082
|
This is gargantuan. Can you add a review guide and/or split into chunks? |
| "modules/resources/dynamic-import-remote-origin-credentials-checker-worker.sub.js": { | ||
| "skip": "Not a test file; used as a worker script by other tests" | ||
| }, | ||
| "modules/resources/dynamic-import-remote-origin-referrer-checker-worker.sub.js": { | ||
| "skip": "Not a test file; used as a worker script by other tests" | ||
| }, | ||
| "modules/resources/dynamic-import-remote-origin-script-worker.sub.js": { | ||
| "skip": "Not a test file; used as a worker script by other tests" | ||
| }, | ||
| "modules/resources/static-import-remote-origin-credentials-checker-worker.sub.js": { | ||
| "skip": "Not a test file; used as a worker script by other tests" | ||
| }, | ||
| "modules/resources/static-import-remote-origin-referrer-checker-worker.sub.js": { | ||
| "skip": "Not a test file; used as a worker script by other tests" | ||
| }, | ||
| "modules/resources/static-import-remote-origin-script-worker.sub.js": { | ||
| "skip": "Not a test file; used as a worker script by other tests" | ||
| }, |
There was a problem hiding this comment.
Does this apply to all *.sub.js as a convention? See Wildcard patterns in file names
| * For `blob:` URLs, the script must be held in memory, so blobs backed by a file, | ||
| such as those returned by [`fs.openAsBlob()`][], cannot be used. | ||
|
|
||
| ### Differences from the HTML Standard |
There was a problem hiding this comment.
Finally, I haven't added dedicated tests for this outside of the WPT, which should cover it.
I believe that for all limitations or differences you document there should be dedicated tests.
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.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.