forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreams.js
More file actions
32 lines (29 loc) · 862 Bytes
/
streams.js
File metadata and controls
32 lines (29 loc) · 862 Bytes
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
/* eslint-disable node-core/require-common-first, node-core/required-modules */
'use strict';
async function collectStream(readable, callback) {
let data = '';
try {
readable.setEncoding('utf8');
for await (const chunk of readable)
data += chunk;
if (typeof callback === 'function')
process.nextTick(callback, null, data);
return data;
} catch (err) {
if (typeof callback === 'function') {
process.nextTick(callback, err);
} else {
throw err;
}
}
}
function collectChildStreams(child, waitFor) {
const stdout = child.stdout && collectStream(child.stdout);
const stderr = child.stderr && collectStream(child.stderr);
return Promise.all([stdout, stderr, waitFor])
.then(([stdout, stderr, data]) => ({ stdout, stderr, data }));
}
module.exports = {
collectChildStreams,
collectStream,
};