utils
Common utility library for Node.js —
Throttlerfor concurrent promise queues,Durationfor human-readable time parsing,FileUtils/JSONUtils/YAMLUtilsfor filesystem and config helpers,Debounce,Waiter, and more.
When to use it
- You need to limit concurrency when processing a large batch of async operations without complex queue setup.
- You need to parse human-readable durations (
"5m","2h30m") into milliseconds in configuration or CLI flags. - You need reliable file I/O with support for JSON, JSONC, and YAML in one consistent API.
Install
pnpm add @webda/utils
Configuration
@webda/utils is a pure library — no webda.config.json entry required.
Usage
import { Throttler, Duration } from "@webda/utils";
import { FileUtils, JSONUtils } from "@webda/utils";
// Throttler — run at most N promises concurrently
const throttler = new Throttler(5 /* concurrency */);
const ids = ["a", "b", "c", "d", "e", "f", "g", "h"];
await Promise.all(ids.map(id => throttler.queue(async () => processItem(id), id)));
await throttler.waitForCompletion();
// At most 5 processItem() calls were running at any point in time
// Duration — parse human-readable time strings
const ms = Duration.parse("2m30s"); // → 150000
const ms2 = Duration.parse("1h"); // → 3600000
const label = Duration.format(90000); // → "1m30s"
// FileUtils — load JSON, JSONC, or YAML transparently
const config = FileUtils.load("./webda.config.jsonc"); // parses comments
const data = JSONUtils.load("./data.json");
// JSONUtils.stringify — pretty-print with sorted keys
const pretty = JSONUtils.stringify({ b: 2, a: 1 }, undefined, 2);
// { "a": 1, "b": 2 } — keys sorted
// Waiter — poll until a condition is met with backoff
import { Waiter } from "@webda/utils";
await new Waiter(async () => {
const status = await checkDeploymentStatus();
return status === "COMPLETE";
}, 200 /* pollMs */, 30000 /* timeoutMs */).wait();
TypeScript utility types
@webda/utils also holds the shared types and helpers that used to live in @webda/tsc-esm:
import type { Attributes, DeepPartial, FilterAttributes, Merge } from "@webda/utils";
import { NotEnumerable, isMainModule } from "@webda/utils";
class Session {
// Kept out of enumeration, so it is skipped by JSON serialisation and Object.keys
@NotEnumerable
dirty: boolean;
}
type SessionAttributes = Attributes<Session>; // every non-method key
- Types —
Attributes,Methods,FilterAttributes,FilterOutAttributes,PickByType,OmitByType,OmitByTypeRecursive,DeepPartial,Merge,SetOptional,ReadonlyKeys,IsUnion,ArrayElementand theConstructorfamily. - Helpers —
NotEnumerable,StaticInterface,assertUnreachable, and the Node-onlyisMainModuleandgetFileName.
The decorator factories createClassDecorator, createPropertyDecorator and
createMethodDecorator come from @webda/decorators.
Reference
- API reference: see the auto-generated typedoc at
docs/pages/Modules/utils/. - Source:
packages/utils - Related:
@webda/corewhich re-exports many of these utilities;@webda/workoutfor logging utilities used alongside these helpers.
Classes
- BufferWritableStream
- CancelableLoopPromise
- CancelablePromise
- ContentCredentialNotFoundError
- ContentExistsError
- ContentNotFoundError
- ContentStorageNotFoundError
- ContentStoragesRegistry
- DirtyState
- Duration
- FileContentStorage
- FileSize
- GunzipConditional
- MemoryContentStorage
- NDJSonReader
- NDJSONStream
- RegExpStringValidator
- RegExpValidator
- Throttler
- WaitDelayerFactories
Interfaces
- ContentGetOptions
- ContentInfo
- ContentPutOptions
- ContentStorage
- DebouncedFunction
- DebounceOptions
- StateOptions
- StateStatus
- StorageFinder
- StorageRegistration
- WaitDelayerDefinition
Type Aliases
- AbstractConstructor
- Annotation
- AnyConstructor
- ArrayElement
- Attributes
- ClassAnnotation
- Constructor
- ContentBody
- CustomConstructor
- DeepPartial
- FilterAttributes
- FilterOutAttributes
- FunctionArgs
- FunctionReturn
- IfEquals
- IsUnion
- Merge
- Methods
- OmitByType
- OmitByTypeRecursive
- OmitFirstArg
- OmitFirtArg
- OmitPrefixed
- OmitTargetArgs
- PartialModel
- PickByType
- Pojo
- Prototype
- ReadonlyKeys
- SetOptional
- TransformCaseType
- WaitDelayer
- WaitDelayerFactory
Variables
- ContentStorages
- defaultFileContentStorage
- FileUtils
- globMatch
- JSONUtils
- MAX_GLOB_PATTERN_LENGTH
- MAX_GLOB_VALUE_LENGTH
- NotEnumerable
- OPERATION_PATTERN
- WEBDA_DIRTY
- YAMLUtils
Functions
- assertUnreachable
- ContentStorageContract
- createByteStream
- debounce
- deepFreeze
- DirtyMixIn
- getCommonJS
- getContent
- getFileName
- getUuid
- iamGlobMatch
- isMainModule
- listConfiguredServiceTypes
- listSharedImports
- lookupMimeType
- nextTick
- parseContentUrl
- parseImportDescriptor
- putContent
- runWithCurrentDirectory
- sanitizeFilename
- sleep
- State
- StaticInterface
- streamToBuffer
- toBuffer
- toImportSpecifier
- toReadable
- track
- TransformCase
- WaitExponentialDelay
- WaitFor
- WaitLinearDelay