Skip to content

Worker Threads: Add transferList to Worker constructor options #32250

@sinclairzx81

Description

@sinclairzx81

Is your feature request related to a problem? Please describe.

Based on the current documentation listed here, it doesn't seem possible to specify a transferList on the Worker constructor. This prohibits passing transferList items such as MessagePort. Attempts to pass a MessagePort within the Workers workerData currently results in the following error.

TypeError: MessagePort was found in message but not listed in transferList

Describe the solution you'd like

Given the current error, and if its technically feasible to do so...being able to supply a transferList on the Worker constructor would be helpful in initializing the Worker immediately with a MessagePort rather than sending the MessagePort on a subsequent postMessage(...). Perhaps something like the following could be allowed.

const channel = new MessageChannel()
const workerData = { port: channel.port1 }
const transferList = [channel.port1]
const worker = new Worker('./worker.js', { workerData, transferList  })

Describe alternatives you've considered

It is entirely possible to work around this by sending a MessagePort in a subsequent postMessage(..., transferList). However it doing so involves async initialization of the Worker which is less desirable than initializing immediately on the Worker constructor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.good first issueIssues that are suitable for first-time contributors.workerIssues and PRs related to Worker support.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions