A context storage for async resources, based on the Node.js Async Hooks API.
$ npm install ah-context-store // soon...
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();
});
})();
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() {}
- Promise-based demos.
async-await
demos.- Hapi v17 demo, to illustrate HTTP request tracing across multiple services.
Clone the project...
$ 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
- "Grokking Asynchronous Work in Node.js" by Thorsten Lorenz: