Universal production building blocks for Tupik: pluggable key/value storage with TTL, a structured
JSON logger and duck-typed OpenTelemetry tracing. Zero dependencies and no node:* imports, so it runs on any runtime.
npm install @tupik/infraimport { createLogger, createMemoryStorage, createRuntimeTracer, namespaced } from '@tupik/infra';
const storage = createMemoryStorage();
const sessions = namespaced(storage, 'session'); // keys stored as 'session:<key>'
await sessions.set('abc', { user: 1 }, { ttlMs: 60_000 });
console.log(await sessions.get('abc')); // { user: 1 }
const log = createLogger({ level: 'info', base: { service: 'api' }, redact: ['password'] });
log.child({ requestId: 'r1' }).info('login', { password: 'secret' });
// {"level":"info","time":...,"msg":"login","service":"api","requestId":"r1","password":"[redacted]"}
// Without `tracer` every span is a no-op; pass `trace.getTracer('api')` from @opentelemetry/api to record spans.
const tracer = createRuntimeTracer();
const value = await tracer.span('compute', (span) => {
span.setAttribute('items', 3);
return 42;
});
console.log(tracer.enabled, value); // false 42| Export | Purpose |
|---|---|
createMemoryStorage({ now? }) |
In-memory Storage (get/set with ttlMs/delete/clear/keys) |
namespaced(storage, prefix) |
View over any Storage that stores keys as prefix:key |
createLogger({ level?, base?, sink?, redact?, maskIp?, errorDetail?, now? }) |
JSON-lines logger with child(bindings); redacts DEFAULT_REDACT_KEYS at any depth and masks IPs by default |
maskIp(value), DEFAULT_REDACT_KEYS, IP_LOG_KEYS |
IP masking (IPv4 /24, IPv6 /48), the default redaction list and the IP keys |
createRedactor({ redact?, maskIp?, errorDetail? }) |
The logger's deep redaction as a function (single source, also inlined by @tupik/middleware) |
createRuntimeTracer({ tracer?, attributes?, errorDetail? }) |
span(name, fn, { attributes, kind, links, context }?) over any OTel-compatible tracer; errorDetail: 'name' keeps only error.type |
parseTraceparent(header), formatTraceparent(spanContext) |
W3C Trace Context traceparent in and out |
createAttributeRedactor({ redact?, maskIp?, sensitive? }) |
Flat OTel attributes after createRedactor, with sensitive field paths (e.g. a route's pii/health/secret) as '[redacted]' |
MIT