Skip to content

Commit 7cf1697

Browse files
committed
global exposed.
1 parent e77d8f5 commit 7cf1697

File tree

3 files changed

+100
-67
lines changed

3 files changed

+100
-67
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-execution-context",
3-
"version": "1.0.5",
3+
"version": "1.1.0",
44
"description": "Provides execution context wrapper for node JS, can be used to create execution wrapper for handling requests and more",
55
"author": "Oded Goldglas <[email protected]>",
66
"license": "ISC",

src/index.js

Lines changed: 72 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@ const { isProduction } = require('./lib');
33
const { create: createHooks } = require('./hooks');
44
const { ExecutionContextErrors } = require('./constants');
55

6-
/**
7-
* The global service context execution map
8-
* @type ExecutionContextMap
9-
*/
10-
const executionContextMap = new Map();
11-
12-
// Sets node async hooks setup
13-
asyncHooks.createHook(
14-
createHooks(executionContextMap)
15-
).enable();
16-
176
/**
187
* Handles execution context error, throws when none production
198
* @param code
@@ -28,65 +17,82 @@ const handleError = (code) => {
2817

2918
/**
3019
* The Execution Context API
20+
* @return {ExecutionContextAPI}
3121
*/
32-
const Context = {
33-
34-
/**
35-
* Creates an execution context for the current asyncId process.
36-
* This will expose Context get / update at any point after.
37-
* @param {Object} initialContext - The initial context to be used
38-
* @returns void
39-
*/
40-
create: (initialContext = {}) => {
41-
const asyncId = asyncHooks.executionAsyncId();
42-
43-
// Creation is allowed once per execution context
44-
if (executionContextMap.has(asyncId)) handleError(ExecutionContextErrors.CONTEXT_ALREADY_DECLARED);
45-
46-
executionContextMap.set(asyncId, {
47-
context: { ...initialContext, executionId: asyncId },
48-
children: []
49-
});
50-
},
51-
52-
/**
53-
* Updates the current async process context
54-
* @param {Object} update - The update to apply on the current process context
55-
* @returns void
56-
*/
57-
update: (update = {}) => {
58-
const asyncId = asyncHooks.executionAsyncId();
59-
60-
if (!executionContextMap.has(asyncId)) handleError(ExecutionContextErrors.CONTEXT_DOES_NOT_EXISTS);
61-
62-
const contextData = executionContextMap.get(asyncId);
63-
64-
// Update target is always the root context, ref updates will need to be channeled
65-
const targetContextData = contextData.ref
66-
? executionContextMap.get(contextData.ref)
67-
: contextData;
68-
69-
targetContextData.context = { ...targetContextData.context, ...update };
70-
},
22+
const createExecutionContext = () => {
7123

7224
/**
73-
* Gets the current async process execution context
74-
* @returns {Object}
25+
* The global service context execution map
26+
* @type ExecutionContextMap
7527
*/
76-
get: () => {
77-
const asyncId = asyncHooks.executionAsyncId();
78-
if (!executionContextMap.has(asyncId)) handleError(ExecutionContextErrors.CONTEXT_DOES_NOT_EXISTS);
79-
80-
const { context = {}, ref } = executionContextMap.get(asyncId);
81-
if (ref) {
82-
83-
// Ref will be used to point out on the root context
84-
return executionContextMap.get(ref).context;
28+
const executionContextMap = new Map();
29+
30+
// Sets node async hooks setup
31+
asyncHooks.createHook(
32+
createHooks(executionContextMap)
33+
).enable();
34+
35+
return {
36+
37+
/**
38+
* Creates an execution context for the current asyncId process.
39+
* This will expose Context get / update at any point after.
40+
* @param {Object} initialContext - The initial context to be used
41+
* @returns void
42+
*/
43+
create: (initialContext = {}) => {
44+
const asyncId = asyncHooks.executionAsyncId();
45+
46+
// Creation is allowed once per execution context
47+
if (executionContextMap.has(asyncId)) handleError(ExecutionContextErrors.CONTEXT_ALREADY_DECLARED);
48+
49+
executionContextMap.set(asyncId, {
50+
context: { ...initialContext, executionId: asyncId },
51+
children: []
52+
});
53+
},
54+
55+
/**
56+
* Updates the current async process context.
57+
* @param {Object} update - The update to apply on the current process context.
58+
* @returns void
59+
*/
60+
update: (update = {}) => {
61+
const asyncId = asyncHooks.executionAsyncId();
62+
63+
if (!executionContextMap.has(asyncId)) handleError(ExecutionContextErrors.CONTEXT_DOES_NOT_EXISTS);
64+
65+
const contextData = executionContextMap.get(asyncId);
66+
67+
// Update target is always the root context, ref updates will need to be channeled
68+
const targetContextData = contextData.ref
69+
? executionContextMap.get(contextData.ref)
70+
: contextData;
71+
72+
targetContextData.context = { ...targetContextData.context, ...update };
73+
},
74+
75+
/**
76+
* Gets the current async process execution context.
77+
* @returns {Object}
78+
*/
79+
get: () => {
80+
const asyncId = asyncHooks.executionAsyncId();
81+
if (!executionContextMap.has(asyncId)) handleError(ExecutionContextErrors.CONTEXT_DOES_NOT_EXISTS);
82+
83+
const { context = {}, ref } = executionContextMap.get(asyncId);
84+
if (ref) {
85+
86+
// Ref will be used to point out on the root context
87+
return executionContextMap.get(ref).context;
88+
}
89+
90+
// Root context
91+
return context;
8592
}
93+
};
94+
}
8695

87-
// Root context
88-
return context;
89-
}
90-
};
96+
global.ExecutionContext = global.ExecutionContext || createExecutionContext();
9197

92-
module.exports = Context;
98+
module.exports = global.ExecutionContext;

src/types.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,30 @@ interface HookCallbacks {
3030
*/
3131
destroy?(asyncId: number): void;
3232
}
33+
34+
interface ExecutionContextAPI {
35+
36+
/**
37+
* Creates an execution context for the current asyncId process.
38+
* @param initialContext
39+
*/
40+
create(initialContext: object): void;
41+
42+
/**
43+
* Updates the current async process context.
44+
* @param update
45+
*/
46+
update(update: object): void;
47+
48+
/**
49+
* Gets the current async process execution context.
50+
*/
51+
get(): object
52+
53+
/**
54+
* Runs a given function within an async resource context
55+
* @param fn
56+
* @param initialContext
57+
*/
58+
run(fn: Function, initialContext: object): void
59+
}

0 commit comments

Comments
 (0)