UNPKG

@mondaydotcomorg/node-execution-context

Version:

Persistent execution context allowing you to get/set the context anywhere implemented using async hooks. Can be used to create request level execution context, a stack trace that persists through async resources, or anything else you need to survive the e

30 lines (24 loc) 902 B
const asyncHooks = require("async_hooks"); const { verifyContextIsObject } = require("../utils/verify-is-type-object"); const { CONTEXT_ALREADY_DECLARED } = require("../errors"); const IS_NOT_PROD = process.env.NODE_ENV !== "production"; function createExecutionContext( context, traceOptions = { enabled: false, initialData: {} } ) { const asyncId = asyncHooks.executionAsyncId(); const { enabled, initialData } = traceOptions; let traceArray; if (enabled) { traceArray = [ `asyncId ${asyncId}: ${initialData}, eAID: ${asyncHooks.executionAsyncId()}` ]; } // const executionContextCheck = executionContextMap.get(asyncId); if (IS_NOT_PROD) { if (executionContextMap.has(asyncId)) throw CONTEXT_ALREADY_DECLARED; verifyContextIsObject(context); } executionContextMap.set(asyncId, { context, traceArray }); } module.exports = { createExecutionContext };