-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasynctrace.js
163 lines (139 loc) · 4.7 KB
/
asynctrace.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
'use strict';
try {
var tracing = require('tracing');
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') throw e;
require('async-listener');
tracing = process;
}
var util = require('util');
var PATH_PREFIX = process.cwd().toLowerCase();
var sep = require('path').sep;
var settings = {
// `null`ing a style will remove it from the trace output
tracingModuleStyle: null,
// tracingModuleStyle: "\x1B[31m",
modulesStyle: "\x1B[32m",
globalsStyle: "\x1B[33m",
coreStyle: "\x1B[34m",
ownStyle: "\x1B[1m",
mocha: true,
BOUNDARY: ' [sync boundary]',
useColors: true
};
tracing.addAsyncListener({
'create': asyncFunctionInitialized,
'before': asyncCallbackBefore,
'error': asyncCallbackError,
'after': asyncCallbackAfter
});
function asyncFunctionInitialized(oldFrames) {
oldFrames = oldFrames || Error._frames || [];
var frames = StackError.getStackFrames(asyncFunctionInitialized);
var funcName;
try { funcName = frames[1] && frames[1].getFunctionName(); } catch (e) {}
if (funcName === 'createTCP') return oldFrames;
frames.unshift(settings.BOUNDARY);
frames.push.apply(frames, oldFrames);
Error._frames = frames;
return frames;
}
function asyncCallbackBefore(__, frames) {
Error._frames = frames;
}
function asyncCallbackAfter(__, frames) {
Error._frames = frames;
}
function asyncCallbackError(oldFrames, error) {
if (error._passed) return;
var frames = (oldFrames || []);
error.stack += v8StackFormating('', frames);
error._passed = true;
}
/* ===================== stack chain util ======================== */
function StackError(otp) {
Error.captureStackTrace(this, otp);
Error.prepareStackTrace = function justStoreStackStace(error, frames) {
error._frames = frames;
return '';
};
this.stack; // jshint ignore:line
delete Error.prepareStackTrace;
}
StackError.getStackFrames = function getStackFrames(otp) {
return (new this(otp))._frames;
};
util.inherits(StackError, Error);
/* ===================== stack chain manipulation & formating ======================== */
function categorizeFrame(frame) {
var name = frame && frame.getFileName() && frame.getFileName().toLowerCase();
if (!name) return (frame._section = 'core');
if (name === 'tracing.js') return (frame._section = 'tracingModule');
if (!~name.indexOf(sep)) return (frame._section = 'core');
if (name.indexOf(PATH_PREFIX) !== 0) return (frame._section = 'globals');
if (~(name.replace(PATH_PREFIX, '')).indexOf('node_modules')) return (frame._section = 'modules');
frame._section = 'own';
}
function reducer(seed, frame) {
if (typeof frame == 'string') {
if (frame != seed[seed.length - 1]) seed.push(frame);
return seed;
}
categorizeFrame(frame);
seed.push(frame);
return seed;
}
function v8StackFormating(error, frames) {
frames = frames.reduce(reducer, []);
var lines = [];
lines.push(error.toString());
frames.push({ toString: function () { return '<the nexus>\n'; }, _section: 'core' });
for (var i = 0; i < frames.length; i++) {
var frame = frames[i];
if (typeof frame == 'string') {
lines.push(frame);
continue;
}
var line;
try {
line = frame.toString();
} catch (e) {
try {
line = "<error: " + e + ">";
} catch (ee) {
// Any code that reaches this point is seriously nasty!
line = "<error>";
}
}
var style = getStyle(frame._section);
var prefix = style + " at ";
var suffix = settings.useColors ? "\x1B[0m" : '';
if (typeof style == 'string') lines.push(prefix + line + suffix);
}
return lines.join("\n");
}
function getStyle(sec) {
return (settings.useColors) ? settings[sec + 'Style'] : '';
}
/* ===================== 3rd party integrations ======================== */
function setupForMocha() {
try {
var mocha = Object.keys(require.cache)
.filter(function (k) { return ~k.search(/mocha.index\.js/); })
.map(function (k) { return require.cache[k].exports; })
.pop();
var shimmer = require('shimmer');
shimmer.wrap(mocha.prototype, 'run', function (original) {
return function () {
var runner = original.apply(this, arguments);
settings.useColors = this.options.useColors;
runner.on('test', function () {
Error._frames = null;
});
};
});
} catch (e) {
if (!(e instanceof TypeError)) console.error(e);
}
}
if (settings.mocha) setupForMocha();