Skip to content

Instantly share code, notes, and snippets.

@vdeturckheim
Created April 30, 2020 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdeturckheim/abdaf3b7316e88bf9004507fb6dd1712 to your computer and use it in GitHub Desktop.
Save vdeturckheim/abdaf3b7316e88bf9004507fb6dd1712 to your computer and use it in GitHub Desktop.
'use strict';
const run = function (getStack) {
process.nextTick(() => {
process.nextTick(() => {
process.nextTick(() => {
process.nextTick(() => {
process.nextTick(() => {
process.nextTick(() => {
console.log(getStack());
})
})
})
})
})
})
}
const AH = require('async_hooks');
const getFrames = function () {
let res = [];
const f0 = Error.prepareStackTrace;
Error.prepareStackTrace = function (_, x) {
res = x;
}
new Error().stack;
Error.prepareStackTrace = f0;
return res;
};
const hook = AH.createHook({
init(asyncId, type, triggerAsyncId, resource) {
const parent = AH.executionAsyncResource();
const frames = getFrames();
if (parent.frames) {
frames.push(...parent.frames);
}
resource.frames = frames;
}
})
.enable();
const getStrackTrace = function () {
return AH.executionAsyncResource().frames.map((x) => x.getFileName() + ':' + x.getLineNumber()).join('\n');
}
run(getStrackTrace);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment