workerpool
Advanced tools
Comparing version 3.1.0 to 3.1.1
@@ -7,4 +7,4 @@ /** | ||
* | ||
* @version 3.1.0 | ||
* @date 2019-02-17 | ||
* @version 3.1.1 | ||
* @date 2019-02-25 | ||
* | ||
@@ -879,2 +879,3 @@ * @license | ||
worker.isBrowserWorker = true; | ||
// add node.js API to the web worker | ||
@@ -897,2 +898,3 @@ worker.on = function (event, callback) { | ||
}); | ||
worker.isWorkerThread = true; | ||
// make the worker mimic a child_process | ||
@@ -916,3 +918,3 @@ worker.send = function(message) { | ||
// no WorkerThreads, fallback to sub-process based workers | ||
return child_process.fork( | ||
var worker = child_process.fork( | ||
script, | ||
@@ -922,2 +924,5 @@ options.forkArgs, | ||
); | ||
worker.isChildProcess = true; | ||
return worker; | ||
} | ||
@@ -987,7 +992,9 @@ | ||
} else { | ||
var WorkerThreads; | ||
if (options.nodeWorker === 'thread') { | ||
var WorkerThreads = ensureWorkerThreads(); | ||
WorkerThreads = ensureWorkerThreads(); | ||
this.worker = setupWorkerThreadWorker(this.script, WorkerThreads); | ||
} else if (options.nodeWorker === 'auto') { | ||
WorkerThreads = tryRequire('worker_threads'); | ||
if (WorkerThreads) { | ||
@@ -994,0 +1001,0 @@ this.worker = setupWorkerThreadWorker(this.script, WorkerThreads); |
@@ -7,4 +7,4 @@ /** | ||
* | ||
* @version 3.1.0 | ||
* @date 2019-02-17 | ||
* @version 3.1.1 | ||
* @date 2019-02-25 | ||
* | ||
@@ -26,3 +26,3 @@ * @license | ||
*/ | ||
!function(r,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("os"),function(){try{return require("worker_threads")}catch(r){}}(),require("child_process")):"function"==typeof define&&define.amd?define(["os","worker_threads","child_process"],e):"object"==typeof exports?exports.workerpool=e(require("os"),function(){try{return require("worker_threads")}catch(r){}}(),require("child_process")):r.workerpool=e(r.os,r.worker_threads,r.child_process)}(this,function(__WEBPACK_EXTERNAL_MODULE_2__,__WEBPACK_EXTERNAL_MODULE_11__,__WEBPACK_EXTERNAL_MODULE_12__){return function(r){function e(o){if(t[o])return t[o].exports;var n=t[o]={exports:{},id:o,loaded:!1};return r[o].call(n.exports,n,n.exports,e),n.loaded=!0,n.exports}var t={};return e.m=r,e.c=t,e.p="",e(0)}([function(r,e,t){var o=t(1);e.pool=function(r,e){return new(t(3))(r,e)},e.worker=function(r){t(10).add(r)},e.Promise=t(4),e.platform=o.platform,e.isMainThread=o.isMainThread,e.cpus=o.cpus},function(r,e,t){r.exports.platform="undefined"!=typeof Window||"undefined"!=typeof WorkerGlobalScope?"browser":"node",r.exports.isMainThread="browser"===r.exports.platform?"undefined"!=typeof Window:!process.connected,r.exports.cpus="browser"===r.exports.platform?self.navigator.hardwareConcurrency:t(2).cpus().length},function(r,e){r.exports=require("os")},function(r,e,t){function o(r,e){"string"==typeof r?this.script=r||null:(this.script=null,e=r),this.workers=[],this.tasks=[],e=e||{},this.forkArgs=e.forkArgs||[],this.forkOpts=e.forkOpts||{},this.debugPortStart=e.debugPortStart||43210,this.nodeWorker=e.nodeWorker,e&&"maxWorkers"in e?(n(e.maxWorkers),this.maxWorkers=e.maxWorkers):this.maxWorkers=Math.max((f.cpus||4)-1,1),e&&"minWorkers"in e&&("max"===e.minWorkers?this.minWorkers=Math.max((f.cpus||4)-1,1):(i(e.minWorkers),this.minWorkers=e.minWorkers,this.maxWorkers=Math.max(this.minWorkers,this.maxWorkers)),this._ensureMinWorkers()),this._boundNext=this._next.bind(this),"thread"===this.nodeWorker&&a.ensureWorkerThreads()}function n(r){if(!s(r)||!u(r)||r<1)throw new TypeError("Option maxWorkers must be an integer number >= 1")}function i(r){if(!s(r)||!u(r)||r<0)throw new TypeError("Option minWorkers must be an integer number >= 0")}function s(r){return"number"==typeof r}function u(r){return Math.round(r)==r}var c=t(4),a=t(5),f=t(1);o.prototype.exec=function(r,e){if(e&&!Array.isArray(e))throw new TypeError('Array expected as argument "params"');if("string"==typeof r){var t=c.defer(),o=this.tasks,n={method:r,params:e,resolver:t,timeout:null};o.push(n);var i=t.promise.timeout;return t.promise.timeout=function(r){return-1!==o.indexOf(n)?(n.timeout=r,t.promise):i.call(t.promise,r)},this._next(),t.promise}if("function"==typeof r)return this.exec("run",[String(r),e]);throw new TypeError('Function or string expected as argument "method"')},o.prototype.proxy=function(){if(arguments.length>0)throw new Error("No arguments expected");var r=this;return this.exec("methods").then(function(e){var t={};return e.forEach(function(e){t[e]=function(){return r.exec(e,Array.prototype.slice.call(arguments))}}),t})},o.prototype._next=function(){if(this.tasks.length>0){var r=this._getWorker();if(r){var e=this,t=this.tasks.shift();if(t.resolver.promise.pending){var o=r.exec(t.method,t.params,t.resolver).then(e._boundNext).catch(function(){r.terminated&&(e._removeWorker(r),e._ensureMinWorkers()),e._next()});"number"==typeof t.timeout&&o.timeout(t.timeout)}else e._next()}}},o.prototype._getWorker=function(){for(var r=this.workers,e=0;e<r.length;e++){var t=r[e];if(!1===t.busy())return t}return r.length<this.maxWorkers?(t=new a(this.script,{forkArgs:this.forkArgs,forkOpts:this.forkOpts,debugPort:this.debugPortStart+r.length,nodeWorker:this.nodeWorker}),r.push(t),t):null},o.prototype._removeWorker=function(r){r.terminate(),this._removeWorkerFromList(r)},o.prototype._removeWorkerFromList=function(r){var e=this.workers.indexOf(r);-1!=e&&this.workers.splice(e,1)},o.prototype.terminate=function(r,e){var t=function(r){this._removeWorkerFromList(r)},o=t.bind(this),n=[];return this.workers.slice().forEach(function(t){var i=t.terminateAndNotify(r,e).then(o);n.push(i)}),c.all(n)},o.prototype.clear=function(r){this.terminate(r)},o.prototype.stats=function(){var r=this.workers.length,e=this.workers.filter(function(r){return r.busy()}).length;return{totalWorkers:r,busyWorkers:e,idleWorkers:r-e,pendingTasks:this.tasks.length,activeTasks:e}},o.prototype._ensureMinWorkers=function(){if(this.minWorkers)for(var r=this.workers.length;r<this.minWorkers;r++)this.workers.push(new a(this.script,{forkArgs:this.forkArgs,forkOpts:this.forkOpts,debugPort:this.debugPortStart+r}))},r.exports=o},function(r,e){"use strict";function t(r,e){var s=this;if(!(this instanceof t))throw new SyntaxError("Constructor must be called with the new operator");if("function"!=typeof r)throw new SyntaxError("Function parameter handler(resolve, reject) missing");var u=[],c=[];this.resolved=!1,this.rejected=!1,this.pending=!0;var a=function(r,e){u.push(r),c.push(e)};this.then=function(r,e){return new t(function(t,n){var i=r?o(r,t,n):t,s=e?o(e,t,n):n;a(i,s)},s)};var f=function(r){return s.resolved=!0,s.rejected=!1,s.pending=!1,u.forEach(function(e){e(r)}),a=function(e,t){e(r)},f=p=function(){},s},p=function(r){return s.resolved=!1,s.rejected=!0,s.pending=!1,c.forEach(function(e){e(r)}),a=function(e,t){t(r)},f=p=function(){},s};this.cancel=function(){return e?e.cancel():p(new n),s},this.timeout=function(r){if(e)e.timeout(r);else{var t=setTimeout(function(){p(new i("Promise timed out after "+r+" ms"))},r);s.always(function(){clearTimeout(t)})}return s},r(function(r){f(r)},function(r){p(r)})}function o(r,e,t){return function(o){try{var n=r(o);n&&"function"==typeof n.then&&"function"==typeof n.catch?n.then(e,t):e(n)}catch(r){t(r)}}}function n(r){this.message=r||"promise cancelled",this.stack=(new Error).stack}function i(r){this.message=r||"timeout exceeded",this.stack=(new Error).stack}t.prototype.catch=function(r){return this.then(null,r)},t.prototype.always=function(r){return this.then(r,r)},t.all=function(r){return new t(function(e,t){var o=r.length,n=[];o?r.forEach(function(r,i){r.then(function(r){n[i]=r,0==--o&&e(n)},function(r){o=0,t(r)})}):e(n)})},t.defer=function(){var r={};return r.promise=new t(function(e,t){r.resolve=e,r.reject=t}),r},n.prototype=new Error,n.prototype.constructor=Error,n.prototype.name="CancellationError",t.CancellationError=n,i.prototype=new Error,i.prototype.constructor=Error,i.prototype.name="TimeoutError",t.TimeoutError=i,r.exports=t},function(r,e,t){"use strict";function o(){var r=n("worker_threads");if(!r)throw new Error("WorkerPool: nodeWorkers = thread is not supported, Node >= 11.7.0 required");return r}function n(r){try{return t(7)(r)}catch(r){if("object"==typeof r&&null!==r&&"MODULE_NOT_FOUND"==r.code)return null;throw r}}function i(){if("browser"==l.platform){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 r=new Blob([t(8)],{type:"text/javascript"});return window.URL.createObjectURL(r)}return __dirname+"/worker.js"}function s(r,e){var t=new e(r);return t.on=function(r,e){this.addEventListener(r,function(r){e(r.data)})},t.send=function(r){this.postMessage(r)},t}function u(r,e){var t=new e.Worker(r,{stdout:!1,stderr:!1});return t.send=function(r){this.postMessage(r)},t.kill=function(){this.terminate()},t.disconnect=function(){this.terminate()},t}function c(r,e,t){return t.fork(r,e.forkArgs,e.forkOpts)}function a(r){r=r||{};var e=process.execArgv.join(" "),t=-1!==e.indexOf("--inspect"),o=-1!==e.indexOf("--debug-brk"),n=[];return t&&(n.push("--inspect="+r.debugPort),o&&n.push("--debug-brk")),h({},r,{forkArgs:r.forkArgs,forkOpts:h({},r.forkOpts,{execArgv:(r.forkOpts&&r.forkOpts.execArgv||[]).concat(n)})})}function f(r){for(var e=new Error(""),t=Object.keys(r),o=0;o<t.length;o++)e[t[o]]=r[t[o]];return e}function p(r,e){function n(r){k.terminated=!0,k.terminating&&k.terminationHandler&&k.terminationHandler(k),k.terminating=!1;for(var e in k.processing)void 0!==k.processing[e]&&k.processing[e].resolver.reject(r);k.processing=Object.create(null)}function p(){k.requestQueue.forEach(k.worker.send.bind(k.worker)),k.requestQueue=[]}this.script=r||i();var d=e||{};if("browser"==l.platform){if("function"!=typeof Worker&&("object"!=typeof Worker||"function"!=typeof Worker.prototype.constructor))throw new Error("WorkerPool: Web workers not supported by the browser");this.worker=s(this.script,Worker)}else if("thread"===d.nodeWorker){var h=o();this.worker=u(this.script,h)}else this.worker="auto"===d.nodeWorker&&h?u(this.script,h):c(this.script,a(d),t(12));var k=this;r||(this.worker.ready=!0),this.requestQueue=[],this.worker.on("message",function(r){if("string"==typeof r&&"ready"===r)k.worker.ready=!0,p();else{var e=r.id,t=k.processing[e];void 0!==t&&(delete k.processing[e],!0===k.terminating&&k.terminate(),r.error?t.resolver.reject(f(r.error)):t.resolver.resolve(r.result))}}),this.worker.on("error",n),this.worker.on("exit",function(){n(new Error("Worker terminated unexpectedly"))}),this.processing=Object.create(null),this.terminating=!1,this.terminated=!1,this.terminationHandler=null,this.lastId=0}var d=t(4),h=t(6),l=t(1);p.prototype.methods=function(){return this.exec("methods")},p.prototype.exec=function(r,e,t){t||(t=d.defer());var o=++this.lastId;this.processing[o]={id:o,resolver:t};var n={id:o,method:r,params:e};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(r){if(!(r instanceof d.CancellationError||r instanceof d.TimeoutError))throw r;delete i.processing[o],i.terminate(!0)}),t.promise},p.prototype.busy=function(){return Object.keys(this.processing).length>0},p.prototype.terminate=function(r,e){if(r){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 e&&(this.terminationHandler=e),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)}},p.prototype.terminateAndNotify=function(r,e){var t=d.defer();return e&&(t.promise.timeout=e),this.terminate(r,function(r){t.resolve(r)}),t.promise},r.exports=p,r.exports._tryRequire=n,r.exports._setupProcessWorker=c,r.exports._setupBrowserWorker=s,r.exports._setupWorkerThreadWorker=u,r.exports.ensureWorkerThreads=o},function(r,e){/* | ||
!function(r,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("os"),function(){try{return require("worker_threads")}catch(r){}}(),require("child_process")):"function"==typeof define&&define.amd?define(["os","worker_threads","child_process"],e):"object"==typeof exports?exports.workerpool=e(require("os"),function(){try{return require("worker_threads")}catch(r){}}(),require("child_process")):r.workerpool=e(r.os,r.worker_threads,r.child_process)}(this,function(__WEBPACK_EXTERNAL_MODULE_2__,__WEBPACK_EXTERNAL_MODULE_11__,__WEBPACK_EXTERNAL_MODULE_12__){return function(r){function e(o){if(t[o])return t[o].exports;var n=t[o]={exports:{},id:o,loaded:!1};return r[o].call(n.exports,n,n.exports,e),n.loaded=!0,n.exports}var t={};return e.m=r,e.c=t,e.p="",e(0)}([function(r,e,t){var o=t(1);e.pool=function(r,e){return new(t(3))(r,e)},e.worker=function(r){t(10).add(r)},e.Promise=t(4),e.platform=o.platform,e.isMainThread=o.isMainThread,e.cpus=o.cpus},function(r,e,t){r.exports.platform="undefined"!=typeof Window||"undefined"!=typeof WorkerGlobalScope?"browser":"node",r.exports.isMainThread="browser"===r.exports.platform?"undefined"!=typeof Window:!process.connected,r.exports.cpus="browser"===r.exports.platform?self.navigator.hardwareConcurrency:t(2).cpus().length},function(r,e){r.exports=require("os")},function(r,e,t){function o(r,e){"string"==typeof r?this.script=r||null:(this.script=null,e=r),this.workers=[],this.tasks=[],e=e||{},this.forkArgs=e.forkArgs||[],this.forkOpts=e.forkOpts||{},this.debugPortStart=e.debugPortStart||43210,this.nodeWorker=e.nodeWorker,e&&"maxWorkers"in e?(n(e.maxWorkers),this.maxWorkers=e.maxWorkers):this.maxWorkers=Math.max((f.cpus||4)-1,1),e&&"minWorkers"in e&&("max"===e.minWorkers?this.minWorkers=Math.max((f.cpus||4)-1,1):(i(e.minWorkers),this.minWorkers=e.minWorkers,this.maxWorkers=Math.max(this.minWorkers,this.maxWorkers)),this._ensureMinWorkers()),this._boundNext=this._next.bind(this),"thread"===this.nodeWorker&&a.ensureWorkerThreads()}function n(r){if(!s(r)||!u(r)||r<1)throw new TypeError("Option maxWorkers must be an integer number >= 1")}function i(r){if(!s(r)||!u(r)||r<0)throw new TypeError("Option minWorkers must be an integer number >= 0")}function s(r){return"number"==typeof r}function u(r){return Math.round(r)==r}var c=t(4),a=t(5),f=t(1);o.prototype.exec=function(r,e){if(e&&!Array.isArray(e))throw new TypeError('Array expected as argument "params"');if("string"==typeof r){var t=c.defer(),o=this.tasks,n={method:r,params:e,resolver:t,timeout:null};o.push(n);var i=t.promise.timeout;return t.promise.timeout=function(r){return-1!==o.indexOf(n)?(n.timeout=r,t.promise):i.call(t.promise,r)},this._next(),t.promise}if("function"==typeof r)return this.exec("run",[String(r),e]);throw new TypeError('Function or string expected as argument "method"')},o.prototype.proxy=function(){if(arguments.length>0)throw new Error("No arguments expected");var r=this;return this.exec("methods").then(function(e){var t={};return e.forEach(function(e){t[e]=function(){return r.exec(e,Array.prototype.slice.call(arguments))}}),t})},o.prototype._next=function(){if(this.tasks.length>0){var r=this._getWorker();if(r){var e=this,t=this.tasks.shift();if(t.resolver.promise.pending){var o=r.exec(t.method,t.params,t.resolver).then(e._boundNext).catch(function(){r.terminated&&(e._removeWorker(r),e._ensureMinWorkers()),e._next()});"number"==typeof t.timeout&&o.timeout(t.timeout)}else e._next()}}},o.prototype._getWorker=function(){for(var r=this.workers,e=0;e<r.length;e++){var t=r[e];if(!1===t.busy())return t}return r.length<this.maxWorkers?(t=new a(this.script,{forkArgs:this.forkArgs,forkOpts:this.forkOpts,debugPort:this.debugPortStart+r.length,nodeWorker:this.nodeWorker}),r.push(t),t):null},o.prototype._removeWorker=function(r){r.terminate(),this._removeWorkerFromList(r)},o.prototype._removeWorkerFromList=function(r){var e=this.workers.indexOf(r);-1!=e&&this.workers.splice(e,1)},o.prototype.terminate=function(r,e){var t=function(r){this._removeWorkerFromList(r)},o=t.bind(this),n=[];return this.workers.slice().forEach(function(t){var i=t.terminateAndNotify(r,e).then(o);n.push(i)}),c.all(n)},o.prototype.clear=function(r){this.terminate(r)},o.prototype.stats=function(){var r=this.workers.length,e=this.workers.filter(function(r){return r.busy()}).length;return{totalWorkers:r,busyWorkers:e,idleWorkers:r-e,pendingTasks:this.tasks.length,activeTasks:e}},o.prototype._ensureMinWorkers=function(){if(this.minWorkers)for(var r=this.workers.length;r<this.minWorkers;r++)this.workers.push(new a(this.script,{forkArgs:this.forkArgs,forkOpts:this.forkOpts,debugPort:this.debugPortStart+r}))},r.exports=o},function(r,e){"use strict";function t(r,e){var s=this;if(!(this instanceof t))throw new SyntaxError("Constructor must be called with the new operator");if("function"!=typeof r)throw new SyntaxError("Function parameter handler(resolve, reject) missing");var u=[],c=[];this.resolved=!1,this.rejected=!1,this.pending=!0;var a=function(r,e){u.push(r),c.push(e)};this.then=function(r,e){return new t(function(t,n){var i=r?o(r,t,n):t,s=e?o(e,t,n):n;a(i,s)},s)};var f=function(r){return s.resolved=!0,s.rejected=!1,s.pending=!1,u.forEach(function(e){e(r)}),a=function(e,t){e(r)},f=p=function(){},s},p=function(r){return s.resolved=!1,s.rejected=!0,s.pending=!1,c.forEach(function(e){e(r)}),a=function(e,t){t(r)},f=p=function(){},s};this.cancel=function(){return e?e.cancel():p(new n),s},this.timeout=function(r){if(e)e.timeout(r);else{var t=setTimeout(function(){p(new i("Promise timed out after "+r+" ms"))},r);s.always(function(){clearTimeout(t)})}return s},r(function(r){f(r)},function(r){p(r)})}function o(r,e,t){return function(o){try{var n=r(o);n&&"function"==typeof n.then&&"function"==typeof n.catch?n.then(e,t):e(n)}catch(r){t(r)}}}function n(r){this.message=r||"promise cancelled",this.stack=(new Error).stack}function i(r){this.message=r||"timeout exceeded",this.stack=(new Error).stack}t.prototype.catch=function(r){return this.then(null,r)},t.prototype.always=function(r){return this.then(r,r)},t.all=function(r){return new t(function(e,t){var o=r.length,n=[];o?r.forEach(function(r,i){r.then(function(r){n[i]=r,0==--o&&e(n)},function(r){o=0,t(r)})}):e(n)})},t.defer=function(){var r={};return r.promise=new t(function(e,t){r.resolve=e,r.reject=t}),r},n.prototype=new Error,n.prototype.constructor=Error,n.prototype.name="CancellationError",t.CancellationError=n,i.prototype=new Error,i.prototype.constructor=Error,i.prototype.name="TimeoutError",t.TimeoutError=i,r.exports=t},function(r,e,t){"use strict";function o(){var r=n("worker_threads");if(!r)throw new Error("WorkerPool: nodeWorkers = thread is not supported, Node >= 11.7.0 required");return r}function n(r){try{return t(7)(r)}catch(r){if("object"==typeof r&&null!==r&&"MODULE_NOT_FOUND"==r.code)return null;throw r}}function i(){if("browser"==l.platform){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 r=new Blob([t(8)],{type:"text/javascript"});return window.URL.createObjectURL(r)}return __dirname+"/worker.js"}function s(r,e){var t=new e(r);return t.isBrowserWorker=!0,t.on=function(r,e){this.addEventListener(r,function(r){e(r.data)})},t.send=function(r){this.postMessage(r)},t}function u(r,e){var t=new e.Worker(r,{stdout:!1,stderr:!1});return t.isWorkerThread=!0,t.send=function(r){this.postMessage(r)},t.kill=function(){this.terminate()},t.disconnect=function(){this.terminate()},t}function c(r,e,t){var o=t.fork(r,e.forkArgs,e.forkOpts);return o.isChildProcess=!0,o}function a(r){r=r||{};var e=process.execArgv.join(" "),t=-1!==e.indexOf("--inspect"),o=-1!==e.indexOf("--debug-brk"),n=[];return t&&(n.push("--inspect="+r.debugPort),o&&n.push("--debug-brk")),h({},r,{forkArgs:r.forkArgs,forkOpts:h({},r.forkOpts,{execArgv:(r.forkOpts&&r.forkOpts.execArgv||[]).concat(n)})})}function f(r){for(var e=new Error(""),t=Object.keys(r),o=0;o<t.length;o++)e[t[o]]=r[t[o]];return e}function p(r,e){function p(r){m.terminated=!0,m.terminating&&m.terminationHandler&&m.terminationHandler(m),m.terminating=!1;for(var e in m.processing)void 0!==m.processing[e]&&m.processing[e].resolver.reject(r);m.processing=Object.create(null)}function d(){m.requestQueue.forEach(m.worker.send.bind(m.worker)),m.requestQueue=[]}this.script=r||i();var h=e||{};if("browser"==l.platform){if("function"!=typeof Worker&&("object"!=typeof Worker||"function"!=typeof Worker.prototype.constructor))throw new Error("WorkerPool: Web workers not supported by the browser");this.worker=s(this.script,Worker)}else{var k;"thread"===h.nodeWorker?(k=o(),this.worker=u(this.script,k)):"auto"===h.nodeWorker?(k=n("worker_threads"),this.worker=k?u(this.script,k):c(this.script,a(h),t(12))):this.worker=c(this.script,a(h),t(12))}var m=this;r||(this.worker.ready=!0),this.requestQueue=[],this.worker.on("message",function(r){if("string"==typeof r&&"ready"===r)m.worker.ready=!0,d();else{var e=r.id,t=m.processing[e];void 0!==t&&(delete m.processing[e],!0===m.terminating&&m.terminate(),r.error?t.resolver.reject(f(r.error)):t.resolver.resolve(r.result))}}),this.worker.on("error",p),this.worker.on("exit",function(){p(new Error("Worker terminated unexpectedly"))}),this.processing=Object.create(null),this.terminating=!1,this.terminated=!1,this.terminationHandler=null,this.lastId=0}var d=t(4),h=t(6),l=t(1);p.prototype.methods=function(){return this.exec("methods")},p.prototype.exec=function(r,e,t){t||(t=d.defer());var o=++this.lastId;this.processing[o]={id:o,resolver:t};var n={id:o,method:r,params:e};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(r){if(!(r instanceof d.CancellationError||r instanceof d.TimeoutError))throw r;delete i.processing[o],i.terminate(!0)}),t.promise},p.prototype.busy=function(){return Object.keys(this.processing).length>0},p.prototype.terminate=function(r,e){if(r){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 e&&(this.terminationHandler=e),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)}},p.prototype.terminateAndNotify=function(r,e){var t=d.defer();return e&&(t.promise.timeout=e),this.terminate(r,function(r){t.resolve(r)}),t.promise},r.exports=p,r.exports._tryRequire=n,r.exports._setupProcessWorker=c,r.exports._setupBrowserWorker=s,r.exports._setupWorkerThreadWorker=u,r.exports.ensureWorkerThreads=o},function(r,e){/* | ||
object-assign | ||
@@ -29,0 +29,0 @@ (c) Sindre Sorhus |
@@ -5,2 +5,8 @@ # workerpool history | ||
## 2019-02-25, version 3.1.1 | ||
- Fix option `nodeWorker: 'auto'` not using worker threads when available. | ||
Thanks @stefanpenner. | ||
## 2019-02-17, version 3.1.0 | ||
@@ -7,0 +13,0 @@ |
@@ -56,2 +56,3 @@ 'use strict'; | ||
worker.isBrowserWorker = true; | ||
// add node.js API to the web worker | ||
@@ -74,2 +75,3 @@ worker.on = function (event, callback) { | ||
}); | ||
worker.isWorkerThread = true; | ||
// make the worker mimic a child_process | ||
@@ -93,3 +95,3 @@ worker.send = function(message) { | ||
// no WorkerThreads, fallback to sub-process based workers | ||
return child_process.fork( | ||
var worker = child_process.fork( | ||
script, | ||
@@ -99,2 +101,5 @@ options.forkArgs, | ||
); | ||
worker.isChildProcess = true; | ||
return worker; | ||
} | ||
@@ -164,7 +169,9 @@ | ||
} else { | ||
var WorkerThreads; | ||
if (options.nodeWorker === 'thread') { | ||
var WorkerThreads = ensureWorkerThreads(); | ||
WorkerThreads = ensureWorkerThreads(); | ||
this.worker = setupWorkerThreadWorker(this.script, WorkerThreads); | ||
} else if (options.nodeWorker === 'auto') { | ||
WorkerThreads = tryRequire('worker_threads'); | ||
if (WorkerThreads) { | ||
@@ -171,0 +178,0 @@ this.worker = setupWorkerThreadWorker(this.script, WorkerThreads); |
{ | ||
"name": "workerpool", | ||
"license": "Apache-2.0", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"description": "Offload tasks to a pool of workers on node.js and in the browser", | ||
@@ -24,2 +24,3 @@ "homepage": "https://github.com/josdejong/workerpool", | ||
"test": "mocha test --timeout 10000", | ||
"test:debug": "mocha debug test --timeout 10000", | ||
"coverage": "istanbul cover _mocha -- test; echo \"\nCoverage report is available at ./coverage/lcov-report/index.html\"", | ||
@@ -26,0 +27,0 @@ "prepublishOnly": "npm run build && npm run test" |
@@ -34,13 +34,26 @@ var assert = require('assert'), | ||
var WorkerThreads = tryRequire('worker_threads'); | ||
it('supports auto', function() { | ||
var pool = new Pool({ nodeWorker: 'auto' }); | ||
return pool.exec(add, [3, 4]) | ||
var result = pool.exec(add, [3, 4]); | ||
assert.equal(pool.workers.length, 1); | ||
var worker = pool.workers[0].worker; | ||
if (WorkerThreads) { | ||
assert.equal(worker.isWorkerThread, true); | ||
} else { | ||
assert.equal(worker.isChildProcess, true); | ||
} | ||
return result; | ||
}); | ||
var WorkerThreads = tryRequire('worker_threads'); | ||
if (WorkerThreads) { | ||
it('supports thread', function() { | ||
var pool = new Pool({ nodeWorker: 'thread' }); | ||
return pool.exec(add, [3, 4]) | ||
var work = pool.exec(add, [3, 4]); | ||
assert.equal(pool.workers.length, 1); | ||
var worker = pool.workers[0].worker; | ||
assert.equal(worker.isWorkerThread, true); | ||
return work; | ||
}); | ||
@@ -47,0 +60,0 @@ } else { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
215072
4499