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');
const asyncHooks = require('async_hooks')
const print = require('../lib/print')
function init(id, type, triggerAsyncId, resource) {
print({ stage: 'init', id, type, triggerAsyncId })
}
const hook = asyncHooks.createHook({ init })
setTimeout(() => {}, 10)
// TODO:
// Here we don't see anything printed. Do you know why?
hook.enable()
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);
}));
const async_hooks = require('async_hooks');
const hook = async_hooks.createHook({
init(asyncId, type, triggerAsyncId) { }
});
hook.enable();
function createHooks(namespace) {
function init(asyncId, type, triggerId, resource) {
if (namespace.context[triggerId]) {
namespace.context[asyncId] = namespace.context[triggerId];
}
}
function destroy(asyncId) {
delete namespace.context[asyncId];
}
const asyncHook = asyncHooks.createHook({ init, destroy });
asyncHook.enable();
}