forked from davidflanagan/jstdg7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.js
33 lines (29 loc) · 1.38 KB
/
copy.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
// This function writes the specified chunk to the specified stream and
// returns a Promise that will be fulfilled when it is OK to write again.
// Because it returns a Promise, it can be used with await.
function write(stream, chunk) {
// Write the specified chunk to the specified stream
let hasMoreRoom = stream.write(chunk);
if (hasMoreRoom) { // If buffer is not full, return
return Promise.resolve(null); // an already resolved Promise object
} else {
return new Promise(resolve => { // Otherwise, return a Promise that
stream.once("drain", resolve); // resolves on the drain event.
});
}
}
// Copy data from the source stream to the destination stream
// respecting backpressure from the destination stream.
// This is much like calling source.pipe(destination).
async function copy(source, destination) {
// Set an error handler on the destination stream in case standard
// output closes unexpectedly (when piping output to `head`, e.g.)
destination.on("error", err => process.exit());
// Use a for/await loop to asynchronously read chunks from the input stream
for await (let chunk of source) {
// Write the chunk and wait until there is more room in the buffer.
await write(destination, chunk);
}
}
// Copy standard input to standard output
copy(process.stdin, process.stdout);