@homeofthings/node-utils
v1.0.2
Published
HomeOfThings - Node Utils: various utilities and common types
Downloads
190
Readme
@homeofthings/node-utils
AsyncContext<T>
: little wrapper aroundAsyncLocalStorage
providing default valueasyncContext = new AsyncContext(defaultContext); ... asyncContext.set(newContext) ... currentContext = asyncContext.get();
ConfigService
: singleton service based on node-config
configService = new ConfigService({ configDirectory: 'path/to/config' });
then use one of the methods provided by the ConfigService
:
export declare class ConfigService {
readonly configDirectory: string;
readonly environment: string;
constructor(_opts: ConfigOptions);
getConfig(key: string): object | undefined;
reloadConfig(): void;
getString(key: string, defaultValue: string): string;
getNumber(key: string, defaultValue: number): number;
getBoolean(key: string, defaultValue: boolean): boolean;
getObject(key: string, defaultValue: object): object;
// resolve path relative to config-directory
getPath(key: string, defaultValue: string): string;
getOptionalString(key: string): string | undefined;
getOptionalNumber(key: string): number | undefined;
getOptionalBoolean(key: string): boolean | undefined;
getOptionalObject(key: string): object | undefined;
// resolve path relative to config-directory
getOptionalPath(key: string): string | undefined;
}
reload on SIGHUP:
process.on('SIGHUP', () => ConfigService.getInstance().reloadConfig());
LruCache<T>
: LRU cachecache = new LruCache<UserSession, number>(SESSION_CACHE_SIZE); cache.set(id, userSession); // add this to the cache and mark it as least recently used ... cache.get(anotherId); // if it is available in the cache it will be marked as least recently used
sequentialize
: runPromises
in sequenceawait sequentialize(item.map(() => doWork(item)));
wait
: wait until a condition is true or timed outawait wait(condition); // polls until condition is true await wait(condition, 1000); // polls until condition is true or timed out after 1000ms
WritableStrings
: aWritable
for writing to a string arrayquoteArgs
andquoteArg
: quote arguments for better readability (e.g. for logging)