@ricsam/r5d-worker 0.0.29 → 0.0.31

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.
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var process_tree_exports = {};
20
+ __export(process_tree_exports, {
21
+ terminateProcessTree: () => terminateProcessTree
22
+ });
23
+ module.exports = __toCommonJS(process_tree_exports);
24
+ const DEFAULT_TERMINATION_GRACE_MS = 2e3;
25
+ const DEFAULT_FORCE_KILL_WAIT_MS = 2e3;
26
+ const PROCESS_GROUP_POLL_INTERVAL_MS = 25;
27
+ const processTerminations = /* @__PURE__ */ new WeakMap();
28
+ function errorCode(error) {
29
+ if (!error || typeof error !== "object" || !("code" in error)) {
30
+ return void 0;
31
+ }
32
+ return typeof error.code === "string" ? error.code : void 0;
33
+ }
34
+ function delay(ms) {
35
+ return new Promise((resolve) => setTimeout(resolve, ms));
36
+ }
37
+ function isPosixProcessGroupRunning(processGroupId) {
38
+ try {
39
+ process.kill(-processGroupId, 0);
40
+ return true;
41
+ } catch (error) {
42
+ if (errorCode(error) === "ESRCH") {
43
+ return false;
44
+ }
45
+ if (errorCode(error) === "EPERM") {
46
+ return true;
47
+ }
48
+ throw error;
49
+ }
50
+ }
51
+ function signalPosixProcessGroup(processGroupId, signal) {
52
+ try {
53
+ process.kill(-processGroupId, signal);
54
+ } catch (error) {
55
+ if (errorCode(error) !== "ESRCH") {
56
+ throw error;
57
+ }
58
+ }
59
+ }
60
+ async function waitForPosixProcessGroupExit(processGroupId, timeoutMs) {
61
+ const deadline = Date.now() + Math.max(0, timeoutMs);
62
+ while (isPosixProcessGroupRunning(processGroupId)) {
63
+ const remainingMs = deadline - Date.now();
64
+ if (remainingMs <= 0) {
65
+ return false;
66
+ }
67
+ await delay(Math.min(PROCESS_GROUP_POLL_INTERVAL_MS, remainingMs));
68
+ }
69
+ return true;
70
+ }
71
+ async function runWindowsTaskkill(pid) {
72
+ const taskkill = Bun.spawn(["taskkill", "/PID", String(pid), "/T", "/F"], {
73
+ stdout: "ignore",
74
+ stderr: "pipe"
75
+ });
76
+ const [exitCode, stderr] = await Promise.all([
77
+ taskkill.exited,
78
+ taskkill.stderr ? new Response(taskkill.stderr).text() : Promise.resolve("")
79
+ ]);
80
+ if (exitCode !== 0 && !/not found|no running instance/i.test(stderr)) {
81
+ throw new Error(`Failed to terminate process tree ${pid}: ${stderr.trim() || `taskkill exited ${exitCode}`}`);
82
+ }
83
+ }
84
+ async function terminateProcessTreeOnce(subprocess, options) {
85
+ if (!Number.isSafeInteger(subprocess.pid) || subprocess.pid <= 0) {
86
+ throw new Error(`Cannot terminate invalid subprocess PID: ${subprocess.pid}`);
87
+ }
88
+ if (process.platform === "win32") {
89
+ await runWindowsTaskkill(subprocess.pid);
90
+ return;
91
+ }
92
+ const graceMs = options.graceMs ?? DEFAULT_TERMINATION_GRACE_MS;
93
+ const forceKillWaitMs = options.forceKillWaitMs ?? DEFAULT_FORCE_KILL_WAIT_MS;
94
+ signalPosixProcessGroup(subprocess.pid, "SIGTERM");
95
+ if (await waitForPosixProcessGroupExit(subprocess.pid, graceMs)) {
96
+ return;
97
+ }
98
+ signalPosixProcessGroup(subprocess.pid, "SIGKILL");
99
+ if (!await waitForPosixProcessGroupExit(subprocess.pid, forceKillWaitMs)) {
100
+ throw new Error(`Process group ${subprocess.pid} remained active after SIGKILL`);
101
+ }
102
+ }
103
+ function terminateProcessTree(subprocess, options = {}) {
104
+ const pending = processTerminations.get(subprocess);
105
+ if (pending) {
106
+ return pending;
107
+ }
108
+ const termination = terminateProcessTreeOnce(subprocess, options).finally(() => {
109
+ processTerminations.delete(subprocess);
110
+ });
111
+ processTerminations.set(subprocess, termination);
112
+ return termination;
113
+ }
114
+ // Annotate the CommonJS export names for ESM import in node:
115
+ 0 && (module.exports = {
116
+ terminateProcessTree
117
+ });