MCPcopy Index your code
hub / github.com/nodejs/node / pipe

Function pipe

lib/internal/streams/pipeline.js:417–470  ·  view source on GitHub ↗
(src, dst, finish, finishOnlyHandleError, { end })

Source from the content-addressed store, hash-verified

415}
416
417function pipe(src, dst, finish, finishOnlyHandleError, { end }) {
418 let ended = false;
419 dst.on('close', () => {
420 if (!ended) {
421 // Finish if the destination closes before the source has completed.
422 finishOnlyHandleError(new ERR_STREAM_PREMATURE_CLOSE());
423 }
424 });
425
426 src.pipe(dst, { end: false }); // If end is true we already will have a listener to end dst.
427
428 if (end) {
429 // Compat. Before node v10.12.0 stdio used to throw an error so
430 // pipe() did/does not end() stdio destinations.
431 // Now they allow it but "secretly" don't close the underlying fd.
432
433 function endFn() {
434 ended = true;
435 dst.end();
436 }
437
438 if (isReadableFinished(src)) { // End the destination if the source has already ended.
439 process.nextTick(endFn);
440 } else {
441 src.once('end', endFn);
442 }
443 } else {
444 finish();
445 }
446
447 eos(src, { readable: true, writable: false }, (err) => {
448 const rState = src._readableState;
449 if (
450 err &&
451 err.code === 'ERR_STREAM_PREMATURE_CLOSE' &&
452 (rState?.ended && !rState.errored && !rState.errorEmitted)
453 ) {
454 // Some readable streams will emit 'close' before 'end'. However, since
455 // this is on the readable side 'end' should still be emitted if the
456 // stream has been ended and no error emitted. This should be allowed in
457 // favor of backwards compatibility. Since the stream is piped to a
458 // destination this should not result in any observable difference.
459 // We don't need to check if this is a writable premature close since
460 // eos will only fail with premature close on the reading side for
461 // duplex streams.
462 src
463 .once('end', finish)
464 .once('error', finish);
465 } else {
466 finish(err);
467 }
468 });
469 return eos(dst, { readable: false, writable: true }, finish);
470}
471
472module.exports = { pipelineImpl, pipeline };

Callers 1

pipelineImplFunction · 0.70

Calls 6

finishOnlyHandleErrorFunction · 0.85
isReadableFinishedFunction · 0.85
eosFunction · 0.85
finishFunction · 0.70
onMethod · 0.45
onceMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…