A module to enrich logs with context, featuring async log contexts and scope management.
Special thanks to Ilias Bhallil for his awesome simple-async-context library.
## Install
Make sure you have Node.js and npm installed, then run:
```bash
npm install @push.rocks/smartcontext
```
This will install the library and its dependencies into your local `node_modules` folder.
## Usage
The `@push.rocks/smartcontext` module provides an efficient way to enrich your code (often for logging) with contextual information. It uses asynchronous context management to support hierarchical scopesâparticularly helpful in complex or nested asynchronous operations in Node.js.
### Basic Setup
```typescript
import { AsyncContext } from '@push.rocks/smartcontext';
const asyncContext = new AsyncContext();
// The parent store is always accessible through `asyncContext.store`
When you call `asyncContext.runScoped(async () => { ... })`, the library automatically creates a **child**`AsyncStore`. Inside that scoped function, `asyncContext.store` refers to the child store. Any data you add or delete there is isolated from the parent store. However, you can still read parent data if it hasnât been overridden.
```typescript
await asyncContext.runScoped(async () => {
// Inside this callback, `asyncContext.store` is a *child* store
Because each call to `runScoped` returns control to the parent store afterward, any keys added in a child scope disappear once the scope completes (unless you explicitly move them to the parent). This mechanism keeps data from leaking between scopes.
If the child deletes a key that exists in the parent, it will only remove it from the childâs view of the store. Once the scope completes, the parent store is unaffected.
console.log(asyncContext.store.get('deletableKey')); // undefined in child
});
console.log(asyncContext.store.get('deletableKey')); // 'originalValue' remains in parent
```
### Parallel or Sequential Scopes
You can call `runScoped` multiple times, whether sequentially or in parallel (with `Promise.all`). Each invocation creates its own isolated child store, preventing data collisions across asynchronous tasks.
The following is a complete test script (using [tapbundle](https://www.npmjs.com/package/@push.rocks/tapbundle)) demonstrating how child stores inherit data from the parent but remain isolated. After each scoped block, new child keys vanish, and any parent keys deleted inside the child remain intact in the parent.
```typescript
import { tap, expect } from '@push.rocks/tapbundle';
import { AsyncContext } from '../ts/logcontext.classes.asynccontext.js';
const asyncContext = new AsyncContext();
tap.test('should run a scoped function and add data to a child store', async () => {
With this updated `runScoped` design, thereâs no need to explicitly instantiate or manage child stores. The context automatically switches from the parent store to the child store while within the callback, then reverts back to the parent store afterwards. This structure makes it easy to:
- Keep each async operationâs state isolated
- Preserve read-access to parent context data
- Avoid overwriting or polluting other operationsâ data
This pattern works particularly well for logging or any scenario where you need to pass metadata through deeply nested async calls without manually juggling that data everywhere in your code.
## 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.
### 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.
### Company Information
Task Venture Capital GmbH
Registered at District Court Bremen HRB 35230 HB, Germany
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.