Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

poolifier

Package Overview
Dependencies
Maintainers
1
Versions
192
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

poolifier - npm Package Compare versions

Comparing version 2.6.37 to 2.6.38

2

./lib/index.js

@@ -1,1 +0,1 @@

"use strict";var e=require("node:events"),t=require("node:worker_threads"),s=require("node:cluster"),r=require("node:crypto"),i=require("node:perf_hooks"),o=require("node:fs"),n=require("node:os"),a=require("node:async_hooks");function h(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(s){if("default"!==s){var r=Object.getOwnPropertyDescriptor(e,s);Object.defineProperty(t,s,r.get?r:{enumerable:!0,get:function(){return e[s]}})}})),t.default=e,Object.freeze(t)}var u=h(n);const k=Object.freeze({fixed:"fixed",dynamic:"dynamic"});class c extends e.EventEmitter{}const d=Object.freeze({ready:"ready",busy:"busy",full:"full",destroy:"destroy",error:"error",taskError:"taskError",backPressure:"backPressure"}),l=Object.freeze({thread:"thread",cluster:"cluster"}),g="default",m=Object.freeze((()=>{})),p={retries:6,runTime:{median:!1},waitTime:{median:!1},elu:{median:!1}},w={aggregate:!1,average:!1,median:!1},y=e=>e instanceof t.Worker?l.thread:e instanceof s.Worker?l.cluster:void 0,f=e=>e instanceof t.Worker?e.threadId:e instanceof s.Worker?e.id:void 0,T=e=>Array.isArray(e)&&0===e.length?0:Array.isArray(e)&&1===e.length?e[0]:e.reduce(((e,t)=>e+t),0)/e.length,W=e=>{if(Array.isArray(e)&&0===e.length)return 0;if(Array.isArray(e)&&1===e.length)return e[0];const t=e.slice().sort(((e,t)=>e-t));return(t[t.length-1>>1]+t[t.length>>1])/2},N=(e,t=2)=>{const s=Math.pow(10,t);return Math.round(e*s*(1+Number.EPSILON))/s},x=e=>"object"==typeof e&&null!==e&&e?.constructor===Object&&"[object Object]"===Object.prototype.toString.call(e),S=(e,t)=>t===e,E=e=>"function"==typeof e&&"AsyncFunction"===e.constructor.name,v=(e,t,s)=>{t.aggregate&&(e.aggregate=(e.aggregate??0)+s,e.minimum=Math.min(s,e.minimum??1/0),e.maximum=Math.max(s,e.maximum??-1/0),(t.average||t.median)&&null!=s&&(e.history.push(s),t.average?e.average=T(e.history):null!=e.average&&delete e.average,t.median?e.median=W(e.history):null!=e.median&&delete e.median))},b=()=>r.webcrypto.getRandomValues(new Uint32Array(1))[0]/4294967296,C=Object.freeze({SOFT:"SOFT",HARD:"HARD"}),I=Object.freeze({ROUND_ROBIN:"ROUND_ROBIN",LEAST_USED:"LEAST_USED",LEAST_BUSY:"LEAST_BUSY",LEAST_ELU:"LEAST_ELU",FAIR_SHARE:"FAIR_SHARE",WEIGHTED_ROUND_ROBIN:"WEIGHTED_ROUND_ROBIN",INTERLEAVED_WEIGHTED_ROUND_ROBIN:"INTERLEAVED_WEIGHTED_ROUND_ROBIN"}),R=Object.freeze({runTime:"runTime",waitTime:"waitTime",elu:"elu"});class z{pool;opts;nextWorkerNodeKey=0;previousWorkerNodeKey=0;strategyPolicy={dynamicWorkerUsage:!1,dynamicWorkerReady:!0};taskStatisticsRequirements={runTime:w,waitTime:w,elu:w};constructor(e,t=p){this.pool=e,this.opts=t,this.opts={...p,...t},this.choose=this.choose.bind(this)}setTaskStatisticsRequirements(e){this.toggleMedianMeasurementStatisticsRequirements(this.taskStatisticsRequirements.runTime,e.runTime?.median),this.toggleMedianMeasurementStatisticsRequirements(this.taskStatisticsRequirements.waitTime,e.waitTime?.median),this.toggleMedianMeasurementStatisticsRequirements(this.taskStatisticsRequirements.elu,e.elu?.median)}toggleMedianMeasurementStatisticsRequirements(e,t){e.average&&t&&(e.average=!1,e.median=t),e.median&&!t&&(e.average=!0,e.median=t)}resetWorkerNodeKeyProperties(){this.nextWorkerNodeKey=0,this.previousWorkerNodeKey=0}setOptions(e){this.opts={...p,...e},this.setTaskStatisticsRequirements(this.opts)}isWorkerNodeReady(e){return this.pool.workerNodes[e].info.ready}hasWorkerNodeBackPressure(e){return this.pool.hasWorkerNodeBackPressure(e)}isWorkerNodeEligible(e){return this.isWorkerNodeReady(e)&&!this.hasWorkerNodeBackPressure(e)}getWorkerTaskRunTime(e){return this.taskStatisticsRequirements.runTime.median?this.pool.workerNodes[e].usage.runTime?.median??0:this.pool.workerNodes[e].usage.runTime?.average??0}getWorkerTaskWaitTime(e){return this.taskStatisticsRequirements.waitTime.median?this.pool.workerNodes[e].usage.waitTime?.median??0:this.pool.workerNodes[e].usage.waitTime?.average??0}getWorkerTaskElu(e){return this.taskStatisticsRequirements.elu.median?this.pool.workerNodes[e].usage.elu.active?.median??0:this.pool.workerNodes[e].usage.elu.active?.average??0}checkNextWorkerNodeEligibility(e){this.isWorkerNodeEligible(this.nextWorkerNodeKey)||(this.nextWorkerNodeKey=void 0,this.previousWorkerNodeKey=e??this.previousWorkerNodeKey)}computeDefaultWorkerWeight(){let e=0;for(const t of n.cpus()){const s=t.speed.toString().length-1;e+=1/(t.speed/Math.pow(10,s))*Math.pow(10,s)}return Math.round(e/n.cpus().length)}}class O extends z{taskStatisticsRequirements={runTime:{aggregate:!0,average:!0,median:!1},waitTime:w,elu:{aggregate:!0,average:!0,median:!1}};workersVirtualTaskEndTimestamp=[];constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return this.workersVirtualTaskEndTimestamp=[],!0}update(e){return this.computeWorkerVirtualTaskEndTimestamp(e),!0}choose(){return this.nextWorkerNodeKey=this.fairShareNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(e){return this.workersVirtualTaskEndTimestamp.splice(e,1),!0}fairShareNextWorkerNodeKey(){let e,t=1/0;for(const[s]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;null==this.workersVirtualTaskEndTimestamp[s]&&this.computeWorkerVirtualTaskEndTimestamp(s);const r=this.workersVirtualTaskEndTimestamp[s];r<t&&(t=r,e=s)}return e}computeWorkerVirtualTaskEndTimestamp(e){this.workersVirtualTaskEndTimestamp[e]=this.getWorkerVirtualTaskEndTimestamp(e,this.getWorkerVirtualTaskStartTimestamp(e))}getWorkerVirtualTaskEndTimestamp(e,t){return t+(this.opts.measurement===R.elu?this.getWorkerTaskElu(e):this.getWorkerTaskRunTime(e))}getWorkerVirtualTaskStartTimestamp(e){return Math.max(performance.now(),this.workersVirtualTaskEndTimestamp[e]??-1/0)}}class F extends z{roundId=0;roundWeights;defaultWorkerWeight;constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts),this.defaultWorkerWeight=this.computeDefaultWorkerWeight(),this.roundWeights=this.getRoundWeights()}reset(){return this.resetWorkerNodeKeyProperties(),this.roundId=0,!0}update(){return!0}choose(){let e,t;for(let s=this.roundId;s<this.roundWeights.length;s++){e=s;for(let e=this.nextWorkerNodeKey??this.previousWorkerNodeKey;e<this.pool.workerNodes.length;e++){if(!this.isWorkerNodeEligible(e))continue;if((this.opts.weights?.[e]??this.defaultWorkerWeight)>=this.roundWeights[s]){t=e;break}}}this.roundId=e,null==t&&(this.previousWorkerNodeKey=this.nextWorkerNodeKey??this.previousWorkerNodeKey),this.nextWorkerNodeKey=t;const s=this.nextWorkerNodeKey;return this.nextWorkerNodeKey===this.pool.workerNodes.length-1?(this.nextWorkerNodeKey=0,this.roundId=this.roundId===this.roundWeights.length-1?0:this.roundId+1):this.nextWorkerNodeKey=(this.nextWorkerNodeKey??this.previousWorkerNodeKey)+1,s}remove(e){return this.nextWorkerNodeKey===e&&(0===this.pool.workerNodes.length?this.nextWorkerNodeKey=0:this.nextWorkerNodeKey>this.pool.workerNodes.length-1&&(this.nextWorkerNodeKey=this.pool.workerNodes.length-1,this.roundId=this.roundId===this.roundWeights.length-1?0:this.roundId+1)),!0}setOptions(e){super.setOptions(e),this.roundWeights=this.getRoundWeights()}getRoundWeights(){return null==this.opts.weights?[this.defaultWorkerWeight]:[...new Set(Object.values(this.opts.weights).slice().sort(((e,t)=>e-t)))]}}class M extends z{taskStatisticsRequirements={runTime:{aggregate:!0,average:!1,median:!1},waitTime:{aggregate:!0,average:!1,median:!1},elu:w};constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return!0}update(){return!0}choose(){return this.nextWorkerNodeKey=this.leastBusyNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(){return!0}leastBusyNextWorkerNodeKey(){let e,t=1/0;for(const[s,r]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;const i=(r.usage.runTime?.aggregate??0)+(r.usage.waitTime?.aggregate??0);if(0===i){e=s;break}i<t&&(t=i,e=s)}return e}}class Q extends z{constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return!0}update(){return!0}choose(){return this.nextWorkerNodeKey=this.leastUsedNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(){return!0}leastUsedNextWorkerNodeKey(){let e,t=1/0;for(const[s,r]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;const i=r.usage.tasks,o=i.executed+i.executing+i.queued;if(0===o){e=s;break}o<t&&(t=o,e=s)}return e}}class K extends z{taskStatisticsRequirements={runTime:w,waitTime:w,elu:{aggregate:!0,average:!1,median:!1}};constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return!0}update(){return!0}choose(){return this.nextWorkerNodeKey=this.leastEluNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(){return!0}leastEluNextWorkerNodeKey(){let e,t=1/0;for(const[s,r]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;const i=r.usage,o=i.elu?.active?.aggregate??0;if(0===o){e=s;break}o<t&&(t=o,e=s)}return e}}class P extends z{constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return this.resetWorkerNodeKeyProperties(),!0}update(){return!0}choose(){const e=this.nextWorkerNodeKey;return this.roundRobinNextWorkerNodeKey(),this.checkNextWorkerNodeEligibility(e),e}remove(e){return this.nextWorkerNodeKey===e&&(0===this.pool.workerNodes.length?this.nextWorkerNodeKey=0:this.nextWorkerNodeKey>this.pool.workerNodes.length-1&&(this.nextWorkerNodeKey=this.pool.workerNodes.length-1)),!0}roundRobinNextWorkerNodeKey(){return this.nextWorkerNodeKey=this.nextWorkerNodeKey===this.pool.workerNodes.length-1?0:(this.nextWorkerNodeKey??this.previousWorkerNodeKey)+1,this.nextWorkerNodeKey}}class q extends z{taskStatisticsRequirements={runTime:{aggregate:!0,average:!0,median:!1},waitTime:w,elu:w};defaultWorkerWeight;workerVirtualTaskRunTime=0;constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts),this.defaultWorkerWeight=this.computeDefaultWorkerWeight()}reset(){return this.resetWorkerNodeKeyProperties(),this.workerVirtualTaskRunTime=0,!0}update(){return!0}choose(){const e=this.nextWorkerNodeKey;return this.weightedRoundRobinNextWorkerNodeKey(),this.checkNextWorkerNodeEligibility(e),e}remove(e){return this.nextWorkerNodeKey===e&&(0===this.pool.workerNodes.length?this.nextWorkerNodeKey=0:this.nextWorkerNodeKey>this.pool.workerNodes.length-1&&(this.nextWorkerNodeKey=this.pool.workerNodes.length-1),this.workerVirtualTaskRunTime=0),!0}weightedRoundRobinNextWorkerNodeKey(){const e=this.workerVirtualTaskRunTime;return e<(this.opts.weights?.[this.nextWorkerNodeKey??this.previousWorkerNodeKey]??this.defaultWorkerWeight)?this.workerVirtualTaskRunTime=e+this.getWorkerTaskRunTime(this.nextWorkerNodeKey??this.previousWorkerNodeKey):(this.nextWorkerNodeKey=this.nextWorkerNodeKey===this.pool.workerNodes.length-1?0:(this.nextWorkerNodeKey??this.previousWorkerNodeKey)+1,this.workerVirtualTaskRunTime=0),this.nextWorkerNodeKey}}class A{workerChoiceStrategy;opts;workerChoiceStrategies;retriesCount=0;constructor(e,t=I.ROUND_ROBIN,s=p){this.workerChoiceStrategy=t,this.opts=s,this.opts={...p,...s},this.execute=this.execute.bind(this),this.workerChoiceStrategies=new Map([[I.ROUND_ROBIN,new(P.bind(this))(e,s)],[I.LEAST_USED,new(Q.bind(this))(e,s)],[I.LEAST_BUSY,new(M.bind(this))(e,s)],[I.LEAST_ELU,new(K.bind(this))(e,s)],[I.FAIR_SHARE,new(O.bind(this))(e,s)],[I.WEIGHTED_ROUND_ROBIN,new(q.bind(this))(e,s)],[I.INTERLEAVED_WEIGHTED_ROUND_ROBIN,new(F.bind(this))(e,s)]])}getStrategyPolicy(){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).strategyPolicy}getTaskStatisticsRequirements(){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).taskStatisticsRequirements}setWorkerChoiceStrategy(e){this.workerChoiceStrategy!==e&&(this.workerChoiceStrategy=e),this.workerChoiceStrategies.get(this.workerChoiceStrategy)?.reset()}update(e){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).update(e)}execute(){const e=this.workerChoiceStrategies.get(this.workerChoiceStrategy).choose();if(null==e&&this.retriesCount<this.opts.retries)return this.retriesCount++,this.execute();if(null==e)throw new Error(`Worker node key chosen is null or undefined after ${this.retriesCount} retries`);return this.retriesCount=0,e}remove(e){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).remove(e)}setOptions(e){this.opts={...p,...e};for(const t of this.workerChoiceStrategies.values())t.setOptions(e)}}class U extends Array{size;constructor(e=1024,...t){super(),this.checkSize(e),this.size=e,arguments.length>1&&this.push(...t)}push(...e){const t=super.push(...e);return t>this.size&&super.splice(0,t-this.size),this.length}unshift(...e){return super.unshift(...e)>this.size&&super.splice(this.size,e.length),this.length}concat(...e){const t=super.concat(e);return t.size=this.size,t.length>t.size&&t.splice(0,t.length-t.size),t}splice(e,t,...s){let r=[];if(arguments.length>=3&&null!=t){if(r=super.splice(e,t,...s),this.length>this.size){const e=super.splice(0,this.length-this.size);r=new U(r.length+e.length,...r,...e)}}else r=2===arguments.length?super.splice(e,t):super.splice(e);return r}resize(e){if(this.checkSize(e),0===e)this.length=0;else if(e<this.size)for(let t=e;t<this.size;t++)super.pop();this.size=e}empty(){return 0===this.length}full(){return this.length===this.size}checkSize(e){if(!Number.isSafeInteger(e))throw new TypeError(`Invalid circular array size: ${e} is not a safe integer`);if(e<0)throw new RangeError(`Invalid circular array size: ${e} < 0`)}}class B{data;next;prev;constructor(e){this.data=e}}class D{head;tail;size;maxSize;constructor(){this.clear()}push(e){const t=new B(e);return null==this.tail?this.head=this.tail=t:(t.prev=this.tail,this.tail=this.tail.next=t),this.incrementSize()}unshift(e){const t=new B(e);return null==this.head?this.head=this.tail=t:(t.next=this.head,this.head=this.head.prev=t),this.incrementSize()}pop(){if(null==this.head)return;const e=this.tail;return this.tail=this.tail.prev,null==this.tail?this.head=void 0:this.tail.next=void 0,--this.size,e?.data}shift(){if(null==this.head)return;const e=this.head;return this.head=this.head.next,null==this.head?this.tail=void 0:this.head.prev=void 0,--this.size,e?.data}peekFirst(){return this.head?.data}peekLast(){return this.tail?.data}clear(){this.head=void 0,this.tail=void 0,this.size=0,this.maxSize=0}[Symbol.iterator](){let e=this.head;return{next:()=>{if(null==e)return{value:void 0,done:!0};const t={value:e.data,done:!1};return e=e.next,t}}}backward(){return{[Symbol.iterator]:()=>{let e=this.tail;return{next:()=>{if(null==e)return{value:void 0,done:!0};const t={value:e.data,done:!1};return e=e.prev,t}}}}}incrementSize(){return++this.size,this.size>this.maxSize&&(this.maxSize=this.size),this.size}}class L{worker;info;usage;messageChannel;tasksQueueBackPressureSize;onBackPressure;onEmptyQueue;tasksQueue;onEmptyQueueCount;taskFunctionsUsage;constructor(e,s){if(null==e)throw new TypeError("Cannot construct a worker node without a worker");if(null==s)throw new TypeError("Cannot construct a worker node without a tasks queue back pressure size");if(!Number.isSafeInteger(s))throw new TypeError("Cannot construct a worker node with a tasks queue back pressure size that is not an integer");this.worker=e,this.info=this.initWorkerInfo(e),this.usage=this.initWorkerUsage(),this.info.type===l.thread&&(this.messageChannel=new t.MessageChannel),this.tasksQueueBackPressureSize=s,this.tasksQueue=new D,this.onEmptyQueueCount=0,this.taskFunctionsUsage=new Map}tasksQueueSize(){return this.tasksQueue.size}enqueueTask(e){const t=this.tasksQueue.push(e);return null!=this.onBackPressure&&this.hasBackPressure()&&this.onBackPressure(this.info.id),t}unshiftTask(e){const t=this.tasksQueue.unshift(e);return null!=this.onBackPressure&&this.hasBackPressure()&&this.onBackPressure(this.info.id),t}dequeueTask(){const e=this.tasksQueue.shift();return null!=this.onEmptyQueue&&0===this.tasksQueue.size&&this.startOnEmptyQueue().catch(m),e}popTask(){const e=this.tasksQueue.pop();return null!=this.onEmptyQueue&&0===this.tasksQueue.size&&this.startOnEmptyQueue().catch(m),e}clearTasksQueue(){this.tasksQueue.clear()}hasBackPressure(){return this.tasksQueue.size>=this.tasksQueueBackPressureSize}resetUsage(){this.usage=this.initWorkerUsage(),this.taskFunctionsUsage.clear()}closeChannel(){null!=this.messageChannel&&(this.messageChannel?.port1.unref(),this.messageChannel?.port2.unref(),this.messageChannel?.port1.close(),this.messageChannel?.port2.close(),delete this.messageChannel)}getTaskFunctionWorkerUsage(e){if(!Array.isArray(this.info.taskFunctions))throw new Error(`Cannot get task function worker usage for task function name '${e}' when task function names list is not yet defined`);if(Array.isArray(this.info.taskFunctions)&&this.info.taskFunctions.length<3)throw new Error(`Cannot get task function worker usage for task function name '${e}' when task function names list has less than 3 elements`);return e===g&&(e=this.info.taskFunctions[1]),this.taskFunctionsUsage.has(e)||this.taskFunctionsUsage.set(e,this.initTaskFunctionWorkerUsage(e)),this.taskFunctionsUsage.get(e)}async startOnEmptyQueue(){this.onEmptyQueueCount>0&&(this.usage.tasks.executing>0||this.tasksQueue.size>0)?this.onEmptyQueueCount=0:(this.onEmptyQueue(this.info.id),++this.onEmptyQueueCount,await(async e=>{await new Promise((t=>{setTimeout(t,e)}))})(((e=0,t=100)=>{const s=Math.pow(2,e)*t;return s+.2*s*b()})(this.onEmptyQueueCount)),await this.startOnEmptyQueue())}initWorkerInfo(e){return{id:f(e),type:y(e),dynamic:!1,ready:!1}}initWorkerUsage(){const e=()=>this.tasksQueue.size,t=()=>this.tasksQueue.maxSize;return{tasks:{executed:0,executing:0,get queued(){return e()},get maxQueued(){return t()},stolen:0,failed:0},runTime:{history:new U},waitTime:{history:new U},elu:{idle:{history:new U},active:{history:new U}}}}initTaskFunctionWorkerUsage(e){const t=()=>{let t=0;for(const s of this.tasksQueue)(s.name===g&&e===this.info.taskFunctions[1]||s.name!==g&&e===s.name)&&++t;return t};return{tasks:{executed:0,executing:0,get queued(){return t()},stolen:0,failed:0},runTime:{history:new U},waitTime:{history:new U},elu:{idle:{history:new U},active:{history:new U}}}}}class V{numberOfWorkers;filePath;opts;workerNodes=[];emitter;promiseResponseMap=new Map;workerChoiceStrategyContext;max;starting;started;startTimestamp;constructor(e,t,s){if(this.numberOfWorkers=e,this.filePath=t,this.opts=s,!this.isMain())throw new Error("Cannot start a pool from a worker with the same type as the pool");this.checkNumberOfWorkers(this.numberOfWorkers),this.checkFilePath(this.filePath),this.checkPoolOptions(this.opts),this.chooseWorkerNode=this.chooseWorkerNode.bind(this),this.executeTask=this.executeTask.bind(this),this.enqueueTask=this.enqueueTask.bind(this),!0===this.opts.enableEvents&&(this.emitter=new c),this.workerChoiceStrategyContext=new A(this,this.opts.workerChoiceStrategy,this.opts.workerChoiceStrategyOptions),this.setupHook(),this.starting=!0,this.startPool(),this.starting=!1,this.started=!0,this.startTimestamp=i.performance.now()}checkFilePath(e){if(null==e||"string"!=typeof e||"string"==typeof e&&0===e.trim().length)throw new Error("Please specify a file with a worker implementation");if(!o.existsSync(e))throw new Error(`Cannot find the worker file '${e}'`)}checkNumberOfWorkers(e){if(null==e)throw new Error("Cannot instantiate a pool without specifying the number of workers");if(!Number.isSafeInteger(e))throw new TypeError("Cannot instantiate a pool with a non safe integer number of workers");if(e<0)throw new RangeError("Cannot instantiate a pool with a negative number of workers");if(this.type===k.fixed&&0===e)throw new RangeError("Cannot instantiate a fixed pool with zero worker")}checkDynamicPoolSize(e,t){if(this.type===k.dynamic){if(null==t)throw new TypeError("Cannot instantiate a dynamic pool without specifying the maximum pool size");if(!Number.isSafeInteger(t))throw new TypeError("Cannot instantiate a dynamic pool with a non safe integer maximum pool size");if(e>t)throw new RangeError("Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size");if(0===t)throw new RangeError("Cannot instantiate a dynamic pool with a maximum pool size equal to zero");if(e===t)throw new RangeError("Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead")}}checkPoolOptions(e){if(!x(e))throw new TypeError("Invalid pool options: must be a plain object");this.opts.workerChoiceStrategy=e.workerChoiceStrategy??I.ROUND_ROBIN,this.checkValidWorkerChoiceStrategy(this.opts.workerChoiceStrategy),this.opts.workerChoiceStrategyOptions={...p,...e.workerChoiceStrategyOptions},this.checkValidWorkerChoiceStrategyOptions(this.opts.workerChoiceStrategyOptions),this.opts.restartWorkerOnError=e.restartWorkerOnError??!0,this.opts.enableEvents=e.enableEvents??!0,this.opts.enableTasksQueue=e.enableTasksQueue??!1,this.opts.enableTasksQueue&&(this.checkValidTasksQueueOptions(e.tasksQueueOptions),this.opts.tasksQueueOptions=this.buildTasksQueueOptions(e.tasksQueueOptions))}checkValidWorkerChoiceStrategy(e){if(!Object.values(I).includes(e))throw new Error(`Invalid worker choice strategy '${e}'`)}checkValidWorkerChoiceStrategyOptions(e){if(!x(e))throw new TypeError("Invalid worker choice strategy options: must be a plain object");if(null!=e.retries&&!Number.isSafeInteger(e.retries))throw new TypeError("Invalid worker choice strategy options: retries must be an integer");if(null!=e.retries&&e.retries<0)throw new RangeError(`Invalid worker choice strategy options: retries '${e.retries}' must be greater or equal than zero`);if(null!=e.weights&&Object.keys(e.weights).length!==this.maxSize)throw new Error("Invalid worker choice strategy options: must have a weight for each worker node");if(null!=e.measurement&&!Object.values(R).includes(e.measurement))throw new Error(`Invalid worker choice strategy options: invalid measurement '${e.measurement}'`)}checkValidTasksQueueOptions(e){if(null!=e&&!x(e))throw new TypeError("Invalid tasks queue options: must be a plain object");if(null!=e?.concurrency&&!Number.isSafeInteger(e.concurrency))throw new TypeError("Invalid worker node tasks concurrency: must be an integer");if(null!=e?.concurrency&&e.concurrency<=0)throw new RangeError(`Invalid worker node tasks concurrency: ${e.concurrency} is a negative integer or zero`);if(null!=e?.queueMaxSize)throw new Error("Invalid tasks queue options: queueMaxSize is deprecated, please use size instead");if(null!=e?.size&&!Number.isSafeInteger(e.size))throw new TypeError("Invalid worker node tasks queue size: must be an integer");if(null!=e?.size&&e.size<=0)throw new RangeError(`Invalid worker node tasks queue size: ${e.size} is a negative integer or zero`)}startPool(){for(;this.workerNodes.reduce(((e,t)=>t.info.dynamic?e:e+1),0)<this.numberOfWorkers;)this.createAndSetupWorkerNode()}get info(){return{version:"2.6.37",type:this.type,worker:this.worker,ready:this.ready,strategy:this.opts.workerChoiceStrategy,minSize:this.minSize,maxSize:this.maxSize,...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.aggregate&&this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.aggregate&&{utilization:N(this.utilization)},workerNodes:this.workerNodes.length,idleWorkerNodes:this.workerNodes.reduce(((e,t)=>0===t.usage.tasks.executing?e+1:e),0),busyWorkerNodes:this.workerNodes.reduce(((e,t)=>t.usage.tasks.executing>0?e+1:e),0),executedTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.executed),0),executingTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.executing),0),...!0===this.opts.enableTasksQueue&&{queuedTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.queued),0)},...!0===this.opts.enableTasksQueue&&{maxQueuedTasks:this.workerNodes.reduce(((e,t)=>e+(t.usage.tasks?.maxQueued??0)),0)},...!0===this.opts.enableTasksQueue&&{backPressure:this.hasBackPressure()},...!0===this.opts.enableTasksQueue&&{stolenTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.stolen),0)},failedTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.failed),0),...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.aggregate&&{runTime:{minimum:N(Math.min(...this.workerNodes.map((e=>e.usage.runTime?.minimum??1/0)))),maximum:N(Math.max(...this.workerNodes.map((e=>e.usage.runTime?.maximum??-1/0)))),...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.average&&{average:N(T(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.runTime.history)),[])))},...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median&&{median:N(W(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.runTime.history)),[])))}}},...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.aggregate&&{waitTime:{minimum:N(Math.min(...this.workerNodes.map((e=>e.usage.waitTime?.minimum??1/0)))),maximum:N(Math.max(...this.workerNodes.map((e=>e.usage.waitTime?.maximum??-1/0)))),...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.average&&{average:N(T(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.waitTime.history)),[])))},...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.median&&{median:N(W(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.waitTime.history)),[])))}}}}}get ready(){return this.workerNodes.reduce(((e,t)=>!t.info.dynamic&&t.info.ready?e+1:e),0)>=this.minSize}get utilization(){const e=(i.performance.now()-this.startTimestamp)*this.maxSize;return(this.workerNodes.reduce(((e,t)=>e+(t.usage.runTime?.aggregate??0)),0)+this.workerNodes.reduce(((e,t)=>e+(t.usage.waitTime?.aggregate??0)),0))/e}get minSize(){return this.numberOfWorkers}get maxSize(){return this.max??this.numberOfWorkers}checkMessageWorkerId(e){if(null==e.workerId)throw new Error("Worker message received without worker id");if(null!=e.workerId&&-1===this.getWorkerNodeKeyByWorkerId(e.workerId))throw new Error(`Worker message received from unknown worker '${e.workerId}'`)}getWorkerNodeKeyByWorker(e){return this.workerNodes.findIndex((t=>t.worker===e))}getWorkerNodeKeyByWorkerId(e){return this.workerNodes.findIndex((t=>t.info.id===e))}setWorkerChoiceStrategy(e,t){this.checkValidWorkerChoiceStrategy(e),this.opts.workerChoiceStrategy=e,this.workerChoiceStrategyContext.setWorkerChoiceStrategy(this.opts.workerChoiceStrategy),null!=t&&this.setWorkerChoiceStrategyOptions(t);for(const[e,t]of this.workerNodes.entries())t.resetUsage(),this.sendStatisticsMessageToWorker(e)}setWorkerChoiceStrategyOptions(e){this.checkValidWorkerChoiceStrategyOptions(e),this.opts.workerChoiceStrategyOptions={...p,...e},this.workerChoiceStrategyContext.setOptions(this.opts.workerChoiceStrategyOptions)}enableTasksQueue(e,t){!0!==this.opts.enableTasksQueue||e||this.flushTasksQueues(),this.opts.enableTasksQueue=e,this.setTasksQueueOptions(t)}setTasksQueueOptions(e){!0===this.opts.enableTasksQueue?(this.checkValidTasksQueueOptions(e),this.opts.tasksQueueOptions=this.buildTasksQueueOptions(e),this.setTasksQueueMaxSize(this.opts.tasksQueueOptions.size)):null!=this.opts.tasksQueueOptions&&delete this.opts.tasksQueueOptions}setTasksQueueMaxSize(e){for(const t of this.workerNodes)t.tasksQueueBackPressureSize=e}buildTasksQueueOptions(e){return{size:Math.pow(this.maxSize,2),concurrency:1,...e}}get full(){return this.workerNodes.length>=this.maxSize}internalBusy(){return!0===this.opts.enableTasksQueue?-1===this.workerNodes.findIndex((e=>e.info.ready&&e.usage.tasks.executing<this.opts.tasksQueueOptions?.concurrency)):-1===this.workerNodes.findIndex((e=>e.info.ready&&0===e.usage.tasks.executing))}listTaskFunctions(){for(const e of this.workerNodes)if(Array.isArray(e.info.taskFunctions)&&e.info.taskFunctions.length>0)return e.info.taskFunctions;return[]}shallExecuteTask(e){return 0===this.tasksQueueSize(e)&&this.workerNodes[e].usage.tasks.executing<this.opts.tasksQueueOptions?.concurrency}async execute(e,t,s){return await new Promise(((o,n)=>{if(!this.started)return void n(new Error("Cannot execute a task on destroyed pool"));if(null!=t&&"string"!=typeof t)return void n(new TypeError("name argument must be a string"));if(null!=t&&"string"==typeof t&&0===t.trim().length)return void n(new TypeError("name argument must not be an empty string"));if(null!=s&&!Array.isArray(s))return void n(new TypeError("transferList argument must be an array"));const a=i.performance.now(),h=this.chooseWorkerNode(),u=this.getWorkerInfo(h),k={name:t??g,data:e??{},transferList:s,timestamp:a,workerId:u.id,taskId:r.randomUUID()};this.promiseResponseMap.set(k.taskId,{resolve:o,reject:n,workerNodeKey:h}),!1===this.opts.enableTasksQueue||!0===this.opts.enableTasksQueue&&this.shallExecuteTask(h)?this.executeTask(h,k):this.enqueueTask(h,k)}))}async destroy(){await Promise.all(this.workerNodes.map((async(e,t)=>{await this.destroyWorkerNode(t)}))),this.emitter?.emit(d.destroy,this.info),this.started=!1}async sendKillMessageToWorker(e,t){await new Promise(((s,r)=>{this.registerWorkerMessageListener(e,(e=>{"success"===e.kill?s():"failure"===e.kill&&r(new Error(`Worker ${t} kill message handling failed`))})),this.sendToWorker(e,{kill:!0,workerId:t})}))}setupHook(){}beforeTaskExecutionHook(e,t){if(null!=this.workerNodes[e]?.usage){const s=this.workerNodes[e].usage;++s.tasks.executing,this.updateWaitTimeWorkerUsage(s,t)}if(this.shallUpdateTaskFunctionWorkerUsage(e)&&null!=this.workerNodes[e].getTaskFunctionWorkerUsage(t.name)){const s=this.workerNodes[e].getTaskFunctionWorkerUsage(t.name);++s.tasks.executing,this.updateWaitTimeWorkerUsage(s,t)}}afterTaskExecutionHook(e,t){if(null!=this.workerNodes[e]?.usage){const s=this.workerNodes[e].usage;this.updateTaskStatisticsWorkerUsage(s,t),this.updateRunTimeWorkerUsage(s,t),this.updateEluWorkerUsage(s,t)}if(this.shallUpdateTaskFunctionWorkerUsage(e)&&null!=this.workerNodes[e].getTaskFunctionWorkerUsage(t.taskPerformance?.name)){const s=this.workerNodes[e].getTaskFunctionWorkerUsage(t.taskPerformance?.name);this.updateTaskStatisticsWorkerUsage(s,t),this.updateRunTimeWorkerUsage(s,t),this.updateEluWorkerUsage(s,t)}}shallUpdateTaskFunctionWorkerUsage(e){const t=this.getWorkerInfo(e);return null!=t&&Array.isArray(t.taskFunctions)&&t.taskFunctions.length>2}updateTaskStatisticsWorkerUsage(e,t){const s=e.tasks;null!=s.executing&&s.executing>0&&--s.executing,null==t.taskError?++s.executed:++s.failed}updateRunTimeWorkerUsage(e,t){null==t.taskError&&v(e.runTime,this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime,t.taskPerformance?.runTime??0)}updateWaitTimeWorkerUsage(e,t){const s=i.performance.now(),r=s-(t.timestamp??s);v(e.waitTime,this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime,r)}updateEluWorkerUsage(e,t){if(null!=t.taskError)return;const s=this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu;v(e.elu.active,s,t.taskPerformance?.elu?.active??0),v(e.elu.idle,s,t.taskPerformance?.elu?.idle??0),s.aggregate&&null!=t.taskPerformance?.elu&&(null!=e.elu.utilization?e.elu.utilization=(e.elu.utilization+t.taskPerformance.elu.utilization)/2:e.elu.utilization=t.taskPerformance.elu.utilization)}chooseWorkerNode(){if(this.shallCreateDynamicWorker()){const e=this.createAndSetupDynamicWorkerNode();if(this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerUsage)return e}return this.workerChoiceStrategyContext.execute()}shallCreateDynamicWorker(){return this.type===k.dynamic&&!this.full&&this.internalBusy()}createAndSetupWorkerNode(){const e=this.createWorker();e.on("online",this.opts.onlineHandler??m),e.on("message",this.opts.messageHandler??m),e.on("error",this.opts.errorHandler??m),e.on("error",(t=>{const s=this.getWorkerNodeKeyByWorker(e),r=this.getWorkerInfo(s);r.ready=!1,this.workerNodes[s].closeChannel(),this.emitter?.emit(d.error,t),!0===this.opts.restartWorkerOnError&&!this.starting&&this.started&&(r.dynamic?this.createAndSetupDynamicWorkerNode():this.createAndSetupWorkerNode()),!0===this.opts.enableTasksQueue&&this.redistributeQueuedTasks(s)})),e.on("exit",this.opts.exitHandler??m),e.once("exit",(()=>{this.removeWorkerNode(e)}));const t=this.addWorkerNode(e);return this.afterWorkerNodeSetup(t),t}createAndSetupDynamicWorkerNode(){const e=this.createAndSetupWorkerNode();this.registerWorkerMessageListener(e,(e=>{const t=this.getWorkerNodeKeyByWorkerId(e.workerId),s=this.workerNodes[t].usage;(S(C.HARD,e.kill)||S(C.SOFT,e.kill)&&(!1===this.opts.enableTasksQueue&&0===s.tasks.executing||!0===this.opts.enableTasksQueue&&0===s.tasks.executing&&0===this.tasksQueueSize(t)))&&this.destroyWorkerNode(t).catch((e=>{this.emitter?.emit(d.error,e)}))}));const t=this.getWorkerInfo(e);return this.sendToWorker(e,{checkActive:!0,workerId:t.id}),t.dynamic=!0,(this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerReady||this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerUsage)&&(t.ready=!0),this.checkAndEmitDynamicWorkerCreationEvents(),e}afterWorkerNodeSetup(e){this.registerWorkerMessageListener(e,this.workerListener()),this.sendStartupMessageToWorker(e),this.sendStatisticsMessageToWorker(e),!0===this.opts.enableTasksQueue&&(this.workerNodes[e].onEmptyQueue=this.taskStealingOnEmptyQueue.bind(this),this.workerNodes[e].onBackPressure=this.tasksStealingOnBackPressure.bind(this))}sendStatisticsMessageToWorker(e){this.sendToWorker(e,{statistics:{runTime:this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.aggregate,elu:this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu.aggregate},workerId:this.getWorkerInfo(e).id})}redistributeQueuedTasks(e){for(;this.tasksQueueSize(e)>0;){let t,s=1/0;for(const[r,i]of this.workerNodes.entries())if(i.info.ready&&r!==e){if(0===i.usage.tasks.queued){t=r;break}i.usage.tasks.queued<s&&(s=i.usage.tasks.queued,t=r)}if(null!=t){const s=this.workerNodes[t],r={...this.dequeueTask(e),workerId:s.info.id};this.shallExecuteTask(t)?this.executeTask(t,r):this.enqueueTask(t,r)}}}updateTaskStolenStatisticsWorkerUsage(e,t){const s=this.workerNodes[e];if(null!=s?.usage&&++s.usage.tasks.stolen,this.shallUpdateTaskFunctionWorkerUsage(e)&&null!=s.getTaskFunctionWorkerUsage(t)){++s.getTaskFunctionWorkerUsage(t).tasks.stolen}}taskStealingOnEmptyQueue(e){const t=this.getWorkerNodeKeyByWorkerId(e),s=this.workerNodes[t],r=this.workerNodes.slice().sort(((e,t)=>t.usage.tasks.queued-e.usage.tasks.queued));for(const i of r){if(0===i.usage.tasks.queued)break;if(i.info.ready&&i.info.id!==e&&i.usage.tasks.queued>0){const e={...i.popTask(),workerId:s.info.id};this.shallExecuteTask(t)?this.executeTask(t,e):this.enqueueTask(t,e),this.updateTaskStolenStatisticsWorkerUsage(t,e.name);break}}}tasksStealingOnBackPressure(e){if(this.opts.tasksQueueOptions?.size<=1)return;const t=this.workerNodes[this.getWorkerNodeKeyByWorkerId(e)],s=this.workerNodes.slice().sort(((e,t)=>e.usage.tasks.queued-t.usage.tasks.queued));for(const[r,i]of s.entries())if(t.usage.tasks.queued>0&&i.info.ready&&i.info.id!==e&&i.usage.tasks.queued<this.opts.tasksQueueOptions?.size-1){const e={...t.popTask(),workerId:i.info.id};this.shallExecuteTask(r)?this.executeTask(r,e):this.enqueueTask(r,e),this.updateTaskStolenStatisticsWorkerUsage(r,e.name)}}workerListener(){return e=>{this.checkMessageWorkerId(e),null!=e.ready&&null!=e.taskFunctions?this.handleWorkerReadyResponse(e):null!=e.taskId?this.handleTaskExecutionResponse(e):null!=e.taskFunctions&&(this.getWorkerInfo(this.getWorkerNodeKeyByWorkerId(e.workerId)).taskFunctions=e.taskFunctions)}}handleWorkerReadyResponse(e){if(!1===e.ready)throw new Error(`Worker ${e.workerId} failed to initialize`);const t=this.getWorkerInfo(this.getWorkerNodeKeyByWorkerId(e.workerId));t.ready=e.ready,t.taskFunctions=e.taskFunctions,null!=this.emitter&&this.ready&&this.emitter.emit(d.ready,this.info)}handleTaskExecutionResponse(e){const{taskId:t,taskError:s,data:r}=e,i=this.promiseResponseMap.get(t);if(null!=i){null!=s?(this.emitter?.emit(d.taskError,s),i.reject(s.message)):i.resolve(r);const o=i.workerNodeKey;this.afterTaskExecutionHook(o,e),this.promiseResponseMap.delete(t),!0===this.opts.enableTasksQueue&&this.tasksQueueSize(o)>0&&this.workerNodes[o].usage.tasks.executing<this.opts.tasksQueueOptions?.concurrency&&this.executeTask(o,this.dequeueTask(o)),this.workerChoiceStrategyContext.update(o)}}checkAndEmitTaskExecutionEvents(){this.busy&&this.emitter?.emit(d.busy,this.info)}checkAndEmitTaskQueuingEvents(){this.hasBackPressure()&&this.emitter?.emit(d.backPressure,this.info)}checkAndEmitDynamicWorkerCreationEvents(){this.type===k.dynamic&&this.full&&this.emitter?.emit(d.full,this.info)}getWorkerInfo(e){return this.workerNodes[e]?.info}addWorkerNode(e){const t=new L(e,this.opts.tasksQueueOptions?.size??Math.pow(this.maxSize,2));this.starting&&(t.info.ready=!0),this.workerNodes.push(t);const s=this.getWorkerNodeKeyByWorker(e);if(-1===s)throw new Error("Worker node added not found");return s}removeWorkerNode(e){const t=this.getWorkerNodeKeyByWorker(e);-1!==t&&(this.workerNodes.splice(t,1),this.workerChoiceStrategyContext.remove(t))}hasWorkerNodeBackPressure(e){return!0===this.opts.enableTasksQueue&&this.workerNodes[e].hasBackPressure()}hasBackPressure(){return!0===this.opts.enableTasksQueue&&-1===this.workerNodes.findIndex((e=>!e.hasBackPressure()))}executeTask(e,t){this.beforeTaskExecutionHook(e,t),this.sendToWorker(e,t,t.transferList),this.checkAndEmitTaskExecutionEvents()}enqueueTask(e,t){const s=this.workerNodes[e].enqueueTask(t);return this.checkAndEmitTaskQueuingEvents(),s}dequeueTask(e){return this.workerNodes[e].dequeueTask()}tasksQueueSize(e){return this.workerNodes[e].tasksQueueSize()}flushTasksQueue(e){for(;this.tasksQueueSize(e)>0;)this.executeTask(e,this.dequeueTask(e));this.workerNodes[e].clearTasksQueue()}flushTasksQueues(){for(const[e]of this.workerNodes.entries())this.flushTasksQueue(e)}}class _ extends V{opts;constructor(e,t,s={}){super(e,t,s),this.opts=s}setupHook(){s.setupPrimary({...this.opts.settings,exec:this.filePath})}isMain(){return s.isPrimary}async destroyWorkerNode(e){this.flushTasksQueue(e);const t=this.workerNodes[e],s=t.worker,r=new Promise((e=>{s.on("exit",(()=>{e()}))}));s.on("disconnect",(()=>{s.kill()})),await this.sendKillMessageToWorker(e,t.info.id),s.disconnect(),await r}sendToWorker(e,t){this.workerNodes[e].worker.send(t)}sendStartupMessageToWorker(e){this.sendToWorker(e,{ready:!1,workerId:this.workerNodes[e].info.id})}registerWorkerMessageListener(e,t){this.workerNodes[e].worker.on("message",t)}createWorker(){return s.fork(this.opts.env)}get type(){return k.fixed}get worker(){return l.cluster}get busy(){return this.internalBusy()}}class j extends V{opts;constructor(e,t,s={}){super(e,t,s),this.opts=s}isMain(){return t.isMainThread}async destroyWorkerNode(e){this.flushTasksQueue(e);const t=this.workerNodes[e],s=t.worker,r=new Promise((e=>{s.on("exit",(()=>{e()}))}));await this.sendKillMessageToWorker(e,t.info.id),t.closeChannel(),await s.terminate(),await r}sendToWorker(e,t,s){this.workerNodes[e].messageChannel.port1.postMessage(t,s)}sendStartupMessageToWorker(e){const t=this.workerNodes[e],s=t.worker,r=t.messageChannel.port2;s.postMessage({ready:!1,workerId:t.info.id,port:r},[r])}registerWorkerMessageListener(e,t){this.workerNodes[e].messageChannel.port1.on("message",t)}createWorker(){return new t.Worker(this.filePath,{env:t.SHARE_ENV,...this.opts.workerOptions})}get type(){return k.fixed}get worker(){return l.thread}get busy(){return this.internalBusy()}}const H=6e4,$={killBehavior:C.SOFT,maxInactiveTime:H,killHandler:m};class G extends a.AsyncResource{isMain;mainWorker;opts;taskFunctions;lastTaskTimestamp;statistics;activeInterval;constructor(e,t,s,r,i=$){if(super(e),this.isMain=t,this.mainWorker=s,this.opts=i,null==this.isMain)throw new Error("isMain parameter is mandatory");this.checkTaskFunctions(r),this.checkWorkerOptions(this.opts),this.isMain||this.getMainWorker().on("message",this.handleReadyMessage.bind(this))}checkWorkerOptions(e){this.opts={...$,...e},delete this.opts.async}checkValidTaskFunction(e,t){if("string"!=typeof e)throw new TypeError("A taskFunctions parameter object key is not a string");if("string"==typeof e&&0===e.trim().length)throw new TypeError("A taskFunctions parameter object key is an empty string");if("function"!=typeof t)throw new TypeError("A taskFunctions parameter object value is not a function")}checkTaskFunctions(e){if(null==e)throw new Error("taskFunctions parameter is mandatory");if(this.taskFunctions=new Map,"function"==typeof e){const t=e.bind(this);this.taskFunctions.set(g,t),this.taskFunctions.set("string"==typeof e.name&&e.name.trim().length>0?e.name:"fn1",t)}else{if(!x(e))throw new TypeError("taskFunctions parameter is not a function or a plain object");{let t=!0;for(const[s,r]of Object.entries(e)){this.checkValidTaskFunction(s,r);const e=r.bind(this);t&&(this.taskFunctions.set(g,e),t=!1),this.taskFunctions.set(s,e)}if(t)throw new Error("taskFunctions parameter object is empty")}}}hasTaskFunction(e){return this.checkTaskFunctionName(e),this.taskFunctions.has(e)}addTaskFunction(e,t){if(this.checkTaskFunctionName(e),e===g)throw new Error("Cannot add a task function with the default reserved name");if("function"!=typeof t)throw new TypeError("fn parameter is not a function");try{const s=t.bind(this);return this.taskFunctions.get(e)===this.taskFunctions.get(g)&&this.taskFunctions.set(g,s),this.taskFunctions.set(e,s),this.sendTaskFunctionsListToMainWorker(),!0}catch{return!1}}removeTaskFunction(e){if(this.checkTaskFunctionName(e),e===g)throw new Error("Cannot remove the task function with the default reserved name");if(this.taskFunctions.get(e)===this.taskFunctions.get(g))throw new Error("Cannot remove the task function used as the default task function");const t=this.taskFunctions.delete(e);return this.sendTaskFunctionsListToMainWorker(),t}listTaskFunctions(){const e=[...this.taskFunctions.keys()];let t=g;for(const[e,s]of this.taskFunctions)if(e!==g&&s===this.taskFunctions.get(g)){t=e;break}return[e[e.indexOf(g)],t,...e.filter((e=>e!==g&&e!==t))]}setDefaultTaskFunction(e){if(this.checkTaskFunctionName(e),e===g)throw new Error("Cannot set the default task function reserved name as the default task function");if(!this.taskFunctions.has(e))throw new Error("Cannot set the default task function to a non-existing task function");try{return this.taskFunctions.set(g,this.taskFunctions.get(e)),!0}catch{return!1}}checkTaskFunctionName(e){if("string"!=typeof e)throw new TypeError("name parameter is not a string");if("string"==typeof e&&0===e.trim().length)throw new TypeError("name parameter is an empty string")}messageListener(e){this.checkMessageWorkerId(e),null!=e.statistics?this.statistics=e.statistics:null!=e.checkActive?e.checkActive?this.startCheckActive():this.stopCheckActive():null!=e.taskId&&null!=e.data?this.run(e):!0===e.kill&&this.handleKillMessage(e)}handleKillMessage(e){if(this.stopCheckActive(),E(this.opts.killHandler))(this.opts.killHandler?.()).then((()=>(this.sendToMainWorker({kill:"success",workerId:this.id}),null))).catch((()=>{this.sendToMainWorker({kill:"failure",workerId:this.id})})).finally((()=>{this.emitDestroy()})).catch(m);else try{this.opts.killHandler?.(),this.sendToMainWorker({kill:"success",workerId:this.id})}catch{this.sendToMainWorker({kill:"failure",workerId:this.id})}finally{this.emitDestroy()}}checkMessageWorkerId(e){if(null==e.workerId)throw new Error("Message worker id is not set");if(null!=e.workerId&&e.workerId!==this.id)throw new Error(`Message worker id ${e.workerId} does not match the worker id ${this.id}`)}startCheckActive(){this.lastTaskTimestamp=i.performance.now(),this.activeInterval=setInterval(this.checkActive.bind(this),(this.opts.maxInactiveTime??H)/2)}stopCheckActive(){null!=this.activeInterval&&(clearInterval(this.activeInterval),delete this.activeInterval)}checkActive(){i.performance.now()-this.lastTaskTimestamp>(this.opts.maxInactiveTime??H)&&this.sendToMainWorker({kill:this.opts.killBehavior,workerId:this.id})}getMainWorker(){if(null==this.mainWorker)throw new Error("Main worker not set");return this.mainWorker}sendTaskFunctionsListToMainWorker(){this.sendToMainWorker({taskFunctions:this.listTaskFunctions(),workerId:this.id})}handleError(e){return e instanceof Error?e.message:e}run(e){const{name:t,taskId:s,data:r}=e,i=this.taskFunctions.get(t??g);null!=i?E(i)?this.runInAsyncScope(this.runAsync.bind(this),this,i,e):this.runInAsyncScope(this.runSync.bind(this),this,i,e):this.sendToMainWorker({taskError:{name:t,message:`Task function '${t}' not found`,data:r},workerId:this.id,taskId:s})}runSync(e,t){const{name:s,taskId:r,data:i}=t;try{let t=this.beginTaskPerformance(s);const o=e(i);t=this.endTaskPerformance(t),this.sendToMainWorker({data:o,taskPerformance:t,workerId:this.id,taskId:r})}catch(e){const t=this.handleError(e);this.sendToMainWorker({taskError:{name:s,message:t,data:i},workerId:this.id,taskId:r})}finally{this.updateLastTaskTimestamp()}}runAsync(e,t){const{name:s,taskId:r,data:i}=t;let o=this.beginTaskPerformance(s);e(i).then((e=>(o=this.endTaskPerformance(o),this.sendToMainWorker({data:e,taskPerformance:o,workerId:this.id,taskId:r}),null))).catch((e=>{const t=this.handleError(e);this.sendToMainWorker({taskError:{name:s,message:t,data:i},workerId:this.id,taskId:r})})).finally((()=>{this.updateLastTaskTimestamp()})).catch(m)}beginTaskPerformance(e){return this.checkStatistics(),{name:e??g,timestamp:i.performance.now(),...this.statistics.elu&&{elu:i.performance.eventLoopUtilization()}}}endTaskPerformance(e){return this.checkStatistics(),{...e,...this.statistics.runTime&&{runTime:i.performance.now()-e.timestamp},...this.statistics.elu&&{elu:i.performance.eventLoopUtilization(e.elu)}}}checkStatistics(){if(null==this.statistics)throw new Error("Performance statistics computation requirements not set")}updateLastTaskTimestamp(){null!=this.activeInterval&&(this.lastTaskTimestamp=i.performance.now())}}exports.ClusterWorker=class extends G{constructor(e,t={}){super("worker-cluster-pool:poolifier",s.isPrimary,s.worker,e,t)}handleReadyMessage(e){if(e.workerId===this.id&&!1===e.ready)try{this.getMainWorker().on("message",this.messageListener.bind(this)),this.sendToMainWorker({ready:!0,taskFunctions:this.listTaskFunctions(),workerId:this.id})}catch{this.sendToMainWorker({ready:!1,taskFunctions:this.listTaskFunctions(),workerId:this.id})}}get id(){return this.getMainWorker().id}sendToMainWorker(e){this.getMainWorker().send(e)}},exports.DynamicClusterPool=class extends _{max;constructor(e,t,s,r={}){super(e,s,r),this.max=t,this.checkDynamicPoolSize(this.numberOfWorkers,this.max)}get type(){return k.dynamic}get busy(){return this.full&&this.internalBusy()}},exports.DynamicThreadPool=class extends j{max;constructor(e,t,s,r={}){super(e,s,r),this.max=t,this.checkDynamicPoolSize(this.numberOfWorkers,this.max)}get type(){return k.dynamic}get busy(){return this.full&&this.internalBusy()}},exports.FixedClusterPool=_,exports.FixedThreadPool=j,exports.KillBehaviors=C,exports.Measurements=R,exports.PoolEvents=d,exports.PoolTypes=k,exports.ThreadWorker=class extends G{port;constructor(e,s={}){super("worker-thread-pool:poolifier",t.isMainThread,t.parentPort,e,s)}handleReadyMessage(e){if(e.workerId===this.id&&!1===e.ready&&null!=e.port)try{this.port=e.port,this.port.on("message",this.messageListener.bind(this)),this.sendToMainWorker({ready:!0,taskFunctions:this.listTaskFunctions(),workerId:this.id})}catch{this.sendToMainWorker({ready:!1,taskFunctions:this.listTaskFunctions(),workerId:this.id})}}handleKillMessage(e){super.handleKillMessage(e),this.port?.unref(),this.port?.close()}get id(){return t.threadId}sendToMainWorker(e){this.port.postMessage(e)}handleError(e){return e}},exports.WorkerChoiceStrategies=I,exports.WorkerTypes=l,exports.availableParallelism=()=>{let e=1;try{e=u.availableParallelism()}catch{const t=u.cpus();Array.isArray(t)&&t.length>0&&(e=t.length)}return e};
"use strict";var e=require("node:events"),t=require("node:worker_threads"),s=require("node:cluster"),r=require("node:crypto"),i=require("node:perf_hooks"),o=require("node:fs"),n=require("node:os"),a=require("node:async_hooks");function h(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(s){if("default"!==s){var r=Object.getOwnPropertyDescriptor(e,s);Object.defineProperty(t,s,r.get?r:{enumerable:!0,get:function(){return e[s]}})}})),t.default=e,Object.freeze(t)}var u=h(n);const k=Object.freeze({fixed:"fixed",dynamic:"dynamic"});class c extends e.EventEmitter{}const d=Object.freeze({ready:"ready",busy:"busy",full:"full",destroy:"destroy",error:"error",taskError:"taskError",backPressure:"backPressure"}),l=Object.freeze({thread:"thread",cluster:"cluster"}),g="default",m=Object.freeze((()=>{})),p={retries:6,runTime:{median:!1},waitTime:{median:!1},elu:{median:!1}},w={aggregate:!1,average:!1,median:!1},y=e=>e instanceof t.Worker?l.thread:e instanceof s.Worker?l.cluster:void 0,f=e=>e instanceof t.Worker?e.threadId:e instanceof s.Worker?e.id:void 0,T=e=>Array.isArray(e)&&0===e.length?0:Array.isArray(e)&&1===e.length?e[0]:e.reduce(((e,t)=>e+t),0)/e.length,W=e=>{if(Array.isArray(e)&&0===e.length)return 0;if(Array.isArray(e)&&1===e.length)return e[0];const t=e.slice().sort(((e,t)=>e-t));return(t[t.length-1>>1]+t[t.length>>1])/2},N=(e,t=2)=>{const s=Math.pow(10,t);return Math.round(e*s*(1+Number.EPSILON))/s},x=e=>"object"==typeof e&&null!==e&&e?.constructor===Object&&"[object Object]"===Object.prototype.toString.call(e),S=(e,t)=>t===e,E=e=>"function"==typeof e&&"AsyncFunction"===e.constructor.name,v=(e,t,s)=>{t.aggregate&&(e.aggregate=(e.aggregate??0)+s,e.minimum=Math.min(s,e.minimum??1/0),e.maximum=Math.max(s,e.maximum??-1/0),(t.average||t.median)&&null!=s&&(e.history.push(s),t.average?e.average=T(e.history):null!=e.average&&delete e.average,t.median?e.median=W(e.history):null!=e.median&&delete e.median))},b=()=>r.webcrypto.getRandomValues(new Uint32Array(1))[0]/4294967296,I=Object.freeze({SOFT:"SOFT",HARD:"HARD"}),C=Object.freeze({ROUND_ROBIN:"ROUND_ROBIN",LEAST_USED:"LEAST_USED",LEAST_BUSY:"LEAST_BUSY",LEAST_ELU:"LEAST_ELU",FAIR_SHARE:"FAIR_SHARE",WEIGHTED_ROUND_ROBIN:"WEIGHTED_ROUND_ROBIN",INTERLEAVED_WEIGHTED_ROUND_ROBIN:"INTERLEAVED_WEIGHTED_ROUND_ROBIN"}),R=Object.freeze({runTime:"runTime",waitTime:"waitTime",elu:"elu"});class z{pool;opts;nextWorkerNodeKey=0;previousWorkerNodeKey=0;strategyPolicy={dynamicWorkerUsage:!1,dynamicWorkerReady:!0};taskStatisticsRequirements={runTime:w,waitTime:w,elu:w};constructor(e,t=p){this.pool=e,this.opts=t,this.opts={...p,...t},this.choose=this.choose.bind(this)}setTaskStatisticsRequirements(e){this.toggleMedianMeasurementStatisticsRequirements(this.taskStatisticsRequirements.runTime,e.runTime?.median),this.toggleMedianMeasurementStatisticsRequirements(this.taskStatisticsRequirements.waitTime,e.waitTime?.median),this.toggleMedianMeasurementStatisticsRequirements(this.taskStatisticsRequirements.elu,e.elu?.median)}toggleMedianMeasurementStatisticsRequirements(e,t){e.average&&t&&(e.average=!1,e.median=t),e.median&&!t&&(e.average=!0,e.median=t)}resetWorkerNodeKeyProperties(){this.nextWorkerNodeKey=0,this.previousWorkerNodeKey=0}setOptions(e){this.opts={...p,...e},this.setTaskStatisticsRequirements(this.opts)}isWorkerNodeReady(e){return this.pool.workerNodes[e].info.ready}hasWorkerNodeBackPressure(e){return this.pool.hasWorkerNodeBackPressure(e)}isWorkerNodeEligible(e){return this.isWorkerNodeReady(e)&&!this.hasWorkerNodeBackPressure(e)}getWorkerTaskRunTime(e){return this.taskStatisticsRequirements.runTime.median?this.pool.workerNodes[e].usage.runTime?.median??0:this.pool.workerNodes[e].usage.runTime?.average??0}getWorkerTaskWaitTime(e){return this.taskStatisticsRequirements.waitTime.median?this.pool.workerNodes[e].usage.waitTime?.median??0:this.pool.workerNodes[e].usage.waitTime?.average??0}getWorkerTaskElu(e){return this.taskStatisticsRequirements.elu.median?this.pool.workerNodes[e].usage.elu.active?.median??0:this.pool.workerNodes[e].usage.elu.active?.average??0}checkNextWorkerNodeEligibility(e){this.isWorkerNodeEligible(this.nextWorkerNodeKey)||(this.nextWorkerNodeKey=void 0,this.previousWorkerNodeKey=e??this.previousWorkerNodeKey)}computeDefaultWorkerWeight(){let e=0;for(const t of n.cpus()){const s=t.speed.toString().length-1;e+=1/(t.speed/Math.pow(10,s))*Math.pow(10,s)}return Math.round(e/n.cpus().length)}}class O extends z{taskStatisticsRequirements={runTime:{aggregate:!0,average:!0,median:!1},waitTime:w,elu:{aggregate:!0,average:!0,median:!1}};workersVirtualTaskEndTimestamp=[];constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return this.workersVirtualTaskEndTimestamp=[],!0}update(e){return this.computeWorkerVirtualTaskEndTimestamp(e),!0}choose(){return this.nextWorkerNodeKey=this.fairShareNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(e){return this.workersVirtualTaskEndTimestamp.splice(e,1),!0}fairShareNextWorkerNodeKey(){let e,t=1/0;for(const[s]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;null==this.workersVirtualTaskEndTimestamp[s]&&this.computeWorkerVirtualTaskEndTimestamp(s);const r=this.workersVirtualTaskEndTimestamp[s];r<t&&(t=r,e=s)}return e}computeWorkerVirtualTaskEndTimestamp(e){this.workersVirtualTaskEndTimestamp[e]=this.getWorkerVirtualTaskEndTimestamp(e,this.getWorkerVirtualTaskStartTimestamp(e))}getWorkerVirtualTaskEndTimestamp(e,t){return t+(this.opts.measurement===R.elu?this.getWorkerTaskElu(e):this.getWorkerTaskRunTime(e))}getWorkerVirtualTaskStartTimestamp(e){return Math.max(performance.now(),this.workersVirtualTaskEndTimestamp[e]??-1/0)}}class F extends z{taskStatisticsRequirements={runTime:{aggregate:!0,average:!0,median:!1},waitTime:w,elu:w};roundId=0;defaultWorkerWeight;roundWeights;workerNodeId=0;workerVirtualTaskRunTime=0;constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts),this.defaultWorkerWeight=this.computeDefaultWorkerWeight(),this.roundWeights=this.getRoundWeights()}reset(){return this.resetWorkerNodeKeyProperties(),this.roundId=0,this.workerNodeId=0,this.workerVirtualTaskRunTime=0,!0}update(){return!0}choose(){for(let e=this.roundId;e<this.roundWeights.length;e++){this.roundId=e;for(let t=this.workerNodeId;t<this.pool.workerNodes.length;t++){if(this.workerNodeId=t,!this.isWorkerNodeEligible(t))continue;this.workerNodeId!==this.nextWorkerNodeKey&&0!==this.workerVirtualTaskRunTime&&(this.workerVirtualTaskRunTime=0);const s=this.opts.weights?.[t]??this.defaultWorkerWeight;if(s>=this.roundWeights[e]&&this.workerVirtualTaskRunTime<s)return this.workerVirtualTaskRunTime=this.workerVirtualTaskRunTime+this.getWorkerTaskRunTime(t),this.previousWorkerNodeKey=this.nextWorkerNodeKey??this.previousWorkerNodeKey,this.nextWorkerNodeKey=t,this.nextWorkerNodeKey}}this.interleavedWeightedRoundRobinNextWorkerNodeId()}interleavedWeightedRoundRobinNextWorkerNodeId(){this.roundId===this.roundWeights.length-1&&this.workerNodeId===this.pool.workerNodes.length-1?(this.roundId=0,this.workerNodeId=0):this.workerNodeId===this.pool.workerNodes.length-1?(this.roundId=this.roundId+1,this.workerNodeId=0):this.workerNodeId=this.workerNodeId+1}remove(e){return this.nextWorkerNodeKey===e&&(0===this.pool.workerNodes.length?(this.roundId=0,this.workerNodeId=0,this.nextWorkerNodeKey=0):this.nextWorkerNodeKey>this.pool.workerNodes.length-1&&(this.workerNodeId===this.nextWorkerNodeKey&&(this.workerNodeId=this.pool.workerNodes.length-1),this.nextWorkerNodeKey=this.pool.workerNodes.length-1),this.workerVirtualTaskRunTime=0),!0}setOptions(e){super.setOptions(e),this.roundWeights=this.getRoundWeights()}getRoundWeights(){return null==this.opts.weights?[this.defaultWorkerWeight]:[...new Set(Object.values(this.opts.weights).slice().sort(((e,t)=>e-t)))]}}class Q extends z{taskStatisticsRequirements={runTime:{aggregate:!0,average:!1,median:!1},waitTime:{aggregate:!0,average:!1,median:!1},elu:w};constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return!0}update(){return!0}choose(){return this.nextWorkerNodeKey=this.leastBusyNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(){return!0}leastBusyNextWorkerNodeKey(){let e,t=1/0;for(const[s,r]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;const i=(r.usage.runTime?.aggregate??0)+(r.usage.waitTime?.aggregate??0);if(0===i){e=s;break}i<t&&(t=i,e=s)}return e}}class M extends z{constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return!0}update(){return!0}choose(){return this.nextWorkerNodeKey=this.leastUsedNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(){return!0}leastUsedNextWorkerNodeKey(){let e,t=1/0;for(const[s,r]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;const i=r.usage.tasks,o=i.executed+i.executing+i.queued;if(0===o){e=s;break}o<t&&(t=o,e=s)}return e}}class q extends z{taskStatisticsRequirements={runTime:w,waitTime:w,elu:{aggregate:!0,average:!1,median:!1}};constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return!0}update(){return!0}choose(){return this.nextWorkerNodeKey=this.leastEluNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(){return!0}leastEluNextWorkerNodeKey(){let e,t=1/0;for(const[s,r]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;const i=r.usage,o=i.elu?.active?.aggregate??0;if(0===o){e=s;break}o<t&&(t=o,e=s)}return e}}class P extends z{constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return this.resetWorkerNodeKeyProperties(),!0}update(){return!0}choose(){const e=this.nextWorkerNodeKey;return this.roundRobinNextWorkerNodeKey(),this.checkNextWorkerNodeEligibility(e),e}remove(e){return this.nextWorkerNodeKey===e&&(0===this.pool.workerNodes.length?this.nextWorkerNodeKey=0:this.nextWorkerNodeKey>this.pool.workerNodes.length-1&&(this.nextWorkerNodeKey=this.pool.workerNodes.length-1)),!0}roundRobinNextWorkerNodeKey(){return this.nextWorkerNodeKey=this.nextWorkerNodeKey===this.pool.workerNodes.length-1?0:(this.nextWorkerNodeKey??this.previousWorkerNodeKey)+1,this.nextWorkerNodeKey}}class K extends z{taskStatisticsRequirements={runTime:{aggregate:!0,average:!0,median:!1},waitTime:w,elu:w};defaultWorkerWeight;workerVirtualTaskRunTime=0;constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts),this.defaultWorkerWeight=this.computeDefaultWorkerWeight()}reset(){return this.resetWorkerNodeKeyProperties(),this.workerVirtualTaskRunTime=0,!0}update(){return!0}choose(){const e=this.nextWorkerNodeKey;return this.weightedRoundRobinNextWorkerNodeKey(),this.checkNextWorkerNodeEligibility(e),e}remove(e){return this.nextWorkerNodeKey===e&&(0===this.pool.workerNodes.length?this.nextWorkerNodeKey=0:this.nextWorkerNodeKey>this.pool.workerNodes.length-1&&(this.nextWorkerNodeKey=this.pool.workerNodes.length-1),this.workerVirtualTaskRunTime=0),!0}weightedRoundRobinNextWorkerNodeKey(){const e=this.opts.weights?.[this.nextWorkerNodeKey??this.previousWorkerNodeKey]??this.defaultWorkerWeight;return this.workerVirtualTaskRunTime<e?this.workerVirtualTaskRunTime=this.workerVirtualTaskRunTime+this.getWorkerTaskRunTime(this.nextWorkerNodeKey??this.previousWorkerNodeKey):(this.nextWorkerNodeKey=this.nextWorkerNodeKey===this.pool.workerNodes.length-1?0:(this.nextWorkerNodeKey??this.previousWorkerNodeKey)+1,this.workerVirtualTaskRunTime=0),this.nextWorkerNodeKey}}class A{workerChoiceStrategy;opts;workerChoiceStrategies;retriesCount=0;constructor(e,t=C.ROUND_ROBIN,s=p){this.workerChoiceStrategy=t,this.opts=s,this.opts={...p,...s},this.execute=this.execute.bind(this),this.workerChoiceStrategies=new Map([[C.ROUND_ROBIN,new(P.bind(this))(e,s)],[C.LEAST_USED,new(M.bind(this))(e,s)],[C.LEAST_BUSY,new(Q.bind(this))(e,s)],[C.LEAST_ELU,new(q.bind(this))(e,s)],[C.FAIR_SHARE,new(O.bind(this))(e,s)],[C.WEIGHTED_ROUND_ROBIN,new(K.bind(this))(e,s)],[C.INTERLEAVED_WEIGHTED_ROUND_ROBIN,new(F.bind(this))(e,s)]])}getStrategyPolicy(){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).strategyPolicy}getTaskStatisticsRequirements(){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).taskStatisticsRequirements}setWorkerChoiceStrategy(e){this.workerChoiceStrategy!==e&&(this.workerChoiceStrategy=e),this.workerChoiceStrategies.get(this.workerChoiceStrategy)?.reset()}update(e){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).update(e)}execute(){const e=this.workerChoiceStrategies.get(this.workerChoiceStrategy).choose();if(null==e&&this.retriesCount<this.opts.retries)return this.retriesCount++,this.execute();if(null==e)throw new Error(`Worker node key chosen is null or undefined after ${this.retriesCount} retries`);return this.retriesCount=0,e}remove(e){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).remove(e)}setOptions(e){this.opts={...p,...e};for(const t of this.workerChoiceStrategies.values())t.setOptions(e)}}class U extends Array{size;constructor(e=1024,...t){super(),this.checkSize(e),this.size=e,arguments.length>1&&this.push(...t)}push(...e){const t=super.push(...e);return t>this.size&&super.splice(0,t-this.size),this.length}unshift(...e){return super.unshift(...e)>this.size&&super.splice(this.size,e.length),this.length}concat(...e){const t=super.concat(e);return t.size=this.size,t.length>t.size&&t.splice(0,t.length-t.size),t}splice(e,t,...s){let r=[];if(arguments.length>=3&&null!=t){if(r=super.splice(e,t,...s),this.length>this.size){const e=super.splice(0,this.length-this.size);r=new U(r.length+e.length,...r,...e)}}else r=2===arguments.length?super.splice(e,t):super.splice(e);return r}resize(e){if(this.checkSize(e),0===e)this.length=0;else if(e<this.size)for(let t=e;t<this.size;t++)super.pop();this.size=e}empty(){return 0===this.length}full(){return this.length===this.size}checkSize(e){if(!Number.isSafeInteger(e))throw new TypeError(`Invalid circular array size: ${e} is not a safe integer`);if(e<0)throw new RangeError(`Invalid circular array size: ${e} < 0`)}}class B{data;next;prev;constructor(e){this.data=e}}class D{head;tail;size;maxSize;constructor(){this.clear()}push(e){const t=new B(e);return null==this.tail?this.head=this.tail=t:(t.prev=this.tail,this.tail=this.tail.next=t),this.incrementSize()}unshift(e){const t=new B(e);return null==this.head?this.head=this.tail=t:(t.next=this.head,this.head=this.head.prev=t),this.incrementSize()}pop(){if(null==this.head)return;const e=this.tail;return this.tail=this.tail.prev,null==this.tail?this.head=void 0:this.tail.next=void 0,--this.size,e?.data}shift(){if(null==this.head)return;const e=this.head;return this.head=this.head.next,null==this.head?this.tail=void 0:this.head.prev=void 0,--this.size,e?.data}peekFirst(){return this.head?.data}peekLast(){return this.tail?.data}clear(){this.head=void 0,this.tail=void 0,this.size=0,this.maxSize=0}[Symbol.iterator](){let e=this.head;return{next:()=>{if(null==e)return{value:void 0,done:!0};const t={value:e.data,done:!1};return e=e.next,t}}}backward(){return{[Symbol.iterator]:()=>{let e=this.tail;return{next:()=>{if(null==e)return{value:void 0,done:!0};const t={value:e.data,done:!1};return e=e.prev,t}}}}}incrementSize(){return++this.size,this.size>this.maxSize&&(this.maxSize=this.size),this.size}}class V{worker;info;usage;messageChannel;tasksQueueBackPressureSize;onBackPressure;onEmptyQueue;tasksQueue;onEmptyQueueCount;taskFunctionsUsage;constructor(e,s){this.checkWorkerNodeArguments(e,s),this.worker=e,this.info=this.initWorkerInfo(e),this.usage=this.initWorkerUsage(),this.info.type===l.thread&&(this.messageChannel=new t.MessageChannel),this.tasksQueueBackPressureSize=s,this.tasksQueue=new D,this.onEmptyQueueCount=0,this.taskFunctionsUsage=new Map}tasksQueueSize(){return this.tasksQueue.size}enqueueTask(e){const t=this.tasksQueue.push(e);return null!=this.onBackPressure&&this.hasBackPressure()&&this.onBackPressure(this.info.id),t}unshiftTask(e){const t=this.tasksQueue.unshift(e);return null!=this.onBackPressure&&this.hasBackPressure()&&this.onBackPressure(this.info.id),t}dequeueTask(){const e=this.tasksQueue.shift();return null!=this.onEmptyQueue&&0===this.tasksQueue.size&&this.startOnEmptyQueue().catch(m),e}popTask(){const e=this.tasksQueue.pop();return null!=this.onEmptyQueue&&0===this.tasksQueue.size&&this.startOnEmptyQueue().catch(m),e}clearTasksQueue(){this.tasksQueue.clear()}hasBackPressure(){return this.tasksQueue.size>=this.tasksQueueBackPressureSize}resetUsage(){this.usage=this.initWorkerUsage(),this.taskFunctionsUsage.clear()}closeChannel(){null!=this.messageChannel&&(this.messageChannel?.port1.unref(),this.messageChannel?.port2.unref(),this.messageChannel?.port1.close(),this.messageChannel?.port2.close(),delete this.messageChannel)}getTaskFunctionWorkerUsage(e){if(!Array.isArray(this.info.taskFunctions))throw new Error(`Cannot get task function worker usage for task function name '${e}' when task function names list is not yet defined`);if(Array.isArray(this.info.taskFunctions)&&this.info.taskFunctions.length<3)throw new Error(`Cannot get task function worker usage for task function name '${e}' when task function names list has less than 3 elements`);return e===g&&(e=this.info.taskFunctions[1]),this.taskFunctionsUsage.has(e)||this.taskFunctionsUsage.set(e,this.initTaskFunctionWorkerUsage(e)),this.taskFunctionsUsage.get(e)}async startOnEmptyQueue(){this.onEmptyQueueCount>0&&(this.usage.tasks.executing>0||this.tasksQueue.size>0)?this.onEmptyQueueCount=0:(this.onEmptyQueue(this.info.id),++this.onEmptyQueueCount,await(async e=>{await new Promise((t=>{setTimeout(t,e)}))})(((e=0,t=100)=>{const s=Math.pow(2,e)*t;return s+.2*s*b()})(this.onEmptyQueueCount)),await this.startOnEmptyQueue())}initWorkerInfo(e){return{id:f(e),type:y(e),dynamic:!1,ready:!1}}initWorkerUsage(){const e=()=>this.tasksQueue.size,t=()=>this.tasksQueue.maxSize;return{tasks:{executed:0,executing:0,get queued(){return e()},get maxQueued(){return t()},stolen:0,failed:0},runTime:{history:new U},waitTime:{history:new U},elu:{idle:{history:new U},active:{history:new U}}}}initTaskFunctionWorkerUsage(e){const t=()=>{let t=0;for(const s of this.tasksQueue)(s.name===g&&e===this.info.taskFunctions[1]||s.name!==g&&e===s.name)&&++t;return t};return{tasks:{executed:0,executing:0,get queued(){return t()},stolen:0,failed:0},runTime:{history:new U},waitTime:{history:new U},elu:{idle:{history:new U},active:{history:new U}}}}checkWorkerNodeArguments(e,t){if(null==e)throw new TypeError("Cannot construct a worker node without a worker");if(null==t)throw new TypeError("Cannot construct a worker node without a tasks queue back pressure size");if(!Number.isSafeInteger(t))throw new TypeError("Cannot construct a worker node with a tasks queue back pressure size that is not an integer");if(t<=0)throw new RangeError("Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer")}}class L{numberOfWorkers;filePath;opts;workerNodes=[];emitter;promiseResponseMap=new Map;workerChoiceStrategyContext;max;starting;started;startTimestamp;constructor(e,t,s){if(this.numberOfWorkers=e,this.filePath=t,this.opts=s,!this.isMain())throw new Error("Cannot start a pool from a worker with the same type as the pool");this.checkNumberOfWorkers(this.numberOfWorkers),this.checkFilePath(this.filePath),this.checkPoolOptions(this.opts),this.chooseWorkerNode=this.chooseWorkerNode.bind(this),this.executeTask=this.executeTask.bind(this),this.enqueueTask=this.enqueueTask.bind(this),!0===this.opts.enableEvents&&(this.emitter=new c),this.workerChoiceStrategyContext=new A(this,this.opts.workerChoiceStrategy,this.opts.workerChoiceStrategyOptions),this.setupHook(),this.starting=!0,this.startPool(),this.starting=!1,this.started=!0,this.startTimestamp=i.performance.now()}checkFilePath(e){if(null==e||"string"!=typeof e||"string"==typeof e&&0===e.trim().length)throw new Error("Please specify a file with a worker implementation");if(!o.existsSync(e))throw new Error(`Cannot find the worker file '${e}'`)}checkNumberOfWorkers(e){if(null==e)throw new Error("Cannot instantiate a pool without specifying the number of workers");if(!Number.isSafeInteger(e))throw new TypeError("Cannot instantiate a pool with a non safe integer number of workers");if(e<0)throw new RangeError("Cannot instantiate a pool with a negative number of workers");if(this.type===k.fixed&&0===e)throw new RangeError("Cannot instantiate a fixed pool with zero worker")}checkDynamicPoolSize(e,t){if(this.type===k.dynamic){if(null==t)throw new TypeError("Cannot instantiate a dynamic pool without specifying the maximum pool size");if(!Number.isSafeInteger(t))throw new TypeError("Cannot instantiate a dynamic pool with a non safe integer maximum pool size");if(e>t)throw new RangeError("Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size");if(0===t)throw new RangeError("Cannot instantiate a dynamic pool with a maximum pool size equal to zero");if(e===t)throw new RangeError("Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead")}}checkPoolOptions(e){if(!x(e))throw new TypeError("Invalid pool options: must be a plain object");this.opts.workerChoiceStrategy=e.workerChoiceStrategy??C.ROUND_ROBIN,this.checkValidWorkerChoiceStrategy(this.opts.workerChoiceStrategy),this.opts.workerChoiceStrategyOptions={...p,...e.workerChoiceStrategyOptions},this.checkValidWorkerChoiceStrategyOptions(this.opts.workerChoiceStrategyOptions),this.opts.restartWorkerOnError=e.restartWorkerOnError??!0,this.opts.enableEvents=e.enableEvents??!0,this.opts.enableTasksQueue=e.enableTasksQueue??!1,this.opts.enableTasksQueue&&(this.checkValidTasksQueueOptions(e.tasksQueueOptions),this.opts.tasksQueueOptions=this.buildTasksQueueOptions(e.tasksQueueOptions))}checkValidWorkerChoiceStrategy(e){if(!Object.values(C).includes(e))throw new Error(`Invalid worker choice strategy '${e}'`)}checkValidWorkerChoiceStrategyOptions(e){if(!x(e))throw new TypeError("Invalid worker choice strategy options: must be a plain object");if(null!=e.retries&&!Number.isSafeInteger(e.retries))throw new TypeError("Invalid worker choice strategy options: retries must be an integer");if(null!=e.retries&&e.retries<0)throw new RangeError(`Invalid worker choice strategy options: retries '${e.retries}' must be greater or equal than zero`);if(null!=e.weights&&Object.keys(e.weights).length!==this.maxSize)throw new Error("Invalid worker choice strategy options: must have a weight for each worker node");if(null!=e.measurement&&!Object.values(R).includes(e.measurement))throw new Error(`Invalid worker choice strategy options: invalid measurement '${e.measurement}'`)}checkValidTasksQueueOptions(e){if(null!=e&&!x(e))throw new TypeError("Invalid tasks queue options: must be a plain object");if(null!=e?.concurrency&&!Number.isSafeInteger(e.concurrency))throw new TypeError("Invalid worker node tasks concurrency: must be an integer");if(null!=e?.concurrency&&e.concurrency<=0)throw new RangeError(`Invalid worker node tasks concurrency: ${e.concurrency} is a negative integer or zero`);if(null!=e?.queueMaxSize)throw new Error("Invalid tasks queue options: queueMaxSize is deprecated, please use size instead");if(null!=e?.size&&!Number.isSafeInteger(e.size))throw new TypeError("Invalid worker node tasks queue size: must be an integer");if(null!=e?.size&&e.size<=0)throw new RangeError(`Invalid worker node tasks queue size: ${e.size} is a negative integer or zero`)}startPool(){for(;this.workerNodes.reduce(((e,t)=>t.info.dynamic?e:e+1),0)<this.numberOfWorkers;)this.createAndSetupWorkerNode()}get info(){return{version:"2.6.38",type:this.type,worker:this.worker,ready:this.ready,strategy:this.opts.workerChoiceStrategy,minSize:this.minSize,maxSize:this.maxSize,...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.aggregate&&this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.aggregate&&{utilization:N(this.utilization)},workerNodes:this.workerNodes.length,idleWorkerNodes:this.workerNodes.reduce(((e,t)=>0===t.usage.tasks.executing?e+1:e),0),busyWorkerNodes:this.workerNodes.reduce(((e,t)=>t.usage.tasks.executing>0?e+1:e),0),executedTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.executed),0),executingTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.executing),0),...!0===this.opts.enableTasksQueue&&{queuedTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.queued),0)},...!0===this.opts.enableTasksQueue&&{maxQueuedTasks:this.workerNodes.reduce(((e,t)=>e+(t.usage.tasks?.maxQueued??0)),0)},...!0===this.opts.enableTasksQueue&&{backPressure:this.hasBackPressure()},...!0===this.opts.enableTasksQueue&&{stolenTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.stolen),0)},failedTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.failed),0),...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.aggregate&&{runTime:{minimum:N(Math.min(...this.workerNodes.map((e=>e.usage.runTime?.minimum??1/0)))),maximum:N(Math.max(...this.workerNodes.map((e=>e.usage.runTime?.maximum??-1/0)))),...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.average&&{average:N(T(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.runTime.history)),[])))},...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median&&{median:N(W(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.runTime.history)),[])))}}},...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.aggregate&&{waitTime:{minimum:N(Math.min(...this.workerNodes.map((e=>e.usage.waitTime?.minimum??1/0)))),maximum:N(Math.max(...this.workerNodes.map((e=>e.usage.waitTime?.maximum??-1/0)))),...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.average&&{average:N(T(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.waitTime.history)),[])))},...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.median&&{median:N(W(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.waitTime.history)),[])))}}}}}get ready(){return this.workerNodes.reduce(((e,t)=>!t.info.dynamic&&t.info.ready?e+1:e),0)>=this.minSize}get utilization(){const e=(i.performance.now()-this.startTimestamp)*this.maxSize;return(this.workerNodes.reduce(((e,t)=>e+(t.usage.runTime?.aggregate??0)),0)+this.workerNodes.reduce(((e,t)=>e+(t.usage.waitTime?.aggregate??0)),0))/e}get minSize(){return this.numberOfWorkers}get maxSize(){return this.max??this.numberOfWorkers}checkMessageWorkerId(e){if(null==e.workerId)throw new Error("Worker message received without worker id");if(null!=e.workerId&&-1===this.getWorkerNodeKeyByWorkerId(e.workerId))throw new Error(`Worker message received from unknown worker '${e.workerId}'`)}getWorkerNodeKeyByWorker(e){return this.workerNodes.findIndex((t=>t.worker===e))}getWorkerNodeKeyByWorkerId(e){return this.workerNodes.findIndex((t=>t.info.id===e))}setWorkerChoiceStrategy(e,t){this.checkValidWorkerChoiceStrategy(e),this.opts.workerChoiceStrategy=e,this.workerChoiceStrategyContext.setWorkerChoiceStrategy(this.opts.workerChoiceStrategy),null!=t&&this.setWorkerChoiceStrategyOptions(t);for(const[e,t]of this.workerNodes.entries())t.resetUsage(),this.sendStatisticsMessageToWorker(e)}setWorkerChoiceStrategyOptions(e){this.checkValidWorkerChoiceStrategyOptions(e),this.opts.workerChoiceStrategyOptions={...p,...e},this.workerChoiceStrategyContext.setOptions(this.opts.workerChoiceStrategyOptions)}enableTasksQueue(e,t){!0!==this.opts.enableTasksQueue||e||this.flushTasksQueues(),this.opts.enableTasksQueue=e,this.setTasksQueueOptions(t)}setTasksQueueOptions(e){!0===this.opts.enableTasksQueue?(this.checkValidTasksQueueOptions(e),this.opts.tasksQueueOptions=this.buildTasksQueueOptions(e),this.setTasksQueueSize(this.opts.tasksQueueOptions.size)):null!=this.opts.tasksQueueOptions&&delete this.opts.tasksQueueOptions}setTasksQueueSize(e){for(const t of this.workerNodes)t.tasksQueueBackPressureSize=e}buildTasksQueueOptions(e){return{size:Math.pow(this.maxSize,2),concurrency:1,...e}}get full(){return this.workerNodes.length>=this.maxSize}internalBusy(){return!0===this.opts.enableTasksQueue?-1===this.workerNodes.findIndex((e=>e.info.ready&&e.usage.tasks.executing<this.opts.tasksQueueOptions?.concurrency)):-1===this.workerNodes.findIndex((e=>e.info.ready&&0===e.usage.tasks.executing))}listTaskFunctions(){for(const e of this.workerNodes)if(Array.isArray(e.info.taskFunctions)&&e.info.taskFunctions.length>0)return e.info.taskFunctions;return[]}shallExecuteTask(e){return 0===this.tasksQueueSize(e)&&this.workerNodes[e].usage.tasks.executing<this.opts.tasksQueueOptions?.concurrency}async execute(e,t,s){return await new Promise(((o,n)=>{if(!this.started)return void n(new Error("Cannot execute a task on destroyed pool"));if(null!=t&&"string"!=typeof t)return void n(new TypeError("name argument must be a string"));if(null!=t&&"string"==typeof t&&0===t.trim().length)return void n(new TypeError("name argument must not be an empty string"));if(null!=s&&!Array.isArray(s))return void n(new TypeError("transferList argument must be an array"));const a=i.performance.now(),h=this.chooseWorkerNode(),u=this.getWorkerInfo(h),k={name:t??g,data:e??{},transferList:s,timestamp:a,workerId:u.id,taskId:r.randomUUID()};this.promiseResponseMap.set(k.taskId,{resolve:o,reject:n,workerNodeKey:h}),!1===this.opts.enableTasksQueue||!0===this.opts.enableTasksQueue&&this.shallExecuteTask(h)?this.executeTask(h,k):this.enqueueTask(h,k)}))}async destroy(){await Promise.all(this.workerNodes.map((async(e,t)=>{await this.destroyWorkerNode(t)}))),this.emitter?.emit(d.destroy,this.info),this.started=!1}async sendKillMessageToWorker(e,t){await new Promise(((s,r)=>{this.registerWorkerMessageListener(e,(e=>{"success"===e.kill?s():"failure"===e.kill&&r(new Error(`Worker ${t} kill message handling failed`))})),this.sendToWorker(e,{kill:!0,workerId:t})}))}setupHook(){}beforeTaskExecutionHook(e,t){if(null!=this.workerNodes[e]?.usage){const s=this.workerNodes[e].usage;++s.tasks.executing,this.updateWaitTimeWorkerUsage(s,t)}if(this.shallUpdateTaskFunctionWorkerUsage(e)&&null!=this.workerNodes[e].getTaskFunctionWorkerUsage(t.name)){const s=this.workerNodes[e].getTaskFunctionWorkerUsage(t.name);++s.tasks.executing,this.updateWaitTimeWorkerUsage(s,t)}}afterTaskExecutionHook(e,t){if(null!=this.workerNodes[e]?.usage){const s=this.workerNodes[e].usage;this.updateTaskStatisticsWorkerUsage(s,t),this.updateRunTimeWorkerUsage(s,t),this.updateEluWorkerUsage(s,t)}if(this.shallUpdateTaskFunctionWorkerUsage(e)&&null!=this.workerNodes[e].getTaskFunctionWorkerUsage(t.taskPerformance?.name)){const s=this.workerNodes[e].getTaskFunctionWorkerUsage(t.taskPerformance?.name);this.updateTaskStatisticsWorkerUsage(s,t),this.updateRunTimeWorkerUsage(s,t),this.updateEluWorkerUsage(s,t)}}shallUpdateTaskFunctionWorkerUsage(e){const t=this.getWorkerInfo(e);return null!=t&&Array.isArray(t.taskFunctions)&&t.taskFunctions.length>2}updateTaskStatisticsWorkerUsage(e,t){const s=e.tasks;null!=s.executing&&s.executing>0&&--s.executing,null==t.taskError?++s.executed:++s.failed}updateRunTimeWorkerUsage(e,t){null==t.taskError&&v(e.runTime,this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime,t.taskPerformance?.runTime??0)}updateWaitTimeWorkerUsage(e,t){const s=i.performance.now(),r=s-(t.timestamp??s);v(e.waitTime,this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime,r)}updateEluWorkerUsage(e,t){if(null!=t.taskError)return;const s=this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu;v(e.elu.active,s,t.taskPerformance?.elu?.active??0),v(e.elu.idle,s,t.taskPerformance?.elu?.idle??0),s.aggregate&&null!=t.taskPerformance?.elu&&(null!=e.elu.utilization?e.elu.utilization=(e.elu.utilization+t.taskPerformance.elu.utilization)/2:e.elu.utilization=t.taskPerformance.elu.utilization)}chooseWorkerNode(){if(this.shallCreateDynamicWorker()){const e=this.createAndSetupDynamicWorkerNode();if(this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerUsage)return e}return this.workerChoiceStrategyContext.execute()}shallCreateDynamicWorker(){return this.type===k.dynamic&&!this.full&&this.internalBusy()}createAndSetupWorkerNode(){const e=this.createWorker();e.on("online",this.opts.onlineHandler??m),e.on("message",this.opts.messageHandler??m),e.on("error",this.opts.errorHandler??m),e.on("error",(t=>{const s=this.getWorkerNodeKeyByWorker(e),r=this.getWorkerInfo(s);r.ready=!1,this.workerNodes[s].closeChannel(),this.emitter?.emit(d.error,t),!0===this.opts.restartWorkerOnError&&!this.starting&&this.started&&(r.dynamic?this.createAndSetupDynamicWorkerNode():this.createAndSetupWorkerNode()),!0===this.opts.enableTasksQueue&&this.redistributeQueuedTasks(s)})),e.on("exit",this.opts.exitHandler??m),e.once("exit",(()=>{this.removeWorkerNode(e)}));const t=this.addWorkerNode(e);return this.afterWorkerNodeSetup(t),t}createAndSetupDynamicWorkerNode(){const e=this.createAndSetupWorkerNode();this.registerWorkerMessageListener(e,(e=>{const t=this.getWorkerNodeKeyByWorkerId(e.workerId),s=this.workerNodes[t].usage;(S(I.HARD,e.kill)||S(I.SOFT,e.kill)&&(!1===this.opts.enableTasksQueue&&0===s.tasks.executing||!0===this.opts.enableTasksQueue&&0===s.tasks.executing&&0===this.tasksQueueSize(t)))&&this.destroyWorkerNode(t).catch((e=>{this.emitter?.emit(d.error,e)}))}));const t=this.getWorkerInfo(e);return this.sendToWorker(e,{checkActive:!0,workerId:t.id}),t.dynamic=!0,(this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerReady||this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerUsage)&&(t.ready=!0),this.checkAndEmitDynamicWorkerCreationEvents(),e}afterWorkerNodeSetup(e){this.registerWorkerMessageListener(e,this.workerListener()),this.sendStartupMessageToWorker(e),this.sendStatisticsMessageToWorker(e),!0===this.opts.enableTasksQueue&&(this.workerNodes[e].onEmptyQueue=this.taskStealingOnEmptyQueue.bind(this),this.workerNodes[e].onBackPressure=this.tasksStealingOnBackPressure.bind(this))}sendStatisticsMessageToWorker(e){this.sendToWorker(e,{statistics:{runTime:this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.aggregate,elu:this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu.aggregate},workerId:this.getWorkerInfo(e).id})}redistributeQueuedTasks(e){for(;this.tasksQueueSize(e)>0;){let t,s=1/0;for(const[r,i]of this.workerNodes.entries())if(i.info.ready&&r!==e){if(0===i.usage.tasks.queued){t=r;break}i.usage.tasks.queued<s&&(s=i.usage.tasks.queued,t=r)}if(null!=t){const s=this.workerNodes[t],r={...this.dequeueTask(e),workerId:s.info.id};this.shallExecuteTask(t)?this.executeTask(t,r):this.enqueueTask(t,r)}}}updateTaskStolenStatisticsWorkerUsage(e,t){const s=this.workerNodes[e];if(null!=s?.usage&&++s.usage.tasks.stolen,this.shallUpdateTaskFunctionWorkerUsage(e)&&null!=s.getTaskFunctionWorkerUsage(t)){++s.getTaskFunctionWorkerUsage(t).tasks.stolen}}taskStealingOnEmptyQueue(e){const t=this.getWorkerNodeKeyByWorkerId(e),s=this.workerNodes[t],r=this.workerNodes.slice().sort(((e,t)=>t.usage.tasks.queued-e.usage.tasks.queued));for(const i of r){if(0===i.usage.tasks.queued)break;if(i.info.ready&&i.info.id!==e&&i.usage.tasks.queued>0){const e={...i.popTask(),workerId:s.info.id};this.shallExecuteTask(t)?this.executeTask(t,e):this.enqueueTask(t,e),this.updateTaskStolenStatisticsWorkerUsage(t,e.name);break}}}tasksStealingOnBackPressure(e){if(this.opts.tasksQueueOptions?.size<=1)return;const t=this.workerNodes[this.getWorkerNodeKeyByWorkerId(e)],s=this.workerNodes.slice().sort(((e,t)=>e.usage.tasks.queued-t.usage.tasks.queued));for(const[r,i]of s.entries())if(t.usage.tasks.queued>0&&i.info.ready&&i.info.id!==e&&i.usage.tasks.queued<this.opts.tasksQueueOptions?.size-1){const e={...t.popTask(),workerId:i.info.id};this.shallExecuteTask(r)?this.executeTask(r,e):this.enqueueTask(r,e),this.updateTaskStolenStatisticsWorkerUsage(r,e.name)}}workerListener(){return e=>{this.checkMessageWorkerId(e),null!=e.ready&&null!=e.taskFunctions?this.handleWorkerReadyResponse(e):null!=e.taskId?this.handleTaskExecutionResponse(e):null!=e.taskFunctions&&(this.getWorkerInfo(this.getWorkerNodeKeyByWorkerId(e.workerId)).taskFunctions=e.taskFunctions)}}handleWorkerReadyResponse(e){if(!1===e.ready)throw new Error(`Worker ${e.workerId} failed to initialize`);const t=this.getWorkerInfo(this.getWorkerNodeKeyByWorkerId(e.workerId));t.ready=e.ready,t.taskFunctions=e.taskFunctions,null!=this.emitter&&this.ready&&this.emitter.emit(d.ready,this.info)}handleTaskExecutionResponse(e){const{taskId:t,taskError:s,data:r}=e,i=this.promiseResponseMap.get(t);if(null!=i){null!=s?(this.emitter?.emit(d.taskError,s),i.reject(s.message)):i.resolve(r);const o=i.workerNodeKey;this.afterTaskExecutionHook(o,e),this.promiseResponseMap.delete(t),!0===this.opts.enableTasksQueue&&this.tasksQueueSize(o)>0&&this.workerNodes[o].usage.tasks.executing<this.opts.tasksQueueOptions?.concurrency&&this.executeTask(o,this.dequeueTask(o)),this.workerChoiceStrategyContext.update(o)}}checkAndEmitTaskExecutionEvents(){this.busy&&this.emitter?.emit(d.busy,this.info)}checkAndEmitTaskQueuingEvents(){this.hasBackPressure()&&this.emitter?.emit(d.backPressure,this.info)}checkAndEmitDynamicWorkerCreationEvents(){this.type===k.dynamic&&this.full&&this.emitter?.emit(d.full,this.info)}getWorkerInfo(e){return this.workerNodes[e]?.info}addWorkerNode(e){const t=new V(e,this.opts.tasksQueueOptions?.size??Math.pow(this.maxSize,2));this.starting&&(t.info.ready=!0),this.workerNodes.push(t);const s=this.getWorkerNodeKeyByWorker(e);if(-1===s)throw new Error("Worker node added not found");return s}removeWorkerNode(e){const t=this.getWorkerNodeKeyByWorker(e);-1!==t&&(this.workerNodes.splice(t,1),this.workerChoiceStrategyContext.remove(t))}hasWorkerNodeBackPressure(e){return!0===this.opts.enableTasksQueue&&this.workerNodes[e].hasBackPressure()}hasBackPressure(){return!0===this.opts.enableTasksQueue&&-1===this.workerNodes.findIndex((e=>!e.hasBackPressure()))}executeTask(e,t){this.beforeTaskExecutionHook(e,t),this.sendToWorker(e,t,t.transferList),this.checkAndEmitTaskExecutionEvents()}enqueueTask(e,t){const s=this.workerNodes[e].enqueueTask(t);return this.checkAndEmitTaskQueuingEvents(),s}dequeueTask(e){return this.workerNodes[e].dequeueTask()}tasksQueueSize(e){return this.workerNodes[e].tasksQueueSize()}flushTasksQueue(e){for(;this.tasksQueueSize(e)>0;)this.executeTask(e,this.dequeueTask(e));this.workerNodes[e].clearTasksQueue()}flushTasksQueues(){for(const[e]of this.workerNodes.entries())this.flushTasksQueue(e)}}class _ extends L{opts;constructor(e,t,s={}){super(e,t,s),this.opts=s}setupHook(){s.setupPrimary({...this.opts.settings,exec:this.filePath})}isMain(){return s.isPrimary}async destroyWorkerNode(e){this.flushTasksQueue(e);const t=this.workerNodes[e],s=t.worker,r=new Promise((e=>{s.on("exit",(()=>{e()}))}));s.on("disconnect",(()=>{s.kill()})),await this.sendKillMessageToWorker(e,t.info.id),s.disconnect(),await r}sendToWorker(e,t){this.workerNodes[e].worker.send(t)}sendStartupMessageToWorker(e){this.sendToWorker(e,{ready:!1,workerId:this.workerNodes[e].info.id})}registerWorkerMessageListener(e,t){this.workerNodes[e].worker.on("message",t)}createWorker(){return s.fork(this.opts.env)}get type(){return k.fixed}get worker(){return l.cluster}get busy(){return this.internalBusy()}}class j extends L{opts;constructor(e,t,s={}){super(e,t,s),this.opts=s}isMain(){return t.isMainThread}async destroyWorkerNode(e){this.flushTasksQueue(e);const t=this.workerNodes[e],s=t.worker,r=new Promise((e=>{s.on("exit",(()=>{e()}))}));await this.sendKillMessageToWorker(e,t.info.id),t.closeChannel(),await s.terminate(),await r}sendToWorker(e,t,s){this.workerNodes[e].messageChannel.port1.postMessage(t,s)}sendStartupMessageToWorker(e){const t=this.workerNodes[e],s=t.worker,r=t.messageChannel.port2;s.postMessage({ready:!1,workerId:t.info.id,port:r},[r])}registerWorkerMessageListener(e,t){this.workerNodes[e].messageChannel.port1.on("message",t)}createWorker(){return new t.Worker(this.filePath,{env:t.SHARE_ENV,...this.opts.workerOptions})}get type(){return k.fixed}get worker(){return l.thread}get busy(){return this.internalBusy()}}const H=6e4,$={killBehavior:I.SOFT,maxInactiveTime:H,killHandler:m};class G extends a.AsyncResource{isMain;mainWorker;opts;taskFunctions;lastTaskTimestamp;statistics;activeInterval;constructor(e,t,s,r,i=$){if(super(e),this.isMain=t,this.mainWorker=s,this.opts=i,null==this.isMain)throw new Error("isMain parameter is mandatory");this.checkTaskFunctions(r),this.checkWorkerOptions(this.opts),this.isMain||this.getMainWorker().on("message",this.handleReadyMessage.bind(this))}checkWorkerOptions(e){this.opts={...$,...e},delete this.opts.async}checkValidTaskFunction(e,t){if("string"!=typeof e)throw new TypeError("A taskFunctions parameter object key is not a string");if("string"==typeof e&&0===e.trim().length)throw new TypeError("A taskFunctions parameter object key is an empty string");if("function"!=typeof t)throw new TypeError("A taskFunctions parameter object value is not a function")}checkTaskFunctions(e){if(null==e)throw new Error("taskFunctions parameter is mandatory");if(this.taskFunctions=new Map,"function"==typeof e){const t=e.bind(this);this.taskFunctions.set(g,t),this.taskFunctions.set("string"==typeof e.name&&e.name.trim().length>0?e.name:"fn1",t)}else{if(!x(e))throw new TypeError("taskFunctions parameter is not a function or a plain object");{let t=!0;for(const[s,r]of Object.entries(e)){this.checkValidTaskFunction(s,r);const e=r.bind(this);t&&(this.taskFunctions.set(g,e),t=!1),this.taskFunctions.set(s,e)}if(t)throw new Error("taskFunctions parameter object is empty")}}}hasTaskFunction(e){return this.checkTaskFunctionName(e),this.taskFunctions.has(e)}addTaskFunction(e,t){if(this.checkTaskFunctionName(e),e===g)throw new Error("Cannot add a task function with the default reserved name");if("function"!=typeof t)throw new TypeError("fn parameter is not a function");try{const s=t.bind(this);return this.taskFunctions.get(e)===this.taskFunctions.get(g)&&this.taskFunctions.set(g,s),this.taskFunctions.set(e,s),this.sendTaskFunctionsListToMainWorker(),!0}catch{return!1}}removeTaskFunction(e){if(this.checkTaskFunctionName(e),e===g)throw new Error("Cannot remove the task function with the default reserved name");if(this.taskFunctions.get(e)===this.taskFunctions.get(g))throw new Error("Cannot remove the task function used as the default task function");const t=this.taskFunctions.delete(e);return this.sendTaskFunctionsListToMainWorker(),t}listTaskFunctions(){const e=[...this.taskFunctions.keys()];let t=g;for(const[e,s]of this.taskFunctions)if(e!==g&&s===this.taskFunctions.get(g)){t=e;break}return[e[e.indexOf(g)],t,...e.filter((e=>e!==g&&e!==t))]}setDefaultTaskFunction(e){if(this.checkTaskFunctionName(e),e===g)throw new Error("Cannot set the default task function reserved name as the default task function");if(!this.taskFunctions.has(e))throw new Error("Cannot set the default task function to a non-existing task function");try{return this.taskFunctions.set(g,this.taskFunctions.get(e)),!0}catch{return!1}}checkTaskFunctionName(e){if("string"!=typeof e)throw new TypeError("name parameter is not a string");if("string"==typeof e&&0===e.trim().length)throw new TypeError("name parameter is an empty string")}messageListener(e){this.checkMessageWorkerId(e),null!=e.statistics?this.statistics=e.statistics:null!=e.checkActive?e.checkActive?this.startCheckActive():this.stopCheckActive():null!=e.taskId&&null!=e.data?this.run(e):!0===e.kill&&this.handleKillMessage(e)}handleKillMessage(e){if(this.stopCheckActive(),E(this.opts.killHandler))(this.opts.killHandler?.()).then((()=>(this.sendToMainWorker({kill:"success",workerId:this.id}),null))).catch((()=>{this.sendToMainWorker({kill:"failure",workerId:this.id})})).finally((()=>{this.emitDestroy()})).catch(m);else try{this.opts.killHandler?.(),this.sendToMainWorker({kill:"success",workerId:this.id})}catch{this.sendToMainWorker({kill:"failure",workerId:this.id})}finally{this.emitDestroy()}}checkMessageWorkerId(e){if(null==e.workerId)throw new Error("Message worker id is not set");if(null!=e.workerId&&e.workerId!==this.id)throw new Error(`Message worker id ${e.workerId} does not match the worker id ${this.id}`)}startCheckActive(){this.lastTaskTimestamp=i.performance.now(),this.activeInterval=setInterval(this.checkActive.bind(this),(this.opts.maxInactiveTime??H)/2)}stopCheckActive(){null!=this.activeInterval&&(clearInterval(this.activeInterval),delete this.activeInterval)}checkActive(){i.performance.now()-this.lastTaskTimestamp>(this.opts.maxInactiveTime??H)&&this.sendToMainWorker({kill:this.opts.killBehavior,workerId:this.id})}getMainWorker(){if(null==this.mainWorker)throw new Error("Main worker not set");return this.mainWorker}sendTaskFunctionsListToMainWorker(){this.sendToMainWorker({taskFunctions:this.listTaskFunctions(),workerId:this.id})}handleError(e){return e instanceof Error?e.message:e}run(e){const{name:t,taskId:s,data:r}=e,i=this.taskFunctions.get(t??g);null!=i?E(i)?this.runInAsyncScope(this.runAsync.bind(this),this,i,e):this.runInAsyncScope(this.runSync.bind(this),this,i,e):this.sendToMainWorker({taskError:{name:t,message:`Task function '${t}' not found`,data:r},workerId:this.id,taskId:s})}runSync(e,t){const{name:s,taskId:r,data:i}=t;try{let t=this.beginTaskPerformance(s);const o=e(i);t=this.endTaskPerformance(t),this.sendToMainWorker({data:o,taskPerformance:t,workerId:this.id,taskId:r})}catch(e){const t=this.handleError(e);this.sendToMainWorker({taskError:{name:s,message:t,data:i},workerId:this.id,taskId:r})}finally{this.updateLastTaskTimestamp()}}runAsync(e,t){const{name:s,taskId:r,data:i}=t;let o=this.beginTaskPerformance(s);e(i).then((e=>(o=this.endTaskPerformance(o),this.sendToMainWorker({data:e,taskPerformance:o,workerId:this.id,taskId:r}),null))).catch((e=>{const t=this.handleError(e);this.sendToMainWorker({taskError:{name:s,message:t,data:i},workerId:this.id,taskId:r})})).finally((()=>{this.updateLastTaskTimestamp()})).catch(m)}beginTaskPerformance(e){return this.checkStatistics(),{name:e??g,timestamp:i.performance.now(),...this.statistics.elu&&{elu:i.performance.eventLoopUtilization()}}}endTaskPerformance(e){return this.checkStatistics(),{...e,...this.statistics.runTime&&{runTime:i.performance.now()-e.timestamp},...this.statistics.elu&&{elu:i.performance.eventLoopUtilization(e.elu)}}}checkStatistics(){if(null==this.statistics)throw new Error("Performance statistics computation requirements not set")}updateLastTaskTimestamp(){null!=this.activeInterval&&(this.lastTaskTimestamp=i.performance.now())}}exports.ClusterWorker=class extends G{constructor(e,t={}){super("worker-cluster-pool:poolifier",s.isPrimary,s.worker,e,t)}handleReadyMessage(e){if(e.workerId===this.id&&!1===e.ready)try{this.getMainWorker().on("message",this.messageListener.bind(this)),this.sendToMainWorker({ready:!0,taskFunctions:this.listTaskFunctions(),workerId:this.id})}catch{this.sendToMainWorker({ready:!1,taskFunctions:this.listTaskFunctions(),workerId:this.id})}}get id(){return this.getMainWorker().id}sendToMainWorker(e){this.getMainWorker().send(e)}},exports.DynamicClusterPool=class extends _{max;constructor(e,t,s,r={}){super(e,s,r),this.max=t,this.checkDynamicPoolSize(this.numberOfWorkers,this.max)}get type(){return k.dynamic}get busy(){return this.full&&this.internalBusy()}},exports.DynamicThreadPool=class extends j{max;constructor(e,t,s,r={}){super(e,s,r),this.max=t,this.checkDynamicPoolSize(this.numberOfWorkers,this.max)}get type(){return k.dynamic}get busy(){return this.full&&this.internalBusy()}},exports.FixedClusterPool=_,exports.FixedThreadPool=j,exports.KillBehaviors=I,exports.Measurements=R,exports.PoolEvents=d,exports.PoolTypes=k,exports.ThreadWorker=class extends G{port;constructor(e,s={}){super("worker-thread-pool:poolifier",t.isMainThread,t.parentPort,e,s)}handleReadyMessage(e){if(e.workerId===this.id&&!1===e.ready&&null!=e.port)try{this.port=e.port,this.port.on("message",this.messageListener.bind(this)),this.sendToMainWorker({ready:!0,taskFunctions:this.listTaskFunctions(),workerId:this.id})}catch{this.sendToMainWorker({ready:!1,taskFunctions:this.listTaskFunctions(),workerId:this.id})}}handleKillMessage(e){super.handleKillMessage(e),this.port?.unref(),this.port?.close()}get id(){return t.threadId}sendToMainWorker(e){this.port.postMessage(e)}handleError(e){return e}},exports.WorkerChoiceStrategies=C,exports.WorkerTypes=l,exports.availableParallelism=()=>{let e=1;try{e=u.availableParallelism()}catch{const t=u.cpus();Array.isArray(t)&&t.length>0&&(e=t.length)}return e};

