-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
154 lines (132 loc) · 3.75 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const asyncHooks = require('async_hooks')
const config = {}
const callstackMap = new Map() // all callstack map
const TCPWrapCallstackContainers = new Map() // request root callstack
const DEFAULT_INTERVAL = 10 // remove expired context interval
const DEFAULT_EXPIRE = 150 // callstack expire time (must be longer than full lifecycle of a request)
const RES_ROOT_ID = '__HTTP_REQUEST_CONTEXT_ROOT_ID__'
// remove callstack after request finish/close
const removeCallstackTrigger = rootId => {
if (TCPWrapCallstackContainers.has(rootId)) {
TCPWrapCallstackContainers.get(rootId).id.forEach(asyncId => {
callstackMap.delete(asyncId)
})
TCPWrapCallstackContainers.delete(rootId)
}
}
// remove asyncId interval
const interval = () => {
setTimeout(interval, config.interval)
const now = Date.now()
for (const asyncId of callstackMap.keys()) {
if (now - callstackMap.get(asyncId).__tm < config.expire) {
break
} else {
callstackMap.delete(asyncId)
}
}
for (const rootId of TCPWrapCallstackContainers.keys()) {
if (now - TCPWrapCallstackContainers.get(rootId).__tm < config.expire) {
break
} else {
removeCallstackTrigger(rootId)
}
}
callstackMap.delete(asyncHooks.executionAsyncId())
}
// find callstack root
const findRootId = id => {
const callstack = callstackMap.get(id)
if (callstack) {
if (callstack.data) {
return id
}
return findRootId(callstack.id)
}
}
asyncHooks.createHook({
init (asyncId) {
callstackMap.set(asyncId, {
id: asyncHooks.executionAsyncId(),
__tm: Date.now()
})
if (config.optimize) {
const rootId = findRootId(asyncId)
if (rootId && TCPWrapCallstackContainers.has(rootId)) {
TCPWrapCallstackContainers.get(rootId).id.push(asyncId)
}
}
}
}).enable()
const middleware = res => {
const rootId = asyncHooks.executionAsyncId()
res[RES_ROOT_ID] = rootId
callstackMap.set(rootId, {
id: rootId,
data: {},
__tm: Date.now()
})
if (config.optimize) {
TCPWrapCallstackContainers.set(rootId, {
id: [rootId],
__tm: Date.now()
})
}
if (config.removeAfterFinish) {
res.on('finish', () => {
process.nextTick(() => {
removeCallstackTrigger(rootId)
})
})
}
if (config.removeAfterClose) {
res.on('close', () => {
process.nextTick(() => {
removeCallstackTrigger(rootId)
})
})
}
}
const initOptions = (options = {}) => {
Object.assign(config, {
interval: (options.interval || DEFAULT_INTERVAL) * 1000,
expire: (options.expire || DEFAULT_EXPIRE) * 1000,
removeAfterFinish: !!options.removeAfterFinish,
removeAfterClose: !!options.removeAfterClose,
optimize: !!options.removeAfterFinish || !!options.removeAfterClose
})
}
module.exports = {
middleware: options => {
initOptions(options)
interval()
return (req, res, next) => {
// res.set('connection', 'close')
middleware(res)
next()
}
},
koaMiddleware: options => {
initOptions(options)
interval()
return async (ctx, next) => {
// ctx.set('connection', 'close')
middleware(ctx.res)
await next()
}
},
set: (key, value) => {
const rootId = findRootId(asyncHooks.executionAsyncId())
if (rootId && callstackMap.has(rootId)) {
const data = Object.prototype.toString.call(key) === '[object Object]' ? key : { [key]: value }
Object.assign(callstackMap.get(rootId).data, data)
}
},
get: (key, res) => {
const rootId = res && res[RES_ROOT_ID] ? res[RES_ROOT_ID] : findRootId(asyncHooks.executionAsyncId())
if (rootId && callstackMap.has(rootId)) {
const { data } = callstackMap.get(rootId)
return typeof key === 'undefined' ? data : data[key]
}
}
}