Base
Environment config and common utilities for travetto applications.
Install: @travetto/base
npm install @travetto/base
yarn add @travetto/base
Base is the foundation of all Travetto applications. It is intended to be a minimal application set, as well as support for commonly shared functionality. It has support for the following key areas:
- Environment Support
- Runtime Flags
- Console Management
- Resource Access
- Standard Error Support
- Stream Utilities
- Object Utilities
- Common Utilities
- Time Utilities
- Process Execution
- Shutdown Management
Environment Support
The functionality we support for testing and retrieving environment information for known environment variables. They can be accessed directly on the Env object, and will return a scoped EnvProp, that is compatible with the property definition. E.g. only showing boolean related fields when the underlying flag supports true
or false
Code: Base Known Environment Flags
interface TravettoEnv {
NODE_ENV: 'development' | 'production';
DEBUG: boolean | string;
TRV_ENV: string;
TRV_ROLE: Role;
TRV_DYNAMIC: boolean;
TRV_RESOURCES: string[];
TRV_SHUTDOWN_WAIT: TimeSpan | number;
TRV_MODULE: string;
TRV_MANIFEST: string;
TRV_BUILD: 'none' | 'info' | 'debug' | 'error' | 'warn',
}
Environment Property
For a given EnvProp, we support the ability to access different properties as a means to better facilitate environment variable usage.
Code: EnvProp Shape
export class EnvProp<T> {
constructor(public readonly key: string) { }
clear(): void;
export(val: T | undefined): Record<string, string>;
get val(): string | undefined;
get list(): string[] | undefined;
add(...items: string[]): void;
get int(): number | undefined;
get bool(): boolean | undefined;
get time(): number | undefined;
get isTrue(): boolean;
get isFalse(): boolean;
get isSet(): boolean;
}
Runtime Flags
Env also provides some convenience methods for common flags used at runtime within the framework. These are wrappers around direct access to process.env
values with a little bit of logic sprinkled in.
Code: Provided Flags
export const Env = delegate({
get name(): string | undefined {
return process.env.TRV_ENV || (!prod() ? RuntimeContext.workspace.defaultEnv : undefined);
},
get production(): boolean {
return prod();
},
get dynamic(): boolean {
return IS_TRUE.test(process.env.TRV_DYNAMIC!);
},
get debug(): false | string {
const val = process.env.DEBUG ?? '';
return (!val && prod()) || IS_FALSE.test(val) ? false : val;
}
});
Resource Access
The primary access patterns for resources, is to directly request a file, and to resolve that file either via file-system look up or leveraging the Manifest's data for what resources were found at manifesting time.
The FileLoader allows for accessing information about the resources, and subsequently reading the file as text/binary or to access the resource as a Readable
stream. If a file is not found, it will throw an AppError with a category of 'notfound'.
The ResourceLoader extends FileLoader and utilizes the Env's TRV_RESOURCES
information on where to attempt to find a requested resource.
Standard Error Support
While the framework is 100 % compatible with standard Error
instances, there are cases in which additional functionality is desired. Within the framework we use AppError (or its derivatives) to represent framework errors. This class is available for use in your own projects. Some of the additional benefits of using this class is enhanced error reporting, as well as better integration with other modules (e.g. the RESTful API module and HTTP status codes).
The AppError takes in a message, and an optional payload and / or error classification. The currently supported error classifications are:
general
- General purpose errorssystem
- Synonym for general
data
- Data format, content, etc are incorrect. Generally correlated to bad input.permission
- Operation failed due to lack of permissionsauth
- Operation failed due to lack of authenticationmissing
- Resource was not found when requestedtimeout
- Operation did not finish in a timely mannerunavailable
- Resource was unresponsive
Console Management
This module provides logging functionality, built upon console operations.
The supported operations are:
console.error
which logs at the ERROR
levelconsole.warn
which logs at the WARN
levelconsole.info
which logs at the INFO
levelconsole.debug
which logs at the DEBUG
levelconsole.log
which logs at the INFO
level
Note: All other console methods are excluded, specifically trace
, inspect
, dir
, time
/timeEnd
How Logging is Instrumented
All of the logging instrumentation occurs at transpilation time. All console.*
methods are replaced with a call to a globally defined variable that delegates to the ConsoleManager. This module, hooks into the ConsoleManager and receives all logging events from all files compiled by the Travetto.
A sample of the instrumentation would be:
Code: Sample logging at various levels
export function work() {
console.debug('Start Work');
try {
1 / 0;
} catch (err) {
console.error('Divide by zero', { error: err });
}
console.debug('End Work');
}
Code: Sample After Transpilation
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.work = void 0;
const tslib_1 = require("tslib");
const ᚕ_c = tslib_1.__importStar(require("@travetto/base/src/console.js"));
var ᚕf = "@travetto/base/doc/transpile.js";
function work() {
ᚕ_c.log({ level: "debug", source: ᚕf, line: 2, scope: "work", args: ['Start Work'] });
try {
1 / 0;
}
catch (err) {
ᚕ_c.log({ level: "error", source: ᚕf, line: 7, scope: "work", args: ['Divide by zero', { error: err }] });
}
ᚕ_c.log({ level: "debug", source: ᚕf, line: 9, scope: "work", args: ['End Work'] });
}
exports.work = work;
Filtering Debug
The debug
messages can be filtered using the patterns from the debug. You can specify wild cards to only DEBUG
specific modules, folders or files. You can specify multiple, and you can also add negations to exclude specific packages.
Terminal: Sample environment flags
$ DEBUG=-@travetto/model npx trv run app
$ DEBUG=-@travetto/registry npx trv run app
$ DEBUG=@travetto/rest npx trv run app
$ DEBUG=@travetto/*,-@travetto/model npx trv run app
Additionally, the logging framework will merge debug into the output stream, and supports the standard usage
Terminal: Sample environment flags for standard usage
$ DEBUG=express:*,@travetto/rest npx trv run rest
Stream Utilities
The StreamUtil class provides basic stream utilities for use within the framework:
toBuffer(src: Readable | Buffer | string): Promise<Buffer>
for converting a stream/buffer/filepath to a Buffer.toReadable(src: Readable | Buffer | string):Promise<Readable>
for converting a stream/buffer/filepath to a ReadablewriteToFile(src: Readable, out: string):Promise<void>
will stream a readable into a file path, and wait for completion.
Object Utilities
Simple functions for providing a minimal facsimile to lodash, but without all the weight. Currently ObjectUtil includes:
isPrimitive(el)
determines if el
is a string
, boolean
, number
or RegExp
isPlainObject(obj)
determines if the obj is a simple objectisFunction(o)
determines if o
is a simple Function
isClass(o)
determines if o
is a class constructorisSimple(a)
determines if a
is a simple valueisPromise(a)
determines if a
is a promise
Common Utilities
Common utilities used throughout the framework. Currently Util includes:
uuid(len: number)
generates a simple uuid for use within the application.allowDenyMatcher(rules[])
builds a matching function that leverages the rules as an allow/deny list, where order of the rules matters. Negative rules are prefixed by '!'.naiveHash(text: string)
produces a fast, and simplistic hash. No guarantees are made, but performs more than adequately for framework purposes.shortHash(text: string)
produces a sha512 hash and returns the first 32 characters.fullHash(text: string, size?: number)
produces a full sha512 hash.resolvablePromise()
produces a Promise
instance with the resolve
and reject
methods attached to the instance. This is extremely useful for integrating promises into async iterations, or any other situation in which the promise creation and the execution flow don't always match up.
Code: Sample makeTemplate Usage
const tpl = makeTemplate((name: 'age'|'name', val) => `**${name}: ${val}**`);
tpl`{{age:20}} {{name: 'bob'}}</>;
// produces
'**age: 20** **name: bob**'
Time Utilities
TimeUtil contains general helper methods, created to assist with time-based inputs via environment variables, command line interfaces, and other string-heavy based input.
Code: Time Utilities
export class TimeUtil {
static isTimeSpan(val: string): val is TimeSpan;
static timeToMs(amount: number | TimeSpan, unit?: TimeUnit): number;
static resolveInput(value: number | string | undefined): number | undefined;
static timeFromNow(amount: number | TimeSpan, unit: TimeUnit = 'ms'): Date;
static prettyDeltaSinceTime(time: number, unit?: TimeUnit): string;
static prettyDelta(delta: number, unit?: TimeUnit): string;
}
Process Execution
ExecUtil exposes getResult
as a means to wrap child_process's process object. This wrapper allows for a promise-based resolution of the subprocess with the ability to capture the stderr/stdout.
A simple example would be:
Code: Running a directory listing via ls
import { spawn } from 'node:child_process';
import { ExecUtil } from '@travetto/base';
export async function executeListing() {
const final = await ExecUtil.getResult(spawn('ls'));
console.log('Listing', { lines: final.stdout.split('\n') });
}
Shutdown Management
Another key lifecycle is the process of shutting down. The framework provides centralized functionality for running operations on graceful shutdown. Primarily used by the framework for cleanup operations, this provides a clean interface for registering shutdown handlers. The code intercepts SIGTERM
and SIGUSR2
, with a default threshold of 2 seconds. These events will start the shutdown process, but also clear out the pending queue. If a kill signal is sent again, it will complete immediately.
As a registered shutdown handler, you can do.
Code: Registering a shutdown handler
import { ShutdownManager } from '@travetto/base';
export function registerShutdownHandler() {
ShutdownManager.onGracefulShutdown(async () => {
});
}