From c63a35a04c190989be80f9218d97e0aca739475e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 3 Jan 2024 22:53:00 +0100 Subject: [PATCH] build: make eslint configuration use strict type checking MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .eslintrc.cjs | 4 +- .../express-cluster/src/main.ts | 3 +- .../express-cluster/src/worker.ts | 3 +- .../express-hybrid/src/express-worker.ts | 3 +- .../express-hybrid/src/main.ts | 3 +- .../fastify-cluster/src/main.ts | 1 - .../fastify-cluster/src/worker.ts | 2 +- .../fastify-hybrid/src/fastify-worker.ts | 2 +- .../fastify-hybrid/src/main.ts | 1 - .../ws-cluster/src/main.ts | 1 - .../ws-hybrid/src/main.ts | 1 - src/deque.ts | 11 +- src/pools/abstract-pool.ts | 172 +++++++++--------- src/pools/cluster/dynamic.ts | 3 +- src/pools/cluster/fixed.ts | 3 +- .../abstract-worker-choice-strategy.ts | 2 +- ...hted-round-robin-worker-choice-strategy.ts | 6 +- ...hted-round-robin-worker-choice-strategy.ts | 2 +- src/pools/thread/dynamic.ts | 3 +- src/pools/thread/fixed.ts | 8 +- src/pools/utils.ts | 71 ++++---- src/pools/worker-node.ts | 3 +- src/pools/worker.ts | 2 +- src/utils.ts | 7 +- src/worker/abstract-worker.ts | 36 ++-- src/worker/cluster-worker.ts | 3 +- src/worker/thread-worker.ts | 3 +- src/worker/utils.ts | 4 +- tests/pools/utils.test.mjs | 6 +- 29 files changed, 184 insertions(+), 185 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 3ab81413..f8251b90 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -116,8 +116,8 @@ module.exports = defineConfig({ project: './tsconfig.json' }, extends: [ - 'plugin:@typescript-eslint/recommended', - 'plugin:@typescript-eslint/recommended-requiring-type-checking', + 'plugin:@typescript-eslint/strict-type-checked', + 'plugin:@typescript-eslint/stylistic-type-checked', 'plugin:import/typescript', 'standard-with-typescript' ], diff --git a/examples/typescript/http-server-pool/express-cluster/src/main.ts b/examples/typescript/http-server-pool/express-cluster/src/main.ts index 512f8d37..2184f970 100644 --- a/examples/typescript/http-server-pool/express-cluster/src/main.ts +++ b/examples/typescript/http-server-pool/express-cluster/src/main.ts @@ -19,8 +19,7 @@ const pool = new FixedClusterPool( .then(response => { if (response.status) { console.info( - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - `Express is listening in cluster worker on port ${response?.port}` + `Express is listening in cluster worker on port ${response.port}` ) } return undefined diff --git a/examples/typescript/http-server-pool/express-cluster/src/worker.ts b/examples/typescript/http-server-pool/express-cluster/src/worker.ts index 7b708730..d5756473 100644 --- a/examples/typescript/http-server-pool/express-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/express-cluster/src/worker.ts @@ -35,13 +35,12 @@ class ExpressWorker extends ClusterWorker { ExpressWorker.server = application.listen(port, () => { console.info( - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `⚡️[express server]: Express server is started in cluster worker at http://localhost:${port}/` ) }) return { status: true, - port: (ExpressWorker.server.address() as AddressInfo)?.port ?? port + port: (ExpressWorker.server.address() as AddressInfo).port } } diff --git a/examples/typescript/http-server-pool/express-hybrid/src/express-worker.ts b/examples/typescript/http-server-pool/express-hybrid/src/express-worker.ts index ac2e34d4..8cc041d4 100644 --- a/examples/typescript/http-server-pool/express-hybrid/src/express-worker.ts +++ b/examples/typescript/http-server-pool/express-hybrid/src/express-worker.ts @@ -70,13 +70,12 @@ ClusterWorkerResponse ExpressWorker.server = application.listen(port, () => { console.info( - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `⚡️[express server]: Express server is started in cluster worker at http://localhost:${port}/` ) }) return { status: true, - port: (ExpressWorker.server.address() as AddressInfo)?.port ?? port + port: (ExpressWorker.server.address() as AddressInfo).port } } diff --git a/examples/typescript/http-server-pool/express-hybrid/src/main.ts b/examples/typescript/http-server-pool/express-hybrid/src/main.ts index 040fdc74..e339f83a 100644 --- a/examples/typescript/http-server-pool/express-hybrid/src/main.ts +++ b/examples/typescript/http-server-pool/express-hybrid/src/main.ts @@ -38,8 +38,7 @@ const pool = new FixedClusterPool( .then(response => { if (response.status) { console.info( - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - `Express is listening in cluster worker on port ${response?.port}` + `Express is listening in cluster worker on port ${response.port}` ) } return undefined diff --git a/examples/typescript/http-server-pool/fastify-cluster/src/main.ts b/examples/typescript/http-server-pool/fastify-cluster/src/main.ts index 588adbed..6fe49eda 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/main.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/main.ts @@ -19,7 +19,6 @@ const pool = new FixedClusterPool( .then(response => { if (response.status) { console.info( - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `Fastify is listening in cluster worker on port ${response.port}` ) } diff --git a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts index 1551cb28..5fca8905 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts @@ -36,7 +36,7 @@ class FastifyWorker extends ClusterWorker { await FastifyWorker.fastify.listen({ port }) return { status: true, - port: (FastifyWorker.fastify.server.address() as AddressInfo)?.port + port: (FastifyWorker.fastify.server.address() as AddressInfo).port } } diff --git a/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts b/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts index 28bf2a01..0f4f6370 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-worker.ts @@ -42,7 +42,7 @@ ClusterWorkerResponse await FastifyWorker.fastify.listen({ port }) return { status: true, - port: (FastifyWorker.fastify.server.address() as AddressInfo)?.port + port: (FastifyWorker.fastify.server.address() as AddressInfo).port } } diff --git a/examples/typescript/http-server-pool/fastify-hybrid/src/main.ts b/examples/typescript/http-server-pool/fastify-hybrid/src/main.ts index d9a0015c..6b9c1727 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/src/main.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/src/main.ts @@ -38,7 +38,6 @@ const pool = new FixedClusterPool( .then(response => { if (response.status) { console.info( - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `Fastify is listening in cluster worker on port ${response.port}` ) } diff --git a/examples/typescript/websocket-server-pool/ws-cluster/src/main.ts b/examples/typescript/websocket-server-pool/ws-cluster/src/main.ts index 695dc954..762b1c0f 100644 --- a/examples/typescript/websocket-server-pool/ws-cluster/src/main.ts +++ b/examples/typescript/websocket-server-pool/ws-cluster/src/main.ts @@ -19,7 +19,6 @@ const pool = new FixedClusterPool( .then(response => { if (response.status) { console.info( - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `WebSocket server is listening in cluster worker on port ${response.port}` ) } diff --git a/examples/typescript/websocket-server-pool/ws-hybrid/src/main.ts b/examples/typescript/websocket-server-pool/ws-hybrid/src/main.ts index 177d7d3b..b35775b4 100644 --- a/examples/typescript/websocket-server-pool/ws-hybrid/src/main.ts +++ b/examples/typescript/websocket-server-pool/ws-hybrid/src/main.ts @@ -38,7 +38,6 @@ const pool = new FixedClusterPool( .then(response => { if (response.status) { console.info( - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `WebSocket server is listening in cluster worker on port ${response.port}` ) } diff --git a/src/deque.ts b/src/deque.ts index 00f60e6f..fe82a798 100644 --- a/src/deque.ts +++ b/src/deque.ts @@ -79,8 +79,7 @@ export class Deque { return } const tail = this.tail - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.tail = this.tail!.prev + this.tail = this.tail?.prev if (this.tail == null) { delete this.head } else { @@ -107,7 +106,7 @@ export class Deque { delete this.head.prev } --this.size - return head?.data + return head.data } /** @@ -156,8 +155,7 @@ export class Deque { value: node.data, done: false } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - node = node.next! + node = node.next return ret } } @@ -185,8 +183,7 @@ export class Deque { value: node.data, done: false } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - node = node.prev! + node = node.prev return ret } } diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 8e32218d..4170b8b6 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -91,7 +91,7 @@ export abstract class AbstractPool< /** * Worker choice strategy context referencing a worker choice algorithm implementation. */ - protected workerChoiceStrategyContext: WorkerChoiceStrategyContext< + protected workerChoiceStrategyContext?: WorkerChoiceStrategyContext< Worker, Data, Response @@ -189,7 +189,9 @@ export abstract class AbstractPool< } } - private checkMinimumNumberOfWorkers (minimumNumberOfWorkers: number): void { + private checkMinimumNumberOfWorkers ( + minimumNumberOfWorkers: number | undefined + ): void { if (minimumNumberOfWorkers == null) { throw new Error( 'Cannot instantiate a pool without specifying the number of workers' @@ -210,13 +212,11 @@ export abstract class AbstractPool< private checkPoolOptions (opts: PoolOptions): void { if (isPlainObject(opts)) { this.opts.startWorkers = opts.startWorkers ?? true - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - checkValidWorkerChoiceStrategy(opts.workerChoiceStrategy!) + checkValidWorkerChoiceStrategy(opts.workerChoiceStrategy) this.opts.workerChoiceStrategy = opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN this.checkValidWorkerChoiceStrategyOptions( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - opts.workerChoiceStrategyOptions! + opts.workerChoiceStrategyOptions ) if (opts.workerChoiceStrategyOptions != null) { this.opts.workerChoiceStrategyOptions = opts.workerChoiceStrategyOptions @@ -225,11 +225,9 @@ export abstract class AbstractPool< this.opts.enableEvents = opts.enableEvents ?? true this.opts.enableTasksQueue = opts.enableTasksQueue ?? false if (this.opts.enableTasksQueue) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - checkValidTasksQueueOptions(opts.tasksQueueOptions!) + checkValidTasksQueueOptions(opts.tasksQueueOptions) this.opts.tasksQueueOptions = this.buildTasksQueueOptions( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - opts.tasksQueueOptions! + opts.tasksQueueOptions ) } } else { @@ -238,7 +236,7 @@ export abstract class AbstractPool< } private checkValidWorkerChoiceStrategyOptions ( - workerChoiceStrategyOptions: WorkerChoiceStrategyOptions + workerChoiceStrategyOptions: WorkerChoiceStrategyOptions | undefined ): void { if ( workerChoiceStrategyOptions != null && @@ -288,9 +286,11 @@ export abstract class AbstractPool< minSize: this.minimumNumberOfWorkers, maxSize: this.maximumNumberOfWorkers ?? this.minimumNumberOfWorkers, ...(this.workerChoiceStrategyContext?.getTaskStatisticsRequirements() - .runTime.aggregate && - this.workerChoiceStrategyContext?.getTaskStatisticsRequirements() - .waitTime.aggregate && { utilization: round(this.utilization) }), + ?.runTime.aggregate === true && + this.workerChoiceStrategyContext.getTaskStatisticsRequirements() + .waitTime.aggregate && { + utilization: round(this.utilization) + }), workerNodes: this.workerNodes.length, idleWorkerNodes: this.workerNodes.reduce( (accumulator, workerNode) => @@ -331,7 +331,7 @@ export abstract class AbstractPool< ...(this.opts.enableTasksQueue === true && { maxQueuedTasks: this.workerNodes.reduce( (accumulator, workerNode) => - accumulator + (workerNode.usage.tasks?.maxQueued ?? 0), + accumulator + (workerNode.usage.tasks.maxQueued ?? 0), 0 ) }), @@ -351,23 +351,23 @@ export abstract class AbstractPool< 0 ), ...(this.workerChoiceStrategyContext?.getTaskStatisticsRequirements() - .runTime.aggregate && { + ?.runTime.aggregate === true && { runTime: { minimum: round( min( ...this.workerNodes.map( - workerNode => workerNode.usage.runTime?.minimum ?? Infinity + workerNode => workerNode.usage.runTime.minimum ?? Infinity ) ) ), maximum: round( max( ...this.workerNodes.map( - workerNode => workerNode.usage.runTime?.maximum ?? -Infinity + workerNode => workerNode.usage.runTime.maximum ?? -Infinity ) ) ), - ...(this.workerChoiceStrategyContext?.getTaskStatisticsRequirements() + ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements() .runTime.average && { average: round( average( @@ -379,7 +379,7 @@ export abstract class AbstractPool< ) ) }), - ...(this.workerChoiceStrategyContext?.getTaskStatisticsRequirements() + ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements() .runTime.median && { median: round( median( @@ -394,23 +394,23 @@ export abstract class AbstractPool< } }), ...(this.workerChoiceStrategyContext?.getTaskStatisticsRequirements() - .waitTime.aggregate && { + ?.waitTime.aggregate === true && { waitTime: { minimum: round( min( ...this.workerNodes.map( - workerNode => workerNode.usage.waitTime?.minimum ?? Infinity + workerNode => workerNode.usage.waitTime.minimum ?? Infinity ) ) ), maximum: round( max( ...this.workerNodes.map( - workerNode => workerNode.usage.waitTime?.maximum ?? -Infinity + workerNode => workerNode.usage.waitTime.maximum ?? -Infinity ) ) ), - ...(this.workerChoiceStrategyContext?.getTaskStatisticsRequirements() + ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements() .waitTime.average && { average: round( average( @@ -422,7 +422,7 @@ export abstract class AbstractPool< ) ) }), - ...(this.workerChoiceStrategyContext?.getTaskStatisticsRequirements() + ...(this.workerChoiceStrategyContext.getTaskStatisticsRequirements() .waitTime.median && { median: round( median( @@ -465,12 +465,12 @@ export abstract class AbstractPool< (this.maximumNumberOfWorkers ?? this.minimumNumberOfWorkers) const totalTasksRunTime = this.workerNodes.reduce( (accumulator, workerNode) => - accumulator + (workerNode.usage.runTime?.aggregate ?? 0), + accumulator + (workerNode.usage.runTime.aggregate ?? 0), 0 ) const totalTasksWaitTime = this.workerNodes.reduce( (accumulator, workerNode) => - accumulator + (workerNode.usage.waitTime?.aggregate ?? 0), + accumulator + (workerNode.usage.waitTime.aggregate ?? 0), 0 ) return (totalTasksRunTime + totalTasksWaitTime) / poolTimeCapacity @@ -523,7 +523,7 @@ export abstract class AbstractPool< ): void { checkValidWorkerChoiceStrategy(workerChoiceStrategy) this.opts.workerChoiceStrategy = workerChoiceStrategy - this.workerChoiceStrategyContext.setWorkerChoiceStrategy( + this.workerChoiceStrategyContext?.setWorkerChoiceStrategy( this.opts.workerChoiceStrategy ) if (workerChoiceStrategyOptions != null) { @@ -537,13 +537,13 @@ export abstract class AbstractPool< /** @inheritDoc */ public setWorkerChoiceStrategyOptions ( - workerChoiceStrategyOptions: WorkerChoiceStrategyOptions + workerChoiceStrategyOptions: WorkerChoiceStrategyOptions | undefined ): void { this.checkValidWorkerChoiceStrategyOptions(workerChoiceStrategyOptions) if (workerChoiceStrategyOptions != null) { this.opts.workerChoiceStrategyOptions = workerChoiceStrategyOptions } - this.workerChoiceStrategyContext.setOptions( + this.workerChoiceStrategyContext?.setOptions( this.opts.workerChoiceStrategyOptions ) } @@ -559,12 +559,13 @@ export abstract class AbstractPool< this.flushTasksQueues() } this.opts.enableTasksQueue = enable - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.setTasksQueueOptions(tasksQueueOptions!) + this.setTasksQueueOptions(tasksQueueOptions) } /** @inheritDoc */ - public setTasksQueueOptions (tasksQueueOptions: TasksQueueOptions): void { + public setTasksQueueOptions ( + tasksQueueOptions: TasksQueueOptions | undefined + ): void { if (this.opts.enableTasksQueue === true) { checkValidTasksQueueOptions(tasksQueueOptions) this.opts.tasksQueueOptions = @@ -589,7 +590,7 @@ export abstract class AbstractPool< } private buildTasksQueueOptions ( - tasksQueueOptions: TasksQueueOptions + tasksQueueOptions: TasksQueueOptions | undefined ): TasksQueueOptions { return { ...getDefaultTasksQueueOptions( @@ -702,24 +703,17 @@ export abstract class AbstractPool< message: MessageValue ): void => { this.checkMessageWorkerId(message) - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const workerId = this.getWorkerInfo(workerNodeKey).id! + const workerId = this.getWorkerInfo(workerNodeKey).id if ( message.taskFunctionOperationStatus != null && message.workerId === workerId ) { if (message.taskFunctionOperationStatus) { resolve(true) - } else if (!message.taskFunctionOperationStatus) { + } else { reject( new Error( - `Task function operation '${ - message.taskFunctionOperation as string - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - }' failed on worker ${message.workerId} with error: '${ - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - message.workerError!.message - }'` + `Task function operation '${message.taskFunctionOperation}' failed on worker ${message.workerId} with error: '${message.workerError?.message}'` ) ) } @@ -767,11 +761,8 @@ export abstract class AbstractPool< new Error( `Task function operation '${ message.taskFunctionOperation as string - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - }' failed on worker ${errorResponse! - .workerId!} with error: '${ - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - errorResponse!.workerError!.message + }' failed on worker ${errorResponse?.workerId} with error: '${ + errorResponse?.workerError?.message }'` ) ) @@ -998,7 +989,8 @@ export abstract class AbstractPool< private async sendKillMessageToWorker (workerNodeKey: number): Promise { await new Promise((resolve, reject) => { - if (this.workerNodes?.[workerNodeKey] == null) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (this.workerNodes[workerNodeKey] == null) { resolve() return } @@ -1009,8 +1001,7 @@ export abstract class AbstractPool< } else if (message.kill === 'failure') { reject( new Error( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - `Kill message handling failed on worker ${message.workerId!}` + `Kill message handling failed on worker ${message.workerId}` ) ) } @@ -1069,7 +1060,8 @@ export abstract class AbstractPool< workerNodeKey: number, task: Task ): void { - if (this.workerNodes[workerNodeKey]?.usage != null) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (this.workerNodes[workerNodeKey].usage != null) { const workerUsage = this.workerNodes[workerNodeKey].usage ++workerUsage.tasks.executing updateWaitTimeWorkerUsage( @@ -1110,7 +1102,8 @@ export abstract class AbstractPool< message: MessageValue ): void { let needWorkerChoiceStrategyUpdate = false - if (this.workerNodes[workerNodeKey]?.usage != null) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (this.workerNodes[workerNodeKey].usage != null) { const workerUsage = this.workerNodes[workerNodeKey].usage updateTaskStatisticsWorkerUsage(workerUsage, message) updateRunTimeWorkerUsage( @@ -1151,7 +1144,7 @@ export abstract class AbstractPool< needWorkerChoiceStrategyUpdate = true } if (needWorkerChoiceStrategyUpdate) { - this.workerChoiceStrategyContext.update(workerNodeKey) + this.workerChoiceStrategyContext?.update(workerNodeKey) } } @@ -1164,7 +1157,6 @@ export abstract class AbstractPool< private shallUpdateTaskFunctionWorkerUsage (workerNodeKey: number): boolean { const workerInfo = this.getWorkerInfo(workerNodeKey) return ( - workerInfo != null && Array.isArray(workerInfo.taskFunctionNames) && workerInfo.taskFunctionNames.length > 2 ) @@ -1181,12 +1173,14 @@ export abstract class AbstractPool< if (this.shallCreateDynamicWorker()) { const workerNodeKey = this.createAndSetupDynamicWorkerNode() if ( - this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerUsage + this.workerChoiceStrategyContext?.getStrategyPolicy() + .dynamicWorkerUsage === true ) { return workerNodeKey } } - return this.workerChoiceStrategyContext.execute() + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return this.workerChoiceStrategyContext!.execute()! } /** @@ -1249,7 +1243,7 @@ export abstract class AbstractPool< ) { this.redistributeQueuedTasks(this.workerNodes.indexOf(workerNode)) } - workerNode?.terminate().catch(error => { + workerNode.terminate().catch(error => { this.emitter?.emit(PoolEvents.error, error) }) }) @@ -1312,8 +1306,10 @@ export abstract class AbstractPool< const workerNode = this.workerNodes[workerNodeKey] workerNode.info.dynamic = true if ( - this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerReady || - this.workerChoiceStrategyContext.getStrategyPolicy().dynamicWorkerUsage + this.workerChoiceStrategyContext?.getStrategyPolicy() + .dynamicWorkerReady === true || + this.workerChoiceStrategyContext?.getStrategyPolicy() + .dynamicWorkerUsage === true ) { workerNode.info.ready = true } @@ -1408,10 +1404,11 @@ export abstract class AbstractPool< this.sendToWorker(workerNodeKey, { statistics: { runTime: - this.workerChoiceStrategyContext.getTaskStatisticsRequirements() - .runTime.aggregate, - elu: this.workerChoiceStrategyContext.getTaskStatisticsRequirements() - .elu.aggregate + this.workerChoiceStrategyContext?.getTaskStatisticsRequirements() + ?.runTime.aggregate ?? false, + elu: + this.workerChoiceStrategyContext?.getTaskStatisticsRequirements()?.elu + .aggregate ?? false } }) } @@ -1456,7 +1453,8 @@ export abstract class AbstractPool< taskName: string ): void { const workerNode = this.workerNodes[workerNodeKey] - if (workerNode?.usage != null) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (workerNode.usage != null) { ++workerNode.usage.tasks.stolen } if ( @@ -1474,7 +1472,8 @@ export abstract class AbstractPool< workerNodeKey: number ): void { const workerNode = this.workerNodes[workerNodeKey] - if (workerNode?.usage != null) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (workerNode.usage != null) { ++workerNode.usage.tasks.sequentiallyStolen } } @@ -1499,7 +1498,8 @@ export abstract class AbstractPool< workerNodeKey: number ): void { const workerNode = this.workerNodes[workerNodeKey] - if (workerNode?.usage != null) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (workerNode.usage != null) { workerNode.usage.tasks.sequentiallyStolen = 0 } } @@ -1527,16 +1527,17 @@ export abstract class AbstractPool< const { workerNodeKey } = eventDetail if (workerNodeKey == null) { throw new Error( - 'WorkerNode event detail workerNodeKey property must be defined' + "WorkerNode event detail 'workerNodeKey' property must be defined" ) } + const workerInfo = this.getWorkerInfo(workerNodeKey) if ( this.cannotStealTask() || - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.info.stealingWorkerNodes! > Math.floor(this.workerNodes.length / 2) + (this.info.stealingWorkerNodes ?? 0) > + Math.floor(this.workerNodes.length / 2) ) { if (previousStolenTask != null) { - this.getWorkerInfo(workerNodeKey).stealing = false + workerInfo.stealing = false } return } @@ -1547,7 +1548,7 @@ export abstract class AbstractPool< (workerNodeTasksUsage.executing > 0 || this.tasksQueueSize(workerNodeKey) > 0) ) { - this.getWorkerInfo(workerNodeKey).stealing = false + workerInfo.stealing = false // eslint-disable-next-line @typescript-eslint/no-non-null-assertion for (const taskName of this.workerNodes[workerNodeKey].info .taskFunctionNames!) { @@ -1559,7 +1560,7 @@ export abstract class AbstractPool< this.resetTaskSequentiallyStolenStatisticsWorkerUsage(workerNodeKey) return } - this.getWorkerInfo(workerNodeKey).stealing = true + workerInfo.stealing = true const stolenTask = this.workerNodeStealTask(workerNodeKey) if ( this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) && @@ -1629,8 +1630,8 @@ export abstract class AbstractPool< ): void => { if ( this.cannotStealTask() || - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.info.stealingWorkerNodes! > Math.floor(this.workerNodes.length / 2) + (this.info.stealingWorkerNodes ?? 0) > + Math.floor(this.workerNodes.length / 2) ) { return } @@ -1658,13 +1659,14 @@ export abstract class AbstractPool< // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.opts.tasksQueueOptions!.size! - sizeOffset ) { - this.getWorkerInfo(workerNodeKey).stealing = true + const workerInfo = this.getWorkerInfo(workerNodeKey) + workerInfo.stealing = true // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const task = sourceWorkerNode.popTask()! this.handleTask(workerNodeKey, task) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.updateTaskStolenStatisticsWorkerUsage(workerNodeKey, task.name!) - this.getWorkerInfo(workerNodeKey).stealing = false + workerInfo.stealing = false } } } @@ -1694,8 +1696,7 @@ export abstract class AbstractPool< private handleWorkerReadyResponse (message: MessageValue): void { const { workerId, ready, taskFunctionNames } = message if (ready == null || !ready) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - throw new Error(`Worker ${workerId!} failed to initialize`) + throw new Error(`Worker ${workerId} failed to initialize`) } const workerNode = this.workerNodes[this.getWorkerNodeKeyByWorkerId(workerId)] @@ -1732,7 +1733,7 @@ export abstract class AbstractPool< this.afterTaskExecutionHook(workerNodeKey, message) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.promiseResponseMap.delete(taskId!) - workerNode?.emit('taskFinished', taskId) + workerNode.emit('taskFinished', taskId) if (this.opts.enableTasksQueue === true && !this.destroying) { const workerNodeTasksUsage = workerNode.usage.tasks if ( @@ -1783,7 +1784,12 @@ export abstract class AbstractPool< * @returns The worker information. */ protected getWorkerInfo (workerNodeKey: number): WorkerInfo { - return this.workerNodes[workerNodeKey]?.info + const workerInfo = this.workerNodes[workerNodeKey]?.info + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (workerInfo == null) { + throw new Error(`Worker node with key '${workerNodeKey}' not found`) + } + return workerInfo } /** @@ -1837,7 +1843,7 @@ export abstract class AbstractPool< const workerNodeKey = this.workerNodes.indexOf(workerNode) if (workerNodeKey !== -1) { this.workerNodes.splice(workerNodeKey, 1) - this.workerChoiceStrategyContext.remove(workerNodeKey) + this.workerChoiceStrategyContext?.remove(workerNodeKey) } } diff --git a/src/pools/cluster/dynamic.ts b/src/pools/cluster/dynamic.ts index 564dbf54..7b2c3798 100644 --- a/src/pools/cluster/dynamic.ts +++ b/src/pools/cluster/dynamic.ts @@ -34,8 +34,7 @@ export class DynamicClusterPool< super(min, filePath, opts, max) checkDynamicPoolSize( this.minimumNumberOfWorkers, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.maximumNumberOfWorkers! + this.maximumNumberOfWorkers ) } diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index aa731562..09dbe509 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -54,8 +54,7 @@ export class FixedClusterPool< ): void { this.workerNodes[workerNodeKey].worker.send({ ...message, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - workerId: this.getWorkerInfo(workerNodeKey).id! + workerId: this.getWorkerInfo(workerNodeKey).id }) } diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index 0a2b6113..55f43092 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -32,7 +32,7 @@ export abstract class AbstractWorkerChoiceStrategy< /** * The previous worker node key. */ - protected previousWorkerNodeKey: number = 0 + protected previousWorkerNodeKey = 0 /** @inheritDoc */ public readonly strategyPolicy: StrategyPolicy = { diff --git a/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts index 3942bb6e..e2aa0c6d 100644 --- a/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts @@ -36,7 +36,7 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< /** * Round id. */ - private roundId: number = 0 + private roundId = 0 /** * Round weights. */ @@ -44,11 +44,11 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< /** * Worker node id. */ - private workerNodeId: number = 0 + private workerNodeId = 0 /** * Worker node virtual task runtime. */ - private workerNodeVirtualTaskRunTime: number = 0 + private workerNodeVirtualTaskRunTime = 0 /** @inheritDoc */ public constructor ( diff --git a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts index 6bbbbaf6..94a832f6 100644 --- a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts @@ -37,7 +37,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy< /** * Worker node virtual task runtime. */ - private workerNodeVirtualTaskRunTime: number = 0 + private workerNodeVirtualTaskRunTime = 0 /** @inheritDoc */ public constructor ( diff --git a/src/pools/thread/dynamic.ts b/src/pools/thread/dynamic.ts index 836cae0b..fde37104 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -34,8 +34,7 @@ export class DynamicThreadPool< super(min, filePath, opts, max) checkDynamicPoolSize( this.minimumNumberOfWorkers, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.maximumNumberOfWorkers! + this.maximumNumberOfWorkers ) } diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index c4256bf3..ff427ea2 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -53,7 +53,7 @@ export class FixedThreadPool< message: MessageValue, transferList?: TransferListItem[] ): void { - this.workerNodes[workerNodeKey].messageChannel?.port1?.postMessage( + this.workerNodes[workerNodeKey].messageChannel?.port1.postMessage( { ...message, workerId: this.getWorkerInfo(workerNodeKey).id }, transferList ) @@ -79,7 +79,7 @@ export class FixedThreadPool< workerNodeKey: number, listener: (message: MessageValue) => void ): void { - this.workerNodes[workerNodeKey].messageChannel?.port1?.on( + this.workerNodes[workerNodeKey].messageChannel?.port1.on( 'message', listener ) @@ -90,7 +90,7 @@ export class FixedThreadPool< workerNodeKey: number, listener: (message: MessageValue) => void ): void { - this.workerNodes[workerNodeKey].messageChannel?.port1?.once( + this.workerNodes[workerNodeKey].messageChannel?.port1.once( 'message', listener ) @@ -101,7 +101,7 @@ export class FixedThreadPool< workerNodeKey: number, listener: (message: MessageValue) => void ): void { - this.workerNodes[workerNodeKey].messageChannel?.port1?.off( + this.workerNodes[workerNodeKey].messageChannel?.port1.off( 'message', listener ) diff --git a/src/pools/utils.ts b/src/pools/utils.ts index 99584d85..c0e803a2 100644 --- a/src/pools/utils.ts +++ b/src/pools/utils.ts @@ -33,7 +33,7 @@ export const getDefaultTasksQueueOptions = ( } } -export const checkFilePath = (filePath: string): void => { +export const checkFilePath = (filePath: string | undefined): void => { if (filePath == null) { throw new TypeError('The worker file path must be specified') } @@ -45,7 +45,10 @@ export const checkFilePath = (filePath: string): void => { } } -export const checkDynamicPoolSize = (min: number, max: number): void => { +export const checkDynamicPoolSize = ( + min: number, + max: number | undefined +): void => { if (max == null) { throw new TypeError( 'Cannot instantiate a dynamic pool without specifying the maximum pool size' @@ -70,7 +73,7 @@ export const checkDynamicPoolSize = (min: number, max: number): void => { } export const checkValidWorkerChoiceStrategy = ( - workerChoiceStrategy: WorkerChoiceStrategy + workerChoiceStrategy: WorkerChoiceStrategy | undefined ): void => { if ( workerChoiceStrategy != null && @@ -81,7 +84,7 @@ export const checkValidWorkerChoiceStrategy = ( } export const checkValidTasksQueueOptions = ( - tasksQueueOptions: TasksQueueOptions + tasksQueueOptions: TasksQueueOptions | undefined ): void => { if (tasksQueueOptions != null && !isPlainObject(tasksQueueOptions)) { throw new TypeError('Invalid tasks queue options: must be a plain object') @@ -118,9 +121,9 @@ export const checkValidTasksQueueOptions = ( } export const checkWorkerNodeArguments = ( - type: WorkerType, - filePath: string, - opts: WorkerNodeOptions + type: WorkerType | undefined, + filePath: string | undefined, + opts: WorkerNodeOptions | undefined ): void => { if (type == null) { throw new TypeError('Cannot construct a worker node without a worker type') @@ -168,10 +171,14 @@ export const checkWorkerNodeArguments = ( */ const updateMeasurementStatistics = ( measurementStatistics: MeasurementStatistics, - measurementRequirements: MeasurementStatisticsRequirements, - measurementValue: number + measurementRequirements: MeasurementStatisticsRequirements | undefined, + measurementValue: number | undefined ): void => { - if (measurementRequirements.aggregate) { + if ( + measurementRequirements != null && + measurementValue != null && + measurementRequirements.aggregate + ) { measurementStatistics.aggregate = (measurementStatistics.aggregate ?? 0) + measurementValue measurementStatistics.minimum = min( @@ -182,10 +189,7 @@ const updateMeasurementStatistics = ( measurementValue, measurementStatistics.maximum ?? -Infinity ) - if ( - (measurementRequirements.average || measurementRequirements.median) && - measurementValue != null - ) { + if (measurementRequirements.average || measurementRequirements.median) { measurementStatistics.history.push(measurementValue) if (measurementRequirements.average) { measurementStatistics.average = average(measurementStatistics.history) @@ -210,11 +214,9 @@ export const updateWaitTimeWorkerUsage = < Data = unknown, Response = unknown >( - workerChoiceStrategyContext: WorkerChoiceStrategyContext< - Worker, - Data, - Response - >, + workerChoiceStrategyContext: + | WorkerChoiceStrategyContext + | undefined, workerUsage: WorkerUsage, task: Task ): void => { @@ -222,7 +224,7 @@ export const updateWaitTimeWorkerUsage = < const taskWaitTime = timestamp - (task.timestamp ?? timestamp) updateMeasurementStatistics( workerUsage.waitTime, - workerChoiceStrategyContext.getTaskStatisticsRequirements().waitTime, + workerChoiceStrategyContext?.getTaskStatisticsRequirements()?.waitTime, taskWaitTime ) } @@ -233,6 +235,7 @@ export const updateTaskStatisticsWorkerUsage = ( ): void => { const workerTaskStatistics = workerUsage.tasks if ( + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition workerTaskStatistics.executing != null && workerTaskStatistics.executing > 0 ) { @@ -250,11 +253,9 @@ export const updateRunTimeWorkerUsage = < Data = unknown, Response = unknown >( - workerChoiceStrategyContext: WorkerChoiceStrategyContext< - Worker, - Data, - Response - >, + workerChoiceStrategyContext: + | WorkerChoiceStrategyContext + | undefined, workerUsage: WorkerUsage, message: MessageValue ): void => { @@ -263,7 +264,7 @@ export const updateRunTimeWorkerUsage = < } updateMeasurementStatistics( workerUsage.runTime, - workerChoiceStrategyContext.getTaskStatisticsRequirements().runTime, + workerChoiceStrategyContext?.getTaskStatisticsRequirements()?.runTime, message.taskPerformance?.runTime ?? 0 ) } @@ -273,19 +274,17 @@ export const updateEluWorkerUsage = < Data = unknown, Response = unknown >( - workerChoiceStrategyContext: WorkerChoiceStrategyContext< - Worker, - Data, - Response - >, + workerChoiceStrategyContext: + | WorkerChoiceStrategyContext + | undefined, workerUsage: WorkerUsage, message: MessageValue ): void => { if (message.workerError != null) { return } - const eluTaskStatisticsRequirements: MeasurementStatisticsRequirements = - workerChoiceStrategyContext.getTaskStatisticsRequirements().elu + const eluTaskStatisticsRequirements = + workerChoiceStrategyContext?.getTaskStatisticsRequirements()?.elu updateMeasurementStatistics( workerUsage.elu.active, eluTaskStatisticsRequirements, @@ -296,7 +295,7 @@ export const updateEluWorkerUsage = < eluTaskStatisticsRequirements, message.taskPerformance?.elu?.idle ?? 0 ) - if (eluTaskStatisticsRequirements.aggregate) { + if (eluTaskStatisticsRequirements?.aggregate === true) { if (message.taskPerformance?.elu != null) { if (workerUsage.elu.utilization != null) { workerUsage.elu.utilization = @@ -319,10 +318,10 @@ export const createWorker = ( case WorkerTypes.thread: return new Worker(filePath, { env: SHARE_ENV, - ...opts?.workerOptions + ...opts.workerOptions }) as unknown as Worker case WorkerTypes.cluster: - return cluster.fork(opts?.env) as unknown as Worker + return cluster.fork(opts.env) as unknown as Worker default: // eslint-disable-next-line @typescript-eslint/restrict-template-expressions throw new Error(`Unknown worker type '${type}'`) diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index 73b2c70c..9fa4ea32 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -61,7 +61,8 @@ export class WorkerNode if (this.info.type === WorkerTypes.thread) { this.messageChannel = new MessageChannel() } - this.tasksQueueBackPressureSize = opts.tasksQueueBackPressureSize + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.tasksQueueBackPressureSize = opts.tasksQueueBackPressureSize! this.tasksQueue = new Deque>() this.onBackPressureStarted = false this.taskFunctionsUsage = new Map() diff --git a/src/pools/worker.ts b/src/pools/worker.ts index f439c882..7f8148ff 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -258,7 +258,7 @@ export interface IWorker { export interface WorkerNodeOptions { workerOptions?: WorkerOptions env?: Record - tasksQueueBackPressureSize: number + tasksQueueBackPressureSize: number | undefined } /** diff --git a/src/utils.ts b/src/utils.ts index a7cb55f9..b38cf948 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -178,7 +178,7 @@ export const round = (num: number, scale = 2): number => { export const isPlainObject = (obj: unknown): boolean => typeof obj === 'object' && obj !== null && - obj?.constructor === Object && + obj.constructor === Object && Object.prototype.toString.call(obj) === '[object Object]' /** @@ -257,6 +257,7 @@ export const once = ( ): ((...args: A) => R) => { let result: R return (...args: A) => { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (fn != null) { result = fn.apply(context, args) ;(fn as unknown as undefined) = (context as unknown as undefined) = @@ -293,7 +294,7 @@ export const buildWorkerChoiceStrategyOptions = < opts?: WorkerChoiceStrategyOptions ): WorkerChoiceStrategyOptions => { opts = clone(opts ?? {}) - opts.weights = opts?.weights ?? getDefaultWeights(pool.info.maxSize) + opts.weights = opts.weights ?? getDefaultWeights(pool.info.maxSize) return { ...{ runTime: { median: false }, @@ -320,8 +321,10 @@ const getDefaultWorkerWeight = (): number => { const cpuSpeed = randomInt(500, 2500) let cpusCycleTimeWeight = 0 for (const cpu of cpus()) { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (cpu.speed == null || cpu.speed === 0) { cpu.speed = + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition cpus().find(cpu => cpu.speed != null && cpu.speed !== 0)?.speed ?? cpuSpeed } diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 2359758d..890f340e 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -71,7 +71,7 @@ export abstract class AbstractWorker< /** * Performance statistics computation requirements. */ - protected statistics!: WorkerStatistics + protected statistics?: WorkerStatistics /** * Handler id of the `activeInterval` worker activity check. */ @@ -86,8 +86,8 @@ export abstract class AbstractWorker< * @param opts - Options for the worker. */ public constructor ( - protected readonly isMain: boolean, - private readonly mainWorker: MainWorker, + protected readonly isMain: boolean | undefined, + private readonly mainWorker: MainWorker | undefined | null, taskFunctions: TaskFunction | TaskFunctions, protected opts: WorkerOptions = DEFAULT_WORKER_OPTIONS ) { @@ -113,7 +113,10 @@ export abstract class AbstractWorker< * @param taskFunctions - The task function(s) parameter that should be checked. */ private checkTaskFunctions ( - taskFunctions: TaskFunction | TaskFunctions + taskFunctions: + | TaskFunction + | TaskFunctions + | undefined ): void { if (taskFunctions == null) { throw new Error('taskFunctions parameter is mandatory') @@ -352,7 +355,7 @@ export abstract class AbstractWorker< taskFunctionOperationStatus: response.status, taskFunctionName, ...(!response.status && - response?.error != null && { + response.error != null && { workerError: { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion name: taskFunctionName!, @@ -370,7 +373,7 @@ export abstract class AbstractWorker< protected handleKillMessage (_message: MessageValue): void { this.stopCheckActive() if (isAsyncFunction(this.opts.killHandler)) { - (this.opts.killHandler?.() as Promise) + (this.opts.killHandler() as Promise) .then(() => { this.sendToMainWorker({ kill: 'success' }) return undefined @@ -492,8 +495,7 @@ export abstract class AbstractWorker< workerError: { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion name: name!, - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - message: `Task function '${name!}' not found`, + message: `Task function '${name}' not found`, data }, taskId @@ -583,18 +585,24 @@ export abstract class AbstractWorker< } private beginTaskPerformance (name?: string): TaskPerformance { - this.checkStatistics() + if (this.statistics == null) { + throw new Error('Performance statistics computation requirements not set') + } return { name: name ?? DEFAULT_TASK_NAME, timestamp: performance.now(), - ...(this.statistics.elu && { elu: performance.eventLoopUtilization() }) + ...(this.statistics.elu && { + elu: performance.eventLoopUtilization() + }) } } private endTaskPerformance ( taskPerformance: TaskPerformance ): TaskPerformance { - this.checkStatistics() + if (this.statistics == null) { + throw new Error('Performance statistics computation requirements not set') + } return { ...taskPerformance, ...(this.statistics.runTime && { @@ -606,12 +614,6 @@ export abstract class AbstractWorker< } } - private checkStatistics (): void { - if (this.statistics == null) { - throw new Error('Performance statistics computation requirements not set') - } - } - private updateLastTaskTimestamp (): void { if (this.activeInterval != null) { this.lastTaskTimestamp = performance.now() diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index 05fe2534..1a9deb30 100644 --- a/src/worker/cluster-worker.ts +++ b/src/worker/cluster-worker.ts @@ -32,8 +32,7 @@ export class ClusterWorker< taskFunctions: TaskFunction | TaskFunctions, opts: WorkerOptions = {} ) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - super(cluster.isPrimary, cluster.worker!, taskFunctions, opts) + super(cluster.isPrimary, cluster.worker, taskFunctions, opts) } /** @inheritDoc */ diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index 7f3cd950..3635d92c 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -42,8 +42,7 @@ export class ThreadWorker< taskFunctions: TaskFunction | TaskFunctions, opts: WorkerOptions = {} ) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - super(isMainThread, parentPort!, taskFunctions, opts) + super(isMainThread, parentPort, taskFunctions, opts) } /** @inheritDoc */ diff --git a/src/worker/utils.ts b/src/worker/utils.ts index b9372401..d8c4c3e9 100644 --- a/src/worker/utils.ts +++ b/src/worker/utils.ts @@ -2,7 +2,9 @@ import { isPlainObject } from '../utils.js' import type { TaskFunction } from './task-functions.js' import { KillBehaviors, type WorkerOptions } from './worker-options.js' -export const checkValidWorkerOptions = (opts: WorkerOptions): void => { +export const checkValidWorkerOptions = ( + opts: WorkerOptions | undefined +): void => { if (opts != null && !isPlainObject(opts)) { throw new TypeError('opts worker options parameter is not a plain object') } diff --git a/tests/pools/utils.test.mjs b/tests/pools/utils.test.mjs index bc0c7126..610b92c3 100644 --- a/tests/pools/utils.test.mjs +++ b/tests/pools/utils.test.mjs @@ -115,13 +115,15 @@ describe('Pool utils test suite', () => { expect( createWorker( WorkerTypes.thread, - './tests/worker-files/thread/testWorker.mjs' + './tests/worker-files/thread/testWorker.mjs', + {} ) ).toBeInstanceOf(ThreadWorker) expect( createWorker( WorkerTypes.cluster, - './tests/worker-files/cluster/testWorker.mjs' + './tests/worker-files/cluster/testWorker.mjs', + {} ) ).toBeInstanceOf(ClusterWorker) }) -- 2.34.1