-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
55 lines (43 loc) · 1.07 KB
/
test.js
File metadata and controls
55 lines (43 loc) · 1.07 KB
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
/* eslint-disable */
const { spawn } = require("child_process");
const { PassThrough, Transform } = require("stream");
const sp = spawn("node", ["-e", "console.error('MockError')"]);
class MyTransform extends Transform {
lastline = "";
prefix = "";
constructor(prefix) {
super();
this.prefix = prefix;
}
_transform(chunk, encoding, cb) {
const data = this.lastline + String(chunk);
const lines = data.split("\n");
this.lastline = lines.pop();
for (const line of lines) {
console.log(line);
this.push(this.prefix + line + "\n");
}
cb();
}
_flush(cb) {
console.log(this.lastline);
if (this.lastline.length > 0) {
this.push(this.prefix + this.lastline);
}
cb();
}
}
const t1 = new MyTransform("[child] ");
const tunnel = new PassThrough();
const chunks = [];
tunnel.on("data", data => {
chunks.push(data);
});
tunnel.on("end", () => {
console.log(
"\n\n-------------\n" +
Buffer.concat(chunks).toString() +
"\n-------------\n\n"
);
});
sp.stderr.pipe(tunnel).pipe(process.stderr);