-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathc-cork.js
42 lines (36 loc) · 859 Bytes
/
c-cork.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
'use strict';
const stream = require('node:stream');
const ENTER = 13;
const createBufferedStream = () => {
const buffers = [];
const writable = new stream.Writable({
write(chunk, encoding, next) {
buffers.push(chunk);
next();
},
final(done) {
const result = Buffer.concat(buffers);
this.emit('result', result);
done();
},
});
return writable;
};
const lines = createBufferedStream();
lines.on('result', (result) => {
console.log({ result });
});
process.stdin.setRawMode(true);
process.stdin.on('data', (data) => {
const key = data[0];
if (key === ENTER) {
lines.write(data);
if (lines.writableCorked === 1) lines.uncork();
lines.end();
process.exit(0);
} else {
if (lines.writableCorked === 0) lines.cork();
lines.write(data);
}
process.stdout.write(data);
});