@@ -1,22 +0,1881 @@

export type { AbstractPool } from './pools/abstract-pool';
export { DynamicClusterPool } from './pools/cluster/dynamic';
export { FixedClusterPool, type ClusterPoolOptions } from './pools/cluster/fixed';
export { PoolEvents, PoolTypes } from './pools/pool';
export type { IPool, PoolEmitter, PoolEvent, PoolInfo, PoolOptions, PoolType, TasksQueueOptions } from './pools/pool';
export { WorkerTypes } from './pools/worker';
export type { ErrorHandler, EventLoopUtilizationMeasurementStatistics, ExitHandler, IWorker, IWorkerNode, MeasurementStatistics, MessageHandler, OnlineHandler, TaskStatistics, WorkerInfo, WorkerNodeEventCallback, WorkerType, WorkerUsage } from './pools/worker';
export { Measurements, WorkerChoiceStrategies } from './pools/selection-strategies/selection-strategies-types';
export type { IWorkerChoiceStrategy, Measurement, MeasurementOptions, MeasurementStatisticsRequirements, StrategyPolicy, TaskStatisticsRequirements, WorkerChoiceStrategy, WorkerChoiceStrategyOptions } from './pools/selection-strategies/selection-strategies-types';
export type { WorkerChoiceStrategyContext } from './pools/selection-strategies/worker-choice-strategy-context';
export { DynamicThreadPool } from './pools/thread/dynamic';
export { FixedThreadPool, type ThreadPoolOptions } from './pools/thread/fixed';
export type { AbstractWorker } from './worker/abstract-worker';
export { ClusterWorker } from './worker/cluster-worker';
export { ThreadWorker } from './worker/thread-worker';
export { KillBehaviors } from './worker/worker-options';
export type { KillBehavior, WorkerOptions, KillHandler } from './worker/worker-options';
export type { TaskAsyncFunction, TaskFunction, TaskFunctions, TaskSyncFunction } from './worker/task-functions';
export type { MessageValue, PromiseResponseWrapper, Task, TaskError, TaskPerformance, WorkerStatistics, Writable } from './utility-types';
export type { CircularArray } from './circular-array';
export type { Deque, Node } from './deque';
export { availableParallelism } from './utils';
/// <reference types="node" />
import { TransferListItem, MessagePort, MessageChannel, Worker as Worker$1, WorkerOptions as WorkerOptions$1 } from 'node:worker_threads';
import { EventLoopUtilization } from 'node:perf_hooks';
import { EventEmitter } from 'node:events';
import { Worker, ClusterSettings } from 'node:cluster';
import { AsyncResource } from 'node:async_hooks';
/**
* Enumeration of kill behaviors.
*/
declare const KillBehaviors: Readonly<{
/**
* If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker **wont** be deleted.
*/
readonly SOFT: "SOFT";
/**
* If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted.
*/
readonly HARD: "HARD";
}>;
/**
* Kill behavior.
*/
type KillBehavior = keyof typeof KillBehaviors;
/**
* Handler called when a worker is killed.
*/
type KillHandler = () => void | Promise<void>;
/**
* Options for workers.
*/
interface WorkerOptions {
/**
* `killBehavior` dictates if your worker will be deleted in case a task is active on it.
*
* - SOFT: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker **won't** be deleted.
* - HARD: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing or queued, then the worker will be deleted.
*
* This option only apply to the newly created workers.
*
* @defaultValue KillBehaviors.SOFT
*/
killBehavior?: KillBehavior;
/**
* Maximum waiting time in milliseconds for tasks on newly created workers.
*
* After this time, newly created workers will be terminated.
* The last active time of your worker will be updated when it terminates a task.
*
* - If `killBehavior` is set to `KillBehaviors.HARD` this value represents also the timeout for the tasks that you submit to the pool,
* when this timeout expires your tasks is interrupted before completion and removed. The worker is killed if is not part of the minimum size of the pool.
* - If `killBehavior` is set to `KillBehaviors.SOFT` your tasks have no timeout and your workers will not be terminated until your task is completed.
*
* @defaultValue 60000
*/
maxInactiveTime?: number;
/**
* The function to call when a worker is killed.
*/
killHandler?: KillHandler;
/**
* Whether your worker will perform asynchronous or not.
*
* @defaultValue false
* @deprecated This option will be removed in the next major version.
*/
async?: boolean;
}
/**
* Task error.
*
* @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
*/
interface TaskError<Data = unknown> {
/**
* Task name triggering the error.
*/
readonly name: string;
/**
* Error message.
*/
readonly message: string;
/**
* Data triggering the error.
*/
readonly data?: Data;
}
/**
* Task performance.
*
* @internal
*/
interface TaskPerformance {
/**
* Task name.
*/
readonly name: string;
/**
* Task performance timestamp.
*/
readonly timestamp: number;
/**
* Task runtime.
*/
readonly runTime?: number;
/**
* Task event loop utilization.
*/
readonly elu?: EventLoopUtilization;
}
/**
* Worker task performance statistics computation settings.
*
* @internal
*/
interface WorkerStatistics {
/**
* Whether the worker computes the task runtime or not.
*/
readonly runTime: boolean;
/**
* Whether the worker computes the task event loop utilization (ELU) or not.
*/
readonly elu: boolean;
}
/**
* Message object that is passed as a task between main worker and worker.
*
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @internal
*/
interface Task<Data = unknown> {
/**
* Worker id.
*/
readonly workerId: number;
/**
* Task name.
*/
readonly name?: string;
/**
* Task input data that will be passed to the worker.
*/
readonly data?: Data;
/**
* Array of transferable objects.
*/
readonly transferList?: TransferListItem[];
/**
* Timestamp.
*/
readonly timestamp?: number;
/**
* Task UUID.
*/
readonly taskId?: string;
}
/**
* Message object that is passed between main worker and worker.
*
* @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
* @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
* @internal
*/
interface MessageValue<Data = unknown, ErrorData = unknown> extends Task<Data> {
/**
* Kill code.
*/
readonly kill?: KillBehavior | true | 'success' | 'failure';
/**
* Task error.
*/
readonly taskError?: TaskError<ErrorData>;
/**
* Task performance.
*/
readonly taskPerformance?: TaskPerformance;
/**
* Task function names.
*/
readonly taskFunctions?: string[];
/**
* Whether the worker computes the given statistics or not.
*/
readonly statistics?: WorkerStatistics;
/**
* Whether the worker is ready or not.
*/
readonly ready?: boolean;
/**
* Whether the worker starts or stops its activity check.
*/
readonly checkActive?: boolean;
/**
* Message port.
*/
readonly port?: MessagePort;
}
/**
* An object holding the task execution response promise resolve/reject callbacks.
*
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
* @internal
*/
interface PromiseResponseWrapper<Response = unknown> {
/**
* Resolve callback to fulfill the promise.
*/
readonly resolve: (value: Response | PromiseLike<Response>) => void;
/**
* Reject callback to reject the promise.
*/
readonly reject: (reason?: unknown) => void;
/**
* The worker node key executing the task.
*/
readonly workerNodeKey: number;
}
type Writable<T> = {
-readonly [P in keyof T]: T[P];
};
/**
* Array with a maximum length and shifting items when full.
*
* @internal
*/
declare class CircularArray<T> extends Array<T> {
size: number;
constructor(size?: number, ...items: T[]);
/** @inheritDoc */
push(...items: T[]): number;
/** @inheritDoc */
unshift(...items: T[]): number;
/** @inheritDoc */
concat(...items: Array<T | ConcatArray<T>>): CircularArray<T>;
/** @inheritDoc */
splice(start: number, deleteCount?: number, ...items: T[]): CircularArray<T>;
resize(size: number): void;
empty(): boolean;
full(): boolean;
private checkSize;
}
/**
* Callback invoked when the worker has started successfully.
*/
type OnlineHandler<Worker extends IWorker> = (this: Worker) => void;
/**
* Callback invoked if the worker has received a message.
*/
type MessageHandler<Worker extends IWorker> = (this: Worker, message: unknown) => void;
/**
* Callback invoked if the worker raised an error.
*/
type ErrorHandler<Worker extends IWorker> = (this: Worker, error: Error) => void;
/**
* Callback invoked when the worker exits successfully.
*/
type ExitHandler<Worker extends IWorker> = (this: Worker, exitCode: number) => void;
/**
* Measurement statistics.
*
* @internal
*/
interface MeasurementStatistics {
/**
* Measurement aggregate.
*/
aggregate?: number;
/**
* Measurement minimum.
*/
minimum?: number;
/**
* Measurement maximum.
*/
maximum?: number;
/**
* Measurement average.
*/
average?: number;
/**
* Measurement median.
*/
median?: number;
/**
* Measurement history.
*/
readonly history: CircularArray<number>;
}
/**
* Event loop utilization measurement statistics.
*
* @internal
*/
interface EventLoopUtilizationMeasurementStatistics {
readonly idle: MeasurementStatistics;
readonly active: MeasurementStatistics;
utilization?: number;
}
/**
* Task statistics.
*
* @internal
*/
interface TaskStatistics {
/**
* Number of executed tasks.
*/
executed: number;
/**
* Number of executing tasks.
*/
executing: number;
/**
* Number of queued tasks.
*/
readonly queued: number;
/**
* Maximum number of queued tasks.
*/
readonly maxQueued?: number;
/**
* Number of stolen tasks.
*/
stolen: number;
/**
* Number of failed tasks.
*/
failed: number;
}
/**
* Enumeration of worker types.
*/
declare const WorkerTypes: Readonly<{
readonly thread: "thread";
readonly cluster: "cluster";
}>;
/**
* Worker type.
*/
type WorkerType = keyof typeof WorkerTypes;
/**
* Worker information.
*
* @internal
*/
interface WorkerInfo {
/**
* Worker id.
*/
readonly id: number | undefined;
/**
* Worker type.
*/
readonly type: WorkerType;
/**
* Dynamic flag.
*/
dynamic: boolean;
/**
* Ready flag.
*/
ready: boolean;
/**
* Task function names.
*/
taskFunctions?: string[];
}
/**
* Worker usage statistics.
*
* @internal
*/
interface WorkerUsage {
/**
* Tasks statistics.
*/
readonly tasks: TaskStatistics;
/**
* Tasks runtime statistics.
*/
readonly runTime: MeasurementStatistics;
/**
* Tasks wait time statistics.
*/
readonly waitTime: MeasurementStatistics;
/**
* Tasks event loop utilization statistics.
*/
readonly elu: EventLoopUtilizationMeasurementStatistics;
}
/**
* Worker interface.
*/
interface IWorker {
/**
* Worker id.
*/
readonly id?: number;
readonly threadId?: number;
/**
* Registers an event listener.
*
* @param event - The event.
* @param handler - The event handler.
*/
readonly on: ((event: 'online', handler: OnlineHandler<this>) => void) & ((event: 'message', handler: MessageHandler<this>) => void) & ((event: 'error', handler: ErrorHandler<this>) => void) & ((event: 'exit', handler: ExitHandler<this>) => void);
/**
* Registers a listener to the exit event that will only be performed once.
*
* @param event - The `'exit'` event.
* @param handler - The exit handler.
*/
readonly once: (event: 'exit', handler: ExitHandler<this>) => void;
}
/**
* Worker node event callback.
*
* @param workerId - The worker id.
* @internal
*/
type WorkerNodeEventCallback = (workerId: number) => void;
/**
* Worker node interface.
*
* @typeParam Worker - Type of worker.
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @internal
*/
interface IWorkerNode<Worker extends IWorker, Data = unknown> {
/**
* Worker.
*/
readonly worker: Worker;
/**
* Worker info.
*/
readonly info: WorkerInfo;
/**
* Worker usage statistics.
*/
readonly usage: WorkerUsage;
/**
* Message channel (worker_threads only).
*/
readonly messageChannel?: MessageChannel;
/**
* Tasks queue back pressure size.
* This is the number of tasks that can be enqueued before the worker node has back pressure.
*/
tasksQueueBackPressureSize: number;
/**
* Callback invoked when worker node tasks queue is back pressured.
*/
onBackPressure?: WorkerNodeEventCallback;
/**
* Callback invoked when worker node tasks queue is empty.
*/
onEmptyQueue?: WorkerNodeEventCallback;
/**
* Tasks queue size.
*
* @returns The tasks queue size.
*/
readonly tasksQueueSize: () => number;
/**
* Enqueue task.
*
* @param task - The task to queue.
* @returns The tasks queue size.
*/
readonly enqueueTask: (task: Task<Data>) => number;
/**
* Prepends a task to the tasks queue.
*
* @param task - The task to prepend.
* @returns The tasks queue size.
*/
readonly unshiftTask: (task: Task<Data>) => number;
/**
* Dequeue task.
*
* @returns The dequeued task.
*/
readonly dequeueTask: () => Task<Data> | undefined;
/**
* Pops a task from the tasks queue.
*
* @returns The popped task.
*/
readonly popTask: () => Task<Data> | undefined;
/**
* Clears tasks queue.
*/
readonly clearTasksQueue: () => void;
/**
* Whether the worker node has back pressure (i.e. its tasks queue is full).
*
* @returns `true` if the worker node has back pressure, `false` otherwise.
*/
readonly hasBackPressure: () => boolean;
/**
* Resets usage statistics.
*/
readonly resetUsage: () => void;
/**
* Closes communication channel.
*/
readonly closeChannel: () => void;
/**
* Gets task function worker usage statistics.
*
* @param name - The task function name.
* @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
*/
readonly getTaskFunctionWorkerUsage: (name: string) => WorkerUsage | undefined;
}
/**
* Enumeration of worker choice strategies.
*/
declare const WorkerChoiceStrategies: Readonly<{
/**
* Round robin worker selection strategy.
*/
readonly ROUND_ROBIN: "ROUND_ROBIN";
/**
* Least used worker selection strategy.
*/
readonly LEAST_USED: "LEAST_USED";
/**
* Least busy worker selection strategy.
*/
readonly LEAST_BUSY: "LEAST_BUSY";
/**
* Least ELU worker selection strategy.
*/
readonly LEAST_ELU: "LEAST_ELU";
/**
* Fair share worker selection strategy.
*/
readonly FAIR_SHARE: "FAIR_SHARE";
/**
* Weighted round robin worker selection strategy.
*/
readonly WEIGHTED_ROUND_ROBIN: "WEIGHTED_ROUND_ROBIN";
/**
* Interleaved weighted round robin worker selection strategy.
*
* @experimental
*/
readonly INTERLEAVED_WEIGHTED_ROUND_ROBIN: "INTERLEAVED_WEIGHTED_ROUND_ROBIN";
}>;
/**
* Worker choice strategy.
*/
type WorkerChoiceStrategy = keyof typeof WorkerChoiceStrategies;
/**
* Enumeration of measurements.
*/
declare const Measurements: Readonly<{
readonly runTime: "runTime";
readonly waitTime: "waitTime";
readonly elu: "elu";
}>;
/**
* Measurement.
*/
type Measurement = keyof typeof Measurements;
/**
* Measurement options.
*/
interface MeasurementOptions {
/**
* Set measurement median.
*/
readonly median: boolean;
}
/**
* Worker choice strategy options.
*/
interface WorkerChoiceStrategyOptions {
/**
* Number of worker choice retries to perform if no worker is eligible.
*
* @defaultValue 6
*/
readonly retries?: number;
/**
* Measurement to use in worker choice strategy supporting it.
*/
readonly measurement?: Measurement;
/**
* Runtime options.
*
* @defaultValue \{ median: false \}
*/
readonly runTime?: MeasurementOptions;
/**
* Wait time options.
*
* @defaultValue \{ median: false \}
*/
readonly waitTime?: MeasurementOptions;
/**
* Event loop utilization options.
*
* @defaultValue \{ median: false \}
*/
readonly elu?: MeasurementOptions;
/**
* Worker weights to use for weighted round robin worker selection strategies.
* A weight is tasks maximum execution time in milliseconds for a worker node.
*
* @defaultValue Weights computed automatically given the CPU performance.
*/
readonly weights?: Record<number, number>;
}
/**
* Measurement statistics requirements.
*
* @internal
*/
interface MeasurementStatisticsRequirements {
/**
* Requires measurement aggregate.
*/
aggregate: boolean;
/**
* Requires measurement average.
*/
average: boolean;
/**
* Requires measurement median.
*/
median: boolean;
}
/**
* Pool worker node worker usage statistics requirements.
*
* @internal
*/
interface TaskStatisticsRequirements {
/**
* Tasks runtime requirements.
*/
readonly runTime: MeasurementStatisticsRequirements;
/**
* Tasks wait time requirements.
*/
readonly waitTime: MeasurementStatisticsRequirements;
/**
* Tasks event loop utilization requirements.
*/
readonly elu: MeasurementStatisticsRequirements;
}
/**
* Strategy policy.
*
* @internal
*/
interface StrategyPolicy {
/**
* Expects tasks execution on the newly created dynamic worker.
*/
readonly dynamicWorkerUsage: boolean;
/**
* Expects the newly created dynamic worker to be flagged as ready.
*/
readonly dynamicWorkerReady: boolean;
}
/**
* Worker choice strategy interface.
*
* @internal
*/
interface IWorkerChoiceStrategy {
/**
* Strategy policy.
*/
readonly strategyPolicy: StrategyPolicy;
/**
* Tasks statistics requirements.
*/
readonly taskStatisticsRequirements: TaskStatisticsRequirements;
/**
* Resets strategy internals.
*
* @returns `true` if the reset is successful, `false` otherwise.
*/
readonly reset: () => boolean;
/**
* Updates the worker node key strategy internals.
*
* @returns `true` if the update is successful, `false` otherwise.
*/
readonly update: (workerNodeKey: number) => boolean;
/**
* Chooses a worker node in the pool and returns its key.
* If the worker node is not eligible, `undefined` is returned.
*
* @returns The worker node key or `undefined`.
*/
readonly choose: () => number | undefined;
/**
* Removes the worker node key from strategy internals.
*
* @param workerNodeKey - The worker node key.
* @returns `true` if the worker node key is removed, `false` otherwise.
*/
readonly remove: (workerNodeKey: number) => boolean;
/**
* Sets the worker choice strategy options.
*
* @param opts - The worker choice strategy options.
*/
readonly setOptions: (opts: WorkerChoiceStrategyOptions) => void;
}
/**
* Enumeration of pool types.
*/
declare const PoolTypes: Readonly<{
/**
* Fixed pool type.
*/
readonly fixed: "fixed";
/**
* Dynamic pool type.
*/
readonly dynamic: "dynamic";
}>;
/**
* Pool type.
*/
type PoolType = keyof typeof PoolTypes;
/**
* Pool events emitter.
*/
declare class PoolEmitter extends EventEmitter {
}
/**
* Enumeration of pool events.
*/
declare const PoolEvents: Readonly<{
readonly ready: "ready";
readonly busy: "busy";
readonly full: "full";
readonly destroy: "destroy";
readonly error: "error";
readonly taskError: "taskError";
readonly backPressure: "backPressure";
}>;
/**
* Pool event.
*/
type PoolEvent = keyof typeof PoolEvents;
/**
* Pool information.
*/
interface PoolInfo {
readonly version: string;
readonly type: PoolType;
readonly worker: WorkerType;
readonly ready: boolean;
readonly strategy: WorkerChoiceStrategy;
readonly minSize: number;
readonly maxSize: number;
/** Pool utilization. */
readonly utilization?: number;
/** Pool total worker nodes. */
readonly workerNodes: number;
/** Pool idle worker nodes. */
readonly idleWorkerNodes: number;
/** Pool busy worker nodes. */
readonly busyWorkerNodes: number;
readonly executedTasks: number;
readonly executingTasks: number;
readonly queuedTasks?: number;
readonly maxQueuedTasks?: number;
readonly backPressure?: boolean;
readonly stolenTasks?: number;
readonly failedTasks: number;
readonly runTime?: {
readonly minimum: number;
readonly maximum: number;
readonly average?: number;
readonly median?: number;
};
readonly waitTime?: {
readonly minimum: number;
readonly maximum: number;
readonly average?: number;
readonly median?: number;
};
}
/**
* Worker node tasks queue options.
*/
interface TasksQueueOptions {
/**
* Maximum tasks queue size per worker node flagging it as back pressured.
*
* @defaultValue (pool maximum size)^2
*/
readonly size?: number;
/**
* @deprecated Use `size` instead.
*/
readonly queueMaxSize?: number;
/**
* Maximum number of tasks that can be executed concurrently on a worker node.
*
* @defaultValue 1
*/
readonly concurrency?: number;
}
/**
* Options for a poolifier pool.
*
* @typeParam Worker - Type of worker.
*/
interface PoolOptions<Worker extends IWorker> {
/**
* A function that will listen for online event on each worker.
*/
onlineHandler?: OnlineHandler<Worker>;
/**
* A function that will listen for message event on each worker.
*/
messageHandler?: MessageHandler<Worker>;
/**
* A function that will listen for error event on each worker.
*/
errorHandler?: ErrorHandler<Worker>;
/**
* A function that will listen for exit event on each worker.
*/
exitHandler?: ExitHandler<Worker>;
/**
* The worker choice strategy to use in this pool.
*
* @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
*/
workerChoiceStrategy?: WorkerChoiceStrategy;
/**
* The worker choice strategy options.
*/
workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions;
/**
* Restart worker on error.
*/
restartWorkerOnError?: boolean;
/**
* Pool events emission.
*
* @defaultValue true
*/
enableEvents?: boolean;
/**
* Pool worker node tasks queue.
*
* @defaultValue false
*/
enableTasksQueue?: boolean;
/**
* Pool worker node tasks queue options.
*/
tasksQueueOptions?: TasksQueueOptions;
}
/**
* Contract definition for a poolifier pool.
*
* @typeParam Worker - Type of worker which manages this pool.
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
*/
interface IPool<Worker extends IWorker, Data = unknown, Response = unknown> {
/**
* Pool information.
*/
readonly info: PoolInfo;
/**
* Pool worker nodes.
*
* @internal
*/
readonly workerNodes: Array<IWorkerNode<Worker, Data>>;
/**
* Whether the worker node has back pressure (i.e. its tasks queue is full).
*
* @param workerNodeKey - The worker node key.
* @returns `true` if the worker node has back pressure, `false` otherwise.
* @internal
*/
readonly hasWorkerNodeBackPressure: (workerNodeKey: number) => boolean;
/**
* Emitter on which events can be listened to.
*
* Events that can currently be listened to:
*
* - `'ready'`: Emitted when the number of workers created in the pool has reached the minimum size expected and are ready.
* - `'busy'`: Emitted when the number of workers created in the pool has reached the maximum size expected and are executing concurrently their tasks quota.
* - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.
* - `'destroy'`: Emitted when the pool is destroyed.
* - `'error'`: Emitted when an uncaught error occurs.
* - `'taskError'`: Emitted when an error occurs while executing a task.
* - `'backPressure'`: Emitted when all worker nodes have back pressure (i.e. their tasks queue is full: queue size \>= maximum queue size).
*/
readonly emitter?: PoolEmitter;
/**
* Executes the specified function in the worker constructor with the task data input parameter.
*
* @param data - The optional task input data for the specified task function. This can only be structured-cloneable data.
* @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
* @param transferList - An optional array of transferable objects to transfer ownership of. Ownership of the transferred objects is given to the pool's worker_threads worker and they should not be used in the main thread afterwards.
* @returns Promise that will be fulfilled when the task is completed.
*/
readonly execute: (data?: Data, name?: string, transferList?: TransferListItem[]) => Promise<Response>;
/**
* Terminates all workers in this pool.
*/
readonly destroy: () => Promise<void>;
/**
* Lists the names of task function available in this pool.
*
* @returns The names of task function available in this pool.
*/
readonly listTaskFunctions: () => string[];
/**
* Sets the worker choice strategy in this pool.
*
* @param workerChoiceStrategy - The worker choice strategy.
* @param workerChoiceStrategyOptions - The worker choice strategy options.
*/
readonly setWorkerChoiceStrategy: (workerChoiceStrategy: WorkerChoiceStrategy, workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions) => void;
/**
* Sets the worker choice strategy options in this pool.
*
* @param workerChoiceStrategyOptions - The worker choice strategy options.
*/
readonly setWorkerChoiceStrategyOptions: (workerChoiceStrategyOptions: WorkerChoiceStrategyOptions) => void;
/**
* Enables/disables the worker node tasks queue in this pool.
*
* @param enable - Whether to enable or disable the worker node tasks queue.
* @param tasksQueueOptions - The worker node tasks queue options.
*/
readonly enableTasksQueue: (enable: boolean, tasksQueueOptions?: TasksQueueOptions) => void;
/**
* Sets the worker node tasks queue options in this pool.
*
* @param tasksQueueOptions - The worker node tasks queue options.
*/
readonly setTasksQueueOptions: (tasksQueueOptions: TasksQueueOptions) => void;
}
/**
* The worker choice strategy context.
*
* @typeParam Worker - Type of worker.
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
*/
declare class WorkerChoiceStrategyContext<Worker extends IWorker, Data = unknown, Response = unknown> {
private workerChoiceStrategy;
private opts;
private readonly workerChoiceStrategies;
/**
* The number of times the worker choice strategy in the context has been retried.
*/
private retriesCount;
/**
* Worker choice strategy context constructor.
*
* @param pool - The pool instance.
* @param workerChoiceStrategy - The worker choice strategy.
* @param opts - The worker choice strategy options.
*/
constructor(pool: IPool<Worker, Data, Response>, workerChoiceStrategy?: WorkerChoiceStrategy, opts?: WorkerChoiceStrategyOptions);
/**
* Gets the strategy policy in the context.
*
* @returns The strategy policy.
*/
getStrategyPolicy(): StrategyPolicy;
/**
* Gets the worker choice strategy in the context task statistics requirements.
*
* @returns The task statistics requirements.
*/
getTaskStatisticsRequirements(): TaskStatisticsRequirements;
/**
* Sets the worker choice strategy to use in the context.
*
* @param workerChoiceStrategy - The worker choice strategy to set.
*/
setWorkerChoiceStrategy(workerChoiceStrategy: WorkerChoiceStrategy): void;
/**
* Updates the worker node key in the worker choice strategy in the context internals.
*
* @returns `true` if the update is successful, `false` otherwise.
*/
update(workerNodeKey: number): boolean;
/**
* Executes the worker choice strategy in the context algorithm.
*
* @returns The key of the worker node.
* @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined .
*/
execute(): number;
/**
* Removes the worker node key from the worker choice strategy in the context.
*
* @param workerNodeKey - The worker node key.
* @returns `true` if the removal is successful, `false` otherwise.
*/
remove(workerNodeKey: number): boolean;
/**
* Sets the worker choice strategies in the context options.
*
* @param opts - The worker choice strategy options.
*/
setOptions(opts: WorkerChoiceStrategyOptions): void;
}
/**
* Base class that implements some shared logic for all poolifier pools.
*
* @typeParam Worker - Type of worker which manages this pool.
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
*/
declare abstract class AbstractPool<Worker extends IWorker, Data = unknown, Response = unknown> implements IPool<Worker, Data, Response> {
protected readonly numberOfWorkers: number;
protected readonly filePath: string;
protected readonly opts: PoolOptions<Worker>;
/** @inheritDoc */
readonly workerNodes: Array<IWorkerNode<Worker, Data>>;
/** @inheritDoc */
readonly emitter?: PoolEmitter;
/**
* The task execution response promise map.
*
* - `key`: The message id of each submitted task.
* - `value`: An object that contains the worker, the execution response promise resolve and reject callbacks.
*
* When we receive a message from the worker, we get a map entry with the promise resolve/reject bound to the message id.
*/
protected promiseResponseMap: Map<string, PromiseResponseWrapper<Response>>;
/**
* Worker choice strategy context referencing a worker choice algorithm implementation.
*/
protected workerChoiceStrategyContext: WorkerChoiceStrategyContext<Worker, Data, Response>;
/**
* Dynamic pool maximum size property placeholder.
*/
protected readonly max?: number;
/**
* Whether the pool is starting or not.
*/
private readonly starting;
/**
* Whether the pool is started or not.
*/
private started;
/**
* The start timestamp of the pool.
*/
private readonly startTimestamp;
/**
* Constructs a new poolifier pool.
*
* @param numberOfWorkers - Number of workers that this pool should manage.
* @param filePath - Path to the worker file.
* @param opts - Options for the pool.
*/
constructor(numberOfWorkers: number, filePath: string, opts: PoolOptions<Worker>);
private checkFilePath;
private checkNumberOfWorkers;
protected checkDynamicPoolSize(min: number, max: number): void;
private checkPoolOptions;
private checkValidWorkerChoiceStrategy;
private checkValidWorkerChoiceStrategyOptions;
private checkValidTasksQueueOptions;
private startPool;
/** @inheritDoc */
get info(): PoolInfo;
/**
* The pool readiness boolean status.
*/
private get ready();
/**
* The approximate pool utilization.
*
* @returns The pool utilization.
*/
private get utilization();
/**
* The pool type.
*
* If it is `'dynamic'`, it provides the `max` property.
*/
protected abstract get type(): PoolType;
/**
* The worker type.
*/
protected abstract get worker(): WorkerType;
/**
* The pool minimum size.
*/
protected get minSize(): number;
/**
* The pool maximum size.
*/
protected get maxSize(): number;
/**
* Checks if the worker id sent in the received message from a worker is valid.
*
* @param message - The received message.
* @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker id is invalid.
*/
private checkMessageWorkerId;
/**
* Gets the given worker its worker node key.
*
* @param worker - The worker.
* @returns The worker node key if found in the pool worker nodes, `-1` otherwise.
*/
private getWorkerNodeKeyByWorker;
/**
* Gets the worker node key given its worker id.
*
* @param workerId - The worker id.
* @returns The worker node key if the worker id is found in the pool worker nodes, `-1` otherwise.
*/
private getWorkerNodeKeyByWorkerId;
/** @inheritDoc */
setWorkerChoiceStrategy(workerChoiceStrategy: WorkerChoiceStrategy, workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions): void;
/** @inheritDoc */
setWorkerChoiceStrategyOptions(workerChoiceStrategyOptions: WorkerChoiceStrategyOptions): void;
/** @inheritDoc */
enableTasksQueue(enable: boolean, tasksQueueOptions?: TasksQueueOptions): void;
/** @inheritDoc */
setTasksQueueOptions(tasksQueueOptions: TasksQueueOptions): void;
private setTasksQueueSize;
private buildTasksQueueOptions;
/**
* Whether the pool is full or not.
*
* The pool filling boolean status.
*/
protected get full(): boolean;
/**
* Whether the pool is busy or not.
*
* The pool busyness boolean status.
*/
protected abstract get busy(): boolean;
/**
* Whether worker nodes are executing concurrently their tasks quota or not.
*
* @returns Worker nodes busyness boolean status.
*/
protected internalBusy(): boolean;
/** @inheritDoc */
listTaskFunctions(): string[];
private shallExecuteTask;
/** @inheritDoc */
execute(data?: Data, name?: string, transferList?: TransferListItem[]): Promise<Response>;
/** @inheritDoc */
destroy(): Promise<void>;
protected sendKillMessageToWorker(workerNodeKey: number, workerId: number): Promise<void>;
/**
* Terminates the worker node given its worker node key.
*
* @param workerNodeKey - The worker node key.
*/
protected abstract destroyWorkerNode(workerNodeKey: number): Promise<void>;
/**
* Setup hook to execute code before worker nodes are created in the abstract constructor.
* Can be overridden.
*
* @virtual
*/
protected setupHook(): void;
/**
* Should return whether the worker is the main worker or not.
*/
protected abstract isMain(): boolean;
/**
* Hook executed before the worker task execution.
* Can be overridden.
*
* @param workerNodeKey - The worker node key.
* @param task - The task to execute.
*/
protected beforeTaskExecutionHook(workerNodeKey: number, task: Task<Data>): void;
/**
* Hook executed after the worker task execution.
* Can be overridden.
*
* @param workerNodeKey - The worker node key.
* @param message - The received message.
*/
protected afterTaskExecutionHook(workerNodeKey: number, message: MessageValue<Response>): void;
/**
* Whether the worker node shall update its task function worker usage or not.
*
* @param workerNodeKey - The worker node key.
* @returns `true` if the worker node shall update its task function worker usage, `false` otherwise.
*/
private shallUpdateTaskFunctionWorkerUsage;
private updateTaskStatisticsWorkerUsage;
private updateRunTimeWorkerUsage;
private updateWaitTimeWorkerUsage;
private updateEluWorkerUsage;
/**
* Chooses a worker node for the next task.
*
* The default worker choice strategy uses a round robin algorithm to distribute the tasks.
*
* @returns The chosen worker node key
*/
private chooseWorkerNode;
/**
* Conditions for dynamic worker creation.
*
* @returns Whether to create a dynamic worker or not.
*/
private shallCreateDynamicWorker;
/**
* Sends a message to worker given its worker node key.
*
* @param workerNodeKey - The worker node key.
* @param message - The message.
* @param transferList - The optional array of transferable objects.
*/
protected abstract sendToWorker(workerNodeKey: number, message: MessageValue<Data>, transferList?: TransferListItem[]): void;
/**
* Creates a new worker.
*
* @returns Newly created worker.
*/
protected abstract createWorker(): Worker;
/**
* Creates a new, completely set up worker node.
*
* @returns New, completely set up worker node key.
*/
protected createAndSetupWorkerNode(): number;
/**
* Creates a new, completely set up dynamic worker node.
*
* @returns New, completely set up dynamic worker node key.
*/
protected createAndSetupDynamicWorkerNode(): number;
/**
* Registers a listener callback on the worker given its worker node key.
*
* @param workerNodeKey - The worker node key.
* @param listener - The message listener callback.
*/
protected abstract registerWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
/**
* Method hooked up after a worker node has been newly created.
* Can be overridden.
*
* @param workerNodeKey - The newly created worker node key.
*/
protected afterWorkerNodeSetup(workerNodeKey: number): void;
/**
* Sends the startup message to worker given its worker node key.
*
* @param workerNodeKey - The worker node key.
*/
protected abstract sendStartupMessageToWorker(workerNodeKey: number): void;
/**
* Sends the statistics message to worker given its worker node key.
*
* @param workerNodeKey - The worker node key.
*/
private sendStatisticsMessageToWorker;
private redistributeQueuedTasks;
private updateTaskStolenStatisticsWorkerUsage;
private taskStealingOnEmptyQueue;
private tasksStealingOnBackPressure;
/**
* This method is the listener registered for each worker message.
*
* @returns The listener function to execute when a message is received from a worker.
*/
protected workerListener(): (message: MessageValue<Response>) => void;
private handleWorkerReadyResponse;
private handleTaskExecutionResponse;
private checkAndEmitTaskExecutionEvents;
private checkAndEmitTaskQueuingEvents;
private checkAndEmitDynamicWorkerCreationEvents;
/**
* Gets the worker information given its worker node key.
*
* @param workerNodeKey - The worker node key.
* @returns The worker information.
*/
protected getWorkerInfo(workerNodeKey: number): WorkerInfo | undefined;
/**
* Adds the given worker in the pool worker nodes.
*
* @param worker - The worker.
* @returns The added worker node key.
* @throws {@link https://nodejs.org/api/errors.html#class-error} If the added worker node is not found.
*/
private addWorkerNode;
/**
* Removes the given worker from the pool worker nodes.
*
* @param worker - The worker.
*/
private removeWorkerNode;
/** @inheritDoc */
hasWorkerNodeBackPressure(workerNodeKey: number): boolean;
private hasBackPressure;
/**
* Executes the given task on the worker given its worker node key.
*
* @param workerNodeKey - The worker node key.
* @param task - The task to execute.
*/
private executeTask;
private enqueueTask;
private dequeueTask;
private tasksQueueSize;
protected flushTasksQueue(workerNodeKey: number): void;
private flushTasksQueues;
}
/**
* Options for a poolifier cluster pool.
*/
interface ClusterPoolOptions extends PoolOptions<Worker> {
/**
* Key/value pairs to add to worker process environment.
*
* @see https://nodejs.org/api/cluster.html#cluster_cluster_fork_env
*/
env?: Record<string, unknown>;
/**
* Cluster settings.
*
* @see https://nodejs.org/api/cluster.html#cluster_cluster_settings
*/
settings?: ClusterSettings;
}
/**
* A cluster pool with a fixed number of workers.
*
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
* @since 2.0.0
*/
declare class FixedClusterPool<Data = unknown, Response = unknown> extends AbstractPool<Worker, Data, Response> {
protected readonly opts: ClusterPoolOptions;
/**
* Constructs a new poolifier fixed cluster pool.
*
* @param numberOfWorkers - Number of workers for this pool.
* @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
* @param opts - Options for this fixed cluster pool.
*/
constructor(numberOfWorkers: number, filePath: string, opts?: ClusterPoolOptions);
/** @inheritDoc */
protected setupHook(): void;
/** @inheritDoc */
protected isMain(): boolean;
/** @inheritDoc */
protected destroyWorkerNode(workerNodeKey: number): Promise<void>;
/** @inheritDoc */
protected sendToWorker(workerNodeKey: number, message: MessageValue<Data>): void;
/** @inheritDoc */
protected sendStartupMessageToWorker(workerNodeKey: number): void;
/** @inheritDoc */
protected registerWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
/** @inheritDoc */
protected createWorker(): Worker;
/** @inheritDoc */
protected get type(): PoolType;
/** @inheritDoc */
protected get worker(): WorkerType;
/** @inheritDoc */
protected get busy(): boolean;
}
/**
* A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.
*
* This cluster pool creates new workers when the others are busy, up to the maximum number of workers.
* When the maximum number of workers is reached and workers are busy, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
*
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
* @since 2.0.0
*/
declare class DynamicClusterPool<Data = unknown, Response = unknown> extends FixedClusterPool<Data, Response> {
protected readonly max: number;
/**
* Constructs a new poolifier dynamic cluster pool.
*
* @param min - Minimum number of workers which are always active.
* @param max - Maximum number of workers that can be created by this pool.
* @param filePath - Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
* @param opts - Options for this dynamic cluster pool.
*/
constructor(min: number, max: number, filePath: string, opts?: ClusterPoolOptions);
/** @inheritDoc */
protected get type(): PoolType;
/** @inheritDoc */
protected get busy(): boolean;
}
/**
* Options for a poolifier thread pool.
*/
interface ThreadPoolOptions extends PoolOptions<Worker$1> {
/**
* Worker options.
*
* @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
*/
workerOptions?: WorkerOptions$1;
}
/**
* A thread pool with a fixed number of threads.
*
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
* @author [Alessandro Pio Ardizio](https://github.com/pioardi)
* @since 0.0.1
*/
declare class FixedThreadPool<Data = unknown, Response = unknown> extends AbstractPool<Worker$1, Data, Response> {
protected readonly opts: ThreadPoolOptions;
/**
* Constructs a new poolifier fixed thread pool.
*
* @param numberOfThreads - Number of threads for this pool.
* @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
* @param opts - Options for this fixed thread pool.
*/
constructor(numberOfThreads: number, filePath: string, opts?: ThreadPoolOptions);
/** @inheritDoc */
protected isMain(): boolean;
/** @inheritDoc */
protected destroyWorkerNode(workerNodeKey: number): Promise<void>;
/** @inheritDoc */
protected sendToWorker(workerNodeKey: number, message: MessageValue<Data>, transferList?: TransferListItem[]): void;
/** @inheritDoc */
protected sendStartupMessageToWorker(workerNodeKey: number): void;
/** @inheritDoc */
protected registerWorkerMessageListener<Message extends Data | Response>(workerNodeKey: number, listener: (message: MessageValue<Message>) => void): void;
/** @inheritDoc */
protected createWorker(): Worker$1;
/** @inheritDoc */
protected get type(): PoolType;
/** @inheritDoc */
protected get worker(): WorkerType;
/** @inheritDoc */
protected get busy(): boolean;
}
/**
* A thread pool with a dynamic number of threads, but a guaranteed minimum number of threads.
*
* This thread pool creates new threads when the others are busy, up to the maximum number of threads.
* When the maximum number of threads is reached and workers are busy, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
*
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
* @author [Alessandro Pio Ardizio](https://github.com/pioardi)
* @since 0.0.1
*/
declare class DynamicThreadPool<Data = unknown, Response = unknown> extends FixedThreadPool<Data, Response> {
protected readonly max: number;
/**
* Constructs a new poolifier dynamic thread pool.
*
* @param min - Minimum number of threads which are always active.
* @param max - Maximum number of threads that can be created by this pool.
* @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
* @param opts - Options for this dynamic thread pool.
*/
constructor(min: number, max: number, filePath: string, opts?: ThreadPoolOptions);
/** @inheritDoc */
protected get type(): PoolType;
/** @inheritDoc */
protected get busy(): boolean;
}
/**
* Task synchronous function that can be executed.
*
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
*/
type TaskSyncFunction<Data = unknown, Response = unknown> = (data?: Data) => Response;
/**
* Task asynchronous function that can be executed.
* This function must return a promise.
*
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
*/
type TaskAsyncFunction<Data = unknown, Response = unknown> = (data?: Data) => Promise<Response>;
/**
* Task function that can be executed.
* This function can be synchronous or asynchronous.
*
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
*/
type TaskFunction<Data = unknown, Response = unknown> = TaskSyncFunction<Data, Response> | TaskAsyncFunction<Data, Response>;
/**
* Tasks functions that can be executed.
* This object can contain synchronous or asynchronous functions.
* The key is the name of the function.
* The value is the function itself.
*
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @typeParam Response - Type of execution response. This can only be structured-cloneable data.
*/
type TaskFunctions<Data = unknown, Response = unknown> = Record<string, TaskFunction<Data, Response>>;
/**
* Base class that implements some shared logic for all poolifier workers.
*
* @typeParam MainWorker - Type of main worker.
* @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
* @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
*/
declare abstract class AbstractWorker<MainWorker extends Worker | MessagePort, Data = unknown, Response = unknown> extends AsyncResource {
protected readonly isMain: boolean;
private readonly mainWorker;
protected opts: WorkerOptions;
/**
* Worker id.
*/
protected abstract id: number;
/**
* Task function(s) processed by the worker when the pool's `execution` function is invoked.
*/
protected taskFunctions: Map<string, TaskFunction<Data, Response>>;
/**
* Timestamp of the last task processed by this worker.
*/
protected lastTaskTimestamp: number;
/**
* Performance statistics computation requirements.
*/
protected statistics: WorkerStatistics;
/**
* Handler id of the `activeInterval` worker activity check.
*/
protected activeInterval?: NodeJS.Timeout;
/**
* Constructs a new poolifier worker.
*
* @param type - The type of async event.
* @param isMain - Whether this is the main worker or not.
* @param mainWorker - Reference to main worker.
* @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function.
* @param opts - Options for the worker.
*/
constructor(type: string, isMain: boolean, mainWorker: MainWorker, taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions);
private checkWorkerOptions;
private checkValidTaskFunction;
/**
* Checks if the `taskFunctions` parameter is passed to the constructor.
*
* @param taskFunctions - The task function(s) parameter that should be checked.
*/
private checkTaskFunctions;
/**
* Checks if the worker has a task function with the given name.
*
* @param name - The name of the task function to check.
* @returns Whether the worker has a task function with the given name or not.
* @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
*/
hasTaskFunction(name: string): boolean;
/**
* Adds a task function to the worker.
* If a task function with the same name already exists, it is replaced.
*
* @param name - The name of the task function to add.
* @param fn - The task function to add.
* @returns Whether the task function was added or not.
* @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
* @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
* @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `fn` parameter is not a function.
*/
addTaskFunction(name: string, fn: TaskFunction<Data, Response>): boolean;
/**
* Removes a task function from the worker.
*
* @param name - The name of the task function to remove.
* @returns Whether the task function existed and was removed or not.
* @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
* @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
* @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the task function used as default task function.
*/
removeTaskFunction(name: string): boolean;
/**
* Lists the names of the worker's task functions.
*
* @returns The names of the worker's task functions.
*/
listTaskFunctions(): string[];
/**
* Sets the default task function to use in the worker.
*
* @param name - The name of the task function to use as default task function.
* @returns Whether the default task function was set or not.
* @throws {@link https://nodejs.org/api/errors.html#class-typeerror} If the `name` parameter is not a string or an empty string.
* @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is the default task function reserved name.
* @throws {@link https://nodejs.org/api/errors.html#class-error} If the `name` parameter is a non-existing task function.
*/
setDefaultTaskFunction(name: string): boolean;
private checkTaskFunctionName;
/**
* Handles the ready message sent by the main worker.
*
* @param message - The ready message.
*/
protected abstract handleReadyMessage(message: MessageValue<Data>): void;
/**
* Worker message listener.
*
* @param message - The received message.
*/
protected messageListener(message: MessageValue<Data>): void;
/**
* Handles a kill message sent by the main worker.
*
* @param message - The kill message.
*/
protected handleKillMessage(message: MessageValue<Data>): void;
/**
* Check if the message worker id is set and matches the worker id.
*
* @param message - The message to check.
* @throws {@link https://nodejs.org/api/errors.html#class-error} If the message worker id is not set or does not match the worker id.
*/
private checkMessageWorkerId;
/**
* Starts the worker check active interval.
*/
private startCheckActive;
/**
* Stops the worker check active interval.
*/
private stopCheckActive;
/**
* Checks if the worker should be terminated, because its living too long.
*/
private checkActive;
/**
* Returns the main worker.
*
* @returns Reference to the main worker.
* @throws {@link https://nodejs.org/api/errors.html#class-error} If the main worker is not set.
*/
protected getMainWorker(): MainWorker;
/**
* Sends a message to main worker.
*
* @param message - The response message.
*/
protected abstract sendToMainWorker(message: MessageValue<Response, Data>): void;
/**
* Sends the list of task function names to the main worker.
*/
protected sendTaskFunctionsListToMainWorker(): void;
/**
* Handles an error and convert it to a string so it can be sent back to the main worker.
*
* @param e - The error raised by the worker.
* @returns The error message.
*/
protected handleError(e: Error | string): string;
/**
* Runs the given task.
*
* @param task - The task to execute.
* @throws {@link https://nodejs.org/api/errors.html#class-error} If the task function is not found.
*/
protected run(task: Task<Data>): void;
/**
* Runs the given task function synchronously.
*
* @param fn - Task function that will be executed.
* @param task - Input data for the task function.
*/
protected runSync(fn: TaskSyncFunction<Data, Response>, task: Task<Data>): void;
/**
* Runs the given task function asynchronously.
*
* @param fn - Task function that will be executed.
* @param task - Input data for the task function.
*/
protected runAsync(fn: TaskAsyncFunction<Data, Response>, task: Task<Data>): void;
private beginTaskPerformance;
private endTaskPerformance;
private checkStatistics;
private updateLastTaskTimestamp;
}
/**
* A cluster worker used by a poolifier `ClusterPool`.
*
* When this worker is inactive for more than the given `maxInactiveTime`,
* it will send a termination request to its main worker.
*
* If you use a `DynamicClusterPool` the extra workers that were created will be terminated,
* but the minimum number of workers will be guaranteed.
*
* @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
* @typeParam Response - Type of response the worker sends back to the main worker. This can only be structured-cloneable data.
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
* @since 2.0.0
*/
declare class ClusterWorker<Data = unknown, Response = unknown> extends AbstractWorker<Worker, Data, Response> {
/**
* Constructs a new poolifier cluster worker.
*
* @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked.
* @param opts - Options for the worker.
*/
constructor(taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions);
/** @inheritDoc */
protected handleReadyMessage(message: MessageValue<Data>): void;
/** @inheritDoc */
protected get id(): number;
/** @inheritDoc */
protected sendToMainWorker(message: MessageValue<Response>): void;
}
/**
* A thread worker used by a poolifier `ThreadPool`.
*
* When this worker is inactive for more than the given `maxInactiveTime`,
* it will send a termination request to its main thread.
*
* If you use a `DynamicThreadPool` the extra workers that were created will be terminated,
* but the minimum number of workers will be guaranteed.
*
* @typeParam Data - Type of data this worker receives from pool's execution. This can only be structured-cloneable data.
* @typeParam Response - Type of response the worker sends back to the main thread. This can only be structured-cloneable data.
* @author [Alessandro Pio Ardizio](https://github.com/pioardi)
* @since 0.0.1
*/
declare class ThreadWorker<Data = unknown, Response = unknown> extends AbstractWorker<MessagePort, Data, Response> {
/**
* Message port used to communicate with the main worker.
*/
private port;
/**
* Constructs a new poolifier thread worker.
*
* @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked.
* @param opts - Options for the worker.
*/
constructor(taskFunctions: TaskFunction<Data, Response> | TaskFunctions<Data, Response>, opts?: WorkerOptions);
/** @inheritDoc */
protected handleReadyMessage(message: MessageValue<Data>): void;
/** @inheritDoc */
protected handleKillMessage(message: MessageValue<Data>): void;
/** @inheritDoc */
protected get id(): number;
/** @inheritDoc */
protected sendToMainWorker(message: MessageValue<Response>): void;
/** @inheritDoc */
protected handleError(e: Error | string): string;
}
/**
* Node.
*
* @typeParam T - Type of node data.
* @internal
*/
declare class Node<T> {
data: T;
next?: Node<T>;
prev?: Node<T>;
constructor(data: T);
}
/**
* Deque.
* Implemented with a doubly linked list.
*
* @typeParam T - Type of deque data.
* @internal
*/
declare class Deque<T> {
private head?;
private tail?;
/** The size of the deque. */
size: number;
/** The maximum size of the deque. */
maxSize: number;
constructor();
/**
* Appends data to the deque.
*
* @param data - Data to append.
* @returns The new size of the queue.
*/
push(data: T): number;
/**
* Prepends data to the deque.
*
* @param data - Data to prepend.
* @returns The new size of the queue.
*/
unshift(data: T): number;
/**
* Pops data from the deque.
*
* @returns The popped data or `undefined` if the deque is empty.
*/
pop(): T | undefined;
/**
* Shifts data from the deque.
*
* @returns The shifted data or `undefined` if the deque is empty.
*/
shift(): T | undefined;
/**
* Peeks at the first data.
* @returns The first data or `undefined` if the deque is empty.
*/
peekFirst(): T | undefined;
/**
* Peeks at the last data.
* @returns The last data or `undefined` if the deque is empty.
*/
peekLast(): T | undefined;
/**
* Clears the deque.
*/
clear(): void;
/**
* Returns an iterator for the deque.
*
* @returns An iterator for the deque.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
*/
[Symbol.iterator](): Iterator<T>;
/**
* Returns an backward iterator for the deque.
*
* @returns An backward iterator for the deque.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
*/
backward(): Iterable<T>;
private incrementSize;
}
/**
* Returns safe host OS optimized estimate of the default amount of parallelism a pool should use.
* Always returns a value greater than zero.
*
* @returns The host OS optimized maximum pool size.
* @internal
*/
declare const availableParallelism: () => number;
export { AbstractPool, AbstractWorker, CircularArray, type ClusterPoolOptions, ClusterWorker, Deque, DynamicClusterPool, DynamicThreadPool, type ErrorHandler, type EventLoopUtilizationMeasurementStatistics, type ExitHandler, FixedClusterPool, FixedThreadPool, type IPool, type IWorker, type IWorkerChoiceStrategy, type IWorkerNode, type KillBehavior, KillBehaviors, type KillHandler, type Measurement, type MeasurementOptions, type MeasurementStatistics, type MeasurementStatisticsRequirements, Measurements, type MessageHandler, type MessageValue, Node, type OnlineHandler, PoolEmitter, type PoolEvent, PoolEvents, type PoolInfo, type PoolOptions, type PoolType, PoolTypes, type PromiseResponseWrapper, type StrategyPolicy, type Task, type TaskAsyncFunction, type TaskError, type TaskFunction, type TaskFunctions, type TaskPerformance, type TaskStatistics, type TaskStatisticsRequirements, type TaskSyncFunction, type TasksQueueOptions, type ThreadPoolOptions, ThreadWorker, WorkerChoiceStrategies, type WorkerChoiceStrategy, WorkerChoiceStrategyContext, type WorkerChoiceStrategyOptions, type WorkerInfo, type WorkerNodeEventCallback, type WorkerOptions, type WorkerStatistics, type WorkerType, WorkerTypes, type WorkerUsage, type Writable, availableParallelism };

