-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
319 lines (291 loc) · 8.88 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const asyncIdMap = new Map()
const sourceMap = new Map()
require('source-map-support').install({
retrieveSourceMap: (source) => {
const sourcemap = sourceMap.get(source)
if (sourcemap) {
return {
url: source,
map: sourcemap,
environment: 'node'
}
}
return null
}
})
const assert = require('assert')
const Module = require('module')
const async_hooks = require('async_hooks')
const _ = require('lodash')
const glob = require('glob')
const uuid = require('node-uuid')
const esprima = require('esprima')
const shimmer = require('shimmer')
const escodegen = require('escodegen')
const debug = require('debug')('koa-await-breakpoint')
async_hooks.createHook({
init (asyncId, type, triggerAsyncId) {
const ctx = getCtx(triggerAsyncId)
if (ctx) {
asyncIdMap.set(asyncId, ctx)
} else {
asyncIdMap.set(asyncId, triggerAsyncId)
}
},
destroy (asyncId) {
asyncIdMap.delete(asyncId)
}
}).enable()
const defaultOpt = {
sourcemap: true,
nodir: true,
absolute: true,
filter: {
ctx: ['state', 'params'],
request: ['method', 'path', 'header', 'query', 'body'],
response: ['status', 'body']
},
loggerName: 'logger',
requestIdPath: 'requestId'
}
module.exports = function (opt) {
opt = _.defaults(opt || {}, defaultOpt)
opt.filter = opt.filter || {}
opt.filter.ctx = opt.filter.ctx || defaultOpt.filter.ctx
opt.filter.request = opt.filter.request || defaultOpt.filter.request
opt.filter.response = opt.filter.response || defaultOpt.filter.response
debug('options: %j', opt)
const name = opt.name
const loggerName = opt.loggerName
const requestIdPath = opt.requestIdPath
const files = opt.files
const exclude_files = opt.exclude_files || []
const store = opt.store || { save: (record) => console.log('%j', record) }
const awaitCondition = opt.awaitCondition
const sourcemap = opt.sourcemap
assert(requestIdPath && _.isString(requestIdPath), '`requestIdPath` option must be string')
assert(files && _.isArray(files), '`files`{array} option required')
assert(_.isArray(exclude_files), '`exclude_files`{array} option required')
assert(store && _.isFunction(store.save), '`store.save`{function} option required, see: koa-yield-breakpoint-mongodb')
if (awaitCondition) {
assert(_.isFunction(awaitCondition), '`awaitCondition` option must be function')
}
// add global logger
global[loggerName] = async function (ctx, fn, fnStr, filename) {
const originalContext = ctx
let requestId = _getRequestId()
const asyncId = async_hooks.executionAsyncId()
if (!requestId) {
const _ctx = getCtx(asyncId)
if (_ctx) {
ctx = _ctx
requestId = _getRequestId()
}
} else {
asyncIdMap.set(asyncId, ctx)
}
let prevRecord
if (requestId) {
prevRecord = _logger('beforeAwait')
}
let result
try {
result = await fn.call(originalContext)
} catch (e) {
// use innermost error info
e._fn = e._fn || fnStr
e._filename = e._filename || filename
throw e
}
if (requestId) {
_logger('afterAwait', result, prevRecord && prevRecord.timestamp)
}
return result
function _getRequestId () {
return ctx && ctx.app && _.get(ctx, requestIdPath)
}
function _logger (type, result, prevTimestamp) {
const _this = _.pick(ctx, opt.filter.ctx)
_this.request = _.pick(ctx.request, opt.filter.request)
_this.response = _.pick(ctx.response, opt.filter.response)
const record = {
name,
requestId,
step: ++ctx.step,
filename,
timestamp: new Date(),
this: _this,
type,
fn: fnStr,
result
}
addTake(ctx, record, prevTimestamp)
debug(record)
store.save(record, ctx)
return record
}
}
let filenames = []
files.forEach(filePattern => {
if (filePattern) {
filenames = filenames.concat(glob.sync(filePattern, opt))
}
})
exclude_files.forEach(filePattern => {
if (filePattern) {
_.pullAll(filenames, glob.sync(filePattern, opt))
}
})
filenames = _.uniq(filenames)
debug('matched files: %j', filenames)
// wrap Module.prototype._compile
shimmer.wrap(Module.prototype, '_compile', function (__compile) {
return function koaBreakpointCompile (content, filename) {
if (!_.includes(filenames, filename)) {
try {
return __compile.call(this, content, filename)
} catch (e) {
// `try { require('...') } catch (e) { ... }` will not print compile error message
debug('cannot compile file: %s', filename)
debug(e.stack)
throw e
}
}
let parsedCodes
try {
parsedCodes = esprima.parse(content, { loc: true })
} catch (e) {
console.error('cannot parse file: %s', filename)
console.error(e.stack)
process.exit(1)
}
findAwaitAndWrapLogger(parsedCodes)
try {
content = escodegen.generate(parsedCodes, {
format: { indent: { style: ' ' } },
sourceMap: filename,
sourceMapWithCode: true
})
} catch (e) {
console.error('cannot generate code for file: %s', filename)
console.error(e.stack)
process.exit(1)
}
debug('file %s regenerate codes:\n%s', filename, content.code)
// add to sourcemap cache
if (sourcemap) {
sourceMap.set(filename, content.map.toString())
}
return __compile.call(this, content.code, filename)
function findAwaitAndWrapLogger (node) {
if (!node || typeof node !== 'object') {
return
}
let condition = {
wrapAwait: true,
deep: true
}
if (node.hasOwnProperty('type') && node.type === 'AwaitExpression' && !node.__skip) {
const codeLine = node.loc.start
const __argument = node.argument
const __expressionStr = escodegen.generate(__argument)
const expressionStr = `
global.${loggerName}(
(typeof ctx !== 'undefined' ? ctx : this),
function(){
return ${__expressionStr}
},
${JSON.stringify(__expressionStr)},
${JSON.stringify(filename + ':' + codeLine.line + ':' + codeLine.column)}
)`
if (awaitCondition) {
condition = awaitCondition(filename, __expressionStr, __argument) || condition
assert(typeof condition === 'object', '`awaitCondition` must return a object')
}
if (condition.wrapAwait) {
try {
node.argument = esprima.parse(expressionStr, { loc: true }).body[0].expression
node.delegate = true
try {
// skip process this AwaitExpression
node.argument.arguments[1].body.body[0].argument.__skip = true
// try correct loc
node.argument.arguments[1].body.body[0].argument.argument = __argument
} catch (e) { /* ignore */ }
} catch (e) {
console.error('cannot parse expression:')
console.error(expressionStr)
console.error(e.stack)
process.exit(1)
}
}
}
if (condition.deep) {
for (const key in node) {
if (node.hasOwnProperty(key)) {
findAwaitAndWrapLogger(node[key])
}
}
}
}
}
})
return async function koaAwaitBreakpoint (ctx, next) {
if (!_.get(ctx, requestIdPath)) {
_.set(ctx, requestIdPath, uuid.v4())
}
ctx.step = 0
ctx.timestamps = {}
_logger(ctx, 'start')
try {
await next()
} catch (e) {
_logger(ctx, 'error', e)
throw e
} finally {
_logger(ctx, 'end')
}
function _logger (ctx, type, err) {
const _this = _.pick(ctx, opt.filter.ctx)
_this.request = _.pick(ctx.request, opt.filter.request)
_this.response = _.pick(ctx.response, opt.filter.response)
const record = {
name,
requestId: _.get(ctx, requestIdPath),
timestamp: new Date(),
this: _this,
type,
step: ++ctx.step
}
if (err) {
record.error = err
record.fn = err._fn
record.filename = err._filename
delete err._fn
delete err._filename
}
addTake(ctx, record)
debug(record)
store.save(record, ctx)
}
}
}
function addTake (ctx, record, prevTimestamp) {
ctx.timestamps[record.step] = record.timestamp
prevTimestamp = prevTimestamp || ctx.timestamps[record.step - 1]
if (prevTimestamp) {
record.take = record.timestamp - prevTimestamp
} else {
// start default 0
record.take = 0
}
}
function getCtx (asyncId) {
if (!asyncId) {
return
}
if (typeof asyncId === 'object' && asyncId.app) {
return asyncId
}
return getCtx(asyncIdMap.get(asyncId))
}