node-execution-context
Advanced tools
Comparing version
# CHANGELOG | ||
## 3.0.2 (February 12, 2021) | ||
- Add `update` API for ease usage in case context is a plain `object`. | ||
## 3.0.1 (February 12, 2021) | ||
@@ -4,0 +8,0 @@ |
{ | ||
"name": "node-execution-context", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "Provides execution context wrapper for node JS, can be used to create execution wrapper for handling requests and more", | ||
@@ -5,0 +5,0 @@ "author": "Oded Goldglas <odedglas@gmail.com>", |
@@ -7,2 +7,3 @@ /** | ||
CONTEXT_DOES_NOT_EXIST: 'Execution context does not exists, please ensure to call create/run before.', | ||
UPDATE_BLOCKED: 'Calling "update" API is allowed only when provided context is a plain `object`.', | ||
MONITOR_MISS_CONFIGURATION: 'Monitoring option is off by default, please call `configure` with the proper options.' | ||
@@ -9,0 +10,0 @@ }; |
@@ -35,2 +35,11 @@ const { supportAsyncLocalStorage } = require('../lib'); | ||
/** | ||
* Updates the current asynchronous execution context with a given value. | ||
* @param {*} context - The new context to expose current asynchronous execution. | ||
* @returns void | ||
*/ | ||
update(context) { | ||
this.manager.update(context); | ||
} | ||
/** | ||
* Gets the current asynchronous execution context. | ||
@@ -37,0 +46,0 @@ * @return {*} |
@@ -55,2 +55,31 @@ const { ExecutionContextErrors } = require('./constants'); | ||
describe('Update', () => { | ||
it('Throws an error when context is not created', () => { | ||
expect(() => Context.get()) | ||
.toThrow(ExecutionContextErrors.CONTEXT_DOES_NOT_EXIST); | ||
}); | ||
it('Throws an error when context is a pain `object`', () => { | ||
Context.create({ my: 'thing' }); | ||
expect(() => Context.update('Hey')) | ||
.toThrow(ExecutionContextErrors.UPDATE_BLOCKED); | ||
}); | ||
describe('When context is created', () => { | ||
it('Update context', () => { | ||
Context.create({ val: 'value', other: 'o' }); | ||
const context = Context.get(); | ||
expect(context.val).toEqual('value'); | ||
Context.update({ val: false }); | ||
expect(Context.get()).toMatchObject({ | ||
val: false, | ||
other: 'o' | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Run', () => { | ||
@@ -57,0 +86,0 @@ let spies; |
@@ -1,3 +0,1 @@ | ||
import { ExecutionMapUsage } from '../lib/types'; | ||
interface ExecutionContextNode { | ||
@@ -4,0 +2,0 @@ asyncId: number; |
@@ -62,2 +62,10 @@ const semver = require('semver'); | ||
/** | ||
* Returns true if a given thing is an object | ||
* @param value - The thing to check. | ||
*/ | ||
isObject: (value) => !!value && | ||
!Array.isArray(value) && | ||
typeof value === 'object', | ||
/** | ||
* Returns a monitoring report over the "executionContext" memory usage. | ||
@@ -64,0 +72,0 @@ * @param {ExecutionContextMap} executionContextMap The execution map to monitor. |
@@ -38,2 +38,17 @@ const lib = require ('.'); | ||
describe('isObject', () => { | ||
it.each([ | ||
[false, false], | ||
[1, false], | ||
['', false], | ||
[undefined, false], | ||
[[], false], | ||
[() => {}, false], | ||
[class Test {}, false], | ||
[{}, true], | ||
])('Returns true when given value is object (%p)', (value, expected) => { | ||
expect(lib.isObject(value)).toEqual(expected); | ||
}); | ||
}); | ||
describe('supportAsyncLocalStorage', () => { | ||
@@ -40,0 +55,0 @@ describe('When node version is lower than 12.17.0', () => { |
const asyncHooks = require('async_hooks'); | ||
const { monitorMap, handleError, ExecutionContextResource } = require('../../lib'); | ||
const { monitorMap, handleError, isObject, ExecutionContextResource } = require('../../lib'); | ||
const { create: createHooks, onChildProcessDestroy } = require('./hooks'); | ||
@@ -106,2 +106,14 @@ const { ExecutionContextErrors } = require('../../ExecutionContext/constants'); | ||
update(context) { | ||
const asyncId = asyncHooks.executionAsyncId(); | ||
if (!executionContextMap.has(asyncId)) return handleError(ExecutionContextErrors.CONTEXT_DOES_NOT_EXIST); | ||
if (!isObject(context)) return handleError(ExecutionContextErrors.UPDATE_BLOCKED); | ||
// Update target is always the root context, ref updates will need to be channeled | ||
const rootContext = this._getRootContext(asyncId); | ||
Object.assign(rootContext.context, context); | ||
} | ||
configure(config) { | ||
@@ -108,0 +120,0 @@ this.config = config; |
const { AsyncLocalStorage } = require('async_hooks'); | ||
const { handleError, isUndefined } = require('../../lib'); | ||
const { handleError, isUndefined, isObject } = require('../../lib'); | ||
const { ExecutionContextErrors } = require('../../ExecutionContext/constants'); | ||
@@ -52,2 +52,16 @@ const { DEPRECATION_MESSAGES } = require('./constants'); | ||
update(context) { | ||
if (!validateStore(this.asyncLocaleStorage)) { | ||
return handleError(ExecutionContextErrors.CONTEXT_DOES_NOT_EXIST); | ||
} | ||
if (!isObject(context)) { | ||
return handleError(ExecutionContextErrors.UPDATE_BLOCKED); | ||
} | ||
const current = this.asyncLocaleStorage.getStore().context; | ||
Object.assign(current, context); | ||
} | ||
configure() { | ||
@@ -54,0 +68,0 @@ console.warn(DEPRECATION_MESSAGES.CONFIGURE); // eslint-disable-line no-console |
@@ -21,2 +21,8 @@ import { ExecutionMapUsage, ExecutionMapUsageEntry } from './lib/types'; | ||
/** | ||
* Updates the current asynchronous execution context with a given value. | ||
* @param context - The value to update. | ||
*/ | ||
update(context: Record<string, unknown>): void; | ||
/** | ||
* Gets the current asynchronous execution context. | ||
@@ -46,3 +52,3 @@ */ | ||
*/ | ||
monitor(): ExecutionMapUsage|undefined; | ||
monitor(): ExecutionMapUsage | undefined; | ||
} | ||
@@ -49,0 +55,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
210570
3.89%3216
3.81%