-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput-buffer.js
88 lines (60 loc) · 2.53 KB
/
output-buffer.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
class OutputBuffer {
constructor(stream, options) {
// options parameter is optional
options = options || {};
// setup buffer to capture output, so we can recover it later
// NOTE: default maximal buffer size is 10kb
this.buffer = [];
this.bufferOverFlow = false;
this.bufferSize = 0;
this.maxBufferSize = options.maxBufferSize || 10 * 1024;
// original stream whose output needs to be captured
this.stream = stream;
this.originalStreamWriter = stream.write;
// start capturing output
this.capture();
}
capture() {
// overwrite stream writer with function that captures the output
this.stream.write = (string, encoding, fd) => {
// check if buffer can be extended
if (this.bufferSize + string.length <= this.maxBufferSize) {
// append string to buffer
this.buffer.push(string);
this.bufferSize += string.length;
} else {
// extend buffer if it has not overflown previously
if (!this.bufferOverFlow) {
this.buffer.push(
(
this.maxBufferSize - this.bufferSize < 3 ?
'' :
string.slice(0, this.maxBufferSize - this.bufferSize)
)
+ "..."
);
this.bufferSize = this.maxBufferSize;
// indicate that a buffer overflow has occurred
this.bufferOverFlow = true;
}
// throw an Error to indicate a buffer overrun
throw new Error(`output buffer has exceeded maximal size (${
this.maxBufferSize.toString()
} bytes)`);
}
};
// return current object for chaining
return this;
}
release() {
// restore original stream writer
this.stream.write = this.originalStreamWriter;
// return current object for chaining
return this;
}
get output() {
// return string that has been written to the output stream
return this.buffer.join('');
}
}
module.exports = OutputBuffer;