@rushstack/worker-pool 0.6.13 → 0.7.0

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/CHANGELOG.json CHANGED
@@ -1,6 +1,35 @@
1
1
  {
2
2
  "name": "@rushstack/worker-pool",
3
3
  "entries": [
4
+ {
5
+ "version": "0.7.0",
6
+ "tag": "@rushstack/worker-pool_v0.7.0",
7
+ "date": "Thu, 19 Feb 2026 00:04:53 GMT",
8
+ "comments": {
9
+ "minor": [
10
+ {
11
+ "comment": "Normalize package layout. CommonJS is now under `lib-commonjs`, DTS is now under `lib-dts`, and ESM is now under `lib-esm`. Imports to `lib` still work as before, handled by the `\"exports\"` field in `package.json`."
12
+ }
13
+ ],
14
+ "dependency": [
15
+ {
16
+ "comment": "Updating dependency \"@rushstack/heft\" to `1.2.0`"
17
+ }
18
+ ]
19
+ }
20
+ },
21
+ {
22
+ "version": "0.6.14",
23
+ "tag": "@rushstack/worker-pool_v0.6.14",
24
+ "date": "Sat, 07 Feb 2026 01:13:26 GMT",
25
+ "comments": {
26
+ "dependency": [
27
+ {
28
+ "comment": "Updating dependency \"@rushstack/heft\" to `1.1.14`"
29
+ }
30
+ ]
31
+ }
32
+ },
4
33
  {
5
34
  "version": "0.6.13",
6
35
  "tag": "@rushstack/worker-pool_v0.6.13",
package/CHANGELOG.md CHANGED
@@ -1,6 +1,18 @@
1
1
  # Change Log - @rushstack/worker-pool
2
2
 
3
- This log was last generated on Wed, 04 Feb 2026 20:42:47 GMT and should not be manually modified.
3
+ This log was last generated on Thu, 19 Feb 2026 00:04:53 GMT and should not be manually modified.
4
+
5
+ ## 0.7.0
6
+ Thu, 19 Feb 2026 00:04:53 GMT
7
+
8
+ ### Minor changes
9
+
10
+ - Normalize package layout. CommonJS is now under `lib-commonjs`, DTS is now under `lib-dts`, and ESM is now under `lib-esm`. Imports to `lib` still work as before, handled by the `"exports"` field in `package.json`.
11
+
12
+ ## 0.6.14
13
+ Sat, 07 Feb 2026 01:13:26 GMT
14
+
15
+ _Version update only_
4
16
 
5
17
  ## 0.6.13
6
18
  Wed, 04 Feb 2026 20:42:47 GMT
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.56.1"
8
+ "packageVersion": "7.56.3"
9
9
  }
10
10
  ]
11
11
  }
