Repository navigation
Worker instantiation from URLs #30780
Description
Activity
- addedworkerIssues and PRs related to the worker_threads module and Worker API.Issues and PRs related to the worker_threads module and Worker API.esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.
on Dec 3, 2019 IIRC we have prior art on accepting file urls for various APIs, but i don't remember which ones.
IIRC we have prior art on accepting file urls for various APIs, but i don't remember which ones.
The initial URL support for the
fsmodule supported this, but it was later withdrawn from the PR because a) it could invalidate previously existing security checks when reading from paths that are partially user-controlled and b) file URLs are, sadly, also valid paths for files in directories namedfile:.I think a) is not a large concern here.
b) has typically been also seen as a backwards-incompatible change. Perhaps to implement it that way upfront now though would be permissible.
Then, if users really need a directory named file: they can convert the path into a URL.
Support for
new Worker(import.meta)would eliminate ambiguity. Just a thought, not suggesting it's a good idea.That said a filename matching
/^file:/or/^data:/is very edge case for this API, does anyone even usenew Worker(pathRelativeToCWD)? The documentation only suggestsnew Worker(__filename)which would never result in a filename that is ambiguous to URL's.new Worker('./other-file.js')would be unreliable as it would be based onprocess.cwd(), insteadnew Worker(require.resolve('./other-file.js'))ornew Worker(new URL('./other-file.js', import.meta.url).href)would be needed. I'd go as far as saying that passing a relative path to this API should be an error.does anyone even use
new Worker(pathRelativeToCWD)?I used it multiple times. It is convenient in apps to always use
new Worker('./src/workers/myworker.js')from anywhere in the hierarchy and as not necessarily unreliable. My apps are always started with their root as working directory.Reacted by Anna HenningsenJust having something like the following would work...
new Worker(new URL(import.meta.url));
^ This approach is how
fsaccepts URLs, it just acceptsURLobjects, strings are always treated as paths.- added a commit that references this issue
on Mar 11, 2020 - addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.
on Mar 12, 2020 - added a commit that references this issue
on Mar 17, 2020 - added a commit that references this issue
on Mar 24, 2020 - added 2 commits that reference this issue
on Apr 25, 2020
When creating workers from within modules, we don't have
__filenameor__dirnameso rather have to rely on import.meta.url, something like:To avoid the
fileURLToPathcall being necessary here, should we consider supporting URLs as input intonew Worker?