@@ -1,1 +0,1 @@

"use strict";var e=require("node:events"),t=require("node:worker_threads"),s=require("node:cluster"),r=require("node:crypto"),i=require("node:perf_hooks"),o=require("node:fs"),n=require("node:os"),a=require("node:async_hooks");function h(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(s){if("default"!==s){var r=Object.getOwnPropertyDescriptor(e,s);Object.defineProperty(t,s,r.get?r:{enumerable:!0,get:function(){return e[s]}})}})),t.default=e,Object.freeze(t)}var u=h(n);const k=Object.freeze({fixed:"fixed",dynamic:"dynamic"});class c extends e.EventEmitter{}const d=Object.freeze({ready:"ready",busy:"busy",full:"full",destroy:"destroy",error:"error",taskError:"taskError",backPressure:"backPressure"}),l=Object.freeze({thread:"thread",cluster:"cluster"}),g="default",m=Object.freeze((()=>{})),p={retries:6,runTime:{median:!1},waitTime:{median:!1},elu:{median:!1}},w={aggregate:!1,average:!1,median:!1},y=e=>e instanceof t.Worker?l.thread:e instanceof s.Worker?l.cluster:void 0,f=e=>e instanceof t.Worker?e.threadId:e instanceof s.Worker?e.id:void 0,T=e=>Array.isArray(e)&&0===e.length?0:Array.isArray(e)&&1===e.length?e[0]:e.reduce(((e,t)=>e+t),0)/e.length,W=e=>{if(Array.isArray(e)&&0===e.length)return 0;if(Array.isArray(e)&&1===e.length)return e[0];const t=e.slice().sort(((e,t)=>e-t));return(t[t.length-1>>1]+t[t.length>>1])/2},N=(e,t=2)=>{const s=Math.pow(10,t);return Math.round(e*s*(1+Number.EPSILON))/s},x=e=>"object"==typeof e&&null!==e&&e?.constructor===Object&&"[object Object]"===Object.prototype.toString.call(e),S=(e,t)=>t===e,E=e=>"function"==typeof e&&"AsyncFunction"===e.constructor.name,v=(e,t,s)=>{t.aggregate&&(e.aggregate=(e.aggregate??0)+s,e.minimum=Math.min(s,e.minimum??1/0),e.maximum=Math.max(s,e.maximum??-1/0),(t.average||t.median)&&null!=s&&(e.history.push(s),t.average?e.average=T(e.history):null!=e.average&&delete e.average,t.median?e.median=W(e.history):null!=e.median&&delete e.median))},b=()=>r.webcrypto.getRandomValues(new Uint32Array(1))[0]/4294967296,C=Object.freeze({SOFT:"SOFT",HARD:"HARD"}),I=Object.freeze({ROUND_ROBIN:"ROUND_ROBIN",LEAST_USED:"LEAST_USED",LEAST_BUSY:"LEAST_BUSY",LEAST_ELU:"LEAST_ELU",FAIR_SHARE:"FAIR_SHARE",WEIGHTED_ROUND_ROBIN:"WEIGHTED_ROUND_ROBIN",INTERLEAVED_WEIGHTED_ROUND_ROBIN:"INTERLEAVED_WEIGHTED_ROUND_ROBIN"}),R=Object.freeze({runTime:"runTime",waitTime:"waitTime",elu:"elu"});class z{pool;opts;nextWorkerNodeKey=0;previousWorkerNodeKey=0;strategyPolicy={dynamicWorkerUsage:!1,dynamicWorkerReady:!0};taskStatisticsRequirements={runTime:w,waitTime:w,elu:w};constructor(e,t=p){this.pool=e,this.opts=t,this.opts={...p,...t},this.choose=this.choose.bind(this)}setTaskStatisticsRequirements(e){this.toggleMedianMeasurementStatisticsRequirements(this.taskStatisticsRequirements.runTime,e.runTime?.median),this.toggleMedianMeasurementStatisticsRequirements(this.taskStatisticsRequirements.waitTime,e.waitTime?.median),this.toggleMedianMeasurementStatisticsRequirements(this.taskStatisticsRequirements.elu,e.elu?.median)}toggleMedianMeasurementStatisticsRequirements(e,t){e.average&&t&&(e.average=!1,e.median=t),e.median&&!t&&(e.average=!0,e.median=t)}resetWorkerNodeKeyProperties(){this.nextWorkerNodeKey=0,this.previousWorkerNodeKey=0}setOptions(e){this.opts={...p,...e},this.setTaskStatisticsRequirements(this.opts)}isWorkerNodeReady(e){return this.pool.workerNodes[e].info.ready}hasWorkerNodeBackPressure(e){return this.pool.hasWorkerNodeBackPressure(e)}isWorkerNodeEligible(e){return this.isWorkerNodeReady(e)&&!this.hasWorkerNodeBackPressure(e)}getWorkerTaskRunTime(e){return this.taskStatisticsRequirements.runTime.median?this.pool.workerNodes[e].usage.runTime?.median??0:this.pool.workerNodes[e].usage.runTime?.average??0}getWorkerTaskWaitTime(e){return this.taskStatisticsRequirements.waitTime.median?this.pool.workerNodes[e].usage.waitTime?.median??0:this.pool.workerNodes[e].usage.waitTime?.average??0}getWorkerTaskElu(e){return this.taskStatisticsRequirements.elu.median?this.pool.workerNodes[e].usage.elu.active?.median??0:this.pool.workerNodes[e].usage.elu.active?.average??0}checkNextWorkerNodeEligibility(e){this.isWorkerNodeEligible(this.nextWorkerNodeKey)||(this.nextWorkerNodeKey=void 0,this.previousWorkerNodeKey=e??this.previousWorkerNodeKey)}computeDefaultWorkerWeight(){let e=0;for(const t of n.cpus()){const s=t.speed.toString().length-1;e+=1/(t.speed/Math.pow(10,s))*Math.pow(10,s)}return Math.round(e/n.cpus().length)}}class O extends z{taskStatisticsRequirements={runTime:{aggregate:!0,average:!0,median:!1},waitTime:w,elu:{aggregate:!0,average:!0,median:!1}};workersVirtualTaskEndTimestamp=[];constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return this.workersVirtualTaskEndTimestamp=[],!0}update(e){return this.computeWorkerVirtualTaskEndTimestamp(e),!0}choose(){return this.nextWorkerNodeKey=this.fairShareNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(e){return this.workersVirtualTaskEndTimestamp.splice(e,1),!0}fairShareNextWorkerNodeKey(){let e,t=1/0;for(const[s]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;null==this.workersVirtualTaskEndTimestamp[s]&&this.computeWorkerVirtualTaskEndTimestamp(s);const r=this.workersVirtualTaskEndTimestamp[s];r<t&&(t=r,e=s)}return e}computeWorkerVirtualTaskEndTimestamp(e){this.workersVirtualTaskEndTimestamp[e]=this.getWorkerVirtualTaskEndTimestamp(e,this.getWorkerVirtualTaskStartTimestamp(e))}getWorkerVirtualTaskEndTimestamp(e,t){return t+(this.opts.measurement===R.elu?this.getWorkerTaskElu(e):this.getWorkerTaskRunTime(e))}getWorkerVirtualTaskStartTimestamp(e){return Math.max(performance.now(),this.workersVirtualTaskEndTimestamp[e]??-1/0)}}class F extends z{roundId=0;roundWeights;defaultWorkerWeight;constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts),this.defaultWorkerWeight=this.computeDefaultWorkerWeight(),this.roundWeights=this.getRoundWeights()}reset(){return this.resetWorkerNodeKeyProperties(),this.roundId=0,!0}update(){return!0}choose(){let e,t;for(let s=this.roundId;s<this.roundWeights.length;s++){e=s;for(let e=this.nextWorkerNodeKey??this.previousWorkerNodeKey;e<this.pool.workerNodes.length;e++){if(!this.isWorkerNodeEligible(e))continue;if((this.opts.weights?.[e]??this.defaultWorkerWeight)>=this.roundWeights[s]){t=e;break}}}this.roundId=e,null==t&&(this.previousWorkerNodeKey=this.nextWorkerNodeKey??this.previousWorkerNodeKey),this.nextWorkerNodeKey=t;const s=this.nextWorkerNodeKey;return this.nextWorkerNodeKey===this.pool.workerNodes.length-1?(this.nextWorkerNodeKey=0,this.roundId=this.roundId===this.roundWeights.length-1?0:this.roundId+1):this.nextWorkerNodeKey=(this.nextWorkerNodeKey??this.previousWorkerNodeKey)+1,s}remove(e){return this.nextWorkerNodeKey===e&&(0===this.pool.workerNodes.length?this.nextWorkerNodeKey=0:this.nextWorkerNodeKey>this.pool.workerNodes.length-1&&(this.nextWorkerNodeKey=this.pool.workerNodes.length-1,this.roundId=this.roundId===this.roundWeights.length-1?0:this.roundId+1)),!0}setOptions(e){super.setOptions(e),this.roundWeights=this.getRoundWeights()}getRoundWeights(){return null==this.opts.weights?[this.defaultWorkerWeight]:[...new Set(Object.values(this.opts.weights).slice().sort(((e,t)=>e-t)))]}}class M extends z{taskStatisticsRequirements={runTime:{aggregate:!0,average:!1,median:!1},waitTime:{aggregate:!0,average:!1,median:!1},elu:w};constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return!0}update(){return!0}choose(){return this.nextWorkerNodeKey=this.leastBusyNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(){return!0}leastBusyNextWorkerNodeKey(){let e,t=1/0;for(const[s,r]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;const i=(r.usage.runTime?.aggregate??0)+(r.usage.waitTime?.aggregate??0);if(0===i){e=s;break}i<t&&(t=i,e=s)}return e}}class Q extends z{constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return!0}update(){return!0}choose(){return this.nextWorkerNodeKey=this.leastUsedNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(){return!0}leastUsedNextWorkerNodeKey(){let e,t=1/0;for(const[s,r]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;const i=r.usage.tasks,o=i.executed+i.executing+i.queued;if(0===o){e=s;break}o<t&&(t=o,e=s)}return e}}class K extends z{taskStatisticsRequirements={runTime:w,waitTime:w,elu:{aggregate:!0,average:!1,median:!1}};constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return!0}update(){return!0}choose(){return this.nextWorkerNodeKey=this.leastEluNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(){return!0}leastEluNextWorkerNodeKey(){let e,t=1/0;for(const[s,r]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;const i=r.usage,o=i.elu?.active?.aggregate??0;if(0===o){e=s;break}o<t&&(t=o,e=s)}return e}}class P extends z{constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return this.resetWorkerNodeKeyProperties(),!0}update(){return!0}choose(){const e=this.nextWorkerNodeKey;return this.roundRobinNextWorkerNodeKey(),this.checkNextWorkerNodeEligibility(e),e}remove(e){return this.nextWorkerNodeKey===e&&(0===this.pool.workerNodes.length?this.nextWorkerNodeKey=0:this.nextWorkerNodeKey>this.pool.workerNodes.length-1&&(this.nextWorkerNodeKey=this.pool.workerNodes.length-1)),!0}roundRobinNextWorkerNodeKey(){return this.nextWorkerNodeKey=this.nextWorkerNodeKey===this.pool.workerNodes.length-1?0:(this.nextWorkerNodeKey??this.previousWorkerNodeKey)+1,this.nextWorkerNodeKey}}class q extends z{taskStatisticsRequirements={runTime:{aggregate:!0,average:!0,median:!1},waitTime:w,elu:w};defaultWorkerWeight;workerVirtualTaskRunTime=0;constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts),this.defaultWorkerWeight=this.computeDefaultWorkerWeight()}reset(){return this.resetWorkerNodeKeyProperties(),this.workerVirtualTaskRunTime=0,!0}update(){return!0}choose(){const e=this.nextWorkerNodeKey;return this.weightedRoundRobinNextWorkerNodeKey(),this.checkNextWorkerNodeEligibility(e),e}remove(e){return this.nextWorkerNodeKey===e&&(0===this.pool.workerNodes.length?this.nextWorkerNodeKey=0:this.nextWorkerNodeKey>this.pool.workerNodes.length-1&&(this.nextWorkerNodeKey=this.pool.workerNodes.length-1),this.workerVirtualTaskRunTime=0),!0}weightedRoundRobinNextWorkerNodeKey(){const e=this.workerVirtualTaskRunTime;return e<(this.opts.weights?.[this.nextWorkerNodeKey??this.previousWorkerNodeKey]??this.defaultWorkerWeight)?this.workerVirtualTaskRunTime=e+this.getWorkerTaskRunTime(this.nextWorkerNodeKey??this.previousWorkerNodeKey):(this.nextWorkerNodeKey=this.nextWorkerNodeKey===this.pool.workerNodes.length-1?0:(this.nextWorkerNodeKey??this.previousWorkerNodeKey)+1,this.workerVirtualTaskRunTime=0),this.nextWorkerNodeKey}}class A{workerChoiceStrategy;opts;workerChoiceStrategies;retriesCount=0;constructor(e,t=I.ROUND_ROBIN,s=p){this.workerChoiceStrategy=t,this.opts=s,this.opts={...p,...s},this.execute=this.execute.bind(this),this.workerChoiceStrategies=new Map([[I.ROUND_ROBIN,new(P.bind(this))(e,s)],[I.LEAST_USED,new(Q.bind(this))(e,s)],[I.LEAST_BUSY,new(M.bind(this))(e,s)],[I.LEAST_ELU,new(K.bind(this))(e,s)],[I.FAIR_SHARE,new(O.bind(this))(e,s)],[I.WEIGHTED_ROUND_ROBIN,new(q.bind(this))(e,s)],[I.INTERLEAVED_WEIGHTED_ROUND_ROBIN,new(F.bind(this))(e,s)]])}getStrategyPolicy(){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).strategyPolicy}getTaskStatisticsRequirements(){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).taskStatisticsRequirements}setWorkerChoiceStrategy(e){this.workerChoiceStrategy!==e&&(this.workerChoiceStrategy=e),this.workerChoiceStrategies.get(this.workerChoiceStrategy)?.reset()}update(e){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).update(e)}execute(){const e=this.workerChoiceStrategies.get(this.workerChoiceStrategy).choose();if(null==e&&this.retriesCount<this.opts.retries)return this.retriesCount++,this.execute();if(null==e)throw new Error(`Worker node key chosen is null or undefined after ${this.retriesCount} retries`);return this.retriesCount=0,e}remove(e){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).remove(e)}setOptions(e){this.opts={...p,...e};for(const t of this.workerChoiceStrategies.values())t.setOptions(e)}}class U extends Array{size;constructor(e=1024,...t){super(),this.checkSize(e),this.size=e,arguments.length>1&&this.push(...t)}push(...e){const t=super.push(...e);return t>this.size&&super.splice(0,t-this.size),this.length}unshift(...e){return super.unshift(...e)>this.size&&super.splice(this.size,e.length),this.length}concat(...e){const t=super.concat(e);return t.size=this.size,t.length>t.size&&t.splice(0,t.length-t.size),t}splice(e,t,...s){let r=[];if(arguments.length>=3&&null!=t){if(r=super.splice(e,t,...s),this.length>this.size){const e=super.splice(0,this.length-this.size);r=new U(r.length+e.length,...r,...e)}}else r=2===arguments.length?super.splice(e,t):super.splice(e);return r}resize(e){if(this.checkSize(e),0===e)this.length=0;else if(e<this.size)for(let t=e;t<this.size;t++)super.pop();this.size=e}empty(){return 0===this.length}full(){return this.length===this.size}checkSize(e){if(!Number.isSafeInteger(e))throw new TypeError(`Invalid circular array size: ${e} is not a safe integer`);if(e<0)throw new RangeError(`Invalid circular array size: ${e} < 0`)}}class B{data;next;prev;constructor(e){this.data=e}}class D{head;tail;size;maxSize;constructor(){this.clear()}push(e){const t=new B(e);return null==this.tail?this.head=this.tail=t:(t.prev=this.tail,this.tail=this.tail.next=t),this.incrementSize()}unshift(e){const t=new B(e);return null==this.head?this.head=this.tail=t:(t.next=this.head,this.head=this.head.prev=t),this.incrementSize()}pop(){if(null==this.head)return;const e=this.tail;return this.tail=this.tail.prev,null==this.tail?this.head=void 0:this.tail.next=void 0,--this.size,e?.data}shift(){if(null==this.head)return;const e=this.head;return this.head=this.head.next,null==this.head?this.tail=void 0:this.head.prev=void 0,--this.size,e?.data}peekFirst(){return this.head?.data}peekLast(){return this.tail?.data}clear(){this.head=void 0,this.tail=void 0,this.size=0,this.maxSize=0}[Symbol.iterator](){let e=this.head;return{next:()=>{if(null==e)return{value:void 0,done:!0};const t={value:e.data,done:!1};return e=e.next,t}}}backward(){return{[Symbol.iterator]:()=>{let e=this.tail;return{next:()=>{if(null==e)return{value:void 0,done:!0};const t={value:e.data,done:!1};return e=e.prev,t}}}}}incrementSize(){return++this.size,this.size>this.maxSize&&(this.maxSize=this.size),this.size}}class L{worker;info;usage;messageChannel;tasksQueueBackPressureSize;onBackPressure;onEmptyQueue;tasksQueue;onEmptyQueueCount;taskFunctionsUsage;constructor(e,s){if(null==e)throw new TypeError("Cannot construct a worker node without a worker");if(null==s)throw new TypeError("Cannot construct a worker node without a tasks queue back pressure size");if(!Number.isSafeInteger(s))throw new TypeError("Cannot construct a worker node with a tasks queue back pressure size that is not an integer");this.worker=e,this.info=this.initWorkerInfo(e),this.usage=this.initWorkerUsage(),this.info.type===l.thread&&(this.messageChannel=new t.MessageChannel),this.tasksQueueBackPressureSize=s,this.tasksQueue=new D,this.onEmptyQueueCount=0,this.taskFunctionsUsage=new Map}tasksQueueSize(){return this.tasksQueue.size}enqueueTask(e){const t=this.tasksQueue.push(e);return null!=this.onBackPressure&&this.hasBackPressure()&&this.onBackPressure(this.info.id),t}unshiftTask(e){const t=this.tasksQueue.unshift(e);return null!=this.onBackPressure&&this.hasBackPressure()&&this.onBackPressure(this.info.id),t}dequeueTask(){const e=this.tasksQueue.shift();return null!=this.onEmptyQueue&&0===this.tasksQueue.size&&this.startOnEmptyQueue().catch(m),e}popTask(){const e=this.tasksQueue.pop();return null!=this.onEmptyQueue&&0===this.tasksQueue.size&&this.startOnEmptyQueue().catch(m),e}clearTasksQueue(){this.tasksQueue.clear()}hasBackPressure(){return this.tasksQueue.size>=this.tasksQueueBackPressureSize}resetUsage(){this.usage=this.initWorkerUsage(),this.taskFunctionsUsage.clear()}closeChannel(){null!=this.messageChannel&&(this.messageChannel?.port1.unref(),this.messageChannel?.port2.unref(),this.messageChannel?.port1.close(),this.messageChannel?.port2.close(),delete this.messageChannel)}getTaskFunctionWorkerUsage(e){if(!Array.isArray(this.info.taskFunctions))throw new Error(`Cannot get task function worker usage for task function name '${e}' when task function names list is not yet defined`);if(Array.isArray(this.info.taskFunctions)&&this.info.taskFunctions.length<3)throw new Error(`Cannot get task function worker usage for task function name '${e}' when task function names list has less than 3 elements`);return e===g&&(e=this.info.taskFunctions[1]),this.taskFunctionsUsage.has(e)||this.taskFunctionsUsage.set(e,this.initTaskFunctionWorkerUsage(e)),this.taskFunctionsUsage.get(e)}async startOnEmptyQueue(){this.onEmptyQueueCount>0&&(this.usage.tasks.executing>0||this.tasksQueue.size>0)?this.onEmptyQueueCount=0:(this.onEmptyQueue(this.info.id),++this.onEmptyQueueCount,await(async e=>{await new Promise((t=>{setTimeout(t,e)}))})(((e=0,t=100)=>{const s=Math.pow(2,e)*t;return s+.2*s*b()})(this.onEmptyQueueCount)),await this.startOnEmptyQueue())}initWorkerInfo(e){return{id:f(e),type:y(e),dynamic:!1,ready:!1}}initWorkerUsage(){const e=()=>this.tasksQueue.size,t=()=>this.tasksQueue.maxSize;return{tasks:{executed:0,executing:0,get queued(){return e()},get maxQueued(){return t()},stolen:0,failed:0},runTime:{history:new U},waitTime:{history:new U},elu:{idle:{history:new U},active:{history:new U}}}}initTaskFunctionWorkerUsage(e){const t=()=>{let t=0;for(const s of this.tasksQueue)(s.name===g&&e===this.info.taskFunctions[1]||s.name!==g&&e===s.name)&&++t;return t};return{tasks:{executed:0,executing:0,get queued(){return t()},stolen:0,failed:0},runTime:{history:new U},waitTime:{history:new U},elu:{idle:{history:new U},active:{history:new U}}}}}class V{numberOfWorkers;filePath;opts;workerNodes=[];emitter;promiseResponseMap=new Map;workerChoiceStrategyContext;max;starting;started;startTimestamp;constructor(e,t,s){if(this.numberOfWorkers=e,this.filePath=t,this.opts=s,!this.isMain())throw new Error("Cannot start a pool from a worker with the same type as the pool");this.checkNumberOfWorkers(this.numberOfWorkers),this.checkFilePath(this.filePath),this.checkPoolOptions(this.opts),this.chooseWorkerNode=this.chooseWorkerNode.bind(this),this.executeTask=this.executeTask.bind(this),this.enqueueTask=this.enqueueTask.bind(this),!0===this.opts.enableEvents&&(this.emitter=new c),this.workerChoiceStrategyContext=new A(this,this.opts.workerChoiceStrategy,this.opts.workerChoiceStrategyOptions),this.setupHook(),this.starting=!0,this.startPool(),this.starting=!1,this.started=!0,this.startTimestamp=i.performance.now()}checkFilePath(e){if(null==e||"string"!=typeof e||"string"==typeof e&&0===e.trim().length)throw new Error("Please specify a file with a worker implementation");if(!o.existsSync(e))throw new Error(`Cannot find the worker file '${e}'`)}checkNumberOfWorkers(e){if(null==e)throw new Error("Cannot instantiate a pool without specifying the number of workers");if(!Number.isSafeInteger(e))throw new TypeError("Cannot instantiate a pool with a non safe integer number of workers");if(e<0)throw new RangeError("Cannot instantiate a pool with a negative number of workers");if(this.type===k.fixed&&0===e)throw new RangeError("Cannot instantiate a fixed pool with zero worker")}checkDynamicPoolSize(e,t){if(this.type===k.dynamic){if(null==t)throw new TypeError("Cannot instantiate a dynamic pool without specifying the maximum pool size");if(!Number.isSafeInteger(t))throw new TypeError("Cannot instantiate a dynamic pool with a non safe integer maximum pool size");if(e>t)throw new RangeError("Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size");if(0===t)throw new RangeError("Cannot instantiate a dynamic pool with a maximum pool size equal to zero");if(e===t)throw new RangeError("Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead")}}checkPoolOptions(e){if(!x(e))throw new TypeError("Invalid pool options: must be a plain object");this.opts.workerChoiceStrategy=e.workerChoiceStrategy??I.ROUND_ROBIN,this.checkValidWorkerChoiceStrategy(this.opts.workerChoiceStrategy),this.opts.workerChoiceStrategyOptions={...p,...e.workerChoiceStrategyOptions},this.checkValidWorkerChoiceStrategyOptions(this.opts.workerChoiceStrategyOptions),this.opts.restartWorkerOnError=e.restartWorkerOnError??!0,this.opts.enableEvents=e.enableEvents??!0,this.opts.enableTasksQueue=e.enableTasksQueue??!1,this.opts.enableTasksQueue&&(this.checkValidTasksQueueOptions(e.tasksQueueOptions),this.opts.tasksQueueOptions=this.buildTasksQueueOptions(e.tasksQueueOptions))}checkValidWorkerChoiceStrategy(e){if(!Object.values(I).includes(e))throw new Error(`Invalid worker choice strategy '${e}'`)}checkValidWorkerChoiceStrategyOptions(e){if(!x(e))throw new TypeError("Invalid worker choice strategy options: must be a plain object");if(null!=e.retries&&!Number.isSafeInteger(e.retries))throw new TypeError("Invalid worker choice strategy options: retries must be an integer");if(null!=e.retries&&e.retries<0)throw new RangeError(`Invalid worker choice strategy options: retries '${e.retries}' must be greater or equal than zero`);if(null!=e.weights&&Object.keys(e.weights).length!==this.maxSize)throw new Error("Invalid worker choice strategy options: must have a weight for each worker node");if(null!=e.measurement&&!Object.values(R).includes(e.measurement))throw new Error(`Invalid worker choice strategy options: invalid measurement '${e.measurement}'`)}checkValidTasksQueueOptions(e){if(null!=e&&!x(e))throw new TypeError("Invalid tasks queue options: must be a plain object");if(null!=e?.concurrency&&!Number.isSafeInteger(e.concurrency))throw new TypeError("Invalid worker node tasks concurrency: must be an integer");if(null!=e?.concurrency&&e.concurrency<=0)throw new RangeError(`Invalid worker node tasks concurrency: ${e.concurrency} is a negative integer or zero`);if(null!=e?.queueMaxSize)throw new Error("Invalid tasks queue options: queueMaxSize is deprecated, please use size instead");if(null!=e?.size&&!Number.isSafeInteger(e.size))throw new TypeError("Invalid worker node tasks queue size: must be an integer");if(null!=e?.size&&e.size<=0)throw new RangeError(`Invalid worker node tasks queue size: ${e.size} is a negative integer or zero`)}startPool(){for(;this.workerNodes.reduce(((e,t)=>t.info.dynamic?e:e+1),0)<this.numberOfWorkers;)this.createAndSetupWorkerNode()}get info(){return{version:"2.6.37",type:this.type,worker:this.worker,ready:this.ready,strategy:this.opts.workerChoiceStrategy,minSize:this.minSize,maxSize:this.maxSize,...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.aggregate&&this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.aggregate&&{utilization:N(this.utilization)},workerNodes:this.workerNodes.length,idleWorkerNodes:this.workerNodes.reduce(((e,t)=>0===t.usage.tasks.executing?e+1:e),0),busyWorkerNodes:this.workerNodes.reduce(((e,t)=>t.usage.tasks.executing>0?e+1:e),0),executedTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.executed),0),executingTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.executing),0),...!0===this.opts.enableTasksQueue&&{queuedTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.queued),0)},...!0===this.opts.enableTasksQueue&&{maxQueuedTasks:this.workerNodes.reduce(((e,t)=>e+(t.usage.tasks?.maxQueued??0)),0)},...!0===this.opts.enableTasksQueue&&{backPressure:this.hasBackPressure()},...!0===this.opts.enableTasksQueue&&{stolenTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.stolen),0)},failedTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.failed),0),...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.aggregate&&{runTime:{minimum:N(Math.min(...this.workerNodes.map((e=>e.usage.runTime?.minimum??1/0)))),maximum:N(Math.max(...this.workerNodes.map((e=>e.usage.runTime?.maximum??-1/0)))),...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.average&&{average:N(T(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.runTime.history)),[])))},...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median&&{median:N(W(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.runTime.history)),[])))}}},...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.aggregate&&{waitTime:{minimum:N(Math.min(...this.workerNodes.map((e=>e.usage.waitTime?.minimum??1/0)))),maximum:N(Math.max(...this.workerNodes.map((e=>e.usage.waitTime?.maximum??-1/0)))),...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.average&&{average:N(T(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.waitTime.history)),[])))},...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.median&&{median:N(W(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.waitTime.history)),[])))}}}}}get ready(){return this.workerNodes.reduce(((e,t)=>!t.info.dynamic&&t.info.ready?e+1:e),0)>=this.minSize}get utilization(){const e=(i.performance.now()-this.startTimestamp)*this.maxSize;return(this.workerNodes.reduce(((e,t)=>e+(t.usage.runTime?.aggregate??0)),0)+this.workerNodes.reduce(((e,t)=>e+(t.usage.waitTime?.aggregate??0)),0))/e}get minSize(){return this.numberOfWorkers}get maxSize(){return this.max??this.numberOfWorkers}checkMessageWorkerId(e){if(null==e.workerId)throw new Error("Worker message received without worker id");if(null!=e.workerId&&-1===this.getWorkerNodeKeyByWorkerId(e.workerId))throw new Error(`Worker message received from unknown worker '${e.workerId}'`)}getWorkerNodeKeyByWorker(e){return this.workerNodes.findIndex((t=>t.worker===e))}getWorkerNodeKeyByWorkerId(e){return this.workerNodes.findIndex((t=>t.info.id===e))}setWorkerChoiceStrategy(e,t){this.checkValidWorkerChoiceStrategy(e),this.opts.workerChoiceStrategy=e,this.workerChoiceStrategyContext.setWorkerChoiceStrategy(this.opts.workerChoiceStrategy),null!=t&&this.setWorkerChoiceStrategyOptions(t);for(const[e,t]of this.workerNodes.entries())t.resetUsage(),this.sendStatisticsMessageToWorker(e)}setWorkerChoiceStrategyOptions(e){this.checkValidWorkerChoiceStrategyOptions(e),this.opts.workerChoiceStrategyOptions={...p,...e},this.workerChoiceStrategyContext.setOptions(this.opts.workerChoiceStrategyOptions)}enableTasksQueue(e,t){!0!==this.opts.enableTasksQueue||e||this.flushTasksQueues(),this.opts.enableTasksQueue=e,this.setTasksQueueOptions(t)}setTasksQueueOptions(e){!0===this.opts.enableTasksQueue?(this.checkValidTasksQueueOptions(e),this.opts.tasksQueueOptions=this.buildTasksQueueOptions(e),this.setTasksQueueMaxSize(this.opts.tasksQueueOptions.size)):null!=this.opts.tasksQueueOptions&&delete this.opts.tasksQueueOptions}setTasksQueueMaxSize(e){for(const t of this.workerNodes)t.tasksQueueBackPressureSize=e}buildTasksQueueOptions(e){return{size:Math.pow(this.maxSize,2),concurrency:1,...e}}get full(){return this.workerNodes.length>=this.maxSize}internalBusy(){return!0===this.opts.enableTasksQueue?-1===this.workerNodes.findIndex((e=>e.info.ready&&e.usage.tasks.executing<this.opts.tasksQueueOptions?.concurrency)):-1===this.workerNodes.findIndex((e=>e.info.ready&&0===e.usage.tasks.executing))}listTaskFunctions(){for(const e of this.workerNodes)if(Array.isArray(e.info.taskFunctions)&&e.info.taskFunctions.length>0)return e.info.taskFunctions;return[]}shallExecuteTask(e){return 0===this.tasksQueueSize(e)&&this.workerNodes[e].usage.tasks.executing<this.opts.tasksQueueOptions?.concurrency}async execute(e,t,s){return await new Promise(((o,n)=>{if(!this.started)return void n(new Error("Cannot execute a task on destroyed pool"));if(null!=t&&"string"!=typeof t)return void n(new TypeError("name argument must be a string"));if(null!=t&&"string"==typeof t&&0===t.trim().length)return void n(new TypeError("name argument must not be an empty string"));if(null!=s&&!Array.isArray(s))return void n(new TypeError("transferList argument must be an array"));const a=i.performance.now(),h=this.chooseWorkerNode(),u=this.getWorkerInfo(h),k={name:t??g,data:e??{},transferList:s,timestamp:a,workerId:u.id,taskId:r.randomUUID()};this.promiseResponseMap.set(k.taskId,{resolve:o,reject:n,workerNodeKey:h}),!1===this.opts.enableTasksQueue||!0===this.opts.enableTasksQueue&&this.shallExecuteTask(h)?this.executeTask(h,k):this.enqueueTask(h,k)}))}async destroy(){await Promise.all(this.workerNodes.map((async(e,t)=>{await this.destroyWorkerNode(t)}))),this.emitter?.emit(d.destroy,this.info),this.started=!1}async sendKillMessageToWorker(e,t){await new Promise(((s,r)=>{this.registerWorkerMessageListener(e,(e=>{"success"===e.kill?s():"failure"===e.kill&&r(new Error(`Worker ${t} kill message handling failed`))})),this.sendToWorker(e,{kill:!0,workerId:t})}))}setupHook(){}beforeTaskExecutionHook(e,t){if(null!=this.workerNodes[e]?.usage){const s=this.workerNodes[e].usage;++s.tasks.executing,this.updateWaitTimeWorkerUsage(s,t)}if(this.shallUpdateTaskFunctionWorkerUsage(e)&&null!=this.workerNodes[e].getTaskFunctionWorkerUsage(t.name)){const s=this.workerNodes[e].getTaskFunctionWorkerUsage(t.name);++s.tasks.executing,this.updateWaitTimeWorkerUsage(s,t)}}afterTaskExecutionHook(e,t){if(null!=this.workerNodes[e]?.usage){const s=this.workerNodes[e].usage;this.updateTaskStatisticsWorkerUsage(s,t),this.updateRunTimeWorkerUsage(s,t),this.updateEluWorkerUsage(s,t)}if(this.shallUpdateTaskFunctionWorkerUsage(e)&&null!=this.workerNodes[e].getTaskFunctionWorkerUsage(t.taskPerformance?.name)){const s=this.workerNodes[e].getTaskFunctionWorkerUsage(t.taskPerformance?.name);this.updateTaskStatisticsWorkerUsage(s,t),this.updateRunTimeWorkerUsage(s,t),this.updateEluWorkerUsage(s,t)}}shallUpdateTaskFunctionWorkerUsage(e){const t=this.getWorkerInfo(e);return null!=t&&Array.isArray(t.taskFunctions)&&t.taskFunctions.length>2}updateTaskStatisticsWorkerUsage(e,t){const s=e.tasks;null!=s.executing&&s.executing>0&&--s.executing,null==t.taskError?++s.executed:++s.failed}updateRunTimeWorkerUsage(e,t){null==t.taskError&&v(e.runTime,this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime,t.taskPerformance?.runTime??0)}updateWaitTimeWorkerUsage(e,t){const s=i.performance.now(),r=s-(t.timestamp??s);v(e.waitTime,this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime,r)}updateEluWorkerUsage(e,t){if(null!=t.taskError)return;const s=this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu;v(e.elu.active,s,t.taskPerformance?.elu?.active??0),v(e.elu.idle,s,t.taskPerformance?.elu?.idle??0),s.aggregate&&null!=t.taskPerformance?.elu&&(null!=e.elu.utilization?e.elu.utilization=(e.elu.utilization+t.taskPerformance.elu.utilization)/2:e.elu.utilization=t.taskPerformance.elu.utilization)}chooseWorkerNode(){if(this.shallCreateDynamicWorker()){const e=this.createAndSetupDynamicWorkerNode();if(this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerUsage)return e}return this.workerChoiceStrategyContext.execute()}shallCreateDynamicWorker(){return this.type===k.dynamic&&!this.full&&this.internalBusy()}createAndSetupWorkerNode(){const e=this.createWorker();e.on("online",this.opts.onlineHandler??m),e.on("message",this.opts.messageHandler??m),e.on("error",this.opts.errorHandler??m),e.on("error",(t=>{const s=this.getWorkerNodeKeyByWorker(e),r=this.getWorkerInfo(s);r.ready=!1,this.workerNodes[s].closeChannel(),this.emitter?.emit(d.error,t),!0===this.opts.restartWorkerOnError&&!this.starting&&this.started&&(r.dynamic?this.createAndSetupDynamicWorkerNode():this.createAndSetupWorkerNode()),!0===this.opts.enableTasksQueue&&this.redistributeQueuedTasks(s)})),e.on("exit",this.opts.exitHandler??m),e.once("exit",(()=>{this.removeWorkerNode(e)}));const t=this.addWorkerNode(e);return this.afterWorkerNodeSetup(t),t}createAndSetupDynamicWorkerNode(){const e=this.createAndSetupWorkerNode();this.registerWorkerMessageListener(e,(e=>{const t=this.getWorkerNodeKeyByWorkerId(e.workerId),s=this.workerNodes[t].usage;(S(C.HARD,e.kill)||S(C.SOFT,e.kill)&&(!1===this.opts.enableTasksQueue&&0===s.tasks.executing||!0===this.opts.enableTasksQueue&&0===s.tasks.executing&&0===this.tasksQueueSize(t)))&&this.destroyWorkerNode(t).catch((e=>{this.emitter?.emit(d.error,e)}))}));const t=this.getWorkerInfo(e);return this.sendToWorker(e,{checkActive:!0,workerId:t.id}),t.dynamic=!0,(this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerReady||this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerUsage)&&(t.ready=!0),this.checkAndEmitDynamicWorkerCreationEvents(),e}afterWorkerNodeSetup(e){this.registerWorkerMessageListener(e,this.workerListener()),this.sendStartupMessageToWorker(e),this.sendStatisticsMessageToWorker(e),!0===this.opts.enableTasksQueue&&(this.workerNodes[e].onEmptyQueue=this.taskStealingOnEmptyQueue.bind(this),this.workerNodes[e].onBackPressure=this.tasksStealingOnBackPressure.bind(this))}sendStatisticsMessageToWorker(e){this.sendToWorker(e,{statistics:{runTime:this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.aggregate,elu:this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu.aggregate},workerId:this.getWorkerInfo(e).id})}redistributeQueuedTasks(e){for(;this.tasksQueueSize(e)>0;){let t,s=1/0;for(const[r,i]of this.workerNodes.entries())if(i.info.ready&&r!==e){if(0===i.usage.tasks.queued){t=r;break}i.usage.tasks.queued<s&&(s=i.usage.tasks.queued,t=r)}if(null!=t){const s=this.workerNodes[t],r={...this.dequeueTask(e),workerId:s.info.id};this.shallExecuteTask(t)?this.executeTask(t,r):this.enqueueTask(t,r)}}}updateTaskStolenStatisticsWorkerUsage(e,t){const s=this.workerNodes[e];if(null!=s?.usage&&++s.usage.tasks.stolen,this.shallUpdateTaskFunctionWorkerUsage(e)&&null!=s.getTaskFunctionWorkerUsage(t)){++s.getTaskFunctionWorkerUsage(t).tasks.stolen}}taskStealingOnEmptyQueue(e){const t=this.getWorkerNodeKeyByWorkerId(e),s=this.workerNodes[t],r=this.workerNodes.slice().sort(((e,t)=>t.usage.tasks.queued-e.usage.tasks.queued));for(const i of r){if(0===i.usage.tasks.queued)break;if(i.info.ready&&i.info.id!==e&&i.usage.tasks.queued>0){const e={...i.popTask(),workerId:s.info.id};this.shallExecuteTask(t)?this.executeTask(t,e):this.enqueueTask(t,e),this.updateTaskStolenStatisticsWorkerUsage(t,e.name);break}}}tasksStealingOnBackPressure(e){if(this.opts.tasksQueueOptions?.size<=1)return;const t=this.workerNodes[this.getWorkerNodeKeyByWorkerId(e)],s=this.workerNodes.slice().sort(((e,t)=>e.usage.tasks.queued-t.usage.tasks.queued));for(const[r,i]of s.entries())if(t.usage.tasks.queued>0&&i.info.ready&&i.info.id!==e&&i.usage.tasks.queued<this.opts.tasksQueueOptions?.size-1){const e={...t.popTask(),workerId:i.info.id};this.shallExecuteTask(r)?this.executeTask(r,e):this.enqueueTask(r,e),this.updateTaskStolenStatisticsWorkerUsage(r,e.name)}}workerListener(){return e=>{this.checkMessageWorkerId(e),null!=e.ready&&null!=e.taskFunctions?this.handleWorkerReadyResponse(e):null!=e.taskId?this.handleTaskExecutionResponse(e):null!=e.taskFunctions&&(this.getWorkerInfo(this.getWorkerNodeKeyByWorkerId(e.workerId)).taskFunctions=e.taskFunctions)}}handleWorkerReadyResponse(e){if(!1===e.ready)throw new Error(`Worker ${e.workerId} failed to initialize`);const t=this.getWorkerInfo(this.getWorkerNodeKeyByWorkerId(e.workerId));t.ready=e.ready,t.taskFunctions=e.taskFunctions,null!=this.emitter&&this.ready&&this.emitter.emit(d.ready,this.info)}handleTaskExecutionResponse(e){const{taskId:t,taskError:s,data:r}=e,i=this.promiseResponseMap.get(t);if(null!=i){null!=s?(this.emitter?.emit(d.taskError,s),i.reject(s.message)):i.resolve(r);const o=i.workerNodeKey;this.afterTaskExecutionHook(o,e),this.promiseResponseMap.delete(t),!0===this.opts.enableTasksQueue&&this.tasksQueueSize(o)>0&&this.workerNodes[o].usage.tasks.executing<this.opts.tasksQueueOptions?.concurrency&&this.executeTask(o,this.dequeueTask(o)),this.workerChoiceStrategyContext.update(o)}}checkAndEmitTaskExecutionEvents(){this.busy&&this.emitter?.emit(d.busy,this.info)}checkAndEmitTaskQueuingEvents(){this.hasBackPressure()&&this.emitter?.emit(d.backPressure,this.info)}checkAndEmitDynamicWorkerCreationEvents(){this.type===k.dynamic&&this.full&&this.emitter?.emit(d.full,this.info)}getWorkerInfo(e){return this.workerNodes[e]?.info}addWorkerNode(e){const t=new L(e,this.opts.tasksQueueOptions?.size??Math.pow(this.maxSize,2));this.starting&&(t.info.ready=!0),this.workerNodes.push(t);const s=this.getWorkerNodeKeyByWorker(e);if(-1===s)throw new Error("Worker node added not found");return s}removeWorkerNode(e){const t=this.getWorkerNodeKeyByWorker(e);-1!==t&&(this.workerNodes.splice(t,1),this.workerChoiceStrategyContext.remove(t))}hasWorkerNodeBackPressure(e){return!0===this.opts.enableTasksQueue&&this.workerNodes[e].hasBackPressure()}hasBackPressure(){return!0===this.opts.enableTasksQueue&&-1===this.workerNodes.findIndex((e=>!e.hasBackPressure()))}executeTask(e,t){this.beforeTaskExecutionHook(e,t),this.sendToWorker(e,t,t.transferList),this.checkAndEmitTaskExecutionEvents()}enqueueTask(e,t){const s=this.workerNodes[e].enqueueTask(t);return this.checkAndEmitTaskQueuingEvents(),s}dequeueTask(e){return this.workerNodes[e].dequeueTask()}tasksQueueSize(e){return this.workerNodes[e].tasksQueueSize()}flushTasksQueue(e){for(;this.tasksQueueSize(e)>0;)this.executeTask(e,this.dequeueTask(e));this.workerNodes[e].clearTasksQueue()}flushTasksQueues(){for(const[e]of this.workerNodes.entries())this.flushTasksQueue(e)}}class _ extends V{opts;constructor(e,t,s={}){super(e,t,s),this.opts=s}setupHook(){s.setupPrimary({...this.opts.settings,exec:this.filePath})}isMain(){return s.isPrimary}async destroyWorkerNode(e){this.flushTasksQueue(e);const t=this.workerNodes[e],s=t.worker,r=new Promise((e=>{s.on("exit",(()=>{e()}))}));s.on("disconnect",(()=>{s.kill()})),await this.sendKillMessageToWorker(e,t.info.id),s.disconnect(),await r}sendToWorker(e,t){this.workerNodes[e].worker.send(t)}sendStartupMessageToWorker(e){this.sendToWorker(e,{ready:!1,workerId:this.workerNodes[e].info.id})}registerWorkerMessageListener(e,t){this.workerNodes[e].worker.on("message",t)}createWorker(){return s.fork(this.opts.env)}get type(){return k.fixed}get worker(){return l.cluster}get busy(){return this.internalBusy()}}class j extends V{opts;constructor(e,t,s={}){super(e,t,s),this.opts=s}isMain(){return t.isMainThread}async destroyWorkerNode(e){this.flushTasksQueue(e);const t=this.workerNodes[e],s=t.worker,r=new Promise((e=>{s.on("exit",(()=>{e()}))}));await this.sendKillMessageToWorker(e,t.info.id),t.closeChannel(),await s.terminate(),await r}sendToWorker(e,t,s){this.workerNodes[e].messageChannel.port1.postMessage(t,s)}sendStartupMessageToWorker(e){const t=this.workerNodes[e],s=t.worker,r=t.messageChannel.port2;s.postMessage({ready:!1,workerId:t.info.id,port:r},[r])}registerWorkerMessageListener(e,t){this.workerNodes[e].messageChannel.port1.on("message",t)}createWorker(){return new t.Worker(this.filePath,{env:t.SHARE_ENV,...this.opts.workerOptions})}get type(){return k.fixed}get worker(){return l.thread}get busy(){return this.internalBusy()}}const H=6e4,$={killBehavior:C.SOFT,maxInactiveTime:H,killHandler:m};class G extends a.AsyncResource{isMain;mainWorker;opts;taskFunctions;lastTaskTimestamp;statistics;activeInterval;constructor(e,t,s,r,i=$){if(super(e),this.isMain=t,this.mainWorker=s,this.opts=i,null==this.isMain)throw new Error("isMain parameter is mandatory");this.checkTaskFunctions(r),this.checkWorkerOptions(this.opts),this.isMain||this.getMainWorker().on("message",this.handleReadyMessage.bind(this))}checkWorkerOptions(e){this.opts={...$,...e},delete this.opts.async}checkValidTaskFunction(e,t){if("string"!=typeof e)throw new TypeError("A taskFunctions parameter object key is not a string");if("string"==typeof e&&0===e.trim().length)throw new TypeError("A taskFunctions parameter object key is an empty string");if("function"!=typeof t)throw new TypeError("A taskFunctions parameter object value is not a function")}checkTaskFunctions(e){if(null==e)throw new Error("taskFunctions parameter is mandatory");if(this.taskFunctions=new Map,"function"==typeof e){const t=e.bind(this);this.taskFunctions.set(g,t),this.taskFunctions.set("string"==typeof e.name&&e.name.trim().length>0?e.name:"fn1",t)}else{if(!x(e))throw new TypeError("taskFunctions parameter is not a function or a plain object");{let t=!0;for(const[s,r]of Object.entries(e)){this.checkValidTaskFunction(s,r);const e=r.bind(this);t&&(this.taskFunctions.set(g,e),t=!1),this.taskFunctions.set(s,e)}if(t)throw new Error("taskFunctions parameter object is empty")}}}hasTaskFunction(e){return this.checkTaskFunctionName(e),this.taskFunctions.has(e)}addTaskFunction(e,t){if(this.checkTaskFunctionName(e),e===g)throw new Error("Cannot add a task function with the default reserved name");if("function"!=typeof t)throw new TypeError("fn parameter is not a function");try{const s=t.bind(this);return this.taskFunctions.get(e)===this.taskFunctions.get(g)&&this.taskFunctions.set(g,s),this.taskFunctions.set(e,s),this.sendTaskFunctionsListToMainWorker(),!0}catch{return!1}}removeTaskFunction(e){if(this.checkTaskFunctionName(e),e===g)throw new Error("Cannot remove the task function with the default reserved name");if(this.taskFunctions.get(e)===this.taskFunctions.get(g))throw new Error("Cannot remove the task function used as the default task function");const t=this.taskFunctions.delete(e);return this.sendTaskFunctionsListToMainWorker(),t}listTaskFunctions(){const e=[...this.taskFunctions.keys()];let t=g;for(const[e,s]of this.taskFunctions)if(e!==g&&s===this.taskFunctions.get(g)){t=e;break}return[e[e.indexOf(g)],t,...e.filter((e=>e!==g&&e!==t))]}setDefaultTaskFunction(e){if(this.checkTaskFunctionName(e),e===g)throw new Error("Cannot set the default task function reserved name as the default task function");if(!this.taskFunctions.has(e))throw new Error("Cannot set the default task function to a non-existing task function");try{return this.taskFunctions.set(g,this.taskFunctions.get(e)),!0}catch{return!1}}checkTaskFunctionName(e){if("string"!=typeof e)throw new TypeError("name parameter is not a string");if("string"==typeof e&&0===e.trim().length)throw new TypeError("name parameter is an empty string")}messageListener(e){this.checkMessageWorkerId(e),null!=e.statistics?this.statistics=e.statistics:null!=e.checkActive?e.checkActive?this.startCheckActive():this.stopCheckActive():null!=e.taskId&&null!=e.data?this.run(e):!0===e.kill&&this.handleKillMessage(e)}handleKillMessage(e){if(this.stopCheckActive(),E(this.opts.killHandler))(this.opts.killHandler?.()).then((()=>(this.sendToMainWorker({kill:"success",workerId:this.id}),null))).catch((()=>{this.sendToMainWorker({kill:"failure",workerId:this.id})})).finally((()=>{this.emitDestroy()})).catch(m);else try{this.opts.killHandler?.(),this.sendToMainWorker({kill:"success",workerId:this.id})}catch{this.sendToMainWorker({kill:"failure",workerId:this.id})}finally{this.emitDestroy()}}checkMessageWorkerId(e){if(null==e.workerId)throw new Error("Message worker id is not set");if(null!=e.workerId&&e.workerId!==this.id)throw new Error(`Message worker id ${e.workerId} does not match the worker id ${this.id}`)}startCheckActive(){this.lastTaskTimestamp=i.performance.now(),this.activeInterval=setInterval(this.checkActive.bind(this),(this.opts.maxInactiveTime??H)/2)}stopCheckActive(){null!=this.activeInterval&&(clearInterval(this.activeInterval),delete this.activeInterval)}checkActive(){i.performance.now()-this.lastTaskTimestamp>(this.opts.maxInactiveTime??H)&&this.sendToMainWorker({kill:this.opts.killBehavior,workerId:this.id})}getMainWorker(){if(null==this.mainWorker)throw new Error("Main worker not set");return this.mainWorker}sendTaskFunctionsListToMainWorker(){this.sendToMainWorker({taskFunctions:this.listTaskFunctions(),workerId:this.id})}handleError(e){return e instanceof Error?e.message:e}run(e){const{name:t,taskId:s,data:r}=e,i=this.taskFunctions.get(t??g);null!=i?E(i)?this.runInAsyncScope(this.runAsync.bind(this),this,i,e):this.runInAsyncScope(this.runSync.bind(this),this,i,e):this.sendToMainWorker({taskError:{name:t,message:`Task function '${t}' not found`,data:r},workerId:this.id,taskId:s})}runSync(e,t){const{name:s,taskId:r,data:i}=t;try{let t=this.beginTaskPerformance(s);const o=e(i);t=this.endTaskPerformance(t),this.sendToMainWorker({data:o,taskPerformance:t,workerId:this.id,taskId:r})}catch(e){const t=this.handleError(e);this.sendToMainWorker({taskError:{name:s,message:t,data:i},workerId:this.id,taskId:r})}finally{this.updateLastTaskTimestamp()}}runAsync(e,t){const{name:s,taskId:r,data:i}=t;let o=this.beginTaskPerformance(s);e(i).then((e=>(o=this.endTaskPerformance(o),this.sendToMainWorker({data:e,taskPerformance:o,workerId:this.id,taskId:r}),null))).catch((e=>{const t=this.handleError(e);this.sendToMainWorker({taskError:{name:s,message:t,data:i},workerId:this.id,taskId:r})})).finally((()=>{this.updateLastTaskTimestamp()})).catch(m)}beginTaskPerformance(e){return this.checkStatistics(),{name:e??g,timestamp:i.performance.now(),...this.statistics.elu&&{elu:i.performance.eventLoopUtilization()}}}endTaskPerformance(e){return this.checkStatistics(),{...e,...this.statistics.runTime&&{runTime:i.performance.now()-e.timestamp},...this.statistics.elu&&{elu:i.performance.eventLoopUtilization(e.elu)}}}checkStatistics(){if(null==this.statistics)throw new Error("Performance statistics computation requirements not set")}updateLastTaskTimestamp(){null!=this.activeInterval&&(this.lastTaskTimestamp=i.performance.now())}}exports.ClusterWorker=class extends G{constructor(e,t={}){super("worker-cluster-pool:poolifier",s.isPrimary,s.worker,e,t)}handleReadyMessage(e){if(e.workerId===this.id&&!1===e.ready)try{this.getMainWorker().on("message",this.messageListener.bind(this)),this.sendToMainWorker({ready:!0,taskFunctions:this.listTaskFunctions(),workerId:this.id})}catch{this.sendToMainWorker({ready:!1,taskFunctions:this.listTaskFunctions(),workerId:this.id})}}get id(){return this.getMainWorker().id}sendToMainWorker(e){this.getMainWorker().send(e)}},exports.DynamicClusterPool=class extends _{max;constructor(e,t,s,r={}){super(e,s,r),this.max=t,this.checkDynamicPoolSize(this.numberOfWorkers,this.max)}get type(){return k.dynamic}get busy(){return this.full&&this.internalBusy()}},exports.DynamicThreadPool=class extends j{max;constructor(e,t,s,r={}){super(e,s,r),this.max=t,this.checkDynamicPoolSize(this.numberOfWorkers,this.max)}get type(){return k.dynamic}get busy(){return this.full&&this.internalBusy()}},exports.FixedClusterPool=_,exports.FixedThreadPool=j,exports.KillBehaviors=C,exports.Measurements=R,exports.PoolEvents=d,exports.PoolTypes=k,exports.ThreadWorker=class extends G{port;constructor(e,s={}){super("worker-thread-pool:poolifier",t.isMainThread,t.parentPort,e,s)}handleReadyMessage(e){if(e.workerId===this.id&&!1===e.ready&&null!=e.port)try{this.port=e.port,this.port.on("message",this.messageListener.bind(this)),this.sendToMainWorker({ready:!0,taskFunctions:this.listTaskFunctions(),workerId:this.id})}catch{this.sendToMainWorker({ready:!1,taskFunctions:this.listTaskFunctions(),workerId:this.id})}}handleKillMessage(e){super.handleKillMessage(e),this.port?.unref(),this.port?.close()}get id(){return t.threadId}sendToMainWorker(e){this.port.postMessage(e)}handleError(e){return e}},exports.WorkerChoiceStrategies=I,exports.WorkerTypes=l,exports.availableParallelism=()=>{let e=1;try{e=u.availableParallelism()}catch{const t=u.cpus();Array.isArray(t)&&t.length>0&&(e=t.length)}return e};
"use strict";var e=require("node:events"),t=require("node:worker_threads"),s=require("node:cluster"),r=require("node:crypto"),i=require("node:perf_hooks"),o=require("node:fs"),n=require("node:os"),a=require("node:async_hooks");function h(e){var t=Object.create(null);return e&&Object.keys(e).forEach((function(s){if("default"!==s){var r=Object.getOwnPropertyDescriptor(e,s);Object.defineProperty(t,s,r.get?r:{enumerable:!0,get:function(){return e[s]}})}})),t.default=e,Object.freeze(t)}var u=h(n);const k=Object.freeze({fixed:"fixed",dynamic:"dynamic"});class c extends e.EventEmitter{}const d=Object.freeze({ready:"ready",busy:"busy",full:"full",destroy:"destroy",error:"error",taskError:"taskError",backPressure:"backPressure"}),l=Object.freeze({thread:"thread",cluster:"cluster"}),g="default",m=Object.freeze((()=>{})),p={retries:6,runTime:{median:!1},waitTime:{median:!1},elu:{median:!1}},w={aggregate:!1,average:!1,median:!1},y=e=>e instanceof t.Worker?l.thread:e instanceof s.Worker?l.cluster:void 0,f=e=>e instanceof t.Worker?e.threadId:e instanceof s.Worker?e.id:void 0,T=e=>Array.isArray(e)&&0===e.length?0:Array.isArray(e)&&1===e.length?e[0]:e.reduce(((e,t)=>e+t),0)/e.length,W=e=>{if(Array.isArray(e)&&0===e.length)return 0;if(Array.isArray(e)&&1===e.length)return e[0];const t=e.slice().sort(((e,t)=>e-t));return(t[t.length-1>>1]+t[t.length>>1])/2},N=(e,t=2)=>{const s=Math.pow(10,t);return Math.round(e*s*(1+Number.EPSILON))/s},x=e=>"object"==typeof e&&null!==e&&e?.constructor===Object&&"[object Object]"===Object.prototype.toString.call(e),S=(e,t)=>t===e,E=e=>"function"==typeof e&&"AsyncFunction"===e.constructor.name,v=(e,t,s)=>{t.aggregate&&(e.aggregate=(e.aggregate??0)+s,e.minimum=Math.min(s,e.minimum??1/0),e.maximum=Math.max(s,e.maximum??-1/0),(t.average||t.median)&&null!=s&&(e.history.push(s),t.average?e.average=T(e.history):null!=e.average&&delete e.average,t.median?e.median=W(e.history):null!=e.median&&delete e.median))},b=()=>r.webcrypto.getRandomValues(new Uint32Array(1))[0]/4294967296,I=Object.freeze({SOFT:"SOFT",HARD:"HARD"}),C=Object.freeze({ROUND_ROBIN:"ROUND_ROBIN",LEAST_USED:"LEAST_USED",LEAST_BUSY:"LEAST_BUSY",LEAST_ELU:"LEAST_ELU",FAIR_SHARE:"FAIR_SHARE",WEIGHTED_ROUND_ROBIN:"WEIGHTED_ROUND_ROBIN",INTERLEAVED_WEIGHTED_ROUND_ROBIN:"INTERLEAVED_WEIGHTED_ROUND_ROBIN"}),R=Object.freeze({runTime:"runTime",waitTime:"waitTime",elu:"elu"});class z{pool;opts;nextWorkerNodeKey=0;previousWorkerNodeKey=0;strategyPolicy={dynamicWorkerUsage:!1,dynamicWorkerReady:!0};taskStatisticsRequirements={runTime:w,waitTime:w,elu:w};constructor(e,t=p){this.pool=e,this.opts=t,this.opts={...p,...t},this.choose=this.choose.bind(this)}setTaskStatisticsRequirements(e){this.toggleMedianMeasurementStatisticsRequirements(this.taskStatisticsRequirements.runTime,e.runTime?.median),this.toggleMedianMeasurementStatisticsRequirements(this.taskStatisticsRequirements.waitTime,e.waitTime?.median),this.toggleMedianMeasurementStatisticsRequirements(this.taskStatisticsRequirements.elu,e.elu?.median)}toggleMedianMeasurementStatisticsRequirements(e,t){e.average&&t&&(e.average=!1,e.median=t),e.median&&!t&&(e.average=!0,e.median=t)}resetWorkerNodeKeyProperties(){this.nextWorkerNodeKey=0,this.previousWorkerNodeKey=0}setOptions(e){this.opts={...p,...e},this.setTaskStatisticsRequirements(this.opts)}isWorkerNodeReady(e){return this.pool.workerNodes[e].info.ready}hasWorkerNodeBackPressure(e){return this.pool.hasWorkerNodeBackPressure(e)}isWorkerNodeEligible(e){return this.isWorkerNodeReady(e)&&!this.hasWorkerNodeBackPressure(e)}getWorkerTaskRunTime(e){return this.taskStatisticsRequirements.runTime.median?this.pool.workerNodes[e].usage.runTime?.median??0:this.pool.workerNodes[e].usage.runTime?.average??0}getWorkerTaskWaitTime(e){return this.taskStatisticsRequirements.waitTime.median?this.pool.workerNodes[e].usage.waitTime?.median??0:this.pool.workerNodes[e].usage.waitTime?.average??0}getWorkerTaskElu(e){return this.taskStatisticsRequirements.elu.median?this.pool.workerNodes[e].usage.elu.active?.median??0:this.pool.workerNodes[e].usage.elu.active?.average??0}checkNextWorkerNodeEligibility(e){this.isWorkerNodeEligible(this.nextWorkerNodeKey)||(this.nextWorkerNodeKey=void 0,this.previousWorkerNodeKey=e??this.previousWorkerNodeKey)}computeDefaultWorkerWeight(){let e=0;for(const t of n.cpus()){const s=t.speed.toString().length-1;e+=1/(t.speed/Math.pow(10,s))*Math.pow(10,s)}return Math.round(e/n.cpus().length)}}class O extends z{taskStatisticsRequirements={runTime:{aggregate:!0,average:!0,median:!1},waitTime:w,elu:{aggregate:!0,average:!0,median:!1}};workersVirtualTaskEndTimestamp=[];constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return this.workersVirtualTaskEndTimestamp=[],!0}update(e){return this.computeWorkerVirtualTaskEndTimestamp(e),!0}choose(){return this.nextWorkerNodeKey=this.fairShareNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(e){return this.workersVirtualTaskEndTimestamp.splice(e,1),!0}fairShareNextWorkerNodeKey(){let e,t=1/0;for(const[s]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;null==this.workersVirtualTaskEndTimestamp[s]&&this.computeWorkerVirtualTaskEndTimestamp(s);const r=this.workersVirtualTaskEndTimestamp[s];r<t&&(t=r,e=s)}return e}computeWorkerVirtualTaskEndTimestamp(e){this.workersVirtualTaskEndTimestamp[e]=this.getWorkerVirtualTaskEndTimestamp(e,this.getWorkerVirtualTaskStartTimestamp(e))}getWorkerVirtualTaskEndTimestamp(e,t){return t+(this.opts.measurement===R.elu?this.getWorkerTaskElu(e):this.getWorkerTaskRunTime(e))}getWorkerVirtualTaskStartTimestamp(e){return Math.max(performance.now(),this.workersVirtualTaskEndTimestamp[e]??-1/0)}}class F extends z{taskStatisticsRequirements={runTime:{aggregate:!0,average:!0,median:!1},waitTime:w,elu:w};roundId=0;defaultWorkerWeight;roundWeights;workerNodeId=0;workerVirtualTaskRunTime=0;constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts),this.defaultWorkerWeight=this.computeDefaultWorkerWeight(),this.roundWeights=this.getRoundWeights()}reset(){return this.resetWorkerNodeKeyProperties(),this.roundId=0,this.workerNodeId=0,this.workerVirtualTaskRunTime=0,!0}update(){return!0}choose(){for(let e=this.roundId;e<this.roundWeights.length;e++){this.roundId=e;for(let t=this.workerNodeId;t<this.pool.workerNodes.length;t++){if(this.workerNodeId=t,!this.isWorkerNodeEligible(t))continue;this.workerNodeId!==this.nextWorkerNodeKey&&0!==this.workerVirtualTaskRunTime&&(this.workerVirtualTaskRunTime=0);const s=this.opts.weights?.[t]??this.defaultWorkerWeight;if(s>=this.roundWeights[e]&&this.workerVirtualTaskRunTime<s)return this.workerVirtualTaskRunTime=this.workerVirtualTaskRunTime+this.getWorkerTaskRunTime(t),this.previousWorkerNodeKey=this.nextWorkerNodeKey??this.previousWorkerNodeKey,this.nextWorkerNodeKey=t,this.nextWorkerNodeKey}}this.interleavedWeightedRoundRobinNextWorkerNodeId()}interleavedWeightedRoundRobinNextWorkerNodeId(){this.roundId===this.roundWeights.length-1&&this.workerNodeId===this.pool.workerNodes.length-1?(this.roundId=0,this.workerNodeId=0):this.workerNodeId===this.pool.workerNodes.length-1?(this.roundId=this.roundId+1,this.workerNodeId=0):this.workerNodeId=this.workerNodeId+1}remove(e){return this.nextWorkerNodeKey===e&&(0===this.pool.workerNodes.length?(this.roundId=0,this.workerNodeId=0,this.nextWorkerNodeKey=0):this.nextWorkerNodeKey>this.pool.workerNodes.length-1&&(this.workerNodeId===this.nextWorkerNodeKey&&(this.workerNodeId=this.pool.workerNodes.length-1),this.nextWorkerNodeKey=this.pool.workerNodes.length-1),this.workerVirtualTaskRunTime=0),!0}setOptions(e){super.setOptions(e),this.roundWeights=this.getRoundWeights()}getRoundWeights(){return null==this.opts.weights?[this.defaultWorkerWeight]:[...new Set(Object.values(this.opts.weights).slice().sort(((e,t)=>e-t)))]}}class Q extends z{taskStatisticsRequirements={runTime:{aggregate:!0,average:!1,median:!1},waitTime:{aggregate:!0,average:!1,median:!1},elu:w};constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return!0}update(){return!0}choose(){return this.nextWorkerNodeKey=this.leastBusyNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(){return!0}leastBusyNextWorkerNodeKey(){let e,t=1/0;for(const[s,r]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;const i=(r.usage.runTime?.aggregate??0)+(r.usage.waitTime?.aggregate??0);if(0===i){e=s;break}i<t&&(t=i,e=s)}return e}}class M extends z{constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return!0}update(){return!0}choose(){return this.nextWorkerNodeKey=this.leastUsedNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(){return!0}leastUsedNextWorkerNodeKey(){let e,t=1/0;for(const[s,r]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;const i=r.usage.tasks,o=i.executed+i.executing+i.queued;if(0===o){e=s;break}o<t&&(t=o,e=s)}return e}}class q extends z{taskStatisticsRequirements={runTime:w,waitTime:w,elu:{aggregate:!0,average:!1,median:!1}};constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return!0}update(){return!0}choose(){return this.nextWorkerNodeKey=this.leastEluNextWorkerNodeKey(),this.nextWorkerNodeKey}remove(){return!0}leastEluNextWorkerNodeKey(){let e,t=1/0;for(const[s,r]of this.pool.workerNodes.entries()){if(!this.isWorkerNodeEligible(s))continue;const i=r.usage,o=i.elu?.active?.aggregate??0;if(0===o){e=s;break}o<t&&(t=o,e=s)}return e}}class P extends z{constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts)}reset(){return this.resetWorkerNodeKeyProperties(),!0}update(){return!0}choose(){const e=this.nextWorkerNodeKey;return this.roundRobinNextWorkerNodeKey(),this.checkNextWorkerNodeEligibility(e),e}remove(e){return this.nextWorkerNodeKey===e&&(0===this.pool.workerNodes.length?this.nextWorkerNodeKey=0:this.nextWorkerNodeKey>this.pool.workerNodes.length-1&&(this.nextWorkerNodeKey=this.pool.workerNodes.length-1)),!0}roundRobinNextWorkerNodeKey(){return this.nextWorkerNodeKey=this.nextWorkerNodeKey===this.pool.workerNodes.length-1?0:(this.nextWorkerNodeKey??this.previousWorkerNodeKey)+1,this.nextWorkerNodeKey}}class K extends z{taskStatisticsRequirements={runTime:{aggregate:!0,average:!0,median:!1},waitTime:w,elu:w};defaultWorkerWeight;workerVirtualTaskRunTime=0;constructor(e,t=p){super(e,t),this.setTaskStatisticsRequirements(this.opts),this.defaultWorkerWeight=this.computeDefaultWorkerWeight()}reset(){return this.resetWorkerNodeKeyProperties(),this.workerVirtualTaskRunTime=0,!0}update(){return!0}choose(){const e=this.nextWorkerNodeKey;return this.weightedRoundRobinNextWorkerNodeKey(),this.checkNextWorkerNodeEligibility(e),e}remove(e){return this.nextWorkerNodeKey===e&&(0===this.pool.workerNodes.length?this.nextWorkerNodeKey=0:this.nextWorkerNodeKey>this.pool.workerNodes.length-1&&(this.nextWorkerNodeKey=this.pool.workerNodes.length-1),this.workerVirtualTaskRunTime=0),!0}weightedRoundRobinNextWorkerNodeKey(){const e=this.opts.weights?.[this.nextWorkerNodeKey??this.previousWorkerNodeKey]??this.defaultWorkerWeight;return this.workerVirtualTaskRunTime<e?this.workerVirtualTaskRunTime=this.workerVirtualTaskRunTime+this.getWorkerTaskRunTime(this.nextWorkerNodeKey??this.previousWorkerNodeKey):(this.nextWorkerNodeKey=this.nextWorkerNodeKey===this.pool.workerNodes.length-1?0:(this.nextWorkerNodeKey??this.previousWorkerNodeKey)+1,this.workerVirtualTaskRunTime=0),this.nextWorkerNodeKey}}class A{workerChoiceStrategy;opts;workerChoiceStrategies;retriesCount=0;constructor(e,t=C.ROUND_ROBIN,s=p){this.workerChoiceStrategy=t,this.opts=s,this.opts={...p,...s},this.execute=this.execute.bind(this),this.workerChoiceStrategies=new Map([[C.ROUND_ROBIN,new(P.bind(this))(e,s)],[C.LEAST_USED,new(M.bind(this))(e,s)],[C.LEAST_BUSY,new(Q.bind(this))(e,s)],[C.LEAST_ELU,new(q.bind(this))(e,s)],[C.FAIR_SHARE,new(O.bind(this))(e,s)],[C.WEIGHTED_ROUND_ROBIN,new(K.bind(this))(e,s)],[C.INTERLEAVED_WEIGHTED_ROUND_ROBIN,new(F.bind(this))(e,s)]])}getStrategyPolicy(){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).strategyPolicy}getTaskStatisticsRequirements(){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).taskStatisticsRequirements}setWorkerChoiceStrategy(e){this.workerChoiceStrategy!==e&&(this.workerChoiceStrategy=e),this.workerChoiceStrategies.get(this.workerChoiceStrategy)?.reset()}update(e){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).update(e)}execute(){const e=this.workerChoiceStrategies.get(this.workerChoiceStrategy).choose();if(null==e&&this.retriesCount<this.opts.retries)return this.retriesCount++,this.execute();if(null==e)throw new Error(`Worker node key chosen is null or undefined after ${this.retriesCount} retries`);return this.retriesCount=0,e}remove(e){return this.workerChoiceStrategies.get(this.workerChoiceStrategy).remove(e)}setOptions(e){this.opts={...p,...e};for(const t of this.workerChoiceStrategies.values())t.setOptions(e)}}class U extends Array{size;constructor(e=1024,...t){super(),this.checkSize(e),this.size=e,arguments.length>1&&this.push(...t)}push(...e){const t=super.push(...e);return t>this.size&&super.splice(0,t-this.size),this.length}unshift(...e){return super.unshift(...e)>this.size&&super.splice(this.size,e.length),this.length}concat(...e){const t=super.concat(e);return t.size=this.size,t.length>t.size&&t.splice(0,t.length-t.size),t}splice(e,t,...s){let r=[];if(arguments.length>=3&&null!=t){if(r=super.splice(e,t,...s),this.length>this.size){const e=super.splice(0,this.length-this.size);r=new U(r.length+e.length,...r,...e)}}else r=2===arguments.length?super.splice(e,t):super.splice(e);return r}resize(e){if(this.checkSize(e),0===e)this.length=0;else if(e<this.size)for(let t=e;t<this.size;t++)super.pop();this.size=e}empty(){return 0===this.length}full(){return this.length===this.size}checkSize(e){if(!Number.isSafeInteger(e))throw new TypeError(`Invalid circular array size: ${e} is not a safe integer`);if(e<0)throw new RangeError(`Invalid circular array size: ${e} < 0`)}}class B{data;next;prev;constructor(e){this.data=e}}class D{head;tail;size;maxSize;constructor(){this.clear()}push(e){const t=new B(e);return null==this.tail?this.head=this.tail=t:(t.prev=this.tail,this.tail=this.tail.next=t),this.incrementSize()}unshift(e){const t=new B(e);return null==this.head?this.head=this.tail=t:(t.next=this.head,this.head=this.head.prev=t),this.incrementSize()}pop(){if(null==this.head)return;const e=this.tail;return this.tail=this.tail.prev,null==this.tail?this.head=void 0:this.tail.next=void 0,--this.size,e?.data}shift(){if(null==this.head)return;const e=this.head;return this.head=this.head.next,null==this.head?this.tail=void 0:this.head.prev=void 0,--this.size,e?.data}peekFirst(){return this.head?.data}peekLast(){return this.tail?.data}clear(){this.head=void 0,this.tail=void 0,this.size=0,this.maxSize=0}[Symbol.iterator](){let e=this.head;return{next:()=>{if(null==e)return{value:void 0,done:!0};const t={value:e.data,done:!1};return e=e.next,t}}}backward(){return{[Symbol.iterator]:()=>{let e=this.tail;return{next:()=>{if(null==e)return{value:void 0,done:!0};const t={value:e.data,done:!1};return e=e.prev,t}}}}}incrementSize(){return++this.size,this.size>this.maxSize&&(this.maxSize=this.size),this.size}}class V{worker;info;usage;messageChannel;tasksQueueBackPressureSize;onBackPressure;onEmptyQueue;tasksQueue;onEmptyQueueCount;taskFunctionsUsage;constructor(e,s){this.checkWorkerNodeArguments(e,s),this.worker=e,this.info=this.initWorkerInfo(e),this.usage=this.initWorkerUsage(),this.info.type===l.thread&&(this.messageChannel=new t.MessageChannel),this.tasksQueueBackPressureSize=s,this.tasksQueue=new D,this.onEmptyQueueCount=0,this.taskFunctionsUsage=new Map}tasksQueueSize(){return this.tasksQueue.size}enqueueTask(e){const t=this.tasksQueue.push(e);return null!=this.onBackPressure&&this.hasBackPressure()&&this.onBackPressure(this.info.id),t}unshiftTask(e){const t=this.tasksQueue.unshift(e);return null!=this.onBackPressure&&this.hasBackPressure()&&this.onBackPressure(this.info.id),t}dequeueTask(){const e=this.tasksQueue.shift();return null!=this.onEmptyQueue&&0===this.tasksQueue.size&&this.startOnEmptyQueue().catch(m),e}popTask(){const e=this.tasksQueue.pop();return null!=this.onEmptyQueue&&0===this.tasksQueue.size&&this.startOnEmptyQueue().catch(m),e}clearTasksQueue(){this.tasksQueue.clear()}hasBackPressure(){return this.tasksQueue.size>=this.tasksQueueBackPressureSize}resetUsage(){this.usage=this.initWorkerUsage(),this.taskFunctionsUsage.clear()}closeChannel(){null!=this.messageChannel&&(this.messageChannel?.port1.unref(),this.messageChannel?.port2.unref(),this.messageChannel?.port1.close(),this.messageChannel?.port2.close(),delete this.messageChannel)}getTaskFunctionWorkerUsage(e){if(!Array.isArray(this.info.taskFunctions))throw new Error(`Cannot get task function worker usage for task function name '${e}' when task function names list is not yet defined`);if(Array.isArray(this.info.taskFunctions)&&this.info.taskFunctions.length<3)throw new Error(`Cannot get task function worker usage for task function name '${e}' when task function names list has less than 3 elements`);return e===g&&(e=this.info.taskFunctions[1]),this.taskFunctionsUsage.has(e)||this.taskFunctionsUsage.set(e,this.initTaskFunctionWorkerUsage(e)),this.taskFunctionsUsage.get(e)}async startOnEmptyQueue(){this.onEmptyQueueCount>0&&(this.usage.tasks.executing>0||this.tasksQueue.size>0)?this.onEmptyQueueCount=0:(this.onEmptyQueue(this.info.id),++this.onEmptyQueueCount,await(async e=>{await new Promise((t=>{setTimeout(t,e)}))})(((e=0,t=100)=>{const s=Math.pow(2,e)*t;return s+.2*s*b()})(this.onEmptyQueueCount)),await this.startOnEmptyQueue())}initWorkerInfo(e){return{id:f(e),type:y(e),dynamic:!1,ready:!1}}initWorkerUsage(){const e=()=>this.tasksQueue.size,t=()=>this.tasksQueue.maxSize;return{tasks:{executed:0,executing:0,get queued(){return e()},get maxQueued(){return t()},stolen:0,failed:0},runTime:{history:new U},waitTime:{history:new U},elu:{idle:{history:new U},active:{history:new U}}}}initTaskFunctionWorkerUsage(e){const t=()=>{let t=0;for(const s of this.tasksQueue)(s.name===g&&e===this.info.taskFunctions[1]||s.name!==g&&e===s.name)&&++t;return t};return{tasks:{executed:0,executing:0,get queued(){return t()},stolen:0,failed:0},runTime:{history:new U},waitTime:{history:new U},elu:{idle:{history:new U},active:{history:new U}}}}checkWorkerNodeArguments(e,t){if(null==e)throw new TypeError("Cannot construct a worker node without a worker");if(null==t)throw new TypeError("Cannot construct a worker node without a tasks queue back pressure size");if(!Number.isSafeInteger(t))throw new TypeError("Cannot construct a worker node with a tasks queue back pressure size that is not an integer");if(t<=0)throw new RangeError("Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer")}}class L{numberOfWorkers;filePath;opts;workerNodes=[];emitter;promiseResponseMap=new Map;workerChoiceStrategyContext;max;starting;started;startTimestamp;constructor(e,t,s){if(this.numberOfWorkers=e,this.filePath=t,this.opts=s,!this.isMain())throw new Error("Cannot start a pool from a worker with the same type as the pool");this.checkNumberOfWorkers(this.numberOfWorkers),this.checkFilePath(this.filePath),this.checkPoolOptions(this.opts),this.chooseWorkerNode=this.chooseWorkerNode.bind(this),this.executeTask=this.executeTask.bind(this),this.enqueueTask=this.enqueueTask.bind(this),!0===this.opts.enableEvents&&(this.emitter=new c),this.workerChoiceStrategyContext=new A(this,this.opts.workerChoiceStrategy,this.opts.workerChoiceStrategyOptions),this.setupHook(),this.starting=!0,this.startPool(),this.starting=!1,this.started=!0,this.startTimestamp=i.performance.now()}checkFilePath(e){if(null==e||"string"!=typeof e||"string"==typeof e&&0===e.trim().length)throw new Error("Please specify a file with a worker implementation");if(!o.existsSync(e))throw new Error(`Cannot find the worker file '${e}'`)}checkNumberOfWorkers(e){if(null==e)throw new Error("Cannot instantiate a pool without specifying the number of workers");if(!Number.isSafeInteger(e))throw new TypeError("Cannot instantiate a pool with a non safe integer number of workers");if(e<0)throw new RangeError("Cannot instantiate a pool with a negative number of workers");if(this.type===k.fixed&&0===e)throw new RangeError("Cannot instantiate a fixed pool with zero worker")}checkDynamicPoolSize(e,t){if(this.type===k.dynamic){if(null==t)throw new TypeError("Cannot instantiate a dynamic pool without specifying the maximum pool size");if(!Number.isSafeInteger(t))throw new TypeError("Cannot instantiate a dynamic pool with a non safe integer maximum pool size");if(e>t)throw new RangeError("Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size");if(0===t)throw new RangeError("Cannot instantiate a dynamic pool with a maximum pool size equal to zero");if(e===t)throw new RangeError("Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead")}}checkPoolOptions(e){if(!x(e))throw new TypeError("Invalid pool options: must be a plain object");this.opts.workerChoiceStrategy=e.workerChoiceStrategy??C.ROUND_ROBIN,this.checkValidWorkerChoiceStrategy(this.opts.workerChoiceStrategy),this.opts.workerChoiceStrategyOptions={...p,...e.workerChoiceStrategyOptions},this.checkValidWorkerChoiceStrategyOptions(this.opts.workerChoiceStrategyOptions),this.opts.restartWorkerOnError=e.restartWorkerOnError??!0,this.opts.enableEvents=e.enableEvents??!0,this.opts.enableTasksQueue=e.enableTasksQueue??!1,this.opts.enableTasksQueue&&(this.checkValidTasksQueueOptions(e.tasksQueueOptions),this.opts.tasksQueueOptions=this.buildTasksQueueOptions(e.tasksQueueOptions))}checkValidWorkerChoiceStrategy(e){if(!Object.values(C).includes(e))throw new Error(`Invalid worker choice strategy '${e}'`)}checkValidWorkerChoiceStrategyOptions(e){if(!x(e))throw new TypeError("Invalid worker choice strategy options: must be a plain object");if(null!=e.retries&&!Number.isSafeInteger(e.retries))throw new TypeError("Invalid worker choice strategy options: retries must be an integer");if(null!=e.retries&&e.retries<0)throw new RangeError(`Invalid worker choice strategy options: retries '${e.retries}' must be greater or equal than zero`);if(null!=e.weights&&Object.keys(e.weights).length!==this.maxSize)throw new Error("Invalid worker choice strategy options: must have a weight for each worker node");if(null!=e.measurement&&!Object.values(R).includes(e.measurement))throw new Error(`Invalid worker choice strategy options: invalid measurement '${e.measurement}'`)}checkValidTasksQueueOptions(e){if(null!=e&&!x(e))throw new TypeError("Invalid tasks queue options: must be a plain object");if(null!=e?.concurrency&&!Number.isSafeInteger(e.concurrency))throw new TypeError("Invalid worker node tasks concurrency: must be an integer");if(null!=e?.concurrency&&e.concurrency<=0)throw new RangeError(`Invalid worker node tasks concurrency: ${e.concurrency} is a negative integer or zero`);if(null!=e?.queueMaxSize)throw new Error("Invalid tasks queue options: queueMaxSize is deprecated, please use size instead");if(null!=e?.size&&!Number.isSafeInteger(e.size))throw new TypeError("Invalid worker node tasks queue size: must be an integer");if(null!=e?.size&&e.size<=0)throw new RangeError(`Invalid worker node tasks queue size: ${e.size} is a negative integer or zero`)}startPool(){for(;this.workerNodes.reduce(((e,t)=>t.info.dynamic?e:e+1),0)<this.numberOfWorkers;)this.createAndSetupWorkerNode()}get info(){return{version:"2.6.38",type:this.type,worker:this.worker,ready:this.ready,strategy:this.opts.workerChoiceStrategy,minSize:this.minSize,maxSize:this.maxSize,...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.aggregate&&this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.aggregate&&{utilization:N(this.utilization)},workerNodes:this.workerNodes.length,idleWorkerNodes:this.workerNodes.reduce(((e,t)=>0===t.usage.tasks.executing?e+1:e),0),busyWorkerNodes:this.workerNodes.reduce(((e,t)=>t.usage.tasks.executing>0?e+1:e),0),executedTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.executed),0),executingTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.executing),0),...!0===this.opts.enableTasksQueue&&{queuedTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.queued),0)},...!0===this.opts.enableTasksQueue&&{maxQueuedTasks:this.workerNodes.reduce(((e,t)=>e+(t.usage.tasks?.maxQueued??0)),0)},...!0===this.opts.enableTasksQueue&&{backPressure:this.hasBackPressure()},...!0===this.opts.enableTasksQueue&&{stolenTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.stolen),0)},failedTasks:this.workerNodes.reduce(((e,t)=>e+t.usage.tasks.failed),0),...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.aggregate&&{runTime:{minimum:N(Math.min(...this.workerNodes.map((e=>e.usage.runTime?.minimum??1/0)))),maximum:N(Math.max(...this.workerNodes.map((e=>e.usage.runTime?.maximum??-1/0)))),...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.average&&{average:N(T(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.runTime.history)),[])))},...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.median&&{median:N(W(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.runTime.history)),[])))}}},...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.aggregate&&{waitTime:{minimum:N(Math.min(...this.workerNodes.map((e=>e.usage.waitTime?.minimum??1/0)))),maximum:N(Math.max(...this.workerNodes.map((e=>e.usage.waitTime?.maximum??-1/0)))),...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.average&&{average:N(T(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.waitTime.history)),[])))},...this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime.median&&{median:N(W(this.workerNodes.reduce(((e,t)=>e.concat(t.usage.waitTime.history)),[])))}}}}}get ready(){return this.workerNodes.reduce(((e,t)=>!t.info.dynamic&&t.info.ready?e+1:e),0)>=this.minSize}get utilization(){const e=(i.performance.now()-this.startTimestamp)*this.maxSize;return(this.workerNodes.reduce(((e,t)=>e+(t.usage.runTime?.aggregate??0)),0)+this.workerNodes.reduce(((e,t)=>e+(t.usage.waitTime?.aggregate??0)),0))/e}get minSize(){return this.numberOfWorkers}get maxSize(){return this.max??this.numberOfWorkers}checkMessageWorkerId(e){if(null==e.workerId)throw new Error("Worker message received without worker id");if(null!=e.workerId&&-1===this.getWorkerNodeKeyByWorkerId(e.workerId))throw new Error(`Worker message received from unknown worker '${e.workerId}'`)}getWorkerNodeKeyByWorker(e){return this.workerNodes.findIndex((t=>t.worker===e))}getWorkerNodeKeyByWorkerId(e){return this.workerNodes.findIndex((t=>t.info.id===e))}setWorkerChoiceStrategy(e,t){this.checkValidWorkerChoiceStrategy(e),this.opts.workerChoiceStrategy=e,this.workerChoiceStrategyContext.setWorkerChoiceStrategy(this.opts.workerChoiceStrategy),null!=t&&this.setWorkerChoiceStrategyOptions(t);for(const[e,t]of this.workerNodes.entries())t.resetUsage(),this.sendStatisticsMessageToWorker(e)}setWorkerChoiceStrategyOptions(e){this.checkValidWorkerChoiceStrategyOptions(e),this.opts.workerChoiceStrategyOptions={...p,...e},this.workerChoiceStrategyContext.setOptions(this.opts.workerChoiceStrategyOptions)}enableTasksQueue(e,t){!0!==this.opts.enableTasksQueue||e||this.flushTasksQueues(),this.opts.enableTasksQueue=e,this.setTasksQueueOptions(t)}setTasksQueueOptions(e){!0===this.opts.enableTasksQueue?(this.checkValidTasksQueueOptions(e),this.opts.tasksQueueOptions=this.buildTasksQueueOptions(e),this.setTasksQueueSize(this.opts.tasksQueueOptions.size)):null!=this.opts.tasksQueueOptions&&delete this.opts.tasksQueueOptions}setTasksQueueSize(e){for(const t of this.workerNodes)t.tasksQueueBackPressureSize=e}buildTasksQueueOptions(e){return{size:Math.pow(this.maxSize,2),concurrency:1,...e}}get full(){return this.workerNodes.length>=this.maxSize}internalBusy(){return!0===this.opts.enableTasksQueue?-1===this.workerNodes.findIndex((e=>e.info.ready&&e.usage.tasks.executing<this.opts.tasksQueueOptions?.concurrency)):-1===this.workerNodes.findIndex((e=>e.info.ready&&0===e.usage.tasks.executing))}listTaskFunctions(){for(const e of this.workerNodes)if(Array.isArray(e.info.taskFunctions)&&e.info.taskFunctions.length>0)return e.info.taskFunctions;return[]}shallExecuteTask(e){return 0===this.tasksQueueSize(e)&&this.workerNodes[e].usage.tasks.executing<this.opts.tasksQueueOptions?.concurrency}async execute(e,t,s){return await new Promise(((o,n)=>{if(!this.started)return void n(new Error("Cannot execute a task on destroyed pool"));if(null!=t&&"string"!=typeof t)return void n(new TypeError("name argument must be a string"));if(null!=t&&"string"==typeof t&&0===t.trim().length)return void n(new TypeError("name argument must not be an empty string"));if(null!=s&&!Array.isArray(s))return void n(new TypeError("transferList argument must be an array"));const a=i.performance.now(),h=this.chooseWorkerNode(),u=this.getWorkerInfo(h),k={name:t??g,data:e??{},transferList:s,timestamp:a,workerId:u.id,taskId:r.randomUUID()};this.promiseResponseMap.set(k.taskId,{resolve:o,reject:n,workerNodeKey:h}),!1===this.opts.enableTasksQueue||!0===this.opts.enableTasksQueue&&this.shallExecuteTask(h)?this.executeTask(h,k):this.enqueueTask(h,k)}))}async destroy(){await Promise.all(this.workerNodes.map((async(e,t)=>{await this.destroyWorkerNode(t)}))),this.emitter?.emit(d.destroy,this.info),this.started=!1}async sendKillMessageToWorker(e,t){await new Promise(((s,r)=>{this.registerWorkerMessageListener(e,(e=>{"success"===e.kill?s():"failure"===e.kill&&r(new Error(`Worker ${t} kill message handling failed`))})),this.sendToWorker(e,{kill:!0,workerId:t})}))}setupHook(){}beforeTaskExecutionHook(e,t){if(null!=this.workerNodes[e]?.usage){const s=this.workerNodes[e].usage;++s.tasks.executing,this.updateWaitTimeWorkerUsage(s,t)}if(this.shallUpdateTaskFunctionWorkerUsage(e)&&null!=this.workerNodes[e].getTaskFunctionWorkerUsage(t.name)){const s=this.workerNodes[e].getTaskFunctionWorkerUsage(t.name);++s.tasks.executing,this.updateWaitTimeWorkerUsage(s,t)}}afterTaskExecutionHook(e,t){if(null!=this.workerNodes[e]?.usage){const s=this.workerNodes[e].usage;this.updateTaskStatisticsWorkerUsage(s,t),this.updateRunTimeWorkerUsage(s,t),this.updateEluWorkerUsage(s,t)}if(this.shallUpdateTaskFunctionWorkerUsage(e)&&null!=this.workerNodes[e].getTaskFunctionWorkerUsage(t.taskPerformance?.name)){const s=this.workerNodes[e].getTaskFunctionWorkerUsage(t.taskPerformance?.name);this.updateTaskStatisticsWorkerUsage(s,t),this.updateRunTimeWorkerUsage(s,t),this.updateEluWorkerUsage(s,t)}}shallUpdateTaskFunctionWorkerUsage(e){const t=this.getWorkerInfo(e);return null!=t&&Array.isArray(t.taskFunctions)&&t.taskFunctions.length>2}updateTaskStatisticsWorkerUsage(e,t){const s=e.tasks;null!=s.executing&&s.executing>0&&--s.executing,null==t.taskError?++s.executed:++s.failed}updateRunTimeWorkerUsage(e,t){null==t.taskError&&v(e.runTime,this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime,t.taskPerformance?.runTime??0)}updateWaitTimeWorkerUsage(e,t){const s=i.performance.now(),r=s-(t.timestamp??s);v(e.waitTime,this.workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime,r)}updateEluWorkerUsage(e,t){if(null!=t.taskError)return;const s=this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu;v(e.elu.active,s,t.taskPerformance?.elu?.active??0),v(e.elu.idle,s,t.taskPerformance?.elu?.idle??0),s.aggregate&&null!=t.taskPerformance?.elu&&(null!=e.elu.utilization?e.elu.utilization=(e.elu.utilization+t.taskPerformance.elu.utilization)/2:e.elu.utilization=t.taskPerformance.elu.utilization)}chooseWorkerNode(){if(this.shallCreateDynamicWorker()){const e=this.createAndSetupDynamicWorkerNode();if(this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerUsage)return e}return this.workerChoiceStrategyContext.execute()}shallCreateDynamicWorker(){return this.type===k.dynamic&&!this.full&&this.internalBusy()}createAndSetupWorkerNode(){const e=this.createWorker();e.on("online",this.opts.onlineHandler??m),e.on("message",this.opts.messageHandler??m),e.on("error",this.opts.errorHandler??m),e.on("error",(t=>{const s=this.getWorkerNodeKeyByWorker(e),r=this.getWorkerInfo(s);r.ready=!1,this.workerNodes[s].closeChannel(),this.emitter?.emit(d.error,t),!0===this.opts.restartWorkerOnError&&!this.starting&&this.started&&(r.dynamic?this.createAndSetupDynamicWorkerNode():this.createAndSetupWorkerNode()),!0===this.opts.enableTasksQueue&&this.redistributeQueuedTasks(s)})),e.on("exit",this.opts.exitHandler??m),e.once("exit",(()=>{this.removeWorkerNode(e)}));const t=this.addWorkerNode(e);return this.afterWorkerNodeSetup(t),t}createAndSetupDynamicWorkerNode(){const e=this.createAndSetupWorkerNode();this.registerWorkerMessageListener(e,(e=>{const t=this.getWorkerNodeKeyByWorkerId(e.workerId),s=this.workerNodes[t].usage;(S(I.HARD,e.kill)||S(I.SOFT,e.kill)&&(!1===this.opts.enableTasksQueue&&0===s.tasks.executing||!0===this.opts.enableTasksQueue&&0===s.tasks.executing&&0===this.tasksQueueSize(t)))&&this.destroyWorkerNode(t).catch((e=>{this.emitter?.emit(d.error,e)}))}));const t=this.getWorkerInfo(e);return this.sendToWorker(e,{checkActive:!0,workerId:t.id}),t.dynamic=!0,(this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerReady||this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerUsage)&&(t.ready=!0),this.checkAndEmitDynamicWorkerCreationEvents(),e}afterWorkerNodeSetup(e){this.registerWorkerMessageListener(e,this.workerListener()),this.sendStartupMessageToWorker(e),this.sendStatisticsMessageToWorker(e),!0===this.opts.enableTasksQueue&&(this.workerNodes[e].onEmptyQueue=this.taskStealingOnEmptyQueue.bind(this),this.workerNodes[e].onBackPressure=this.tasksStealingOnBackPressure.bind(this))}sendStatisticsMessageToWorker(e){this.sendToWorker(e,{statistics:{runTime:this.workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime.aggregate,elu:this.workerChoiceStrategyContext.getTaskStatisticsRequirements().elu.aggregate},workerId:this.getWorkerInfo(e).id})}redistributeQueuedTasks(e){for(;this.tasksQueueSize(e)>0;){let t,s=1/0;for(const[r,i]of this.workerNodes.entries())if(i.info.ready&&r!==e){if(0===i.usage.tasks.queued){t=r;break}i.usage.tasks.queued<s&&(s=i.usage.tasks.queued,t=r)}if(null!=t){const s=this.workerNodes[t],r={...this.dequeueTask(e),workerId:s.info.id};this.shallExecuteTask(t)?this.executeTask(t,r):this.enqueueTask(t,r)}}}updateTaskStolenStatisticsWorkerUsage(e,t){const s=this.workerNodes[e];if(null!=s?.usage&&++s.usage.tasks.stolen,this.shallUpdateTaskFunctionWorkerUsage(e)&&null!=s.getTaskFunctionWorkerUsage(t)){++s.getTaskFunctionWorkerUsage(t).tasks.stolen}}taskStealingOnEmptyQueue(e){const t=this.getWorkerNodeKeyByWorkerId(e),s=this.workerNodes[t],r=this.workerNodes.slice().sort(((e,t)=>t.usage.tasks.queued-e.usage.tasks.queued));for(const i of r){if(0===i.usage.tasks.queued)break;if(i.info.ready&&i.info.id!==e&&i.usage.tasks.queued>0){const e={...i.popTask(),workerId:s.info.id};this.shallExecuteTask(t)?this.executeTask(t,e):this.enqueueTask(t,e),this.updateTaskStolenStatisticsWorkerUsage(t,e.name);break}}}tasksStealingOnBackPressure(e){if(this.opts.tasksQueueOptions?.size<=1)return;const t=this.workerNodes[this.getWorkerNodeKeyByWorkerId(e)],s=this.workerNodes.slice().sort(((e,t)=>e.usage.tasks.queued-t.usage.tasks.queued));for(const[r,i]of s.entries())if(t.usage.tasks.queued>0&&i.info.ready&&i.info.id!==e&&i.usage.tasks.queued<this.opts.tasksQueueOptions?.size-1){const e={...t.popTask(),workerId:i.info.id};this.shallExecuteTask(r)?this.executeTask(r,e):this.enqueueTask(r,e),this.updateTaskStolenStatisticsWorkerUsage(r,e.name)}}workerListener(){return e=>{this.checkMessageWorkerId(e),null!=e.ready&&null!=e.taskFunctions?this.handleWorkerReadyResponse(e):null!=e.taskId?this.handleTaskExecutionResponse(e):null!=e.taskFunctions&&(this.getWorkerInfo(this.getWorkerNodeKeyByWorkerId(e.workerId)).taskFunctions=e.taskFunctions)}}handleWorkerReadyResponse(e){if(!1===e.ready)throw new Error(`Worker ${e.workerId} failed to initialize`);const t=this.getWorkerInfo(this.getWorkerNodeKeyByWorkerId(e.workerId));t.ready=e.ready,t.taskFunctions=e.taskFunctions,null!=this.emitter&&this.ready&&this.emitter.emit(d.ready,this.info)}handleTaskExecutionResponse(e){const{taskId:t,taskError:s,data:r}=e,i=this.promiseResponseMap.get(t);if(null!=i){null!=s?(this.emitter?.emit(d.taskError,s),i.reject(s.message)):i.resolve(r);const o=i.workerNodeKey;this.afterTaskExecutionHook(o,e),this.promiseResponseMap.delete(t),!0===this.opts.enableTasksQueue&&this.tasksQueueSize(o)>0&&this.workerNodes[o].usage.tasks.executing<this.opts.tasksQueueOptions?.concurrency&&this.executeTask(o,this.dequeueTask(o)),this.workerChoiceStrategyContext.update(o)}}checkAndEmitTaskExecutionEvents(){this.busy&&this.emitter?.emit(d.busy,this.info)}checkAndEmitTaskQueuingEvents(){this.hasBackPressure()&&this.emitter?.emit(d.backPressure,this.info)}checkAndEmitDynamicWorkerCreationEvents(){this.type===k.dynamic&&this.full&&this.emitter?.emit(d.full,this.info)}getWorkerInfo(e){return this.workerNodes[e]?.info}addWorkerNode(e){const t=new V(e,this.opts.tasksQueueOptions?.size??Math.pow(this.maxSize,2));this.starting&&(t.info.ready=!0),this.workerNodes.push(t);const s=this.getWorkerNodeKeyByWorker(e);if(-1===s)throw new Error("Worker node added not found");return s}removeWorkerNode(e){const t=this.getWorkerNodeKeyByWorker(e);-1!==t&&(this.workerNodes.splice(t,1),this.workerChoiceStrategyContext.remove(t))}hasWorkerNodeBackPressure(e){return!0===this.opts.enableTasksQueue&&this.workerNodes[e].hasBackPressure()}hasBackPressure(){return!0===this.opts.enableTasksQueue&&-1===this.workerNodes.findIndex((e=>!e.hasBackPressure()))}executeTask(e,t){this.beforeTaskExecutionHook(e,t),this.sendToWorker(e,t,t.transferList),this.checkAndEmitTaskExecutionEvents()}enqueueTask(e,t){const s=this.workerNodes[e].enqueueTask(t);return this.checkAndEmitTaskQueuingEvents(),s}dequeueTask(e){return this.workerNodes[e].dequeueTask()}tasksQueueSize(e){return this.workerNodes[e].tasksQueueSize()}flushTasksQueue(e){for(;this.tasksQueueSize(e)>0;)this.executeTask(e,this.dequeueTask(e));this.workerNodes[e].clearTasksQueue()}flushTasksQueues(){for(const[e]of this.workerNodes.entries())this.flushTasksQueue(e)}}class _ extends L{opts;constructor(e,t,s={}){super(e,t,s),this.opts=s}setupHook(){s.setupPrimary({...this.opts.settings,exec:this.filePath})}isMain(){return s.isPrimary}async destroyWorkerNode(e){this.flushTasksQueue(e);const t=this.workerNodes[e],s=t.worker,r=new Promise((e=>{s.on("exit",(()=>{e()}))}));s.on("disconnect",(()=>{s.kill()})),await this.sendKillMessageToWorker(e,t.info.id),s.disconnect(),await r}sendToWorker(e,t){this.workerNodes[e].worker.send(t)}sendStartupMessageToWorker(e){this.sendToWorker(e,{ready:!1,workerId:this.workerNodes[e].info.id})}registerWorkerMessageListener(e,t){this.workerNodes[e].worker.on("message",t)}createWorker(){return s.fork(this.opts.env)}get type(){return k.fixed}get worker(){return l.cluster}get busy(){return this.internalBusy()}}class j extends L{opts;constructor(e,t,s={}){super(e,t,s),this.opts=s}isMain(){return t.isMainThread}async destroyWorkerNode(e){this.flushTasksQueue(e);const t=this.workerNodes[e],s=t.worker,r=new Promise((e=>{s.on("exit",(()=>{e()}))}));await this.sendKillMessageToWorker(e,t.info.id),t.closeChannel(),await s.terminate(),await r}sendToWorker(e,t,s){this.workerNodes[e].messageChannel.port1.postMessage(t,s)}sendStartupMessageToWorker(e){const t=this.workerNodes[e],s=t.worker,r=t.messageChannel.port2;s.postMessage({ready:!1,workerId:t.info.id,port:r},[r])}registerWorkerMessageListener(e,t){this.workerNodes[e].messageChannel.port1.on("message",t)}createWorker(){return new t.Worker(this.filePath,{env:t.SHARE_ENV,...this.opts.workerOptions})}get type(){return k.fixed}get worker(){return l.thread}get busy(){return this.internalBusy()}}const H=6e4,$={killBehavior:I.SOFT,maxInactiveTime:H,killHandler:m};class G extends a.AsyncResource{isMain;mainWorker;opts;taskFunctions;lastTaskTimestamp;statistics;activeInterval;constructor(e,t,s,r,i=$){if(super(e),this.isMain=t,this.mainWorker=s,this.opts=i,null==this.isMain)throw new Error("isMain parameter is mandatory");this.checkTaskFunctions(r),this.checkWorkerOptions(this.opts),this.isMain||this.getMainWorker().on("message",this.handleReadyMessage.bind(this))}checkWorkerOptions(e){this.opts={...$,...e},delete this.opts.async}checkValidTaskFunction(e,t){if("string"!=typeof e)throw new TypeError("A taskFunctions parameter object key is not a string");if("string"==typeof e&&0===e.trim().length)throw new TypeError("A taskFunctions parameter object key is an empty string");if("function"!=typeof t)throw new TypeError("A taskFunctions parameter object value is not a function")}checkTaskFunctions(e){if(null==e)throw new Error("taskFunctions parameter is mandatory");if(this.taskFunctions=new Map,"function"==typeof e){const t=e.bind(this);this.taskFunctions.set(g,t),this.taskFunctions.set("string"==typeof e.name&&e.name.trim().length>0?e.name:"fn1",t)}else{if(!x(e))throw new TypeError("taskFunctions parameter is not a function or a plain object");{let t=!0;for(const[s,r]of Object.entries(e)){this.checkValidTaskFunction(s,r);const e=r.bind(this);t&&(this.taskFunctions.set(g,e),t=!1),this.taskFunctions.set(s,e)}if(t)throw new Error("taskFunctions parameter object is empty")}}}hasTaskFunction(e){return this.checkTaskFunctionName(e),this.taskFunctions.has(e)}addTaskFunction(e,t){if(this.checkTaskFunctionName(e),e===g)throw new Error("Cannot add a task function with the default reserved name");if("function"!=typeof t)throw new TypeError("fn parameter is not a function");try{const s=t.bind(this);return this.taskFunctions.get(e)===this.taskFunctions.get(g)&&this.taskFunctions.set(g,s),this.taskFunctions.set(e,s),this.sendTaskFunctionsListToMainWorker(),!0}catch{return!1}}removeTaskFunction(e){if(this.checkTaskFunctionName(e),e===g)throw new Error("Cannot remove the task function with the default reserved name");if(this.taskFunctions.get(e)===this.taskFunctions.get(g))throw new Error("Cannot remove the task function used as the default task function");const t=this.taskFunctions.delete(e);return this.sendTaskFunctionsListToMainWorker(),t}listTaskFunctions(){const e=[...this.taskFunctions.keys()];let t=g;for(const[e,s]of this.taskFunctions)if(e!==g&&s===this.taskFunctions.get(g)){t=e;break}return[e[e.indexOf(g)],t,...e.filter((e=>e!==g&&e!==t))]}setDefaultTaskFunction(e){if(this.checkTaskFunctionName(e),e===g)throw new Error("Cannot set the default task function reserved name as the default task function");if(!this.taskFunctions.has(e))throw new Error("Cannot set the default task function to a non-existing task function");try{return this.taskFunctions.set(g,this.taskFunctions.get(e)),!0}catch{return!1}}checkTaskFunctionName(e){if("string"!=typeof e)throw new TypeError("name parameter is not a string");if("string"==typeof e&&0===e.trim().length)throw new TypeError("name parameter is an empty string")}messageListener(e){this.checkMessageWorkerId(e),null!=e.statistics?this.statistics=e.statistics:null!=e.checkActive?e.checkActive?this.startCheckActive():this.stopCheckActive():null!=e.taskId&&null!=e.data?this.run(e):!0===e.kill&&this.handleKillMessage(e)}handleKillMessage(e){if(this.stopCheckActive(),E(this.opts.killHandler))(this.opts.killHandler?.()).then((()=>(this.sendToMainWorker({kill:"success",workerId:this.id}),null))).catch((()=>{this.sendToMainWorker({kill:"failure",workerId:this.id})})).finally((()=>{this.emitDestroy()})).catch(m);else try{this.opts.killHandler?.(),this.sendToMainWorker({kill:"success",workerId:this.id})}catch{this.sendToMainWorker({kill:"failure",workerId:this.id})}finally{this.emitDestroy()}}checkMessageWorkerId(e){if(null==e.workerId)throw new Error("Message worker id is not set");if(null!=e.workerId&&e.workerId!==this.id)throw new Error(`Message worker id ${e.workerId} does not match the worker id ${this.id}`)}startCheckActive(){this.lastTaskTimestamp=i.performance.now(),this.activeInterval=setInterval(this.checkActive.bind(this),(this.opts.maxInactiveTime??H)/2)}stopCheckActive(){null!=this.activeInterval&&(clearInterval(this.activeInterval),delete this.activeInterval)}checkActive(){i.performance.now()-this.lastTaskTimestamp>(this.opts.maxInactiveTime??H)&&this.sendToMainWorker({kill:this.opts.killBehavior,workerId:this.id})}getMainWorker(){if(null==this.mainWorker)throw new Error("Main worker not set");return this.mainWorker}sendTaskFunctionsListToMainWorker(){this.sendToMainWorker({taskFunctions:this.listTaskFunctions(),workerId:this.id})}handleError(e){return e instanceof Error?e.message:e}run(e){const{name:t,taskId:s,data:r}=e,i=this.taskFunctions.get(t??g);null!=i?E(i)?this.runInAsyncScope(this.runAsync.bind(this),this,i,e):this.runInAsyncScope(this.runSync.bind(this),this,i,e):this.sendToMainWorker({taskError:{name:t,message:`Task function '${t}' not found`,data:r},workerId:this.id,taskId:s})}runSync(e,t){const{name:s,taskId:r,data:i}=t;try{let t=this.beginTaskPerformance(s);const o=e(i);t=this.endTaskPerformance(t),this.sendToMainWorker({data:o,taskPerformance:t,workerId:this.id,taskId:r})}catch(e){const t=this.handleError(e);this.sendToMainWorker({taskError:{name:s,message:t,data:i},workerId:this.id,taskId:r})}finally{this.updateLastTaskTimestamp()}}runAsync(e,t){const{name:s,taskId:r,data:i}=t;let o=this.beginTaskPerformance(s);e(i).then((e=>(o=this.endTaskPerformance(o),this.sendToMainWorker({data:e,taskPerformance:o,workerId:this.id,taskId:r}),null))).catch((e=>{const t=this.handleError(e);this.sendToMainWorker({taskError:{name:s,message:t,data:i},workerId:this.id,taskId:r})})).finally((()=>{this.updateLastTaskTimestamp()})).catch(m)}beginTaskPerformance(e){return this.checkStatistics(),{name:e??g,timestamp:i.performance.now(),...this.statistics.elu&&{elu:i.performance.eventLoopUtilization()}}}endTaskPerformance(e){return this.checkStatistics(),{...e,...this.statistics.runTime&&{runTime:i.performance.now()-e.timestamp},...this.statistics.elu&&{elu:i.performance.eventLoopUtilization(e.elu)}}}checkStatistics(){if(null==this.statistics)throw new Error("Performance statistics computation requirements not set")}updateLastTaskTimestamp(){null!=this.activeInterval&&(this.lastTaskTimestamp=i.performance.now())}}exports.ClusterWorker=class extends G{constructor(e,t={}){super("worker-cluster-pool:poolifier",s.isPrimary,s.worker,e,t)}handleReadyMessage(e){if(e.workerId===this.id&&!1===e.ready)try{this.getMainWorker().on("message",this.messageListener.bind(this)),this.sendToMainWorker({ready:!0,taskFunctions:this.listTaskFunctions(),workerId:this.id})}catch{this.sendToMainWorker({ready:!1,taskFunctions:this.listTaskFunctions(),workerId:this.id})}}get id(){return this.getMainWorker().id}sendToMainWorker(e){this.getMainWorker().send(e)}},exports.DynamicClusterPool=class extends _{max;constructor(e,t,s,r={}){super(e,s,r),this.max=t,this.checkDynamicPoolSize(this.numberOfWorkers,this.max)}get type(){return k.dynamic}get busy(){return this.full&&this.internalBusy()}},exports.DynamicThreadPool=class extends j{max;constructor(e,t,s,r={}){super(e,s,r),this.max=t,this.checkDynamicPoolSize(this.numberOfWorkers,this.max)}get type(){return k.dynamic}get busy(){return this.full&&this.internalBusy()}},exports.FixedClusterPool=_,exports.FixedThreadPool=j,exports.KillBehaviors=I,exports.Measurements=R,exports.PoolEvents=d,exports.PoolTypes=k,exports.ThreadWorker=class extends G{port;constructor(e,s={}){super("worker-thread-pool:poolifier",t.isMainThread,t.parentPort,e,s)}handleReadyMessage(e){if(e.workerId===this.id&&!1===e.ready&&null!=e.port)try{this.port=e.port,this.port.on("message",this.messageListener.bind(this)),this.sendToMainWorker({ready:!0,taskFunctions:this.listTaskFunctions(),workerId:this.id})}catch{this.sendToMainWorker({ready:!1,taskFunctions:this.listTaskFunctions(),workerId:this.id})}}handleKillMessage(e){super.handleKillMessage(e),this.port?.unref(),this.port?.close()}get id(){return t.threadId}sendToMainWorker(e){this.port.postMessage(e)}handleError(e){return e}},exports.WorkerChoiceStrategies=C,exports.WorkerTypes=l,exports.availableParallelism=()=>{let e=1;try{e=u.availableParallelism()}catch{const t=u.cpus();Array.isArray(t)&&t.length>0&&(e=t.length)}return e};
{
"$schema": "https://json.schemastore.org/package",
"name": "poolifier",
"version": "2.6.37",
"version": "2.6.38",
"description": "Fast and small Node.js Worker_Threads and Cluster Worker Pool",

@@ -99,3 +99,3 @@ "license": "MIT",

"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jsdoc": "^46.5.0",
"eslint-plugin-jsdoc": "^46.5.1",
"eslint-plugin-n": "^16.0.2",

@@ -111,3 +111,3 @@ "eslint-plugin-promise": "^6.1.1",

"mochawesome": "^7.1.3",
"prettier": "^3.0.2",
"prettier": "^3.0.3",
"release-it": "^16.1.5",

@@ -118,2 +118,3 @@ "rollup": "^3.28.1",

"rollup-plugin-delete": "^2.0.0",
"rollup-plugin-dts": "^6.0.0",
"rome": "^12.1.3",

@@ -120,0 +121,0 @@ "sinon": "^15.2.0",

@@ -38,5 +38,5 @@ <div align="center">

- Easy to use :white_check_mark:
- Performance [benchmarks](./benchmarks/README.md) :white_check_mark:
- Fixed and dynamic pool size :white_check_mark:
- Easy switch from a pool type to another :white_check_mark:
- Performance [benchmarks](./benchmarks/README.md) :white_check_mark:
- No runtime dependencies :white_check_mark:

@@ -192,9 +192,12 @@ - Proper integration with Node.js [async_hooks](https://nodejs.org/api/async_hooks.html) :white_check_mark:

**_Contributors_**
**Maintainers:**
- [**Shinigami92**](https://github.com/Shinigami92)
- [**Jérôme Benoit**](https://github.com/jerome-benoit)
**Contributors:**
- [**Shinigami92**](https://github.com/Shinigami92)
## License
[MIT](./LICENSE)

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc