|
| 1 | +const asyncHooks = require('async_hooks'); |
| 2 | +const { isProduction, monitorMap, ExecutionContextResource } = require('../lib'); |
| 3 | +const { create: createHooks, onChildProcessDestroy } = require('../hooks'); |
| 4 | +const { |
| 5 | + DEFAULT_CONFIG, |
| 6 | + ROOT_DOMAIN, |
| 7 | + ExecutionContextErrors |
| 8 | +} = require('./constants'); |
| 9 | + |
| 10 | +/** |
| 11 | + * The execution context maps which acts as the execution context in memory storage. |
| 12 | + * @see ExecutionContext.monitor |
| 13 | + * @type ExecutionContextMap |
| 14 | + */ |
| 15 | +const executionContextMap = new Map(); |
| 16 | + |
| 17 | +/** |
| 18 | + * Creates a root execution context node. |
| 19 | + * @param {Number} asyncId - The current async id. |
| 20 | + * @param {Object} initialContext - The initial context ro provide this execution chain. |
| 21 | + * @param {Object} config - The configuration the root context created with. |
| 22 | + * @param {Object} domain - The domain to create the execution context under. |
| 23 | + * @return {ExecutionContextNode} |
| 24 | + */ |
| 25 | +const createRootContext = ({ asyncId, initialContext, config, domain = ROOT_DOMAIN }) => ({ |
| 26 | + asyncId, |
| 27 | + domain, |
| 28 | + context: { ...initialContext, executionId: asyncId }, |
| 29 | + children: [], |
| 30 | + ...config, |
| 31 | + ...(config.monitor && { created: Date.now() }) |
| 32 | +}); |
| 33 | + |
| 34 | +/** |
| 35 | + * Handles execution context error, throws when none production. |
| 36 | + * @param {String} code - The error code to log. |
| 37 | + */ |
| 38 | +const handleError = (code) => { |
| 39 | + if (!isProduction()) { |
| 40 | + throw code; |
| 41 | + } |
| 42 | + |
| 43 | + console.error(code); // eslint-disable-line no-console |
| 44 | +}; |
| 45 | + |
| 46 | +class ExecutionContext { |
| 47 | + constructor() { |
| 48 | + this.config = { ...DEFAULT_CONFIG }; |
| 49 | + |
| 50 | + // Sets node async hooks setup |
| 51 | + asyncHooks.createHook( |
| 52 | + createHooks(executionContextMap) |
| 53 | + ).enable(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Configures current execution context. |
| 58 | + * @param {ExecutionContextConfig} config - the configuration to use. |
| 59 | + */ |
| 60 | + configure(config) { |
| 61 | + this.config = config; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Creates an execution context for the current asyncId process. |
| 66 | + * This will expose Context get / update at any point after. |
| 67 | + * @param {Object} initialContext - The initial context to be used. |
| 68 | + * @param {String} domain - The domain the context is created under. |
| 69 | + * @returns void |
| 70 | + */ |
| 71 | + create(initialContext = {}, domain = ROOT_DOMAIN) { |
| 72 | + const config = this.config; |
| 73 | + const asyncId = asyncHooks.executionAsyncId(); |
| 74 | + |
| 75 | + const refContext = executionContextMap.get(asyncId); |
| 76 | + if (refContext) { |
| 77 | + |
| 78 | + // Execution context creation is allowed once per domain |
| 79 | + if (domain === refContext.domain) return handleError(ExecutionContextErrors.CONTEXT_ALREADY_DECLARED); |
| 80 | + |
| 81 | + // Setting up domain initial context |
| 82 | + initialContext = { ...this.get(), ...initialContext }; |
| 83 | + |
| 84 | + // Disconnecting current async id from stored parent chain |
| 85 | + onChildProcessDestroy(executionContextMap, asyncId, refContext.ref); |
| 86 | + } |
| 87 | + |
| 88 | + // Creating root context node |
| 89 | + const rootContext = createRootContext({ |
| 90 | + asyncId, |
| 91 | + initialContext, |
| 92 | + config, |
| 93 | + domain |
| 94 | + }); |
| 95 | + |
| 96 | + executionContextMap.set(asyncId, rootContext); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Updates the current async process context. |
| 101 | + * @param {Object} update - The update to apply on the current process context. |
| 102 | + * @returns void |
| 103 | + */ |
| 104 | + update(update = {}) { |
| 105 | + const asyncId = asyncHooks.executionAsyncId(); |
| 106 | + |
| 107 | + if (!executionContextMap.has(asyncId)) return handleError(ExecutionContextErrors.CONTEXT_DOES_NOT_EXISTS); |
| 108 | + |
| 109 | + const contextData = executionContextMap.get(asyncId); |
| 110 | + |
| 111 | + // Update target is always the root context, ref updates will need to be channeled |
| 112 | + const targetContextData = contextData.ref |
| 113 | + ? executionContextMap.get(contextData.ref) |
| 114 | + : contextData; |
| 115 | + |
| 116 | + targetContextData.context = { ...targetContextData.context, ...update }; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Gets the current async process execution context. |
| 121 | + * @returns {Object} |
| 122 | + */ |
| 123 | + get() { |
| 124 | + const asyncId = asyncHooks.executionAsyncId(); |
| 125 | + if (!executionContextMap.has(asyncId)) return handleError(ExecutionContextErrors.CONTEXT_DOES_NOT_EXISTS); |
| 126 | + |
| 127 | + const { context = {}, ref } = executionContextMap.get(asyncId); |
| 128 | + if (ref) { |
| 129 | + |
| 130 | + // Ref will be used to point out on the root context |
| 131 | + return executionContextMap.get(ref).context; |
| 132 | + } |
| 133 | + |
| 134 | + // Root context |
| 135 | + return context; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Runs a given function within "AsyncResource" context, this will ensure the function executed within a uniq execution context. |
| 140 | + * @param {Function} fn - The function to run. |
| 141 | + * @param {Object} initialContext - The initial context to expose to the function execution. |
| 142 | + * @param {String} domain - The domain to create the exectuion context under. |
| 143 | + */ |
| 144 | + run(fn, initialContext, domain) { |
| 145 | + const resource = new ExecutionContextResource(); |
| 146 | + |
| 147 | + resource.runInAsyncScope(() => { |
| 148 | + this.create(initialContext, domain); |
| 149 | + |
| 150 | + fn(); |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Monitors current execution map usage |
| 156 | + * @return {ExecutionMapUsage} |
| 157 | + */ |
| 158 | + monitor() { |
| 159 | + if (!this.config.monitor) { |
| 160 | + throw new Error(ExecutionContextErrors.MONITOR_MISS_CONFIGURATION); |
| 161 | + } |
| 162 | + |
| 163 | + return monitorMap(executionContextMap); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +module.exports = ExecutionContext; |
0 commit comments