@gtkx/utils 1.0.0-rc.3 → 1.0.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.
Files changed (256) hide show
  1. package/README.md +8 -9
  2. package/dist/array/index-before-or-end.d.ts +0 -12
  3. package/dist/array/index-before-or-end.d.ts.map +1 -1
  4. package/dist/array/index-before-or-end.js +0 -12
  5. package/dist/array/index-before-or-end.js.map +1 -1
  6. package/dist/array/index.d.ts +5 -5
  7. package/dist/array/index.js.map +1 -1
  8. package/dist/array/remove.d.ts +0 -11
  9. package/dist/array/remove.d.ts.map +1 -1
  10. package/dist/array/remove.js +0 -11
  11. package/dist/array/remove.js.map +1 -1
  12. package/dist/array/sort-strings-by.d.ts +0 -12
  13. package/dist/array/sort-strings-by.d.ts.map +1 -1
  14. package/dist/array/sort-strings-by.js +0 -12
  15. package/dist/array/sort-strings-by.js.map +1 -1
  16. package/dist/array/sort-strings.d.ts +0 -9
  17. package/dist/array/sort-strings.d.ts.map +1 -1
  18. package/dist/array/sort-strings.js +0 -9
  19. package/dist/array/sort-strings.js.map +1 -1
  20. package/dist/array/uniq-by.d.ts +0 -15
  21. package/dist/array/uniq-by.d.ts.map +1 -1
  22. package/dist/array/uniq-by.js +0 -15
  23. package/dist/array/uniq-by.js.map +1 -1
  24. package/dist/class/any-class.d.ts +2 -3
  25. package/dist/class/any-class.d.ts.map +1 -1
  26. package/dist/class/any-class.js.map +1 -1
  27. package/dist/class/get-parent-class.d.ts +1 -7
  28. package/dist/class/get-parent-class.d.ts.map +1 -1
  29. package/dist/class/get-parent-class.js +0 -6
  30. package/dist/class/get-parent-class.js.map +1 -1
  31. package/dist/class/index.d.ts +3 -3
  32. package/dist/class/index.js.map +1 -1
  33. package/dist/class/walk-class-chain.d.ts +1 -10
  34. package/dist/class/walk-class-chain.d.ts.map +1 -1
  35. package/dist/class/walk-class-chain.js +0 -9
  36. package/dist/class/walk-class-chain.js.map +1 -1
  37. package/dist/error/error-like.js.map +1 -1
  38. package/dist/error/error-message.d.ts +0 -10
  39. package/dist/error/error-message.d.ts.map +1 -1
  40. package/dist/error/error-message.js +0 -10
  41. package/dist/error/error-message.js.map +1 -1
  42. package/dist/error/format-child-process-error.d.ts +0 -9
  43. package/dist/error/format-child-process-error.d.ts.map +1 -1
  44. package/dist/error/format-child-process-error.js +0 -9
  45. package/dist/error/format-child-process-error.js.map +1 -1
  46. package/dist/error/index.d.ts +3 -3
  47. package/dist/error/index.js.map +1 -1
  48. package/dist/error/normalize-error.d.ts +0 -10
  49. package/dist/error/normalize-error.d.ts.map +1 -1
  50. package/dist/error/normalize-error.js +0 -10
  51. package/dist/error/normalize-error.js.map +1 -1
  52. package/dist/index.d.ts +12 -13
  53. package/dist/index.d.ts.map +1 -1
  54. package/dist/index.js +0 -1
  55. package/dist/index.js.map +1 -1
  56. package/dist/log/create-logger.d.ts +1 -12
  57. package/dist/log/create-logger.d.ts.map +1 -1
  58. package/dist/log/create-logger.js +0 -11
  59. package/dist/log/create-logger.js.map +1 -1
  60. package/dist/log/default-logger.d.ts +4 -30
  61. package/dist/log/default-logger.d.ts.map +1 -1
  62. package/dist/log/default-logger.js +3 -29
  63. package/dist/log/default-logger.js.map +1 -1
  64. package/dist/log/index.d.ts +5 -3
  65. package/dist/log/index.d.ts.map +1 -1
  66. package/dist/log/index.js +3 -1
  67. package/dist/log/index.js.map +1 -1
  68. package/dist/log/logger.d.ts +2 -43
  69. package/dist/log/logger.d.ts.map +1 -1
  70. package/dist/log/logger.js +3 -34
  71. package/dist/log/logger.js.map +1 -1
  72. package/dist/map/get-or-insert.d.ts +1 -16
  73. package/dist/map/get-or-insert.d.ts.map +1 -1
  74. package/dist/map/get-or-insert.js +0 -15
  75. package/dist/map/get-or-insert.js.map +1 -1
  76. package/dist/map/index.d.ts +1 -1
  77. package/dist/map/index.js.map +1 -1
  78. package/dist/object/index.d.ts +3 -3
  79. package/dist/object/index.js.map +1 -1
  80. package/dist/object/omit.d.ts +0 -12
  81. package/dist/object/omit.d.ts.map +1 -1
  82. package/dist/object/omit.js +0 -12
  83. package/dist/object/omit.js.map +1 -1
  84. package/dist/object/pick-by.d.ts +0 -12
  85. package/dist/object/pick-by.d.ts.map +1 -1
  86. package/dist/object/pick-by.js +0 -12
  87. package/dist/object/pick-by.js.map +1 -1
  88. package/dist/object/structured-clone.d.ts +0 -16
  89. package/dist/object/structured-clone.d.ts.map +1 -1
  90. package/dist/object/structured-clone.js +0 -16
  91. package/dist/object/structured-clone.js.map +1 -1
  92. package/dist/predicate/index.d.ts +6 -6
  93. package/dist/predicate/index.js.map +1 -1
  94. package/dist/predicate/is-deep-equal.d.ts.map +1 -1
  95. package/dist/predicate/is-deep-equal.js +0 -13
  96. package/dist/predicate/is-deep-equal.js.map +1 -1
  97. package/dist/predicate/is-plain-object.d.ts +0 -14
  98. package/dist/predicate/is-plain-object.d.ts.map +1 -1
  99. package/dist/predicate/is-plain-object.js +0 -14
  100. package/dist/predicate/is-plain-object.js.map +1 -1
  101. package/dist/predicate/is-record.d.ts +0 -11
  102. package/dist/predicate/is-record.d.ts.map +1 -1
  103. package/dist/predicate/is-record.js +0 -11
  104. package/dist/predicate/is-record.js.map +1 -1
  105. package/dist/predicate/is-same-array-by.d.ts +0 -12
  106. package/dist/predicate/is-same-array-by.d.ts.map +1 -1
  107. package/dist/predicate/is-same-array-by.js +0 -12
  108. package/dist/predicate/is-same-array-by.js.map +1 -1
  109. package/dist/predicate/is-same-array.d.ts +0 -12
  110. package/dist/predicate/is-same-array.d.ts.map +1 -1
  111. package/dist/predicate/is-same-array.js +0 -12
  112. package/dist/predicate/is-same-array.js.map +1 -1
  113. package/dist/predicate/is-shallow-equal.d.ts.map +1 -1
  114. package/dist/predicate/is-shallow-equal.js +0 -14
  115. package/dist/predicate/is-shallow-equal.js.map +1 -1
  116. package/dist/process/exit-code-for-signal.d.ts +0 -11
  117. package/dist/process/exit-code-for-signal.d.ts.map +1 -1
  118. package/dist/process/exit-code-for-signal.js +0 -11
  119. package/dist/process/exit-code-for-signal.js.map +1 -1
  120. package/dist/process/index.d.ts +4 -3
  121. package/dist/process/index.d.ts.map +1 -1
  122. package/dist/process/index.js +2 -1
  123. package/dist/process/index.js.map +1 -1
  124. package/dist/process/install-graceful-shutdown.d.ts +1 -7
  125. package/dist/process/install-graceful-shutdown.d.ts.map +1 -1
  126. package/dist/process/install-graceful-shutdown.js +7 -13
  127. package/dist/process/install-graceful-shutdown.js.map +1 -1
  128. package/dist/process/kill-marked-processes.d.ts +4 -0
  129. package/dist/process/kill-marked-processes.d.ts.map +1 -0
  130. package/dist/process/kill-marked-processes.js +38 -0
  131. package/dist/process/kill-marked-processes.js.map +1 -0
  132. package/dist/process/process-guard.d.ts +2 -0
  133. package/dist/process/process-guard.d.ts.map +1 -0
  134. package/dist/process/process-guard.js +16 -0
  135. package/dist/process/process-guard.js.map +1 -0
  136. package/dist/process/resolve-executable.d.ts +2 -12
  137. package/dist/process/resolve-executable.d.ts.map +1 -1
  138. package/dist/process/resolve-executable.js +6 -14
  139. package/dist/process/resolve-executable.js.map +1 -1
  140. package/dist/process/spawn-with-parent-death-signal.d.ts +8 -0
  141. package/dist/process/spawn-with-parent-death-signal.d.ts.map +1 -0
  142. package/dist/process/spawn-with-parent-death-signal.js +50 -0
  143. package/dist/process/spawn-with-parent-death-signal.js.map +1 -0
  144. package/dist/set/drain.d.ts +0 -8
  145. package/dist/set/drain.d.ts.map +1 -1
  146. package/dist/set/drain.js +0 -8
  147. package/dist/set/drain.js.map +1 -1
  148. package/dist/set/index.d.ts +1 -1
  149. package/dist/set/index.js.map +1 -1
  150. package/dist/source/index.d.ts +3 -3
  151. package/dist/source/index.js.map +1 -1
  152. package/dist/source/sanitize-identifier.d.ts +0 -11
  153. package/dist/source/sanitize-identifier.d.ts.map +1 -1
  154. package/dist/source/sanitize-identifier.js +0 -11
  155. package/dist/source/sanitize-identifier.js.map +1 -1
  156. package/dist/source/source-string-literal.d.ts +0 -10
  157. package/dist/source/source-string-literal.d.ts.map +1 -1
  158. package/dist/source/source-string-literal.js +0 -10
  159. package/dist/source/source-string-literal.js.map +1 -1
  160. package/dist/source/to-camel-identifier.d.ts +0 -10
  161. package/dist/source/to-camel-identifier.d.ts.map +1 -1
  162. package/dist/source/to-camel-identifier.js +0 -10
  163. package/dist/source/to-camel-identifier.js.map +1 -1
  164. package/dist/string/camel-case.d.ts +0 -14
  165. package/dist/string/camel-case.d.ts.map +1 -1
  166. package/dist/string/camel-case.js +0 -14
  167. package/dist/string/camel-case.js.map +1 -1
  168. package/dist/string/index.d.ts +5 -5
  169. package/dist/string/index.js.map +1 -1
  170. package/dist/string/kebab-case.d.ts +0 -11
  171. package/dist/string/kebab-case.d.ts.map +1 -1
  172. package/dist/string/kebab-case.js +0 -11
  173. package/dist/string/kebab-case.js.map +1 -1
  174. package/dist/string/lower-first.d.ts +0 -10
  175. package/dist/string/lower-first.d.ts.map +1 -1
  176. package/dist/string/lower-first.js +0 -10
  177. package/dist/string/lower-first.js.map +1 -1
  178. package/dist/string/pascal-case.d.ts +0 -13
  179. package/dist/string/pascal-case.d.ts.map +1 -1
  180. package/dist/string/pascal-case.js +0 -13
  181. package/dist/string/pascal-case.js.map +1 -1
  182. package/dist/string/upper-first.d.ts +0 -10
  183. package/dist/string/upper-first.d.ts.map +1 -1
  184. package/dist/string/upper-first.js +0 -10
  185. package/dist/string/upper-first.js.map +1 -1
  186. package/dist/util/index.d.ts +1 -1
  187. package/dist/util/index.js.map +1 -1
  188. package/dist/util/package-version.d.ts +1 -10
  189. package/dist/util/package-version.d.ts.map +1 -1
  190. package/dist/util/package-version.js +2 -11
  191. package/dist/util/package-version.js.map +1 -1
  192. package/package.json +1 -1
  193. package/src/array/index-before-or-end.ts +0 -12
  194. package/src/array/index.ts +5 -5
  195. package/src/array/remove.ts +0 -11
  196. package/src/array/sort-strings-by.ts +0 -12
  197. package/src/array/sort-strings.ts +0 -9
  198. package/src/array/uniq-by.ts +0 -15
  199. package/src/class/any-class.ts +2 -3
  200. package/src/class/get-parent-class.ts +1 -7
  201. package/src/class/index.ts +3 -3
  202. package/src/class/walk-class-chain.ts +2 -11
  203. package/src/error/error-like.ts +1 -1
  204. package/src/error/error-message.ts +1 -11
  205. package/src/error/format-child-process-error.ts +2 -11
  206. package/src/error/index.ts +3 -3
  207. package/src/error/normalize-error.ts +2 -12
  208. package/src/index.ts +12 -13
  209. package/src/log/create-logger.ts +1 -12
  210. package/src/log/default-logger.ts +4 -30
  211. package/src/log/index.ts +5 -3
  212. package/src/log/logger.ts +4 -45
  213. package/src/map/get-or-insert.ts +1 -16
  214. package/src/map/index.ts +1 -1
  215. package/src/object/index.ts +3 -3
  216. package/src/object/omit.ts +0 -12
  217. package/src/object/pick-by.ts +0 -12
  218. package/src/object/structured-clone.ts +1 -17
  219. package/src/predicate/index.ts +6 -6
  220. package/src/predicate/is-deep-equal.ts +2 -15
  221. package/src/predicate/is-plain-object.ts +0 -14
  222. package/src/predicate/is-record.ts +0 -11
  223. package/src/predicate/is-same-array-by.ts +0 -12
  224. package/src/predicate/is-same-array.ts +1 -13
  225. package/src/predicate/is-shallow-equal.ts +2 -16
  226. package/src/process/exit-code-for-signal.ts +0 -11
  227. package/src/process/index.ts +4 -3
  228. package/src/process/install-graceful-shutdown.ts +12 -18
  229. package/src/process/kill-marked-processes.ts +44 -0
  230. package/src/process/process-guard.ts +20 -0
  231. package/src/process/resolve-executable.ts +7 -14
  232. package/src/process/spawn-with-parent-death-signal.ts +74 -0
  233. package/src/set/drain.ts +0 -8
  234. package/src/set/index.ts +1 -1
  235. package/src/source/index.ts +3 -3
  236. package/src/source/sanitize-identifier.ts +0 -11
  237. package/src/source/source-string-literal.ts +0 -10
  238. package/src/source/to-camel-identifier.ts +2 -12
  239. package/src/string/camel-case.ts +2 -16
  240. package/src/string/index.ts +5 -5
  241. package/src/string/kebab-case.ts +0 -11
  242. package/src/string/lower-first.ts +0 -10
  243. package/src/string/pascal-case.ts +2 -15
  244. package/src/string/upper-first.ts +0 -10
  245. package/src/util/index.ts +1 -1
  246. package/src/util/package-version.ts +2 -11
  247. package/dist/function/call-method.d.ts +0 -3
  248. package/dist/function/call-method.d.ts.map +0 -1
  249. package/dist/function/call-method.js +0 -19
  250. package/dist/function/call-method.js.map +0 -1
  251. package/dist/function/index.d.ts +0 -2
  252. package/dist/function/index.d.ts.map +0 -1
  253. package/dist/function/index.js +0 -2
  254. package/dist/function/index.js.map +0 -1
  255. package/src/function/call-method.ts +0 -22
  256. package/src/function/index.ts +0 -1
@@ -1,17 +1,3 @@
1
- /**
2
- * Checks whether a value is a plain object, i.e. one whose prototype is `Object.prototype` or `null`.
3
- *
4
- * Class instances, arrays, and other exotic objects return `false`.
5
- *
6
- * @param value - The value to test.
7
- * @returns `true` when `value` is a plain object.
8
- *
9
- * @example
10
- * isPlainObject({ a: 1 }); // true
11
- * isPlainObject(Object.create(null)); // true
12
- * isPlainObject([1, 2]); // false
13
- * isPlainObject(new Date()); // false
14
- */
15
1
  function isPlainObject(value: unknown): value is Record<string, unknown> {
16
2
  if (typeof value !== "object" || value === null) {
17
3
  return false;
@@ -1,14 +1,3 @@
1
- /**
2
- * Checks whether a value is a non-null object (including arrays and class instances, excluding `null`).
3
- *
4
- * @param value - The value to test.
5
- * @returns `true` when `value` is a non-null object.
6
- *
7
- * @example
8
- * isRecord({ a: 1 }); // true
9
- * isRecord(null); // false
10
- * isRecord("x"); // false
11
- */
12
1
  function isRecord(value: unknown): value is Record<string, unknown> {
13
2
  return typeof value === "object" && value !== null;
14
3
  }
@@ -1,15 +1,3 @@
1
- /**
2
- * Checks whether two arrays have the same length and elements the comparator treats as equal, in order.
3
- *
4
- * @template T - The type of the array elements.
5
- * @param a - The first array.
6
- * @param b - The second array.
7
- * @param isEqual - Compares elements at the same index; return `true` when they are equal.
8
- * @returns `true` when the arrays are element-wise equal under `isEqual`.
9
- *
10
- * @example
11
- * isSameArrayBy([{ id: 1 }], [{ id: 1 }], (x, y) => x.id === y.id); // true
12
- */
13
1
  function isSameArrayBy<T>(a: T[], b: T[], isEqual: (x: T, y: T) => boolean): boolean {
14
2
  if (a.length !== b.length) {
15
3
  return false;
@@ -1,17 +1,5 @@
1
- import { isSameArrayBy } from "./is-same-array-by.js";
1
+ import { isSameArrayBy } from "./is-same-array-by.ts";
2
2
 
3
- /**
4
- * Checks whether two arrays have the same length and strictly equal (`===`) elements in order.
5
- *
6
- * @template T - The type of the array elements.
7
- * @param a - The first array.
8
- * @param b - The second array.
9
- * @returns `true` when the arrays are element-wise strictly equal.
10
- *
11
- * @example
12
- * isSameArray([1, 2], [1, 2]); // true
13
- * isSameArray([1, 2], [2, 1]); // false
14
- */
15
3
  function isSameArray<T>(a: T[], b: T[]): boolean {
16
4
  return isSameArrayBy(a, b, (x, y) => x === y);
17
5
  }
@@ -1,20 +1,6 @@
1
- import { areObjectKeysEqual } from "./are-object-keys-equal.js";
2
- import { isPlainObject } from "./is-plain-object.js";
1
+ import { areObjectKeysEqual } from "./are-object-keys-equal.ts";
2
+ import { isPlainObject } from "./is-plain-object.ts";
3
3
 
4
- /**
5
- * Checks whether two values are shallowly equal: identical, or plain objects with the same own
6
- * enumerable keys and strictly equal (`===`) values. Values that are not both plain objects are
7
- * equal only when identical.
8
- *
9
- * @param a - The first value.
10
- * @param b - The second value.
11
- * @returns `true` when the values are shallowly equal.
12
- *
13
- * @example
14
- * isShallowEqual({ a: 1 }, { a: 1 }); // true
15
- * isShallowEqual({ a: 1 }, { a: 2 }); // false
16
- * isShallowEqual(3, 3); // true
17
- */
18
4
  const isStrictEqual = (a: unknown, b: unknown): boolean => a === b;
19
5
 
20
6
  function isShallowEqual(a: unknown, b: unknown): boolean {
@@ -1,14 +1,3 @@
1
- /**
2
- * Maps a terminating signal to its conventional process exit code (130 for `SIGINT`, 143 otherwise),
3
- * or 0 when no signal is given.
4
- *
5
- * @param signal - The signal that triggered termination, or `null`.
6
- * @returns The conventional exit code for the signal.
7
- *
8
- * @example
9
- * exitCodeForSignal("SIGINT"); // 130
10
- * exitCodeForSignal(null); // 0
11
- */
12
1
  function exitCodeForSignal(signal: NodeJS.Signals | null): number {
13
2
  if (!signal) {
14
3
  return 0;
@@ -1,3 +1,4 @@
1
- export { exitCodeForSignal } from "./exit-code-for-signal.js";
2
- export { installGracefulShutdown } from "./install-graceful-shutdown.js";
3
- export { resolveExecutable } from "./resolve-executable.js";
1
+ export { exitCodeForSignal } from "./exit-code-for-signal.ts";
2
+ export { installGracefulShutdown } from "./install-graceful-shutdown.ts";
3
+ export { resolveExecutable, tryResolveExecutable } from "./resolve-executable.ts";
4
+ export { spawnWithParentDeathSignal } from "./spawn-with-parent-death-signal.ts";
@@ -1,5 +1,5 @@
1
- import { error } from "../log/default-logger.js";
2
- import { exitCodeForSignal } from "./exit-code-for-signal.js";
1
+ import { error } from "../log/default-logger.ts";
2
+ import { exitCodeForSignal } from "./exit-code-for-signal.ts";
3
3
 
4
4
  type GracefulShutdownOptions = {
5
5
  onSignal: (signal: NodeJS.Signals) => void | Promise<void>;
@@ -14,8 +14,8 @@ type ShutdownState = {
14
14
  forceKillMs: number;
15
15
  coalesceWindowMs: number;
16
16
  firstSignal: NodeJS.Signals | null;
17
- exited: boolean;
18
- coalescing: boolean;
17
+ hasExited: boolean;
18
+ isCoalescing: boolean;
19
19
  forceTimer: NodeJS.Timeout | null;
20
20
  coalesceTimer: NodeJS.Timeout | null;
21
21
  };
@@ -37,11 +37,11 @@ const clearTimers = (state: ShutdownState): void => {
37
37
  };
38
38
 
39
39
  const finish = (state: ShutdownState, signal: NodeJS.Signals, isGraceful: boolean): void => {
40
- if (state.exited) {
40
+ if (state.hasExited) {
41
41
  return;
42
42
  }
43
43
 
44
- state.exited = true;
44
+ state.hasExited = true;
45
45
  clearTimers(state);
46
46
  const { exitCode } = state.options;
47
47
  const defaultCode = isGraceful ? 0 : exitCodeForSignal(signal);
@@ -62,10 +62,10 @@ const beginShutdown = (state: ShutdownState, signal: NodeJS.Signals): void => {
62
62
  state.firstSignal = signal;
63
63
 
64
64
  if (state.coalesceWindowMs > 0) {
65
- state.coalescing = true;
65
+ state.isCoalescing = true;
66
66
 
67
67
  state.coalesceTimer = setTimeout(() => {
68
- state.coalescing = false;
68
+ state.isCoalescing = false;
69
69
  }, state.coalesceWindowMs);
70
70
 
71
71
  state.coalesceTimer.unref();
@@ -90,7 +90,7 @@ const handle = (state: ShutdownState, signal: NodeJS.Signals): void => {
90
90
  return;
91
91
  }
92
92
 
93
- if (state.coalescing) {
93
+ if (state.isCoalescing) {
94
94
  return;
95
95
  }
96
96
 
@@ -98,20 +98,14 @@ const handle = (state: ShutdownState, signal: NodeJS.Signals): void => {
98
98
  finish(state, signal, false);
99
99
  };
100
100
 
101
- /**
102
- * Registers handlers for `SIGINT`, `SIGTERM`, and `SIGHUP` that run the given cleanup callback once
103
- * and then exit, forcing exit if a repeated signal arrives or the cleanup exceeds its timeout.
104
- *
105
- * @param options - The shutdown callbacks and timing configuration.
106
- */
107
101
  function installGracefulShutdown(options: GracefulShutdownOptions): void {
108
102
  const state: ShutdownState = {
109
103
  options,
110
104
  forceKillMs: options.forceKillAfterMs ?? DEFAULT_FORCE_KILL_TIMEOUT_MS,
111
105
  coalesceWindowMs: options.coalesceWindowMs ?? DEFAULT_COALESCE_WINDOW_MS,
112
106
  firstSignal: null,
113
- exited: false,
114
- coalescing: false,
107
+ hasExited: false,
108
+ isCoalescing: false,
115
109
  forceTimer: null,
116
110
  coalesceTimer: null,
117
111
  };
@@ -123,4 +117,4 @@ function installGracefulShutdown(options: GracefulShutdownOptions): void {
123
117
  }
124
118
  }
125
119
 
126
- export { installGracefulShutdown, type GracefulShutdownOptions };
120
+ export { installGracefulShutdown };
@@ -0,0 +1,44 @@
1
+ import { readdirSync, readFileSync } from "node:fs";
2
+
3
+ const PROCESS_MARKER = "GTKX_PROCESS_GUARD";
4
+ const MAX_KILL_PASSES = 8;
5
+
6
+ const isMarked = (pid: number, prefix: string): boolean => {
7
+ try {
8
+ return readFileSync(`/proc/${String(pid)}/environ`, "utf8")
9
+ .split("\0")
10
+ .some((assignment) => assignment.startsWith(prefix));
11
+ } catch {
12
+ return false;
13
+ }
14
+ };
15
+
16
+ const markedPids = (prefix: string): number[] =>
17
+ readdirSync("/proc")
18
+ .map(Number)
19
+ .filter((pid) => Number.isSafeInteger(pid) && pid > 1 && pid !== process.pid)
20
+ .filter((pid) => isMarked(pid, prefix));
21
+
22
+ const killPid = (pid: number): void => {
23
+ try {
24
+ process.kill(pid, "SIGKILL");
25
+ } catch {
26
+ return;
27
+ }
28
+ };
29
+
30
+ function killMarkedProcesses(prefix: string): void {
31
+ for (let pass = 0; pass < MAX_KILL_PASSES; pass += 1) {
32
+ const pids = markedPids(prefix);
33
+
34
+ if (pids.length === 0) {
35
+ return;
36
+ }
37
+
38
+ for (const pid of pids) {
39
+ killPid(pid);
40
+ }
41
+ }
42
+ }
43
+
44
+ export { PROCESS_MARKER, killMarkedProcesses };
@@ -0,0 +1,20 @@
1
+ import { killMarkedProcesses } from "./kill-marked-processes.ts";
2
+
3
+ const GUARD_PREFIX = process.argv[2] ?? "";
4
+ const WATCHED_SIGNALS = ["SIGTERM", "SIGINT", "SIGHUP"] as const satisfies NodeJS.Signals[];
5
+
6
+ const sweep = (): void => {
7
+ if (GUARD_PREFIX.length > 0) {
8
+ killMarkedProcesses(GUARD_PREFIX);
9
+ }
10
+
11
+ process.exit(0);
12
+ };
13
+
14
+ process.stdin.resume();
15
+ process.stdin.on("end", sweep);
16
+ process.stdin.on("error", sweep);
17
+
18
+ for (const signal of WATCHED_SIGNALS) {
19
+ process.on(signal, sweep);
20
+ }
@@ -25,18 +25,7 @@ const findOnPath = (command: string): string | undefined => {
25
25
  return undefined;
26
26
  };
27
27
 
28
- /**
29
- * Resolves an executable to an absolute path, searching `PATH` only for a bare command name, so
30
- * spawning it never re-runs a lookup against a `PATH` that may have changed.
31
- *
32
- * @param command - An executable name, or a path to one.
33
- * @returns The absolute path of the executable.
34
- * @throws When a bare command name is not found on `PATH`.
35
- *
36
- * @example
37
- * resolveExecutable("weston"); // "/usr/bin/weston"
38
- */
39
- function resolveExecutable(command: string): string {
28
+ function tryResolveExecutable(command: string): string | undefined {
40
29
  if (isAbsolute(command)) {
41
30
  return command;
42
31
  }
@@ -45,7 +34,11 @@ function resolveExecutable(command: string): string {
45
34
  return resolve(command);
46
35
  }
47
36
 
48
- const found = findOnPath(command);
37
+ return findOnPath(command);
38
+ }
39
+
40
+ function resolveExecutable(command: string): string {
41
+ const found = tryResolveExecutable(command);
49
42
 
50
43
  if (found === undefined) {
51
44
  throw new Error(`Cannot find the "${command}" executable on PATH`);
@@ -54,4 +47,4 @@ function resolveExecutable(command: string): string {
54
47
  return found;
55
48
  }
56
49
 
57
- export { resolveExecutable };
50
+ export { resolveExecutable, tryResolveExecutable };
@@ -0,0 +1,74 @@
1
+ import { type ChildProcess, spawn, type StdioOptions } from "node:child_process";
2
+ import { randomBytes } from "node:crypto";
3
+ import { Socket } from "node:net";
4
+ import { dirname, extname, join } from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ import { killMarkedProcesses, PROCESS_MARKER } from "./kill-marked-processes.ts";
7
+ import { resolveExecutable } from "./resolve-executable.ts";
8
+
9
+ type ParentDeathSpawnOptions = {
10
+ stdio?: StdioOptions;
11
+ env?: NodeJS.ProcessEnv;
12
+ };
13
+
14
+ type GuardState = {
15
+ child?: ChildProcess | undefined;
16
+ };
17
+
18
+ const MODULE_PATH = fileURLToPath(import.meta.url);
19
+ const GUARD_PATH = join(dirname(MODULE_PATH), `process-guard${extname(MODULE_PATH)}`);
20
+ const RUN_ID = randomBytes(8).toString("hex");
21
+ const RUN_PREFIX = `${PROCESS_MARKER}=${RUN_ID}/`;
22
+ const guard: GuardState = {};
23
+
24
+ const releaseGuard = (child: ChildProcess): void => {
25
+ child.unref();
26
+ const { stdin } = child;
27
+
28
+ if (stdin instanceof Socket) {
29
+ stdin.unref();
30
+ }
31
+ };
32
+
33
+ const startGuard = (): void => {
34
+ if (guard.child) {
35
+ return;
36
+ }
37
+
38
+ const child = spawn(process.execPath, [GUARD_PATH, RUN_PREFIX], {
39
+ detached: true,
40
+ stdio: ["pipe", "ignore", "ignore"],
41
+ });
42
+
43
+ child.on("error", () => {
44
+ guard.child = undefined;
45
+ });
46
+
47
+ guard.child = child;
48
+ releaseGuard(child);
49
+ };
50
+
51
+ function spawnWithParentDeathSignal(
52
+ command: string,
53
+ args: string[],
54
+ options: ParentDeathSpawnOptions = {},
55
+ ): ChildProcess {
56
+ const executable = resolveExecutable(command);
57
+ const setpriv = resolveExecutable("setpriv");
58
+ const jobValue = `${RUN_ID}/${randomBytes(4).toString("hex")}`;
59
+ startGuard();
60
+
61
+ const child = spawn(setpriv, ["--pdeathsig", "SIGKILL", executable, ...args], {
62
+ detached: true,
63
+ stdio: options.stdio ?? "ignore",
64
+ env: { ...(options.env ?? process.env), [PROCESS_MARKER]: jobValue },
65
+ });
66
+
67
+ child.on("exit", () => {
68
+ killMarkedProcesses(`${PROCESS_MARKER}=${jobValue}`);
69
+ });
70
+
71
+ return child;
72
+ }
73
+
74
+ export { spawnWithParentDeathSignal };
package/src/set/drain.ts CHANGED
@@ -1,11 +1,3 @@
1
- /**
2
- * Iterates every item currently in `set`, then clears it. Use it to drain a set of pending work
3
- * accumulated since the last drain.
4
- *
5
- * @template T - The set item type.
6
- * @param set - The set to drain and clear.
7
- * @param visit - Called with each item before the set is cleared.
8
- */
9
1
  const drain = <T>(set: Set<T>, visit: (item: T) => void): void => {
10
2
  for (const item of set) {
11
3
  visit(item);
package/src/set/index.ts CHANGED
@@ -1 +1 @@
1
- export { drain } from "./drain.js";
1
+ export { drain } from "./drain.ts";
@@ -1,3 +1,3 @@
1
- export { sanitizeIdentifier } from "./sanitize-identifier.js";
2
- export { sourceStringLiteral } from "./source-string-literal.js";
3
- export { toCamelIdentifier } from "./to-camel-identifier.js";
1
+ export { sanitizeIdentifier } from "./sanitize-identifier.ts";
2
+ export { sourceStringLiteral } from "./source-string-literal.ts";
3
+ export { toCamelIdentifier } from "./to-camel-identifier.ts";
@@ -48,17 +48,6 @@ const RESERVED: Set<string> = new Set([
48
48
  "yield",
49
49
  ]);
50
50
 
51
- /**
52
- * Returns the name unchanged, or with a trailing underscore when it collides with a reserved word,
53
- * so it is safe to emit as a JavaScript identifier.
54
- *
55
- * @param name - The candidate identifier.
56
- * @returns The name, suffixed with `_` when it is a reserved word.
57
- *
58
- * @example
59
- * sanitizeIdentifier("iconName"); // "iconName"
60
- * sanitizeIdentifier("class"); // "class_"
61
- */
62
51
  function sanitizeIdentifier(name: string): string {
63
52
  return RESERVED.has(name) ? `${name}_` : name;
64
53
  }
@@ -10,16 +10,6 @@ const SOURCE_ESCAPES: Record<string, string> = {
10
10
 
11
11
  const UNSAFE_SOURCE_CHARS = new RegExp(`[${Object.keys(SOURCE_ESCAPES).join("")}]`, "g");
12
12
 
13
- /**
14
- * Encodes a string as a JavaScript string literal, additionally escaping characters that are unsafe
15
- * to embed in generated source (angle brackets and the line and paragraph separators).
16
- *
17
- * @param value - The string to encode.
18
- * @returns The quoted, source-safe string literal.
19
- *
20
- * @example
21
- * sourceStringLiteral("hello"); // '"hello"'
22
- */
23
13
  function sourceStringLiteral(value: string): string {
24
14
  return JSON.stringify(value).replaceAll(UNSAFE_SOURCE_CHARS, (char) => SOURCE_ESCAPES[char] ?? char);
25
15
  }
@@ -1,16 +1,6 @@
1
- import { camelCase } from "../string/camel-case.js";
2
- import { sanitizeIdentifier } from "./sanitize-identifier.js";
1
+ import { camelCase } from "../string/camel-case.ts";
2
+ import { sanitizeIdentifier } from "./sanitize-identifier.ts";
3
3
 
4
- /**
5
- * Converts a name to camel case and sanitizes it into a valid JavaScript identifier.
6
- *
7
- * @param name - The name to convert.
8
- * @returns The camel-cased, reserved-word-safe identifier.
9
- *
10
- * @example
11
- * toCamelIdentifier("icon_name"); // "iconName"
12
- * toCamelIdentifier("class"); // "class_"
13
- */
14
4
  function toCamelIdentifier(name: string): string {
15
5
  return sanitizeIdentifier(camelCase(name));
16
6
  }
@@ -1,20 +1,6 @@
1
- import { upperFirst } from "./upper-first.js";
2
- import { mapWordSegments } from "./word-segments.js";
1
+ import { upperFirst } from "./upper-first.ts";
2
+ import { mapWordSegments } from "./word-segments.ts";
3
3
 
4
- /**
5
- * Converts an underscore- or hyphen-delimited string to camel case.
6
- *
7
- * Unlike a general-purpose converter, this splits only on `_` and `-` and keeps the first segment
8
- * verbatim, so GObject acronyms are preserved (`camelCase("GLArea")` stays `"GLArea"`).
9
- *
10
- * @param str - The string to convert.
11
- * @returns The camel-cased string.
12
- *
13
- * @example
14
- * camelCase("icon_name"); // "iconName"
15
- * camelCase("start-widget"); // "startWidget"
16
- * camelCase("Box"); // "Box"
17
- */
18
4
  function camelCase(str: string): string {
19
5
  return mapWordSegments(str, (part, index) => (index === 0 ? part : upperFirst(part)));
20
6
  }
@@ -1,5 +1,5 @@
1
- export { camelCase } from "./camel-case.js";
2
- export { kebabCase } from "./kebab-case.js";
3
- export { lowerFirst } from "./lower-first.js";
4
- export { pascalCase } from "./pascal-case.js";
5
- export { upperFirst } from "./upper-first.js";
1
+ export { camelCase } from "./camel-case.ts";
2
+ export { kebabCase } from "./kebab-case.ts";
3
+ export { lowerFirst } from "./lower-first.ts";
4
+ export { pascalCase } from "./pascal-case.ts";
5
+ export { upperFirst } from "./upper-first.ts";
@@ -1,14 +1,3 @@
1
- /**
2
- * Converts a camelCase or PascalCase string to kebab case by lower-casing uppercase letters and
3
- * inserting hyphens before interior ones.
4
- *
5
- * @param str - The string to convert.
6
- * @returns The kebab-cased string.
7
- *
8
- * @example
9
- * kebabCase("iconName"); // "icon-name"
10
- * kebabCase("Title"); // "title"
11
- */
12
1
  function kebabCase(str: string): string {
13
2
  return str.replaceAll(/[A-Z]/g, (char, index: number) =>
14
3
  index === 0 ? char.toLowerCase() : `-${char.toLowerCase()}`,
@@ -1,13 +1,3 @@
1
- /**
2
- * Converts the first character of a string to lower case.
3
- *
4
- * @param str - The string to transform.
5
- * @returns The string with its first character lower-cased.
6
- *
7
- * @example
8
- * lowerFirst("Fred"); // "fred"
9
- * lowerFirst("FRED"); // "fRED"
10
- */
11
1
  function lowerFirst(str: string): string {
12
2
  return str.charAt(0).toLowerCase() + str.slice(1);
13
3
  }
@@ -1,19 +1,6 @@
1
- import { upperFirst } from "./upper-first.js";
2
- import { mapWordSegments } from "./word-segments.js";
1
+ import { upperFirst } from "./upper-first.ts";
2
+ import { mapWordSegments } from "./word-segments.ts";
3
3
 
4
- /**
5
- * Converts an underscore- or hyphen-delimited string to Pascal case.
6
- *
7
- * Splits only on `_` and `-` and capitalizes each segment's first character, so GObject acronyms
8
- * are preserved (`pascalCase("GLArea")` stays `"GLArea"`).
9
- *
10
- * @param str - The string to convert.
11
- * @returns The Pascal-cased string.
12
- *
13
- * @example
14
- * pascalCase("icon_name"); // "IconName"
15
- * pascalCase("scrolled-window"); // "ScrolledWindow"
16
- */
17
4
  function pascalCase(str: string): string {
18
5
  return mapWordSegments(str, upperFirst);
19
6
  }
@@ -1,13 +1,3 @@
1
- /**
2
- * Converts the first character of a string to upper case.
3
- *
4
- * @param str - The string to transform.
5
- * @returns The string with its first character upper-cased.
6
- *
7
- * @example
8
- * upperFirst("fred"); // "Fred"
9
- * upperFirst("Fred"); // "Fred"
10
- */
11
1
  function upperFirst(str: string): string {
12
2
  return str.charAt(0).toUpperCase() + str.slice(1);
13
3
  }
package/src/util/index.ts CHANGED
@@ -1 +1 @@
1
- export { packageVersion } from "./package-version.js";
1
+ export { packageVersion } from "./package-version.ts";
@@ -1,16 +1,7 @@
1
1
  import { createRequire } from "node:module";
2
2
 
3
- /**
4
- * Reads the `version` field from the `package.json` next to the calling module.
5
- *
6
- * @param importMetaUrl - The caller's `import.meta.url`, used to resolve the sibling `package.json`.
7
- * @returns The package's version string.
8
- *
9
- * @example
10
- * packageVersion(import.meta.url); // "1.2.3"
11
- */
12
- function packageVersion(importMetaUrl: string): string {
13
- return (createRequire(importMetaUrl)("../package.json") as { version: string }).version;
3
+ function packageVersion(importMetaUrl: string, specifier: string): string {
4
+ return (createRequire(importMetaUrl)(specifier) as { version: string }).version;
14
5
  }
15
6
 
16
7
  export { packageVersion };
@@ -1,3 +0,0 @@
1
- declare function callMethod(target: object, method: string, args: unknown[]): unknown;
2
- export { callMethod };
3
- //# sourceMappingURL=call-method.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"call-method.d.ts","sourceRoot":"","sources":["../../src/function/call-method.ts"],"names":[],"mappings":"AAeA,iBAAS,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,OAAO,CAI5E;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
@@ -1,19 +0,0 @@
1
- /**
2
- * Calls the named method on `target` with the given arguments, returning `undefined` when the
3
- * property is missing or not callable.
4
- *
5
- * @param target - The object to read the method from and bind as `this`.
6
- * @param method - The name of the method to look up.
7
- * @param args - The arguments passed to the method.
8
- * @returns The method's return value, or `undefined` when it is not a function.
9
- *
10
- * @example
11
- * callMethod(console, "log", ["hi"]); // logs "hi"; returns undefined
12
- */
13
- const isCallable = (value) => typeof value === "function";
14
- function callMethod(target, method, args) {
15
- const fn = Reflect.get(target, method);
16
- return isCallable(fn) ? Reflect.apply(fn, target, args) : undefined;
17
- }
18
- export { callMethod };
19
- //# sourceMappingURL=call-method.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"call-method.js","sourceRoot":"","sources":["../../src/function/call-method.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,GAAG,CAAC,KAAc,EAA0D,EAAE,CAC1F,OAAO,KAAK,KAAK,UAAU,CAAC;AAEhC,SAAS,UAAU,CAAC,MAAc,EAAE,MAAc,EAAE,IAAe;IAC/D,MAAM,EAAE,GAAY,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEhD,OAAO,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AACxE,CAAC;AAED,OAAO,EAAE,UAAU,EAAE,CAAC","sourcesContent":["/**\n * Calls the named method on `target` with the given arguments, returning `undefined` when the\n * property is missing or not callable.\n *\n * @param target - The object to read the method from and bind as `this`.\n * @param method - The name of the method to look up.\n * @param args - The arguments passed to the method.\n * @returns The method's return value, or `undefined` when it is not a function.\n *\n * @example\n * callMethod(console, \"log\", [\"hi\"]); // logs \"hi\"; returns undefined\n */\nconst isCallable = (value: unknown): value is (this: object, ...args: unknown[]) => unknown =>\n typeof value === \"function\";\n\nfunction callMethod(target: object, method: string, args: unknown[]): unknown {\n const fn: unknown = Reflect.get(target, method);\n\n return isCallable(fn) ? Reflect.apply(fn, target, args) : undefined;\n}\n\nexport { callMethod };\n"]}
@@ -1,2 +0,0 @@
1
- export { callMethod } from "./call-method.js";
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/function/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC"}
@@ -1,2 +0,0 @@
1
- export { callMethod } from "./call-method.js";
2
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/function/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC","sourcesContent":["export { callMethod } from \"./call-method.js\";\n"]}