Socket
Socket
Sign inDemoInstall

workerpool

Package Overview
Dependencies
0
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 5.0.0 to 5.0.1

4

dist/worker.js

@@ -7,4 +7,4 @@ /**

*
* @version 5.0.0
* @date 2019-08-25
* @version 5.0.1
* @date 2019-10-13
*

@@ -11,0 +11,0 @@ * @license

@@ -7,4 +7,4 @@ /**

*
* @version 5.0.0
* @date 2019-08-25
* @version 5.0.1
* @date 2019-10-13
*

@@ -793,2 +793,8 @@ * @license

Pool.prototype.terminate = function (force, timeout) {
// cancel any pending tasks
this.tasks.forEach(function (task) {
task.resolver.reject(new Error('Pool terminated'));
});
this.tasks.length = 0;
var f = function (worker) {

@@ -809,8 +815,9 @@ this._removeWorkerFromList(worker);

// DEPRECATED
/**
* Close all active workers. Unlike terminate, this function does not return a promise.
* @param force
* @deprecated
*/
Pool.prototype.clear = function (force) {
console.warn('Pool.clear() is deprecated. Use Pool.terminate() instead.');
this.terminate(force);

@@ -817,0 +824,0 @@ };

@@ -7,4 +7,4 @@ /**

*
* @version 5.0.0
* @date 2019-08-25
* @version 5.0.1
* @date 2019-10-13
*

@@ -26,3 +26,3 @@ * @license

*/
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define("workerpool",[],r):"object"==typeof exports?exports.workerpool=r():e.workerpool=r()}("undefined"!=typeof self?self:this,function(){return d=[function(e,r,t){var o=t(2),n="undefined"!=typeof process&&null!=typeof process.versions&&null!=process.versions.node;e.exports.platform=n?"node":"browser",e.exports.isMainThread=n?!process.connected:"undefined"!=typeof Window,e.exports.cpus="browser"===e.exports.platform?self.navigator.hardwareConcurrency:o("os").cpus().length},function(e,r,t){"use strict";function a(e,t){var o=this;if(!(this instanceof a))throw new SyntaxError("Constructor must be called with the new operator");if("function"!=typeof e)throw new SyntaxError("Function parameter handler(resolve, reject) missing");var n=[],i=[];this.resolved=!1,this.rejected=!1,this.pending=!0;var s=function(e,r){n.push(e),i.push(r)};this.then=function(n,i){return new a(function(e,r){var t=n?c(n,e,r):e,o=i?c(i,e,r):r;s(t,o)},o)};var r=function(t){return o.resolved=!0,o.rejected=!1,o.pending=!1,n.forEach(function(e){e(t)}),s=function(e,r){e(t)},r=u=function(){},o},u=function(t){return o.resolved=!1,o.rejected=!0,o.pending=!1,i.forEach(function(e){e(t)}),s=function(e,r){r(t)},r=u=function(){},o};this.cancel=function(){return t?t.cancel():u(new f),o},this.timeout=function(e){if(t)t.timeout(e);else{var r=setTimeout(function(){u(new p("Promise timed out after "+e+" ms"))},e);o.always(function(){clearTimeout(r)})}return o},e(function(e){r(e)},function(e){u(e)})}function c(t,o,n){return function(e){try{var r=t(e);r&&"function"==typeof r.then&&"function"==typeof r.catch?r.then(o,n):o(r)}catch(e){n(e)}}}function f(e){this.message=e||"promise cancelled",this.stack=(new Error).stack}function p(e){this.message=e||"timeout exceeded",this.stack=(new Error).stack}a.prototype.catch=function(e){return this.then(null,e)},a.prototype.always=function(e){return this.then(e,e)},a.all=function(e){return new a(function(t,o){var n=e.length,i=[];n?e.forEach(function(e,r){e.then(function(e){i[r]=e,0==--n&&t(i)},function(e){n=0,o(e)})}):t(i)})},a.defer=function(){var t={};return t.promise=new a(function(e,r){t.resolve=e,t.reject=r}),t},(f.prototype=new Error).constructor=Error,f.prototype.name="CancellationError",a.CancellationError=f,(p.prototype=new Error).constructor=Error,p.prototype.name="TimeoutError",a.TimeoutError=p,e.exports=a},function(module,exports){var requireFoolWebpack=eval("typeof require !== 'undefined' ? require : function (module) { throw new Error('Module \" + module + \" not found.') }");module.exports=requireFoolWebpack},function(e,r,t){var o=t(0);r.pool=function(e,r){return new(t(4))(e,r)},r.worker=function(e){var r=t(8);r.add(e)},r.Promise=t(1),r.platform=o.platform,r.isMainThread=o.isMainThread,r.cpus=o.cpus},function(e,r,t){var s=t(1),o=t(5),n=t(0),i=new(t(7));function u(e,r){"string"==typeof e?this.script=e||null:(this.script=null,r=e),this.workers=[],this.tasks=[],(r=r||{}).nodeWorker&&console.warn('WARNING: Option "nodeWorker" is deprecated since workerpool@5.0.0. Please use "workerType" instead.'),this.forkArgs=r.forkArgs||[],this.forkOpts=r.forkOpts||{},this.debugPortStart=r.debugPortStart||43210,this.nodeWorker=r.nodeWorker,this.workerType=r.workerType||r.nodeWorker||"auto",this.maxQueueSize=r.maxQueueSize||1/0,r&&"maxWorkers"in r?(function(e){if(!a(e)||!c(e)||e<1)throw new TypeError("Option maxWorkers must be an integer number >= 1")}(r.maxWorkers),this.maxWorkers=r.maxWorkers):this.maxWorkers=Math.max((n.cpus||4)-1,1),r&&"minWorkers"in r&&("max"===r.minWorkers?this.minWorkers=this.maxWorkers:(function(e){if(!a(e)||!c(e)||e<0)throw new TypeError("Option minWorkers must be an integer number >= 0")}(r.minWorkers),this.minWorkers=r.minWorkers,this.maxWorkers=Math.max(this.minWorkers,this.maxWorkers)),this._ensureMinWorkers()),this._boundNext=this._next.bind(this),"thread"===this.workerType&&o.ensureWorkerThreads()}function a(e){return"number"==typeof e}function c(e){return Math.round(e)==e}u.prototype.exec=function(e,r){if(r&&!Array.isArray(r))throw new TypeError('Array expected as argument "params"');if("string"==typeof e){var t=s.defer();if(this.tasks.length>=this.maxQueueSize)throw new Error("Max queue size of "+this.maxQueueSize+" reached");var o=this.tasks,n={method:e,params:r,resolver:t,timeout:null};o.push(n);var i=t.promise.timeout;return t.promise.timeout=function(e){return-1!==o.indexOf(n)?(n.timeout=e,t.promise):i.call(t.promise,e)},this._next(),t.promise}if("function"==typeof e)return this.exec("run",[String(e),r]);throw new TypeError('Function or string expected as argument "method"')},u.prototype.proxy=function(){if(0<arguments.length)throw new Error("No arguments expected");var t=this;return this.exec("methods").then(function(e){var r={};return e.forEach(function(e){r[e]=function(){return t.exec(e,Array.prototype.slice.call(arguments))}}),r})},u.prototype._next=function(){if(0<this.tasks.length){var e=this._getWorker();if(e){var r=this,t=this.tasks.shift();if(t.resolver.promise.pending){var o=e.exec(t.method,t.params,t.resolver).then(r._boundNext).catch(function(){e.terminated&&(r._removeWorker(e),r._ensureMinWorkers()),r._next()});"number"==typeof t.timeout&&o.timeout(t.timeout)}else r._next()}}},u.prototype._getWorker=function(){for(var e=this.workers,r=0;r<e.length;r++){var t=e[r];if(!1===t.busy())return t}return e.length<this.maxWorkers?(t=this._createWorkerHandler(),e.push(t),t):null},u.prototype._removeWorker=function(e){i.releasePort(e.debugPort),e.terminate(),this._removeWorkerFromList(e)},u.prototype._removeWorkerFromList=function(e){var r=this.workers.indexOf(e);-1!==r&&this.workers.splice(r,1)},u.prototype.terminate=function(t,o){var n=function(e){this._removeWorkerFromList(e)}.bind(this),i=[];return this.workers.slice().forEach(function(e){var r=e.terminateAndNotify(t,o).then(n);i.push(r)}),s.all(i)},u.prototype.clear=function(e){this.terminate(e)},u.prototype.stats=function(){var e=this.workers.length,r=this.workers.filter(function(e){return e.busy()}).length;return{totalWorkers:e,busyWorkers:r,idleWorkers:e-r,pendingTasks:this.tasks.length,activeTasks:r}},u.prototype._ensureMinWorkers=function(){if(this.minWorkers)for(var e=this.workers.length;e<this.minWorkers;e++)this.workers.push(this._createWorkerHandler())},u.prototype._createWorkerHandler=function(){return new o(this.script,{forkArgs:this.forkArgs,forkOpts:this.forkOpts,debugPort:i.nextAvailableStartingAt(this.debugPortStart),workerType:this.workerType})},e.exports=u},function(e,r,s){"use strict";var u=s(1),a=s(0),c=s(2);function f(){var e=d();if(!e)throw new Error("WorkerPool: workerType = 'thread' is not supported, Node >= 11.7.0 required");return e}function p(){if("function"!=typeof Worker&&("object"!=typeof Worker||"function"!=typeof Worker.prototype.constructor))throw new Error("WorkerPool: Web Workers not supported")}function d(){try{return c("worker_threads")}catch(e){if("object"==typeof e&&null!==e&&"MODULE_NOT_FOUND"===e.code)return null;throw e}}function h(e,r){var t=new r(e);return t.isBrowserWorker=!0,t.on=function(e,r){this.addEventListener(e,function(e){r(e.data)})},t.send=function(e){this.postMessage(e)},t}function l(e,r){var t=new r.Worker(e,{stdout:!1,stderr:!1});return t.isWorkerThread=!0,t.send=function(e){this.postMessage(e)},t.kill=function(){this.terminate()},t.disconnect=function(){this.terminate()},t}function k(e,r,t){var o=t.fork(e,r.forkArgs,r.forkOpts);return o.isChildProcess=!0,o}function m(e){e=e||{};var r=process.execArgv.join(" "),t=-1!==r.indexOf("--inspect"),o=-1!==r.indexOf("--debug-brk"),n=[];return t&&(n.push("--inspect="+e.debugPort),o&&n.push("--debug-brk")),process.execArgv.forEach(function(e){-1<e.indexOf("--max-old-space-size")&&n.push(e)}),Object.assign({},e,{forkArgs:e.forkArgs,forkOpts:Object.assign({},e.forkOpts,{execArgv:(e.forkOpts&&e.forkOpts.execArgv||[]).concat(n)})})}function t(e,r){var o=this,t=r||{};function n(e){for(var r in o.terminated=!0,o.terminating&&o.terminationHandler&&o.terminationHandler(o),o.terminating=!1,o.processing)void 0!==o.processing[r]&&o.processing[r].resolver.reject(e);o.processing=Object.create(null)}this.script=e||function(){if("browser"!==a.platform)return __dirname+"/worker.js";if("undefined"==typeof Blob)throw new Error("Blob not supported by the browser");if(!window.URL||"function"!=typeof window.URL.createObjectURL)throw new Error("URL.createObjectURL not supported by the browser");var e=new Blob([s(6)],{type:"text/javascript"});return window.URL.createObjectURL(e)}(),this.worker=function(e,r){if("web"===r.workerType)return p(),h(e,Worker);if("thread"===r.workerType)return l(e,t=f());if("process"!==r.workerType&&r.workerType){if("browser"===a.platform)return p(),h(e,Worker);var t=d();return t?l(e,t):k(e,m(r),c("child_process"))}return k(e,m(r),c("child_process"))}(this.script,t),this.debugPort=t.debugPort,e||(this.worker.ready=!0),this.requestQueue=[],this.worker.on("message",function(e){if("string"==typeof e&&"ready"===e)o.worker.ready=!0,o.requestQueue.forEach(o.worker.send.bind(o.worker)),o.requestQueue=[];else{var r=e.id,t=o.processing[r];void 0!==t&&(delete o.processing[r],!0===o.terminating&&o.terminate(),e.error?t.resolver.reject(function(e){for(var r=new Error(""),t=Object.keys(e),o=0;o<t.length;o++)r[t[o]]=e[t[o]];return r}(e.error)):t.resolver.resolve(e.result))}});var i=this.worker;this.worker.on("error",n),this.worker.on("exit",function(e,r){var t="Workerpool Worker terminated Unexpectedly\n";t+=" exitCode: `"+e+"`\n",t+=" signalCode: `"+r+"`\n",t+=" workerpool.script: `"+o.script+"`\n",t+=" spawnArgs: `"+i.spawnargs+"`\n",t+=" spawnfile: `"+i.spawnfile+"`\n",t+=" stdout: `"+i.stdout+"`\n",t+=" stderr: `"+i.stderr+"`\n",n(new Error(t))}),this.processing=Object.create(null),this.terminating=!1,this.terminated=!1,this.terminationHandler=null,this.lastId=0}t.prototype.methods=function(){return this.exec("methods")},t.prototype.exec=function(e,r,t){t=t||u.defer();var o=++this.lastId;this.processing[o]={id:o,resolver:t};var n={id:o,method:e,params:r};this.terminated?t.reject(new Error("Worker is terminated")):this.worker.ready?this.worker.send(n):this.requestQueue.push(n);var i=this;return t.promise.catch(function(e){if(!(e instanceof u.CancellationError||e instanceof u.TimeoutError))throw e;delete i.processing[o],i.terminate(!0)}),t.promise},t.prototype.busy=function(){return 0<Object.keys(this.processing).length},t.prototype.terminate=function(e,r){if(e){for(var t in this.processing)void 0!==this.processing[t]&&this.processing[t].resolver.reject(new Error("Worker terminated"));this.processing=Object.create(null)}if("function"==typeof r&&(this.terminationHandler=r),this.busy())this.terminating=!0;else{if(this.worker){if("function"==typeof this.worker.kill)this.worker.kill();else{if("function"!=typeof this.worker.terminate)throw new Error("Failed to terminate worker");this.worker.terminate()}this.worker=null}this.terminating=!1,this.terminated=!0,this.terminationHandler&&this.terminationHandler(this)}},t.prototype.terminateAndNotify=function(e,r){var t=u.defer();return r&&(t.promise.timeout=r),this.terminate(e,function(e){t.resolve(e)}),t.promise},e.exports=t,e.exports._tryRequireWorkerThreads=d,e.exports._setupProcessWorker=k,e.exports._setupBrowserWorker=h,e.exports._setupWorkerThreadWorker=l,e.exports.ensureWorkerThreads=f},function(e,r){e.exports='!function(o){var n={};function t(e){if(n[e])return n[e].exports;var r=n[e]={i:e,l:!1,exports:{}};return o[e].call(r.exports,r,r.exports,t),r.l=!0,r.exports}t.m=o,t.c=n,t.d=function(e,r,o){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:o})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(r,e){if(1&e&&(r=t(r)),8&e)return r;if(4&e&&"object"==typeof r&&r&&r.__esModule)return r;var o=Object.create(null);if(t.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:r}),2&e&&"string"!=typeof r)for(var n in r)t.d(o,n,function(e){return r[e]}.bind(null,n));return o},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="",t(t.s=0)}([function(module,exports,__webpack_require__){var requireFoolWebpack=eval("typeof require !== \'undefined\' ? require : function (module) { throw new Error(\'Module \\" + module + \\" not found.\') }"),worker={};if("undefined"!=typeof self&&"function"==typeof postMessage&&"function"==typeof addEventListener)worker.on=function(e,r){addEventListener(e,function(e){r(e.data)})},worker.send=function(e){postMessage(e)};else{if("undefined"==typeof process)throw new Error("Script must be executed as a worker");var WorkerThreads;try{WorkerThreads=requireFoolWebpack("worker_threads")}catch(e){if("object"!=typeof e||null===e||"MODULE_NOT_FOUND"!=e.code)throw e}if(WorkerThreads&&null!==WorkerThreads.parentPort){var parentPort=WorkerThreads.parentPort;worker.send=parentPort.postMessage.bind(parentPort),worker.on=parentPort.on.bind(parentPort)}else worker.on=process.on.bind(process),worker.send=process.send.bind(process),worker.on("disconnect",function(){process.exit(1)})}function convertError(o){return Object.getOwnPropertyNames(o).reduce(function(e,r){return Object.defineProperty(e,r,{value:o[r],enumerable:!0})},{})}function isPromise(e){return e&&"function"==typeof e.then&&"function"==typeof e.catch}worker.methods={},worker.methods.run=function run(fn,args){var f=eval("("+fn+")");return f.apply(f,args)},worker.methods.methods=function(){return Object.keys(worker.methods)},worker.on("message",function(r){try{var e=worker.methods[r.method];if(!e)throw new Error(\'Unknown method "\'+r.method+\'"\');var o=e.apply(e,r.params);isPromise(o)?o.then(function(e){worker.send({id:r.id,result:e,error:null})}).catch(function(e){worker.send({id:r.id,result:null,error:convertError(e)})}):worker.send({id:r.id,result:o,error:null})}catch(e){worker.send({id:r.id,result:null,error:convertError(e)})}}),worker.register=function(e){if(e)for(var r in e)e.hasOwnProperty(r)&&(worker.methods[r]=e[r]);worker.send("ready")},exports.add=worker.register}]);'},function(e,r,t){"use strict";function o(){this.ports=Object.create(null),this.length=0}(e.exports=o).prototype.nextAvailableStartingAt=function(e){for(;!0===this.ports[e];)e++;if(65535<=e)throw new Error("WorkerPool debug port limit reached: "+e+">= 65535");return this.ports[e]=!0,this.length++,e},o.prototype.releasePort=function(e){delete this.ports[e],this.length--}},function(module,exports,__webpack_require__){var requireFoolWebpack=eval("typeof require !== 'undefined' ? require : function (module) { throw new Error('Module \" + module + \" not found.') }"),worker={};if("undefined"!=typeof self&&"function"==typeof postMessage&&"function"==typeof addEventListener)worker.on=function(e,r){addEventListener(e,function(e){r(e.data)})},worker.send=function(e){postMessage(e)};else{if("undefined"==typeof process)throw new Error("Script must be executed as a worker");var WorkerThreads;try{WorkerThreads=requireFoolWebpack("worker_threads")}catch(e){if("object"!=typeof e||null===e||"MODULE_NOT_FOUND"!=e.code)throw e}if(WorkerThreads&&null!==WorkerThreads.parentPort){var parentPort=WorkerThreads.parentPort;worker.send=parentPort.postMessage.bind(parentPort),worker.on=parentPort.on.bind(parentPort)}else worker.on=process.on.bind(process),worker.send=process.send.bind(process),worker.on("disconnect",function(){process.exit(1)})}function convertError(t){return Object.getOwnPropertyNames(t).reduce(function(e,r){return Object.defineProperty(e,r,{value:t[r],enumerable:!0})},{})}function isPromise(e){return e&&"function"==typeof e.then&&"function"==typeof e.catch}worker.methods={},worker.methods.run=function run(fn,args){var f=eval("("+fn+")");return f.apply(f,args)},worker.methods.methods=function(){return Object.keys(worker.methods)},worker.on("message",function(r){try{var e=worker.methods[r.method];if(!e)throw new Error('Unknown method "'+r.method+'"');var t=e.apply(e,r.params);isPromise(t)?t.then(function(e){worker.send({id:r.id,result:e,error:null})}).catch(function(e){worker.send({id:r.id,result:null,error:convertError(e)})}):worker.send({id:r.id,result:t,error:null})}catch(e){worker.send({id:r.id,result:null,error:convertError(e)})}}),worker.register=function(e){if(e)for(var r in e)e.hasOwnProperty(r)&&(worker.methods[r]=e[r]);worker.send("ready")},exports.add=worker.register}],e={},g.m=d,g.c=e,g.d=function(e,r,t){g.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},g.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},g.t=function(r,e){if(1&e&&(r=g(r)),8&e)return r;if(4&e&&"object"==typeof r&&r&&r.__esModule)return r;var t=Object.create(null);if(g.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:r}),2&e&&"string"!=typeof r)for(var o in r)g.d(t,o,function(e){return r[e]}.bind(null,o));return t},g.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return g.d(r,"a",r),r},g.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},g.p="",g(g.s=3);function g(r){if(e[r])return e[r].exports;var t=e[r]={i:r,l:!1,exports:{}};return d[r].call(t.exports,t,t.exports,g),t.l=!0,t.exports}var d,e});
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define("workerpool",[],r):"object"==typeof exports?exports.workerpool=r():e.workerpool=r()}("undefined"!=typeof self?self:this,function(){return d=[function(e,r,t){var o=t(2),n="undefined"!=typeof process&&null!=typeof process.versions&&null!=process.versions.node;e.exports.platform=n?"node":"browser",e.exports.isMainThread=n?!process.connected:"undefined"!=typeof Window,e.exports.cpus="browser"===e.exports.platform?self.navigator.hardwareConcurrency:o("os").cpus().length},function(e,r,t){"use strict";function a(e,t){var o=this;if(!(this instanceof a))throw new SyntaxError("Constructor must be called with the new operator");if("function"!=typeof e)throw new SyntaxError("Function parameter handler(resolve, reject) missing");var n=[],i=[];this.resolved=!1,this.rejected=!1,this.pending=!0;var s=function(e,r){n.push(e),i.push(r)};this.then=function(n,i){return new a(function(e,r){var t=n?c(n,e,r):e,o=i?c(i,e,r):r;s(t,o)},o)};var r=function(t){return o.resolved=!0,o.rejected=!1,o.pending=!1,n.forEach(function(e){e(t)}),s=function(e,r){e(t)},r=u=function(){},o},u=function(t){return o.resolved=!1,o.rejected=!0,o.pending=!1,i.forEach(function(e){e(t)}),s=function(e,r){r(t)},r=u=function(){},o};this.cancel=function(){return t?t.cancel():u(new f),o},this.timeout=function(e){if(t)t.timeout(e);else{var r=setTimeout(function(){u(new p("Promise timed out after "+e+" ms"))},e);o.always(function(){clearTimeout(r)})}return o},e(function(e){r(e)},function(e){u(e)})}function c(t,o,n){return function(e){try{var r=t(e);r&&"function"==typeof r.then&&"function"==typeof r.catch?r.then(o,n):o(r)}catch(e){n(e)}}}function f(e){this.message=e||"promise cancelled",this.stack=(new Error).stack}function p(e){this.message=e||"timeout exceeded",this.stack=(new Error).stack}a.prototype.catch=function(e){return this.then(null,e)},a.prototype.always=function(e){return this.then(e,e)},a.all=function(e){return new a(function(t,o){var n=e.length,i=[];n?e.forEach(function(e,r){e.then(function(e){i[r]=e,0==--n&&t(i)},function(e){n=0,o(e)})}):t(i)})},a.defer=function(){var t={};return t.promise=new a(function(e,r){t.resolve=e,t.reject=r}),t},(f.prototype=new Error).constructor=Error,f.prototype.name="CancellationError",a.CancellationError=f,(p.prototype=new Error).constructor=Error,p.prototype.name="TimeoutError",a.TimeoutError=p,e.exports=a},function(module,exports){var requireFoolWebpack=eval("typeof require !== 'undefined' ? require : function (module) { throw new Error('Module \" + module + \" not found.') }");module.exports=requireFoolWebpack},function(e,r,t){var o=t(0);r.pool=function(e,r){return new(t(4))(e,r)},r.worker=function(e){var r=t(8);r.add(e)},r.Promise=t(1),r.platform=o.platform,r.isMainThread=o.isMainThread,r.cpus=o.cpus},function(e,r,t){var s=t(1),o=t(5),n=t(0),i=new(t(7));function u(e,r){"string"==typeof e?this.script=e||null:(this.script=null,r=e),this.workers=[],this.tasks=[],(r=r||{}).nodeWorker&&console.warn('WARNING: Option "nodeWorker" is deprecated since workerpool@5.0.0. Please use "workerType" instead.'),this.forkArgs=r.forkArgs||[],this.forkOpts=r.forkOpts||{},this.debugPortStart=r.debugPortStart||43210,this.nodeWorker=r.nodeWorker,this.workerType=r.workerType||r.nodeWorker||"auto",this.maxQueueSize=r.maxQueueSize||1/0,r&&"maxWorkers"in r?(function(e){if(!a(e)||!c(e)||e<1)throw new TypeError("Option maxWorkers must be an integer number >= 1")}(r.maxWorkers),this.maxWorkers=r.maxWorkers):this.maxWorkers=Math.max((n.cpus||4)-1,1),r&&"minWorkers"in r&&("max"===r.minWorkers?this.minWorkers=this.maxWorkers:(function(e){if(!a(e)||!c(e)||e<0)throw new TypeError("Option minWorkers must be an integer number >= 0")}(r.minWorkers),this.minWorkers=r.minWorkers,this.maxWorkers=Math.max(this.minWorkers,this.maxWorkers)),this._ensureMinWorkers()),this._boundNext=this._next.bind(this),"thread"===this.workerType&&o.ensureWorkerThreads()}function a(e){return"number"==typeof e}function c(e){return Math.round(e)==e}u.prototype.exec=function(e,r){if(r&&!Array.isArray(r))throw new TypeError('Array expected as argument "params"');if("string"==typeof e){var t=s.defer();if(this.tasks.length>=this.maxQueueSize)throw new Error("Max queue size of "+this.maxQueueSize+" reached");var o=this.tasks,n={method:e,params:r,resolver:t,timeout:null};o.push(n);var i=t.promise.timeout;return t.promise.timeout=function(e){return-1!==o.indexOf(n)?(n.timeout=e,t.promise):i.call(t.promise,e)},this._next(),t.promise}if("function"==typeof e)return this.exec("run",[String(e),r]);throw new TypeError('Function or string expected as argument "method"')},u.prototype.proxy=function(){if(0<arguments.length)throw new Error("No arguments expected");var t=this;return this.exec("methods").then(function(e){var r={};return e.forEach(function(e){r[e]=function(){return t.exec(e,Array.prototype.slice.call(arguments))}}),r})},u.prototype._next=function(){if(0<this.tasks.length){var e=this._getWorker();if(e){var r=this,t=this.tasks.shift();if(t.resolver.promise.pending){var o=e.exec(t.method,t.params,t.resolver).then(r._boundNext).catch(function(){e.terminated&&(r._removeWorker(e),r._ensureMinWorkers()),r._next()});"number"==typeof t.timeout&&o.timeout(t.timeout)}else r._next()}}},u.prototype._getWorker=function(){for(var e=this.workers,r=0;r<e.length;r++){var t=e[r];if(!1===t.busy())return t}return e.length<this.maxWorkers?(t=this._createWorkerHandler(),e.push(t),t):null},u.prototype._removeWorker=function(e){i.releasePort(e.debugPort),e.terminate(),this._removeWorkerFromList(e)},u.prototype._removeWorkerFromList=function(e){var r=this.workers.indexOf(e);-1!==r&&this.workers.splice(r,1)},u.prototype.terminate=function(t,o){this.tasks.forEach(function(e){e.resolver.reject(new Error("Pool terminated"))}),this.tasks.length=0;var n=function(e){this._removeWorkerFromList(e)}.bind(this),i=[];return this.workers.slice().forEach(function(e){var r=e.terminateAndNotify(t,o).then(n);i.push(r)}),s.all(i)},u.prototype.clear=function(e){console.warn("Pool.clear() is deprecated. Use Pool.terminate() instead."),this.terminate(e)},u.prototype.stats=function(){var e=this.workers.length,r=this.workers.filter(function(e){return e.busy()}).length;return{totalWorkers:e,busyWorkers:r,idleWorkers:e-r,pendingTasks:this.tasks.length,activeTasks:r}},u.prototype._ensureMinWorkers=function(){if(this.minWorkers)for(var e=this.workers.length;e<this.minWorkers;e++)this.workers.push(this._createWorkerHandler())},u.prototype._createWorkerHandler=function(){return new o(this.script,{forkArgs:this.forkArgs,forkOpts:this.forkOpts,debugPort:i.nextAvailableStartingAt(this.debugPortStart),workerType:this.workerType})},e.exports=u},function(e,r,s){"use strict";var u=s(1),a=s(0),c=s(2);function f(){var e=d();if(!e)throw new Error("WorkerPool: workerType = 'thread' is not supported, Node >= 11.7.0 required");return e}function p(){if("function"!=typeof Worker&&("object"!=typeof Worker||"function"!=typeof Worker.prototype.constructor))throw new Error("WorkerPool: Web Workers not supported")}function d(){try{return c("worker_threads")}catch(e){if("object"==typeof e&&null!==e&&"MODULE_NOT_FOUND"===e.code)return null;throw e}}function h(e,r){var t=new r(e);return t.isBrowserWorker=!0,t.on=function(e,r){this.addEventListener(e,function(e){r(e.data)})},t.send=function(e){this.postMessage(e)},t}function l(e,r){var t=new r.Worker(e,{stdout:!1,stderr:!1});return t.isWorkerThread=!0,t.send=function(e){this.postMessage(e)},t.kill=function(){this.terminate()},t.disconnect=function(){this.terminate()},t}function k(e,r,t){var o=t.fork(e,r.forkArgs,r.forkOpts);return o.isChildProcess=!0,o}function m(e){e=e||{};var r=process.execArgv.join(" "),t=-1!==r.indexOf("--inspect"),o=-1!==r.indexOf("--debug-brk"),n=[];return t&&(n.push("--inspect="+e.debugPort),o&&n.push("--debug-brk")),process.execArgv.forEach(function(e){-1<e.indexOf("--max-old-space-size")&&n.push(e)}),Object.assign({},e,{forkArgs:e.forkArgs,forkOpts:Object.assign({},e.forkOpts,{execArgv:(e.forkOpts&&e.forkOpts.execArgv||[]).concat(n)})})}function t(e,r){var o=this,t=r||{};function n(e){for(var r in o.terminated=!0,o.terminating&&o.terminationHandler&&o.terminationHandler(o),o.terminating=!1,o.processing)void 0!==o.processing[r]&&o.processing[r].resolver.reject(e);o.processing=Object.create(null)}this.script=e||function(){if("browser"!==a.platform)return __dirname+"/worker.js";if("undefined"==typeof Blob)throw new Error("Blob not supported by the browser");if(!window.URL||"function"!=typeof window.URL.createObjectURL)throw new Error("URL.createObjectURL not supported by the browser");var e=new Blob([s(6)],{type:"text/javascript"});return window.URL.createObjectURL(e)}(),this.worker=function(e,r){if("web"===r.workerType)return p(),h(e,Worker);if("thread"===r.workerType)return l(e,t=f());if("process"!==r.workerType&&r.workerType){if("browser"===a.platform)return p(),h(e,Worker);var t=d();return t?l(e,t):k(e,m(r),c("child_process"))}return k(e,m(r),c("child_process"))}(this.script,t),this.debugPort=t.debugPort,e||(this.worker.ready=!0),this.requestQueue=[],this.worker.on("message",function(e){if("string"==typeof e&&"ready"===e)o.worker.ready=!0,o.requestQueue.forEach(o.worker.send.bind(o.worker)),o.requestQueue=[];else{var r=e.id,t=o.processing[r];void 0!==t&&(delete o.processing[r],!0===o.terminating&&o.terminate(),e.error?t.resolver.reject(function(e){for(var r=new Error(""),t=Object.keys(e),o=0;o<t.length;o++)r[t[o]]=e[t[o]];return r}(e.error)):t.resolver.resolve(e.result))}});var i=this.worker;this.worker.on("error",n),this.worker.on("exit",function(e,r){var t="Workerpool Worker terminated Unexpectedly\n";t+=" exitCode: `"+e+"`\n",t+=" signalCode: `"+r+"`\n",t+=" workerpool.script: `"+o.script+"`\n",t+=" spawnArgs: `"+i.spawnargs+"`\n",t+=" spawnfile: `"+i.spawnfile+"`\n",t+=" stdout: `"+i.stdout+"`\n",t+=" stderr: `"+i.stderr+"`\n",n(new Error(t))}),this.processing=Object.create(null),this.terminating=!1,this.terminated=!1,this.terminationHandler=null,this.lastId=0}t.prototype.methods=function(){return this.exec("methods")},t.prototype.exec=function(e,r,t){t=t||u.defer();var o=++this.lastId;this.processing[o]={id:o,resolver:t};var n={id:o,method:e,params:r};this.terminated?t.reject(new Error("Worker is terminated")):this.worker.ready?this.worker.send(n):this.requestQueue.push(n);var i=this;return t.promise.catch(function(e){if(!(e instanceof u.CancellationError||e instanceof u.TimeoutError))throw e;delete i.processing[o],i.terminate(!0)}),t.promise},t.prototype.busy=function(){return 0<Object.keys(this.processing).length},t.prototype.terminate=function(e,r){if(e){for(var t in this.processing)void 0!==this.processing[t]&&this.processing[t].resolver.reject(new Error("Worker terminated"));this.processing=Object.create(null)}if("function"==typeof r&&(this.terminationHandler=r),this.busy())this.terminating=!0;else{if(this.worker){if("function"==typeof this.worker.kill)this.worker.kill();else{if("function"!=typeof this.worker.terminate)throw new Error("Failed to terminate worker");this.worker.terminate()}this.worker=null}this.terminating=!1,this.terminated=!0,this.terminationHandler&&this.terminationHandler(this)}},t.prototype.terminateAndNotify=function(e,r){var t=u.defer();return r&&(t.promise.timeout=r),this.terminate(e,function(e){t.resolve(e)}),t.promise},e.exports=t,e.exports._tryRequireWorkerThreads=d,e.exports._setupProcessWorker=k,e.exports._setupBrowserWorker=h,e.exports._setupWorkerThreadWorker=l,e.exports.ensureWorkerThreads=f},function(e,r){e.exports='!function(o){var n={};function t(e){if(n[e])return n[e].exports;var r=n[e]={i:e,l:!1,exports:{}};return o[e].call(r.exports,r,r.exports,t),r.l=!0,r.exports}t.m=o,t.c=n,t.d=function(e,r,o){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:o})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(r,e){if(1&e&&(r=t(r)),8&e)return r;if(4&e&&"object"==typeof r&&r&&r.__esModule)return r;var o=Object.create(null);if(t.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:r}),2&e&&"string"!=typeof r)for(var n in r)t.d(o,n,function(e){return r[e]}.bind(null,n));return o},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="",t(t.s=0)}([function(module,exports,__webpack_require__){var requireFoolWebpack=eval("typeof require !== \'undefined\' ? require : function (module) { throw new Error(\'Module \\" + module + \\" not found.\') }"),worker={};if("undefined"!=typeof self&&"function"==typeof postMessage&&"function"==typeof addEventListener)worker.on=function(e,r){addEventListener(e,function(e){r(e.data)})},worker.send=function(e){postMessage(e)};else{if("undefined"==typeof process)throw new Error("Script must be executed as a worker");var WorkerThreads;try{WorkerThreads=requireFoolWebpack("worker_threads")}catch(e){if("object"!=typeof e||null===e||"MODULE_NOT_FOUND"!=e.code)throw e}if(WorkerThreads&&null!==WorkerThreads.parentPort){var parentPort=WorkerThreads.parentPort;worker.send=parentPort.postMessage.bind(parentPort),worker.on=parentPort.on.bind(parentPort)}else worker.on=process.on.bind(process),worker.send=process.send.bind(process),worker.on("disconnect",function(){process.exit(1)})}function convertError(o){return Object.getOwnPropertyNames(o).reduce(function(e,r){return Object.defineProperty(e,r,{value:o[r],enumerable:!0})},{})}function isPromise(e){return e&&"function"==typeof e.then&&"function"==typeof e.catch}worker.methods={},worker.methods.run=function run(fn,args){var f=eval("("+fn+")");return f.apply(f,args)},worker.methods.methods=function(){return Object.keys(worker.methods)},worker.on("message",function(r){try{var e=worker.methods[r.method];if(!e)throw new Error(\'Unknown method "\'+r.method+\'"\');var o=e.apply(e,r.params);isPromise(o)?o.then(function(e){worker.send({id:r.id,result:e,error:null})}).catch(function(e){worker.send({id:r.id,result:null,error:convertError(e)})}):worker.send({id:r.id,result:o,error:null})}catch(e){worker.send({id:r.id,result:null,error:convertError(e)})}}),worker.register=function(e){if(e)for(var r in e)e.hasOwnProperty(r)&&(worker.methods[r]=e[r]);worker.send("ready")},exports.add=worker.register}]);'},function(e,r,t){"use strict";function o(){this.ports=Object.create(null),this.length=0}(e.exports=o).prototype.nextAvailableStartingAt=function(e){for(;!0===this.ports[e];)e++;if(65535<=e)throw new Error("WorkerPool debug port limit reached: "+e+">= 65535");return this.ports[e]=!0,this.length++,e},o.prototype.releasePort=function(e){delete this.ports[e],this.length--}},function(module,exports,__webpack_require__){var requireFoolWebpack=eval("typeof require !== 'undefined' ? require : function (module) { throw new Error('Module \" + module + \" not found.') }"),worker={};if("undefined"!=typeof self&&"function"==typeof postMessage&&"function"==typeof addEventListener)worker.on=function(e,r){addEventListener(e,function(e){r(e.data)})},worker.send=function(e){postMessage(e)};else{if("undefined"==typeof process)throw new Error("Script must be executed as a worker");var WorkerThreads;try{WorkerThreads=requireFoolWebpack("worker_threads")}catch(e){if("object"!=typeof e||null===e||"MODULE_NOT_FOUND"!=e.code)throw e}if(WorkerThreads&&null!==WorkerThreads.parentPort){var parentPort=WorkerThreads.parentPort;worker.send=parentPort.postMessage.bind(parentPort),worker.on=parentPort.on.bind(parentPort)}else worker.on=process.on.bind(process),worker.send=process.send.bind(process),worker.on("disconnect",function(){process.exit(1)})}function convertError(t){return Object.getOwnPropertyNames(t).reduce(function(e,r){return Object.defineProperty(e,r,{value:t[r],enumerable:!0})},{})}function isPromise(e){return e&&"function"==typeof e.then&&"function"==typeof e.catch}worker.methods={},worker.methods.run=function run(fn,args){var f=eval("("+fn+")");return f.apply(f,args)},worker.methods.methods=function(){return Object.keys(worker.methods)},worker.on("message",function(r){try{var e=worker.methods[r.method];if(!e)throw new Error('Unknown method "'+r.method+'"');var t=e.apply(e,r.params);isPromise(t)?t.then(function(e){worker.send({id:r.id,result:e,error:null})}).catch(function(e){worker.send({id:r.id,result:null,error:convertError(e)})}):worker.send({id:r.id,result:t,error:null})}catch(e){worker.send({id:r.id,result:null,error:convertError(e)})}}),worker.register=function(e){if(e)for(var r in e)e.hasOwnProperty(r)&&(worker.methods[r]=e[r]);worker.send("ready")},exports.add=worker.register}],e={},g.m=d,g.c=e,g.d=function(e,r,t){g.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},g.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},g.t=function(r,e){if(1&e&&(r=g(r)),8&e)return r;if(4&e&&"object"==typeof r&&r&&r.__esModule)return r;var t=Object.create(null);if(g.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:r}),2&e&&"string"!=typeof r)for(var o in r)g.d(t,o,function(e){return r[e]}.bind(null,o));return t},g.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return g.d(r,"a",r),r},g.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},g.p="",g(g.s=3);function g(r){if(e[r])return e[r].exports;var t=e[r]={i:r,l:!1,exports:{}};return d[r].call(t.exports,t,t.exports,g),t.l=!0,t.exports}var d,e});
//# sourceMappingURL=workerpool.map

@@ -5,2 +5,7 @@ # workerpool history

## 2019-10-13, version 5.0.1
- Fixed #96: WorkerPool not cancelling any pending tasks on termination.
## 2019-08-25, version 5.0.0

@@ -7,0 +12,0 @@

{
"name": "workerpool",
"license": "Apache-2.0",
"version": "5.0.0",
"version": "5.0.1",
"description": "Offload tasks to a pool of workers on node.js and in the browser",

@@ -43,7 +43,7 @@ "homepage": "https://github.com/josdejong/workerpool",

"istanbul": "0.4.5",
"mocha": "6.2.0",
"uglify-js": "3.6.0",
"webpack": "4.39.2"
"mocha": "6.2.1",
"uglify-js": "3.6.1",
"webpack": "4.41.1"
},
"dependencies": {}
}

@@ -196,5 +196,5 @@ # workerpool

- `workerType: 'auto' | 'web' | 'process' | 'thread'`.
- In case of `'auto'`, workerpool will automatically pick a suitable type of worker: when in a browser environment, `'web'` will be used. When in a node.js environment, `worker_threads` will be used if available (Node.js >= 11.7.0), else `child_process` will be used.
- In case of `'auto'` (default), workerpool will automatically pick a suitable type of worker: when in a browser environment, `'web'` will be used. When in a node.js environment, `worker_threads` will be used if available (Node.js >= 11.7.0), else `child_process` will be used.
- In case of `'web'`, a Web Worker will be used. Only available in a browser environment.
- In case of `'process'` (default), `child_process` will be used. Only available in a node.js environment.
- In case of `'process'`, `child_process` will be used. Only available in a node.js environment.
- In case of `'thread'`, `worker_threads` will be used. If `worker_threads` are not available, an error is thrown. Only available in a node.js environment.

