From 67f3f2d6cb8f915ec71f81c4533ab80a6c6a6f0f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 30 Dec 2023 10:40:58 +0100 Subject: [PATCH] refactor: renable standard JS linter rules MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .eslintrc.cjs | 5 +- .lintstagedrc.js | 1 - .prettierrc.json | 2 +- .../typescript/http-client-pool/src/worker.ts | 6 +- .../express-cluster/src/worker.ts | 2 +- .../express-hybrid/src/express-worker.ts | 2 +- .../src/request-handler-worker.ts | 4 +- .../express-worker_threads/src/worker.ts | 4 +- .../fastify-cluster/src/worker.ts | 2 +- .../fastify-hybrid/src/fastify-poolifier.ts | 4 +- .../fastify-hybrid/src/fastify-worker.ts | 2 +- .../src/request-handler-worker.ts | 4 +- .../src/fastify-poolifier.ts | 4 +- .../fastify-worker_threads/src/worker.ts | 4 +- .../typescript/smtp-client-pool/src/worker.ts | 3 +- .../ws-cluster/src/worker.ts | 4 +- .../ws-hybrid/src/request-handler-worker.ts | 4 +- .../ws-hybrid/src/websocket-server-worker.ts | 2 +- .../ws-worker_threads/src/worker.ts | 4 +- package.json | 2 +- pnpm-lock.yaml | 37 ++-- src/deque.ts | 9 +- src/pools/abstract-pool.ts | 173 ++++++++++-------- src/pools/cluster/dynamic.ts | 3 +- src/pools/cluster/fixed.ts | 3 +- .../abstract-worker-choice-strategy.ts | 12 +- .../fair-share-worker-choice-strategy.ts | 12 +- ...hted-round-robin-worker-choice-strategy.ts | 6 +- ...hted-round-robin-worker-choice-strategy.ts | 6 +- .../worker-choice-strategy-context.ts | 43 ++--- src/pools/thread/dynamic.ts | 3 +- src/pools/thread/fixed.ts | 5 +- src/pools/worker-node.ts | 12 +- src/worker/abstract-worker.ts | 34 ++-- src/worker/cluster-worker.ts | 3 +- src/worker/thread-worker.ts | 3 +- 36 files changed, 227 insertions(+), 202 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index ccf04244..522fdce5 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -121,7 +121,7 @@ module.exports = defineConfig({ 'standard-with-typescript' ], rules: { - '@typescript-eslint/non-nullable-type-assertion-style': 'off', + 'operator-linebreak': 'off', 'tsdoc/syntax': 'warn' } }, @@ -151,7 +151,8 @@ module.exports = defineConfig({ '@typescript-eslint/no-unnecessary-type-assertion': 'off', '@typescript-eslint/no-redundant-type-constituents': 'off', '@typescript-eslint/strict-boolean-expressions': 'off', - '@typescript-eslint/return-await': 'off' + // '@typescript-eslint/return-await': 'off', + '@typescript-eslint/no-non-null-assertion': 'off' } }, { diff --git a/.lintstagedrc.js b/.lintstagedrc.js index 1886925b..507f281d 100644 --- a/.lintstagedrc.js +++ b/.lintstagedrc.js @@ -1,7 +1,6 @@ export default { '**/*.{ts,tsx,js,jsx,cjs,mjs}': [ 'biome format --write', - 'ts-standard --fix', 'eslint --cache --fix' ], '**/!(package.json|tsconfig.json)*.json': ['biome format --write'], diff --git a/.prettierrc.json b/.prettierrc.json index 80901f79..890136a1 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -2,5 +2,5 @@ "$schema": "https://json.schemastore.org/prettierrc", "semi": false, "singleQuote": true, - "trailingComma": "none" + "trailingComma": "es5" } diff --git a/examples/typescript/http-client-pool/src/worker.ts b/examples/typescript/http-client-pool/src/worker.ts index 63f77fd7..1e89761f 100644 --- a/examples/typescript/http-client-pool/src/worker.ts +++ b/examples/typescript/http-client-pool/src/worker.ts @@ -11,7 +11,7 @@ class HttpClientWorker extends ThreadWorker { super({ node_fetch: async (workerData?: WorkerData) => { const response = await nodeFetch( - (workerData as WorkerData).input as URL | NodeFetchRequestInfo, + workerData!.input as URL | NodeFetchRequestInfo, workerData?.init as NodeFetchRequestInit ) // The response is not structured-cloneable, so we return the response text body instead. @@ -21,7 +21,7 @@ class HttpClientWorker extends ThreadWorker { }, fetch: async (workerData?: WorkerData) => { const response = await fetch( - (workerData as WorkerData).input as URL | RequestInfo, + workerData!.input as URL | RequestInfo, workerData?.init as RequestInit ) // The response is not structured-cloneable, so we return the response text body instead. @@ -32,7 +32,7 @@ class HttpClientWorker extends ThreadWorker { axios: async (workerData?: WorkerData) => { const response = await axios({ method: 'get', - url: (workerData as WorkerData).input as string, + url: workerData!.input as string, ...workerData?.axiosRequestConfig }) return { 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 940503df..7b708730 100644 --- a/examples/typescript/http-server-pool/express-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/express-cluster/src/worker.ts @@ -17,7 +17,7 @@ class ExpressWorker extends ClusterWorker { private static readonly startExpress = ( workerData?: WorkerData ): WorkerResponse => { - const { port } = workerData as WorkerData + const { port } = workerData! const application: Express = express() 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 c7ca23ad..ac2e34d4 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 @@ -32,7 +32,7 @@ ClusterWorkerResponse workerData?: ClusterWorkerData ): ClusterWorkerResponse => { const { port, workerFile, minWorkers, maxWorkers, ...poolOptions } = - workerData as ClusterWorkerData + workerData! ExpressWorker.requestHandlerPool = new DynamicThreadPool< ThreadWorkerData, diff --git a/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts b/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts index 8c247819..ac4fb97a 100644 --- a/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts +++ b/examples/typescript/http-server-pool/express-hybrid/src/request-handler-worker.ts @@ -24,9 +24,7 @@ class RequestHandlerWorker< factorial: (workerData?: Data) => { return { data: { - number: RequestHandlerWorker.factorial( - workerData?.data?.number as number - ) + number: RequestHandlerWorker.factorial(workerData!.data.number!) } } as unknown as Response } diff --git a/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts b/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts index 3f573647..223ee368 100644 --- a/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts +++ b/examples/typescript/http-server-pool/express-worker_threads/src/worker.ts @@ -24,9 +24,7 @@ class RequestHandlerWorker< factorial: (workerData?: Data) => { return { body: { - number: RequestHandlerWorker.factorial( - workerData?.body?.number as number - ) + number: RequestHandlerWorker.factorial(workerData!.body.number!) } } as unknown as Response } 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 2f6c8f6d..1551cb28 100644 --- a/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-cluster/src/worker.ts @@ -16,7 +16,7 @@ class FastifyWorker extends ClusterWorker { private static readonly startFastify = async ( workerData?: WorkerData ): Promise => { - const { port } = workerData as WorkerData + const { port } = workerData! FastifyWorker.fastify = Fastify({ logger: true diff --git a/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-poolifier.ts b/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-poolifier.ts index 2bdf2040..3c5e31e3 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-poolifier.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/src/fastify-poolifier.ts @@ -22,8 +22,8 @@ const fastifyPoolifierPlugin: FastifyPluginCallback = ( } const { workerFile, minWorkers, maxWorkers, ...poolOptions } = options const pool = new DynamicThreadPool( - minWorkers as number, - maxWorkers as number, + minWorkers!, + maxWorkers!, workerFile, poolOptions ) 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 388730dc..28bf2a01 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 @@ -13,7 +13,7 @@ ClusterWorkerResponse private static readonly startFastify = async ( workerData?: ClusterWorkerData ): Promise => { - const { port, ...fastifyPoolifierOptions } = workerData as ClusterWorkerData + const { port, ...fastifyPoolifierOptions } = workerData! FastifyWorker.fastify = Fastify({ logger: true diff --git a/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts b/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts index 8c247819..ac4fb97a 100644 --- a/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts +++ b/examples/typescript/http-server-pool/fastify-hybrid/src/request-handler-worker.ts @@ -24,9 +24,7 @@ class RequestHandlerWorker< factorial: (workerData?: Data) => { return { data: { - number: RequestHandlerWorker.factorial( - workerData?.data?.number as number - ) + number: RequestHandlerWorker.factorial(workerData!.data.number!) } } as unknown as Response } diff --git a/examples/typescript/http-server-pool/fastify-worker_threads/src/fastify-poolifier.ts b/examples/typescript/http-server-pool/fastify-worker_threads/src/fastify-poolifier.ts index aa9a6d14..cd1909b7 100644 --- a/examples/typescript/http-server-pool/fastify-worker_threads/src/fastify-poolifier.ts +++ b/examples/typescript/http-server-pool/fastify-worker_threads/src/fastify-poolifier.ts @@ -22,8 +22,8 @@ const fastifyPoolifierPlugin: FastifyPluginCallback = ( } const { workerFile, minWorkers, maxWorkers, ...poolOptions } = options const pool = new DynamicThreadPool( - minWorkers as number, - maxWorkers as number, + minWorkers!, + maxWorkers!, workerFile, poolOptions ) diff --git a/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts b/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts index 3f573647..223ee368 100644 --- a/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts +++ b/examples/typescript/http-server-pool/fastify-worker_threads/src/worker.ts @@ -24,9 +24,7 @@ class RequestHandlerWorker< factorial: (workerData?: Data) => { return { body: { - number: RequestHandlerWorker.factorial( - workerData?.body?.number as number - ) + number: RequestHandlerWorker.factorial(workerData!.body.number!) } } as unknown as Response } diff --git a/examples/typescript/smtp-client-pool/src/worker.ts b/examples/typescript/smtp-client-pool/src/worker.ts index 448575d1..01e49b09 100644 --- a/examples/typescript/smtp-client-pool/src/worker.ts +++ b/examples/typescript/smtp-client-pool/src/worker.ts @@ -1,6 +1,5 @@ import { ThreadWorker } from 'poolifier' import { createTransport } from 'nodemailer' -import type Mail from 'nodemailer/lib/mailer/index.js' import type SMTPTransport from 'nodemailer/lib/smtp-transport/index.js' import type { WorkerData } from './types.js' @@ -12,7 +11,7 @@ SMTPTransport.SentMessageInfo super({ nodemailer: async (workerData?: WorkerData) => { return await createTransport(workerData?.smtpTransport).sendMail( - workerData?.mail as Mail.Options + workerData!.mail ) } }) 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 0e8dc20a..18ece026 100644 --- a/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts +++ b/examples/typescript/websocket-server-pool/ws-cluster/src/worker.ts @@ -21,7 +21,7 @@ class WebSocketServerWorker extends ClusterWorker { private static readonly startWebSocketServer = ( workerData?: WorkerData ): WorkerResponse => { - const { port } = workerData as WorkerData + const { port } = workerData! WebSocketServerWorker.wss = new WebSocketServer({ port }, () => { console.info( @@ -50,7 +50,7 @@ class WebSocketServerWorker extends ClusterWorker { JSON.stringify({ type: MessageType.factorial, data: { - number: WebSocketServerWorker.factorial(data.number as number) + number: WebSocketServerWorker.factorial(data.number!) } }) ) diff --git a/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts b/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts index 8c247819..ac4fb97a 100644 --- a/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts +++ b/examples/typescript/websocket-server-pool/ws-hybrid/src/request-handler-worker.ts @@ -24,9 +24,7 @@ class RequestHandlerWorker< factorial: (workerData?: Data) => { return { data: { - number: RequestHandlerWorker.factorial( - workerData?.data?.number as number - ) + number: RequestHandlerWorker.factorial(workerData!.data.number!) } } as unknown as Response } 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 46745d96..4e6529e0 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 @@ -32,7 +32,7 @@ ClusterWorkerResponse workerData?: ClusterWorkerData ): ClusterWorkerResponse => { const { port, workerFile, minWorkers, maxWorkers, ...poolOptions } = - workerData as ClusterWorkerData + workerData! WebSocketServerWorker.requestHandlerPool = new DynamicThreadPool< ThreadWorkerData, diff --git a/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts b/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts index 6f9f0914..a3b106c6 100644 --- a/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts +++ b/examples/typescript/websocket-server-pool/ws-worker_threads/src/worker.ts @@ -24,9 +24,7 @@ class RequestHandlerWorker< factorial: (workerData?: Data) => { return { data: { - number: RequestHandlerWorker.factorial( - workerData?.data?.number as number - ) + number: RequestHandlerWorker.factorial(workerData!.data.number!) } } as unknown as Response } diff --git a/package.json b/package.json index e725f6f1..d057f36f 100644 --- a/package.json +++ b/package.json @@ -131,7 +131,7 @@ "eslint-import-resolver-typescript": "^3.6.1", "eslint-plugin-import": "^2.29.1", "eslint-plugin-jsdoc": "^46.9.1", - "eslint-plugin-n": "^16.5.0", + "eslint-plugin-n": "^16.6.0", "eslint-plugin-promise": "^6.1.1", "eslint-plugin-spellcheck": "^0.0.20", "eslint-plugin-tsdoc": "^0.2.17", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b15b576f..d2f1f525 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -52,10 +52,10 @@ devDependencies: version: 8.56.0 eslint-config-standard: specifier: ^17.1.0 - version: 17.1.0(eslint-plugin-import@2.29.1)(eslint-plugin-n@16.5.0)(eslint-plugin-promise@6.1.1)(eslint@8.56.0) + version: 17.1.0(eslint-plugin-import@2.29.1)(eslint-plugin-n@16.6.0)(eslint-plugin-promise@6.1.1)(eslint@8.56.0) eslint-config-standard-with-typescript: specifier: ^43.0.0 - version: 43.0.0(@typescript-eslint/eslint-plugin@6.16.0)(eslint-plugin-import@2.29.1)(eslint-plugin-n@16.5.0)(eslint-plugin-promise@6.1.1)(eslint@8.56.0)(typescript@5.3.3) + version: 43.0.0(@typescript-eslint/eslint-plugin@6.16.0)(eslint-plugin-import@2.29.1)(eslint-plugin-n@16.6.0)(eslint-plugin-promise@6.1.1)(eslint@8.56.0)(typescript@5.3.3) eslint-define-config: specifier: ^2.1.0 version: 2.1.0 @@ -69,8 +69,8 @@ devDependencies: specifier: ^46.9.1 version: 46.9.1(eslint@8.56.0) eslint-plugin-n: - specifier: ^16.5.0 - version: 16.5.0(eslint@8.56.0) + specifier: ^16.6.0 + version: 16.6.0(eslint@8.56.0) eslint-plugin-promise: specifier: ^6.1.1 version: 6.1.1(eslint@8.56.0) @@ -1298,16 +1298,16 @@ packages: through: 2.3.8 dev: true - /acorn-jsx@5.3.2(acorn@8.11.2): + /acorn-jsx@5.3.2(acorn@8.11.3): resolution: {integrity: sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==} peerDependencies: acorn: ^6.0.0 || ^7.0.0 || ^8.0.0 dependencies: - acorn: 8.11.2 + acorn: 8.11.3 dev: true - /acorn@8.11.2: - resolution: {integrity: sha512-nc0Axzp/0FILLEVsm4fNwLCwMttvhEI263QtVPQcbpfZZ3ts0hLsZGOpE6czNlid7CJ9MlyH8reXkpsf3YUY4w==} + /acorn@8.11.3: + resolution: {integrity: sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==} engines: {node: '>=0.4.0'} hasBin: true dev: true @@ -2458,7 +2458,7 @@ packages: - supports-color dev: true - /eslint-config-standard-with-typescript@43.0.0(@typescript-eslint/eslint-plugin@6.16.0)(eslint-plugin-import@2.29.1)(eslint-plugin-n@16.5.0)(eslint-plugin-promise@6.1.1)(eslint@8.56.0)(typescript@5.3.3): + /eslint-config-standard-with-typescript@43.0.0(@typescript-eslint/eslint-plugin@6.16.0)(eslint-plugin-import@2.29.1)(eslint-plugin-n@16.6.0)(eslint-plugin-promise@6.1.1)(eslint@8.56.0)(typescript@5.3.3): resolution: {integrity: sha512-AT0qK01M5bmsWiE3UZvaQO5da1y1n6uQckAKqGNe6zPW5IOzgMLXZxw77nnFm+C11nxAZXsCPrbsgJhSrGfX6Q==} peerDependencies: '@typescript-eslint/eslint-plugin': ^6.4.0 @@ -2471,9 +2471,9 @@ packages: '@typescript-eslint/eslint-plugin': 6.16.0(@typescript-eslint/parser@6.16.0)(eslint@8.56.0)(typescript@5.3.3) '@typescript-eslint/parser': 6.16.0(eslint@8.56.0)(typescript@5.3.3) eslint: 8.56.0 - eslint-config-standard: 17.1.0(eslint-plugin-import@2.29.1)(eslint-plugin-n@16.5.0)(eslint-plugin-promise@6.1.1)(eslint@8.56.0) + eslint-config-standard: 17.1.0(eslint-plugin-import@2.29.1)(eslint-plugin-n@16.6.0)(eslint-plugin-promise@6.1.1)(eslint@8.56.0) eslint-plugin-import: 2.29.1(@typescript-eslint/parser@6.16.0)(eslint-import-resolver-typescript@3.6.1)(eslint@8.56.0) - eslint-plugin-n: 16.5.0(eslint@8.56.0) + eslint-plugin-n: 16.6.0(eslint@8.56.0) eslint-plugin-promise: 6.1.1(eslint@8.56.0) typescript: 5.3.3 transitivePeerDependencies: @@ -2494,7 +2494,7 @@ packages: eslint-plugin-promise: 6.1.1(eslint@8.56.0) dev: true - /eslint-config-standard@17.1.0(eslint-plugin-import@2.29.1)(eslint-plugin-n@16.5.0)(eslint-plugin-promise@6.1.1)(eslint@8.56.0): + /eslint-config-standard@17.1.0(eslint-plugin-import@2.29.1)(eslint-plugin-n@16.6.0)(eslint-plugin-promise@6.1.1)(eslint@8.56.0): resolution: {integrity: sha512-IwHwmaBNtDK4zDHQukFDW5u/aTb8+meQWZvNFWkiGmbWjD6bqyuSSBxxXKkCftCUzc1zwCH2m/baCNDLGmuO5Q==} engines: {node: '>=12.0.0'} peerDependencies: @@ -2505,7 +2505,7 @@ packages: dependencies: eslint: 8.56.0 eslint-plugin-import: 2.29.1(@typescript-eslint/parser@6.16.0)(eslint-import-resolver-typescript@3.6.1)(eslint@8.56.0) - eslint-plugin-n: 16.5.0(eslint@8.56.0) + eslint-plugin-n: 16.6.0(eslint@8.56.0) eslint-plugin-promise: 6.1.1(eslint@8.56.0) dev: true @@ -2737,8 +2737,8 @@ packages: semver: 7.5.4 dev: true - /eslint-plugin-n@16.5.0(eslint@8.56.0): - resolution: {integrity: sha512-Hw02Bj1QrZIlKyj471Tb1jSReTl4ghIMHGuBGiMVmw+s0jOPbI4CBuYpGbZr+tdQ+VAvSK6FDSta3J4ib/SKHQ==} + /eslint-plugin-n@16.6.0(eslint@8.56.0): + resolution: {integrity: sha512-Ag3tYFF90lYU8JdHEl9qSSpeLYbVnO+Oj7sgPUarWUacv1mPL3d5h5yG4Bv3tLe71hrcxmgTi7oByYwKXaVatw==} engines: {node: '>=16.0.0'} peerDependencies: eslint: '>=7.0.0' @@ -2748,6 +2748,7 @@ packages: eslint: 8.56.0 eslint-plugin-es-x: 7.5.0(eslint@8.56.0) get-tsconfig: 4.7.2 + globals: 13.24.0 ignore: 5.3.0 is-builtin-module: 3.2.1 is-core-module: 2.13.1 @@ -2907,8 +2908,8 @@ packages: resolution: {integrity: sha512-oruZaFkjorTpF32kDSI5/75ViwGeZginGGy2NoOSg3Q9bnwlnmDm4HLnkl0RE3n+njDXR037aY1+x58Z/zFdwQ==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} dependencies: - acorn: 8.11.2 - acorn-jsx: 5.3.2(acorn@8.11.2) + acorn: 8.11.3 + acorn-jsx: 5.3.2(acorn@8.11.3) eslint-visitor-keys: 3.4.3 dev: true @@ -6052,7 +6053,7 @@ packages: hasBin: true dependencies: '@jridgewell/source-map': 0.3.5 - acorn: 8.11.2 + acorn: 8.11.3 commander: 2.20.3 source-map-support: 0.5.21 dev: true diff --git a/src/deque.ts b/src/deque.ts index 723f1a49..00f60e6f 100644 --- a/src/deque.ts +++ b/src/deque.ts @@ -79,7 +79,8 @@ export class Deque { return } const tail = this.tail - this.tail = (this.tail as Node).prev + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.tail = this.tail!.prev if (this.tail == null) { delete this.head } else { @@ -155,7 +156,8 @@ export class Deque { value: node.data, done: false } - node = node.next as Node + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + node = node.next! return ret } } @@ -183,7 +185,8 @@ export class Deque { value: node.data, done: false } - node = node.prev as Node + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + node = node.prev! return ret } } diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index e210fa0f..31e73db1 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -35,11 +35,9 @@ import { import type { IWorker, IWorkerNode, - TaskStatistics, WorkerInfo, WorkerNodeEventDetail, - WorkerType, - WorkerUsage + WorkerType } from './worker.js' import { Measurements, @@ -212,13 +210,13 @@ export abstract class AbstractPool< private checkPoolOptions (opts: PoolOptions): void { if (isPlainObject(opts)) { this.opts.startWorkers = opts.startWorkers ?? true - checkValidWorkerChoiceStrategy( - opts.workerChoiceStrategy as WorkerChoiceStrategy - ) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + checkValidWorkerChoiceStrategy(opts.workerChoiceStrategy!) this.opts.workerChoiceStrategy = opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN this.checkValidWorkerChoiceStrategyOptions( - opts.workerChoiceStrategyOptions as WorkerChoiceStrategyOptions + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + opts.workerChoiceStrategyOptions! ) if (opts.workerChoiceStrategyOptions != null) { this.opts.workerChoiceStrategyOptions = opts.workerChoiceStrategyOptions @@ -227,9 +225,11 @@ export abstract class AbstractPool< this.opts.enableEvents = opts.enableEvents ?? true this.opts.enableTasksQueue = opts.enableTasksQueue ?? false if (this.opts.enableTasksQueue) { - checkValidTasksQueueOptions(opts.tasksQueueOptions as TasksQueueOptions) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + checkValidTasksQueueOptions(opts.tasksQueueOptions!) this.opts.tasksQueueOptions = this.buildTasksQueueOptions( - opts.tasksQueueOptions as TasksQueueOptions + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + opts.tasksQueueOptions! ) } } else { @@ -283,7 +283,8 @@ export abstract class AbstractPool< worker: this.worker, started: this.started, ready: this.ready, - strategy: this.opts.workerChoiceStrategy as WorkerChoiceStrategy, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + strategy: this.opts.workerChoiceStrategy!, minSize: this.minimumNumberOfWorkers, maxSize: this.maximumNumberOfWorkers ?? this.minimumNumberOfWorkers, ...(this.workerChoiceStrategyContext?.getTaskStatisticsRequirements() @@ -559,7 +560,8 @@ export abstract class AbstractPool< this.flushTasksQueues() } this.opts.enableTasksQueue = enable - this.setTasksQueueOptions(tasksQueueOptions as TasksQueueOptions) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.setTasksQueueOptions(tasksQueueOptions!) } /** @inheritDoc */ @@ -568,7 +570,8 @@ export abstract class AbstractPool< checkValidTasksQueueOptions(tasksQueueOptions) this.opts.tasksQueueOptions = this.buildTasksQueueOptions(tasksQueueOptions) - this.setTasksQueueSize(this.opts.tasksQueueOptions.size as number) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.setTasksQueueSize(this.opts.tasksQueueOptions.size!) if (this.opts.tasksQueueOptions.taskStealing === true) { this.unsetTaskStealing() this.setTaskStealing() @@ -670,7 +673,8 @@ export abstract class AbstractPool< workerNode => workerNode.info.ready && workerNode.usage.tasks.executing < - (this.opts.tasksQueueOptions?.concurrency as number) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.opts.tasksQueueOptions!.concurrency! ) === -1 ) } @@ -686,7 +690,8 @@ export abstract class AbstractPool< if (this.opts.enableTasksQueue === true) { return ( this.workerNodes[workerNodeKey].usage.tasks.executing >= - (this.opts.tasksQueueOptions?.concurrency as number) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.opts.tasksQueueOptions!.concurrency! ) } return this.workerNodes[workerNodeKey].usage.tasks.executing > 0 @@ -701,7 +706,8 @@ export abstract class AbstractPool< message: MessageValue ): void => { this.checkMessageWorkerId(message) - const workerId = this.getWorkerInfo(workerNodeKey).id as number + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const workerId = this.getWorkerInfo(workerNodeKey).id! if ( message.taskFunctionOperationStatus != null && message.workerId === workerId @@ -713,8 +719,10 @@ 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 ${message.workerId} with error: '${ - message.workerError?.message as string + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + message.workerError!.message }'` ) ) @@ -763,10 +771,11 @@ export abstract class AbstractPool< new Error( `Task function operation '${ message.taskFunctionOperation as string - }' failed on worker ${ - errorResponse?.workerId as number - } with error: '${ - errorResponse?.workerError?.message 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 }'` ) ) @@ -871,7 +880,8 @@ export abstract class AbstractPool< return ( this.tasksQueueSize(workerNodeKey) === 0 && this.workerNodes[workerNodeKey].usage.tasks.executing < - (this.opts.tasksQueueOptions?.concurrency as number) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.opts.tasksQueueOptions!.concurrency! ) } @@ -916,7 +926,8 @@ export abstract class AbstractPool< timestamp, taskId: randomUUID() } - this.promiseResponseMap.set(task.taskId as string, { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.promiseResponseMap.set(task.taskId!, { resolve, reject, workerNodeKey, @@ -1002,9 +1013,8 @@ export abstract class AbstractPool< } else if (message.kill === 'failure') { reject( new Error( - `Kill message handling failed on worker ${ - message.workerId as number - }` + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + `Kill message handling failed on worker ${message.workerId!}` ) ) } @@ -1074,13 +1084,15 @@ export abstract class AbstractPool< } if ( this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) && - this.workerNodes[workerNodeKey].getTaskFunctionWorkerUsage( - task.name as string - ) != null + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.workerNodes[workerNodeKey].getTaskFunctionWorkerUsage(task.name!) != + null ) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const taskFunctionWorkerUsage = this.workerNodes[ workerNodeKey - ].getTaskFunctionWorkerUsage(task.name as string) as WorkerUsage + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + ].getTaskFunctionWorkerUsage(task.name!)! ++taskFunctionWorkerUsage.tasks.executing updateWaitTimeWorkerUsage( this.workerChoiceStrategyContext, @@ -1120,14 +1132,15 @@ export abstract class AbstractPool< if ( this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) && this.workerNodes[workerNodeKey].getTaskFunctionWorkerUsage( - message.taskPerformance?.name as string + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + message.taskPerformance!.name ) != null ) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const taskFunctionWorkerUsage = this.workerNodes[ workerNodeKey - ].getTaskFunctionWorkerUsage( - message.taskPerformance?.name as string - ) as WorkerUsage + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + ].getTaskFunctionWorkerUsage(message.taskPerformance!.name)! updateTaskStatisticsWorkerUsage(taskFunctionWorkerUsage, message) updateRunTimeWorkerUsage( this.workerChoiceStrategyContext, @@ -1436,7 +1449,8 @@ export abstract class AbstractPool< ) this.handleTask( destinationWorkerNodeKey, - this.dequeueTask(workerNodeKey) as Task + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.dequeueTask(workerNodeKey)! ) } } @@ -1453,9 +1467,9 @@ export abstract class AbstractPool< this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) && workerNode.getTaskFunctionWorkerUsage(taskName) != null ) { - const taskFunctionWorkerUsage = workerNode.getTaskFunctionWorkerUsage( - taskName - ) as WorkerUsage + const taskFunctionWorkerUsage = + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + workerNode.getTaskFunctionWorkerUsage(taskName)! ++taskFunctionWorkerUsage.tasks.stolen } } @@ -1478,9 +1492,9 @@ export abstract class AbstractPool< this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) && workerNode.getTaskFunctionWorkerUsage(taskName) != null ) { - const taskFunctionWorkerUsage = workerNode.getTaskFunctionWorkerUsage( - taskName - ) as WorkerUsage + const taskFunctionWorkerUsage = + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + workerNode.getTaskFunctionWorkerUsage(taskName)! ++taskFunctionWorkerUsage.tasks.sequentiallyStolen } } @@ -1503,9 +1517,9 @@ export abstract class AbstractPool< this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) && workerNode.getTaskFunctionWorkerUsage(taskName) != null ) { - const taskFunctionWorkerUsage = workerNode.getTaskFunctionWorkerUsage( - taskName - ) as WorkerUsage + const taskFunctionWorkerUsage = + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + workerNode.getTaskFunctionWorkerUsage(taskName)! taskFunctionWorkerUsage.tasks.sequentiallyStolen = 0 } } @@ -1522,8 +1536,8 @@ export abstract class AbstractPool< } if ( this.cannotStealTask() || - (this.info.stealingWorkerNodes as number) > - Math.floor(this.workerNodes.length / 2) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.info.stealingWorkerNodes! > Math.floor(this.workerNodes.length / 2) ) { if (previousStolenTask != null) { this.getWorkerInfo(workerNodeKey).stealing = false @@ -1538,8 +1552,9 @@ export abstract class AbstractPool< this.tasksQueueSize(workerNodeKey) > 0) ) { this.getWorkerInfo(workerNodeKey).stealing = false + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion for (const taskName of this.workerNodes[workerNodeKey].info - .taskFunctionNames as string[]) { + .taskFunctionNames!) { this.resetTaskSequentiallyStolenStatisticsTaskFunctionWorkerUsage( workerNodeKey, taskName @@ -1554,10 +1569,11 @@ export abstract class AbstractPool< this.shallUpdateTaskFunctionWorkerUsage(workerNodeKey) && stolenTask != null ) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const taskFunctionTasksWorkerUsage = this.workerNodes[ workerNodeKey - ].getTaskFunctionWorkerUsage(stolenTask.name as string) - ?.tasks as TaskStatistics + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + ].getTaskFunctionWorkerUsage(stolenTask.name!)!.tasks if ( taskFunctionTasksWorkerUsage.sequentiallyStolen === 0 || (previousStolenTask != null && @@ -1566,12 +1582,14 @@ export abstract class AbstractPool< ) { this.updateTaskSequentiallyStolenStatisticsTaskFunctionWorkerUsage( workerNodeKey, - stolenTask.name as string + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + stolenTask.name! ) } else { this.resetTaskSequentiallyStolenStatisticsTaskFunctionWorkerUsage( workerNodeKey, - stolenTask.name as string + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + stolenTask.name! ) } } @@ -1600,13 +1618,12 @@ export abstract class AbstractPool< sourceWorkerNode.usage.tasks.queued > 0 ) if (sourceWorkerNode != null) { - const task = sourceWorkerNode.popTask() as Task + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const task = sourceWorkerNode.popTask()! this.handleTask(workerNodeKey, task) this.updateTaskSequentiallyStolenStatisticsWorkerUsage(workerNodeKey) - this.updateTaskStolenStatisticsWorkerUsage( - workerNodeKey, - task.name as string - ) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.updateTaskStolenStatisticsWorkerUsage(workerNodeKey, task.name!) return task } } @@ -1616,14 +1633,15 @@ export abstract class AbstractPool< ): void => { if ( this.cannotStealTask() || - (this.info.stealingWorkerNodes as number) > - Math.floor(this.workerNodes.length / 2) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.info.stealingWorkerNodes! > Math.floor(this.workerNodes.length / 2) ) { return } const { workerId } = eventDetail const sizeOffset = 1 - if ((this.opts.tasksQueueOptions?.size as number) <= sizeOffset) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + if (this.opts.tasksQueueOptions!.size! <= sizeOffset) { return } const sourceWorkerNode = @@ -1641,15 +1659,15 @@ export abstract class AbstractPool< !workerNode.info.stealing && workerNode.info.id !== workerId && workerNode.usage.tasks.queued < - (this.opts.tasksQueueOptions?.size as number) - sizeOffset + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.opts.tasksQueueOptions!.size! - sizeOffset ) { this.getWorkerInfo(workerNodeKey).stealing = true - const task = sourceWorkerNode.popTask() as Task + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const task = sourceWorkerNode.popTask()! this.handleTask(workerNodeKey, task) - this.updateTaskStolenStatisticsWorkerUsage( - workerNodeKey, - task.name as string - ) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.updateTaskStolenStatisticsWorkerUsage(workerNodeKey, task.name!) this.getWorkerInfo(workerNodeKey).stealing = false } } @@ -1680,7 +1698,8 @@ export abstract class AbstractPool< private handleWorkerReadyResponse (message: MessageValue): void { const { workerId, ready, taskFunctionNames } = message if (ready === false) { - throw new Error(`Worker ${workerId as number} failed to initialize`) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + throw new Error(`Worker ${workerId!} failed to initialize`) } const workerInfo = this.getWorkerInfo( this.getWorkerNodeKeyByWorkerId(workerId) @@ -1695,7 +1714,8 @@ export abstract class AbstractPool< private handleTaskExecutionResponse (message: MessageValue): void { const { workerId, taskId, workerError, data } = message - const promiseResponse = this.promiseResponseMap.get(taskId as string) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const promiseResponse = this.promiseResponseMap.get(taskId!) if (promiseResponse != null) { const { resolve, reject, workerNodeKey, asyncResource } = promiseResponse const workerNode = this.workerNodes[workerNodeKey] @@ -1715,19 +1735,19 @@ export abstract class AbstractPool< } asyncResource?.emitDestroy() this.afterTaskExecutionHook(workerNodeKey, message) - this.promiseResponseMap.delete(taskId as string) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.promiseResponseMap.delete(taskId!) workerNode?.emit('taskFinished', taskId) if (this.opts.enableTasksQueue === true && !this.destroying) { const workerNodeTasksUsage = workerNode.usage.tasks if ( this.tasksQueueSize(workerNodeKey) > 0 && workerNodeTasksUsage.executing < - (this.opts.tasksQueueOptions?.concurrency as number) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.opts.tasksQueueOptions!.concurrency! ) { - this.executeTask( - workerNodeKey, - this.dequeueTask(workerNodeKey) as Task - ) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.executeTask(workerNodeKey, this.dequeueTask(workerNodeKey)!) } if ( workerNodeTasksUsage.executing === 0 && @@ -1735,7 +1755,8 @@ export abstract class AbstractPool< workerNodeTasksUsage.sequentiallyStolen === 0 ) { workerNode.emit('idleWorkerNode', { - workerId: workerId as number, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + workerId: workerId!, workerNodeKey }) } @@ -1875,10 +1896,8 @@ export abstract class AbstractPool< protected flushTasksQueue (workerNodeKey: number): number { let flushedTasks = 0 while (this.tasksQueueSize(workerNodeKey) > 0) { - this.executeTask( - workerNodeKey, - this.dequeueTask(workerNodeKey) as Task - ) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.executeTask(workerNodeKey, this.dequeueTask(workerNodeKey)!) ++flushedTasks } this.workerNodes[workerNodeKey].clearTasksQueue() diff --git a/src/pools/cluster/dynamic.ts b/src/pools/cluster/dynamic.ts index 311d5c9d..8ef11c00 100644 --- a/src/pools/cluster/dynamic.ts +++ b/src/pools/cluster/dynamic.ts @@ -34,7 +34,8 @@ export class DynamicClusterPool< super(min, filePath, opts, max) checkDynamicPoolSize( this.minimumNumberOfWorkers, - this.maximumNumberOfWorkers as number + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.maximumNumberOfWorkers! ) } diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 3b45dc17..aa731562 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -54,7 +54,8 @@ export class FixedClusterPool< ): void { this.workerNodes[workerNodeKey].worker.send({ ...message, - workerId: this.getWorkerInfo(workerNodeKey).id as number + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + 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 49b7c584..daed6b70 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -70,15 +70,18 @@ export abstract class AbstractWorkerChoiceStrategy< ): void { this.toggleMedianMeasurementStatisticsRequirements( this.taskStatisticsRequirements.runTime, - opts.runTime?.median as boolean + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + opts.runTime!.median ) this.toggleMedianMeasurementStatisticsRequirements( this.taskStatisticsRequirements.waitTime, - opts.waitTime?.median as boolean + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + opts.waitTime!.median ) this.toggleMedianMeasurementStatisticsRequirements( this.taskStatisticsRequirements.elu, - opts.elu?.median as boolean + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + opts.elu!.median ) } @@ -141,7 +144,8 @@ export abstract class AbstractWorkerChoiceStrategy< * Check the next worker node readiness. */ protected checkNextWorkerNodeReadiness (): void { - if (!this.isWorkerNodeReady(this.nextWorkerNodeKey as number)) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + if (!this.isWorkerNodeReady(this.nextWorkerNodeKey!)) { delete this.nextWorkerNodeKey } } diff --git a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts index c6b87ade..e8f9cc61 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -1,6 +1,6 @@ import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../../utils.js' import type { IPool } from '../pool.js' -import type { IWorker, StrategyData } from '../worker.js' +import type { IWorker } from '../worker.js' import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js' import { type IWorkerChoiceStrategy, @@ -87,9 +87,10 @@ export class FairShareWorkerChoiceStrategy< } } return this.isWorkerNodeReady(workerNodeKey) && - (workerNode.strategyData.virtualTaskEndTimestamp as number) < - ((workerNodes[minWorkerNodeKey].strategyData as StrategyData) - .virtualTaskEndTimestamp as number) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + workerNode.strategyData.virtualTaskEndTimestamp! < + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + workerNodes[minWorkerNodeKey].strategyData!.virtualTaskEndTimestamp! ? workerNodeKey : minWorkerNodeKey }, @@ -131,7 +132,8 @@ export class FairShareWorkerChoiceStrategy< ?.virtualTaskEndTimestamp const now = performance.now() return now < (virtualTaskEndTimestamp ?? -Infinity) - ? (virtualTaskEndTimestamp as number) + ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + virtualTaskEndTimestamp! : now } } 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 db439f02..fe4b4883 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 @@ -94,7 +94,8 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< ) { this.workerNodeVirtualTaskRunTime = 0 } - const workerWeight = this.opts.weights?.[workerNodeKey] as number + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const workerWeight = this.opts.weights![workerNodeKey]! if ( this.isWorkerNodeReady(workerNodeKey) && workerWeight >= this.roundWeights[roundIndex] && @@ -156,7 +157,8 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy< private getRoundWeights (): number[] { return [ ...new Set( - Object.values(this.opts.weights as Record) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + Object.values(this.opts.weights!) .slice() .sort((a, b) => a - b) ) 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 4b7eac7a..5ccc9c32 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 @@ -89,9 +89,9 @@ export class WeightedRoundRobinWorkerChoiceStrategy< } private weightedRoundRobinNextWorkerNodeKey (): number | undefined { - const workerWeight = this.opts.weights?.[ - this.nextWorkerNodeKey ?? this.previousWorkerNodeKey - ] as number + const workerWeight = + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.opts.weights![this.nextWorkerNodeKey ?? this.previousWorkerNodeKey]! if (this.workerNodeVirtualTaskRunTime < workerWeight) { this.workerNodeVirtualTaskRunTime = this.workerNodeVirtualTaskRunTime + diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index d76ee8af..330e584d 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -115,11 +115,9 @@ export class WorkerChoiceStrategyContext< * @returns The strategy policy. */ public getStrategyPolicy (): StrategyPolicy { - return ( - this.workerChoiceStrategies.get( - this.workerChoiceStrategy - ) as IWorkerChoiceStrategy - ).strategyPolicy + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return this.workerChoiceStrategies.get(this.workerChoiceStrategy)! + .strategyPolicy } /** @@ -128,11 +126,9 @@ export class WorkerChoiceStrategyContext< * @returns The task statistics requirements. */ public getTaskStatisticsRequirements (): TaskStatisticsRequirements { - return ( - this.workerChoiceStrategies.get( - this.workerChoiceStrategy - ) as IWorkerChoiceStrategy - ).taskStatisticsRequirements + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return this.workerChoiceStrategies.get(this.workerChoiceStrategy)! + .taskStatisticsRequirements } /** @@ -155,11 +151,10 @@ export class WorkerChoiceStrategyContext< * @returns `true` if the update is successful, `false` otherwise. */ public update (workerNodeKey: number): boolean { - return ( - this.workerChoiceStrategies.get( - this.workerChoiceStrategy - ) as IWorkerChoiceStrategy - ).update(workerNodeKey) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return this.workerChoiceStrategies + .get(this.workerChoiceStrategy)! + .update(workerNodeKey) } /** @@ -169,9 +164,10 @@ export class WorkerChoiceStrategyContext< * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined. */ public execute (): number { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const workerChoiceStrategy = this.workerChoiceStrategies.get( this.workerChoiceStrategy - ) as IWorkerChoiceStrategy + )! if (!workerChoiceStrategy.hasPoolWorkerNodesReady()) { return this.execute() } @@ -195,10 +191,8 @@ export class WorkerChoiceStrategyContext< retriesCount++ } chooseCount++ - } while ( - workerNodeKey == null && - retriesCount < (this.opts?.retries as number) - ) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + } while (workerNodeKey == null && retriesCount < this.opts!.retries!) if (workerNodeKey == null) { throw new Error( `Worker node key chosen is null or undefined after ${retriesCount} retries` @@ -214,11 +208,10 @@ export class WorkerChoiceStrategyContext< * @returns `true` if the removal is successful, `false` otherwise. */ public remove (workerNodeKey: number): boolean { - return ( - this.workerChoiceStrategies.get( - this.workerChoiceStrategy - ) as IWorkerChoiceStrategy - ).remove(workerNodeKey) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return this.workerChoiceStrategies + .get(this.workerChoiceStrategy)! + .remove(workerNodeKey) } /** diff --git a/src/pools/thread/dynamic.ts b/src/pools/thread/dynamic.ts index 2fc36081..2a7d0149 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -34,7 +34,8 @@ export class DynamicThreadPool< super(min, filePath, opts, max) checkDynamicPoolSize( this.minimumNumberOfWorkers, - this.maximumNumberOfWorkers as number + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.maximumNumberOfWorkers! ) } diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index 11a24e27..c4256bf3 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -1,5 +1,4 @@ import { - type MessageChannel, type MessagePort, type TransferListItem, type Worker, @@ -63,8 +62,8 @@ export class FixedThreadPool< /** @inheritDoc */ protected sendStartupMessageToWorker (workerNodeKey: number): void { const workerNode = this.workerNodes[workerNodeKey] - const port2: MessagePort = (workerNode.messageChannel as MessageChannel) - .port2 + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const port2: MessagePort = workerNode.messageChannel!.port2 workerNode.worker.postMessage( { ready: false, diff --git a/src/pools/worker-node.ts b/src/pools/worker-node.ts index bf2600d5..6a5881da 100644 --- a/src/pools/worker-node.ts +++ b/src/pools/worker-node.ts @@ -80,7 +80,8 @@ export class WorkerNode const tasksQueueSize = this.tasksQueue.push(task) if (this.hasBackPressure() && !this.onBackPressureStarted) { this.onBackPressureStarted = true - this.emit('backPressure', { workerId: this.info.id as number }) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.emit('backPressure', { workerId: this.info.id! }) this.onBackPressureStarted = false } return tasksQueueSize @@ -91,7 +92,8 @@ export class WorkerNode const tasksQueueSize = this.tasksQueue.unshift(task) if (this.hasBackPressure() && !this.onBackPressureStarted) { this.onBackPressureStarted = true - this.emit('backPressure', { workerId: this.info.id as number }) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.emit('backPressure', { workerId: this.info.id! }) this.onBackPressureStarted = false } return tasksQueueSize @@ -212,7 +214,8 @@ export class WorkerNode private initWorkerInfo (worker: Worker): WorkerInfo { return { id: getWorkerId(worker), - type: getWorkerType(worker) as WorkerType, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + type: getWorkerType(worker)!, dynamic: false, ready: false, stealing: false @@ -263,7 +266,8 @@ export class WorkerNode for (const task of this.tasksQueue) { if ( (task.name === DEFAULT_TASK_NAME && - name === (this.info.taskFunctionNames as string[])[1]) || + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + name === this.info.taskFunctionNames![1]) || (task.name !== DEFAULT_TASK_NAME && name === task.name) ) { ++taskFunctionQueueSize diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 81523d8c..2359758d 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -277,10 +277,8 @@ export abstract class AbstractWorker< 'Cannot set the default task function to a non-existing task function' ) } - this.taskFunctions.set( - DEFAULT_TASK_NAME, - this.taskFunctions.get(name) as TaskFunction - ) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + this.taskFunctions.set(DEFAULT_TASK_NAME, this.taskFunctions.get(name)!) this.sendTaskFunctionNamesToMainWorker() return { status: true } } catch (error) { @@ -328,19 +326,22 @@ export abstract class AbstractWorker< switch (taskFunctionOperation) { case 'add': response = this.addTaskFunction( - taskFunctionName as string, - // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func - new Function(`return ${taskFunction as string}`)() as TaskFunction< + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + taskFunctionName!, + // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func, @typescript-eslint/no-non-null-assertion + new Function(`return ${taskFunction!}`)() as TaskFunction< Data, Response > ) break case 'remove': - response = this.removeTaskFunction(taskFunctionName as string) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + response = this.removeTaskFunction(taskFunctionName!) break case 'default': - response = this.setDefaultTaskFunction(taskFunctionName as string) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + response = this.setDefaultTaskFunction(taskFunctionName!) break default: response = { status: false, error: new Error('Unknown task operation') } @@ -353,7 +354,8 @@ export abstract class AbstractWorker< ...(!response.status && response?.error != null && { workerError: { - name: taskFunctionName as string, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + name: taskFunctionName!, message: this.handleError(response.error as Error | string) } }) @@ -488,8 +490,10 @@ export abstract class AbstractWorker< if (!this.taskFunctions.has(taskFunctionName)) { this.sendToMainWorker({ workerError: { - name: name as string, - message: `Task function '${name as string}' not found`, + // 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`, data }, taskId @@ -527,7 +531,8 @@ export abstract class AbstractWorker< } catch (error) { this.sendToMainWorker({ workerError: { - name: name as string, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + name: name!, message: this.handleError(error as Error | string), data }, @@ -563,7 +568,8 @@ export abstract class AbstractWorker< .catch(error => { this.sendToMainWorker({ workerError: { - name: name as string, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + name: name!, message: this.handleError(error as Error | string), data }, diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index fa810c7e..05fe2534 100644 --- a/src/worker/cluster-worker.ts +++ b/src/worker/cluster-worker.ts @@ -32,7 +32,8 @@ export class ClusterWorker< taskFunctions: TaskFunction | TaskFunctions, opts: WorkerOptions = {} ) { - super(cluster.isPrimary, cluster.worker as Worker, taskFunctions, opts) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + super(cluster.isPrimary, cluster.worker!, taskFunctions, opts) } /** @inheritDoc */ diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index 2a50249f..7f3cd950 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -42,7 +42,8 @@ export class ThreadWorker< taskFunctions: TaskFunction | TaskFunctions, opts: WorkerOptions = {} ) { - super(isMainThread, parentPort as MessagePort, taskFunctions, opts) + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + super(isMainThread, parentPort!, taskFunctions, opts) } /** @inheritDoc */ -- 2.34.1