This repository has been archived by the owner on Dec 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.js
172 lines (139 loc) · 4.37 KB
/
debug.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
var index = require('./')
var streamId = 0;
Object.keys(index).forEach(function(key) {
exports[key] = index[key];
})
exports.create = function(writer, options) {
var asyncStream = index.create.apply(index, arguments);
var state = {
bufferId: 0,
substreamId: 0
}
if(arguments.length === 1 && typeof writer.write !== 'function') {
options = writer;
writer = null;
}
asyncStream.name = (options && options.name) || 'originalStream'+(streamId++ ? streamId : '');
asyncStream.on('finish', function() {
streamId--;
});
asyncStream.writer.name = 'originalWriter';
console.log('\n' + bold(asyncStream.name + green('.create()')));
console.log(createDiagram(asyncStream));
logEvents(asyncStream, state);
return asyncStream;
}
function logEvents(asyncStream, state) {
var _write = asyncStream.write;
asyncStream.write = function(str) {
var thisStream = _write.apply(this, arguments);
console.log('\n' + bold(this.name + cyan('.write(') + reset(grey(str)) + cyan(')')));
console.log(createDiagram(this));
return thisStream;
}
var _beginAsync = asyncStream.beginAsync;
asyncStream.beginAsync = function() {
var newStream = _beginAsync.apply(this, arguments);
newStream.name = newStream.name || 'stream'+(++state.substreamId);
this.writer.name = 'buffer'+(++state.bufferId);
console.log('\n' + bold(newStream.name + grey(' = ') + this.name + green('.beginAsync()')));
console.log(createDiagram(this));
logEvents(newStream, state);
return newStream;
}
var _end = asyncStream.end;
asyncStream.end = function() {
var thisStream = _end.apply(this, arguments);
console.log('\n' + bold(this.name + red('.end()')));
console.log(createDiagram(this));
return thisStream;
}
}
function createDiagram(asyncStream) {
var streams = [];
var writers = [];
var contents = [];
var currentWriter = asyncStream._originalWriter;
while(currentWriter) {
var currentIndex = writers.length;
writers.push(currentWriter.name);
contents.push(currentWriter.toString());
if(currentWriter.stream) {
streams[currentIndex] = currentWriter.stream.name;
}
if(currentWriter.stream === asyncStream) {
streams[currentIndex] += '*'
}
currentWriter = currentWriter.next;
}
var line1 = '';
var line2 = '';
var line3 = '';
var line4 = '';
writers.forEach(function(writer, i) {
var stream = streams[i] || '';
var content = contents[i] || '';
var longest = Math.max(writer.length, stream.length, content.length);
line1 += stream + ' '.repeat(longest-stream.length);
if(stream) {
line2 += '↕' + ' '.repeat(longest-1);
} else {
line2 += ' '.repeat(longest);
}
line3 += writer + ' '.repeat(longest-writer.length);
line4 += content + ' '.repeat(longest-content.length);
if(i !== writers.length-1) {
line1 += ' ';
line2 += ' ';
line3 += grey(' → ');
line4 += ' ';
}
})
if(line1 === ' '.repeat(line1.length)) line1 = null;
if(line2 === ' '.repeat(line2.length)) line2 = null;
return (line1 ? line1 + '\n' : '') + (line2 ? grey(line2) + '\n' : '') + line3 + '\n' + magenta(line4) + '\n';
}
var canFormat = (function () {
if (process.stdout && !process.stdout.isTTY) {
return false;
}
if (process.platform === 'win32') {
return true;
}
if ('COLORTERM' in process.env) {
return true;
}
if (process.env.TERM === 'dumb') {
return false;
}
if (/^screen|^xterm|^vt100|color|ansi|cygwin|linux/i.test(process.env.TERM)) {
return true;
}
return false;
})();
function format(str, begin, end) {
begin = canFormat ? '\u001b['+begin+'m' : '';
end = canFormat ? '\u001b['+end+'m' : '';
return begin + str + end;
}
function reset(str) {
return format(str, 0, 0);
}
function red(str) {
return format(str, 31, 39);
}
function cyan(str) {
return format(str, 36, 39);
}
function magenta(str) {
return format(str, 35, 39);
}
function green(str) {
return format(str, 32, 39);
}
function grey(str) {
return format(str, 90, 39);
}
function bold(str) {
return format(str, 1, 22);
}