diff --git a/package-lock.json b/package-lock.json index 43f5c358..5b216db5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "workerpool", - "version": "9.1.0", + "version": "9.1.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "workerpool", - "version": "9.1.0", + "version": "9.1.1", "license": "Apache-2.0", "devDependencies": { "@babel/core": "7.23.7", diff --git a/package.json b/package.json index 8b395516..07cea6b5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "workerpool", "license": "Apache-2.0", - "version": "9.1.0", + "version": "9.1.1", "description": "Offload tasks to a pool of workers on node.js and in the browser", "homepage": "https://github.com/josdejong/workerpool", "author": "Jos de Jong (https://github.com/josdejong)", diff --git a/src/Pool.js b/src/Pool.js index 60f3bd90..0df7b9cd 100644 --- a/src/Pool.js +++ b/src/Pool.js @@ -34,6 +34,8 @@ function Pool(script, options) { /** @readonly */ this.workerOpts = Object.freeze(options.workerOpts || {}); /** @readonly */ + this.webWorker = options.webWorker || null; + /** @readonly */ this.workerThreadOpts = Object.freeze(options.workerThreadOpts || {}) /** @private */ this.debugPortStart = (options.debugPortStart || 43210); @@ -430,6 +432,7 @@ Pool.prototype._createWorkerHandler = function () { workerType: this.workerType, workerTerminateTimeout: this.workerTerminateTimeout, emitStdStreams: this.emitStdStreams, + webWorker: this.webWorker }); } diff --git a/src/WorkerHandler.js b/src/WorkerHandler.js index 224c7645..9c8b637f 100644 --- a/src/WorkerHandler.js +++ b/src/WorkerHandler.js @@ -64,7 +64,7 @@ function getDefaultWorker() { function setupWorker(script, options) { if (options.workerType === 'web') { // browser only ensureWebWorker(); - return setupBrowserWorker(script, options.workerOpts, Worker); + return setupBrowserWorker(script, options.workerOpts, Worker, options.webWorker); } else if (options.workerType === 'thread') { // node.js only WorkerThreads = ensureWorkerThreads(); return setupWorkerThreadWorker(script, WorkerThreads, options); @@ -73,7 +73,7 @@ function setupWorker(script, options) { } else { // options.workerType === 'auto' or undefined if (environment.platform === 'browser') { ensureWebWorker(); - return setupBrowserWorker(script, options.workerOpts, Worker); + return setupBrowserWorker(script, options.workerOpts, Worker, options.webWorker); } else { // environment.platform === 'node' var WorkerThreads = tryRequireWorkerThreads(); @@ -86,12 +86,11 @@ function setupWorker(script, options) { } } -function setupBrowserWorker(script, workerOpts, Worker) { +function setupBrowserWorker(script, workerOpts, Worker, existingWorker) { // validate the options right before creating the worker (not when creating the pool) validateOptions(workerOpts, workerOptsNames, 'workerOpts') - // create the web worker - var worker = new Worker(script, workerOpts); + var worker = existingWorker || new Worker(script, workerOpts); worker.isBrowserWorker = true; // add node.js API to the web worker diff --git a/src/types.js b/src/types.js index d07658fc..355fd878 100644 --- a/src/types.js +++ b/src/types.js @@ -21,6 +21,7 @@ * @property {string[]} [forkArgs] For `process` worker type. An array passed as `args` to [child_process.fork](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options) * @property {import('child_process').ForkOptions} [forkOpts] For `process` worker type. An object passed as `options` to [child_process.fork](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options). * @property {WorkerOptions} [workerOpts] For `web` worker type. An object passed to the [constructor of the web worker](https://html.spec.whatwg.org/multipage/workers.html#dom-worker). See [WorkerOptions specification](https://html.spec.whatwg.org/multipage/workers.html#workeroptions) for available options. + * @property {Worker} [webWorker] For `web` worker type. An allocated `Worker` object. * @property {import('worker_threads').WorkerOptions} [workerThreadOpts] Object`. For `worker` worker type. An object passed to [worker_threads.options](https://nodejs.org/api/worker_threads.html#new-workerfilename-options). * @property {boolean} [emitStdStreams] Capture stdout and stderr from the worker and emit them via the `stdout` and `stderr` events. Not supported by the `web` worker type. * @property { (arg: WorkerArg) => WorkerArg | undefined } [onCreateWorker] A callback that is called whenever a worker is being created. It can be used to allocate resources for each worker for example. Optionally, this callback can return an object containing one or more of the `WorkerArg` properties. The provided properties will be used to override the Pool properties for the worker being created.