@rushstack/worker-pool 0.4.10 → 0.4.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -24
- package/README.md +12 -12
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/WorkerPool.js.map +1 -1
- package/lib/index.js.map +1 -1
- package/package.json +2 -2
package/LICENSE
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
@rushstack/worker-pool
|
|
2
|
-
|
|
3
|
-
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
4
|
-
|
|
5
|
-
MIT License
|
|
6
|
-
|
|
7
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
|
8
|
-
a copy of this software and associated documentation files (the
|
|
9
|
-
"Software"), to deal in the Software without restriction, including
|
|
10
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
|
11
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
|
12
|
-
permit persons to whom the Software is furnished to do so, subject to
|
|
13
|
-
the following conditions:
|
|
14
|
-
|
|
15
|
-
The above copyright notice and this permission notice shall be
|
|
16
|
-
included in all copies or substantial portions of the Software.
|
|
17
|
-
|
|
18
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
19
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
20
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
21
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
22
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
23
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
24
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
1
|
+
@rushstack/worker-pool
|
|
2
|
+
|
|
3
|
+
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
4
|
+
|
|
5
|
+
MIT License
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
8
|
+
a copy of this software and associated documentation files (the
|
|
9
|
+
"Software"), to deal in the Software without restriction, including
|
|
10
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
11
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
12
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
13
|
+
the following conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be
|
|
16
|
+
included in all copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
19
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
20
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
21
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
22
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
23
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
24
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
# @rushstack/worker-pool
|
|
2
|
-
|
|
3
|
-
This library contains a lightweight worker pool using the NodeJS worker_threads API.
|
|
4
|
-
|
|
5
|
-
## Links
|
|
6
|
-
|
|
7
|
-
- [CHANGELOG.md](
|
|
8
|
-
https://github.com/microsoft/rushstack/blob/main/libraries/worker-pool/CHANGELOG.md) - Find
|
|
9
|
-
out what's new in the latest version
|
|
10
|
-
- [API Reference](https://rushstack.io/pages/api/worker-pool/)
|
|
11
|
-
|
|
12
|
-
`@rushstack/worker-pool` is part of the [Rush Stack](https://rushstack.io/) family of projects.
|
|
1
|
+
# @rushstack/worker-pool
|
|
2
|
+
|
|
3
|
+
This library contains a lightweight worker pool using the NodeJS worker_threads API.
|
|
4
|
+
|
|
5
|
+
## Links
|
|
6
|
+
|
|
7
|
+
- [CHANGELOG.md](
|
|
8
|
+
https://github.com/microsoft/rushstack/blob/main/libraries/worker-pool/CHANGELOG.md) - Find
|
|
9
|
+
out what's new in the latest version
|
|
10
|
+
- [API Reference](https://rushstack.io/pages/api/worker-pool/)
|
|
11
|
+
|
|
12
|
+
`@rushstack/worker-pool` is part of the [Rush Stack](https://rushstack.io/) family of projects.
|
package/dist/tsdoc-metadata.json
CHANGED
package/lib/WorkerPool.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WorkerPool.js","sourceRoot":"","sources":["../src/WorkerPool.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,mDAAwC;AAExC;;;GAGG;AACU,QAAA,gBAAgB,GAAkB,MAAM,CAAC,UAAU,CAAC,CAAC;AAiClE;;;;GAIG;AACH,MAAa,UAAU;IAgBrB,YAAmB,OAA2B;QAC5C,MAAM,EAAE,EAAE,EAAE,UAAU,EAAE,iBAAiB,EAAE,aAAa,EAAE,UAAU,EAAE,gBAAgB,EAAE,GAAG,OAAO,CAAC;QAEnG,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;QACxB,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;QACxB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC;QACjB,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;QACtB,IAAI,CAAC,kB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,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;SACjC;QAED,MAAM,CAAC,KAAK,EAAE,CAAC;QAEf,IAAI,IAAI,CAAC,kBAAkB,EAAE;YAC3B,IAAI,CAAC,kBAAkB,EAAE,CAAC;SAC3B;QAED,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;YACvC,KAAK,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE;gBAClD,OAAO,EAAE,CAAC;aACX;SACF;IACH,CAAC;IAED;;OAEG;IACK,QAAQ,CAAC,KAAY;QAC3B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QAEpB,KAAK,MAAM,CAAC,EAAE,MAAM,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE;YAChD,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;SACrB;QAED,KAAK,MAAM,CAAC,EAAE,MAAM,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE;YACnD,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;SACrB;IACH,CAAC;CACF;AAxND,gCAwNC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\r\n// See LICENSE in the project root for license information.\r\n\r\nimport { Worker } from 'worker_threads';\r\n\r\n/**\r\n * Symbol to read the ID off of a worker\r\n * @internal\r\n */\r\nexport const WORKER_ID_SYMBOL: unique symbol = Symbol('workerId');\r\n\r\n/**\r\n * @internal\r\n */\r\nexport interface IWorkerPoolOptions {\r\n /**\r\n * Identifier for this pool, to assign to its workers for tracking\r\n */\r\n id: string;\r\n /**\r\n * Maximum number of concurrent workers this WorkerPool may spawn.\r\n */\r\n maxWorkers: number;\r\n /**\r\n * Optional callback invoked when a worker is destroyed.\r\n */\r\n onWorkerDestroyed?: () => void;\r\n /**\r\n * Optional callback invoked on a newly created worker.\r\n */\r\n prepareWorker?: (worker: Worker) => void;\r\n /**\r\n * Optional data to pass to workers when they are initialized.\r\n * Will be subjected to the Structured Clone algorithm.\r\n */\r\n workerData?: unknown;\r\n /**\r\n * Absolute path to the worker script.\r\n */\r\n workerScriptPath: string;\r\n}\r\n\r\n/**\r\n * Manages a pool of workers.\r\n * Workers will be shutdown by sending them the boolean value `false` in a postMessage.\r\n * @internal\r\n */\r\nexport class WorkerPool {\r\n public id: string;\r\n public maxWorkers: number;\r\n\r\n private readonly _alive: Worker[];\r\n private _error: Error | undefined;\r\n private _finishing: boolean;\r\n private readonly _idle: Worker[];\r\n private _nextId: number;\r\n private readonly _onComplete: [() => void, (error: Error) => void][];\r\n private readonly _onWorkerDestroyed: (() => void) | undefined;\r\n private readonly _pending: [(worker: Worker) => void, (error: Error) => void][];\r\n private readonly _prepare: ((worker: Worker) => void) | undefined;\r\n private readonly _workerData: unknown;\r\n private readonly _workerScript: string;\r\n\r\n public constructor(options: IWorkerPoolOptions) {\r\n const { id, maxWorkers, onWorkerDestroyed, prepareWorker, workerData, workerScriptPath } = options;\r\n\r\n this.id = id;\r\n this.maxWorkers = maxWorkers;\r\n this._alive = [];\r\n this._error = undefined;\r\n this._finishing = false;\r\n this._idle = [];\r\n this._nextId = 0;\r\n this._onComplete = [];\r\n this._onWorkerDestroyed = onWorkerDestroyed;\r\n this._pending = [];\r\n this._prepare = prepareWorker;\r\n this._workerData = workerData;\r\n this._workerScript = workerScriptPath;\r\n }\r\n\r\n /**\r\n * Gets the count of active workers.\r\n */\r\n public getActiveCount(): number {\r\n return this._alive.length - this._idle.length;\r\n }\r\n\r\n /**\r\n * Gets the count of idle workers.\r\n */\r\n public getIdleCount(): number {\r\n return this._idle.length;\r\n }\r\n\r\n /**\r\n * Gets the count of live workers.\r\n */\r\n public getLiveCount(): number {\r\n return this._alive.length;\r\n }\r\n\r\n /**\r\n * Tells the pool to shut down when all workers are done.\r\n * Returns a promise that will be fulfilled if all workers finish successfully, or reject with the first error.\r\n */\r\n public async finishAsync(): Promise<void> {\r\n this._finishing = true;\r\n\r\n if (this._error) {\r\n throw this._error;\r\n }\r\n\r\n if (!this._alive.length) {\r\n // The pool has no live workers, this is a no-op\r\n return;\r\n }\r\n\r\n // Clean up all idle workers\r\n for (const worker of this._idle.splice(0)) {\r\n worker.postMessage(false);\r\n }\r\n\r\n // There are still active workers, wait for them to clean up.\r\n await new Promise<void>((resolve, reject) => this._onComplete.push([resolve, reject]));\r\n }\r\n\r\n /**\r\n * Resets the pool and allows more work\r\n */\r\n public reset(): void {\r\n this._finishing = false;\r\n this._error = undefined;\r\n }\r\n\r\n /**\r\n * Returns a worker to the pool. If the pool is finishing, deallocates the worker.\r\n * @param worker - The worker to free\r\n */\r\n public checkinWorker(worker: Worker): void {\r\n if (this._error) {\r\n // Shut down the worker (failure)\r\n worker.postMessage(false);\r\n return;\r\n }\r\n\r\n const next: [(worker: Worker) => void, unknown] | undefined = this._pending.shift();\r\n\r\n if (next) {\r\n // Perform the next unit of work;\r\n next[0](worker);\r\n } else if (this._finishing) {\r\n // Shut down the worker (success)\r\n worker.postMessage(false);\r\n } else {\r\n // No pending work, idle the workers\r\n this._idle.push(worker);\r\n }\r\n }\r\n\r\n /**\r\n * Checks out a currently available worker or waits for the next free worker.\r\n * @param allowCreate - If creating new workers is allowed (subject to maxSize)\r\n */\r\n public async checkoutWorkerAsync(allowCreate: boolean): Promise<Worker> {\r\n if (this._error) {\r\n throw this._error;\r\n }\r\n\r\n let worker: Worker | undefined = this._idle.shift();\r\n if (!worker && allowCreate) {\r\n worker = this._createWorker();\r\n }\r\n\r\n if (worker) {\r\n return worker;\r\n }\r\n\r\n return await new Promise((resolve: (worker: Worker) => void, reject: (error: Error) => void) => {\r\n this._pending.push([resolve, reject]);\r\n });\r\n }\r\n\r\n /**\r\n * Creates a new worker if allowed by maxSize.\r\n */\r\n private _createWorker(): Worker | undefined {\r\n if (this._alive.length >= this.maxWorkers) {\r\n return;\r\n }\r\n\r\n const worker: Worker & {\r\n [WORKER_ID_SYMBOL]?: string;\r\n } = new Worker(this._workerScript, {\r\n eval: false,\r\n workerData: this._workerData\r\n });\r\n\r\n const id: string = `${this.id}#${++this._nextId}`;\r\n worker[WORKER_ID_SYMBOL] = id;\r\n\r\n this._alive.push(worker);\r\n\r\n worker.on('error', (err) => {\r\n this._onError(err);\r\n this._destroyWorker(worker);\r\n });\r\n\r\n worker.on('exit', (exitCode) => {\r\n if (exitCode !== 0) {\r\n this._onError(new Error(`Worker ${id} exited with code ${exitCode}`));\r\n }\r\n this._destroyWorker(worker);\r\n });\r\n\r\n if (this._prepare) {\r\n this._prepare(worker);\r\n }\r\n\r\n return worker;\r\n }\r\n\r\n /**\r\n * Cleans up a worker\r\n */\r\n private _destroyWorker(worker: Worker): void {\r\n const aliveIndex: number = this._alive.indexOf(worker);\r\n if (aliveIndex >= 0) {\r\n this._alive.splice(aliveIndex, 1);\r\n }\r\n\r\n const freeIndex: number = this._idle.indexOf(worker);\r\n if (freeIndex >= 0) {\r\n this._idle.splice(freeIndex, 1);\r\n }\r\n\r\n worker.unref();\r\n\r\n if (this._onWorkerDestroyed) {\r\n this._onWorkerDestroyed();\r\n }\r\n\r\n if (!this._alive.length && !this._error) {\r\n for (const [resolve] of this._onComplete.splice(0)) {\r\n resolve();\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Notifies all pending callbacks that an error has occurred and switches this pool into error state.\r\n */\r\n private _onError(error: Error): void {\r\n this._error = error;\r\n\r\n for (const [, reject] of this._pending.splice(0)) {\r\n reject(this._error);\r\n }\r\n\r\n for (const [, reject] of this._onComplete.splice(0)) {\r\n reject(this._error);\r\n }\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"WorkerPool.js","sourceRoot":"","sources":["../src/WorkerPool.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAE3D,mDAAwC;AAExC;;;GAGG;A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C,EAAE,MAAM,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE;YACnD,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;SACrB;IACH,CAAC;CACF;AAxND,gCAwNC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport { Worker } from 'worker_threads';\n\n/**\n * Symbol to read the ID off of a worker\n * @internal\n */\nexport const WORKER_ID_SYMBOL: unique symbol = Symbol('workerId');\n\n/**\n * @internal\n */\nexport interface IWorkerPoolOptions {\n /**\n * Identifier for this pool, to assign to its workers for tracking\n */\n id: string;\n /**\n * Maximum number of concurrent workers this WorkerPool may spawn.\n */\n maxWorkers: number;\n /**\n * Optional callback invoked when a worker is destroyed.\n */\n onWorkerDestroyed?: () => void;\n /**\n * Optional callback invoked on a newly created worker.\n */\n prepareWorker?: (worker: Worker) => void;\n /**\n * Optional data to pass to workers when they are initialized.\n * Will be subjected to the Structured Clone algorithm.\n */\n workerData?: unknown;\n /**\n * Absolute path to the worker script.\n */\n workerScriptPath: string;\n}\n\n/**\n * Manages a pool of workers.\n * Workers will be shutdown by sending them the boolean value `false` in a postMessage.\n * @internal\n */\nexport class WorkerPool {\n public id: string;\n public maxWorkers: number;\n\n private readonly _alive: Worker[];\n private _error: Error | undefined;\n private _finishing: boolean;\n private readonly _idle: Worker[];\n private _nextId: number;\n private readonly _onComplete: [() => void, (error: Error) => void][];\n private readonly _onWorkerDestroyed: (() => void) | undefined;\n private readonly _pending: [(worker: Worker) => void, (error: Error) => void][];\n private readonly _prepare: ((worker: Worker) => void) | undefined;\n private readonly _workerData: unknown;\n private readonly _workerScript: string;\n\n public constructor(options: IWorkerPoolOptions) {\n const { id, maxWorkers, onWorkerDestroyed, prepareWorker, workerData, workerScriptPath } = options;\n\n this.id = id;\n this.maxWorkers = maxWorkers;\n this._alive = [];\n this._error = undefined;\n this._finishing = false;\n this._idle = [];\n this._nextId = 0;\n this._onComplete = [];\n this._onWorkerDestroyed = onWorkerDestroyed;\n this._pending = [];\n this._prepare = prepareWorker;\n this._workerData = workerData;\n this._workerScript = workerScriptPath;\n }\n\n /**\n * Gets the count of active workers.\n */\n public getActiveCount(): number {\n return this._alive.length - this._idle.length;\n }\n\n /**\n * Gets the count of idle workers.\n */\n public getIdleCount(): number {\n return this._idle.length;\n }\n\n /**\n * Gets the count of live workers.\n */\n public getLiveCount(): number {\n return this._alive.length;\n }\n\n /**\n * Tells the pool to shut down when all workers are done.\n * Returns a promise that will be fulfilled if all workers finish successfully, or reject with the first error.\n */\n public async finishAsync(): Promise<void> {\n this._finishing = true;\n\n if (this._error) {\n throw this._error;\n }\n\n if (!this._alive.length) {\n // The pool has no live workers, this is a no-op\n return;\n }\n\n // Clean up all idle workers\n for (const worker of this._idle.splice(0)) {\n worker.postMessage(false);\n }\n\n // There are still active workers, wait for them to clean up.\n await new Promise<void>((resolve, reject) => this._onComplete.push([resolve, reject]));\n }\n\n /**\n * Resets the pool and allows more work\n */\n public reset(): void {\n this._finishing = false;\n this._error = undefined;\n }\n\n /**\n * Returns a worker to the pool. If the pool is finishing, deallocates the worker.\n * @param worker - The worker to free\n */\n public checkinWorker(worker: Worker): void {\n if (this._error) {\n // Shut down the worker (failure)\n worker.postMessage(false);\n return;\n }\n\n const next: [(worker: Worker) => void, unknown] | undefined = this._pending.shift();\n\n if (next) {\n // Perform the next unit of work;\n next[0](worker);\n } else if (this._finishing) {\n // Shut down the worker (success)\n worker.postMessage(false);\n } else {\n // No pending work, idle the workers\n this._idle.push(worker);\n }\n }\n\n /**\n * Checks out a currently available worker or waits for the next free worker.\n * @param allowCreate - If creating new workers is allowed (subject to maxSize)\n */\n public async checkoutWorkerAsync(allowCreate: boolean): Promise<Worker> {\n if (this._error) {\n throw this._error;\n }\n\n let worker: Worker | undefined = this._idle.shift();\n if (!worker && allowCreate) {\n worker = this._createWorker();\n }\n\n if (worker) {\n return worker;\n }\n\n return await new Promise((resolve: (worker: Worker) => void, reject: (error: Error) => void) => {\n this._pending.push([resolve, reject]);\n });\n }\n\n /**\n * Creates a new worker if allowed by maxSize.\n */\n private _createWorker(): Worker | undefined {\n if (this._alive.length >= this.maxWorkers) {\n return;\n }\n\n const worker: Worker & {\n [WORKER_ID_SYMBOL]?: string;\n } = new Worker(this._workerScript, {\n eval: false,\n workerData: this._workerData\n });\n\n const id: string = `${this.id}#${++this._nextId}`;\n worker[WORKER_ID_SYMBOL] = id;\n\n this._alive.push(worker);\n\n worker.on('error', (err) => {\n this._onError(err);\n this._destroyWorker(worker);\n });\n\n worker.on('exit', (exitCode) => {\n if (exitCode !== 0) {\n this._onError(new Error(`Worker ${id} exited with code ${exitCode}`));\n }\n this._destroyWorker(worker);\n });\n\n if (this._prepare) {\n this._prepare(worker);\n }\n\n return worker;\n }\n\n /**\n * Cleans up a worker\n */\n private _destroyWorker(worker: Worker): void {\n const aliveIndex: number = this._alive.indexOf(worker);\n if (aliveIndex >= 0) {\n this._alive.splice(aliveIndex, 1);\n }\n\n const freeIndex: number = this._idle.indexOf(worker);\n if (freeIndex >= 0) {\n this._idle.splice(freeIndex, 1);\n }\n\n worker.unref();\n\n if (this._onWorkerDestroyed) {\n this._onWorkerDestroyed();\n }\n\n if (!this._alive.length && !this._error) {\n for (const [resolve] of this._onComplete.splice(0)) {\n resolve();\n }\n }\n }\n\n /**\n * Notifies all pending callbacks that an error has occurred and switches this pool into error state.\n */\n private _onError(error: Error): void {\n this._error = error;\n\n for (const [, reject] of this._pending.splice(0)) {\n reject(this._error);\n }\n\n for (const [, reject] of this._onComplete.splice(0)) {\n reject(this._error);\n }\n }\n}\n"]}
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAS3D,2CAA4D;AAAnD,8GAAA,gBAAgB,OAAA;AAAE,wGAAA,UAAU,OAAA","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,4FAA4F;AAC5F,2DAA2D;;;AAS3D,2CAA4D;AAAnD,8GAAA,gBAAgB,OAAA;AAAE,wGAAA,UAAU,OAAA","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\n/**\n * A lightweight worker pool implementation using the NodeJS `worker_threads` API.\n *\n * @packageDocumentation\n */\n\nexport type { IWorkerPoolOptions } from './WorkerPool';\nexport { WORKER_ID_SYMBOL, WorkerPool } from './WorkerPool';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rushstack/worker-pool",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.12",
|
|
4
4
|
"description": "Lightweight worker pool using NodeJS worker_threads",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "dist/worker-pool.d.ts",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"directory": "libraries/worker-pool"
|
|
12
12
|
},
|
|
13
13
|
"devDependencies": {
|
|
14
|
-
"@rushstack/heft": "0.63.
|
|
14
|
+
"@rushstack/heft": "0.63.2",
|
|
15
15
|
"local-node-rig": "1.0.0"
|
|
16
16
|
},
|
|
17
17
|
"peerDependencies": {
|