node-runtime-utils
v1.0.0
Published
Core runtime utilities for Node.js: buffer helpers, LRU cache, event bus, async primitives, and functional utilities with zero dependencies
Maintainers
Readme
node-runtime-utils
Core utility primitives for Node.js with zero dependencies.
Installation
npm install node-runtime-utilsFeatures
- Buffer utilities — hex conversion, concat, equality checks, splitting
- Type checks — isBuffer, isString, isNumber, isObject, isFunction, isNil, isArray
- Event utilities — promise-based
once,waitFor - Stream utilities —
streamToBuffer,bufferToStream,pipeline - Async primitives —
sleep,retry,timeout,deferred - Object utilities —
deepClone,deepMerge,pick,omit,flattenObject - String utilities —
camelToSnake,snakeToCamel,truncate,escapeHtml
Usage
const {
bufferToHex, sleep, retry, deepClone, pick, once, streamToBuffer
} = require('node-runtime-utils');
// Buffer
const hex = bufferToHex(Buffer.from('hello'));
// Async
await sleep(500);
const result = await retry(() => fetch(url), 3, 300);
const data = await timeout(fetchSomething(), 5000);
// Objects
const clone = deepClone({ a: { b: 1 } });
const subset = pick({ a: 1, b: 2, c: 3 }, ['a', 'c']);
// Streams
const buf = await streamToBuffer(fs.createReadStream('file.txt'));API
Buffer
| Function | Description |
|---|---|
| bufferToHex(buf) | Convert buffer to hex string |
| hexToBuffer(hex) | Convert hex string to buffer |
| bufferConcat(bufs) | Concatenate array of buffers |
| bufferEqual(a, b) | Constant-time buffer equality |
| bufferSplit(buf, delim) | Split buffer by delimiter |
Async
| Function | Description |
|---|---|
| sleep(ms) | Promise that resolves after ms |
| retry(fn, attempts, delay) | Retry with exponential backoff |
| timeout(promise, ms) | Reject if promise exceeds ms |
| deferred() | Returns { promise, resolve, reject } |
License
MIT
