Created
April 30, 2020 22:21
-
-
Save vdeturckheim/abdaf3b7316e88bf9004507fb6dd1712 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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