Skip to content

Worker instantiation from URLs #30780

Description

@guybedford

When creating workers from within modules, we don't have __filename or __dirname so rather have to rely on import.meta.url, something like:

import { Worker } from 'worker_threads';
import { fileURLToPath } from 'url';
new Worker(fileURLToPath(import.meta.url));

To avoid the fileURLToPath call being necessary here, should we consider supporting URLs as input into new Worker?

Activity

  1. added
    workerIssues and PRs related to the worker_threads module and Worker API.
    esmIssues and PRs related to the ECMAScript Modules implementation.
    on Dec 3, 2019
  2. devsnek commented on Dec 3, 2019

    @devsnek
    Member

    IIRC we have prior art on accepting file urls for various APIs, but i don't remember which ones.

  3. addaleax commented on Dec 3, 2019

    @addaleax
    Member

    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 fs module 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 named file:.

    I think a) is not a large concern here.

  4. guybedford commented on Dec 3, 2019

    @guybedford
    ContributorAuthor

    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.

  5. coreyfarrell commented on Dec 5, 2019

    @coreyfarrell
    Member

    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 use new Worker(pathRelativeToCWD)? The documentation only suggests new 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 on process.cwd(), instead new Worker(require.resolve('./other-file.js')) or new 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.

  6. targos commented on Dec 5, 2019

    @targos
    Member

    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.

  7. jasnell commented on Dec 5, 2019

    @jasnell
    Member

    Just having something like the following would work...

    new Worker(new URL(import.meta.url));
  8. Jamesernator commented on Jan 3, 2020

    @Jamesernator

    ^ This approach is how fs accepts URLs, it just accepts URL objects, strings are always treated as paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.feature requestIssues requesting new Node.js features.workerIssues and PRs related to the worker_threads module and Worker API.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions