UNPKG

node-request-context

Version:

Node request context let you save any context per request and easily retrieve it

27 lines (20 loc) 420 B
const asyncHooks = require('async_hooks'); class Namespace { constructor() { this.context = {}; } run(fn) { const eid = asyncHooks.executionAsyncId(); this.context[eid] = {}; fn(); } set(key, val) { const eid = asyncHooks.executionAsyncId(); this.context[eid][key] = val; } get(key) { const eid = asyncHooks.executionAsyncId(); return this.context[eid][key] } } module.exports = Namespace;