@acegalaxy/node-utils
v0.1.0
Published
Small generic Node.js helpers: file-lock (mkdir-based mutex with stale-PID detection), process-group (kill-tree via pgid), string normalize. Zero deps.
Maintainers
Readme
@acegalaxy/node-utils
Small generic Node.js helpers. Zero runtime deps. TypeScript with .d.ts shipped.
Install
npm install @acegalaxy/node-utilsWhat's inside
| Subpath | Purpose |
|---|---|
| @acegalaxy/node-utils/file-lock | mkdir-based mutex with stale-PID detection (cron-safe) |
| @acegalaxy/node-utils/process-group | kill child + grandchildren via process group; spawn-detached helper |
| @acegalaxy/node-utils/normalize | string normalize helpers (lower + trim, plus normalizing key lookups) |
Quick start
file-lock
Cron-safe mutex backed by mkdir + PID liveness check. Auto-cleans stale locks if the owning PID is gone.
const { acquireLock, releaseLock } = require("@acegalaxy/node-utils/file-lock");
const lockPath = "/tmp/myjob.lock";
if (!acquireLock(lockPath)) {
console.log("another instance running — exit");
process.exit(0);
}
try {
await doWork();
} finally {
releaseLock(lockPath);
}process-group
child_process.spawn(..., { detached: true }) puts the child in its own process group. With shell:true, the immediate child is /bin/sh and the real CLI is a grandchild that would survive child.kill(). killTree(pid) signals the whole group.
const { spawn } = require("child_process");
const { killTree, SPAWN_OPTS_DETACHED } = require("@acegalaxy/node-utils/process-group");
const child = spawn("npm", ["run", "long-task"], { shell: true, ...SPAWN_OPTS_DETACHED });
setTimeout(() => killTree(child.pid, "SIGTERM"), 30_000);normalize
const { normalize } = require("@acegalaxy/node-utils/normalize");
normalize(" Hello World "); // "hello world"License
MIT © ACE Galaxy