@@ -229,8 +229,4 @@

If parameter `force` is false (default), workers will finish the tasks they are working on before terminating themselves. When `force` is true, all workers are terminated immediately without finishing running tasks. If `timeout` is provided, worker will be forced to terminal when the timeout expires and the worker has not finished.
If parameter `force` is false (default), workers will finish the tasks they are working on before terminating themselves. Any pending tasks will be rejected with an error 'Pool terminated'. When `force` is true, all workers are terminated immediately without finishing running tasks. If `timeout` is provided, worker will be forced to terminate when the timeout expires and the worker has not finished.
- `Pool.clear([force: boolean])`<br>
*Deprecated: use `Pool.terminate` instead*<br>.
Clear all workers from the pool. If parameter `force` is false (default), workers will finish the tasks they are working on before terminating themselves. When `force` is true, all workers are terminated immediately without finishing running tasks.
The function `Pool.exec` and the proxy functions all return a `Promise`. The promise has the following functions available:

@@ -246,3 +242,3 @@

- `Promise.timeout(delay: number)`<br>
Cancel a running task when it is not resolved or rejected withing given delay in milliseconds. The timer will start when the task is actually started, not when the task is created and queued.
Cancel a running task when it is not resolved or rejected within given delay in milliseconds. The timer will start when the task is actually started, not when the task is created and queued.
The worker executing the task is enforced to terminate immediately.

@@ -249,0 +245,0 @@ The promise will be rejected with a `Promise.TimeoutError`.

@@ -302,2 +302,8 @@ var Promise = require('./Promise');

Pool.prototype.terminate = function (force, timeout) {
// cancel any pending tasks
this.tasks.forEach(function (task) {
task.resolver.reject(new Error('Pool terminated'));
});
this.tasks.length = 0;
var f = function (worker) {

@@ -318,8 +324,9 @@ this._removeWorkerFromList(worker);

// DEPRECATED
/**
* Close all active workers. Unlike terminate, this function does not return a promise.
* @param force
* @deprecated
*/
Pool.prototype.clear = function (force) {
console.warn('Pool.clear() is deprecated. Use Pool.terminate() instead.');
this.terminate(force);

@@ -326,0 +333,0 @@ };

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc