-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrack.js
124 lines (110 loc) · 3.39 KB
/
track.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
/**
* Created by yyrdl on 2017/6/1. Happy Children's Day
* record and modify callstack
*
*/
/**
* the separator between with each callstack-frame
* */
var STACK_LINE_SEPARATOR = null;
/**
* the first native callstack-frame ,right after the custom code
* **/
var END_LINE = null;
var BLANK_LENGTH = 5;
/**
* initialize
* */
(function () {
var error = new Error();
var stack = error.stack;
var index = 0;
index = stack.indexOf(error.name) + error.name.length;
end = stack.indexOf("at") + "at".length;
STACK_LINE_SEPARATOR = stack.substring(index, end);
BLANK_LENGTH = STACK_LINE_SEPARATOR.length - "at".length;
index = stack.indexOf(STACK_LINE_SEPARATOR);
index = stack.indexOf(STACK_LINE_SEPARATOR, index + 1);
index = stack.indexOf(STACK_LINE_SEPARATOR, index + 1);
var end = stack.indexOf(STACK_LINE_SEPARATOR, index + 1);
END_LINE = stack.substring(index, end);
})()
var tail = function (stack) {
var index = stack.indexOf("zco_core_run");
if (index > -1) {
var next_index = stack.lastIndexOf("next", index);
if (next_index != -1 && stack.lastIndexOf(STACK_LINE_SEPARATOR, index) === stack.indexOf(STACK_LINE_SEPARATOR, next_index)) {
index = stack.lastIndexOf(STACK_LINE_SEPARATOR, next_index);
} else {
index = -1;
}
}
return index;
}
var appendStackFrame = function (error, frame) {
if (error && "string" === typeof error.stack) {
var index = tail(error.stack);
if (index != -1) {
error.stack = error.stack.slice(0, index) + "\n " + frame;
} else {
/**
* some times the error is not been caught by zco
*
* */
error.stack = error.stack + "\n " + frame;
}
}
}
var callStackFrame = function (deep) {
var stack = (new Error()).stack; // this is the most expensive operation
var end_index = stack.indexOf(END_LINE);
if (end_index === -1) {
end_index = stack.length;
}
var start_index = stack.indexOf(STACK_LINE_SEPARATOR);
if (!deep) {
deep = 1;
}
for (var i = 0; i < deep; i++) {
start_index = stack.indexOf(STACK_LINE_SEPARATOR, start_index + 1);
}
stack = stack.slice(start_index + BLANK_LENGTH, end_index);
/**
* remove zco internal call-stack
* */
var next_index = stack.indexOf("next");
if (next_index > -1 && stack.indexOf(STACK_LINE_SEPARATOR + " zco_core", next_index) === stack.indexOf(STACK_LINE_SEPARATOR, next_index)) {
var head = stack.slice(0, stack.lastIndexOf(STACK_LINE_SEPARATOR, next_index));
end_index = next_index;
for (; ; ) {
start_index = stack.indexOf("zco_core", end_index + 1);
if (start_index > -1) {
end_index = start_index;
} else {
break;
}
}
end_index = stack.indexOf(STACK_LINE_SEPARATOR, end_index + 1);
if (end_index < 0 || (end_index + STACK_LINE_SEPARATOR.length) == stack.length) {
stack = head;
} else {
stack = head + stack.slice(end_index, stack.length);
}
}
return stack;
}
var makeTimeoutError = function (msg) {
var error = new Error(msg);
error.name = "TimeoutError";
var stack = error.stack;
var first_index = stack.indexOf(STACK_LINE_SEPARATOR);
var msg = stack.substring(0, first_index);
first_index = stack.indexOf(STACK_LINE_SEPARATOR, first_index + 1);
first_index = stack.indexOf(STACK_LINE_SEPARATOR, first_index + 1);
stack = stack.substring(first_index, stack.length);
error.stack = msg + stack;
return error;
}
exports.appendStackFrame = appendStackFrame;
exports.callStackFrame = callStackFrame;
exports.makeTimeoutError = makeTimeoutError;