Skip to content

Commit d9d992a

Browse files
committed
fixup: end stdio
1 parent e9e4444 commit d9d992a

1 file changed

Lines changed: 15 additions & 31 deletions

File tree

lib/internal/streams/pipeline.js

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
const {
77
ArrayIsArray,
88
SymbolAsyncIterator,
9-
SymbolIterator,
10-
Promise
9+
SymbolIterator
1110
} = primordials;
1211

1312
let eos;
@@ -114,10 +113,6 @@ function isStream(obj) {
114113
return isReadable(obj) || isWritable(obj);
115114
}
116115

117-
function isStdio(obj) {
118-
return obj === process.stdout || obj === process.stderr;
119-
}
120-
121116
function isIterable(obj, isAsync) {
122117
if (!obj) return false;
123118
if (isAsync === true) return typeof obj[SymbolAsyncIterator] === 'function';
@@ -151,32 +146,14 @@ async function pump(iterable, writable, finish) {
151146
EE = require('events');
152147
}
153148
let error;
154-
writable.on('error', (err) => {
155-
error = err;
156-
});
157149
try {
158-
let prev;
159-
for await (const next of iterable) {
160-
if (prev != null) {
161-
if (!writable.write(prev)) {
162-
if (writable.destroyed) return;
163-
await EE.once(writable, 'drain');
164-
}
150+
for await (const chunk of iterable) {
151+
if (!writable.write(chunk)) {
152+
if (writable.destroyed) return;
153+
await EE.once(writable, 'drain');
165154
}
166-
prev = next;
167-
}
168-
169-
if (prev != null) {
170-
await new Promise((resolve, reject) => {
171-
writable.write(prev, (err) => {
172-
err ? reject(err) : resolve();
173-
});
174-
});
175-
}
176-
177-
if (!isStdio(writable)) {
178-
writable.end();
179155
}
156+
writable.end();
180157
} catch (err) {
181158
error = err;
182159
} finally {
@@ -225,7 +202,7 @@ function pipeline(...streams) {
225202
const reading = i < streams.length - 1;
226203
const writing = i > 0;
227204

228-
if (isStream(stream) && !isStdio(stream)) {
205+
if (isStream(stream)) {
229206
finishCount++;
230207
destroys.push(destroyer(stream, reading, writing, !reading, finish));
231208
}
@@ -286,8 +263,15 @@ function pipeline(...streams) {
286263
destroys.push(destroyer(ret, false, true, true, finish));
287264
}
288265
} else if (isStream(stream)) {
289-
if (isReadable(ret) && !isStdio(stream)) {
266+
if (isReadable(ret)) {
290267
ret.pipe(stream);
268+
269+
// Compat. Before node v10.12.0 stdio used to throw an error so
270+
// pipe() did/does not end() stdio destinations.
271+
// Now they allow it but "secretly" don't close the underlying fd.
272+
if (stream === process.stdout || stream === process.stderr) {
273+
ret.on('end', () => stream.end());
274+
}
291275
} else {
292276
ret = makeAsyncIterable(ret);
293277

0 commit comments

Comments
 (0)