npx skills add jasnell/node-skills --skill node-utilitiesSKILL.md
Utilities since v24
| Feature | Versions | Stability | Flag |
|---|---|---|---|
util.debounce, throttle, markPromiseAsHandled |
v26.10.0, v24.22.0 | 2 | |
util.isPartialDeepStrictEqual, Buffer.stringLength, isLatin1 |
v26.11.0 | 2 | |
util.convertProcessSignalToExitCode; streamConsumers.bytes |
v25.4.0; v25.6.0; both v24.14.0 | 2 | |
styleText('#rrggbb'), glob({ followSymlinks }) |
v26.1.0, v24.16.0 | 2 | |
fs.mkdtempDisposable[Sync] |
v24.4.0 | 2 | |
fs.mkstemp[Sync] |
v27.0.0 | 1 | |
fs.Utf8Stream |
v24.6.0 | 1 | |
glob({ maxDepth }); fs.openAsBlobSync |
v26.9.0; v26.10.0 | 2 | |
async stat({ throwIfNoEntry }) |
v25.7.0 (also v24.19.0) | 2 | |
stat/lstat signal; allocUnsafe(n, alignment) |
v26.8.0 | 2 | |
Stats *Instant (Temporal) |
v26.2.0 | 2 | |
blob.textStream(), ReadableStreamTee |
v26.5.0, v24.19.0 | 2, 1 | |
URLPattern global |
v24.0.0 | 1 | |
navigator.locks, worker_threads.locks |
v24.5.0 | 1 | |
ErrorEvent; QuotaExceededError |
v25.0.0; v26.0.0 | 2 | |
Global Web Worker |
v26.9.0, default-on in main | 1 | --experimental-web-worker |
process.addUncaughtExceptionCaptureCallback |
v25.9.0 | 1 | |
process.ref/unref Stable |
v26.11.0 (added v23.6.0, v22.14.0) | 2 | |
tty setRawMode('raw'|'io'), rawMode |
v26.8.0, v24.21.0 | 2 | |
stream.compose Stable |
v26.2.0, v24.19.0 | 2 | |
assert.Assert; skipPrototype |
v24.6.0, v22.19.0; v24.9.0 | 2 |
Experimental API. Before relying on these details, run the
node-experimental-checkskill's probe (ornode --versionplus a feature-detect one-liner) and check the docs for the installed version:https://nodejs.org/docs/<version>/api/<module>.html. If the installed version disagrees with this skill, the installed version wins.
Patterns
import { debounce, throttle } from 'node:util';
const save = debounce(async (doc) => write(doc), 300); // all callers get the last result
save.cancel(); // pending reject with AbortError
const get = throttle((id) => load(id), 2, 1000); // 2 starts/s; extras queue
await using dir = await fsp.mkdtempDisposable(join(tmpdir(), 'job-')); // rm -r at scope exit
const { path, handle } = await fsp.mkstemp(join(dir.path, 'f-')); // 27+, mode 0600
await using h = handle; // the result is not disposable
new URLPattern({ pathname: '/users/:id(\\d+)' }).exec(url)?.pathname.groups.id;
await navigator.locks.request('cache', () => refresh()); // resolves with refresh()'s value
Pitfalls
- Older versions ignore new args silently:
allocUnsafe(n, 3),glob({ maxDepth });setRawMode('io')acts as raw. mkdtempDisposable(Buffer)throws on main after creating the directory.usingonfs.Utf8Streamcallsdestroy()and drops buffered data. Callend().- Locks are per process. A queued request does not keep a worker alive.
new Worker('./w.js')resolves againstcwd; pass aURL.
Don't
lodash.debounce/throttle,p-throttle→util.debounce/throttle.tmp/tmp-promise→mkdtempDisposable/mkstemp;sonic-boom→Utf8Stream.urlpattern-polyfill→URLPattern; in-processasync-mutex→navigator.locks(notproper-lockfile: cross-process).chalk.hex()→styleText('#hex');128 + signals[s]→convertProcessSignalToExitCode.
Reference
- rules/util.md: debounce/throttle options, other util.
- rules/fs.md: temp files, Utf8Stream, glob, stat.
- rules/buffer-and-streams.md: Buffer, web streams.
- rules/globals-and-workers.md: URLPattern, locks, Web Worker.
- rules/process-tty-assert.md: process, tty,
Assert.