From d3c8a1a8022726094160d1413f1f289c5d84d715 Mon Sep 17 00:00:00 2001 From: Shinigami Date: Fri, 12 Feb 2021 21:35:08 +0100 Subject: [PATCH] Only allow primitive JSON for transfer between worker and main worker (#128) --- CHANGELOG.md | 5 ++ package-lock.json | 151 ++++++++++++++++------------------ src/pools/abstract-pool.ts | 6 +- src/pools/cluster/dynamic.ts | 8 +- src/pools/cluster/fixed.ts | 12 ++- src/pools/pool.ts | 7 +- src/pools/thread/dynamic.ts | 8 +- src/pools/thread/fixed.ts | 12 ++- src/utility-types.ts | 8 +- src/worker/abstract-worker.ts | 6 +- src/worker/cluster-worker.ts | 12 ++- src/worker/thread-worker.ts | 12 ++- 12 files changed, 112 insertions(+), 135 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02624b45..9a7d9427 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,11 @@ But you should always prefer just using const { DynamicThreadPool } = require('poolifier') ``` +#### New type definitions for input data and response + +For cluster and thread pools, you can now only send and receive serializable `JSON` data. +_This is not a limitation by poolifier but NodeJS._ + #### Internal (protected) methods renaming Those methods are not intended to be used from final users diff --git a/package-lock.json b/package-lock.json index 591e8f31..a48b2f51 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,16 +26,16 @@ "dev": true }, "@babel/core": { - "version": "7.12.13", - "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.12.13.tgz", - "integrity": "sha512-BQKE9kXkPlXHPeqissfxo0lySWJcYdEP0hdtJOH/iJfDdhOCcgtNCjftCJg3qqauB4h+lz2N6ixM++b9DN1Tcw==", + "version": "7.12.16", + "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.12.16.tgz", + "integrity": "sha512-t/hHIB504wWceOeaOoONOhu+gX+hpjfeN6YRBT209X/4sibZQfSF1I0HFRRlBe97UZZosGx5XwUg1ZgNbelmNw==", "dev": true, "requires": { "@babel/code-frame": "^7.12.13", - "@babel/generator": "^7.12.13", + "@babel/generator": "^7.12.15", "@babel/helper-module-transforms": "^7.12.13", "@babel/helpers": "^7.12.13", - "@babel/parser": "^7.12.13", + "@babel/parser": "^7.12.16", "@babel/template": "^7.12.13", "@babel/traverse": "^7.12.13", "@babel/types": "^7.12.13", @@ -87,13 +87,13 @@ } }, "@babel/helper-compilation-targets": { - "version": "7.12.13", - "resolved": "https://registry.npmjs.org/@babel/helper-compilation-targets/-/helper-compilation-targets-7.12.13.tgz", - "integrity": "sha512-dXof20y/6wB5HnLOGyLh/gobsMvDNoekcC+8MCV2iaTd5JemhFkPD73QB+tK3iFC9P0xJC73B6MvKkyUfS9cCw==", + "version": "7.12.16", + "resolved": "https://registry.npmjs.org/@babel/helper-compilation-targets/-/helper-compilation-targets-7.12.16.tgz", + "integrity": "sha512-dBHNEEaZx7F3KoUYqagIhRIeqyyuI65xMndMZ3WwGwEBI609I4TleYQHcrS627vbKyNTXqShoN+fvYD9HuQxAg==", "dev": true, "requires": { "@babel/compat-data": "^7.12.13", - "@babel/helper-validator-option": "^7.12.11", + "@babel/helper-validator-option": "^7.12.16", "browserslist": "^4.14.5", "semver": "^5.5.0" }, @@ -107,22 +107,22 @@ } }, "@babel/helper-create-class-features-plugin": { - "version": "7.12.13", - "resolved": "https://registry.npmjs.org/@babel/helper-create-class-features-plugin/-/helper-create-class-features-plugin-7.12.13.tgz", - "integrity": "sha512-Vs/e9wv7rakKYeywsmEBSRC9KtmE7Px+YBlESekLeJOF0zbGUicGfXSNi3o+tfXSNS48U/7K9mIOOCR79Cl3+Q==", + "version": "7.12.16", + "resolved": "https://registry.npmjs.org/@babel/helper-create-class-features-plugin/-/helper-create-class-features-plugin-7.12.16.tgz", + "integrity": "sha512-KbSEj8l9zYkMVHpQqM3wJNxS1d9h3U9vm/uE5tpjMbaj3lTp+0noe3KPsV5dSD9jxKnf9jO9Ip9FX5PKNZCKow==", "dev": true, "requires": { "@babel/helper-function-name": "^7.12.13", - "@babel/helper-member-expression-to-functions": "^7.12.13", + "@babel/helper-member-expression-to-functions": "^7.12.16", "@babel/helper-optimise-call-expression": "^7.12.13", "@babel/helper-replace-supers": "^7.12.13", "@babel/helper-split-export-declaration": "^7.12.13" } }, "@babel/helper-create-regexp-features-plugin": { - "version": "7.12.13", - "resolved": "https://registry.npmjs.org/@babel/helper-create-regexp-features-plugin/-/helper-create-regexp-features-plugin-7.12.13.tgz", - "integrity": "sha512-XC+kiA0J3at6E85dL5UnCYfVOcIZ834QcAY0TIpgUVnz0zDzg+0TtvZTnJ4g9L1dPRGe30Qi03XCIS4tYCLtqw==", + "version": "7.12.16", + "resolved": "https://registry.npmjs.org/@babel/helper-create-regexp-features-plugin/-/helper-create-regexp-features-plugin-7.12.16.tgz", + "integrity": "sha512-jAcQ1biDYZBdaAxB4yg46/XirgX7jBDiMHDbwYQOgtViLBXGxJpZQ24jutmBqAIB/q+AwB6j+NbBXjKxEY8vqg==", "dev": true, "requires": { "@babel/helper-annotate-as-pure": "^7.12.13", @@ -168,9 +168,9 @@ } }, "@babel/helper-member-expression-to-functions": { - "version": "7.12.13", - "resolved": "https://registry.npmjs.org/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.12.13.tgz", - "integrity": "sha512-B+7nN0gIL8FZ8SvMcF+EPyB21KnCcZHQZFczCxbiNGV/O0rsrSBlWGLzmtBJ3GMjSVMIm4lpFhR+VdVBuIsUcQ==", + "version": "7.12.16", + "resolved": "https://registry.npmjs.org/@babel/helper-member-expression-to-functions/-/helper-member-expression-to-functions-7.12.16.tgz", + "integrity": "sha512-zYoZC1uvebBFmj1wFAlXwt35JLEgecefATtKp20xalwEK8vHAixLBXTGxNrVGEmTT+gzOThUgr8UEdgtalc1BQ==", "dev": true, "requires": { "@babel/types": "^7.12.13" @@ -274,9 +274,9 @@ "dev": true }, "@babel/helper-validator-option": { - "version": "7.12.11", - "resolved": "https://registry.npmjs.org/@babel/helper-validator-option/-/helper-validator-option-7.12.11.tgz", - "integrity": "sha512-TBFCyj939mFSdeX7U7DDj32WtzYY7fDcalgq8v3fBZMNOJQNn7nOYzMaUCiPxPYfCup69mtIpqlKgMZLvQ8Xhw==", + "version": "7.12.16", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-option/-/helper-validator-option-7.12.16.tgz", + "integrity": "sha512-uCgsDBPUQDvzr11ePPo4TVEocxj8RXjUVSC/Y8N1YpVAI/XDdUwGJu78xmlGhTxj2ntaWM7n9LQdRtyhOzT2YQ==", "dev": true }, "@babel/helper-wrap-function": { @@ -327,9 +327,9 @@ } }, "@babel/parser": { - "version": "7.12.15", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.12.15.tgz", - "integrity": "sha512-AQBOU2Z9kWwSZMd6lNjCX0GUgFonL1wAM1db8L8PMk9UDaGsRCArBkU4Sc+UCM3AE4hjbXx+h58Lb3QT4oRmrA==", + "version": "7.12.16", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.12.16.tgz", + "integrity": "sha512-c/+u9cqV6F0+4Hpq01jnJO+GLp2DdT63ppz9Xa+6cHaajM9VFzK/iDXiKK65YtpeVwu+ctfS6iqlMqRgQRzeCw==", "dev": true }, "@babel/plugin-proposal-async-generator-functions": { @@ -354,12 +354,12 @@ } }, "@babel/plugin-proposal-dynamic-import": { - "version": "7.12.1", - "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-dynamic-import/-/plugin-proposal-dynamic-import-7.12.1.tgz", - "integrity": "sha512-a4rhUSZFuq5W8/OO8H7BL5zspjnc1FLd9hlOxIK/f7qG4a0qsqk8uvF/ywgBA8/OmjsapjpvaEOYItfGG1qIvQ==", + "version": "7.12.16", + "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-dynamic-import/-/plugin-proposal-dynamic-import-7.12.16.tgz", + "integrity": "sha512-yiDkYFapVxNOCcBfLnsb/qdsliroM+vc3LHiZwS4gh7pFjo5Xq3BDhYBNn3H3ao+hWPvqeeTdU+s+FIvokov+w==", "dev": true, "requires": { - "@babel/helper-plugin-utils": "^7.10.4", + "@babel/helper-plugin-utils": "^7.12.13", "@babel/plugin-syntax-dynamic-import": "^7.8.0" } }, @@ -435,9 +435,9 @@ } }, "@babel/plugin-proposal-optional-chaining": { - "version": "7.12.13", - "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-optional-chaining/-/plugin-proposal-optional-chaining-7.12.13.tgz", - "integrity": "sha512-0ZwjGfTcnZqyV3y9DSD1Yk3ebp+sIUpT2YDqP8hovzaNZnQq2Kd7PEqa6iOIUDBXBt7Jl3P7YAcEIL5Pz8u09Q==", + "version": "7.12.16", + "resolved": "https://registry.npmjs.org/@babel/plugin-proposal-optional-chaining/-/plugin-proposal-optional-chaining-7.12.16.tgz", + "integrity": "sha512-O3ohPwOhkwji5Mckb7F/PJpJVJY3DpPsrt/F0Bk40+QMk9QpAIqeGusHWqu/mYqsM8oBa6TziL/2mbERWsUZjg==", "dev": true, "requires": { "@babel/helper-plugin-utils": "^7.12.13", @@ -905,19 +905,19 @@ } }, "@babel/preset-env": { - "version": "7.12.13", - "resolved": "https://registry.npmjs.org/@babel/preset-env/-/preset-env-7.12.13.tgz", - "integrity": "sha512-JUVlizG8SoFTz4LmVUL8++aVwzwxcvey3N0j1tRbMAXVEy95uQ/cnEkmEKHN00Bwq4voAV3imQGnQvpkLAxsrw==", + "version": "7.12.16", + "resolved": "https://registry.npmjs.org/@babel/preset-env/-/preset-env-7.12.16.tgz", + "integrity": "sha512-BXCAXy8RE/TzX416pD2hsVdkWo0G+tYd16pwnRV4Sc0fRwTLRS/Ssv8G5RLXUGQv7g4FG7TXkdDJxCjQ5I+Zjg==", "dev": true, "requires": { "@babel/compat-data": "^7.12.13", - "@babel/helper-compilation-targets": "^7.12.13", + "@babel/helper-compilation-targets": "^7.12.16", "@babel/helper-module-imports": "^7.12.13", "@babel/helper-plugin-utils": "^7.12.13", - "@babel/helper-validator-option": "^7.12.11", + "@babel/helper-validator-option": "^7.12.16", "@babel/plugin-proposal-async-generator-functions": "^7.12.13", "@babel/plugin-proposal-class-properties": "^7.12.13", - "@babel/plugin-proposal-dynamic-import": "^7.12.1", + "@babel/plugin-proposal-dynamic-import": "^7.12.16", "@babel/plugin-proposal-export-namespace-from": "^7.12.13", "@babel/plugin-proposal-json-strings": "^7.12.13", "@babel/plugin-proposal-logical-assignment-operators": "^7.12.13", @@ -925,7 +925,7 @@ "@babel/plugin-proposal-numeric-separator": "^7.12.13", "@babel/plugin-proposal-object-rest-spread": "^7.12.13", "@babel/plugin-proposal-optional-catch-binding": "^7.12.13", - "@babel/plugin-proposal-optional-chaining": "^7.12.13", + "@babel/plugin-proposal-optional-chaining": "^7.12.16", "@babel/plugin-proposal-private-methods": "^7.12.13", "@babel/plugin-proposal-unicode-property-regex": "^7.12.13", "@babel/plugin-syntax-async-generators": "^7.8.0", @@ -1366,9 +1366,9 @@ "dev": true }, "@types/node": { - "version": "14.14.26", - "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.26.tgz", - "integrity": "sha512-skWxepWOs+VArEBWd2S/VR3wUavioIIx9/HzW+UJiIjtwa6+kNXdsOeq7FfxDXf56hIcL0ieo2brwMgBJ1+lhw==", + "version": "14.14.27", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.27.tgz", + "integrity": "sha512-Ecfmo4YDQPwuqTCl1yBxLV5ihKfRlkBmzUEDcfIRvDxOTGQEeikr317Ln7Gcv0tjA8dVgKI3rniqW2G1OyKDng==", "dev": true }, "@types/object-path": { @@ -2210,9 +2210,9 @@ } }, "decamelize": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", - "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-4.0.0.tgz", + "integrity": "sha512-9iE1PgSik9HeIIw2JO94IidnE3eBoQrFJ3w7sFuzSX4DpmZ3v5sZpUiV5Swcf6mQEF+Y0ru8Neo+p+nyh2J+hQ==", "dev": true }, "dedent": { @@ -2382,9 +2382,9 @@ "dev": true }, "electron-to-chromium": { - "version": "1.3.662", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.662.tgz", - "integrity": "sha512-IGBXmTGwdVGUVTnZ8ISEvkhDfhhD+CDFndG4//BhvDcEtPYiVrzoB+rzT/Y12OQCf5bvRCrVmrUbGrS9P7a6FQ==", + "version": "1.3.663", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.663.tgz", + "integrity": "sha512-xkVkzHj6k3oRRGlmdgUCCLSLhtFYHDCTH7SeK+LJdJjnsLcrdbpr8EYmfMQhez3V/KPO5UScSpzQ0feYX6Qoyw==", "dev": true }, "emoji-regex": { @@ -4299,6 +4299,12 @@ "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", "dev": true }, + "decamelize": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz", + "integrity": "sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=", + "dev": true + }, "find-up": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", @@ -4365,6 +4371,12 @@ "strip-ansi": "^6.0.0" } }, + "y18n": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.1.tgz", + "integrity": "sha512-wNcy4NvjMYL8gogWWYAO7ZFWFfHcbdbE57tZO8e4cbpj8tfUcwrwqSl3ad8HxpYWCdXcJUCeKKZS62Av1affwQ==", + "dev": true + }, "yargs": { "version": "15.4.1", "resolved": "https://registry.npmjs.org/yargs/-/yargs-15.4.1.tgz", @@ -5218,21 +5230,12 @@ } }, "rollup": { - "version": "2.38.5", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.38.5.tgz", - "integrity": "sha512-VoWt8DysFGDVRGWuHTqZzT02J0ASgjVq/hPs9QcBOGMd7B+jfTr/iqMVEyOi901rE3xq+Deq66GzIT1yt7sGwQ==", + "version": "2.39.0", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.39.0.tgz", + "integrity": "sha512-+WR3bttcq7zE+BntH09UxaW3bQo3vItuYeLsyk4dL2tuwbeSKJuvwiawyhEnvRdRgrII0Uzk00FpctHO/zB1kw==", "dev": true, "requires": { "fsevents": "~2.3.1" - }, - "dependencies": { - "fsevents": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", - "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", - "dev": true, - "optional": true - } } }, "rollup-plugin-delete": { @@ -5736,9 +5739,9 @@ "dev": true }, "ua-parser-js": { - "version": "0.7.23", - "resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-0.7.23.tgz", - "integrity": "sha512-m4hvMLxgGHXG3O3fQVAyyAQpZzDOvwnhOTjYz5Xmr7r/+LpkNy3vJXdVRWgd1TkAb7NGROZuSy96CrlNVjA7KA==", + "version": "0.7.24", + "resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-0.7.24.tgz", + "integrity": "sha512-yo+miGzQx5gakzVK3QFfN0/L9uVhosXBBO7qmnk7c2iw1IhL212wfA3zbnI54B0obGwC/5NWub/iT9sReMx+Fw==", "dev": true }, "uglify-js": { @@ -6103,9 +6106,9 @@ "dev": true }, "y18n": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.1.tgz", - "integrity": "sha512-wNcy4NvjMYL8gogWWYAO7ZFWFfHcbdbE57tZO8e4cbpj8tfUcwrwqSl3ad8HxpYWCdXcJUCeKKZS62Av1affwQ==", + "version": "5.0.5", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.5.tgz", + "integrity": "sha512-hsRUr4FFrvhhRH12wOdfs38Gy7k2FFzB9qgN9v3aLykRq0dRcdcpz5C9FxdS2NuhOrI/628b/KSTJ3rwHysYSg==", "dev": true }, "yallist": { @@ -6144,14 +6147,6 @@ "string-width": "^4.2.0", "y18n": "^5.0.5", "yargs-parser": "^20.2.2" - }, - "dependencies": { - "y18n": { - "version": "5.0.5", - "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.5.tgz", - "integrity": "sha512-hsRUr4FFrvhhRH12wOdfs38Gy7k2FFzB9qgN9v3aLykRq0dRcdcpz5C9FxdS2NuhOrI/628b/KSTJ3rwHysYSg==", - "dev": true - } } }, "yargs-parser": { @@ -6170,14 +6165,6 @@ "decamelize": "^4.0.0", "flat": "^5.0.2", "is-plain-obj": "^2.1.0" - }, - "dependencies": { - "decamelize": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-4.0.0.tgz", - "integrity": "sha512-9iE1PgSik9HeIIw2JO94IidnE3eBoQrFJ3w7sFuzSX4DpmZ3v5sZpUiV5Swcf6mQEF+Y0ru8Neo+p+nyh2J+hQ==", - "dev": true - } } }, "yocto-queue": { diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index d169afad..6d7f5569 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -37,10 +37,8 @@ class PoolEmitter extends EventEmitter {} export abstract class AbstractPool< Worker extends IWorker, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Data = any, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Response = any + Data = unknown, + Response = unknown > implements IPool { public readonly workers: Worker[] = [] public nextWorker: number = 0 diff --git a/src/pools/cluster/dynamic.ts b/src/pools/cluster/dynamic.ts index f2375a02..25306d34 100644 --- a/src/pools/cluster/dynamic.ts +++ b/src/pools/cluster/dynamic.ts @@ -1,5 +1,5 @@ import type { Worker } from 'cluster' -import type { MessageValue } from '../../utility-types' +import type { JSONValue, MessageValue } from '../../utility-types' import type { ClusterPoolOptions } from './fixed' import { FixedClusterPool } from './fixed' @@ -13,10 +13,8 @@ import { FixedClusterPool } from './fixed' * @since 2.0.0 */ export class DynamicClusterPool< - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Data = any, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Response = any + Data extends JSONValue = JSONValue, + Response extends JSONValue = JSONValue > extends FixedClusterPool { /** * @param min Min number of workers that will be always active diff --git a/src/pools/cluster/fixed.ts b/src/pools/cluster/fixed.ts index 00205449..b4071407 100644 --- a/src/pools/cluster/fixed.ts +++ b/src/pools/cluster/fixed.ts @@ -1,5 +1,5 @@ import { fork, isMaster, setupMaster, Worker } from 'cluster' -import type { MessageValue } from '../../utility-types' +import type { JSONValue, MessageValue } from '../../utility-types' import type { PoolOptions } from '../abstract-pool' import { AbstractPool } from '../abstract-pool' @@ -21,12 +21,10 @@ export interface ClusterPoolOptions extends PoolOptions { * @author [Christopher Quadflieg](https://github.com/Shinigami92) * @since 2.0.0 */ -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export class FixedClusterPool extends AbstractPool< - Worker, - Data, - Response -> { +export class FixedClusterPool< + Data extends JSONValue = JSONValue, + Response extends JSONValue = JSONValue +> extends AbstractPool { /** * @param numWorkers Number of workers for this pool. * @param filePath A file path with implementation of `ClusterWorker` class, relative path is fine. diff --git a/src/pools/pool.ts b/src/pools/pool.ts index 0c9b232f..e8f391cc 100644 --- a/src/pools/pool.ts +++ b/src/pools/pool.ts @@ -1,9 +1,4 @@ -export interface IPool< - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Data = any, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Response = any -> { +export interface IPool { destroy(): Promise execute(data: Data): Promise } diff --git a/src/pools/thread/dynamic.ts b/src/pools/thread/dynamic.ts index d6a6e1ba..51dca2f3 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -1,4 +1,4 @@ -import type { MessageValue } from '../../utility-types' +import type { JSONValue, MessageValue } from '../../utility-types' import type { PoolOptions } from '../abstract-pool' import type { ThreadWorkerWithMessageChannel } from './fixed' import { FixedThreadPool } from './fixed' @@ -13,10 +13,8 @@ import { FixedThreadPool } from './fixed' * @since 0.0.1 */ export class DynamicThreadPool< - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Data = any, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Response = any + Data extends JSONValue = JSONValue, + Response extends JSONValue = JSONValue > extends FixedThreadPool { /** * @param min Min number of threads that will be always active diff --git a/src/pools/thread/fixed.ts b/src/pools/thread/fixed.ts index bed8f3a7..badca517 100644 --- a/src/pools/thread/fixed.ts +++ b/src/pools/thread/fixed.ts @@ -1,5 +1,5 @@ import { isMainThread, MessageChannel, SHARE_ENV, Worker } from 'worker_threads' -import type { Draft, MessageValue } from '../../utility-types' +import type { Draft, JSONValue, MessageValue } from '../../utility-types' import type { PoolOptions } from '../abstract-pool' import { AbstractPool } from '../abstract-pool' @@ -13,12 +13,10 @@ export type ThreadWorkerWithMessageChannel = Worker & Draft * @author [Alessandro Pio Ardizio](https://github.com/pioardi) * @since 0.0.1 */ -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export class FixedThreadPool extends AbstractPool< - ThreadWorkerWithMessageChannel, - Data, - Response -> { +export class FixedThreadPool< + Data extends JSONValue = JSONValue, + Response extends JSONValue = JSONValue +> extends AbstractPool { /** * @param numThreads Num of threads for this worker pool. * @param filePath A file path with implementation of `ThreadWorker` class, relative path is fine. diff --git a/src/utility-types.ts b/src/utility-types.ts index 5a3ef4c5..e177adae 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -1,6 +1,12 @@ export type Draft = { -readonly [P in keyof T]?: T[P] } -export interface MessageValue { +export type JSONPrimitive = number | boolean | string | null +// eslint-disable-next-line no-use-before-define +export type JSONValue = JSONPrimitive | JSONArray | JSONObject +export type JSONObject = { [k: string]: JSONValue } +export type JSONArray = Array + +export interface MessageValue { readonly data?: Data readonly id?: number readonly kill?: number diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 5e223623..4b68d897 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -4,10 +4,8 @@ import type { WorkerOptions } from './worker-options' export abstract class AbstractWorker< MainWorker, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Data = any, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Response = any + Data = unknown, + Response = unknown > extends AsyncResource { protected readonly maxInactiveTime: number protected readonly async: boolean diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index 092dd511..4ed74749 100644 --- a/src/worker/cluster-worker.ts +++ b/src/worker/cluster-worker.ts @@ -1,6 +1,6 @@ import type { Worker } from 'cluster' import { isMaster, worker } from 'cluster' -import type { MessageValue } from '../utility-types' +import type { JSONValue, MessageValue } from '../utility-types' import { AbstractWorker } from './abstract-worker' import type { WorkerOptions } from './worker-options' @@ -13,12 +13,10 @@ import type { WorkerOptions } from './worker-options' * @author [Christopher Quadflieg](https://github.com/Shinigami92) * @since 2.0.0 */ -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export class ClusterWorker extends AbstractWorker< - Worker, - Data, - Response -> { +export class ClusterWorker< + Data extends JSONValue = JSONValue, + Response extends JSONValue = JSONValue +> extends AbstractWorker { public constructor (fn: (data: Data) => Response, opts: WorkerOptions = {}) { super('worker-cluster-pool:pioardi', isMaster, fn, opts) diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index 5a16026c..80b06ee9 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -1,5 +1,5 @@ import { isMainThread, parentPort } from 'worker_threads' -import type { MessageValue } from '../utility-types' +import type { JSONValue, MessageValue } from '../utility-types' import { AbstractWorker } from './abstract-worker' import type { WorkerOptions } from './worker-options' @@ -12,12 +12,10 @@ import type { WorkerOptions } from './worker-options' * @author [Alessandro Pio Ardizio](https://github.com/pioardi) * @since 0.0.1 */ -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export class ThreadWorker extends AbstractWorker< - MessagePort, - Data, - Response -> { +export class ThreadWorker< + Data extends JSONValue = JSONValue, + Response extends JSONValue = JSONValue +> extends AbstractWorker { protected parent?: MessagePort public constructor (fn: (data: Data) => Response, opts: WorkerOptions = {}) { -- 2.34.1