Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
common.expectsError(
() => new AsyncResource(), {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
});
common.expectsError(() => {
new AsyncResource('invalid_trigger_id', { triggerAsyncId: null });
}, {
code: 'ERR_INVALID_ASYNC_ID',
type: RangeError,
});
assert.strictEqual(
new AsyncResource('default_trigger_id').triggerAsyncId(),
async_hooks.executionAsyncId()
);
// create first custom event 'alcazares' with triggerAsyncId derived
// from async_hooks executionAsyncId
const alcaTriggerId = async_hooks.executionAsyncId();
const alcaEvent = new AsyncResource('alcazares', alcaTriggerId);
const alcazaresActivities = hooks.activitiesOfTypes([ 'alcazares' ]);
// alcazares event was constructed and thus only has an `init` call
assert.strictEqual(alcazaresActivities.length, 1);
const alcazares = alcazaresActivities[0];
assert.strictEqual(alcazares.type, 'alcazares');
assert.strictEqual(typeof alcazares.uid, 'number');
assert.strictEqual(alcazares.triggerAsyncId, alcaTriggerId);
checkInvocations(alcazares, { init: 1 }, 'alcazares constructed');
async function afterAsync(res) {
let contextMap = Instrument.contextMap
let context = contextMap.get(asyncHooks.executionAsyncId())
// let id = asyncHooks.executionAsyncId()
await res
// console.log(`after ${context.span.operationName}:${id}`)
context.span.finish()
return res
}
function after(res) {
constructor (onerror) {
if (typeof onerror !== 'function') {
// point at where the wrong thing was actually done
const er = new TypeError('onerror must be a function')
Error.captureStackTrace(er, this.constructor)
throw er
}
const eid = executionAsyncId()
this.ids = new Set([eid])
this.onerror = onerror
this.parent = domains.get(executionAsyncId())
this.destroyed = false
domains.set(eid, this)
activateDomains()
}
a.runInAsyncScope(() => {
assert.strictEqual(a.asyncId(), async_hooks.executionAsyncId());
assert.strictEqual(a.triggerAsyncId(), async_hooks.triggerAsyncId());
if (n >= 0)
recurse(n - 1);
assert.strictEqual(a.asyncId(), async_hooks.executionAsyncId());
assert.strictEqual(a.triggerAsyncId(), async_hooks.triggerAsyncId());
});
}
Promise.resolve(42).then(common.mustCall(() => {
assert.strictEqual(async_hooks.executionAsyncId(), promiseAsyncIds[1]);
assert.strictEqual(async_hooks.triggerAsyncId(), promiseAsyncIds[0]);
Promise.resolve(10);
}));
process.on('uncaughtException', common.mustCall(() => {
assert.strictEqual(call_id, async_hooks.executionAsyncId());
call_log[2]++;
}));
async_hooks.runInAsyncIdScope(asyncId, common.mustCall(() => {
assert.strictEqual(async_hooks.executionAsyncId(), asyncId);
}));
if (!hook) {
hook = createHook({
init(asyncId, type, triggerAsyncId) {
const context = contexts.get(triggerAsyncId);
if (context) {
contexts.set(asyncId, context);
}
},
destroy(asyncId) {
contexts.delete(asyncId);
}
})
hook.enable();
}
const id = executionAsyncId();
const oldValue = contexts.get(id);
contexts.set(id, normalizeFlags(flags));
try {
fn();
} finally {
if (oldValue === undefined) {
contexts.delete(id);
} else {
contexts.set(id, oldValue);
}
}
};
export function printAsyncStack() {
console.log(`Async stack:`);
let res = asyncObjects.get(asyncHooks.executionAsyncId());
while (res) {
const { stack, ...rest } = res;
console.log(`%O\n${stack}`, rest);
res = asyncObjects.get(res.triggerId);
}
}
set(key, val) {
const eid = asyncHooks.executionAsyncId();
this.context[eid][key] = val;
}