Skip to content

worker: add addPreload API

Adds a worker_threads.addPreload(module) API to allow injecting preload modules for all new workers without modifying new Worker() calls.

const { Worker, addPreload } = require('worker_threads');

addPreload('./foo.js');

new Worker('console.log(process.execArgv)', { eval: true });

This is currently a draft to solicit feedback on the API. /cc @coreyfarrell @addaleax @gireeshpunathil

This works by injecting the -r {module} into the execArgv and appending to the environment options. This ensures that the modules are inherited.

There are a couple of questions that need answering:

  1. Should injected preload modules run before or after preload modules explicitly listed in execArgv?
  2. Should there be a way of removing preload modules added with addPreload(), and should that have any impact on those added explicitly via execArgv?

Signed-off-by: James M Snell jasnell@gmail.com

Merge request reports

Loading