Files
smartcontext/readme.md

134 lines
4.3 KiB
Markdown
Raw Normal View History

# @push.rocks/smartcontext
2026-02-15 20:09:47 +00:00
A zero-dependency module for hierarchical async context management in Node.js, built on `AsyncLocalStorage`.
2024-04-14 13:39:41 +02:00
## Install
```bash
npm install @push.rocks/smartcontext
2026-02-15 20:09:47 +00:00
# or
pnpm install @push.rocks/smartcontext
2024-04-14 13:39:41 +02:00
```
2018-03-08 23:49:26 +01:00
## Usage
2020-07-20 11:57:20 +00:00
2026-02-15 20:09:47 +00:00
`@push.rocks/smartcontext` provides scoped, hierarchical key-value stores that follow async execution flow. Child scopes inherit parent data, can add or delete keys without affecting the parent, and automatically clean up when the scope exits.
This is useful for log enrichment, request-scoped metadata, transaction tracking, and any scenario where contextual data needs to flow through deeply nested async calls.
### Basic Setup
```typescript
import { AsyncContext } from '@push.rocks/smartcontext';
2018-03-03 14:11:27 +01:00
2026-02-15 20:09:47 +00:00
const ctx = new AsyncContext();
2026-02-15 20:09:47 +00:00
// Add data to the root store
ctx.store.add('userId', 'u_123');
console.log(ctx.store.get('userId')); // 'u_123'
2018-03-03 14:11:27 +01:00
```
2026-02-15 20:09:47 +00:00
### Scoped Execution with `runScoped`
2024-04-14 13:39:41 +02:00
2026-02-15 20:09:47 +00:00
`runScoped` creates an isolated child store for the duration of the callback. Inside the callback, `ctx.store` transparently points to the child store. The child inherits all parent data but any additions or deletions are local to the child.
2018-03-03 14:11:27 +01:00
```typescript
2026-02-15 20:09:47 +00:00
ctx.store.add('requestId', 'req_abc');
2026-02-15 20:09:47 +00:00
await ctx.runScoped(async () => {
// Child store sees parent data
console.log(ctx.store.get('requestId')); // 'req_abc'
2026-02-15 20:09:47 +00:00
// Add child-only data
ctx.store.add('spanId', 'span_001');
console.log(ctx.store.get('spanId')); // 'span_001'
});
2018-03-03 14:11:27 +01:00
2026-02-15 20:09:47 +00:00
// After scope exits, child data is gone
console.log(ctx.store.get('spanId')); // undefined
// Parent data is untouched
console.log(ctx.store.get('requestId')); // 'req_abc'
```
2026-02-15 20:09:47 +00:00
### Deleting Keys in a Child Scope
2018-03-03 14:11:27 +01:00
2026-02-15 20:09:47 +00:00
Deleting a parent key inside a child scope only shadows it within that scope. The parent retains the original value.
2024-04-14 13:39:41 +02:00
```typescript
2026-02-15 20:09:47 +00:00
ctx.store.add('token', 'secret_value');
2026-02-15 20:09:47 +00:00
await ctx.runScoped(async () => {
ctx.store.delete('token');
console.log(ctx.store.get('token')); // undefined (shadowed)
2018-03-03 14:11:27 +01:00
});
2026-02-15 20:09:47 +00:00
console.log(ctx.store.get('token')); // 'secret_value' (parent unaffected)
```
2026-02-15 20:09:47 +00:00
### Parallel Scopes
2026-02-15 20:09:47 +00:00
Multiple concurrent `runScoped` calls each get their own isolated child store, preventing data collisions across async tasks.
```typescript
2026-02-15 20:09:47 +00:00
await Promise.all([
ctx.runScoped(async () => {
ctx.store.add('worker', 'A');
// Only this scope sees worker=A
}),
ctx.runScoped(async () => {
ctx.store.add('worker', 'B');
// Only this scope sees worker=B
}),
]);
console.log(ctx.store.get('worker')); // undefined
```
2026-02-15 20:09:47 +00:00
### Retrieving All Data
2026-02-15 20:09:47 +00:00
`store.getAll()` returns a merged view of the store hierarchy, with child values overriding parent values and deleted keys excluded.
```typescript
2026-02-15 20:09:47 +00:00
ctx.store.add('env', 'production');
ctx.store.add('region', 'eu-west-1');
2026-02-15 20:09:47 +00:00
await ctx.runScoped(async () => {
ctx.store.add('traceId', 'tr_xyz');
console.log(ctx.store.getAll());
// { env: 'production', region: 'eu-west-1', traceId: 'tr_xyz' }
});
2024-04-14 13:39:41 +02:00
```
2026-02-15 20:09:47 +00:00
### API Reference
#### `AsyncContext`
2026-02-15 20:09:47 +00:00
| Method / Property | Description |
|---|---|
| `store` | The current `AsyncStore` (root or scoped child) |
| `runScoped(fn)` | Execute `fn` with an isolated child store |
2024-04-14 13:39:41 +02:00
2026-02-15 20:09:47 +00:00
#### `AsyncStore`
2024-04-14 13:39:41 +02:00
2026-02-15 20:09:47 +00:00
| Method | Description |
|---|---|
| `add(key, value)` | Set a key-value pair |
| `get(key)` | Get a value (checks local store, then parent chain) |
| `delete(key)` | Remove a key (shadows parent key if inherited) |
| `getAll()` | Get all key-value pairs merged from the full parent chain |
2024-04-14 13:39:41 +02:00
## License and Legal Information
This repository is under the [MIT License](./license). Please note that the MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as necessary for reasonable use in describing the origin of the work.
2024-04-14 13:39:41 +02:00
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Usage must be approved in writing by Task Venture Capital GmbH.
2024-04-14 13:39:41 +02:00
### Company Information
2020-07-20 11:57:20 +00:00
2026-02-15 20:09:47 +00:00
Task Venture Capital GmbH
Registered at District Court Bremen HRB 35230 HB, Germany
2020-07-20 11:57:20 +00:00
2026-02-15 20:09:47 +00:00
For any legal inquiries, please contact us at hello@task.vc. By using this repository, you acknowledge that you have read this section and agree to comply with its terms.