@opentelemetry/context-async-hooks
Advanced tools
Comparing version 0.9.0 to 0.10.0
@@ -1,3 +0,4 @@ | ||
import { ContextManager, Context } from '@opentelemetry/context-base'; | ||
export declare class AsyncHooksContextManager implements ContextManager { | ||
import { Context } from '@opentelemetry/context-base'; | ||
import { AbstractAsyncHooksContextManager } from './AbstractAsyncHooksContextManager'; | ||
export declare class AsyncHooksContextManager extends AbstractAsyncHooksContextManager { | ||
private _asyncHook; | ||
@@ -9,37 +10,5 @@ private _contexts; | ||
with<T extends (...args: unknown[]) => ReturnType<T>>(context: Context, fn: T): ReturnType<T>; | ||
bind<T>(target: T, context?: Context): T; | ||
enable(): this; | ||
disable(): this; | ||
private _bindFunction; | ||
/** | ||
* By default, EventEmitter call their callback with their context, which we do | ||
* not want, instead we will bind a specific context to all callbacks that | ||
* go through it. | ||
* @param target EventEmitter a instance of EventEmitter to patch | ||
* @param context the context we want to bind | ||
*/ | ||
private _bindEventEmitter; | ||
/** | ||
* Patch methods that remove a given listener so that we match the "patched" | ||
* version of that listener (the one that propagate context). | ||
* @param ee EventEmitter instance | ||
* @param original reference to the patched method | ||
*/ | ||
private _patchRemoveListener; | ||
/** | ||
* Patch methods that remove all listeners so we remove our | ||
* internal references for a given event. | ||
* @param ee EventEmitter instance | ||
* @param original reference to the patched method | ||
*/ | ||
private _patchRemoveAllListeners; | ||
/** | ||
* Patch methods on an event emitter instance that can add listeners so we | ||
* can force them to propagate a given context. | ||
* @param ee EventEmitter instance | ||
* @param original reference to the patched method | ||
* @param [context] context to propagate when calling listeners | ||
*/ | ||
private _patchAddListener; | ||
/** | ||
* Init hook will be called when userland create a async context, setting the | ||
@@ -57,3 +26,3 @@ * context as the current one if it exist. | ||
/** | ||
* Before hook is called just beforing executing a async context. | ||
* Before hook is called just before executing a async context. | ||
* @param uid uid of the async context | ||
@@ -60,0 +29,0 @@ */ |
@@ -21,12 +21,6 @@ "use strict"; | ||
const asyncHooks = require("async_hooks"); | ||
const events_1 = require("events"); | ||
const ADD_LISTENER_METHODS = [ | ||
'addListener', | ||
'on', | ||
'once', | ||
'prependListener', | ||
'prependOnceListener', | ||
]; | ||
class AsyncHooksContextManager { | ||
const AbstractAsyncHooksContextManager_1 = require("./AbstractAsyncHooksContextManager"); | ||
class AsyncHooksContextManager extends AbstractAsyncHooksContextManager_1.AbstractAsyncHooksContextManager { | ||
constructor() { | ||
super(); | ||
this._contexts = new Map(); | ||
@@ -55,15 +49,2 @@ this._stack = []; | ||
} | ||
bind(target, context) { | ||
// if no specific context to propagate is given, we use the current one | ||
if (context === undefined) { | ||
context = this.active(); | ||
} | ||
if (target instanceof events_1.EventEmitter) { | ||
return this._bindEventEmitter(target, context); | ||
} | ||
else if (typeof target === 'function') { | ||
return this._bindFunction(target, context); | ||
} | ||
return target; | ||
} | ||
enable() { | ||
@@ -79,108 +60,3 @@ this._asyncHook.enable(); | ||
} | ||
_bindFunction(target, context) { | ||
const manager = this; | ||
const contextWrapper = function (...args) { | ||
return manager.with(context, () => target.apply(this, args)); | ||
}; | ||
Object.defineProperty(contextWrapper, 'length', { | ||
enumerable: false, | ||
configurable: true, | ||
writable: false, | ||
value: target.length, | ||
}); | ||
/** | ||
* It isn't possible to tell Typescript that contextWrapper is the same as T | ||
* so we forced to cast as any here. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return contextWrapper; | ||
} | ||
/** | ||
* By default, EventEmitter call their callback with their context, which we do | ||
* not want, instead we will bind a specific context to all callbacks that | ||
* go through it. | ||
* @param target EventEmitter a instance of EventEmitter to patch | ||
* @param context the context we want to bind | ||
*/ | ||
_bindEventEmitter(target, context) { | ||
const ee = target; | ||
if (ee.__ot_listeners !== undefined) | ||
return target; | ||
ee.__ot_listeners = {}; | ||
// patch methods that add a listener to propagate context | ||
ADD_LISTENER_METHODS.forEach(methodName => { | ||
if (ee[methodName] === undefined) | ||
return; | ||
ee[methodName] = this._patchAddListener(ee, ee[methodName], context); | ||
}); | ||
// patch methods that remove a listener | ||
if (typeof ee.removeListener === 'function') { | ||
ee.removeListener = this._patchRemoveListener(ee, ee.removeListener); | ||
} | ||
if (typeof ee.off === 'function') { | ||
ee.off = this._patchRemoveListener(ee, ee.off); | ||
} | ||
// patch method that remove all listeners | ||
if (typeof ee.removeAllListeners === 'function') { | ||
ee.removeAllListeners = this._patchRemoveAllListeners(ee, ee.removeAllListeners); | ||
} | ||
return target; | ||
} | ||
/** | ||
* Patch methods that remove a given listener so that we match the "patched" | ||
* version of that listener (the one that propagate context). | ||
* @param ee EventEmitter instance | ||
* @param original reference to the patched method | ||
*/ | ||
_patchRemoveListener(ee, original) { | ||
return function (event, listener) { | ||
if (ee.__ot_listeners === undefined || | ||
ee.__ot_listeners[event] === undefined) { | ||
return original.call(this, event, listener); | ||
} | ||
const events = ee.__ot_listeners[event]; | ||
const patchedListener = events.get(listener); | ||
return original.call(this, event, patchedListener || listener); | ||
}; | ||
} | ||
/** | ||
* Patch methods that remove all listeners so we remove our | ||
* internal references for a given event. | ||
* @param ee EventEmitter instance | ||
* @param original reference to the patched method | ||
*/ | ||
_patchRemoveAllListeners(ee, original) { | ||
return function (event) { | ||
if (ee.__ot_listeners === undefined || | ||
ee.__ot_listeners[event] === undefined) { | ||
return original.call(this, event); | ||
} | ||
delete ee.__ot_listeners[event]; | ||
return original.call(this, event); | ||
}; | ||
} | ||
/** | ||
* Patch methods on an event emitter instance that can add listeners so we | ||
* can force them to propagate a given context. | ||
* @param ee EventEmitter instance | ||
* @param original reference to the patched method | ||
* @param [context] context to propagate when calling listeners | ||
*/ | ||
_patchAddListener(ee, original, context) { | ||
const contextManager = this; | ||
return function (event, listener) { | ||
if (ee.__ot_listeners === undefined) | ||
ee.__ot_listeners = {}; | ||
let listeners = ee.__ot_listeners[event]; | ||
if (listeners === undefined) { | ||
listeners = new WeakMap(); | ||
ee.__ot_listeners[event] = listeners; | ||
} | ||
const patchedListener = contextManager.bind(listener, context); | ||
// store a weak reference of the user listener to ours | ||
listeners.set(listener, patchedListener); | ||
return original.call(this, event, patchedListener); | ||
}; | ||
} | ||
/** | ||
* Init hook will be called when userland create a async context, setting the | ||
@@ -205,3 +81,3 @@ * context as the current one if it exist. | ||
/** | ||
* Before hook is called just beforing executing a async context. | ||
* Before hook is called just before executing a async context. | ||
* @param uid uid of the async context | ||
@@ -208,0 +84,0 @@ */ |
export * from './AsyncHooksContextManager'; | ||
export * from './AsyncLocalStorageContextManager'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -29,2 +29,3 @@ "use strict"; | ||
__exportStar(require("./AsyncHooksContextManager"), exports); | ||
__exportStar(require("./AsyncLocalStorageContextManager"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
export declare const VERSION = "0.9.0"; | ||
export declare const VERSION = "0.10.0"; | ||
//# sourceMappingURL=version.d.ts.map |
@@ -20,3 +20,3 @@ "use strict"; | ||
// this is autogenerated file, see scripts/version-update.js | ||
exports.VERSION = '0.9.0'; | ||
exports.VERSION = '0.10.0'; | ||
//# sourceMappingURL=version.js.map |
{ | ||
"name": "@opentelemetry/context-async-hooks", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"description": "OpenTelemetry AsyncHooks-based Context Manager", | ||
@@ -35,2 +35,3 @@ "main": "build/src/index.js", | ||
"build/src/**/*.js", | ||
"build/src/**/*.js.map", | ||
"build/src/**/*.d.ts", | ||
@@ -45,6 +46,6 @@ "doc", | ||
"devDependencies": { | ||
"@types/mocha": "7.0.2", | ||
"@types/node": "14.0.13", | ||
"@types/mocha": "8.0.0", | ||
"@types/node": "14.0.25", | ||
"@types/shimmer": "1.0.1", | ||
"codecov": "3.7.0", | ||
"codecov": "3.7.2", | ||
"gts": "2.0.2", | ||
@@ -56,7 +57,8 @@ "mocha": "7.2.0", | ||
"ts-node": "8.10.2", | ||
"typescript": "3.9.5" | ||
"typescript": "3.9.7" | ||
}, | ||
"dependencies": { | ||
"@opentelemetry/context-base": "^0.9.0" | ||
} | ||
"@opentelemetry/context-base": "^0.10.0" | ||
}, | ||
"gitHead": "ab62a4d69b99b3a8c9c26100c04f3226af7859df" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40023
18
433
3
1
+ Added@opentelemetry/context-base@0.10.2(transitive)
- Removed@opentelemetry/context-base@0.9.0(transitive)