workerpool
Advanced tools
Comparing version 6.2.1 to 6.3.0
@@ -7,4 +7,4 @@ /** | ||
* | ||
* @version 6.2.1 | ||
* @date 2022-04-11 | ||
* @version 6.3.0 | ||
* @date 2022-10-24 | ||
* | ||
@@ -34,3 +34,2 @@ * @license | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
/** | ||
@@ -40,4 +39,6 @@ * worker must be started as a child process or a web worker. | ||
*/ | ||
// source of inspiration: https://github.com/sindresorhus/require-fool-webpack | ||
var requireFoolWebpack = eval('typeof require !== \'undefined\'' + ' ? require' + ' : function (module) { throw new Error(\'Module " + module + " not found.\') }'); | ||
/** | ||
@@ -47,11 +48,11 @@ * Special message sent by parent which causes the worker to terminate itself. | ||
*/ | ||
var TERMINATE_METHOD_ID = '__workerpool-terminate__'; | ||
var TERMINATE_METHOD_ID = '__workerpool-terminate__'; // var nodeOSPlatform = require('./environment').nodeOSPlatform; | ||
// var nodeOSPlatform = require('./environment').nodeOSPlatform; | ||
// create a worker API for sending and receiving messages which works both on | ||
// node.js and in the browser | ||
var worker = { | ||
exit: function exit() {} | ||
}; | ||
if (typeof self !== 'undefined' && typeof postMessage === 'function' && typeof addEventListener === 'function') { | ||
@@ -64,3 +65,2 @@ // worker in the browser | ||
}; | ||
worker.send = function (message) { | ||
@@ -71,8 +71,9 @@ postMessage(message); | ||
// node.js | ||
var WorkerThreads; | ||
try { | ||
WorkerThreads = requireFoolWebpack('worker_threads'); | ||
} catch (error) { | ||
if (_typeof(error) === 'object' && error !== null && error.code === 'MODULE_NOT_FOUND') {// no worker_threads, fallback to sub-process based workers | ||
if (_typeof(error) === 'object' && error !== null && error.code === 'MODULE_NOT_FOUND') { | ||
// no worker_threads, fallback to sub-process based workers | ||
} else { | ||
@@ -82,5 +83,3 @@ throw error; | ||
} | ||
if (WorkerThreads && | ||
/* if there is a parentPort, we are in a WorkerThread */ | ||
if (WorkerThreads && /* if there is a parentPort, we are in a WorkerThread */ | ||
WorkerThreads.parentPort !== null) { | ||
@@ -92,4 +91,4 @@ var parentPort = WorkerThreads.parentPort; | ||
worker.on = process.on.bind(process); | ||
worker.send = process.send.bind(process); // register disconnect handler only for subprocess worker to exit when parent is killed unexpectedly | ||
worker.send = process.send.bind(process); | ||
// register disconnect handler only for subprocess worker to exit when parent is killed unexpectedly | ||
worker.on('disconnect', function () { | ||
@@ -103,3 +102,2 @@ process.exit(1); | ||
} | ||
function convertError(error) { | ||
@@ -113,2 +111,3 @@ return Object.getOwnPropertyNames(error).reduce(function (product, name) { | ||
} | ||
/** | ||
@@ -120,10 +119,9 @@ * Test whether a value is a Promise via duck typing. | ||
*/ | ||
function isPromise(value) { | ||
return value && typeof value.then === 'function' && typeof value["catch"] === 'function'; | ||
} // functions available externally | ||
} | ||
// functions available externally | ||
worker.methods = {}; | ||
worker.methods = {}; | ||
/** | ||
@@ -135,3 +133,2 @@ * Execute a function with provided arguments | ||
*/ | ||
worker.methods.run = function run(fn, args) { | ||
@@ -141,2 +138,3 @@ var f = new Function('return (' + fn + ').apply(null, arguments);'); | ||
}; | ||
/** | ||
@@ -146,8 +144,5 @@ * Get a list with methods available on this worker | ||
*/ | ||
worker.methods.methods = function methods() { | ||
return Object.keys(worker.methods); | ||
}; | ||
var currentRequestId = null; | ||
@@ -158,11 +153,9 @@ worker.on('message', function (request) { | ||
} | ||
try { | ||
var method = worker.methods[request.method]; | ||
if (method) { | ||
currentRequestId = request.id; // execute the function | ||
currentRequestId = request.id; | ||
// execute the function | ||
var result = method.apply(method, request.params); | ||
if (isPromise(result)) { | ||
@@ -205,2 +198,3 @@ // promise returned, resolve this and then return | ||
}); | ||
/** | ||
@@ -210,3 +204,2 @@ * Register methods to the worker | ||
*/ | ||
worker.register = function (methods) { | ||
@@ -220,6 +213,4 @@ if (methods) { | ||
} | ||
worker.send('ready'); | ||
}; | ||
worker.emit = function (payload) { | ||
@@ -234,3 +225,2 @@ if (currentRequestId) { | ||
}; | ||
if (true) { | ||
@@ -237,0 +227,0 @@ __webpack_unused_export__ = worker.register; |
@@ -7,4 +7,4 @@ /** | ||
* | ||
* @version 6.2.1 | ||
* @date 2022-04-11 | ||
* @version 6.3.0 | ||
* @date 2022-10-24 | ||
* | ||
@@ -44,9 +44,5 @@ * @license | ||
var Promise = __webpack_require__(219); | ||
var WorkerHandler = __webpack_require__(751); | ||
var environment = __webpack_require__(828); | ||
var DebugPortAllocator = __webpack_require__(833); | ||
var DEBUG_PORT_ALLOCATOR = new DebugPortAllocator(); | ||
@@ -59,3 +55,2 @@ /** | ||
*/ | ||
function Pool(script, options) { | ||
@@ -68,5 +63,3 @@ if (typeof script === 'string') { | ||
} | ||
this.workers = []; // queue with all workers | ||
this.tasks = []; // queue with tasks awaiting execution | ||
@@ -77,2 +70,3 @@ | ||
this.forkOpts = Object.freeze(options.forkOpts || {}); | ||
this.workerThreadOpts = Object.freeze(options.workerThreadOpts || {}); | ||
this.debugPortStart = options.debugPortStart || 43210; | ||
@@ -82,12 +76,10 @@ this.nodeWorker = options.nodeWorker; | ||
this.maxQueueSize = options.maxQueueSize || Infinity; | ||
this.onCreateWorker = options.onCreateWorker || function () { | ||
return null; | ||
}; | ||
this.onTerminateWorker = options.onTerminateWorker || function () { | ||
return null; | ||
}; // configuration | ||
}; | ||
// configuration | ||
if (options && 'maxWorkers' in options) { | ||
@@ -99,3 +91,2 @@ validateMaxWorkers(options.maxWorkers); | ||
} | ||
if (options && 'minWorkers' in options) { | ||
@@ -112,5 +103,3 @@ if (options.minWorkers === 'max') { | ||
} | ||
this._boundNext = this._next.bind(this); | ||
if (this.workerType === 'thread') { | ||
@@ -120,2 +109,3 @@ WorkerHandler.ensureWorkerThreads(); | ||
} | ||
/** | ||
@@ -153,4 +143,2 @@ * Execute a function on a worker. | ||
*/ | ||
Pool.prototype.exec = function (method, params, options) { | ||
@@ -161,11 +149,9 @@ // validate type of arguments | ||
} | ||
if (typeof method === 'string') { | ||
var resolver = Promise.defer(); | ||
if (this.tasks.length >= this.maxQueueSize) { | ||
throw new Error('Max queue size of ' + this.maxQueueSize + ' reached'); | ||
} // add a new task to the queue | ||
} | ||
// add a new task to the queue | ||
var tasks = this.tasks; | ||
@@ -179,7 +165,7 @@ var task = { | ||
}; | ||
tasks.push(task); // replace the timeout method of the Promise with our own, | ||
tasks.push(task); | ||
// replace the timeout method of the Promise with our own, | ||
// which starts the timer as soon as the task is actually started | ||
var originalTimeout = resolver.promise.timeout; | ||
resolver.promise.timeout = function timeout(delay) { | ||
@@ -194,7 +180,6 @@ if (tasks.indexOf(task) !== -1) { | ||
} | ||
}; // trigger task execution | ||
}; | ||
// trigger task execution | ||
this._next(); | ||
return resolver.promise; | ||
@@ -208,2 +193,3 @@ } else if (typeof method === 'function') { | ||
}; | ||
/** | ||
@@ -215,4 +201,2 @@ * Create a proxy for current worker. Returns an object containing all | ||
*/ | ||
Pool.prototype.proxy = function () { | ||
@@ -222,3 +206,2 @@ if (arguments.length > 0) { | ||
} | ||
var pool = this; | ||
@@ -235,2 +218,3 @@ return this.exec('methods').then(function (methods) { | ||
}; | ||
/** | ||
@@ -246,3 +230,2 @@ * Creates new array with the results of calling a provided callback function | ||
*/ | ||
/* TODO: implement map | ||
@@ -258,15 +241,14 @@ Pool.prototype.map = function (array, callback) { | ||
*/ | ||
Pool.prototype._next = function () { | ||
if (this.tasks.length > 0) { | ||
// there are tasks in the queue | ||
// find an available worker | ||
var worker = this._getWorker(); | ||
if (worker) { | ||
// get the first task from the queue | ||
var me = this; | ||
var task = this.tasks.shift(); // check if the task is still pending (and not cancelled -> promise rejected) | ||
var task = this.tasks.shift(); | ||
// check if the task is still pending (and not cancelled -> promise rejected) | ||
if (task.resolver.promise.pending) { | ||
@@ -281,5 +263,5 @@ // send the request to the worker | ||
me._next(); // trigger next task in the queue | ||
}); | ||
}); // start queued timer now | ||
// start queued timer now | ||
if (typeof task.timeout === 'number') { | ||
@@ -295,2 +277,3 @@ promise.timeout(task.timeout); | ||
}; | ||
/** | ||
@@ -305,11 +288,7 @@ * Get an available worker. If no worker is available and the maximum number | ||
*/ | ||
Pool.prototype._getWorker = function () { | ||
// find a non-busy worker | ||
var workers = this.workers; | ||
for (var i = 0; i < workers.length; i++) { | ||
var worker = workers[i]; | ||
if (worker.busy() === false) { | ||
@@ -319,3 +298,2 @@ return worker; | ||
} | ||
if (workers.length < this.maxWorkers) { | ||
@@ -327,5 +305,5 @@ // create a new worker | ||
} | ||
return null; | ||
}; | ||
/** | ||
@@ -339,14 +317,10 @@ * Remove a worker from the pool. | ||
*/ | ||
Pool.prototype._removeWorker = function (worker) { | ||
var me = this; | ||
DEBUG_PORT_ALLOCATOR.releasePort(worker.debugPort); // _removeWorker will call this, but we need it to be removed synchronously | ||
this._removeWorkerFromList(worker); // If minWorkers set, spin up new workers to replace the crashed ones | ||
this._ensureMinWorkers(); // terminate the worker (if not already terminated) | ||
DEBUG_PORT_ALLOCATOR.releasePort(worker.debugPort); | ||
// _removeWorker will call this, but we need it to be removed synchronously | ||
this._removeWorkerFromList(worker); | ||
// If minWorkers set, spin up new workers to replace the crashed ones | ||
this._ensureMinWorkers(); | ||
// terminate the worker (if not already terminated) | ||
return new Promise(function (resolve, reject) { | ||
@@ -359,3 +333,2 @@ worker.terminate(false, function (err) { | ||
}); | ||
if (err) { | ||
@@ -369,2 +342,3 @@ reject(err); | ||
}; | ||
/** | ||
@@ -375,8 +349,5 @@ * Remove a worker from the pool list. | ||
*/ | ||
Pool.prototype._removeWorkerFromList = function (worker) { | ||
// remove from the list with workers | ||
var index = this.workers.indexOf(worker); | ||
if (index !== -1) { | ||
@@ -386,2 +357,3 @@ this.workers.splice(index, 1); | ||
}; | ||
/** | ||
@@ -397,7 +369,6 @@ * Close all active workers. Tasks currently being executed will be finished first. | ||
*/ | ||
Pool.prototype.terminate = function (force, timeout) { | ||
var me = this; // cancel any pending tasks | ||
var me = this; | ||
// cancel any pending tasks | ||
this.tasks.forEach(function (task) { | ||
@@ -407,7 +378,5 @@ task.resolver.reject(new Error('Pool terminated')); | ||
this.tasks.length = 0; | ||
var f = function f(worker) { | ||
this._removeWorkerFromList(worker); | ||
}; | ||
var removeWorker = f.bind(this); | ||
@@ -428,2 +397,3 @@ var promises = []; | ||
}; | ||
/** | ||
@@ -433,4 +403,2 @@ * Retrieve statistics on tasks and workers. | ||
*/ | ||
Pool.prototype.stats = function () { | ||
@@ -449,2 +417,3 @@ var totalWorkers = this.workers.length; | ||
}; | ||
/** | ||
@@ -454,4 +423,2 @@ * Ensures that a minimum of minWorkers is up and running | ||
*/ | ||
Pool.prototype._ensureMinWorkers = function () { | ||
@@ -464,2 +431,3 @@ if (this.minWorkers) { | ||
}; | ||
/** | ||
@@ -470,4 +438,2 @@ * Helper function to create a new WorkerHandler and pass all options. | ||
*/ | ||
Pool.prototype._createWorkerHandler = function () { | ||
@@ -477,2 +443,3 @@ var overridenParams = this.onCreateWorker({ | ||
forkOpts: this.forkOpts, | ||
workerThreadOpts: this.workerThreadOpts, | ||
script: this.script | ||
@@ -483,2 +450,3 @@ }) || {}; | ||
forkOpts: overridenParams.forkOpts || this.forkOpts, | ||
workerThreadOpts: overridenParams.workerThreadOpts || this.workerThreadOpts, | ||
debugPort: DEBUG_PORT_ALLOCATOR.nextAvailableStartingAt(this.debugPortStart), | ||
@@ -488,2 +456,3 @@ workerType: this.workerType | ||
}; | ||
/** | ||
@@ -494,4 +463,2 @@ * Ensure that the maxWorkers option is an integer >= 1 | ||
*/ | ||
function validateMaxWorkers(maxWorkers) { | ||
@@ -502,2 +469,3 @@ if (!isNumber(maxWorkers) || !isInteger(maxWorkers) || maxWorkers < 1) { | ||
} | ||
/** | ||
@@ -508,4 +476,2 @@ * Ensure that the minWorkers option is an integer >= 0 | ||
*/ | ||
function validateMinWorkers(minWorkers) { | ||
@@ -516,2 +482,3 @@ if (!isNumber(minWorkers) || !isInteger(minWorkers) || minWorkers < 0) { | ||
} | ||
/** | ||
@@ -522,7 +489,6 @@ * Test whether a variable is a number | ||
*/ | ||
function isNumber(value) { | ||
return typeof value === 'number'; | ||
} | ||
/** | ||
@@ -533,8 +499,5 @@ * Test whether a number is an integer | ||
*/ | ||
function isInteger(value) { | ||
return Math.round(value) == value; | ||
} | ||
module.exports = Pool; | ||
@@ -549,2 +512,3 @@ | ||
/** | ||
@@ -558,20 +522,18 @@ * Promise | ||
*/ | ||
function Promise(handler, parent) { | ||
var me = this; | ||
if (!(this instanceof Promise)) { | ||
throw new SyntaxError('Constructor must be called with the new operator'); | ||
} | ||
if (typeof handler !== 'function') { | ||
throw new SyntaxError('Function parameter handler(resolve, reject) missing'); | ||
} | ||
var _onSuccess = []; | ||
var _onFail = []; // status | ||
var _onFail = []; | ||
// status | ||
this.resolved = false; | ||
this.rejected = false; | ||
this.pending = true; | ||
/** | ||
@@ -584,8 +546,7 @@ * Process onSuccess and onFail callbacks: add them to the queue. | ||
*/ | ||
var _process = function _process(onSuccess, onFail) { | ||
_onSuccess.push(onSuccess); | ||
_onFail.push(onFail); | ||
}; | ||
/** | ||
@@ -597,4 +558,2 @@ * Add an onSuccess callback and optionally an onFail callback to the Promise | ||
*/ | ||
this.then = function (onSuccess, onFail) { | ||
@@ -604,6 +563,6 @@ return new Promise(function (resolve, reject) { | ||
var f = onFail ? _then(onFail, resolve, reject) : reject; | ||
_process(s, f); | ||
}, me); | ||
}; | ||
/** | ||
@@ -614,4 +573,2 @@ * Resolve the promise | ||
*/ | ||
var _resolve2 = function _resolve(result) { | ||
@@ -622,15 +579,12 @@ // update status | ||
me.pending = false; | ||
_onSuccess.forEach(function (fn) { | ||
fn(result); | ||
}); | ||
_process = function _process(onSuccess, onFail) { | ||
onSuccess(result); | ||
}; | ||
_resolve2 = _reject2 = function _reject() {}; | ||
return me; | ||
}; | ||
/** | ||
@@ -641,4 +595,2 @@ * Reject the promise | ||
*/ | ||
var _reject2 = function _reject(error) { | ||
@@ -649,15 +601,12 @@ // update status | ||
me.pending = false; | ||
_onFail.forEach(function (fn) { | ||
fn(error); | ||
}); | ||
_process = function _process(onSuccess, onFail) { | ||
onFail(error); | ||
}; | ||
_resolve2 = _reject2 = function _reject() {}; | ||
return me; | ||
}; | ||
/** | ||
@@ -667,4 +616,2 @@ * Cancel te promise. This will reject the promise with a CancellationError | ||
*/ | ||
this.cancel = function () { | ||
@@ -676,5 +623,5 @@ if (parent) { | ||
} | ||
return me; | ||
}; | ||
/** | ||
@@ -687,4 +634,2 @@ * Set a timeout for the promise. If the promise is not resolved within | ||
*/ | ||
this.timeout = function (delay) { | ||
@@ -701,7 +646,6 @@ if (parent) { | ||
} | ||
return me; | ||
}; // attach handler passing the resolve and reject functions | ||
}; | ||
// attach handler passing the resolve and reject functions | ||
handler(function (result) { | ||
@@ -713,2 +657,3 @@ _resolve2(result); | ||
} | ||
/** | ||
@@ -722,4 +667,2 @@ * Execute given callback, then call resolve/reject based on the returned result | ||
*/ | ||
function _then(callback, resolve, reject) { | ||
@@ -729,3 +672,2 @@ return function (result) { | ||
var res = callback(result); | ||
if (res && typeof res.then === 'function' && typeof res['catch'] === 'function') { | ||
@@ -742,2 +684,3 @@ // method returned a promise | ||
} | ||
/** | ||
@@ -748,7 +691,7 @@ * Add an onFail callback to the Promise | ||
*/ | ||
Promise.prototype['catch'] = function (onFail) { | ||
return this.then(null, onFail); | ||
}; // TODO: add support for Promise.catch(Error, callback) | ||
}; | ||
// TODO: add support for Promise.catch(Error, callback) | ||
// TODO: add support for Promise.catch(Error, Error, callback) | ||
@@ -761,7 +704,6 @@ | ||
*/ | ||
Promise.prototype.always = function (fn) { | ||
return this.then(fn, fn); | ||
}; | ||
/** | ||
@@ -773,9 +715,6 @@ * Create a promise which resolves when all provided promises are resolved, | ||
*/ | ||
Promise.all = function (promises) { | ||
return new Promise(function (resolve, reject) { | ||
var remaining = promises.length, | ||
results = []; | ||
results = []; | ||
if (remaining) { | ||
@@ -786,3 +725,2 @@ promises.forEach(function (p, i) { | ||
remaining--; | ||
if (remaining == 0) { | ||
@@ -801,2 +739,3 @@ resolve(results); | ||
}; | ||
/** | ||
@@ -806,4 +745,2 @@ * Create a promise resolver | ||
*/ | ||
Promise.defer = function () { | ||
@@ -817,2 +754,3 @@ var resolver = {}; | ||
}; | ||
/** | ||
@@ -823,4 +761,2 @@ * Create a cancellation error | ||
*/ | ||
function CancellationError(message) { | ||
@@ -830,3 +766,2 @@ this.message = message || 'promise cancelled'; | ||
} | ||
CancellationError.prototype = new Error(); | ||
@@ -836,2 +771,3 @@ CancellationError.prototype.constructor = Error; | ||
Promise.CancellationError = CancellationError; | ||
/** | ||
@@ -842,3 +778,2 @@ * Create a timeout error | ||
*/ | ||
function TimeoutError(message) { | ||
@@ -848,3 +783,2 @@ this.message = message || 'timeout exceeded'; | ||
} | ||
TimeoutError.prototype = new Error(); | ||
@@ -865,14 +799,12 @@ TimeoutError.prototype.constructor = Error; | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
var Promise = __webpack_require__(219); | ||
var environment = __webpack_require__(828); | ||
var requireFoolWebpack = __webpack_require__(397); | ||
var requireFoolWebpack = __webpack_require__(397); | ||
/** | ||
@@ -882,5 +814,4 @@ * Special message sent by parent which causes a child process worker to terminate itself. | ||
*/ | ||
var TERMINATE_METHOD_ID = '__workerpool-terminate__'; | ||
var TERMINATE_METHOD_ID = '__workerpool-terminate__'; | ||
/** | ||
@@ -890,16 +821,12 @@ * If sending `TERMINATE_METHOD_ID` does not cause the child process to exit in this many milliseconds, | ||
*/ | ||
var CHILD_PROCESS_EXIT_TIMEOUT = 1000; | ||
function ensureWorkerThreads() { | ||
var WorkerThreads = tryRequireWorkerThreads(); | ||
if (!WorkerThreads) { | ||
throw new Error('WorkerPool: workerType = \'thread\' is not supported, Node >= 11.7.0 required'); | ||
} | ||
return WorkerThreads; | ||
} // check whether Worker is supported by the browser | ||
} | ||
// check whether Worker is supported by the browser | ||
function ensureWebWorker() { | ||
@@ -911,3 +838,2 @@ // Workaround for a bug in PhantomJS (Or QtWebkit): https://github.com/ariya/phantomjs/issues/14534 | ||
} | ||
function tryRequireWorkerThreads() { | ||
@@ -924,5 +850,5 @@ try { | ||
} | ||
} // get the default worker script | ||
} | ||
// get the default worker script | ||
function getDefaultWorker() { | ||
@@ -934,8 +860,7 @@ if (environment.platform === 'browser') { | ||
} | ||
if (!window.URL || typeof window.URL.createObjectURL !== 'function') { | ||
throw new Error('URL.createObjectURL not supported by the browser'); | ||
} // use embedded worker.js | ||
} | ||
// use embedded worker.js | ||
var blob = new Blob([__webpack_require__(670)], { | ||
@@ -950,3 +875,2 @@ type: 'text/javascript' | ||
} | ||
function setupWorker(script, options) { | ||
@@ -960,3 +884,3 @@ if (options.workerType === 'web') { | ||
WorkerThreads = ensureWorkerThreads(); | ||
return setupWorkerThreadWorker(script, WorkerThreads); | ||
return setupWorkerThreadWorker(script, WorkerThreads, options.workerThreadOpts); | ||
} else if (options.workerType === 'process' || !options.workerType) { | ||
@@ -973,3 +897,2 @@ // node.js only | ||
var WorkerThreads = tryRequireWorkerThreads(); | ||
if (WorkerThreads) { | ||
@@ -983,8 +906,7 @@ return setupWorkerThreadWorker(script, WorkerThreads); | ||
} | ||
function setupBrowserWorker(script, Worker) { | ||
// create the web worker | ||
var worker = new Worker(script); | ||
worker.isBrowserWorker = true; // add node.js API to the web worker | ||
worker.isBrowserWorker = true; | ||
// add node.js API to the web worker | ||
worker.on = function (event, callback) { | ||
@@ -995,23 +917,18 @@ this.addEventListener(event, function (message) { | ||
}; | ||
worker.send = function (message) { | ||
this.postMessage(message); | ||
}; | ||
return worker; | ||
} | ||
function setupWorkerThreadWorker(script, WorkerThreads) { | ||
var worker = new WorkerThreads.Worker(script, { | ||
function setupWorkerThreadWorker(script, WorkerThreads, workerThreadOptions) { | ||
var worker = new WorkerThreads.Worker(script, _objectSpread({ | ||
stdout: false, | ||
// automatically pipe worker.STDOUT to process.STDOUT | ||
stderr: false // automatically pipe worker.STDERR to process.STDERR | ||
}); | ||
worker.isWorkerThread = true; // make the worker mimic a child_process | ||
stderr: false | ||
}, workerThreadOptions)); | ||
worker.isWorkerThread = true; | ||
// make the worker mimic a child_process | ||
worker.send = function (message) { | ||
this.postMessage(message); | ||
}; | ||
worker.kill = function () { | ||
@@ -1021,10 +938,7 @@ this.terminate(); | ||
}; | ||
worker.disconnect = function () { | ||
this.terminate(); | ||
}; | ||
return worker; | ||
} | ||
function setupProcessWorker(script, options, child_process) { | ||
@@ -1035,5 +949,5 @@ // no WorkerThreads, fallback to sub-process based workers | ||
return worker; | ||
} // add debug flags to child processes if the node inspector is active | ||
} | ||
// add debug flags to child processes if the node inspector is active | ||
function resolveForkOptions(opts) { | ||
@@ -1045,6 +959,4 @@ opts = opts || {}; | ||
var execArgv = []; | ||
if (inspectorActive) { | ||
execArgv.push('--inspect=' + opts.debugPort); | ||
if (debugBrk) { | ||
@@ -1054,3 +966,2 @@ execArgv.push('--debug-brk'); | ||
} | ||
process.execArgv.forEach(function (arg) { | ||
@@ -1068,2 +979,3 @@ if (arg.indexOf('--max-old-space-size') > -1) { | ||
} | ||
/** | ||
@@ -1074,14 +986,11 @@ * Converts a serialized error to Error | ||
*/ | ||
function objectToError(obj) { | ||
var temp = new Error(''); | ||
var props = Object.keys(obj); | ||
for (var i = 0; i < props.length; i++) { | ||
temp[props[i]] = obj[props[i]]; | ||
} | ||
return temp; | ||
} | ||
/** | ||
@@ -1095,4 +1004,2 @@ * A WorkerHandler controls a single worker. This worker can be a child process | ||
*/ | ||
function WorkerHandler(script, _options) { | ||
@@ -1105,9 +1012,11 @@ var me = this; | ||
this.forkOpts = options.forkOpts; | ||
this.forkArgs = options.forkArgs; // The ready message is only sent if the worker.add method is called (And the default script is not used) | ||
this.forkArgs = options.forkArgs; | ||
this.workerThreadOpts = options.workerThreadOpts; | ||
// The ready message is only sent if the worker.add method is called (And the default script is not used) | ||
if (!script) { | ||
this.worker.ready = true; | ||
} // queue for requests that are received before the worker is ready | ||
} | ||
// queue for requests that are received before the worker is ready | ||
this.requestQueue = []; | ||
@@ -1118,3 +1027,2 @@ this.worker.on('message', function (response) { | ||
} | ||
if (typeof response === 'string' && response === 'ready') { | ||
@@ -1127,3 +1035,2 @@ me.worker.ready = true; | ||
var task = me.processing[id]; | ||
if (task !== undefined) { | ||
@@ -1136,10 +1043,11 @@ if (response.isEvent) { | ||
// remove the task from the queue | ||
delete me.processing[id]; // test if we need to terminate | ||
delete me.processing[id]; | ||
// test if we need to terminate | ||
if (me.terminating === true) { | ||
// complete worker termination if all tasks are finished | ||
me.terminate(); | ||
} // resolve the task's promise | ||
} | ||
// resolve the task's promise | ||
if (response.error) { | ||
@@ -1153,7 +1061,7 @@ task.resolver.reject(objectToError(response.error)); | ||
} | ||
}); // reject all running tasks on worker error | ||
}); | ||
// reject all running tasks on worker error | ||
function onError(error) { | ||
me.terminated = true; | ||
for (var id in me.processing) { | ||
@@ -1164,11 +1072,9 @@ if (me.processing[id] !== undefined) { | ||
} | ||
me.processing = Object.create(null); | ||
} // send all queued requests to worker | ||
} | ||
// send all queued requests to worker | ||
function dispatchQueuedRequests() { | ||
var _iterator = _createForOfIteratorHelper(me.requestQueue.splice(0)), | ||
_step; | ||
_step; | ||
try { | ||
@@ -1185,5 +1091,4 @@ for (_iterator.s(); !(_step = _iterator.n()).done;) { | ||
} | ||
var worker = this.worker; // listen for worker messages error and exit | ||
var worker = this.worker; | ||
// listen for worker messages error and exit | ||
this.worker.on('error', onError); | ||
@@ -1208,2 +1113,3 @@ this.worker.on('exit', function (exitCode, signalCode) { | ||
} | ||
/** | ||
@@ -1213,7 +1119,6 @@ * Get a list with methods available on the worker. | ||
*/ | ||
WorkerHandler.prototype.methods = function () { | ||
return this.exec('methods'); | ||
}; | ||
/** | ||
@@ -1227,12 +1132,11 @@ * Execute a method with given parameters on the worker | ||
*/ | ||
WorkerHandler.prototype.exec = function (method, params, resolver, options) { | ||
if (!resolver) { | ||
resolver = Promise.defer(); | ||
} // generate a unique id for the task | ||
} | ||
// generate a unique id for the task | ||
var id = ++this.lastId; | ||
var id = ++this.lastId; // register a new task as being in progress | ||
// register a new task as being in progress | ||
this.processing[id] = { | ||
@@ -1242,4 +1146,5 @@ id: id, | ||
options: options | ||
}; // build a JSON-RPC request | ||
}; | ||
// build a JSON-RPC request | ||
var request = { | ||
@@ -1250,3 +1155,2 @@ id: id, | ||
}; | ||
if (this.terminated) { | ||
@@ -1259,5 +1163,5 @@ resolver.reject(new Error('Worker is terminated')); | ||
this.requestQueue.push(request); | ||
} // on cancellation, force the worker to terminate | ||
} | ||
// on cancellation, force the worker to terminate | ||
var me = this; | ||
@@ -1268,4 +1172,5 @@ return resolver.promise["catch"](function (error) { | ||
// catch event), and else it will be rejected again when terminating | ||
delete me.processing[id]; // terminate worker | ||
delete me.processing[id]; | ||
// terminate worker | ||
return me.terminateAndNotify(true).then(function () { | ||
@@ -1281,2 +1186,3 @@ throw error; | ||
}; | ||
/** | ||
@@ -1286,7 +1192,6 @@ * Test whether the worker is working or not | ||
*/ | ||
WorkerHandler.prototype.busy = function () { | ||
return Object.keys(this.processing).length > 0; | ||
}; | ||
/** | ||
@@ -1300,7 +1205,4 @@ * Terminate the worker. | ||
*/ | ||
WorkerHandler.prototype.terminate = function (force, callback) { | ||
var me = this; | ||
if (force) { | ||
@@ -1313,10 +1215,7 @@ // cancel all tasks in progress | ||
} | ||
this.processing = Object.create(null); | ||
} | ||
if (typeof callback === 'function') { | ||
this.terminationHandler = callback; | ||
} | ||
if (!this.busy()) { | ||
@@ -1326,3 +1225,2 @@ // all tasks are finished. kill the worker | ||
me.terminated = true; | ||
if (me.worker != null && me.worker.removeAllListeners) { | ||
@@ -1332,6 +1230,4 @@ // removeAllListeners is only available for child_process | ||
} | ||
me.worker = null; | ||
me.terminating = false; | ||
if (me.terminationHandler) { | ||
@@ -1343,3 +1239,2 @@ me.terminationHandler(err, me); | ||
}; | ||
if (this.worker) { | ||
@@ -1351,3 +1246,2 @@ if (typeof this.worker.kill === 'function') { | ||
} | ||
if (this.worker.isChildProcess) { | ||
@@ -1361,10 +1255,7 @@ var cleanExitTimeout = setTimeout(function () { | ||
clearTimeout(cleanExitTimeout); | ||
if (me.worker) { | ||
me.worker.killed = true; | ||
} | ||
cleanup(); | ||
}); | ||
if (this.worker.ready) { | ||
@@ -1381,7 +1272,5 @@ this.worker.send(TERMINATE_METHOD_ID); | ||
} | ||
return; | ||
} else if (typeof this.worker.terminate === 'function') { | ||
this.worker.terminate(); // web worker | ||
this.worker.killed = true; | ||
@@ -1392,3 +1281,2 @@ } else { | ||
} | ||
cleanup(); | ||
@@ -1400,2 +1288,3 @@ } else { | ||
}; | ||
/** | ||
@@ -1411,11 +1300,7 @@ * Terminate the worker, returning a Promise that resolves when the termination has been done. | ||
*/ | ||
WorkerHandler.prototype.terminateAndNotify = function (force, timeout) { | ||
var resolver = Promise.defer(); | ||
if (timeout) { | ||
resolver.promise.timeout = timeout; | ||
} | ||
this.terminate(force, function (err, worker) { | ||
@@ -1430,3 +1315,2 @@ if (err) { | ||
}; | ||
module.exports = WorkerHandler; | ||
@@ -1449,3 +1333,2 @@ module.exports._tryRequireWorkerThreads = tryRequireWorkerThreads; | ||
module.exports = DebugPortAllocator; | ||
function DebugPortAllocator() { | ||
@@ -1455,3 +1338,2 @@ this.ports = Object.create(null); | ||
} | ||
DebugPortAllocator.prototype.nextAvailableStartingAt = function (starting) { | ||
@@ -1461,7 +1343,5 @@ while (this.ports[starting] === true) { | ||
} | ||
if (starting >= MAX_PORTS) { | ||
throw new Error('WorkerPool debug port limit reached: ' + starting + '>= ' + MAX_PORTS); | ||
} | ||
this.ports[starting] = true; | ||
@@ -1471,3 +1351,2 @@ this.length++; | ||
}; | ||
DebugPortAllocator.prototype.releasePort = function (port) { | ||
@@ -1483,19 +1362,20 @@ delete this.ports[port]; | ||
var requireFoolWebpack = __webpack_require__(397); // source: https://github.com/flexdinesh/browser-or-node | ||
var requireFoolWebpack = __webpack_require__(397); | ||
// source: https://github.com/flexdinesh/browser-or-node | ||
var isNode = function isNode(nodeProcess) { | ||
return typeof nodeProcess !== 'undefined' && nodeProcess.versions != null && nodeProcess.versions.node != null; | ||
}; | ||
module.exports.isNode = isNode; | ||
module.exports.isNode = isNode; // determines the JavaScript platform: browser or node | ||
// determines the JavaScript platform: browser or node | ||
module.exports.platform = typeof process !== 'undefined' && isNode(process) ? 'node' : 'browser'; | ||
module.exports.platform = typeof process !== 'undefined' && isNode(process) ? 'node' : 'browser'; // determines whether the code is running in main thread or not | ||
// determines whether the code is running in main thread or not | ||
// note that in node.js we have to check both worker_thread and child_process | ||
var worker_threads = tryRequireFoolWebpack('worker_threads'); | ||
module.exports.isMainThread = module.exports.platform === 'node' ? (!worker_threads || worker_threads.isMainThread) && !process.connected : typeof Window !== 'undefined'; // determines the number of cpus available | ||
module.exports.isMainThread = module.exports.platform === 'node' ? (!worker_threads || worker_threads.isMainThread) && !process.connected : typeof Window !== 'undefined'; | ||
// determines the number of cpus available | ||
module.exports.cpus = module.exports.platform === 'browser' ? self.navigator.hardwareConcurrency : requireFoolWebpack('os').cpus().length; | ||
function tryRequireFoolWebpack(module) { | ||
@@ -1536,3 +1416,2 @@ try { | ||
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } | ||
/** | ||
@@ -1542,4 +1421,6 @@ * worker must be started as a child process or a web worker. | ||
*/ | ||
// source of inspiration: https://github.com/sindresorhus/require-fool-webpack | ||
var requireFoolWebpack = eval('typeof require !== \'undefined\'' + ' ? require' + ' : function (module) { throw new Error(\'Module " + module + " not found.\') }'); | ||
/** | ||
@@ -1549,11 +1430,11 @@ * Special message sent by parent which causes the worker to terminate itself. | ||
*/ | ||
var TERMINATE_METHOD_ID = '__workerpool-terminate__'; | ||
var TERMINATE_METHOD_ID = '__workerpool-terminate__'; // var nodeOSPlatform = require('./environment').nodeOSPlatform; | ||
// var nodeOSPlatform = require('./environment').nodeOSPlatform; | ||
// create a worker API for sending and receiving messages which works both on | ||
// node.js and in the browser | ||
var worker = { | ||
exit: function exit() {} | ||
}; | ||
if (typeof self !== 'undefined' && typeof postMessage === 'function' && typeof addEventListener === 'function') { | ||
@@ -1566,3 +1447,2 @@ // worker in the browser | ||
}; | ||
worker.send = function (message) { | ||
@@ -1573,8 +1453,9 @@ postMessage(message); | ||
// node.js | ||
var WorkerThreads; | ||
try { | ||
WorkerThreads = requireFoolWebpack('worker_threads'); | ||
} catch (error) { | ||
if (_typeof(error) === 'object' && error !== null && error.code === 'MODULE_NOT_FOUND') {// no worker_threads, fallback to sub-process based workers | ||
if (_typeof(error) === 'object' && error !== null && error.code === 'MODULE_NOT_FOUND') { | ||
// no worker_threads, fallback to sub-process based workers | ||
} else { | ||
@@ -1584,5 +1465,3 @@ throw error; | ||
} | ||
if (WorkerThreads && | ||
/* if there is a parentPort, we are in a WorkerThread */ | ||
if (WorkerThreads && /* if there is a parentPort, we are in a WorkerThread */ | ||
WorkerThreads.parentPort !== null) { | ||
@@ -1594,4 +1473,4 @@ var parentPort = WorkerThreads.parentPort; | ||
worker.on = process.on.bind(process); | ||
worker.send = process.send.bind(process); // register disconnect handler only for subprocess worker to exit when parent is killed unexpectedly | ||
worker.send = process.send.bind(process); | ||
// register disconnect handler only for subprocess worker to exit when parent is killed unexpectedly | ||
worker.on('disconnect', function () { | ||
@@ -1605,3 +1484,2 @@ process.exit(1); | ||
} | ||
function convertError(error) { | ||
@@ -1615,2 +1493,3 @@ return Object.getOwnPropertyNames(error).reduce(function (product, name) { | ||
} | ||
/** | ||
@@ -1622,10 +1501,9 @@ * Test whether a value is a Promise via duck typing. | ||
*/ | ||
function isPromise(value) { | ||
return value && typeof value.then === 'function' && typeof value["catch"] === 'function'; | ||
} // functions available externally | ||
} | ||
// functions available externally | ||
worker.methods = {}; | ||
worker.methods = {}; | ||
/** | ||
@@ -1637,3 +1515,2 @@ * Execute a function with provided arguments | ||
*/ | ||
worker.methods.run = function run(fn, args) { | ||
@@ -1643,2 +1520,3 @@ var f = new Function('return (' + fn + ').apply(null, arguments);'); | ||
}; | ||
/** | ||
@@ -1648,8 +1526,5 @@ * Get a list with methods available on this worker | ||
*/ | ||
worker.methods.methods = function methods() { | ||
return Object.keys(worker.methods); | ||
}; | ||
var currentRequestId = null; | ||
@@ -1660,11 +1535,9 @@ worker.on('message', function (request) { | ||
} | ||
try { | ||
var method = worker.methods[request.method]; | ||
if (method) { | ||
currentRequestId = request.id; // execute the function | ||
currentRequestId = request.id; | ||
// execute the function | ||
var result = method.apply(method, request.params); | ||
if (isPromise(result)) { | ||
@@ -1707,2 +1580,3 @@ // promise returned, resolve this and then return | ||
}); | ||
/** | ||
@@ -1712,3 +1586,2 @@ * Register methods to the worker | ||
*/ | ||
worker.register = function (methods) { | ||
@@ -1722,6 +1595,4 @@ if (methods) { | ||
} | ||
worker.send('ready'); | ||
}; | ||
worker.emit = function (payload) { | ||
@@ -1736,3 +1607,2 @@ if (currentRequestId) { | ||
}; | ||
if (true) { | ||
@@ -1777,2 +1647,3 @@ exports.add = worker.register; | ||
var environment = __webpack_require__(828); | ||
/** | ||
@@ -1784,9 +1655,7 @@ * Create a new worker pool | ||
*/ | ||
exports.pool = function pool(script, options) { | ||
var Pool = __webpack_require__(345); | ||
return new Pool(script, options); | ||
}; | ||
/** | ||
@@ -1796,9 +1665,7 @@ * Create a worker and optionally register a set of methods to the worker. | ||
*/ | ||
exports.worker = function worker(methods) { | ||
var worker = __webpack_require__(744); | ||
worker.add(methods); | ||
}; | ||
/** | ||
@@ -1808,9 +1675,7 @@ * Sends an event to the parent worker pool. | ||
*/ | ||
exports.workerEmit = function workerEmit(payload) { | ||
var worker = __webpack_require__(744); | ||
worker.emit(payload); | ||
}; | ||
/** | ||
@@ -1820,4 +1685,2 @@ * Create a promise. | ||
*/ | ||
exports.Promise = __webpack_require__(219); | ||
@@ -1824,0 +1687,0 @@ exports.platform = environment.platform; |
/*! For license information please see workerpool.min.js.LICENSE.txt */ | ||
!function(r,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("workerpool",[],e):"object"==typeof exports?exports.workerpool=e():r.workerpool=e()}("undefined"!=typeof self?self:this,(function(){return function(){var __webpack_modules__={345:function(r,e,t){var o=t(219),n=t(751),i=t(828),s=new(t(833));function u(r,e){"string"==typeof r?this.script=r||null:(this.script=null,e=r),this.workers=[],this.tasks=[],e=e||{},this.forkArgs=Object.freeze(e.forkArgs||[]),this.forkOpts=Object.freeze(e.forkOpts||{}),this.debugPortStart=e.debugPortStart||43210,this.nodeWorker=e.nodeWorker,this.workerType=e.workerType||e.nodeWorker||"auto",this.maxQueueSize=e.maxQueueSize||1/0,this.onCreateWorker=e.onCreateWorker||function(){return null},this.onTerminateWorker=e.onTerminateWorker||function(){return null},e&&"maxWorkers"in e?(function(r){if(!c(r)||!a(r)||r<1)throw new TypeError("Option maxWorkers must be an integer number >= 1")}(e.maxWorkers),this.maxWorkers=e.maxWorkers):this.maxWorkers=Math.max((i.cpus||4)-1,1),e&&"minWorkers"in e&&("max"===e.minWorkers?this.minWorkers=this.maxWorkers:(function(r){if(!c(r)||!a(r)||r<0)throw new TypeError("Option minWorkers must be an integer number >= 0")}(e.minWorkers),this.minWorkers=e.minWorkers,this.maxWorkers=Math.max(this.minWorkers,this.maxWorkers)),this._ensureMinWorkers()),this._boundNext=this._next.bind(this),"thread"===this.workerType&&n.ensureWorkerThreads()}function c(r){return"number"==typeof r}function a(r){return Math.round(r)==r}u.prototype.exec=function(r,e,t){if(e&&!Array.isArray(e))throw new TypeError('Array expected as argument "params"');if("string"==typeof r){var n=o.defer();if(this.tasks.length>=this.maxQueueSize)throw new Error("Max queue size of "+this.maxQueueSize+" reached");var i=this.tasks,s={method:r,params:e,resolver:n,timeout:null,options:t};i.push(s);var u=n.promise.timeout;return n.promise.timeout=function(r){return-1!==i.indexOf(s)?(s.timeout=r,n.promise):u.call(n.promise,r)},this._next(),n.promise}if("function"==typeof r)return this.exec("run",[String(r),e]);throw new TypeError('Function or string expected as argument "method"')},u.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}))},u.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,t.options).then(e._boundNext).catch((function(){if(r.terminated)return e._removeWorker(r)})).then((function(){e._next()}));"number"==typeof t.timeout&&o.timeout(t.timeout)}else e._next()}}},u.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=this._createWorkerHandler(),r.push(t),t):null},u.prototype._removeWorker=function(r){var e=this;return s.releasePort(r.debugPort),this._removeWorkerFromList(r),this._ensureMinWorkers(),new o((function(t,o){r.terminate(!1,(function(n){e.onTerminateWorker({forkArgs:r.forkArgs,forkOpts:r.forkOpts,script:r.script}),n?o(n):t(r)}))}))},u.prototype._removeWorkerFromList=function(r){var e=this.workers.indexOf(r);-1!==e&&this.workers.splice(e,1)},u.prototype.terminate=function(r,e){var t=this;this.tasks.forEach((function(r){r.resolver.reject(new Error("Pool terminated"))})),this.tasks.length=0;var n=function(r){this._removeWorkerFromList(r)}.bind(this),i=[];return this.workers.slice().forEach((function(o){var s=o.terminateAndNotify(r,e).then(n).always((function(){t.onTerminateWorker({forkArgs:o.forkArgs,forkOpts:o.forkOpts,script:o.script})}));i.push(s)})),o.all(i)},u.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}},u.prototype._ensureMinWorkers=function(){if(this.minWorkers)for(var r=this.workers.length;r<this.minWorkers;r++)this.workers.push(this._createWorkerHandler())},u.prototype._createWorkerHandler=function(){var r=this.onCreateWorker({forkArgs:this.forkArgs,forkOpts:this.forkOpts,script:this.script})||{};return new n(r.script||this.script,{forkArgs:r.forkArgs||this.forkArgs,forkOpts:r.forkOpts||this.forkOpts,debugPort:s.nextAvailableStartingAt(this.debugPortStart),workerType:this.workerType})},r.exports=u},219:function(r){"use strict";function e(r,i){var s=this;if(!(this instanceof e))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,o){return new e((function(e,n){var i=r?t(r,e,n):e,s=o?t(o,e,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 i?i.cancel():p(new o),s},this.timeout=function(r){if(i)i.timeout(r);else{var e=setTimeout((function(){p(new n("Promise timed out after "+r+" ms"))}),r);s.always((function(){clearTimeout(e)}))}return s},r((function(r){f(r)}),(function(r){p(r)}))}function t(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 o(r){this.message=r||"promise cancelled",this.stack=(new Error).stack}function n(r){this.message=r||"timeout exceeded",this.stack=(new Error).stack}e.prototype.catch=function(r){return this.then(null,r)},e.prototype.always=function(r){return this.then(r,r)},e.all=function(r){return new e((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)}))},e.defer=function(){var r={};return r.promise=new e((function(e,t){r.resolve=e,r.reject=t})),r},o.prototype=new Error,o.prototype.constructor=Error,o.prototype.name="CancellationError",e.CancellationError=o,n.prototype=new Error,n.prototype.constructor=Error,n.prototype.name="TimeoutError",e.TimeoutError=n,r.exports=e},751:function(r,e,t){"use strict";function o(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,o=new Array(e);t<e;t++)o[t]=r[t];return o}function n(r){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r},n(r)}var i=t(219),s=t(828),u=t(397),c="__workerpool-terminate__";function a(){var r=p();if(!r)throw new Error("WorkerPool: workerType = 'thread' is not supported, Node >= 11.7.0 required");return r}function f(){if("function"!=typeof Worker&&("object"!==("undefined"==typeof Worker?"undefined":n(Worker))||"function"!=typeof Worker.prototype.constructor))throw new Error("WorkerPool: Web Workers not supported")}function p(){try{return u("worker_threads")}catch(r){if("object"===n(r)&&null!==r&&"MODULE_NOT_FOUND"===r.code)return null;throw r}}function d(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 l(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(){return this.terminate(),!0},t.disconnect=function(){this.terminate()},t}function h(r,e,t){var o=t.fork(r,e.forkArgs,e.forkOpts);return o.isChildProcess=!0,o}function k(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")),process.execArgv.forEach((function(r){r.indexOf("--max-old-space-size")>-1&&n.push(r)})),Object.assign({},r,{forkArgs:r.forkArgs,forkOpts:Object.assign({},r.forkOpts,{execArgv:(r.forkOpts&&r.forkOpts.execArgv||[]).concat(n)})})}function w(r,e){var n=this,i=e||{};function c(r){for(var e in n.terminated=!0,n.processing)void 0!==n.processing[e]&&n.processing[e].resolver.reject(r);n.processing=Object.create(null)}this.script=r||function(){if("browser"===s.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(670)],{type:"text/javascript"});return window.URL.createObjectURL(r)}return __dirname+"/worker.js"}(),this.worker=function(r,e){if("web"===e.workerType)return f(),d(r,Worker);if("thread"===e.workerType)return l(r,t=a());if("process"!==e.workerType&&e.workerType){if("browser"===s.platform)return f(),d(r,Worker);var t=p();return t?l(r,t):h(r,k(e),u("child_process"))}return h(r,k(e),u("child_process"))}(this.script,i),this.debugPort=i.debugPort,this.forkOpts=i.forkOpts,this.forkArgs=i.forkArgs,r||(this.worker.ready=!0),this.requestQueue=[],this.worker.on("message",(function(r){if(!n.terminated)if("string"==typeof r&&"ready"===r)n.worker.ready=!0,function(){var r,e=function(r,e){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(!t){if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return o(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?o(r,e):void 0}}(r))||e&&r&&"number"==typeof r.length){t&&(r=t);var n=0,i=function(){};return{s:i,n:function(){return n>=r.length?{done:!0}:{done:!1,value:r[n++]}},e:function(r){throw r},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,u=!0,c=!1;return{s:function(){t=t.call(r)},n:function(){var r=t.next();return u=r.done,r},e:function(r){c=!0,s=r},f:function(){try{u||null==t.return||t.return()}finally{if(c)throw s}}}}(n.requestQueue.splice(0));try{for(e.s();!(r=e.n()).done;){var t=r.value;n.worker.send(t)}}catch(r){e.e(r)}finally{e.f()}}();else{var e=r.id,t=n.processing[e];void 0!==t&&(r.isEvent?t.options&&"function"==typeof t.options.on&&t.options.on(r.payload):(delete n.processing[e],!0===n.terminating&&n.terminate(),r.error?t.resolver.reject(function(r){for(var e=new Error(""),t=Object.keys(r),o=0;o<t.length;o++)e[t[o]]=r[t[o]];return e}(r.error)):t.resolver.resolve(r.result)))}}));var w=this.worker;this.worker.on("error",c),this.worker.on("exit",(function(r,e){var t="Workerpool Worker terminated Unexpectedly\n";t+=" exitCode: `"+r+"`\n",t+=" signalCode: `"+e+"`\n",t+=" workerpool.script: `"+n.script+"`\n",t+=" spawnArgs: `"+w.spawnargs+"`\n",t+=" spawnfile: `"+w.spawnfile+"`\n",t+=" stdout: `"+w.stdout+"`\n",t+=" stderr: `"+w.stderr+"`\n",c(new Error(t))})),this.processing=Object.create(null),this.terminating=!1,this.terminated=!1,this.terminationHandler=null,this.lastId=0}w.prototype.methods=function(){return this.exec("methods")},w.prototype.exec=function(r,e,t,o){t||(t=i.defer());var n=++this.lastId;this.processing[n]={id:n,resolver:t,options:o};var s={id:n,method:r,params:e};this.terminated?t.reject(new Error("Worker is terminated")):this.worker.ready?this.worker.send(s):this.requestQueue.push(s);var u=this;return t.promise.catch((function(r){if(r instanceof i.CancellationError||r instanceof i.TimeoutError)return delete u.processing[n],u.terminateAndNotify(!0).then((function(){throw r}),(function(r){throw r}));throw r}))},w.prototype.busy=function(){return Object.keys(this.processing).length>0},w.prototype.terminate=function(r,e){var t=this;if(r){for(var o in this.processing)void 0!==this.processing[o]&&this.processing[o].resolver.reject(new Error("Worker terminated"));this.processing=Object.create(null)}if("function"==typeof e&&(this.terminationHandler=e),this.busy())this.terminating=!0;else{var n=function(r){if(t.terminated=!0,null!=t.worker&&t.worker.removeAllListeners&&t.worker.removeAllListeners("message"),t.worker=null,t.terminating=!1,t.terminationHandler)t.terminationHandler(r,t);else if(r)throw r};if(this.worker){if("function"==typeof this.worker.kill){if(this.worker.killed)return void n(new Error("worker already killed!"));if(this.worker.isChildProcess){var i=setTimeout((function(){t.worker&&t.worker.kill()}),1e3);this.worker.once("exit",(function(){clearTimeout(i),t.worker&&(t.worker.killed=!0),n()})),this.worker.ready?this.worker.send(c):this.requestQueue.push(c)}else this.worker.kill(),this.worker.killed=!0,n();return}if("function"!=typeof this.worker.terminate)throw new Error("Failed to terminate worker");this.worker.terminate(),this.worker.killed=!0}n()}},w.prototype.terminateAndNotify=function(r,e){var t=i.defer();return e&&(t.promise.timeout=e),this.terminate(r,(function(r,e){r?t.reject(r):t.resolve(e)})),t.promise},r.exports=w,r.exports._tryRequireWorkerThreads=p,r.exports._setupProcessWorker=h,r.exports._setupBrowserWorker=d,r.exports._setupWorkerThreadWorker=l,r.exports.ensureWorkerThreads=a},833:function(r){"use strict";function e(){this.ports=Object.create(null),this.length=0}r.exports=e,e.prototype.nextAvailableStartingAt=function(r){for(;!0===this.ports[r];)r++;if(r>=65535)throw new Error("WorkerPool debug port limit reached: "+r+">= 65535");return this.ports[r]=!0,this.length++,r},e.prototype.releasePort=function(r){delete this.ports[r],this.length--}},828:function(r,e,t){var o=t(397),n=function(r){return void 0!==r&&null!=r.versions&&null!=r.versions.node};r.exports.isNode=n,r.exports.platform="undefined"!=typeof process&&n(process)?"node":"browser";var i=function(r){try{return o("worker_threads")}catch(r){return null}}();r.exports.isMainThread="node"===r.exports.platform?(!i||i.isMainThread)&&!process.connected:"undefined"!=typeof Window,r.exports.cpus="browser"===r.exports.platform?self.navigator.hardwareConcurrency:o("os").cpus().length},670:function(r){r.exports='!function(){var __webpack_exports__={};!function(){var exports=__webpack_exports__,__webpack_unused_export__;function _typeof(r){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r})(r)}var requireFoolWebpack=eval("typeof require !== \'undefined\' ? require : function (module) { throw new Error(\'Module \\" + module + \\" not found.\') }"),TERMINATE_METHOD_ID="__workerpool-terminate__",worker={exit:function(){}},WorkerThreads,parentPort;if("undefined"!=typeof self&&"function"==typeof postMessage&&"function"==typeof addEventListener)worker.on=function(r,e){addEventListener(r,function(r){e(r.data)})},worker.send=function(r){postMessage(r)};else{if("undefined"==typeof process)throw new Error("Script must be executed as a worker");try{WorkerThreads=requireFoolWebpack("worker_threads")}catch(error){if("object"!==_typeof(error)||null===error||"MODULE_NOT_FOUND"!==error.code)throw error}WorkerThreads&&null!==WorkerThreads.parentPort?(parentPort=WorkerThreads.parentPort,worker.send=parentPort.postMessage.bind(parentPort),worker.on=parentPort.on.bind(parentPort)):(worker.on=process.on.bind(process),worker.send=process.send.bind(process),worker.on("disconnect",function(){process.exit(1)}),worker.exit=process.exit.bind(process))}function convertError(o){return Object.getOwnPropertyNames(o).reduce(function(r,e){return Object.defineProperty(r,e,{value:o[e],enumerable:!0})},{})}function isPromise(r){return r&&"function"==typeof r.then&&"function"==typeof r.catch}worker.methods={},worker.methods.run=function(r,e){r=new Function("return ("+r+").apply(null, arguments);");return r.apply(r,e)},worker.methods.methods=function(){return Object.keys(worker.methods)};var currentRequestId=null;worker.on("message",function(e){if(e===TERMINATE_METHOD_ID)return worker.exit(0);try{var r=worker.methods[e.method];if(!r)throw new Error(\'Unknown method "\'+e.method+\'"\');currentRequestId=e.id;var o=r.apply(r,e.params);isPromise(o)?o.then(function(r){worker.send({id:e.id,result:r,error:null}),currentRequestId=null}).catch(function(r){worker.send({id:e.id,result:null,error:convertError(r)}),currentRequestId=null}):(worker.send({id:e.id,result:o,error:null}),currentRequestId=null)}catch(r){worker.send({id:e.id,result:null,error:convertError(r)})}}),worker.register=function(r){if(r)for(var e in r)r.hasOwnProperty(e)&&(worker.methods[e]=r[e]);worker.send("ready")},worker.emit=function(r){currentRequestId&&worker.send({id:currentRequestId,isEvent:!0,payload:r})},__webpack_unused_export__=worker.register,worker.emit}()}();'},397:function(module){var requireFoolWebpack=eval("typeof require !== 'undefined' ? require : function (module) { throw new Error('Module \" + module + \" not found.') }");module.exports=requireFoolWebpack},744:function(__unused_webpack_module,exports){function _typeof(r){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r},_typeof(r)}var requireFoolWebpack=eval("typeof require !== 'undefined' ? require : function (module) { throw new Error('Module \" + module + \" not found.') }"),TERMINATE_METHOD_ID="__workerpool-terminate__",worker={exit:function(){}};if("undefined"!=typeof self&&"function"==typeof postMessage&&"function"==typeof addEventListener)worker.on=function(r,e){addEventListener(r,(function(r){e(r.data)}))},worker.send=function(r){postMessage(r)};else{if("undefined"==typeof process)throw new Error("Script must be executed as a worker");var WorkerThreads;try{WorkerThreads=requireFoolWebpack("worker_threads")}catch(r){if("object"!==_typeof(r)||null===r||"MODULE_NOT_FOUND"!==r.code)throw r}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)})),worker.exit=process.exit.bind(process)}function convertError(r){return Object.getOwnPropertyNames(r).reduce((function(e,t){return Object.defineProperty(e,t,{value:r[t],enumerable:!0})}),{})}function isPromise(r){return r&&"function"==typeof r.then&&"function"==typeof r.catch}worker.methods={},worker.methods.run=function(r,e){var t=new Function("return ("+r+").apply(null, arguments);");return t.apply(t,e)},worker.methods.methods=function(){return Object.keys(worker.methods)};var currentRequestId=null;worker.on("message",(function(r){if(r===TERMINATE_METHOD_ID)return worker.exit(0);try{var e=worker.methods[r.method];if(!e)throw new Error('Unknown method "'+r.method+'"');currentRequestId=r.id;var t=e.apply(e,r.params);isPromise(t)?t.then((function(e){worker.send({id:r.id,result:e,error:null}),currentRequestId=null})).catch((function(e){worker.send({id:r.id,result:null,error:convertError(e)}),currentRequestId=null})):(worker.send({id:r.id,result:t,error:null}),currentRequestId=null)}catch(e){worker.send({id:r.id,result:null,error:convertError(e)})}})),worker.register=function(r){if(r)for(var e in r)r.hasOwnProperty(e)&&(worker.methods[e]=r[e]);worker.send("ready")},worker.emit=function(r){currentRequestId&&worker.send({id:currentRequestId,isEvent:!0,payload:r})},exports.add=worker.register,exports.emit=worker.emit}},__webpack_module_cache__={};function __webpack_require__(r){var e=__webpack_module_cache__[r];if(void 0!==e)return e.exports;var t=__webpack_module_cache__[r]={exports:{}};return __webpack_modules__[r](t,t.exports,__webpack_require__),t.exports}var __webpack_exports__={};return function(){var r=__webpack_exports__,e=__webpack_require__(828);r.pool=function(r,e){return new(__webpack_require__(345))(r,e)},r.worker=function(r){__webpack_require__(744).add(r)},r.workerEmit=function(r){__webpack_require__(744).emit(r)},r.Promise=__webpack_require__(219),r.platform=e.platform,r.isMainThread=e.isMainThread,r.cpus=e.cpus}(),__webpack_exports__}()})); | ||
!function(r,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("workerpool",[],e):"object"==typeof exports?exports.workerpool=e():r.workerpool=e()}("undefined"!=typeof self?self:this,(function(){return function(){var __webpack_modules__={345:function(r,e,t){var o=t(219),n=t(751),i=t(828),s=new(t(833));function u(r,e){"string"==typeof r?this.script=r||null:(this.script=null,e=r),this.workers=[],this.tasks=[],e=e||{},this.forkArgs=Object.freeze(e.forkArgs||[]),this.forkOpts=Object.freeze(e.forkOpts||{}),this.workerThreadOpts=Object.freeze(e.workerThreadOpts||{}),this.debugPortStart=e.debugPortStart||43210,this.nodeWorker=e.nodeWorker,this.workerType=e.workerType||e.nodeWorker||"auto",this.maxQueueSize=e.maxQueueSize||1/0,this.onCreateWorker=e.onCreateWorker||function(){return null},this.onTerminateWorker=e.onTerminateWorker||function(){return null},e&&"maxWorkers"in e?(function(r){if(!c(r)||!a(r)||r<1)throw new TypeError("Option maxWorkers must be an integer number >= 1")}(e.maxWorkers),this.maxWorkers=e.maxWorkers):this.maxWorkers=Math.max((i.cpus||4)-1,1),e&&"minWorkers"in e&&("max"===e.minWorkers?this.minWorkers=this.maxWorkers:(function(r){if(!c(r)||!a(r)||r<0)throw new TypeError("Option minWorkers must be an integer number >= 0")}(e.minWorkers),this.minWorkers=e.minWorkers,this.maxWorkers=Math.max(this.minWorkers,this.maxWorkers)),this._ensureMinWorkers()),this._boundNext=this._next.bind(this),"thread"===this.workerType&&n.ensureWorkerThreads()}function c(r){return"number"==typeof r}function a(r){return Math.round(r)==r}u.prototype.exec=function(r,e,t){if(e&&!Array.isArray(e))throw new TypeError('Array expected as argument "params"');if("string"==typeof r){var n=o.defer();if(this.tasks.length>=this.maxQueueSize)throw new Error("Max queue size of "+this.maxQueueSize+" reached");var i=this.tasks,s={method:r,params:e,resolver:n,timeout:null,options:t};i.push(s);var u=n.promise.timeout;return n.promise.timeout=function(r){return-1!==i.indexOf(s)?(s.timeout=r,n.promise):u.call(n.promise,r)},this._next(),n.promise}if("function"==typeof r)return this.exec("run",[String(r),e]);throw new TypeError('Function or string expected as argument "method"')},u.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}))},u.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,t.options).then(e._boundNext).catch((function(){if(r.terminated)return e._removeWorker(r)})).then((function(){e._next()}));"number"==typeof t.timeout&&o.timeout(t.timeout)}else e._next()}}},u.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=this._createWorkerHandler(),r.push(t),t):null},u.prototype._removeWorker=function(r){var e=this;return s.releasePort(r.debugPort),this._removeWorkerFromList(r),this._ensureMinWorkers(),new o((function(t,o){r.terminate(!1,(function(n){e.onTerminateWorker({forkArgs:r.forkArgs,forkOpts:r.forkOpts,script:r.script}),n?o(n):t(r)}))}))},u.prototype._removeWorkerFromList=function(r){var e=this.workers.indexOf(r);-1!==e&&this.workers.splice(e,1)},u.prototype.terminate=function(r,e){var t=this;this.tasks.forEach((function(r){r.resolver.reject(new Error("Pool terminated"))})),this.tasks.length=0;var n=function(r){this._removeWorkerFromList(r)}.bind(this),i=[];return this.workers.slice().forEach((function(o){var s=o.terminateAndNotify(r,e).then(n).always((function(){t.onTerminateWorker({forkArgs:o.forkArgs,forkOpts:o.forkOpts,script:o.script})}));i.push(s)})),o.all(i)},u.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}},u.prototype._ensureMinWorkers=function(){if(this.minWorkers)for(var r=this.workers.length;r<this.minWorkers;r++)this.workers.push(this._createWorkerHandler())},u.prototype._createWorkerHandler=function(){var r=this.onCreateWorker({forkArgs:this.forkArgs,forkOpts:this.forkOpts,workerThreadOpts:this.workerThreadOpts,script:this.script})||{};return new n(r.script||this.script,{forkArgs:r.forkArgs||this.forkArgs,forkOpts:r.forkOpts||this.forkOpts,workerThreadOpts:r.workerThreadOpts||this.workerThreadOpts,debugPort:s.nextAvailableStartingAt(this.debugPortStart),workerType:this.workerType})},r.exports=u},219:function(r){"use strict";function e(r,i){var s=this;if(!(this instanceof e))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,o){return new e((function(e,n){var i=r?t(r,e,n):e,s=o?t(o,e,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 i?i.cancel():p(new o),s},this.timeout=function(r){if(i)i.timeout(r);else{var e=setTimeout((function(){p(new n("Promise timed out after "+r+" ms"))}),r);s.always((function(){clearTimeout(e)}))}return s},r((function(r){f(r)}),(function(r){p(r)}))}function t(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 o(r){this.message=r||"promise cancelled",this.stack=(new Error).stack}function n(r){this.message=r||"timeout exceeded",this.stack=(new Error).stack}e.prototype.catch=function(r){return this.then(null,r)},e.prototype.always=function(r){return this.then(r,r)},e.all=function(r){return new e((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)}))},e.defer=function(){var r={};return r.promise=new e((function(e,t){r.resolve=e,r.reject=t})),r},o.prototype=new Error,o.prototype.constructor=Error,o.prototype.name="CancellationError",e.CancellationError=o,n.prototype=new Error,n.prototype.constructor=Error,n.prototype.name="TimeoutError",e.TimeoutError=n,r.exports=e},751:function(r,e,t){"use strict";function o(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,o=new Array(e);t<e;t++)o[t]=r[t];return o}function n(r,e){var t=Object.keys(r);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(r);e&&(o=o.filter((function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))),t.push.apply(t,o)}return t}function i(r,e,t){return e in r?Object.defineProperty(r,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[e]=t,r}function s(r){return s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r},s(r)}var u=t(219),c=t(828),a=t(397),f="__workerpool-terminate__";function p(){var r=l();if(!r)throw new Error("WorkerPool: workerType = 'thread' is not supported, Node >= 11.7.0 required");return r}function d(){if("function"!=typeof Worker&&("object"!==("undefined"==typeof Worker?"undefined":s(Worker))||"function"!=typeof Worker.prototype.constructor))throw new Error("WorkerPool: Web Workers not supported")}function l(){try{return a("worker_threads")}catch(r){if("object"===s(r)&&null!==r&&"MODULE_NOT_FOUND"===r.code)return null;throw r}}function h(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 k(r,e,t){var o=new e.Worker(r,function(r){for(var e=1;e<arguments.length;e++){var t=null!=arguments[e]?arguments[e]:{};e%2?n(Object(t),!0).forEach((function(e){i(r,e,t[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(t)):n(Object(t)).forEach((function(e){Object.defineProperty(r,e,Object.getOwnPropertyDescriptor(t,e))}))}return r}({stdout:!1,stderr:!1},t));return o.isWorkerThread=!0,o.send=function(r){this.postMessage(r)},o.kill=function(){return this.terminate(),!0},o.disconnect=function(){this.terminate()},o}function w(r,e,t){var o=t.fork(r,e.forkArgs,e.forkOpts);return o.isChildProcess=!0,o}function m(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")),process.execArgv.forEach((function(r){r.indexOf("--max-old-space-size")>-1&&n.push(r)})),Object.assign({},r,{forkArgs:r.forkArgs,forkOpts:Object.assign({},r.forkOpts,{execArgv:(r.forkOpts&&r.forkOpts.execArgv||[]).concat(n)})})}function y(r,e){var n=this,i=e||{};function s(r){for(var e in n.terminated=!0,n.processing)void 0!==n.processing[e]&&n.processing[e].resolver.reject(r);n.processing=Object.create(null)}this.script=r||function(){if("browser"===c.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(670)],{type:"text/javascript"});return window.URL.createObjectURL(r)}return __dirname+"/worker.js"}(),this.worker=function(r,e){if("web"===e.workerType)return d(),h(r,Worker);if("thread"===e.workerType)return k(r,t=p(),e.workerThreadOpts);if("process"!==e.workerType&&e.workerType){if("browser"===c.platform)return d(),h(r,Worker);var t=l();return t?k(r,t):w(r,m(e),a("child_process"))}return w(r,m(e),a("child_process"))}(this.script,i),this.debugPort=i.debugPort,this.forkOpts=i.forkOpts,this.forkArgs=i.forkArgs,this.workerThreadOpts=i.workerThreadOpts,r||(this.worker.ready=!0),this.requestQueue=[],this.worker.on("message",(function(r){if(!n.terminated)if("string"==typeof r&&"ready"===r)n.worker.ready=!0,function(){var r,e=function(r,e){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(!t){if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return o(r,e);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?o(r,e):void 0}}(r))||e&&r&&"number"==typeof r.length){t&&(r=t);var n=0,i=function(){};return{s:i,n:function(){return n>=r.length?{done:!0}:{done:!1,value:r[n++]}},e:function(r){throw r},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,u=!0,c=!1;return{s:function(){t=t.call(r)},n:function(){var r=t.next();return u=r.done,r},e:function(r){c=!0,s=r},f:function(){try{u||null==t.return||t.return()}finally{if(c)throw s}}}}(n.requestQueue.splice(0));try{for(e.s();!(r=e.n()).done;){var t=r.value;n.worker.send(t)}}catch(r){e.e(r)}finally{e.f()}}();else{var e=r.id,t=n.processing[e];void 0!==t&&(r.isEvent?t.options&&"function"==typeof t.options.on&&t.options.on(r.payload):(delete n.processing[e],!0===n.terminating&&n.terminate(),r.error?t.resolver.reject(function(r){for(var e=new Error(""),t=Object.keys(r),o=0;o<t.length;o++)e[t[o]]=r[t[o]];return e}(r.error)):t.resolver.resolve(r.result)))}}));var u=this.worker;this.worker.on("error",s),this.worker.on("exit",(function(r,e){var t="Workerpool Worker terminated Unexpectedly\n";t+=" exitCode: `"+r+"`\n",t+=" signalCode: `"+e+"`\n",t+=" workerpool.script: `"+n.script+"`\n",t+=" spawnArgs: `"+u.spawnargs+"`\n",t+=" spawnfile: `"+u.spawnfile+"`\n",t+=" stdout: `"+u.stdout+"`\n",t+=" stderr: `"+u.stderr+"`\n",s(new Error(t))})),this.processing=Object.create(null),this.terminating=!1,this.terminated=!1,this.terminationHandler=null,this.lastId=0}y.prototype.methods=function(){return this.exec("methods")},y.prototype.exec=function(r,e,t,o){t||(t=u.defer());var n=++this.lastId;this.processing[n]={id:n,resolver:t,options:o};var i={id:n,method:r,params:e};this.terminated?t.reject(new Error("Worker is terminated")):this.worker.ready?this.worker.send(i):this.requestQueue.push(i);var s=this;return t.promise.catch((function(r){if(r instanceof u.CancellationError||r instanceof u.TimeoutError)return delete s.processing[n],s.terminateAndNotify(!0).then((function(){throw r}),(function(r){throw r}));throw r}))},y.prototype.busy=function(){return Object.keys(this.processing).length>0},y.prototype.terminate=function(r,e){var t=this;if(r){for(var o in this.processing)void 0!==this.processing[o]&&this.processing[o].resolver.reject(new Error("Worker terminated"));this.processing=Object.create(null)}if("function"==typeof e&&(this.terminationHandler=e),this.busy())this.terminating=!0;else{var n=function(r){if(t.terminated=!0,null!=t.worker&&t.worker.removeAllListeners&&t.worker.removeAllListeners("message"),t.worker=null,t.terminating=!1,t.terminationHandler)t.terminationHandler(r,t);else if(r)throw r};if(this.worker){if("function"==typeof this.worker.kill){if(this.worker.killed)return void n(new Error("worker already killed!"));if(this.worker.isChildProcess){var i=setTimeout((function(){t.worker&&t.worker.kill()}),1e3);this.worker.once("exit",(function(){clearTimeout(i),t.worker&&(t.worker.killed=!0),n()})),this.worker.ready?this.worker.send(f):this.requestQueue.push(f)}else this.worker.kill(),this.worker.killed=!0,n();return}if("function"!=typeof this.worker.terminate)throw new Error("Failed to terminate worker");this.worker.terminate(),this.worker.killed=!0}n()}},y.prototype.terminateAndNotify=function(r,e){var t=u.defer();return e&&(t.promise.timeout=e),this.terminate(r,(function(r,e){r?t.reject(r):t.resolve(e)})),t.promise},r.exports=y,r.exports._tryRequireWorkerThreads=l,r.exports._setupProcessWorker=w,r.exports._setupBrowserWorker=h,r.exports._setupWorkerThreadWorker=k,r.exports.ensureWorkerThreads=p},833:function(r){"use strict";function e(){this.ports=Object.create(null),this.length=0}r.exports=e,e.prototype.nextAvailableStartingAt=function(r){for(;!0===this.ports[r];)r++;if(r>=65535)throw new Error("WorkerPool debug port limit reached: "+r+">= 65535");return this.ports[r]=!0,this.length++,r},e.prototype.releasePort=function(r){delete this.ports[r],this.length--}},828:function(r,e,t){var o=t(397),n=function(r){return void 0!==r&&null!=r.versions&&null!=r.versions.node};r.exports.isNode=n,r.exports.platform="undefined"!=typeof process&&n(process)?"node":"browser";var i=function(r){try{return o("worker_threads")}catch(r){return null}}();r.exports.isMainThread="node"===r.exports.platform?(!i||i.isMainThread)&&!process.connected:"undefined"!=typeof Window,r.exports.cpus="browser"===r.exports.platform?self.navigator.hardwareConcurrency:o("os").cpus().length},670:function(r){r.exports='!function(){var __webpack_exports__={};!function(){var exports=__webpack_exports__,__webpack_unused_export__;function _typeof(r){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r})(r)}var requireFoolWebpack=eval("typeof require !== \'undefined\' ? require : function (module) { throw new Error(\'Module \\" + module + \\" not found.\') }"),TERMINATE_METHOD_ID="__workerpool-terminate__",worker={exit:function(){}},WorkerThreads,parentPort;if("undefined"!=typeof self&&"function"==typeof postMessage&&"function"==typeof addEventListener)worker.on=function(r,e){addEventListener(r,function(r){e(r.data)})},worker.send=function(r){postMessage(r)};else{if("undefined"==typeof process)throw new Error("Script must be executed as a worker");try{WorkerThreads=requireFoolWebpack("worker_threads")}catch(error){if("object"!==_typeof(error)||null===error||"MODULE_NOT_FOUND"!==error.code)throw error}WorkerThreads&&null!==WorkerThreads.parentPort?(parentPort=WorkerThreads.parentPort,worker.send=parentPort.postMessage.bind(parentPort),worker.on=parentPort.on.bind(parentPort)):(worker.on=process.on.bind(process),worker.send=process.send.bind(process),worker.on("disconnect",function(){process.exit(1)}),worker.exit=process.exit.bind(process))}function convertError(o){return Object.getOwnPropertyNames(o).reduce(function(r,e){return Object.defineProperty(r,e,{value:o[e],enumerable:!0})},{})}function isPromise(r){return r&&"function"==typeof r.then&&"function"==typeof r.catch}worker.methods={},worker.methods.run=function(r,e){r=new Function("return ("+r+").apply(null, arguments);");return r.apply(r,e)},worker.methods.methods=function(){return Object.keys(worker.methods)};var currentRequestId=null;worker.on("message",function(e){if(e===TERMINATE_METHOD_ID)return worker.exit(0);try{var r=worker.methods[e.method];if(!r)throw new Error(\'Unknown method "\'+e.method+\'"\');currentRequestId=e.id;var o=r.apply(r,e.params);isPromise(o)?o.then(function(r){worker.send({id:e.id,result:r,error:null}),currentRequestId=null}).catch(function(r){worker.send({id:e.id,result:null,error:convertError(r)}),currentRequestId=null}):(worker.send({id:e.id,result:o,error:null}),currentRequestId=null)}catch(r){worker.send({id:e.id,result:null,error:convertError(r)})}}),worker.register=function(r){if(r)for(var e in r)r.hasOwnProperty(e)&&(worker.methods[e]=r[e]);worker.send("ready")},worker.emit=function(r){currentRequestId&&worker.send({id:currentRequestId,isEvent:!0,payload:r})},__webpack_unused_export__=worker.register,worker.emit}()}();'},397:function(module){var requireFoolWebpack=eval("typeof require !== 'undefined' ? require : function (module) { throw new Error('Module \" + module + \" not found.') }");module.exports=requireFoolWebpack},744:function(__unused_webpack_module,exports){function _typeof(r){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r},_typeof(r)}var requireFoolWebpack=eval("typeof require !== 'undefined' ? require : function (module) { throw new Error('Module \" + module + \" not found.') }"),TERMINATE_METHOD_ID="__workerpool-terminate__",worker={exit:function(){}};if("undefined"!=typeof self&&"function"==typeof postMessage&&"function"==typeof addEventListener)worker.on=function(r,e){addEventListener(r,(function(r){e(r.data)}))},worker.send=function(r){postMessage(r)};else{if("undefined"==typeof process)throw new Error("Script must be executed as a worker");var WorkerThreads;try{WorkerThreads=requireFoolWebpack("worker_threads")}catch(r){if("object"!==_typeof(r)||null===r||"MODULE_NOT_FOUND"!==r.code)throw r}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)})),worker.exit=process.exit.bind(process)}function convertError(r){return Object.getOwnPropertyNames(r).reduce((function(e,t){return Object.defineProperty(e,t,{value:r[t],enumerable:!0})}),{})}function isPromise(r){return r&&"function"==typeof r.then&&"function"==typeof r.catch}worker.methods={},worker.methods.run=function(r,e){var t=new Function("return ("+r+").apply(null, arguments);");return t.apply(t,e)},worker.methods.methods=function(){return Object.keys(worker.methods)};var currentRequestId=null;worker.on("message",(function(r){if(r===TERMINATE_METHOD_ID)return worker.exit(0);try{var e=worker.methods[r.method];if(!e)throw new Error('Unknown method "'+r.method+'"');currentRequestId=r.id;var t=e.apply(e,r.params);isPromise(t)?t.then((function(e){worker.send({id:r.id,result:e,error:null}),currentRequestId=null})).catch((function(e){worker.send({id:r.id,result:null,error:convertError(e)}),currentRequestId=null})):(worker.send({id:r.id,result:t,error:null}),currentRequestId=null)}catch(e){worker.send({id:r.id,result:null,error:convertError(e)})}})),worker.register=function(r){if(r)for(var e in r)r.hasOwnProperty(e)&&(worker.methods[e]=r[e]);worker.send("ready")},worker.emit=function(r){currentRequestId&&worker.send({id:currentRequestId,isEvent:!0,payload:r})},exports.add=worker.register,exports.emit=worker.emit}},__webpack_module_cache__={};function __webpack_require__(r){var e=__webpack_module_cache__[r];if(void 0!==e)return e.exports;var t=__webpack_module_cache__[r]={exports:{}};return __webpack_modules__[r](t,t.exports,__webpack_require__),t.exports}var __webpack_exports__={};return function(){var r=__webpack_exports__,e=__webpack_require__(828);r.pool=function(r,e){return new(__webpack_require__(345))(r,e)},r.worker=function(r){__webpack_require__(744).add(r)},r.workerEmit=function(r){__webpack_require__(744).emit(r)},r.Promise=__webpack_require__(219),r.platform=e.platform,r.isMainThread=e.isMainThread,r.cpus=e.cpus}(),__webpack_exports__}()})); | ||
//# sourceMappingURL=workerpool.min.js.map |
@@ -7,4 +7,4 @@ /** | ||
* | ||
* @version 6.2.1 | ||
* @date 2022-04-11 | ||
* @version 6.3.0 | ||
* @date 2022-10-24 | ||
* | ||
@@ -11,0 +11,0 @@ * @license |
@@ -5,2 +5,7 @@ # workerpool history | ||
## 2022-10-24, version 6.3.0 | ||
- Implement option `workerThreadOpts` to pass options to a worker of type | ||
`thread`, a `worker_thread` (#357, fixes #356). Thanks @galElmalah. | ||
## 2022-04-11, version 6.2.1 | ||
@@ -7,0 +12,0 @@ |
{ | ||
"name": "workerpool", | ||
"license": "Apache-2.0", | ||
"version": "6.2.1", | ||
"version": "6.3.0", | ||
"description": "Offload tasks to a pool of workers on node.js and in the browser", | ||
@@ -37,7 +37,7 @@ "homepage": "https://github.com/josdejong/workerpool", | ||
"devDependencies": { | ||
"@babel/core": "7.17.9", | ||
"@babel/preset-env": "7.16.11", | ||
"babel-loader": "8.2.4", | ||
"date-format": "4.0.6", | ||
"del": "6.0.0", | ||
"@babel/core": "7.19.6", | ||
"@babel/preset-env": "7.19.4", | ||
"babel-loader": "8.2.5", | ||
"date-format": "4.0.14", | ||
"del": "6.1.1", | ||
"fancy-log": "2.0.0", | ||
@@ -49,6 +49,6 @@ "find-process": "1.4.7", | ||
"mocha": "9.2.2", | ||
"uglify-js": "3.15.4", | ||
"webpack": "5.72.0" | ||
"uglify-js": "3.17.4", | ||
"webpack": "5.74.0" | ||
}, | ||
"dependencies": {} | ||
} |
204
README.md
# workerpool | ||
**workerpool** offers an easy way to create a pool of workers for both dynamically offloading computations as well as managing a pool of dedicated workers. **workerpool** basically implements a [thread pool pattern](http://en.wikipedia.org/wiki/Thread_pool_pattern). There is a pool of workers to execute tasks. New tasks are put in a queue. A worker executes one task at a time, and once finished, picks a new task from the queue. Workers can be accessed via a natural, promise based proxy, as if they are available straight in the main application. | ||
@@ -9,3 +7,2 @@ | ||
## Features | ||
@@ -22,3 +19,2 @@ | ||
## Why | ||
@@ -35,5 +31,4 @@ | ||
For front-end processes, this is not a desired situation. | ||
Therefore, CPU intensive tasks should be offloaded from the main event loop onto dedicated *workers*. In a browser environment, [Web Workers](http://www.html5rocks.com/en/tutorials/workers/basics/) can be used. In node.js, [child processes](https://nodejs.org/api/child_process.html) and [worker_threads](https://nodejs.org/api/worker_threads.html) are available. An application should be split in separate, decoupled parts, which can run independent of each other in a parallelized way. Effectively, this results in an architecture which achieves concurrency by means of isolated processes and message passing. | ||
Therefore, CPU intensive tasks should be offloaded from the main event loop onto dedicated _workers_. In a browser environment, [Web Workers](http://www.html5rocks.com/en/tutorials/workers/basics/) can be used. In node.js, [child processes](https://nodejs.org/api/child_process.html) and [worker_threads](https://nodejs.org/api/worker_threads.html) are available. An application should be split in separate, decoupled parts, which can run independent of each other in a parallelized way. Effectively, this results in an architecture which achieves concurrency by means of isolated processes and message passing. | ||
## Install | ||
@@ -45,3 +40,2 @@ | ||
## Load | ||
@@ -67,3 +61,2 @@ | ||
## Use | ||
@@ -76,2 +69,3 @@ | ||
**myApp.js** | ||
```js | ||
@@ -85,12 +79,13 @@ const workerpool = require('workerpool'); | ||
pool.exec(add, [3, 4]) | ||
.then(function (result) { | ||
console.log('result', result); // outputs 7 | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
}) | ||
.then(function () { | ||
pool.terminate(); // terminate all workers when done | ||
}); | ||
pool | ||
.exec(add, [3, 4]) | ||
.then(function (result) { | ||
console.log('result', result); // outputs 7 | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
}) | ||
.then(function () { | ||
pool.terminate(); // terminate all workers when done | ||
}); | ||
``` | ||
@@ -100,3 +95,2 @@ | ||
### Dedicated workers | ||
@@ -107,2 +101,3 @@ | ||
**myWorker.js** | ||
```js | ||
@@ -119,3 +114,3 @@ const workerpool = require('workerpool'); | ||
workerpool.worker({ | ||
fibonacci: fibonacci | ||
fibonacci: fibonacci, | ||
}); | ||
@@ -127,2 +122,3 @@ ``` | ||
**myApp.js** | ||
```js | ||
@@ -135,27 +131,29 @@ const workerpool = require('workerpool'); | ||
// run registered functions on the worker via exec | ||
pool.exec('fibonacci', [10]) | ||
.then(function (result) { | ||
console.log('Result: ' + result); // outputs 55 | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
}) | ||
.then(function () { | ||
pool.terminate(); // terminate all workers when done | ||
}); | ||
pool | ||
.exec('fibonacci', [10]) | ||
.then(function (result) { | ||
console.log('Result: ' + result); // outputs 55 | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
}) | ||
.then(function () { | ||
pool.terminate(); // terminate all workers when done | ||
}); | ||
// or run registered functions on the worker via a proxy: | ||
pool.proxy() | ||
.then(function (worker) { | ||
return worker.fibonacci(10); | ||
}) | ||
.then(function (result) { | ||
console.log('Result: ' + result); // outputs 55 | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
}) | ||
.then(function () { | ||
pool.terminate(); // terminate all workers when done | ||
}); | ||
pool | ||
.proxy() | ||
.then(function (worker) { | ||
return worker.fibonacci(10); | ||
}) | ||
.then(function (result) { | ||
console.log('Result: ' + result); // outputs 55 | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
}) | ||
.then(function () { | ||
pool.terminate(); // terminate all workers when done | ||
}); | ||
``` | ||
@@ -166,5 +164,5 @@ | ||
**myAsyncWorker.js** | ||
```js | ||
define(['workerpool/dist/workerpool'], function(workerpool) { | ||
define(['workerpool/dist/workerpool'], function (workerpool) { | ||
// a deliberately inefficient implementation of the fibonacci sequence | ||
@@ -178,5 +176,4 @@ function fibonacci(n) { | ||
workerpool.worker({ | ||
fibonacci: fibonacci | ||
fibonacci: fibonacci, | ||
}); | ||
}); | ||
@@ -191,3 +188,2 @@ ``` | ||
## API | ||
@@ -217,2 +213,3 @@ | ||
- `forkOpts: Object`. For `process` worker type. An object passed as `options` to [child_process.fork](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options). See nodejs documentation for available options. | ||
- `workerThreadOpts: Object`. For `worker` worker type. An object passed to [worker_threads.options](https://nodejs.org/api/worker_threads.html#new-workerfilename-options). See nodejs documentation for available options. | ||
- `onCreateWorker: Function`. A callback that is called whenever a worker is being created. It can be used to allocate resources for each worker for example. The callback is passed as argument an object with the following properties: | ||
@@ -222,4 +219,4 @@ - `forkArgs: String[]`: the `forkArgs` option of this pool | ||
- `script: string`: the `script` option of this pool | ||
Optionally, this callback can return an object containing one or more of the above properties. The provided properties will be used to override the Pool properties for the worker being created. | ||
- `onTerminateWorker: Function`. A callback that is called whenever a worker is being terminated. It can be used to release resources that might have been allocated for this specific worker. The callback is passed as argument an object as described for `onCreateWorker`, with each property sets with the value for the worker being terminated. | ||
Optionally, this callback can return an object containing one or more of the above properties. The provided properties will be used to override the Pool properties for the worker being created. | ||
- `onTerminateWorker: Function`. A callback that is called whenever a worker is being terminated. It can be used to release resources that might have been allocated for this specific worker. The callback is passed as argument an object as described for `onCreateWorker`, with each property sets with the value for the worker being terminated. | ||
@@ -232,2 +229,3 @@ > Important note on `'workerType'`: when sending and receiving primitive data types (plain JSON) from and to a worker, the different worker types (`'web'`, `'process'`, `'thread'`) can be used interchangeably. However, when using more advanced data types like buffers, the API and returned results can vary. In these cases, it is best not to use the `'auto'` setting but have a fixed `'workerType'` and good unit testing in place. | ||
Execute a function on a worker with given arguments. | ||
- When `method` is a string, a method with this name must exist at the worker and must be registered to make it accessible via the pool. The function will be executed on the worker with given parameters. | ||
@@ -242,15 +240,15 @@ - When `method` is a function, the provided function `fn` will be stringified, send to the worker, and executed there with the provided parameters. The provided function must be static, it must not depend on variables in a surrounding scope. | ||
- `Pool.stats() : Object`<br> | ||
Retrieve statistics on workers, and active and pending tasks. | ||
Retrieve statistics on workers, and active and pending tasks. | ||
Returns an object containing the following properties: | ||
Returns an object containing the following properties: | ||
``` | ||
{ | ||
totalWorkers: 0, | ||
busyWorkers: 0, | ||
idleWorkers: 0, | ||
pendingTasks: 0, | ||
activeTasks: 0 | ||
} | ||
``` | ||
``` | ||
{ | ||
totalWorkers: 0, | ||
busyWorkers: 0, | ||
idleWorkers: 0, | ||
pendingTasks: 0, | ||
activeTasks: 0 | ||
} | ||
``` | ||
@@ -287,9 +285,10 @@ - `Pool.terminate([force: boolean [, timeout: number]])` | ||
// offload a function to a worker | ||
pool1.exec(add, [2, 4]) | ||
.then(function (result) { | ||
console.log(result); // will output 6 | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
}); | ||
pool1 | ||
.exec(add, [2, 4]) | ||
.then(function (result) { | ||
console.log(result); // will output 6 | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
}); | ||
@@ -300,27 +299,28 @@ // create a dedicated worker | ||
// supposed myWorker.js contains a function 'fibonacci' | ||
pool2.exec('fibonacci', [10]) | ||
.then(function (result) { | ||
console.log(result); // will output 55 | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
}); | ||
pool2 | ||
.exec('fibonacci', [10]) | ||
.then(function (result) { | ||
console.log(result); // will output 55 | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
}); | ||
// create a proxy to myWorker.js | ||
pool2.proxy() | ||
.then(function (myWorker) { | ||
return myWorker.fibonacci(10) | ||
}) | ||
.then(function (result) { | ||
console.log(result); // will output 55 | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
}); | ||
pool2 | ||
.proxy() | ||
.then(function (myWorker) { | ||
return myWorker.fibonacci(10); | ||
}) | ||
.then(function (result) { | ||
console.log(result); // will output 55 | ||
}) | ||
.catch(function (err) { | ||
console.error(err); | ||
}); | ||
// create a pool with a specified maximum number of workers | ||
const pool3 = workerpool.pool({maxWorkers: 7}); | ||
const pool3 = workerpool.pool({ maxWorkers: 7 }); | ||
``` | ||
### worker | ||
@@ -351,3 +351,3 @@ | ||
add: add, | ||
multiply: multiply | ||
multiply: multiply, | ||
}); | ||
@@ -364,3 +364,3 @@ ``` | ||
return new Promise(function (resolve, reject) { | ||
setTimeout(resolve, delay) | ||
setTimeout(resolve, delay); | ||
}); | ||
@@ -371,3 +371,3 @@ } | ||
workerpool.worker({ | ||
timeout: timeout | ||
timeout: timeout, | ||
}); | ||
@@ -392,9 +392,9 @@ ``` | ||
workerpool.workerEmit({ | ||
status: 'in_progress' | ||
status: 'in_progress', | ||
}); | ||
workerpool.workerEmit({ | ||
status: 'complete' | ||
status: 'complete', | ||
}); | ||
return true; | ||
@@ -405,3 +405,3 @@ } | ||
workerpool.worker({ | ||
eventExample: eventExample | ||
eventExample: eventExample, | ||
}); | ||
@@ -420,4 +420,4 @@ ``` | ||
} | ||
} | ||
}) | ||
}, | ||
}); | ||
``` | ||
@@ -429,7 +429,6 @@ | ||
- **platform**: The Javascript platform. Either *node* or *browser* | ||
- **platform**: The Javascript platform. Either _node_ or _browser_ | ||
- **isMainThread**: Whether the code is running in main thread or not (Workers) | ||
- **cpus**: The number of CPUs/cores available | ||
## Roadmap | ||
@@ -444,3 +443,2 @@ | ||
## Related libraries | ||
@@ -459,3 +457,2 @@ | ||
## Build | ||
@@ -479,3 +476,2 @@ | ||
## Test | ||
@@ -499,3 +495,2 @@ | ||
## Publish | ||
@@ -505,3 +500,3 @@ | ||
- Update version in package.json, run `npm install` to update it in `package-lock.json` too. | ||
- Push to github | ||
- Push to Github | ||
- Deploy to npm via `npm publish` | ||
@@ -514,3 +509,2 @@ - Add a git tag with the version number like: | ||
## License | ||
@@ -524,3 +518,3 @@ | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
@@ -527,0 +521,0 @@ Unless required by applicable law or agreed to in writing, software |
@@ -28,2 +28,3 @@ var Promise = require('./Promise'); | ||
this.forkOpts = Object.freeze(options.forkOpts || {}); | ||
this.workerThreadOpts = Object.freeze(options.workerThreadOpts || {}) | ||
this.debugPortStart = (options.debugPortStart || 43210); | ||
@@ -391,2 +392,3 @@ this.nodeWorker = options.nodeWorker; | ||
forkOpts: this.forkOpts, | ||
workerThreadOpts: this.workerThreadOpts, | ||
script: this.script | ||
@@ -398,2 +400,3 @@ }) || {}; | ||
forkOpts: overridenParams.forkOpts || this.forkOpts, | ||
workerThreadOpts: overridenParams.workerThreadOpts || this.workerThreadOpts, | ||
debugPort: DEBUG_PORT_ALLOCATOR.nextAvailableStartingAt(this.debugPortStart), | ||
@@ -400,0 +403,0 @@ workerType: this.workerType |
@@ -76,3 +76,3 @@ 'use strict'; | ||
WorkerThreads = ensureWorkerThreads(); | ||
return setupWorkerThreadWorker(script, WorkerThreads); | ||
return setupWorkerThreadWorker(script, WorkerThreads, options.workerThreadOpts); | ||
} else if (options.workerType === 'process' || !options.workerType) { // node.js only | ||
@@ -113,6 +113,7 @@ return setupProcessWorker(script, resolveForkOptions(options), requireFoolWebpack('child_process')); | ||
function setupWorkerThreadWorker(script, WorkerThreads) { | ||
function setupWorkerThreadWorker(script, WorkerThreads, workerThreadOptions) { | ||
var worker = new WorkerThreads.Worker(script, { | ||
stdout: false, // automatically pipe worker.STDOUT to process.STDOUT | ||
stderr: false // automatically pipe worker.STDERR to process.STDERR | ||
stderr: false, // automatically pipe worker.STDERR to process.STDERR | ||
...workerThreadOptions | ||
}); | ||
@@ -214,2 +215,3 @@ worker.isWorkerThread = true; | ||
this.forkArgs = options.forkArgs; | ||
this.workerThreadOpts = options.workerThreadOpts | ||
@@ -216,0 +218,0 @@ // The ready message is only sent if the worker.add method is called (And the default script is not used) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
332914
3137
491