new Worker(filename[, options])
filename<string> | <URL> 工作线程主脚本或模块的路径。 必须是以./或../开头的绝对路径或相对路径(即相对于当前工作目录)、或者是使用file:或data:协议的 WHATWGURL对象 当使用data:网址时,使用 ECMAScript 模块加载器根据 MIME 类型解释数据。 如果options.eval是true,则这是包含 JavaScript 代码(而不是路径)的字符串。options<Object>argv<any[]> 将被字符串化并附加到工作线程中的process.argv的参数列表。 这与workerData非常相似,但这些值将在全局process.argv上可用,就像它们作为命令行选项传给脚本一样。env<Object> 如果设置,则指定工作线程内process.env的初始值。 作为特殊值,worker.SHARE_ENV可用于指定父线程和子线程应该共享它们的环境变量;在这种情况下,对一个线程的process.env对象的更改也会影响另一个线程。 默认值:process.env。eval<boolean> 如果true并且第一个参数是string,则将构造函数的第一个参数解释为一旦工作线程在线就执行的脚本。execArgv<string[]> 传给工作线程的 node CLI 选项的列表。 不支持 V8 选项(如--max-old-space-size)和影响进程的选项(如--title)。 如果设置,则这将在工作线程内部提供为process.execArgv。 默认情况下,选项将从父线程继承。stdin<boolean> 如果设置为true,则worker.stdin将提供其内容将在工作线程中显示为process.stdin的可写流。 默认情况下,不提供任何数据。stdout<boolean> 如果设置为true,则worker.stdout将不会自动通过管道传输到父线程中的process.stdout。stderr<boolean> 如果设置为true,则worker.stderr将不会自动通过管道传输到父线程中的process.stderr。workerData<any> 任何将被克隆并作为require('worker_threads').workerData可用的 JavaScript 值。 克隆将按照 HTML 结构化克隆算法中的描述进行,如果无法克隆对象(例如,因为它包含function),则会抛出错误。trackUnmanagedFds<boolean> 如果设置为true,则工作线程将跟踪通过fs.open()和fs.close()管理的原始文件描述符,并在工作线程退出时关闭它们,类似于网络套接字或通过FileHandleAPI 管理的文件描述符等其他资源。 此选项会被所有嵌套的Worker自动继承。 默认值:false.transferList<Object[]> 如果在workerData中传入了一个或多个类似MessagePort的对象,则这些条目需要transferList,否则将抛出ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST。 有关详细信息,请参阅port.postMessage()。resourceLimits<Object> 新的 JS 引擎实例的一组可选资源限制。 达到这些限制将导致Worker实例终止。 这些限制只影响 JS 引擎,不影响外部数据,不包括ArrayBuffer。 即使设置了这些限制,如果遇到全局内存不足的情况,进程仍可能会中止。
filename<string> | <URL> The path to the Worker’s main script or module. Must be either an absolute path or a relative path (i.e. relative to the current working directory) starting with./or../, or a WHATWGURLobject usingfile:ordata:protocol. When using adata:URL, the data is interpreted based on MIME type using the ECMAScript module loader. Ifoptions.evalistrue, this is a string containing JavaScript code rather than a path.options<Object>argv<any[]> List of arguments which would be stringified and appended toprocess.argvin the worker. This is mostly similar to theworkerDatabut the values will be available on the globalprocess.argvas if they were passed as CLI options to the script.env<Object> If set, specifies the initial value ofprocess.envinside the Worker thread. As a special value,worker.SHARE_ENVmay be used to specify that the parent thread and the child thread should share their environment variables; in that case, changes to one thread’sprocess.envobject will affect the other thread as well. Default:process.env.eval<boolean> Iftrueand the first argument is astring, interpret the first argument to the constructor as a script that is executed once the worker is online.execArgv<string[]> List of node CLI options passed to the worker. V8 options (such as--max-old-space-size) and options that affect the process (such as--title) are not supported. If set, this will be provided asprocess.execArgvinside the worker. By default, options will be inherited from the parent thread.stdin<boolean> If this is set totrue, thenworker.stdinwill provide a writable stream whose contents will appear asprocess.stdininside the Worker. By default, no data is provided.stdout<boolean> If this is set totrue, thenworker.stdoutwill not automatically be piped through toprocess.stdoutin the parent.stderr<boolean> If this is set totrue, thenworker.stderrwill not automatically be piped through toprocess.stderrin the parent.workerData<any> Any JavaScript value that will be cloned and made available asrequire('worker_threads').workerData. The cloning will occur as described in the HTML structured clone algorithm, and an error will be thrown if the object cannot be cloned (e.g. because it containsfunctions).trackUnmanagedFds<boolean> If this is set totrue, then the Worker will track raw file descriptors managed throughfs.open()andfs.close(), and close them when the Worker exits, similar to other resources like network sockets or file descriptors managed through theFileHandleAPI. This option is automatically inherited by all nestedWorkers. Default:false.transferList<Object[]> If one or moreMessagePort-like objects are passed inworkerData, atransferListis required for those items orERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LISTwill be thrown. Seeport.postMessage()for more information.resourceLimits<Object> An optional set of resource limits for the new JS engine instance. Reaching these limits will lead to termination of theWorkerinstance. These limits only affect the JS engine, and no external data, including noArrayBuffers. Even if these limits are set, the process may still abort if it encounters a global out-of-memory situation.maxOldGenerationSizeMb<number> The maximum size of the main heap in MB.maxYoungGenerationSizeMb<number> The maximum size of a heap space for recently created objects.codeRangeSizeMb<number> The size of a pre-allocated memory range used for generated code.stackSizeMb<number> The default maximum stack size for the thread. Small values may lead to unusable Worker instances. Default:4.