@@ -0,0 +1,185 @@
1
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2
+ // See LICENSE in the project root for license information.
3
+ import { Worker } from 'node:worker_threads';
4
+ /**
5
+ * Symbol to read the ID off of a worker
6
+ * @internal
7
+ */
8
+ export const WORKER_ID_SYMBOL = Symbol('workerId');
9
+ /**
10
+ * Manages a pool of workers.
11
+ * Workers will be shutdown by sending them the boolean value `false` in a postMessage.
12
+ * @internal
13
+ */
14
+ export class WorkerPool {
15
+ constructor(options) {
16
+ const { id, maxWorkers, onWorkerDestroyed, prepareWorker, workerData, workerScriptPath, workerResourceLimits } = options;
17
+ this.id = id;
18
+ this.maxWorkers = maxWorkers;
19
+ this._alive = [];
20
+ this._error = undefined;
21
+ this._finishing = false;
22
+ this._idle = [];
23
+ this._nextId = 0;
24
+ this._onComplete = [];
25
+ this._onWorkerDestroyed = onWorkerDestroyed;
26
+ this._pending = [];
27
+ this._prepare = prepareWorker;
28
+ this._workerData = workerData;
29
+ this._workerScript = workerScriptPath;
30
+ this._workerResourceLimits = workerResourceLimits;
31
+ }
32
+ /**
33
+ * Gets the count of active workers.
34
+ */
35
+ getActiveCount() {
36
+ return this._alive.length - this._idle.length;
37
+ }
38
+ /**
39
+ * Gets the count of idle workers.
40
+ */
41
+ getIdleCount() {
42
+ return this._idle.length;
43
+ }
44
+ /**
45
+ * Gets the count of live workers.
46
+ */
47
+ getLiveCount() {
48
+ return this._alive.length;
49
+ }
50
+ /**
51
+ * Tells the pool to shut down when all workers are done.
52
+ * Returns a promise that will be fulfilled if all workers finish successfully, or reject with the first error.
53
+ */
54
+ async finishAsync() {
55
+ this._finishing = true;
56
+ if (this._error) {
57
+ throw this._error;
58
+ }
59
+ if (!this._alive.length) {
60
+ // The pool has no live workers, this is a no-op
61
+ return;
62
+ }
63
+ // Clean up all idle workers
64
+ for (const worker of this._idle.splice(0)) {
65
+ worker.postMessage(false);
66
+ }
67
+ // There are still active workers, wait for them to clean up.
68
+ await new Promise((resolve, reject) => this._onComplete.push([resolve, reject]));
69
+ }
70
+ /**
71
+ * Resets the pool and allows more work
72
+ */
73
+ reset() {
74
+ this._finishing = false;
75
+ this._error = undefined;
76
+ }
77
+ /**
78
+ * Returns a worker to the pool. If the pool is finishing, deallocates the worker.
79
+ * @param worker - The worker to free
80
+ */
81
+ checkinWorker(worker) {
82
+ if (this._error) {
83
+ // Shut down the worker (failure)
84
+ worker.postMessage(false);
85
+ return;
86
+ }
87
+ const next = this._pending.shift();
88
+ if (next) {
89
+ // Perform the next unit of work;
90
+ next[0](worker);
91
+ }
92
+ else if (this._finishing) {
93
+ // Shut down the worker (success)
94
+ worker.postMessage(false);
95
+ }
96
+ else {
97
+ // No pending work, idle the workers
98
+ this._idle.push(worker);
99
+ }
100
+ }
101
+ /**
102
+ * Checks out a currently available worker or waits for the next free worker.
103
+ * @param allowCreate - If creating new workers is allowed (subject to maxSize)
104
+ */
105
+ async checkoutWorkerAsync(allowCreate) {
106
+ if (this._error) {
107
+ throw this._error;
108
+ }
109
+ let worker = this._idle.shift();
110
+ if (!worker && allowCreate) {
111
+ worker = this._createWorker();
112
+ }
113
+ if (worker) {
114
+ return worker;
115
+ }
116
+ return await new Promise((resolve, reject) => {
117
+ this._pending.push([resolve, reject]);
118
+ });
119
+ }
120
+ /**
121
+ * Creates a new worker if allowed by maxSize.
122
+ */
123
+ _createWorker() {
124
+ if (this._alive.length >= this.maxWorkers) {
125
+ return;
126
+ }
127
+ const worker = new Worker(this._workerScript, {
128
+ eval: false,
129
+ workerData: this._workerData,
130
+ resourceLimits: this._workerResourceLimits
131
+ });
132
+ const id = `${this.id}#${++this._nextId}`;
133
+ worker[WORKER_ID_SYMBOL] = id;
134
+ this._alive.push(worker);
135
+ worker.on('error', (err) => {
136
+ this._onError(err);
137
+ this._destroyWorker(worker);
138
+ });
139
+ worker.once('exit', (exitCode) => {
140
+ if (exitCode !== 0) {
141
+ this._onError(new Error(`Worker ${id} exited with code ${exitCode}`));
142
+ }
143
+ this._destroyWorker(worker);
144
+ });
145
+ if (this._prepare) {
146
+ this._prepare(worker);
147
+ }
148
+ return worker;
149
+ }
150
+ /**
151
+ * Cleans up a worker
152
+ */
153
+ _destroyWorker(worker) {
154
+ const aliveIndex = this._alive.indexOf(worker);
155
+ if (aliveIndex >= 0) {
156
+ this._alive.splice(aliveIndex, 1);
157
+ }
158
+ const freeIndex = this._idle.indexOf(worker);
159
+ if (freeIndex >= 0) {
160
+ this._idle.splice(freeIndex, 1);
161
+ }
162
+ worker.unref();
163
+ if (this._onWorkerDestroyed) {
164
+ this._onWorkerDestroyed();
165
+ }
166
+ if (!this._alive.length && !this._error) {
167
+ for (const [resolve] of this._onComplete.splice(0)) {
168
+ resolve();
169
+ }
170
+ }
171
+ }
172
+ /**
173
+ * Notifies all pending callbacks that an error has occurred and switches this pool into error state.
174
+ */
175
+ _onError(error) {
176
+ this._error = error;
177
+ for (const [, reject] of this._pending.splice(0)) {
178
+ reject(this._error);
179
+ }
180
+ for (const [, reject] of this._onComplete.splice(0)) {
181
+ reject(this._error);
182
+ }
183
+ }
184
+ }
185
+ //# sourceMappingURL=WorkerPool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WorkerPool.js","sourceRoot":"","sources":["../src/WorkerPool.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAE3D,OAAO,EAAuB,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAElE;;;GAGG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAkB,MAAM,CAAC,UAAU,CAAC,CAAC;AAsClE;;;;GAIG;AACH,MAA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ACK,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,CAAC;YACjD,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACtB,CAAC;QAED,KAAK,MAAM,CAAC,EAAE,MAAM,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;YACpD,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;CACF","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 { type ResourceLimits, Worker } from 'node: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 * Optional resource limits for the workers.\n */\n workerResourceLimits?: ResourceLimits;\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 private readonly _workerResourceLimits: ResourceLimits | undefined;\n\n public constructor(options: IWorkerPoolOptions) {\n const {\n id,\n maxWorkers,\n onWorkerDestroyed,\n prepareWorker,\n workerData,\n workerScriptPath,\n workerResourceLimits\n } = 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 this._workerResourceLimits = workerResourceLimits;\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 resourceLimits: this._workerResourceLimits\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.once('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"]}
@@ -0,0 +1,4 @@
1
+ // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2
+ // See LICENSE in the project root for license information.
3
+ export { WORKER_ID_SYMBOL, WorkerPool } from './WorkerPool';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAW3D,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC","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/// <reference types=\"node\" preserve=\"true\" />\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,9 +1,30 @@
1
1
  {
2
2
  "name": "@rushstack/worker-pool",
3
- "version": "0.6.13",
3
+ "version": "0.7.0",
4
4
  "description": "Lightweight worker pool using NodeJS worker_threads",
5
- "main": "lib/index.js",
6
- "typings": "dist/worker-pool.d.ts",
5
+ "main": "./lib-commonjs/index.js",
6
+ "module": "./lib-esm/index.js",
7
+ "types": "./dist/worker-pool.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/worker-pool.d.ts",
11
+ "import": "./lib-esm/index.js",
12
+ "require": "./lib-commonjs/index.js"
13
+ },
14
+ "./lib/*": {
15
+ "types": "./lib-dts/*.d.ts",
16
+ "import": "./lib-esm/*.js",
17
+ "require": "./lib-commonjs/*.js"
18
+ },
19
+ "./package.json": "./package.json"
20
+ },
21
+ "typesVersions": {
22
+ "*": {
23
+ "lib/*": [
24
+ "lib-dts/*"
25
+ ]
26
+ }
27
+ },
7
28
  "license": "MIT",
8
29
  "repository": {
9
30
  "url": "https://github.com/microsoft/rushstack.git",
@@ -12,8 +33,8 @@
12
33
  },
13
34
  "devDependencies": {
14
35
  "eslint": "~9.37.0",
15
- "@rushstack/heft": "1.1.13",
16
- "local-node-rig": "1.0.0"
36
+ "local-node-rig": "1.0.0",
37
+ "@rushstack/heft": "1.2.0"
17
38
  },
18
39
  "peerDependencies": {
19
40
  "@types/node": "*"
@@ -23,6 +44,7 @@
23
44
  "optional": true
24
45
  }
25
46
  },
47
+ "sideEffects": false,
26
48
  "scripts": {
27
49
  "build": "heft build --clean",
28
50
  "_phase:build": "heft run --only build -- --clean",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes