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:
- Should injected preload modules run before or after preload modules explicitly listed in execArgv?
- Should there be a way of removing preload modules added with
addPreload()
, and should that have any impact on those added explicitly viaexecArgv
?
Signed-off-by: James M Snell jasnell@gmail.com