workerpool.js 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  1. /**
  2. * workerpool.js
  3. * https://github.com/josdejong/workerpool
  4. *
  5. * Offload tasks to a pool of workers on node.js and in the browser.
  6. *
  7. * @version 9.3.4
  8. * @date 2025-09-10
  9. *
  10. * @license
  11. * Copyright (C) 2014-2022 Jos de Jong <wjosdejong@gmail.com>
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  14. * use this file except in compliance with the License. You may obtain a copy
  15. * of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  22. * License for the specific language governing permissions and limitations under
  23. * the License.
  24. */
  25. (function (global, factory) {
  26. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  27. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  28. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.workerpool = {}));
  29. })(this, (function (exports) { 'use strict';
  30. var src = {};
  31. var environment = {exports: {}};
  32. (function (module) {
  33. // source: https://github.com/flexdinesh/browser-or-node
  34. // source: https://github.com/mozilla/pdf.js/blob/7ea0e40e588864cd938d1836ec61f1928d3877d3/src/shared/util.js#L24
  35. var isNode = function isNode(nodeProcess) {
  36. return typeof nodeProcess !== 'undefined' && nodeProcess.versions != null && nodeProcess.versions.node != null && nodeProcess + '' === '[object process]';
  37. };
  38. module.exports.isNode = isNode;
  39. // determines the JavaScript platform: browser or node
  40. module.exports.platform = typeof process !== 'undefined' && isNode(process) ? 'node' : 'browser';
  41. // determines whether the code is running in main thread or not
  42. // note that in node.js we have to check both worker_thread and child_process
  43. var worker_threads = module.exports.platform === 'node' && require('worker_threads');
  44. module.exports.isMainThread = module.exports.platform === 'node' ? (!worker_threads || worker_threads.isMainThread) && !process.connected : typeof Window !== 'undefined';
  45. // determines the number of cpus available
  46. module.exports.cpus = module.exports.platform === 'browser' ? self.navigator.hardwareConcurrency : require('os').cpus().length;
  47. })(environment);
  48. var environmentExports = environment.exports;
  49. var _Promise$1 = {};
  50. var hasRequired_Promise;
  51. function require_Promise() {
  52. if (hasRequired_Promise) return _Promise$1;
  53. hasRequired_Promise = 1;
  54. /**
  55. * Promise
  56. *
  57. * Inspired by https://gist.github.com/RubaXa/8501359 from RubaXa <trash@rubaxa.org>
  58. * @template T
  59. * @template [E=Error]
  60. * @param {Function} handler Called as handler(resolve: Function, reject: Function)
  61. * @param {Promise} [parent] Parent promise for propagation of cancel and timeout
  62. */
  63. function Promise(handler, parent) {
  64. var me = this;
  65. if (!(this instanceof Promise)) {
  66. throw new SyntaxError('Constructor must be called with the new operator');
  67. }
  68. if (typeof handler !== 'function') {
  69. throw new SyntaxError('Function parameter handler(resolve, reject) missing');
  70. }
  71. var _onSuccess = [];
  72. var _onFail = [];
  73. // status
  74. /**
  75. * @readonly
  76. */
  77. this.resolved = false;
  78. /**
  79. * @readonly
  80. */
  81. this.rejected = false;
  82. /**
  83. * @readonly
  84. */
  85. this.pending = true;
  86. /**
  87. * @readonly
  88. */
  89. this[Symbol.toStringTag] = 'Promise';
  90. /**
  91. * Process onSuccess and onFail callbacks: add them to the queue.
  92. * Once the promise is resolved, the function _promise is replace.
  93. * @param {Function} onSuccess
  94. * @param {Function} onFail
  95. * @private
  96. */
  97. var _process = function _process(onSuccess, onFail) {
  98. _onSuccess.push(onSuccess);
  99. _onFail.push(onFail);
  100. };
  101. /**
  102. * Add an onSuccess callback and optionally an onFail callback to the Promise
  103. * @template TT
  104. * @template [TE=never]
  105. * @param {(r: T) => TT | PromiseLike<TT>} onSuccess
  106. * @param {(r: E) => TE | PromiseLike<TE>} [onFail]
  107. * @returns {Promise<TT | TE, any>} promise
  108. */
  109. this.then = function (onSuccess, onFail) {
  110. return new Promise(function (resolve, reject) {
  111. var s = onSuccess ? _then(onSuccess, resolve, reject) : resolve;
  112. var f = onFail ? _then(onFail, resolve, reject) : reject;
  113. _process(s, f);
  114. }, me);
  115. };
  116. /**
  117. * Resolve the promise
  118. * @param {*} result
  119. * @type {Function}
  120. */
  121. var _resolve2 = function _resolve(result) {
  122. // update status
  123. me.resolved = true;
  124. me.rejected = false;
  125. me.pending = false;
  126. _onSuccess.forEach(function (fn) {
  127. fn(result);
  128. });
  129. _process = function _process(onSuccess, onFail) {
  130. onSuccess(result);
  131. };
  132. _resolve2 = _reject2 = function _reject() {};
  133. return me;
  134. };
  135. /**
  136. * Reject the promise
  137. * @param {Error} error
  138. * @type {Function}
  139. */
  140. var _reject2 = function _reject(error) {
  141. // update status
  142. me.resolved = false;
  143. me.rejected = true;
  144. me.pending = false;
  145. _onFail.forEach(function (fn) {
  146. fn(error);
  147. });
  148. _process = function _process(onSuccess, onFail) {
  149. onFail(error);
  150. };
  151. _resolve2 = _reject2 = function _reject() {};
  152. return me;
  153. };
  154. /**
  155. * Cancel the promise. This will reject the promise with a CancellationError
  156. * @returns {this} self
  157. */
  158. this.cancel = function () {
  159. if (parent) {
  160. parent.cancel();
  161. } else {
  162. _reject2(new CancellationError());
  163. }
  164. return me;
  165. };
  166. /**
  167. * Set a timeout for the promise. If the promise is not resolved within
  168. * the time, the promise will be cancelled and a TimeoutError is thrown.
  169. * If the promise is resolved in time, the timeout is removed.
  170. * @param {number} delay Delay in milliseconds
  171. * @returns {this} self
  172. */
  173. this.timeout = function (delay) {
  174. if (parent) {
  175. parent.timeout(delay);
  176. } else {
  177. var timer = setTimeout(function () {
  178. _reject2(new TimeoutError('Promise timed out after ' + delay + ' ms'));
  179. }, delay);
  180. me.always(function () {
  181. clearTimeout(timer);
  182. });
  183. }
  184. return me;
  185. };
  186. // attach handler passing the resolve and reject functions
  187. handler(function (result) {
  188. _resolve2(result);
  189. }, function (error) {
  190. _reject2(error);
  191. });
  192. }
  193. /**
  194. * Execute given callback, then call resolve/reject based on the returned result
  195. * @param {Function} callback
  196. * @param {Function} resolve
  197. * @param {Function} reject
  198. * @returns {Function}
  199. * @private
  200. */
  201. function _then(callback, resolve, reject) {
  202. return function (result) {
  203. try {
  204. var res = callback(result);
  205. if (res && typeof res.then === 'function' && typeof res['catch'] === 'function') {
  206. // method returned a promise
  207. res.then(resolve, reject);
  208. } else {
  209. resolve(res);
  210. }
  211. } catch (error) {
  212. reject(error);
  213. }
  214. };
  215. }
  216. /**
  217. * Add an onFail callback to the Promise
  218. * @template TT
  219. * @param {(error: E) => TT | PromiseLike<TT>} onFail
  220. * @returns {Promise<T | TT>} promise
  221. */
  222. Promise.prototype['catch'] = function (onFail) {
  223. return this.then(null, onFail);
  224. };
  225. // TODO: add support for Promise.catch(Error, callback)
  226. // TODO: add support for Promise.catch(Error, Error, callback)
  227. /**
  228. * Execute given callback when the promise either resolves or rejects.
  229. * @template TT
  230. * @param {() => Promise<TT>} fn
  231. * @returns {Promise<TT>} promise
  232. */
  233. Promise.prototype.always = function (fn) {
  234. return this.then(fn, fn);
  235. };
  236. /**
  237. * Execute given callback when the promise either resolves or rejects.
  238. * Same semantics as Node's Promise.finally()
  239. * @param {Function | null | undefined} [fn]
  240. * @returns {Promise} promise
  241. */
  242. Promise.prototype.finally = function (fn) {
  243. var me = this;
  244. var final = function final() {
  245. return new Promise(function (resolve) {
  246. return resolve();
  247. }).then(fn).then(function () {
  248. return me;
  249. });
  250. };
  251. return this.then(final, final);
  252. };
  253. /**
  254. * Create a promise which resolves when all provided promises are resolved,
  255. * and fails when any of the promises resolves.
  256. * @param {Promise[]} promises
  257. * @returns {Promise<any[], any>} promise
  258. */
  259. Promise.all = function (promises) {
  260. return new Promise(function (resolve, reject) {
  261. var remaining = promises.length,
  262. results = [];
  263. if (remaining) {
  264. promises.forEach(function (p, i) {
  265. p.then(function (result) {
  266. results[i] = result;
  267. remaining--;
  268. if (remaining == 0) {
  269. resolve(results);
  270. }
  271. }, function (error) {
  272. remaining = 0;
  273. reject(error);
  274. });
  275. });
  276. } else {
  277. resolve(results);
  278. }
  279. });
  280. };
  281. /**
  282. * Create a promise resolver
  283. * @returns {{promise: Promise, resolve: Function, reject: Function}} resolver
  284. */
  285. Promise.defer = function () {
  286. var resolver = {};
  287. resolver.promise = new Promise(function (resolve, reject) {
  288. resolver.resolve = resolve;
  289. resolver.reject = reject;
  290. });
  291. return resolver;
  292. };
  293. /**
  294. * Create a cancellation error
  295. * @param {String} [message]
  296. * @extends Error
  297. */
  298. function CancellationError(message) {
  299. this.message = message || 'promise cancelled';
  300. this.stack = new Error().stack;
  301. }
  302. CancellationError.prototype = new Error();
  303. CancellationError.prototype.constructor = Error;
  304. CancellationError.prototype.name = 'CancellationError';
  305. Promise.CancellationError = CancellationError;
  306. /**
  307. * Create a timeout error
  308. * @param {String} [message]
  309. * @extends Error
  310. */
  311. function TimeoutError(message) {
  312. this.message = message || 'timeout exceeded';
  313. this.stack = new Error().stack;
  314. }
  315. TimeoutError.prototype = new Error();
  316. TimeoutError.prototype.constructor = Error;
  317. TimeoutError.prototype.name = 'TimeoutError';
  318. Promise.TimeoutError = TimeoutError;
  319. _Promise$1.Promise = Promise;
  320. return _Promise$1;
  321. }
  322. function _arrayLikeToArray(r, a) {
  323. (null == a || a > r.length) && (a = r.length);
  324. for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
  325. return n;
  326. }
  327. function _createForOfIteratorHelper(r, e) {
  328. var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
  329. if (!t) {
  330. if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e) {
  331. t && (r = t);
  332. var n = 0,
  333. F = function () {};
  334. return {
  335. s: F,
  336. n: function () {
  337. return n >= r.length ? {
  338. done: true
  339. } : {
  340. done: false,
  341. value: r[n++]
  342. };
  343. },
  344. e: function (r) {
  345. throw r;
  346. },
  347. f: F
  348. };
  349. }
  350. throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  351. }
  352. var o,
  353. a = true,
  354. u = false;
  355. return {
  356. s: function () {
  357. t = t.call(r);
  358. },
  359. n: function () {
  360. var r = t.next();
  361. return a = r.done, r;
  362. },
  363. e: function (r) {
  364. u = true, o = r;
  365. },
  366. f: function () {
  367. try {
  368. a || null == t.return || t.return();
  369. } finally {
  370. if (u) throw o;
  371. }
  372. }
  373. };
  374. }
  375. function _defineProperty(e, r, t) {
  376. return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
  377. value: t,
  378. enumerable: true,
  379. configurable: true,
  380. writable: true
  381. }) : e[r] = t, e;
  382. }
  383. function ownKeys(e, r) {
  384. var t = Object.keys(e);
  385. if (Object.getOwnPropertySymbols) {
  386. var o = Object.getOwnPropertySymbols(e);
  387. r && (o = o.filter(function (r) {
  388. return Object.getOwnPropertyDescriptor(e, r).enumerable;
  389. })), t.push.apply(t, o);
  390. }
  391. return t;
  392. }
  393. function _objectSpread2(e) {
  394. for (var r = 1; r < arguments.length; r++) {
  395. var t = null != arguments[r] ? arguments[r] : {};
  396. r % 2 ? ownKeys(Object(t), true).forEach(function (r) {
  397. _defineProperty(e, r, t[r]);
  398. }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
  399. Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
  400. });
  401. }
  402. return e;
  403. }
  404. function _toPrimitive(t, r) {
  405. if ("object" != typeof t || !t) return t;
  406. var e = t[Symbol.toPrimitive];
  407. if (void 0 !== e) {
  408. var i = e.call(t, r);
  409. if ("object" != typeof i) return i;
  410. throw new TypeError("@@toPrimitive must return a primitive value.");
  411. }
  412. return ("string" === r ? String : Number)(t);
  413. }
  414. function _toPropertyKey(t) {
  415. var i = _toPrimitive(t, "string");
  416. return "symbol" == typeof i ? i : i + "";
  417. }
  418. function _typeof(o) {
  419. "@babel/helpers - typeof";
  420. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
  421. return typeof o;
  422. } : function (o) {
  423. return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
  424. }, _typeof(o);
  425. }
  426. function _unsupportedIterableToArray(r, a) {
  427. if (r) {
  428. if ("string" == typeof r) return _arrayLikeToArray(r, a);
  429. var t = {}.toString.call(r).slice(8, -1);
  430. 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) ? _arrayLikeToArray(r, a) : void 0;
  431. }
  432. }
  433. var WorkerHandler = {exports: {}};
  434. var validateOptions = {};
  435. /**
  436. * Validate that the object only contains known option names
  437. * - Throws an error when unknown options are detected
  438. * - Throws an error when some of the allowed options are attached
  439. * @param {Object | undefined} options
  440. * @param {string[]} allowedOptionNames
  441. * @param {string} objectName
  442. * @retrun {Object} Returns the original options
  443. */
  444. var hasRequiredValidateOptions;
  445. function requireValidateOptions() {
  446. if (hasRequiredValidateOptions) return validateOptions;
  447. hasRequiredValidateOptions = 1;
  448. validateOptions.validateOptions = function validateOptions(options, allowedOptionNames, objectName) {
  449. if (!options) {
  450. return;
  451. }
  452. var optionNames = options ? Object.keys(options) : [];
  453. // check for unknown properties
  454. var unknownOptionName = optionNames.find(function (optionName) {
  455. return !allowedOptionNames.includes(optionName);
  456. });
  457. if (unknownOptionName) {
  458. throw new Error('Object "' + objectName + '" contains an unknown option "' + unknownOptionName + '"');
  459. }
  460. // check for inherited properties which are not present on the object itself
  461. var illegalOptionName = allowedOptionNames.find(function (allowedOptionName) {
  462. return Object.prototype[allowedOptionName] && !optionNames.includes(allowedOptionName);
  463. });
  464. if (illegalOptionName) {
  465. throw new Error('Object "' + objectName + '" contains an inherited option "' + illegalOptionName + '" which is ' + 'not defined in the object itself but in its prototype. Only plain objects are allowed. ' + 'Please remove the option from the prototype or override it with a value "undefined".');
  466. }
  467. return options;
  468. };
  469. // source: https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker
  470. validateOptions.workerOptsNames = ['credentials', 'name', 'type'];
  471. // source: https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options
  472. validateOptions.forkOptsNames = ['cwd', 'detached', 'env', 'execPath', 'execArgv', 'gid', 'serialization', 'signal', 'killSignal', 'silent', 'stdio', 'uid', 'windowsVerbatimArguments', 'timeout'];
  473. // source: https://nodejs.org/api/worker_threads.html#new-workerfilename-options
  474. validateOptions.workerThreadOptsNames = ['argv', 'env', 'eval', 'execArgv', 'stdin', 'stdout', 'stderr', 'workerData', 'trackUnmanagedFds', 'transferList', 'resourceLimits', 'name'];
  475. return validateOptions;
  476. }
  477. /**
  478. * embeddedWorker.js contains an embedded version of worker.js.
  479. * This file is automatically generated,
  480. * changes made in this file will be overwritten.
  481. */
  482. var embeddedWorker;
  483. var hasRequiredEmbeddedWorker;
  484. function requireEmbeddedWorker() {
  485. if (hasRequiredEmbeddedWorker) return embeddedWorker;
  486. hasRequiredEmbeddedWorker = 1;
  487. embeddedWorker = "!function(e,n){\"object\"==typeof exports&&\"undefined\"!=typeof module?module.exports=n():\"function\"==typeof define&&define.amd?define(n):(e=\"undefined\"!=typeof globalThis?globalThis:e||self).worker=n()}(this,(function(){\"use strict\";function e(n){return e=\"function\"==typeof Symbol&&\"symbol\"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&\"function\"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?\"symbol\":typeof e},e(n)}function n(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,\"default\")?e.default:e}var t={};var r=function(e,n){this.message=e,this.transfer=n},o={};function i(e,n){var t=this;if(!(this instanceof i))throw new SyntaxError(\"Constructor must be called with the new operator\");if(\"function\"!=typeof e)throw new SyntaxError(\"Function parameter handler(resolve, reject) missing\");var r=[],o=[];this.resolved=!1,this.rejected=!1,this.pending=!0,this[Symbol.toStringTag]=\"Promise\";var a=function(e,n){r.push(e),o.push(n)};this.then=function(e,n){return new i((function(t,r){var o=e?s(e,t,r):t,i=n?s(n,t,r):r;a(o,i)}),t)};var f=function(e){return t.resolved=!0,t.rejected=!1,t.pending=!1,r.forEach((function(n){n(e)})),a=function(n,t){n(e)},f=d=function(){},t},d=function(e){return t.resolved=!1,t.rejected=!0,t.pending=!1,o.forEach((function(n){n(e)})),a=function(n,t){t(e)},f=d=function(){},t};this.cancel=function(){return n?n.cancel():d(new u),t},this.timeout=function(e){if(n)n.timeout(e);else{var r=setTimeout((function(){d(new c(\"Promise timed out after \"+e+\" ms\"))}),e);t.always((function(){clearTimeout(r)}))}return t},e((function(e){f(e)}),(function(e){d(e)}))}function s(e,n,t){return function(r){try{var o=e(r);o&&\"function\"==typeof o.then&&\"function\"==typeof o.catch?o.then(n,t):n(o)}catch(e){t(e)}}}function u(e){this.message=e||\"promise cancelled\",this.stack=(new Error).stack}function c(e){this.message=e||\"timeout exceeded\",this.stack=(new Error).stack}return i.prototype.catch=function(e){return this.then(null,e)},i.prototype.always=function(e){return this.then(e,e)},i.prototype.finally=function(e){var n=this,t=function(){return new i((function(e){return e()})).then(e).then((function(){return n}))};return this.then(t,t)},i.all=function(e){return new i((function(n,t){var r=e.length,o=[];r?e.forEach((function(e,i){e.then((function(e){o[i]=e,0==--r&&n(o)}),(function(e){r=0,t(e)}))})):n(o)}))},i.defer=function(){var e={};return e.promise=new i((function(n,t){e.resolve=n,e.reject=t})),e},u.prototype=new Error,u.prototype.constructor=Error,u.prototype.name=\"CancellationError\",i.CancellationError=u,c.prototype=new Error,c.prototype.constructor=Error,c.prototype.name=\"TimeoutError\",i.TimeoutError=c,o.Promise=i,function(n){var t=r,i=o.Promise,s=\"__workerpool-cleanup__\",u={exit:function(){}},c={addAbortListener:function(e){u.abortListeners.push(e)},emit:u.emit};if(\"undefined\"!=typeof self&&\"function\"==typeof postMessage&&\"function\"==typeof addEventListener)u.on=function(e,n){addEventListener(e,(function(e){n(e.data)}))},u.send=function(e,n){n?postMessage(e,n):postMessage(e)};else{if(\"undefined\"==typeof process)throw new Error(\"Script must be executed as a worker\");var a;try{a=require(\"worker_threads\")}catch(n){if(\"object\"!==e(n)||null===n||\"MODULE_NOT_FOUND\"!==n.code)throw n}if(a&&null!==a.parentPort){var f=a.parentPort;u.send=f.postMessage.bind(f),u.on=f.on.bind(f),u.exit=process.exit.bind(process)}else u.on=process.on.bind(process),u.send=function(e){process.send(e)},u.on(\"disconnect\",(function(){process.exit(1)})),u.exit=process.exit.bind(process)}function d(e){return e&&e.toJSON?JSON.parse(JSON.stringify(e)):JSON.parse(JSON.stringify(e,Object.getOwnPropertyNames(e)))}function l(e){return e&&\"function\"==typeof e.then&&\"function\"==typeof e.catch}u.methods={},u.methods.run=function(e,n){var t=new Function(\"return (\"+e+\").apply(this, arguments);\");return t.worker=c,t.apply(t,n)},u.methods.methods=function(){return Object.keys(u.methods)},u.terminationHandler=void 0,u.abortListenerTimeout=1e3,u.abortListeners=[],u.terminateAndExit=function(e){var n=function(){u.exit(e)};if(!u.terminationHandler)return n();var t=u.terminationHandler(e);return l(t)?(t.then(n,n),t):(n(),new i((function(e,n){n(new Error(\"Worker terminating\"))})))},u.cleanup=function(e){if(!u.abortListeners.length)return u.send({id:e,method:s,error:d(new Error(\"Worker terminating\"))}),new i((function(e){e()}));var n,t=u.abortListeners.map((function(e){return e()})),r=new i((function(e,t){n=setTimeout((function(){t(new Error(\"Timeout occured waiting for abort handler, killing worker\"))}),u.abortListenerTimeout)})),o=i.all(t).then((function(){clearTimeout(n),u.abortListeners.length||(u.abortListeners=[])}),(function(){clearTimeout(n),u.exit()}));return new i((function(e,n){o.then(e,n),r.then(e,n)})).then((function(){u.send({id:e,method:s,error:null})}),(function(n){u.send({id:e,method:s,error:n?d(n):null})}))};var p=null;u.on(\"message\",(function(e){if(\"__workerpool-terminate__\"===e)return u.terminateAndExit(0);if(e.method===s)return u.cleanup(e.id);try{var n=u.methods[e.method];if(!n)throw new Error('Unknown method \"'+e.method+'\"');p=e.id;var r=n.apply(n,e.params);l(r)?r.then((function(n){n instanceof t?u.send({id:e.id,result:n.message,error:null},n.transfer):u.send({id:e.id,result:n,error:null}),p=null})).catch((function(n){u.send({id:e.id,result:null,error:d(n)}),p=null})):(r instanceof t?u.send({id:e.id,result:r.message,error:null},r.transfer):u.send({id:e.id,result:r,error:null}),p=null)}catch(n){u.send({id:e.id,result:null,error:d(n)})}})),u.register=function(e,n){if(e)for(var t in e)e.hasOwnProperty(t)&&(u.methods[t]=e[t],u.methods[t].worker=c);n&&(u.terminationHandler=n.onTerminate,u.abortListenerTimeout=n.abortListenerTimeout||1e3),u.send(\"ready\")},u.emit=function(e){if(p){if(e instanceof t)return void u.send({id:p,isEvent:!0,payload:e.message},e.transfer);u.send({id:p,isEvent:!0,payload:e})}},n.add=u.register,n.emit=u.emit}(t),n(t)}));\n//# sourceMappingURL=worker.min.js.map\n";
  488. return embeddedWorker;
  489. }
  490. var hasRequiredWorkerHandler;
  491. function requireWorkerHandler() {
  492. if (hasRequiredWorkerHandler) return WorkerHandler.exports;
  493. hasRequiredWorkerHandler = 1;
  494. var _require$$ = require_Promise(),
  495. Promise = _require$$.Promise;
  496. var environment = environmentExports;
  497. var _require$$2 = requireValidateOptions(),
  498. validateOptions = _require$$2.validateOptions,
  499. forkOptsNames = _require$$2.forkOptsNames,
  500. workerThreadOptsNames = _require$$2.workerThreadOptsNames,
  501. workerOptsNames = _require$$2.workerOptsNames;
  502. /**
  503. * Special message sent by parent which causes a child process worker to terminate itself.
  504. * Not a "message object"; this string is the entire message.
  505. */
  506. var TERMINATE_METHOD_ID = '__workerpool-terminate__';
  507. /**
  508. * Special message by parent which causes a child process worker to perform cleaup
  509. * steps before determining if the child process worker should be terminated.
  510. */
  511. var CLEANUP_METHOD_ID = '__workerpool-cleanup__';
  512. function ensureWorkerThreads() {
  513. var WorkerThreads = tryRequireWorkerThreads();
  514. if (!WorkerThreads) {
  515. throw new Error('WorkerPool: workerType = \'thread\' is not supported, Node >= 11.7.0 required');
  516. }
  517. return WorkerThreads;
  518. }
  519. // check whether Worker is supported by the browser
  520. function ensureWebWorker() {
  521. // Workaround for a bug in PhantomJS (Or QtWebkit): https://github.com/ariya/phantomjs/issues/14534
  522. if (typeof Worker !== 'function' && ((typeof Worker === "undefined" ? "undefined" : _typeof(Worker)) !== 'object' || typeof Worker.prototype.constructor !== 'function')) {
  523. throw new Error('WorkerPool: Web Workers not supported');
  524. }
  525. }
  526. function tryRequireWorkerThreads() {
  527. try {
  528. return require('worker_threads');
  529. } catch (error) {
  530. if (_typeof(error) === 'object' && error !== null && error.code === 'MODULE_NOT_FOUND') {
  531. // no worker_threads available (old version of node.js)
  532. return null;
  533. } else {
  534. throw error;
  535. }
  536. }
  537. }
  538. // get the default worker script
  539. function getDefaultWorker() {
  540. if (environment.platform === 'browser') {
  541. // test whether the browser supports all features that we need
  542. if (typeof Blob === 'undefined') {
  543. throw new Error('Blob not supported by the browser');
  544. }
  545. if (!window.URL || typeof window.URL.createObjectURL !== 'function') {
  546. throw new Error('URL.createObjectURL not supported by the browser');
  547. }
  548. // use embedded worker.js
  549. var blob = new Blob([requireEmbeddedWorker()], {
  550. type: 'text/javascript'
  551. });
  552. return window.URL.createObjectURL(blob);
  553. } else {
  554. // use external worker.js in current directory
  555. return __dirname + '/worker.js';
  556. }
  557. }
  558. function setupWorker(script, options) {
  559. if (options.workerType === 'web') {
  560. // browser only
  561. ensureWebWorker();
  562. return setupBrowserWorker(script, options.workerOpts, Worker);
  563. } else if (options.workerType === 'thread') {
  564. // node.js only
  565. WorkerThreads = ensureWorkerThreads();
  566. return setupWorkerThreadWorker(script, WorkerThreads, options);
  567. } else if (options.workerType === 'process' || !options.workerType) {
  568. // node.js only
  569. return setupProcessWorker(script, resolveForkOptions(options), require('child_process'));
  570. } else {
  571. // options.workerType === 'auto' or undefined
  572. if (environment.platform === 'browser') {
  573. ensureWebWorker();
  574. return setupBrowserWorker(script, options.workerOpts, Worker);
  575. } else {
  576. // environment.platform === 'node'
  577. var WorkerThreads = tryRequireWorkerThreads();
  578. if (WorkerThreads) {
  579. return setupWorkerThreadWorker(script, WorkerThreads, options);
  580. } else {
  581. return setupProcessWorker(script, resolveForkOptions(options), require('child_process'));
  582. }
  583. }
  584. }
  585. }
  586. function setupBrowserWorker(script, workerOpts, Worker) {
  587. // validate the options right before creating the worker (not when creating the pool)
  588. validateOptions(workerOpts, workerOptsNames, 'workerOpts');
  589. // create the web worker
  590. var worker = new Worker(script, workerOpts);
  591. worker.isBrowserWorker = true;
  592. // add node.js API to the web worker
  593. worker.on = function (event, callback) {
  594. this.addEventListener(event, function (message) {
  595. callback(message.data);
  596. });
  597. };
  598. worker.send = function (message, transfer) {
  599. this.postMessage(message, transfer);
  600. };
  601. return worker;
  602. }
  603. function setupWorkerThreadWorker(script, WorkerThreads, options) {
  604. var _options$emitStdStrea, _options$emitStdStrea2;
  605. // validate the options right before creating the worker thread (not when creating the pool)
  606. validateOptions(options === null || options === void 0 ? void 0 : options.workerThreadOpts, workerThreadOptsNames, 'workerThreadOpts');
  607. var worker = new WorkerThreads.Worker(script, _objectSpread2({
  608. stdout: (_options$emitStdStrea = options === null || options === void 0 ? void 0 : options.emitStdStreams) !== null && _options$emitStdStrea !== void 0 ? _options$emitStdStrea : false,
  609. // pipe worker.STDOUT to process.STDOUT if not requested
  610. stderr: (_options$emitStdStrea2 = options === null || options === void 0 ? void 0 : options.emitStdStreams) !== null && _options$emitStdStrea2 !== void 0 ? _options$emitStdStrea2 : false
  611. }, options === null || options === void 0 ? void 0 : options.workerThreadOpts));
  612. worker.isWorkerThread = true;
  613. worker.send = function (message, transfer) {
  614. this.postMessage(message, transfer);
  615. };
  616. worker.kill = function () {
  617. this.terminate();
  618. return true;
  619. };
  620. worker.disconnect = function () {
  621. this.terminate();
  622. };
  623. if (options !== null && options !== void 0 && options.emitStdStreams) {
  624. worker.stdout.on('data', function (data) {
  625. return worker.emit("stdout", data);
  626. });
  627. worker.stderr.on('data', function (data) {
  628. return worker.emit("stderr", data);
  629. });
  630. }
  631. return worker;
  632. }
  633. function setupProcessWorker(script, options, child_process) {
  634. // validate the options right before creating the child process (not when creating the pool)
  635. validateOptions(options.forkOpts, forkOptsNames, 'forkOpts');
  636. // no WorkerThreads, fallback to sub-process based workers
  637. var worker = child_process.fork(script, options.forkArgs, options.forkOpts);
  638. // ignore transfer argument since it is not supported by process
  639. var send = worker.send;
  640. worker.send = function (message) {
  641. return send.call(worker, message);
  642. };
  643. if (options.emitStdStreams) {
  644. worker.stdout.on('data', function (data) {
  645. return worker.emit("stdout", data);
  646. });
  647. worker.stderr.on('data', function (data) {
  648. return worker.emit("stderr", data);
  649. });
  650. }
  651. worker.isChildProcess = true;
  652. return worker;
  653. }
  654. // add debug flags to child processes if the node inspector is active
  655. function resolveForkOptions(opts) {
  656. opts = opts || {};
  657. var processExecArgv = process.execArgv.join(' ');
  658. var inspectorActive = processExecArgv.indexOf('--inspect') !== -1;
  659. var debugBrk = processExecArgv.indexOf('--debug-brk') !== -1;
  660. var execArgv = [];
  661. if (inspectorActive) {
  662. execArgv.push('--inspect=' + opts.debugPort);
  663. if (debugBrk) {
  664. execArgv.push('--debug-brk');
  665. }
  666. }
  667. process.execArgv.forEach(function (arg) {
  668. if (arg.indexOf('--max-old-space-size') > -1) {
  669. execArgv.push(arg);
  670. }
  671. });
  672. return Object.assign({}, opts, {
  673. forkArgs: opts.forkArgs,
  674. forkOpts: Object.assign({}, opts.forkOpts, {
  675. execArgv: (opts.forkOpts && opts.forkOpts.execArgv || []).concat(execArgv),
  676. stdio: opts.emitStdStreams ? "pipe" : undefined
  677. })
  678. });
  679. }
  680. /**
  681. * Converts a serialized error to Error
  682. * @param {Object} obj Error that has been serialized and parsed to object
  683. * @return {Error} The equivalent Error.
  684. */
  685. function objectToError(obj) {
  686. var temp = new Error('');
  687. var props = Object.keys(obj);
  688. for (var i = 0; i < props.length; i++) {
  689. temp[props[i]] = obj[props[i]];
  690. }
  691. return temp;
  692. }
  693. function handleEmittedStdPayload(handler, payload) {
  694. // TODO: refactor if parallel task execution gets added
  695. Object.values(handler.processing).forEach(function (task) {
  696. var _task$options;
  697. return task === null || task === void 0 || (_task$options = task.options) === null || _task$options === void 0 ? void 0 : _task$options.on(payload);
  698. });
  699. Object.values(handler.tracking).forEach(function (task) {
  700. var _task$options2;
  701. return task === null || task === void 0 || (_task$options2 = task.options) === null || _task$options2 === void 0 ? void 0 : _task$options2.on(payload);
  702. });
  703. }
  704. /**
  705. * A WorkerHandler controls a single worker. This worker can be a child process
  706. * on node.js or a WebWorker in a browser environment.
  707. * @param {String} [script] If no script is provided, a default worker with a
  708. * function run will be created.
  709. * @param {import('./types.js').WorkerPoolOptions} [_options] See docs
  710. * @constructor
  711. */
  712. function WorkerHandler$1(script, _options) {
  713. var me = this;
  714. var options = _options || {};
  715. this.script = script || getDefaultWorker();
  716. this.worker = setupWorker(this.script, options);
  717. this.debugPort = options.debugPort;
  718. this.forkOpts = options.forkOpts;
  719. this.forkArgs = options.forkArgs;
  720. this.workerOpts = options.workerOpts;
  721. this.workerThreadOpts = options.workerThreadOpts;
  722. this.workerTerminateTimeout = options.workerTerminateTimeout;
  723. // The ready message is only sent if the worker.add method is called (And the default script is not used)
  724. if (!script) {
  725. this.worker.ready = true;
  726. }
  727. // queue for requests that are received before the worker is ready
  728. this.requestQueue = [];
  729. this.worker.on("stdout", function (data) {
  730. handleEmittedStdPayload(me, {
  731. "stdout": data.toString()
  732. });
  733. });
  734. this.worker.on("stderr", function (data) {
  735. handleEmittedStdPayload(me, {
  736. "stderr": data.toString()
  737. });
  738. });
  739. this.worker.on('message', function (response) {
  740. if (me.terminated) {
  741. return;
  742. }
  743. if (typeof response === 'string' && response === 'ready') {
  744. me.worker.ready = true;
  745. dispatchQueuedRequests();
  746. } else {
  747. // find the task from the processing queue, and run the tasks callback
  748. var id = response.id;
  749. var task = me.processing[id];
  750. if (task !== undefined) {
  751. if (response.isEvent) {
  752. if (task.options && typeof task.options.on === 'function') {
  753. task.options.on(response.payload);
  754. }
  755. } else {
  756. // remove the task from the queue
  757. delete me.processing[id];
  758. // test if we need to terminate
  759. if (me.terminating === true) {
  760. // complete worker termination if all tasks are finished
  761. me.terminate();
  762. }
  763. // resolve the task's promise
  764. if (response.error) {
  765. task.resolver.reject(objectToError(response.error));
  766. } else {
  767. task.resolver.resolve(response.result);
  768. }
  769. }
  770. } else {
  771. // if the task is not the current, it might be tracked for cleanup
  772. var task = me.tracking[id];
  773. if (task !== undefined) {
  774. if (response.isEvent) {
  775. if (task.options && typeof task.options.on === 'function') {
  776. task.options.on(response.payload);
  777. }
  778. }
  779. }
  780. }
  781. if (response.method === CLEANUP_METHOD_ID) {
  782. var trackedTask = me.tracking[response.id];
  783. if (trackedTask !== undefined) {
  784. if (response.error) {
  785. clearTimeout(trackedTask.timeoutId);
  786. trackedTask.resolver.reject(objectToError(response.error));
  787. } else {
  788. me.tracking && clearTimeout(trackedTask.timeoutId);
  789. // if we do not encounter an error wrap the the original timeout error and reject
  790. trackedTask.resolver.reject(new WrappedTimeoutError(trackedTask.error));
  791. }
  792. }
  793. delete me.tracking[id];
  794. }
  795. }
  796. });
  797. // reject all running tasks on worker error
  798. function onError(error) {
  799. me.terminated = true;
  800. for (var id in me.processing) {
  801. if (me.processing[id] !== undefined) {
  802. me.processing[id].resolver.reject(error);
  803. }
  804. }
  805. me.processing = Object.create(null);
  806. }
  807. // send all queued requests to worker
  808. function dispatchQueuedRequests() {
  809. var _iterator = _createForOfIteratorHelper(me.requestQueue.splice(0)),
  810. _step;
  811. try {
  812. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  813. var request = _step.value;
  814. me.worker.send(request.message, request.transfer);
  815. }
  816. } catch (err) {
  817. _iterator.e(err);
  818. } finally {
  819. _iterator.f();
  820. }
  821. }
  822. var worker = this.worker;
  823. // listen for worker messages error and exit
  824. this.worker.on('error', onError);
  825. this.worker.on('exit', function (exitCode, signalCode) {
  826. var message = 'Workerpool Worker terminated Unexpectedly\n';
  827. message += ' exitCode: `' + exitCode + '`\n';
  828. message += ' signalCode: `' + signalCode + '`\n';
  829. message += ' workerpool.script: `' + me.script + '`\n';
  830. message += ' spawnArgs: `' + worker.spawnargs + '`\n';
  831. message += ' spawnfile: `' + worker.spawnfile + '`\n';
  832. message += ' stdout: `' + worker.stdout + '`\n';
  833. message += ' stderr: `' + worker.stderr + '`\n';
  834. onError(new Error(message));
  835. });
  836. this.processing = Object.create(null); // queue with tasks currently in progress
  837. this.tracking = Object.create(null); // queue with tasks being monitored for cleanup status
  838. this.terminating = false;
  839. this.terminated = false;
  840. this.cleaning = false;
  841. this.terminationHandler = null;
  842. this.lastId = 0;
  843. }
  844. /**
  845. * Get a list with methods available on the worker.
  846. * @return {Promise.<String[], Error>} methods
  847. */
  848. WorkerHandler$1.prototype.methods = function () {
  849. return this.exec('methods');
  850. };
  851. /**
  852. * Execute a method with given parameters on the worker
  853. * @param {String} method
  854. * @param {Array} [params]
  855. * @param {{resolve: Function, reject: Function}} [resolver]
  856. * @param {import('./types.js').ExecOptions} [options]
  857. * @return {Promise.<*, Error>} result
  858. */
  859. WorkerHandler$1.prototype.exec = function (method, params, resolver, options) {
  860. if (!resolver) {
  861. resolver = Promise.defer();
  862. }
  863. // generate a unique id for the task
  864. var id = ++this.lastId;
  865. // register a new task as being in progress
  866. this.processing[id] = {
  867. id: id,
  868. resolver: resolver,
  869. options: options
  870. };
  871. // build a JSON-RPC request
  872. var request = {
  873. message: {
  874. id: id,
  875. method: method,
  876. params: params
  877. },
  878. transfer: options && options.transfer
  879. };
  880. if (this.terminated) {
  881. resolver.reject(new Error('Worker is terminated'));
  882. } else if (this.worker.ready) {
  883. // send the request to the worker
  884. this.worker.send(request.message, request.transfer);
  885. } else {
  886. this.requestQueue.push(request);
  887. }
  888. // on cancellation, force the worker to terminate
  889. var me = this;
  890. return resolver.promise.catch(function (error) {
  891. if (error instanceof Promise.CancellationError || error instanceof Promise.TimeoutError) {
  892. me.tracking[id] = {
  893. id: id,
  894. resolver: Promise.defer(),
  895. options: options,
  896. error: error
  897. };
  898. // remove this task from the queue. It is already rejected (hence this
  899. // catch event), and else it will be rejected again when terminating
  900. delete me.processing[id];
  901. me.tracking[id].resolver.promise = me.tracking[id].resolver.promise.catch(function (err) {
  902. delete me.tracking[id];
  903. // if we find the error is an instance of WrappedTimeoutError we know the error should not cause termination
  904. // as the response from the worker did not contain an error. We still wish to throw the original timeout error
  905. // to the caller.
  906. if (err instanceof WrappedTimeoutError) {
  907. throw err.error;
  908. }
  909. var promise = me.terminateAndNotify(true).then(function () {
  910. throw err;
  911. }, function (err) {
  912. throw err;
  913. });
  914. return promise;
  915. });
  916. me.worker.send({
  917. id: id,
  918. method: CLEANUP_METHOD_ID
  919. });
  920. /**
  921. * Sets a timeout to reject the cleanup operation if the message sent to the worker
  922. * does not receive a response. see worker.tryCleanup for worker cleanup operations.
  923. * Here we use the workerTerminateTimeout as the worker will be terminated if the timeout does invoke.
  924. *
  925. * We need this timeout in either case of a Timeout or Cancellation Error as if
  926. * the worker does not send a message we still need to give a window of time for a response.
  927. *
  928. * The workerTermniateTimeout is used here if this promise is rejected the worker cleanup
  929. * operations will occure.
  930. */
  931. me.tracking[id].timeoutId = setTimeout(function () {
  932. me.tracking[id].resolver.reject(error);
  933. }, me.workerTerminateTimeout);
  934. return me.tracking[id].resolver.promise;
  935. } else {
  936. throw error;
  937. }
  938. });
  939. };
  940. /**
  941. * Test whether the worker is processing any tasks or cleaning up before termination.
  942. * @return {boolean} Returns true if the worker is busy
  943. */
  944. WorkerHandler$1.prototype.busy = function () {
  945. return this.cleaning || Object.keys(this.processing).length > 0;
  946. };
  947. /**
  948. * Terminate the worker.
  949. * @param {boolean} [force=false] If false (default), the worker is terminated
  950. * after finishing all tasks currently in
  951. * progress. If true, the worker will be
  952. * terminated immediately.
  953. * @param {function} [callback=null] If provided, will be called when process terminates.
  954. */
  955. WorkerHandler$1.prototype.terminate = function (force, callback) {
  956. var me = this;
  957. if (force) {
  958. // cancel all tasks in progress
  959. for (var id in this.processing) {
  960. if (this.processing[id] !== undefined) {
  961. this.processing[id].resolver.reject(new Error('Worker terminated'));
  962. }
  963. }
  964. this.processing = Object.create(null);
  965. }
  966. // If we are terminating, cancel all tracked task for cleanup
  967. for (var _i = 0, _Object$values = Object.values(me.tracking); _i < _Object$values.length; _i++) {
  968. var task = _Object$values[_i];
  969. clearTimeout(task.timeoutId);
  970. task.resolver.reject(new Error('Worker Terminating'));
  971. }
  972. me.tracking = Object.create(null);
  973. if (typeof callback === 'function') {
  974. this.terminationHandler = callback;
  975. }
  976. if (!this.busy()) {
  977. // all tasks are finished. kill the worker
  978. var cleanup = function cleanup(err) {
  979. me.terminated = true;
  980. me.cleaning = false;
  981. if (me.worker != null && me.worker.removeAllListeners) {
  982. // removeAllListeners is only available for child_process
  983. me.worker.removeAllListeners('message');
  984. }
  985. me.worker = null;
  986. me.terminating = false;
  987. if (me.terminationHandler) {
  988. me.terminationHandler(err, me);
  989. } else if (err) {
  990. throw err;
  991. }
  992. };
  993. if (this.worker) {
  994. if (typeof this.worker.kill === 'function') {
  995. if (this.worker.killed) {
  996. cleanup(new Error('worker already killed!'));
  997. return;
  998. }
  999. // child process and worker threads
  1000. var cleanExitTimeout = setTimeout(function () {
  1001. if (me.worker) {
  1002. me.worker.kill();
  1003. }
  1004. }, this.workerTerminateTimeout);
  1005. this.worker.once('exit', function () {
  1006. clearTimeout(cleanExitTimeout);
  1007. if (me.worker) {
  1008. me.worker.killed = true;
  1009. }
  1010. cleanup();
  1011. });
  1012. if (this.worker.ready) {
  1013. this.worker.send(TERMINATE_METHOD_ID);
  1014. } else {
  1015. this.requestQueue.push({
  1016. message: TERMINATE_METHOD_ID
  1017. });
  1018. }
  1019. // mark that the worker is cleaning up resources
  1020. // to prevent new tasks from being executed
  1021. this.cleaning = true;
  1022. return;
  1023. } else if (typeof this.worker.terminate === 'function') {
  1024. this.worker.terminate(); // web worker
  1025. this.worker.killed = true;
  1026. } else {
  1027. throw new Error('Failed to terminate worker');
  1028. }
  1029. }
  1030. cleanup();
  1031. } else {
  1032. // we can't terminate immediately, there are still tasks being executed
  1033. this.terminating = true;
  1034. }
  1035. };
  1036. /**
  1037. * Terminate the worker, returning a Promise that resolves when the termination has been done.
  1038. * @param {boolean} [force=false] If false (default), the worker is terminated
  1039. * after finishing all tasks currently in
  1040. * progress. If true, the worker will be
  1041. * terminated immediately.
  1042. * @param {number} [timeout] If provided and non-zero, worker termination promise will be rejected
  1043. * after timeout if worker process has not been terminated.
  1044. * @return {Promise.<WorkerHandler, Error>}
  1045. */
  1046. WorkerHandler$1.prototype.terminateAndNotify = function (force, timeout) {
  1047. var resolver = Promise.defer();
  1048. if (timeout) {
  1049. resolver.promise.timeout(timeout);
  1050. }
  1051. this.terminate(force, function (err, worker) {
  1052. if (err) {
  1053. resolver.reject(err);
  1054. } else {
  1055. resolver.resolve(worker);
  1056. }
  1057. });
  1058. return resolver.promise;
  1059. };
  1060. /**
  1061. * Wrapper error type to denote that a TimeoutError has already been proceesed
  1062. * and we should skip cleanup operations
  1063. * @param {Promise.TimeoutError} timeoutError
  1064. */
  1065. function WrappedTimeoutError(timeoutError) {
  1066. this.error = timeoutError;
  1067. this.stack = new Error().stack;
  1068. }
  1069. WorkerHandler.exports = WorkerHandler$1;
  1070. WorkerHandler.exports._tryRequireWorkerThreads = tryRequireWorkerThreads;
  1071. WorkerHandler.exports._setupProcessWorker = setupProcessWorker;
  1072. WorkerHandler.exports._setupBrowserWorker = setupBrowserWorker;
  1073. WorkerHandler.exports._setupWorkerThreadWorker = setupWorkerThreadWorker;
  1074. WorkerHandler.exports.ensureWorkerThreads = ensureWorkerThreads;
  1075. return WorkerHandler.exports;
  1076. }
  1077. var debugPortAllocator;
  1078. var hasRequiredDebugPortAllocator;
  1079. function requireDebugPortAllocator() {
  1080. if (hasRequiredDebugPortAllocator) return debugPortAllocator;
  1081. hasRequiredDebugPortAllocator = 1;
  1082. var MAX_PORTS = 65535;
  1083. debugPortAllocator = DebugPortAllocator;
  1084. function DebugPortAllocator() {
  1085. this.ports = Object.create(null);
  1086. this.length = 0;
  1087. }
  1088. DebugPortAllocator.prototype.nextAvailableStartingAt = function (starting) {
  1089. while (this.ports[starting] === true) {
  1090. starting++;
  1091. }
  1092. if (starting >= MAX_PORTS) {
  1093. throw new Error('WorkerPool debug port limit reached: ' + starting + '>= ' + MAX_PORTS);
  1094. }
  1095. this.ports[starting] = true;
  1096. this.length++;
  1097. return starting;
  1098. };
  1099. DebugPortAllocator.prototype.releasePort = function (port) {
  1100. delete this.ports[port];
  1101. this.length--;
  1102. };
  1103. return debugPortAllocator;
  1104. }
  1105. var Pool_1;
  1106. var hasRequiredPool;
  1107. function requirePool() {
  1108. if (hasRequiredPool) return Pool_1;
  1109. hasRequiredPool = 1;
  1110. var _require$$ = require_Promise(),
  1111. Promise = _require$$.Promise;
  1112. var WorkerHandler = requireWorkerHandler();
  1113. var environment = environmentExports;
  1114. var DebugPortAllocator = requireDebugPortAllocator();
  1115. var DEBUG_PORT_ALLOCATOR = new DebugPortAllocator();
  1116. /**
  1117. * A pool to manage workers, which can be created using the function workerpool.pool.
  1118. *
  1119. * @param {String} [script] Optional worker script
  1120. * @param {import('./types.js').WorkerPoolOptions} [options] See docs
  1121. * @constructor
  1122. */
  1123. function Pool(script, options) {
  1124. if (typeof script === 'string') {
  1125. /** @readonly */
  1126. this.script = script || null;
  1127. } else {
  1128. this.script = null;
  1129. options = script;
  1130. }
  1131. /** @private */
  1132. this.workers = []; // queue with all workers
  1133. /** @private */
  1134. this.tasks = []; // queue with tasks awaiting execution
  1135. options = options || {};
  1136. /** @readonly */
  1137. this.forkArgs = Object.freeze(options.forkArgs || []);
  1138. /** @readonly */
  1139. this.forkOpts = Object.freeze(options.forkOpts || {});
  1140. /** @readonly */
  1141. this.workerOpts = Object.freeze(options.workerOpts || {});
  1142. /** @readonly */
  1143. this.workerThreadOpts = Object.freeze(options.workerThreadOpts || {});
  1144. /** @private */
  1145. this.debugPortStart = options.debugPortStart || 43210;
  1146. /** @readonly @deprecated */
  1147. this.nodeWorker = options.nodeWorker;
  1148. /** @readonly
  1149. * @type {'auto' | 'web' | 'process' | 'thread'}
  1150. */
  1151. this.workerType = options.workerType || options.nodeWorker || 'auto';
  1152. /** @readonly */
  1153. this.maxQueueSize = options.maxQueueSize || Infinity;
  1154. /** @readonly */
  1155. this.workerTerminateTimeout = options.workerTerminateTimeout || 1000;
  1156. /** @readonly */
  1157. this.onCreateWorker = options.onCreateWorker || function () {
  1158. return null;
  1159. };
  1160. /** @readonly */
  1161. this.onTerminateWorker = options.onTerminateWorker || function () {
  1162. return null;
  1163. };
  1164. /** @readonly */
  1165. this.emitStdStreams = options.emitStdStreams || false;
  1166. // configuration
  1167. if (options && 'maxWorkers' in options) {
  1168. validateMaxWorkers(options.maxWorkers);
  1169. /** @readonly */
  1170. this.maxWorkers = options.maxWorkers;
  1171. } else {
  1172. this.maxWorkers = Math.max((environment.cpus || 4) - 1, 1);
  1173. }
  1174. if (options && 'minWorkers' in options) {
  1175. if (options.minWorkers === 'max') {
  1176. /** @readonly */
  1177. this.minWorkers = this.maxWorkers;
  1178. } else {
  1179. validateMinWorkers(options.minWorkers);
  1180. this.minWorkers = options.minWorkers;
  1181. this.maxWorkers = Math.max(this.minWorkers, this.maxWorkers); // in case minWorkers is higher than maxWorkers
  1182. }
  1183. this._ensureMinWorkers();
  1184. }
  1185. /** @private */
  1186. this._boundNext = this._next.bind(this);
  1187. if (this.workerType === 'thread') {
  1188. WorkerHandler.ensureWorkerThreads();
  1189. }
  1190. }
  1191. /**
  1192. * Execute a function on a worker.
  1193. *
  1194. * Example usage:
  1195. *
  1196. * var pool = new Pool()
  1197. *
  1198. * // call a function available on the worker
  1199. * pool.exec('fibonacci', [6])
  1200. *
  1201. * // offload a function
  1202. * function add(a, b) {
  1203. * return a + b
  1204. * };
  1205. * pool.exec(add, [2, 4])
  1206. * .then(function (result) {
  1207. * console.log(result); // outputs 6
  1208. * })
  1209. * .catch(function(error) {
  1210. * console.log(error);
  1211. * });
  1212. * @template { (...args: any[]) => any } T
  1213. * @param {String | T} method Function name or function.
  1214. * If `method` is a string, the corresponding
  1215. * method on the worker will be executed
  1216. * If `method` is a Function, the function
  1217. * will be stringified and executed via the
  1218. * workers built-in function `run(fn, args)`.
  1219. * @param {Parameters<T> | null} [params] Function arguments applied when calling the function
  1220. * @param {import('./types.js').ExecOptions} [options] Options
  1221. * @return {Promise<ReturnType<T>>}
  1222. */
  1223. Pool.prototype.exec = function (method, params, options) {
  1224. // validate type of arguments
  1225. if (params && !Array.isArray(params)) {
  1226. throw new TypeError('Array expected as argument "params"');
  1227. }
  1228. if (typeof method === 'string') {
  1229. var resolver = Promise.defer();
  1230. if (this.tasks.length >= this.maxQueueSize) {
  1231. throw new Error('Max queue size of ' + this.maxQueueSize + ' reached');
  1232. }
  1233. // add a new task to the queue
  1234. var tasks = this.tasks;
  1235. var task = {
  1236. method: method,
  1237. params: params,
  1238. resolver: resolver,
  1239. timeout: null,
  1240. options: options
  1241. };
  1242. tasks.push(task);
  1243. // replace the timeout method of the Promise with our own,
  1244. // which starts the timer as soon as the task is actually started
  1245. var originalTimeout = resolver.promise.timeout;
  1246. resolver.promise.timeout = function timeout(delay) {
  1247. if (tasks.indexOf(task) !== -1) {
  1248. // task is still queued -> start the timer later on
  1249. task.timeout = delay;
  1250. return resolver.promise;
  1251. } else {
  1252. // task is already being executed -> start timer immediately
  1253. return originalTimeout.call(resolver.promise, delay);
  1254. }
  1255. };
  1256. // trigger task execution
  1257. this._next();
  1258. return resolver.promise;
  1259. } else if (typeof method === 'function') {
  1260. // send stringified function and function arguments to worker
  1261. return this.exec('run', [String(method), params], options);
  1262. } else {
  1263. throw new TypeError('Function or string expected as argument "method"');
  1264. }
  1265. };
  1266. /**
  1267. * Create a proxy for current worker. Returns an object containing all
  1268. * methods available on the worker. All methods return promises resolving the methods result.
  1269. * @template { { [k: string]: (...args: any[]) => any } } T
  1270. * @return {Promise<import('./types.js').Proxy<T>, Error>} Returns a promise which resolves with a proxy object
  1271. */
  1272. Pool.prototype.proxy = function () {
  1273. if (arguments.length > 0) {
  1274. throw new Error('No arguments expected');
  1275. }
  1276. var pool = this;
  1277. return this.exec('methods').then(function (methods) {
  1278. var proxy = {};
  1279. methods.forEach(function (method) {
  1280. proxy[method] = function () {
  1281. return pool.exec(method, Array.prototype.slice.call(arguments));
  1282. };
  1283. });
  1284. return proxy;
  1285. });
  1286. };
  1287. /**
  1288. * Creates new array with the results of calling a provided callback function
  1289. * on every element in this array.
  1290. * @param {Array} array
  1291. * @param {function} callback Function taking two arguments:
  1292. * `callback(currentValue, index)`
  1293. * @return {Promise.<Array>} Returns a promise which resolves with an Array
  1294. * containing the results of the callback function
  1295. * executed for each of the array elements.
  1296. */
  1297. /* TODO: implement map
  1298. Pool.prototype.map = function (array, callback) {
  1299. };
  1300. */
  1301. /**
  1302. * Grab the first task from the queue, find a free worker, and assign the
  1303. * worker to the task.
  1304. * @private
  1305. */
  1306. Pool.prototype._next = function () {
  1307. if (this.tasks.length > 0) {
  1308. // there are tasks in the queue
  1309. // find an available worker
  1310. var worker = this._getWorker();
  1311. if (worker) {
  1312. // get the first task from the queue
  1313. var me = this;
  1314. var task = this.tasks.shift();
  1315. // check if the task is still pending (and not cancelled -> promise rejected)
  1316. if (task.resolver.promise.pending) {
  1317. // send the request to the worker
  1318. var promise = worker.exec(task.method, task.params, task.resolver, task.options).then(me._boundNext).catch(function () {
  1319. // if the worker crashed and terminated, remove it from the pool
  1320. if (worker.terminated) {
  1321. return me._removeWorker(worker);
  1322. }
  1323. }).then(function () {
  1324. me._next(); // trigger next task in the queue
  1325. });
  1326. // start queued timer now
  1327. if (typeof task.timeout === 'number') {
  1328. promise.timeout(task.timeout);
  1329. }
  1330. } else {
  1331. // The task taken was already complete (either rejected or resolved), so just trigger next task in the queue
  1332. me._next();
  1333. }
  1334. }
  1335. }
  1336. };
  1337. /**
  1338. * Get an available worker. If no worker is available and the maximum number
  1339. * of workers isn't yet reached, a new worker will be created and returned.
  1340. * If no worker is available and the maximum number of workers is reached,
  1341. * null will be returned.
  1342. *
  1343. * @return {WorkerHandler | null} worker
  1344. * @private
  1345. */
  1346. Pool.prototype._getWorker = function () {
  1347. // find a non-busy worker
  1348. var workers = this.workers;
  1349. for (var i = 0; i < workers.length; i++) {
  1350. var worker = workers[i];
  1351. if (worker.busy() === false) {
  1352. return worker;
  1353. }
  1354. }
  1355. if (workers.length < this.maxWorkers) {
  1356. // create a new worker
  1357. worker = this._createWorkerHandler();
  1358. workers.push(worker);
  1359. return worker;
  1360. }
  1361. return null;
  1362. };
  1363. /**
  1364. * Remove a worker from the pool.
  1365. * Attempts to terminate worker if not already terminated, and ensures the minimum
  1366. * pool size is met.
  1367. * @param {WorkerHandler} worker
  1368. * @return {Promise<WorkerHandler>}
  1369. * @private
  1370. */
  1371. Pool.prototype._removeWorker = function (worker) {
  1372. var me = this;
  1373. DEBUG_PORT_ALLOCATOR.releasePort(worker.debugPort);
  1374. // _removeWorker will call this, but we need it to be removed synchronously
  1375. this._removeWorkerFromList(worker);
  1376. // If minWorkers set, spin up new workers to replace the crashed ones
  1377. this._ensureMinWorkers();
  1378. // terminate the worker (if not already terminated)
  1379. return new Promise(function (resolve, reject) {
  1380. worker.terminate(false, function (err) {
  1381. me.onTerminateWorker({
  1382. forkArgs: worker.forkArgs,
  1383. forkOpts: worker.forkOpts,
  1384. workerThreadOpts: worker.workerThreadOpts,
  1385. script: worker.script
  1386. });
  1387. if (err) {
  1388. reject(err);
  1389. } else {
  1390. resolve(worker);
  1391. }
  1392. });
  1393. });
  1394. };
  1395. /**
  1396. * Remove a worker from the pool list.
  1397. * @param {WorkerHandler} worker
  1398. * @private
  1399. */
  1400. Pool.prototype._removeWorkerFromList = function (worker) {
  1401. // remove from the list with workers
  1402. var index = this.workers.indexOf(worker);
  1403. if (index !== -1) {
  1404. this.workers.splice(index, 1);
  1405. }
  1406. };
  1407. /**
  1408. * Close all active workers. Tasks currently being executed will be finished first.
  1409. * @param {boolean} [force=false] If false (default), the workers are terminated
  1410. * after finishing all tasks currently in
  1411. * progress. If true, the workers will be
  1412. * terminated immediately.
  1413. * @param {number} [timeout] If provided and non-zero, worker termination promise will be rejected
  1414. * after timeout if worker process has not been terminated.
  1415. * @return {Promise.<void, Error>}
  1416. */
  1417. Pool.prototype.terminate = function (force, timeout) {
  1418. var me = this;
  1419. // cancel any pending tasks
  1420. this.tasks.forEach(function (task) {
  1421. task.resolver.reject(new Error('Pool terminated'));
  1422. });
  1423. this.tasks.length = 0;
  1424. var f = function f(worker) {
  1425. DEBUG_PORT_ALLOCATOR.releasePort(worker.debugPort);
  1426. this._removeWorkerFromList(worker);
  1427. };
  1428. var removeWorker = f.bind(this);
  1429. var promises = [];
  1430. var workers = this.workers.slice();
  1431. workers.forEach(function (worker) {
  1432. var termPromise = worker.terminateAndNotify(force, timeout).then(removeWorker).always(function () {
  1433. me.onTerminateWorker({
  1434. forkArgs: worker.forkArgs,
  1435. forkOpts: worker.forkOpts,
  1436. workerThreadOpts: worker.workerThreadOpts,
  1437. script: worker.script
  1438. });
  1439. });
  1440. promises.push(termPromise);
  1441. });
  1442. return Promise.all(promises);
  1443. };
  1444. /**
  1445. * Retrieve statistics on tasks and workers.
  1446. * @return {{totalWorkers: number, busyWorkers: number, idleWorkers: number, pendingTasks: number, activeTasks: number}} Returns an object with statistics
  1447. */
  1448. Pool.prototype.stats = function () {
  1449. var totalWorkers = this.workers.length;
  1450. var busyWorkers = this.workers.filter(function (worker) {
  1451. return worker.busy();
  1452. }).length;
  1453. return {
  1454. totalWorkers: totalWorkers,
  1455. busyWorkers: busyWorkers,
  1456. idleWorkers: totalWorkers - busyWorkers,
  1457. pendingTasks: this.tasks.length,
  1458. activeTasks: busyWorkers
  1459. };
  1460. };
  1461. /**
  1462. * Ensures that a minimum of minWorkers is up and running
  1463. * @private
  1464. */
  1465. Pool.prototype._ensureMinWorkers = function () {
  1466. if (this.minWorkers) {
  1467. for (var i = this.workers.length; i < this.minWorkers; i++) {
  1468. this.workers.push(this._createWorkerHandler());
  1469. }
  1470. }
  1471. };
  1472. /**
  1473. * Helper function to create a new WorkerHandler and pass all options.
  1474. * @return {WorkerHandler}
  1475. * @private
  1476. */
  1477. Pool.prototype._createWorkerHandler = function () {
  1478. var overriddenParams = this.onCreateWorker({
  1479. forkArgs: this.forkArgs,
  1480. forkOpts: this.forkOpts,
  1481. workerOpts: this.workerOpts,
  1482. workerThreadOpts: this.workerThreadOpts,
  1483. script: this.script
  1484. }) || {};
  1485. return new WorkerHandler(overriddenParams.script || this.script, {
  1486. forkArgs: overriddenParams.forkArgs || this.forkArgs,
  1487. forkOpts: overriddenParams.forkOpts || this.forkOpts,
  1488. workerOpts: overriddenParams.workerOpts || this.workerOpts,
  1489. workerThreadOpts: overriddenParams.workerThreadOpts || this.workerThreadOpts,
  1490. debugPort: DEBUG_PORT_ALLOCATOR.nextAvailableStartingAt(this.debugPortStart),
  1491. workerType: this.workerType,
  1492. workerTerminateTimeout: this.workerTerminateTimeout,
  1493. emitStdStreams: this.emitStdStreams
  1494. });
  1495. };
  1496. /**
  1497. * Ensure that the maxWorkers option is an integer >= 1
  1498. * @param {*} maxWorkers
  1499. * @returns {boolean} returns true maxWorkers has a valid value
  1500. */
  1501. function validateMaxWorkers(maxWorkers) {
  1502. if (!isNumber(maxWorkers) || !isInteger(maxWorkers) || maxWorkers < 1) {
  1503. throw new TypeError('Option maxWorkers must be an integer number >= 1');
  1504. }
  1505. }
  1506. /**
  1507. * Ensure that the minWorkers option is an integer >= 0
  1508. * @param {*} minWorkers
  1509. * @returns {boolean} returns true when minWorkers has a valid value
  1510. */
  1511. function validateMinWorkers(minWorkers) {
  1512. if (!isNumber(minWorkers) || !isInteger(minWorkers) || minWorkers < 0) {
  1513. throw new TypeError('Option minWorkers must be an integer number >= 0');
  1514. }
  1515. }
  1516. /**
  1517. * Test whether a variable is a number
  1518. * @param {*} value
  1519. * @returns {boolean} returns true when value is a number
  1520. */
  1521. function isNumber(value) {
  1522. return typeof value === 'number';
  1523. }
  1524. /**
  1525. * Test whether a number is an integer
  1526. * @param {number} value
  1527. * @returns {boolean} Returns true if value is an integer
  1528. */
  1529. function isInteger(value) {
  1530. return Math.round(value) == value;
  1531. }
  1532. Pool_1 = Pool;
  1533. return Pool_1;
  1534. }
  1535. var worker$1 = {};
  1536. /**
  1537. * The helper class for transferring data from the worker to the main thread.
  1538. *
  1539. * @param {Object} message The object to deliver to the main thread.
  1540. * @param {Object[]} transfer An array of transferable Objects to transfer ownership of.
  1541. */
  1542. var transfer;
  1543. var hasRequiredTransfer;
  1544. function requireTransfer() {
  1545. if (hasRequiredTransfer) return transfer;
  1546. hasRequiredTransfer = 1;
  1547. function Transfer(message, transfer) {
  1548. this.message = message;
  1549. this.transfer = transfer;
  1550. }
  1551. transfer = Transfer;
  1552. return transfer;
  1553. }
  1554. var hasRequiredWorker;
  1555. function requireWorker() {
  1556. if (hasRequiredWorker) return worker$1;
  1557. hasRequiredWorker = 1;
  1558. (function (exports) {
  1559. var Transfer = requireTransfer();
  1560. /**
  1561. * worker must handle async cleanup handlers. Use custom Promise implementation.
  1562. */
  1563. var Promise = require_Promise().Promise;
  1564. /**
  1565. * Special message sent by parent which causes the worker to terminate itself.
  1566. * Not a "message object"; this string is the entire message.
  1567. */
  1568. var TERMINATE_METHOD_ID = '__workerpool-terminate__';
  1569. /**
  1570. * Special message by parent which causes a child process worker to perform cleaup
  1571. * steps before determining if the child process worker should be terminated.
  1572. */
  1573. var CLEANUP_METHOD_ID = '__workerpool-cleanup__';
  1574. // var nodeOSPlatform = require('./environment').nodeOSPlatform;
  1575. var TIMEOUT_DEFAULT = 1000;
  1576. // create a worker API for sending and receiving messages which works both on
  1577. // node.js and in the browser
  1578. var worker = {
  1579. exit: function exit() {}
  1580. };
  1581. // api for in worker communication with parent process
  1582. // works in both node.js and the browser
  1583. var publicWorker = {
  1584. /**
  1585. * Registers listeners which will trigger when a task is timed out or cancled. If all listeners resolve, the worker executing the given task will not be terminated.
  1586. * *Note*: If there is a blocking operation within a listener, the worker will be terminated.
  1587. * @param {() => Promise<void>} listener
  1588. */
  1589. addAbortListener: function addAbortListener(listener) {
  1590. worker.abortListeners.push(listener);
  1591. },
  1592. /**
  1593. * Emit an event from the worker thread to the main thread.
  1594. * @param {any} payload
  1595. */
  1596. emit: worker.emit
  1597. };
  1598. if (typeof self !== 'undefined' && typeof postMessage === 'function' && typeof addEventListener === 'function') {
  1599. // worker in the browser
  1600. worker.on = function (event, callback) {
  1601. addEventListener(event, function (message) {
  1602. callback(message.data);
  1603. });
  1604. };
  1605. worker.send = function (message, transfer) {
  1606. transfer ? postMessage(message, transfer) : postMessage(message);
  1607. };
  1608. } else if (typeof process !== 'undefined') {
  1609. // node.js
  1610. var WorkerThreads;
  1611. try {
  1612. WorkerThreads = require('worker_threads');
  1613. } catch (error) {
  1614. if (_typeof(error) === 'object' && error !== null && error.code === 'MODULE_NOT_FOUND') ; else {
  1615. throw error;
  1616. }
  1617. }
  1618. if (WorkerThreads && /* if there is a parentPort, we are in a WorkerThread */
  1619. WorkerThreads.parentPort !== null) {
  1620. var parentPort = WorkerThreads.parentPort;
  1621. worker.send = parentPort.postMessage.bind(parentPort);
  1622. worker.on = parentPort.on.bind(parentPort);
  1623. worker.exit = process.exit.bind(process);
  1624. } else {
  1625. worker.on = process.on.bind(process);
  1626. // ignore transfer argument since it is not supported by process
  1627. worker.send = function (message) {
  1628. process.send(message);
  1629. };
  1630. // register disconnect handler only for subprocess worker to exit when parent is killed unexpectedly
  1631. worker.on('disconnect', function () {
  1632. process.exit(1);
  1633. });
  1634. worker.exit = process.exit.bind(process);
  1635. }
  1636. } else {
  1637. throw new Error('Script must be executed as a worker');
  1638. }
  1639. function convertError(error) {
  1640. if (error && error.toJSON) {
  1641. return JSON.parse(JSON.stringify(error));
  1642. }
  1643. // turn a class like Error (having non-enumerable properties) into a plain object
  1644. return JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error)));
  1645. }
  1646. /**
  1647. * Test whether a value is a Promise via duck typing.
  1648. * @param {*} value
  1649. * @returns {boolean} Returns true when given value is an object
  1650. * having functions `then` and `catch`.
  1651. */
  1652. function isPromise(value) {
  1653. return value && typeof value.then === 'function' && typeof value.catch === 'function';
  1654. }
  1655. // functions available externally
  1656. worker.methods = {};
  1657. /**
  1658. * Execute a function with provided arguments
  1659. * @param {String} fn Stringified function
  1660. * @param {Array} [args] Function arguments
  1661. * @returns {*}
  1662. */
  1663. worker.methods.run = function run(fn, args) {
  1664. var f = new Function('return (' + fn + ').apply(this, arguments);');
  1665. f.worker = publicWorker;
  1666. return f.apply(f, args);
  1667. };
  1668. /**
  1669. * Get a list with methods available on this worker
  1670. * @return {String[]} methods
  1671. */
  1672. worker.methods.methods = function methods() {
  1673. return Object.keys(worker.methods);
  1674. };
  1675. /**
  1676. * Custom handler for when the worker is terminated.
  1677. */
  1678. worker.terminationHandler = undefined;
  1679. worker.abortListenerTimeout = TIMEOUT_DEFAULT;
  1680. /**
  1681. * Abort handlers for resolving errors which may cause a timeout or cancellation
  1682. * to occur from a worker context
  1683. */
  1684. worker.abortListeners = [];
  1685. /**
  1686. * Cleanup and exit the worker.
  1687. * @param {Number} code
  1688. * @returns {Promise<void>}
  1689. */
  1690. worker.terminateAndExit = function (code) {
  1691. var _exit = function _exit() {
  1692. worker.exit(code);
  1693. };
  1694. if (!worker.terminationHandler) {
  1695. return _exit();
  1696. }
  1697. var result = worker.terminationHandler(code);
  1698. if (isPromise(result)) {
  1699. result.then(_exit, _exit);
  1700. return result;
  1701. } else {
  1702. _exit();
  1703. return new Promise(function (_resolve, reject) {
  1704. reject(new Error("Worker terminating"));
  1705. });
  1706. }
  1707. };
  1708. /**
  1709. * Called within the worker message handler to run abort handlers if registered to perform cleanup operations.
  1710. * @param {Integer} [requestId] id of task which is currently executing in the worker
  1711. * @return {Promise<void>}
  1712. */
  1713. worker.cleanup = function (requestId) {
  1714. if (!worker.abortListeners.length) {
  1715. worker.send({
  1716. id: requestId,
  1717. method: CLEANUP_METHOD_ID,
  1718. error: convertError(new Error('Worker terminating'))
  1719. });
  1720. // If there are no handlers registered, reject the promise with an error as we want the handler to be notified
  1721. // that cleanup should begin and the handler should be GCed.
  1722. return new Promise(function (resolve) {
  1723. resolve();
  1724. });
  1725. }
  1726. var _exit = function _exit() {
  1727. worker.exit();
  1728. };
  1729. var _abort = function _abort() {
  1730. if (!worker.abortListeners.length) {
  1731. worker.abortListeners = [];
  1732. }
  1733. };
  1734. var promises = worker.abortListeners.map(function (listener) {
  1735. return listener();
  1736. });
  1737. var timerId;
  1738. var timeoutPromise = new Promise(function (_resolve, reject) {
  1739. timerId = setTimeout(function () {
  1740. reject(new Error('Timeout occured waiting for abort handler, killing worker'));
  1741. }, worker.abortListenerTimeout);
  1742. });
  1743. // Once a promise settles we need to clear the timeout to prevet fulfulling the promise twice
  1744. var settlePromise = Promise.all(promises).then(function () {
  1745. clearTimeout(timerId);
  1746. _abort();
  1747. }, function () {
  1748. clearTimeout(timerId);
  1749. _exit();
  1750. });
  1751. // Returns a promise which will result in one of the following cases
  1752. // - Resolve once all handlers resolve
  1753. // - Reject if one or more handlers exceed the 'abortListenerTimeout' interval
  1754. // - Reject if one or more handlers reject
  1755. // Upon one of the above cases a message will be sent to the handler with the result of the handler execution
  1756. // which will either kill the worker if the result contains an error, or keep it in the pool if the result
  1757. // does not contain an error.
  1758. return new Promise(function (resolve, reject) {
  1759. settlePromise.then(resolve, reject);
  1760. timeoutPromise.then(resolve, reject);
  1761. }).then(function () {
  1762. worker.send({
  1763. id: requestId,
  1764. method: CLEANUP_METHOD_ID,
  1765. error: null
  1766. });
  1767. }, function (err) {
  1768. worker.send({
  1769. id: requestId,
  1770. method: CLEANUP_METHOD_ID,
  1771. error: err ? convertError(err) : null
  1772. });
  1773. });
  1774. };
  1775. var currentRequestId = null;
  1776. worker.on('message', function (request) {
  1777. if (request === TERMINATE_METHOD_ID) {
  1778. return worker.terminateAndExit(0);
  1779. }
  1780. if (request.method === CLEANUP_METHOD_ID) {
  1781. return worker.cleanup(request.id);
  1782. }
  1783. try {
  1784. var method = worker.methods[request.method];
  1785. if (method) {
  1786. currentRequestId = request.id;
  1787. // execute the function
  1788. var result = method.apply(method, request.params);
  1789. if (isPromise(result)) {
  1790. // promise returned, resolve this and then return
  1791. result.then(function (result) {
  1792. if (result instanceof Transfer) {
  1793. worker.send({
  1794. id: request.id,
  1795. result: result.message,
  1796. error: null
  1797. }, result.transfer);
  1798. } else {
  1799. worker.send({
  1800. id: request.id,
  1801. result: result,
  1802. error: null
  1803. });
  1804. }
  1805. currentRequestId = null;
  1806. }).catch(function (err) {
  1807. worker.send({
  1808. id: request.id,
  1809. result: null,
  1810. error: convertError(err)
  1811. });
  1812. currentRequestId = null;
  1813. });
  1814. } else {
  1815. // immediate result
  1816. if (result instanceof Transfer) {
  1817. worker.send({
  1818. id: request.id,
  1819. result: result.message,
  1820. error: null
  1821. }, result.transfer);
  1822. } else {
  1823. worker.send({
  1824. id: request.id,
  1825. result: result,
  1826. error: null
  1827. });
  1828. }
  1829. currentRequestId = null;
  1830. }
  1831. } else {
  1832. throw new Error('Unknown method "' + request.method + '"');
  1833. }
  1834. } catch (err) {
  1835. worker.send({
  1836. id: request.id,
  1837. result: null,
  1838. error: convertError(err)
  1839. });
  1840. }
  1841. });
  1842. /**
  1843. * Register methods to the worker
  1844. * @param {Object} [methods]
  1845. * @param {import('./types.js').WorkerRegisterOptions} [options]
  1846. */
  1847. worker.register = function (methods, options) {
  1848. if (methods) {
  1849. for (var name in methods) {
  1850. if (methods.hasOwnProperty(name)) {
  1851. worker.methods[name] = methods[name];
  1852. worker.methods[name].worker = publicWorker;
  1853. }
  1854. }
  1855. }
  1856. if (options) {
  1857. worker.terminationHandler = options.onTerminate;
  1858. // register listener timeout or default to 1 second
  1859. worker.abortListenerTimeout = options.abortListenerTimeout || TIMEOUT_DEFAULT;
  1860. }
  1861. worker.send('ready');
  1862. };
  1863. worker.emit = function (payload) {
  1864. if (currentRequestId) {
  1865. if (payload instanceof Transfer) {
  1866. worker.send({
  1867. id: currentRequestId,
  1868. isEvent: true,
  1869. payload: payload.message
  1870. }, payload.transfer);
  1871. return;
  1872. }
  1873. worker.send({
  1874. id: currentRequestId,
  1875. isEvent: true,
  1876. payload: payload
  1877. });
  1878. }
  1879. };
  1880. {
  1881. exports.add = worker.register;
  1882. exports.emit = worker.emit;
  1883. }
  1884. })(worker$1);
  1885. return worker$1;
  1886. }
  1887. var platform = environmentExports.platform,
  1888. isMainThread = environmentExports.isMainThread,
  1889. cpus = environmentExports.cpus;
  1890. /** @typedef {import("./Pool")} Pool */
  1891. /** @typedef {import("./types.js").WorkerPoolOptions} WorkerPoolOptions */
  1892. /** @typedef {import("./types.js").WorkerRegisterOptions} WorkerRegisterOptions */
  1893. /**
  1894. * @template { { [k: string]: (...args: any[]) => any } } T
  1895. * @typedef {import('./types.js').Proxy<T>} Proxy<T>
  1896. */
  1897. /**
  1898. * @overload
  1899. * Create a new worker pool
  1900. * @param {WorkerPoolOptions} [script]
  1901. * @returns {Pool} pool
  1902. */
  1903. /**
  1904. * @overload
  1905. * Create a new worker pool
  1906. * @param {string} [script]
  1907. * @param {WorkerPoolOptions} [options]
  1908. * @returns {Pool} pool
  1909. */
  1910. function pool(script, options) {
  1911. var Pool = requirePool();
  1912. return new Pool(script, options);
  1913. }
  1914. var pool_1 = src.pool = pool;
  1915. /**
  1916. * Create a worker and optionally register a set of methods to the worker.
  1917. * @param {{ [k: string]: (...args: any[]) => any }} [methods]
  1918. * @param {WorkerRegisterOptions} [options]
  1919. */
  1920. function worker(methods, options) {
  1921. var worker = requireWorker();
  1922. worker.add(methods, options);
  1923. }
  1924. var worker_1 = src.worker = worker;
  1925. /**
  1926. * Sends an event to the parent worker pool.
  1927. * @param {any} payload
  1928. */
  1929. function workerEmit(payload) {
  1930. var worker = requireWorker();
  1931. worker.emit(payload);
  1932. }
  1933. var workerEmit_1 = src.workerEmit = workerEmit;
  1934. var _require$$ = require_Promise(),
  1935. Promise$1 = _require$$.Promise;
  1936. var _Promise = src.Promise = Promise$1;
  1937. var Transfer = src.Transfer = requireTransfer();
  1938. var platform_1 = src.platform = platform;
  1939. var isMainThread_1 = src.isMainThread = isMainThread;
  1940. var cpus_1 = src.cpus = cpus;
  1941. exports.Promise = _Promise;
  1942. exports.Transfer = Transfer;
  1943. exports.cpus = cpus_1;
  1944. exports.default = src;
  1945. exports.isMainThread = isMainThread_1;
  1946. exports.platform = platform_1;
  1947. exports.pool = pool_1;
  1948. exports.worker = worker_1;
  1949. exports.workerEmit = workerEmit_1;
  1950. Object.defineProperty(exports, '__esModule', { value: true });
  1951. }));
  1952. //# sourceMappingURL=workerpool.js.map