https://github.com/mawrkus/ah-context-store
🔗 A context storage for async resources, based on the Node.js Async Hooks API
https://github.com/mawrkus/ah-context-store
async-hooks async-resources nodejs
Last synced: 3 months ago
JSON representation
🔗 A context storage for async resources, based on the Node.js Async Hooks API
- Host: GitHub
- URL: https://github.com/mawrkus/ah-context-store
- Owner: mawrkus
- Created: 2018-11-16T02:30:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T15:19:21.000Z (over 3 years ago)
- Last Synced: 2025-03-08T21:40:51.866Z (over 1 year ago)
- Topics: async-hooks, async-resources, nodejs
- Language: JavaScript
- Homepage:
- Size: 1.27 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Async Context Store
A context storage for async resources, based on the Node.js [Async Hooks API](https://nodejs.org/api/async_hooks.html).
## 🔗 Installation (not yet published)
```shell
$ npm install ah-context-store // soon...
```
## 🔗 Usage
## Example
```javascript
const AsyncContextStore = require('async-context-store');
const asyncContextStore = new AsyncContextStore().enable();
const resolveAfter = ms => new Promise(resolve => setTimeout(resolve, ms));
function handleRequest(requestId) {
return resolveAfter(10, `handle(${requestId})`)
.then(() => {
asyncContextStore.logStore();
assert.strictEqual(requestId, asyncContextStore.get('request.id'));
});
}
(async () => {
const request1P = resolveAfter(10, 'request1')
.then(() => {
asyncContextStore.set('request.id', 42).logStore();
})
.then(() => {
assert.strictEqual(42, asyncContextStore.get('request.id'));
return handleRequest(42);
});
const request2P = resolveAfter(10, 'request2')
.then(() => {
asyncContextStore.set('request.id', 69).logStore();
})
.then(() => {
assert.strictEqual(69, asyncContextStore.get('request.id'));
return handleRequest(69);
});
return Promise.all([request1P, request2P])
.then(() => {
assert.strictEqual(undefined, asyncContextStore.get('request.id'));
asyncContextStore.logStore().disable();
});
})();
```
## API
```javascript
class AsyncContextStore {
/**
* @param {String[]} [debug=[]] ['methods', 'hooks]
*/
constructor({ debug } = { debug: [] }) {}
/**
* @return {Number} Number of contexts currently stored.
*/
get size() {}
/**
* @return {Object}
*/
get store() {}
/**
* @return {AsyncContextStore} this
*/
enable() {}
/**
* @return {AsyncContextStore} this
*/
disable() {}
/**
* @param {String} key
* @param {*} value
* @return {AsyncContextStore} this
*/
set(key, value) {}
/**
* @param {String} key
* @return {*} value
*/
get(key) {}
/**
* @param {...any} args
* @return {AsyncContextStore} this
*/
log(...args) {}
/**
* @param {Number} [asyncId=this._asyncHooks.executionAsyncId()]
* @return {AsyncContextStore} this
*/
logContext(asyncId = this._asyncHooks.executionAsyncId()) {}
/**
* @return {AsyncContextStore} this
*/
logStore() {}
```
## 🔗 Demos
- Promise-based demos.
- `async-await` demos.
- [Hapi v17](https://hapijs.com/api/17.7.0) demo, to illustrate HTTP request tracing across multiple services.
Clone the project...
```shell
$ git clone https://github.com/mawrkus/async-context-store.git
$ cd async-context-store
$ npm install
$ npm run demo:async-await
$ npm run demo:promises
$ npm run demo:server
$ ./demos/demo-server-stress.sh my-agent
```
## 🔗 Resources
- "Grokking Asynchronous Work in Node.js" by Thorsten Lorenz:
+ Video -> https://www.youtube.com/watch?v=8Xoht4J6Jjw
+ Slides -> http://thlorenz.com/talks/async-hooks/book/