@ariestools/threads 8.1.2 → 8.1.3
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/dist/browser/master/implementation.node.d.ts +28 -0
- package/dist/browser/master/implementation.node.d.ts.map +1 -1
- package/dist/browser/master/index-node.d.ts +2 -1
- package/dist/browser/master/index-node.d.ts.map +1 -1
- package/dist/neutral/master/implementation.node.d.ts +28 -0
- package/dist/neutral/master/implementation.node.d.ts.map +1 -1
- package/dist/neutral/master/index-node.d.ts +2 -1
- package/dist/neutral/master/index-node.d.ts.map +1 -1
- package/dist/node/index-node.mjs +43 -16
- package/dist/node/index-node.mjs.map +2 -2
- package/dist/node/master/implementation.node.d.ts +28 -0
- package/dist/node/master/implementation.node.d.ts.map +1 -1
- package/dist/node/master/implementation.node.mjs +43 -16
- package/dist/node/master/implementation.node.mjs.map +2 -2
- package/dist/node/master/index-node.d.ts +2 -1
- package/dist/node/master/index-node.d.ts.map +1 -1
- package/dist/node/master/index-node.mjs +43 -16
- package/dist/node/master/index-node.mjs.map +2 -2
- package/dist/node/master/pool-node.mjs.map +1 -1
- package/dist/node/master/register.mjs +8 -15
- package/dist/node/master/register.mjs.map +2 -2
- package/package.json +6 -6
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import type { ImplementationExport } from '../types/master.ts';
|
|
2
2
|
/** Default thread pool size based on the number of CPU cores. */
|
|
3
3
|
export declare const defaultPoolSize: number;
|
|
4
|
+
/** Options for optional process-signal integration with the worker registry. */
|
|
5
|
+
export interface WorkerSignalHandlersOptions {
|
|
6
|
+
/**
|
|
7
|
+
* When true, call `process.exit(0|1)` after workers terminate on a signal.
|
|
8
|
+
* Default `false` — the host owns process lifetime.
|
|
9
|
+
*/
|
|
10
|
+
exitProcess?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Signals to listen for. Defaults to `SIGINT` and `SIGTERM`.
|
|
13
|
+
*/
|
|
14
|
+
signals?: NodeJS.Signals[];
|
|
15
|
+
}
|
|
4
16
|
/**
|
|
5
17
|
* Get the Node.js-specific worker implementation using `worker_threads`, lazily initializing it on first call.
|
|
6
18
|
* @returns The platform-specific worker implementation export.
|
|
@@ -11,4 +23,20 @@ export declare function getWorkerImplementation(): ImplementationExport;
|
|
|
11
23
|
* @returns True if running in a worker thread (not the main thread), false otherwise.
|
|
12
24
|
*/
|
|
13
25
|
export declare function isWorkerRuntime(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Opt-in: terminate tracked workers when the process receives the given signals.
|
|
28
|
+
*
|
|
29
|
+
* Libraries must not own host process lifetime. Call this only from an
|
|
30
|
+
* application entrypoint that wants automatic worker cleanup on signal.
|
|
31
|
+
* Returns a disposer that removes the handlers (idempotent).
|
|
32
|
+
*
|
|
33
|
+
* By default does **not** call `process.exit` — the host chooses exit status
|
|
34
|
+
* and may complete async cleanup after the disposer or after the signal.
|
|
35
|
+
*/
|
|
36
|
+
export declare function installWorkerSignalHandlers(options?: WorkerSignalHandlersOptions): () => void;
|
|
37
|
+
/**
|
|
38
|
+
* Remove handlers installed by {@link installWorkerSignalHandlers}.
|
|
39
|
+
* Safe to call when none are installed.
|
|
40
|
+
*/
|
|
41
|
+
export declare function uninstallWorkerSignalHandlers(): void;
|
|
14
42
|
//# sourceMappingURL=implementation.node.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"implementation.node.d.ts","sourceRoot":"","sources":["../../../src/master/implementation.node.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EACV,oBAAoB,EACrB,MAAM,oBAAoB,CAAA;AAE3B,iEAAiE;AACjE,eAAO,MAAM,eAAe,QAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"implementation.node.d.ts","sourceRoot":"","sources":["../../../src/master/implementation.node.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EACV,oBAAoB,EACrB,MAAM,oBAAoB,CAAA;AAE3B,iEAAiE;AACjE,eAAO,MAAM,eAAe,QAAgB,CAAA;AAE5C,gFAAgF;AAChF,MAAM,WAAW,2BAA2B;IAC1C;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,EAAE,CAAA;CAC3B;AAuFD;;;GAGG;AACH,wBAAgB,uBAAuB,IAAI,oBAAoB,CAK9D;AAED;;;GAGG;AACH,wBAAgB,eAAe,YAE9B;AAED;;;;;;;;;GASG;AACH,wBAAgB,2BAA2B,CACzC,OAAO,GAAE,2BAAgC,GACxC,MAAM,IAAI,CA0BZ;AAED;;;GAGG;AACH,wBAAgB,6BAA6B,IAAI,IAAI,CAKpD"}
|
|
@@ -11,8 +11,16 @@ function resolveScriptPath(scriptPath, baseURL) {
|
|
|
11
11
|
const absolutePath = makeAbsolute(scriptPath);
|
|
12
12
|
return absolutePath;
|
|
13
13
|
}
|
|
14
|
+
var registry = {
|
|
15
|
+
allWorkers: [],
|
|
16
|
+
signalHandlers: /* @__PURE__ */ new Map()
|
|
17
|
+
};
|
|
18
|
+
async function terminateAllWorkers() {
|
|
19
|
+
const workers = [...registry.allWorkers];
|
|
20
|
+
registry.allWorkers = [];
|
|
21
|
+
await Promise.all(workers.map((worker) => worker.terminate()));
|
|
22
|
+
}
|
|
14
23
|
function initWorkerThreadsWorker() {
|
|
15
|
-
let allWorkers = [];
|
|
16
24
|
class Worker extends NativeWorker {
|
|
17
25
|
mappedEventListeners;
|
|
18
26
|
constructor(scriptPath, options) {
|
|
@@ -24,7 +32,10 @@ function initWorkerThreadsWorker() {
|
|
|
24
32
|
super(resolvedScriptPath, options);
|
|
25
33
|
}
|
|
26
34
|
this.mappedEventListeners = /* @__PURE__ */ new WeakMap();
|
|
27
|
-
allWorkers.push(this);
|
|
35
|
+
registry.allWorkers.push(this);
|
|
36
|
+
this.once("exit", () => {
|
|
37
|
+
registry.allWorkers = registry.allWorkers.filter((worker) => worker !== this);
|
|
38
|
+
});
|
|
28
39
|
}
|
|
29
40
|
addEventListener(eventName, rawListener) {
|
|
30
41
|
const listener = (message) => {
|
|
@@ -38,19 +49,6 @@ function initWorkerThreadsWorker() {
|
|
|
38
49
|
this.off(eventName, listener);
|
|
39
50
|
}
|
|
40
51
|
}
|
|
41
|
-
const terminateWorkersAndMaster = () => {
|
|
42
|
-
void (async () => {
|
|
43
|
-
try {
|
|
44
|
-
await Promise.all(allWorkers.map((worker) => worker.terminate()));
|
|
45
|
-
process.exit(0);
|
|
46
|
-
} catch {
|
|
47
|
-
process.exit(1);
|
|
48
|
-
}
|
|
49
|
-
})();
|
|
50
|
-
allWorkers = [];
|
|
51
|
-
};
|
|
52
|
-
process.on("SIGINT", () => terminateWorkersAndMaster());
|
|
53
|
-
process.on("SIGTERM", () => terminateWorkersAndMaster());
|
|
54
52
|
class BlobWorker extends Worker {
|
|
55
53
|
constructor(blob, options) {
|
|
56
54
|
super(Buffer.from(blob).toString("utf-8"), { ...options, fromSource: true });
|
|
@@ -74,9 +72,38 @@ function getWorkerImplementation() {
|
|
|
74
72
|
function isWorkerRuntime() {
|
|
75
73
|
return !isMainThread;
|
|
76
74
|
}
|
|
75
|
+
function installWorkerSignalHandlers(options = {}) {
|
|
76
|
+
getWorkerImplementation();
|
|
77
|
+
const signals = options.signals ?? ["SIGINT", "SIGTERM"];
|
|
78
|
+
const exitProcess = options.exitProcess === true;
|
|
79
|
+
uninstallWorkerSignalHandlers();
|
|
80
|
+
for (const signal of signals) {
|
|
81
|
+
const handler = () => {
|
|
82
|
+
void (async () => {
|
|
83
|
+
try {
|
|
84
|
+
await terminateAllWorkers();
|
|
85
|
+
if (exitProcess) process.exit(0);
|
|
86
|
+
} catch {
|
|
87
|
+
if (exitProcess) process.exit(1);
|
|
88
|
+
}
|
|
89
|
+
})();
|
|
90
|
+
};
|
|
91
|
+
process.on(signal, handler);
|
|
92
|
+
registry.signalHandlers.set(signal, handler);
|
|
93
|
+
}
|
|
94
|
+
return uninstallWorkerSignalHandlers;
|
|
95
|
+
}
|
|
96
|
+
function uninstallWorkerSignalHandlers() {
|
|
97
|
+
for (const [signal, handler] of registry.signalHandlers) {
|
|
98
|
+
process.off(signal, handler);
|
|
99
|
+
}
|
|
100
|
+
registry.signalHandlers.clear();
|
|
101
|
+
}
|
|
77
102
|
export {
|
|
78
103
|
defaultPoolSize,
|
|
79
104
|
getWorkerImplementation,
|
|
80
|
-
|
|
105
|
+
installWorkerSignalHandlers,
|
|
106
|
+
isWorkerRuntime,
|
|
107
|
+
uninstallWorkerSignalHandlers
|
|
81
108
|
};
|
|
82
109
|
//# sourceMappingURL=implementation.node.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/master/implementation.node.ts"],
|
|
4
|
-
"sourcesContent": ["/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable unicorn/text-encoding-identifier-case */\n/// <reference types=\"node\" />\n\nimport { cpus } from 'node:os'\nimport path from 'node:path'\nimport { cwd } from 'node:process'\nimport { isMainThread, Worker as NativeWorker } from 'node:worker_threads'\n\nimport type {\n ImplementationExport, ThreadsWorkerOptions, WorkerImplementation,\n} from '../types/master.ts'\n\n/** Default thread pool size based on the number of CPU cores. */\nexport const defaultPoolSize = cpus().length\n\nfunction resolveScriptPath(scriptPath: string, baseURL?: string) {\n const makeAbsolute = (filePath: string) => {\n return path.isAbsolute(filePath) ? filePath : path.join(baseURL ?? cwd(), filePath)\n }\n\n const absolutePath = makeAbsolute(scriptPath)\n return absolutePath\n}\n\
|
|
5
|
-
"mappings": ";AAIA,SAAS,YAAY;AACrB,OAAO,UAAU;AACjB,SAAS,WAAW;AACpB,SAAS,cAAc,UAAU,oBAAoB;AAO9C,IAAM,kBAAkB,KAAK,EAAE;
|
|
4
|
+
"sourcesContent": ["/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable unicorn/text-encoding-identifier-case */\n/// <reference types=\"node\" />\n\nimport { cpus } from 'node:os'\nimport path from 'node:path'\nimport { cwd } from 'node:process'\nimport { isMainThread, Worker as NativeWorker } from 'node:worker_threads'\n\nimport type {\n ImplementationExport, ThreadsWorkerOptions, WorkerImplementation,\n} from '../types/master.ts'\n\n/** Default thread pool size based on the number of CPU cores. */\nexport const defaultPoolSize = cpus().length\n\n/** Options for optional process-signal integration with the worker registry. */\nexport interface WorkerSignalHandlersOptions {\n /**\n * When true, call `process.exit(0|1)` after workers terminate on a signal.\n * Default `false` \u2014 the host owns process lifetime.\n */\n exitProcess?: boolean\n /**\n * Signals to listen for. Defaults to `SIGINT` and `SIGTERM`.\n */\n signals?: NodeJS.Signals[]\n}\n\ntype SignalHandler = () => void\n\ninterface WorkerRegistry {\n allWorkers: NativeWorker[]\n /** Active signal \u2192 handler map for the optional opt-in integration. */\n signalHandlers: Map<NodeJS.Signals, SignalHandler>\n}\n\nfunction resolveScriptPath(scriptPath: string, baseURL?: string) {\n const makeAbsolute = (filePath: string) => {\n return path.isAbsolute(filePath) ? filePath : path.join(baseURL ?? cwd(), filePath)\n }\n\n const absolutePath = makeAbsolute(scriptPath)\n return absolutePath\n}\n\n/**\n * Shared registry so opt-in signal handlers can terminate every worker\n * created through this implementation without owning process lifetime.\n */\nconst registry: WorkerRegistry = {\n allWorkers: [],\n signalHandlers: new Map(),\n}\n\nasync function terminateAllWorkers(): Promise<void> {\n const workers = [...registry.allWorkers]\n registry.allWorkers = []\n await Promise.all(workers.map(worker => worker.terminate()))\n}\n\nfunction initWorkerThreadsWorker(): ImplementationExport {\n class Worker extends NativeWorker {\n private mappedEventListeners: WeakMap<EventListener, EventListener>\n\n constructor(scriptPath: string, options?: ThreadsWorkerOptions & { fromSource: boolean }) {\n const resolvedScriptPath = options?.fromSource === true ? null : resolveScriptPath(scriptPath, options?._baseURL)\n if (resolvedScriptPath === null) {\n // `options.fromSource` is true\n const sourceCode = scriptPath\n super(sourceCode, { ...options, eval: true })\n } else {\n super(resolvedScriptPath, options)\n }\n\n this.mappedEventListeners = new WeakMap()\n registry.allWorkers.push(this)\n this.once('exit', () => {\n registry.allWorkers = registry.allWorkers.filter(worker => worker !== this)\n })\n }\n\n addEventListener(eventName: string, rawListener: EventListener) {\n const listener = (message: any) => {\n rawListener({ data: message } as any)\n }\n this.mappedEventListeners.set(rawListener, listener)\n this.on(eventName, listener)\n }\n\n removeEventListener(eventName: string, rawListener: EventListener) {\n const listener = this.mappedEventListeners.get(rawListener) || rawListener\n this.off(eventName, listener)\n }\n }\n\n class BlobWorker extends Worker {\n constructor(blob: Uint8Array, options?: ThreadsWorkerOptions) {\n super(Buffer.from(blob).toString('utf-8'), { ...options, fromSource: true })\n }\n\n static fromText(source: string, options?: ThreadsWorkerOptions): WorkerImplementation {\n return new Worker(source, { ...options, fromSource: true }) as any\n }\n }\n\n return {\n blob: BlobWorker as any,\n default: Worker as any,\n }\n}\n\nlet implementation: ImplementationExport\n\n/**\n * Get the Node.js-specific worker implementation using `worker_threads`, lazily initializing it on first call.\n * @returns The platform-specific worker implementation export.\n */\nexport function getWorkerImplementation(): ImplementationExport {\n if (implementation === undefined) {\n implementation = initWorkerThreadsWorker()\n }\n return implementation\n}\n\n/**\n * Check whether the current code is running inside a Node.js worker thread.\n * @returns True if running in a worker thread (not the main thread), false otherwise.\n */\nexport function isWorkerRuntime() {\n return !isMainThread\n}\n\n/**\n * Opt-in: terminate tracked workers when the process receives the given signals.\n *\n * Libraries must not own host process lifetime. Call this only from an\n * application entrypoint that wants automatic worker cleanup on signal.\n * Returns a disposer that removes the handlers (idempotent).\n *\n * By default does **not** call `process.exit` \u2014 the host chooses exit status\n * and may complete async cleanup after the disposer or after the signal.\n */\nexport function installWorkerSignalHandlers(\n options: WorkerSignalHandlersOptions = {},\n): () => void {\n // Ensure the registry is initialized even if no Worker has been constructed yet.\n getWorkerImplementation()\n\n const signals: NodeJS.Signals[] = options.signals ?? ['SIGINT', 'SIGTERM']\n const exitProcess = options.exitProcess === true\n\n // Idempotent: replace any prior installation for the same process.\n uninstallWorkerSignalHandlers()\n\n for (const signal of signals) {\n const handler: SignalHandler = () => {\n void (async () => {\n try {\n await terminateAllWorkers()\n if (exitProcess) process.exit(0)\n } catch {\n if (exitProcess) process.exit(1)\n }\n })()\n }\n process.on(signal, handler)\n registry.signalHandlers.set(signal, handler)\n }\n\n return uninstallWorkerSignalHandlers\n}\n\n/**\n * Remove handlers installed by {@link installWorkerSignalHandlers}.\n * Safe to call when none are installed.\n */\nexport function uninstallWorkerSignalHandlers(): void {\n for (const [signal, handler] of registry.signalHandlers) {\n process.off(signal, handler)\n }\n registry.signalHandlers.clear()\n}\n"],
|
|
5
|
+
"mappings": "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",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -11,5 +11,6 @@ export type Worker = WorkerType;
|
|
|
11
11
|
export declare const BlobWorker: typeof BlobWorkerClass;
|
|
12
12
|
/** Worker implementation. Either web worker or a Node.js Worker class. */
|
|
13
13
|
export declare const Worker: typeof import("../types/master.ts").WorkerImplementation;
|
|
14
|
-
export {
|
|
14
|
+
export type { WorkerSignalHandlersOptions } from './implementation.node.ts';
|
|
15
|
+
export { installWorkerSignalHandlers, isWorkerRuntime, uninstallWorkerSignalHandlers, } from './implementation.node.ts';
|
|
15
16
|
//# sourceMappingURL=index-node.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index-node.d.ts","sourceRoot":"","sources":["../../../src/master/index-node.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,IAAI,eAAe,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAG7F,YAAY,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtE,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAA;AACxC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEpC,mEAAmE;AACnE,MAAM,MAAM,UAAU,GAAG,OAAO,eAAe,CAAA;AAC/C,4DAA4D;AAC5D,MAAM,MAAM,MAAM,GAAG,UAAU,CAAA;AAE/B,yEAAyE;AACzE,eAAO,MAAM,UAAU,wBAAoD,CAAA;AAE3E,0EAA0E;AAC1E,eAAO,MAAM,MAAM,0DAAuD,CAAA;AAE1E,
|
|
1
|
+
{"version":3,"file":"index-node.d.ts","sourceRoot":"","sources":["../../../src/master/index-node.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,IAAI,eAAe,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAG7F,YAAY,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACtE,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAA;AACxC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEpC,mEAAmE;AACnE,MAAM,MAAM,UAAU,GAAG,OAAO,eAAe,CAAA;AAC/C,4DAA4D;AAC5D,MAAM,MAAM,MAAM,GAAG,UAAU,CAAA;AAE/B,yEAAyE;AACzE,eAAO,MAAM,UAAU,wBAAoD,CAAA;AAE3E,0EAA0E;AAC1E,eAAO,MAAM,MAAM,0DAAuD,CAAA;AAE1E,YAAY,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAA;AAC3E,OAAO,EACL,2BAA2B,EAC3B,eAAe,EACf,6BAA6B,GAC9B,MAAM,0BAA0B,CAAA"}
|
|
@@ -11,8 +11,16 @@ function resolveScriptPath(scriptPath, baseURL) {
|
|
|
11
11
|
const absolutePath = makeAbsolute(scriptPath);
|
|
12
12
|
return absolutePath;
|
|
13
13
|
}
|
|
14
|
+
var registry = {
|
|
15
|
+
allWorkers: [],
|
|
16
|
+
signalHandlers: /* @__PURE__ */ new Map()
|
|
17
|
+
};
|
|
18
|
+
async function terminateAllWorkers() {
|
|
19
|
+
const workers = [...registry.allWorkers];
|
|
20
|
+
registry.allWorkers = [];
|
|
21
|
+
await Promise.all(workers.map((worker) => worker.terminate()));
|
|
22
|
+
}
|
|
14
23
|
function initWorkerThreadsWorker() {
|
|
15
|
-
let allWorkers = [];
|
|
16
24
|
class Worker3 extends NativeWorker {
|
|
17
25
|
mappedEventListeners;
|
|
18
26
|
constructor(scriptPath, options) {
|
|
@@ -24,7 +32,10 @@ function initWorkerThreadsWorker() {
|
|
|
24
32
|
super(resolvedScriptPath, options);
|
|
25
33
|
}
|
|
26
34
|
this.mappedEventListeners = /* @__PURE__ */ new WeakMap();
|
|
27
|
-
allWorkers.push(this);
|
|
35
|
+
registry.allWorkers.push(this);
|
|
36
|
+
this.once("exit", () => {
|
|
37
|
+
registry.allWorkers = registry.allWorkers.filter((worker) => worker !== this);
|
|
38
|
+
});
|
|
28
39
|
}
|
|
29
40
|
addEventListener(eventName, rawListener) {
|
|
30
41
|
const listener = (message) => {
|
|
@@ -38,19 +49,6 @@ function initWorkerThreadsWorker() {
|
|
|
38
49
|
this.off(eventName, listener);
|
|
39
50
|
}
|
|
40
51
|
}
|
|
41
|
-
const terminateWorkersAndMaster = () => {
|
|
42
|
-
void (async () => {
|
|
43
|
-
try {
|
|
44
|
-
await Promise.all(allWorkers.map((worker) => worker.terminate()));
|
|
45
|
-
process.exit(0);
|
|
46
|
-
} catch {
|
|
47
|
-
process.exit(1);
|
|
48
|
-
}
|
|
49
|
-
})();
|
|
50
|
-
allWorkers = [];
|
|
51
|
-
};
|
|
52
|
-
process.on("SIGINT", () => terminateWorkersAndMaster());
|
|
53
|
-
process.on("SIGTERM", () => terminateWorkersAndMaster());
|
|
54
52
|
class BlobWorker2 extends Worker3 {
|
|
55
53
|
constructor(blob, options) {
|
|
56
54
|
super(Buffer.from(blob).toString("utf-8"), { ...options, fromSource: true });
|
|
@@ -74,6 +72,33 @@ function getWorkerImplementation() {
|
|
|
74
72
|
function isWorkerRuntime() {
|
|
75
73
|
return !isMainThread;
|
|
76
74
|
}
|
|
75
|
+
function installWorkerSignalHandlers(options = {}) {
|
|
76
|
+
getWorkerImplementation();
|
|
77
|
+
const signals = options.signals ?? ["SIGINT", "SIGTERM"];
|
|
78
|
+
const exitProcess = options.exitProcess === true;
|
|
79
|
+
uninstallWorkerSignalHandlers();
|
|
80
|
+
for (const signal of signals) {
|
|
81
|
+
const handler = () => {
|
|
82
|
+
void (async () => {
|
|
83
|
+
try {
|
|
84
|
+
await terminateAllWorkers();
|
|
85
|
+
if (exitProcess) process.exit(0);
|
|
86
|
+
} catch {
|
|
87
|
+
if (exitProcess) process.exit(1);
|
|
88
|
+
}
|
|
89
|
+
})();
|
|
90
|
+
};
|
|
91
|
+
process.on(signal, handler);
|
|
92
|
+
registry.signalHandlers.set(signal, handler);
|
|
93
|
+
}
|
|
94
|
+
return uninstallWorkerSignalHandlers;
|
|
95
|
+
}
|
|
96
|
+
function uninstallWorkerSignalHandlers() {
|
|
97
|
+
for (const [signal, handler] of registry.signalHandlers) {
|
|
98
|
+
process.off(signal, handler);
|
|
99
|
+
}
|
|
100
|
+
registry.signalHandlers.clear();
|
|
101
|
+
}
|
|
77
102
|
|
|
78
103
|
// src/master/pool-browser.ts
|
|
79
104
|
import DebugLogger from "debug";
|
|
@@ -811,7 +836,9 @@ export {
|
|
|
811
836
|
Pool,
|
|
812
837
|
Thread,
|
|
813
838
|
Worker2 as Worker,
|
|
839
|
+
installWorkerSignalHandlers,
|
|
814
840
|
isWorkerRuntime,
|
|
815
|
-
spawn
|
|
841
|
+
spawn,
|
|
842
|
+
uninstallWorkerSignalHandlers
|
|
816
843
|
};
|
|
817
844
|
//# sourceMappingURL=index-node.mjs.map
|