From 6e5d7052fe741b50e68f8614b33d3754be41415f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 31 May 2024 16:28:25 +0200 Subject: [PATCH] build: reenable eslint type checking MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .lintstagedrc.js | 5 ++- eslint.config.js | 40 +++++++++++-------- .../typescript/http-client-pool/src/main.ts | 6 +-- .../typescript/http-client-pool/src/worker.ts | 1 + .../express-cluster/src/main.ts | 3 +- .../express-cluster/src/worker.ts | 2 +- .../express-hybrid/src/express-worker.ts | 3 +- .../express-hybrid/src/main.ts | 3 +- .../express-worker_threads/src/main.ts | 3 +- .../fastify-cluster/src/main.ts | 3 +- .../fastify-hybrid/src/main.ts | 3 +- examples/typescript/pool.ts | 1 + .../typescript/smtp-client-pool/src/main.ts | 6 +-- .../ws-cluster/src/main.ts | 3 +- .../ws-cluster/src/worker.ts | 5 ++- .../ws-hybrid/src/main.ts | 3 +- .../ws-hybrid/src/websocket-server-worker.ts | 6 ++- .../ws-worker_threads/src/main.ts | 5 ++- src/circular-buffer.ts | 6 ++- src/fixed-priority-queue.ts | 6 ++- src/pools/abstract-pool.ts | 31 ++++++++++---- .../selection-strategies-utils.ts | 4 ++ .../worker-choice-strategies-context.ts | 2 +- src/pools/utils.ts | 9 +++-- src/priority-queue.ts | 4 +- src/utils.ts | 1 + src/worker/abstract-worker.ts | 14 ++++--- src/worker/utils.ts | 1 + 28 files changed, 116 insertions(+), 63 deletions(-) diff --git a/.lintstagedrc.js b/.lintstagedrc.js index 8cdf685f..403bbda9 100644 --- a/.lintstagedrc.js +++ b/.lintstagedrc.js @@ -1,7 +1,8 @@ export default { '**/*.{ts,tsx,js,jsx,cjs,mjs}': [ - 'biome format --write', - 'eslint --cache --fix', + // 'biome format --write', + // 'eslint --cache --fix', + 'eslint --cache', ], '**/*.json': ['biome format --write'], '**/*.{md,yml,yaml}': ['prettier --cache --write'], diff --git a/eslint.config.js b/eslint.config.js index e0fa23c6..8a100a1a 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -17,7 +17,10 @@ export default defineFlatConfig([ js.configs.recommended, ...nodePlugin.configs['flat/mixed-esm-and-cjs'], jsdoc.configs['flat/recommended-typescript'], - ...tseslint.config(...tseslint.configs.strict, ...tseslint.configs.stylistic), + ...tseslint.config( + ...tseslint.configs.strictTypeChecked, + ...tseslint.configs.stylisticTypeChecked + ), ...neostandard({ ts: true, globals: { @@ -25,18 +28,14 @@ export default defineFlatConfig([ ...globals.mocha, }, }), - // ...tseslint.config( - // ...tseslint.configs.strictTypeChecked, - // ...tseslint.configs.stylisticTypeChecked, - // { - // languageOptions: { - // parserOptions: { - // project: true, - // tsconfigRootDir: import.meta.dirname, - // }, - // }, - // } - // ), + { + languageOptions: { + parserOptions: { + project: true, + tsconfigRootDir: import.meta.dirname, + }, + }, + }, { plugins: { 'simple-import-sort': simpleImportSort, @@ -73,11 +72,16 @@ export default defineFlatConfig([ }, }, { - files: ['examples/**/*.ts'], - rules: { - 'no-undef': 'off', - }, + files: ['**/*.js', '**/*.mjs', '**/*.cjs'], + ...tseslint.configs.disableTypeChecked, }, + // examples specific configuration + // { + // files: ['examples/**/*.ts'], + // rules: { + // 'no-undef': 'off', + // }, + // }, { files: ['examples/**/*.js', 'examples/**/*.cjs'], rules: { @@ -90,12 +94,14 @@ export default defineFlatConfig([ '@typescript-eslint/no-require-imports': 'off', }, }, + // benchmarks specific configuration { files: ['benchmarks/**/*.cjs'], rules: { '@typescript-eslint/no-require-imports': 'off', }, }, + // tests specific configuration { files: ['tests/**/*.js', 'tests/**/*.mjs', 'tests/**/*.cjs'], rules: { diff --git a/examples/typescript/http-client-pool/src/main.ts b/examples/typescript/http-client-pool/src/main.ts index 722de1e6..696671ea 100644 --- a/examples/typescript/http-client-pool/src/main.ts +++ b/examples/typescript/http-client-pool/src/main.ts @@ -18,9 +18,9 @@ for (const workerFunction of ['node_fetch', 'fetch', 'axios']) { const responses = await Promise.all(httpClientPoolPromises) const elapsedTime = performance.now() - now console.info( - `Received in ${elapsedTime.toFixed(2)}ms an array with ${ - responses.length - } responses from ${parallelism} parallel requests made with HTTP client pool task function ${workerFunction} on ${requestUrl}:\n`, + `Received in ${elapsedTime.toFixed( + 2 + )}ms an array with ${responses.length.toString()} responses from ${parallelism.toString()} parallel requests made with HTTP client pool task function ${workerFunction} on ${requestUrl}:\n`, responses ) } catch (error) { diff --git a/examples/typescript/http-client-pool/src/worker.ts b/examples/typescript/http-client-pool/src/worker.ts index 95917d64..5878523e 100644 --- a/examples/typescript/http-client-pool/src/worker.ts +++ b/examples/typescript/http-client-pool/src/worker.ts @@ -40,6 +40,7 @@ class HttpClientWorker extends ThreadWorker { ...workerData?.axiosRequestConfig, }) return { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment text: response.data, } }, 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 eeb196a6..0b7c45b1 100644 --- a/examples/typescript/http-server-pool/express-cluster/src/main.ts +++ b/examples/typescript/http-server-pool/express-cluster/src/main.ts @@ -21,7 +21,8 @@ const pool = new FixedClusterPool( .then(response => { if (response.status) { console.info( - `Express is listening in cluster worker on port ${response.port}` + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + `Express is listening in cluster worker on port ${response.port?.toString()}` ) } 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 af25b056..6c0aad8c 100644 --- a/examples/typescript/http-server-pool/express-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/express-cluster/src/worker.ts @@ -50,7 +50,7 @@ class ExpressWorker extends ClusterWorker { ExpressWorker.server = application.listen(port, () => { listenerPort = (ExpressWorker.server.address() as AddressInfo).port console.info( - `⚡️[express server]: Express server is started in cluster worker at http://localhost:${listenerPort}/` + `⚡️[express server]: Express server is started in cluster worker at http://localhost:${listenerPort.toString()}/` ) }) return { 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 360cf913..f9364b43 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 @@ -54,6 +54,7 @@ class ExpressWorker extends ClusterWorker< application.all('/api/echo', (req: Request, res: Response) => { ExpressWorker.requestHandlerPool + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment .execute({ data: req.body }, 'echo') .then(response => { return res.send(response.data).end() @@ -75,7 +76,7 @@ class ExpressWorker extends ClusterWorker< ExpressWorker.server = application.listen(port, () => { listenerPort = (ExpressWorker.server.address() as AddressInfo).port console.info( - `⚡️[express server]: Express server is started in cluster worker at http://localhost:${listenerPort}/` + `⚡️[express server]: Express server is started in cluster worker at http://localhost:${listenerPort.toString()}/` ) }) return { 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 1406248f..d7b6c89d 100644 --- a/examples/typescript/http-server-pool/express-hybrid/src/main.ts +++ b/examples/typescript/http-server-pool/express-hybrid/src/main.ts @@ -40,7 +40,8 @@ const pool = new FixedClusterPool( .then(response => { if (response.status) { console.info( - `Express is listening in cluster worker on port ${response.port}` + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + `Express is listening in cluster worker on port ${response.port?.toString()}` ) } return undefined diff --git a/examples/typescript/http-server-pool/express-worker_threads/src/main.ts b/examples/typescript/http-server-pool/express-worker_threads/src/main.ts index e0cb7042..dc70d7ec 100644 --- a/examples/typescript/http-server-pool/express-worker_threads/src/main.ts +++ b/examples/typescript/http-server-pool/express-worker_threads/src/main.ts @@ -23,6 +23,7 @@ expressApp.use(express.json()) expressApp.all('/api/echo', (req: Request, res: Response) => { requestHandlerPool + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment .execute({ body: req.body }, 'echo') .then(response => { return res.send(response.body).end() @@ -43,7 +44,7 @@ expressApp.get('/api/factorial/:number', (req: Request, res: Response) => { try { expressApp.listen(port, () => { console.info( - `⚡️[express server]: Express server is started at http://localhost:${port}/` + `⚡️[express server]: Express server is started at http://localhost:${port.toString()}/` ) }) } catch (err) { 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 794a93b6..a548b13d 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/main.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/main.ts @@ -21,7 +21,8 @@ const pool = new FixedClusterPool( .then(response => { if (response.status) { console.info( - `Fastify is listening in cluster worker on port ${response.port}` + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + `Fastify is listening in cluster worker on port ${response.port?.toString()}` ) } return undefined 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 4622324c..74ed1f70 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/src/main.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/src/main.ts @@ -40,7 +40,8 @@ const pool = new FixedClusterPool( .then(response => { if (response.status) { console.info( - `Fastify is listening in cluster worker on port ${response.port}` + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + `Fastify is listening in cluster worker on port ${response.port?.toString()}` ) } return undefined diff --git a/examples/typescript/pool.ts b/examples/typescript/pool.ts index 5d3d8e75..1be0691a 100644 --- a/examples/typescript/pool.ts +++ b/examples/typescript/pool.ts @@ -45,6 +45,7 @@ const dynamicPool = new DynamicThreadPool( await dynamicPool.execute() +// eslint-disable-next-line @typescript-eslint/no-misused-promises setTimeout(async () => { await fixedPool.destroy() await dynamicPool.destroy() diff --git a/examples/typescript/smtp-client-pool/src/main.ts b/examples/typescript/smtp-client-pool/src/main.ts index e00372dc..4d1f2ea8 100644 --- a/examples/typescript/smtp-client-pool/src/main.ts +++ b/examples/typescript/smtp-client-pool/src/main.ts @@ -32,9 +32,9 @@ try { await Promise.all(smtpClientPoolPromises) const elapsedTime = performance.now() - now console.info( - `Send in parallel in ${elapsedTime.toFixed(2)}ms ${ - tos.length - } mails with SMTP client pool` + `Send in parallel in ${elapsedTime.toFixed( + 2 + )}ms ${tos.length.toString()} mails with SMTP client pool` ) } catch (error) { console.error(error) 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 200afb35..6e108d6f 100644 --- a/examples/typescript/websocket-server-pool/ws-cluster/src/main.ts +++ b/examples/typescript/websocket-server-pool/ws-cluster/src/main.ts @@ -21,7 +21,8 @@ const pool = new FixedClusterPool( .then(response => { if (response.status) { console.info( - `WebSocket server is listening in cluster worker on port ${response.port}` + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + `WebSocket server is listening in cluster worker on port ${response.port?.toString()}` ) } return undefined diff --git a/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts b/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts index a3ab5e28..d9091e7f 100644 --- a/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts +++ b/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts @@ -33,7 +33,7 @@ class WebSocketServerWorker extends ClusterWorker { WebSocketServerWorker.wss = new WebSocketServer({ port }, () => { console.info( - `⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/` + `⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port.toString()}/` ) }) @@ -41,6 +41,7 @@ class WebSocketServerWorker extends ClusterWorker { ws.on('error', console.error) ws.on('message', (message: RawData) => { const { type, data } = JSON.parse( + // eslint-disable-next-line @typescript-eslint/no-base-to-string message.toString() ) as MessagePayload switch (type) { @@ -62,7 +63,7 @@ class WebSocketServerWorker extends ClusterWorker { number: WebSocketServerWorker.factorial(data.number!), }, }, - (_, v) => (typeof v === 'bigint' ? v.toString() : v) + (_, v: unknown) => (typeof v === 'bigint' ? v.toString() : v) ) ) break 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 5ba88051..400f0bee 100644 --- a/examples/typescript/websocket-server-pool/ws-hybrid/src/main.ts +++ b/examples/typescript/websocket-server-pool/ws-hybrid/src/main.ts @@ -40,7 +40,8 @@ const pool = new FixedClusterPool( .then(response => { if (response.status) { console.info( - `WebSocket server is listening in cluster worker on port ${response.port}` + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + `WebSocket server is listening in cluster worker on port ${response.port?.toString()}` ) } return undefined diff --git a/examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts b/examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts index 6aa993d2..72ac2a1b 100644 --- a/examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts +++ b/examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts @@ -48,7 +48,7 @@ class WebSocketServerWorker extends ClusterWorker< WebSocketServerWorker.wss = new WebSocketServer({ port }, () => { console.info( - `⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/` + `⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port.toString()}/` ) }) @@ -56,6 +56,7 @@ class WebSocketServerWorker extends ClusterWorker< ws.on('error', console.error) ws.on('message', (message: RawData) => { const { type, data } = JSON.parse( + // eslint-disable-next-line @typescript-eslint/no-base-to-string message.toString() ) as MessagePayload switch (type) { @@ -83,7 +84,8 @@ class WebSocketServerWorker extends ClusterWorker< type: MessageType.factorial, data: response.data, }, - (_, v) => (typeof v === 'bigint' ? v.toString() : v) + (_, v: unknown) => + typeof v === 'bigint' ? v.toString() : v ) ) return undefined diff --git a/examples/typescript/websocket-server-pool/ws-worker_threads/src/main.ts b/examples/typescript/websocket-server-pool/ws-worker_threads/src/main.ts index 2954cbde..cb28beeb 100644 --- a/examples/typescript/websocket-server-pool/ws-worker_threads/src/main.ts +++ b/examples/typescript/websocket-server-pool/ws-worker_threads/src/main.ts @@ -6,7 +6,7 @@ import { type DataPayload, type MessagePayload, MessageType } from './types.js' const port = 8080 const wss = new WebSocketServer({ port }, () => { console.info( - `⚡️[ws server]: WebSocket server is started at ws://localhost:${port}/` + `⚡️[ws server]: WebSocket server is started at ws://localhost:${port.toString()}/` ) }) @@ -18,6 +18,7 @@ wss.on('connection', ws => { ws.on('error', console.error) ws.on('message', (message: RawData) => { const { type, data } = JSON.parse( + // eslint-disable-next-line @typescript-eslint/no-base-to-string message.toString() ) as MessagePayload switch (type) { @@ -45,7 +46,7 @@ wss.on('connection', ws => { type: MessageType.factorial, data: response.data, }, - (_, v) => (typeof v === 'bigint' ? v.toString() : v) + (_, v: unknown) => (typeof v === 'bigint' ? v.toString() : v) ) ) return undefined diff --git a/src/circular-buffer.ts b/src/circular-buffer.ts index b4a60030..5015483b 100644 --- a/src/circular-buffer.ts +++ b/src/circular-buffer.ts @@ -85,11 +85,13 @@ export class CircularBuffer { private checkSize (size: number): void { if (!Number.isSafeInteger(size)) { throw new TypeError( - `Invalid circular buffer size: '${size}' is not an integer` + `Invalid circular buffer size: '${size.toString()}' is not an integer` ) } if (size < 0) { - throw new RangeError(`Invalid circular buffer size: ${size} < 0`) + throw new RangeError( + `Invalid circular buffer size: ${size.toString()} < 0` + ) } } } diff --git a/src/fixed-priority-queue.ts b/src/fixed-priority-queue.ts index efcc35ca..a3de0209 100644 --- a/src/fixed-priority-queue.ts +++ b/src/fixed-priority-queue.ts @@ -175,11 +175,13 @@ export class FixedPriorityQueue { private checkSize (size: number): void { if (!Number.isSafeInteger(size)) { throw new TypeError( - `Invalid fixed priority queue size: '${size}' is not an integer` + `Invalid fixed priority queue size: '${size.toString()}' is not an integer` ) } if (size < 0) { - throw new RangeError(`Invalid fixed priority queue size: ${size} < 0`) + throw new RangeError( + `Invalid fixed priority queue size: ${size.toString()} < 0` + ) } } } diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index e4dac676..3b676faa 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -658,7 +658,7 @@ export abstract class AbstractPool< throw new Error('Worker message received without worker id') } else if (this.getWorkerNodeKeyByWorkerId(message.workerId) === -1) { throw new Error( - `Worker message received from unknown worker '${message.workerId}'` + `Worker message received from unknown worker '${message.workerId.toString()}'` ) } } @@ -891,7 +891,11 @@ export abstract class AbstractPool< } else { reject( new Error( - `Task function operation '${message.taskFunctionOperation}' failed on worker ${message.workerId} with error: '${message.workerError?.message}'` + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + `Task function operation '${message.taskFunctionOperation?.toString()}' failed on worker ${message.workerId?.toString()} with error: '${ + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + message.workerError?.message + }'` ) ) } @@ -939,7 +943,9 @@ export abstract class AbstractPool< new Error( `Task function operation '${ message.taskFunctionOperation as string - }' failed on worker ${errorResponse?.workerId} with error: '${ + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + }' failed on worker ${errorResponse?.workerId?.toString()} with error: '${ + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions errorResponse?.workerError?.message }'` ) @@ -1284,6 +1290,7 @@ export abstract class AbstractPool< private async sendKillMessageToWorker (workerNodeKey: number): Promise { await new Promise((resolve, reject) => { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (this.workerNodes[workerNodeKey] == null) { resolve() return @@ -1295,7 +1302,8 @@ export abstract class AbstractPool< } else if (message.kill === 'failure') { reject( new Error( - `Kill message handling failed on worker ${message.workerId}` + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + `Kill message handling failed on worker ${message.workerId?.toString()}` ) ) } @@ -1351,6 +1359,7 @@ export abstract class AbstractPool< workerNodeKey: number, task: Task ): void { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (this.workerNodes[workerNodeKey]?.usage != null) { const workerUsage = this.workerNodes[workerNodeKey].usage ++workerUsage.tasks.executing @@ -1391,6 +1400,7 @@ export abstract class AbstractPool< message: MessageValue ): void { let needWorkerChoiceStrategiesUpdate = false + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (this.workerNodes[workerNodeKey]?.usage != null) { const workerUsage = this.workerNodes[workerNodeKey].usage updateTaskStatisticsWorkerUsage(workerUsage, message) @@ -1568,6 +1578,7 @@ export abstract class AbstractPool< ) { this.redistributeQueuedTasks(this.workerNodes.indexOf(workerNode)) } + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition workerNode?.terminate().catch((error: unknown) => { this.emitter?.emit(PoolEvents.error, error) }) @@ -1786,6 +1797,7 @@ export abstract class AbstractPool< taskName: string ): void { const workerNode = this.workerNodes[workerNodeKey] + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (workerNode?.usage != null) { ++workerNode.usage.tasks.stolen } @@ -1804,6 +1816,7 @@ export abstract class AbstractPool< previousTaskName?: string ): void { const workerNode = this.workerNodes[workerNodeKey] + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (workerNode?.usage != null) { ++workerNode.usage.tasks.sequentiallyStolen } @@ -1832,6 +1845,7 @@ export abstract class AbstractPool< taskName: string ): void { const workerNode = this.workerNodes[workerNodeKey] + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (workerNode?.usage != null) { workerNode.usage.tasks.sequentiallyStolen = 0 } @@ -1859,7 +1873,7 @@ export abstract class AbstractPool< const workerInfo = this.getWorkerInfo(workerNodeKey) if (workerInfo == null) { throw new Error( - `Worker node with key '${workerNodeKey}' not found in pool` + `Worker node with key '${workerNodeKey.toString()}' not found in pool` ) } if ( @@ -1975,7 +1989,7 @@ export abstract class AbstractPool< const workerInfo = this.getWorkerInfo(workerNodeKey) if (workerInfo == null) { throw new Error( - `Worker node with key '${workerNodeKey}' not found in pool` + `Worker node with key '${workerNodeKey.toString()}' not found in pool` ) } workerInfo.stealing = true @@ -2032,7 +2046,8 @@ export abstract class AbstractPool< private handleWorkerReadyResponse (message: MessageValue): void { const { workerId, ready, taskFunctionsProperties } = message if (ready == null || !ready) { - throw new Error(`Worker ${workerId} failed to initialize`) + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + throw new Error(`Worker ${workerId?.toString()} failed to initialize`) } const workerNodeKey = this.getWorkerNodeKeyByWorkerId(workerId) const workerNode = this.workerNodes[workerNodeKey] @@ -2068,10 +2083,12 @@ export abstract class AbstractPool< this.afterTaskExecutionHook(workerNodeKey, message) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.promiseResponseMap.delete(taskId!) + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition workerNode?.emit('taskFinished', taskId) if ( this.opts.enableTasksQueue === true && !this.destroying && + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition workerNode != null ) { const workerNodeTasksUsage = workerNode.usage.tasks diff --git a/src/pools/selection-strategies/selection-strategies-utils.ts b/src/pools/selection-strategies/selection-strategies-utils.ts index 007f1e57..1c0b91b4 100644 --- a/src/pools/selection-strategies/selection-strategies-utils.ts +++ b/src/pools/selection-strategies/selection-strategies-utils.ts @@ -33,13 +33,16 @@ const estimatedCpuSpeed = (): number => { const getDefaultWorkerWeight = (): number => { const currentCpus = cpus() let estCpuSpeed: number | undefined + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (currentCpus.every(cpu => cpu.speed == null || cpu.speed === 0)) { estCpuSpeed = estimatedCpuSpeed() } let cpusCycleTimeWeight = 0 for (const cpu of currentCpus) { + // 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 currentCpus.find(cpu => cpu.speed != null && cpu.speed !== 0)?.speed ?? estCpuSpeed ?? 2000 @@ -180,6 +183,7 @@ export const getWorkerChoiceStrategy = ( ))(pool, opts) default: throw new Error( + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `Worker choice strategy '${workerChoiceStrategy}' is not valid` ) } diff --git a/src/pools/selection-strategies/worker-choice-strategies-context.ts b/src/pools/selection-strategies/worker-choice-strategies-context.ts index b7b0ad56..a736896a 100644 --- a/src/pools/selection-strategies/worker-choice-strategies-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategies-context.ts @@ -174,7 +174,7 @@ export class WorkerChoiceStrategiesContext< } while (workerNodeKey == null && retriesCount < this.retries) if (workerNodeKey == null) { throw new Error( - `Worker node key chosen is null or undefined after ${retriesCount} retries` + `Worker node key chosen is null or undefined after ${retriesCount.toString()} retries` ) } return workerNodeKey diff --git a/src/pools/utils.ts b/src/pools/utils.ts index 3ac39b4d..24f2cfb2 100644 --- a/src/pools/utils.ts +++ b/src/pools/utils.ts @@ -89,7 +89,7 @@ export const checkDynamicPoolSize = ( export const checkValidPriority = (priority: number | undefined): void => { if (priority != null && !Number.isSafeInteger(priority)) { - throw new TypeError(`Invalid property 'priority': '${priority}'`) + throw new TypeError(`Invalid property 'priority': '${priority.toString()}'`) } if ( priority != null && @@ -130,7 +130,7 @@ export const checkValidTasksQueueOptions = ( tasksQueueOptions.concurrency <= 0 ) { throw new RangeError( - `Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency} is a negative integer or zero` + `Invalid worker node tasks concurrency: ${tasksQueueOptions.concurrency.toString()} is a negative integer or zero` ) } if ( @@ -143,7 +143,7 @@ export const checkValidTasksQueueOptions = ( } if (tasksQueueOptions?.size != null && tasksQueueOptions.size <= 0) { throw new RangeError( - `Invalid worker node tasks queue size: ${tasksQueueOptions.size} is a negative integer or zero` + `Invalid worker node tasks queue size: ${tasksQueueOptions.size.toString()} is a negative integer or zero` ) } } @@ -261,6 +261,7 @@ const updateMeasurementStatistics = ( } } if (env.NODE_ENV === 'test') { + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access exports.updateMeasurementStatistics = updateMeasurementStatistics } @@ -290,6 +291,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 ) { @@ -377,6 +379,7 @@ export const createWorker = ( case WorkerTypes.cluster: 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/priority-queue.ts b/src/priority-queue.ts index c613cd2e..93e7b76d 100644 --- a/src/priority-queue.ts +++ b/src/priority-queue.ts @@ -40,11 +40,11 @@ export class PriorityQueue { ) { if (!Number.isSafeInteger(bucketSize)) { throw new TypeError( - `Invalid bucket size: '${bucketSize}' is not an integer` + `Invalid bucket size: '${bucketSize.toString()}' is not an integer` ) } if (bucketSize < 0) { - throw new RangeError(`Invalid bucket size: ${bucketSize} < 0`) + throw new RangeError(`Invalid bucket size: ${bucketSize.toString()} < 0`) } this.bucketSize = bucketSize this.head = this.tail = new FixedPriorityQueue( diff --git a/src/utils.ts b/src/utils.ts index af6a432b..b36eba27 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -205,6 +205,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) = diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 9b9ded5e..7803ce2e 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -355,9 +355,10 @@ export abstract class AbstractWorker< switch (taskFunctionOperation) { case 'add': response = this.addTaskFunction(taskFunctionProperties.name, { - // eslint-disable-next-line no-new-func + // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func taskFunction: new Function( - `return ${taskFunction}` + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + `return ${taskFunction!}` )() as TaskFunction, ...(taskFunctionProperties.priority != null && { priority: taskFunctionProperties.priority, @@ -408,7 +409,7 @@ export abstract class AbstractWorker< }) } else { try { - this.opts.killHandler?.() + ;(this.opts.killHandler as (() => void) | undefined)?.() this.sendToMainWorker({ kill: 'success' }) } catch { this.sendToMainWorker({ kill: 'failure' }) @@ -426,7 +427,7 @@ export abstract class AbstractWorker< throw new Error('Message worker id is not set') } else if (message.workerId !== this.id) { throw new Error( - `Message worker id ${message.workerId} does not match the worker id ${this.id}` + `Message worker id ${message.workerId.toString()} does not match the worker id ${this.id.toString()}` ) } } @@ -514,7 +515,8 @@ export abstract class AbstractWorker< workerError: { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion name: name!, - message: `Task function '${name}' not found`, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + message: `Task function '${name!}' not found`, data, }, taskId, @@ -553,7 +555,7 @@ export abstract class AbstractWorker< workerError: { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion name: name!, - message: this.handleError(error as Error | string), + message: this.handleError(error), data, }, taskId, diff --git a/src/worker/utils.ts b/src/worker/utils.ts index f324a235..59a0289b 100644 --- a/src/worker/utils.ts +++ b/src/worker/utils.ts @@ -53,6 +53,7 @@ export const checkValidTaskFunctionObjectEntry = < } if (typeof fnObj.taskFunction !== 'function') { throw new TypeError( + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `taskFunction object 'taskFunction' property '${fnObj.taskFunction}' is not a function` ) } -- 2.34.1