-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
96 lines (84 loc) · 2.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict'
const asyncHooks = require('async_hooks')
const cache = new Map()// WeakMap maybe?
let resourcesCount = 0
const asyncHooksRegExNode8 = /\((internal\/)?async_hooks\.js:/
const asyncHooksRegExNode16 = /node:internal\/async_hooks:/
function isInternalStacktrace (frames, i) {
return !asyncHooksRegExNode16.test(frames[i]) && !asyncHooksRegExNode8.test(frames[i])
}
function cleanStack (stack) {
const frames = stack.split('\n')
// this part is opinionated, but it's here to avoid confusing people with internals
let i = frames.length - 1
while (i && isInternalStacktrace(frames, i)) {
i--
}
return frames.slice(i + 1, stack.length - 1)
}
module.exports = (callback, options) => {
let continuityId
options = options || {}
options.threshold = (options.threshold || 20)
options.resourcesCap = (options.resourcesCap || 0)
Error.stackTraceLimit = Infinity
const asyncHook = asyncHooks.createHook({ init, before, after, destroy })
const debugLog = (title, message) => (options.debug && process._rawDebug(title, message))
function init (asyncId, type, triggerAsyncId, resource) {
const e = {}
Error.captureStackTrace(e)
debugLog('init', asyncId)
const cached = { asyncId, type, stack: e.stack }
if (options.resourcesCap > resourcesCount) {
cached.resource = resource
resourcesCount += 1
}
cache.set(asyncId, cached)
}
function before (asyncId) {
debugLog('before', asyncId)
if (options.trimFalsePositives) {
continuityId = asyncId
}
const cached = cache.get(asyncId)
if (!cached) { return }
cached.t0 = hrtime()
}
function after (asyncId) {
debugLog('after', asyncId)
if (options.trimFalsePositives && continuityId !== asyncId) {
// drop for interuptions
return
}
const cached = cache.get(asyncId)
if (!cached) { return }
const t1 = hrtime()
const dt = (t1 - cached.t0) / 1000
// process._rawDebug(dt > options.threshold, options.threshold, dt, cached)
if (dt > options.threshold) {
debugLog('stack', cached.stack)
callback(dt, cleanStack(cached.stack), {
type: cached.type,
resource: cached.resource
})
}
}
function destroy (asyncId) {
const cached = cache.get(asyncId)
if (!cached) { return }
if (cached.resource) {
resourcesCount -= 1
}
cache.delete(asyncId)
}
asyncHook.enable()
return {
stop: () => {
asyncHook.disable()
}
}
}
function hrtime () {
const t = process.hrtime()
return t[0] * 1000000 + t[1] / 1000
}