pipeline.js
This file
Path
lib/io/pipeline.js
53 lines · 1.5 KiB · JavaScript
Last changed by
Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 1–3why, one step back1import {finished} from 'node:stream/promises';2import {isStandardStream} from '../utils/standard-stream.js';3Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
lines 4–6why, one step back4// Similar to `Stream.pipeline(source, destination)`, but does not destroy standard streams.5// `onDestinationEnd` runs when the destination ends, before the source is aborted.6export const pipeStreams = (source, destination, onDestinationEnd) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 7–8why, one step back7 source.pipe(destination);8 onSourceFinish(source, destination);Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
line 9why, one step back9 onDestinationFinish(source, destination, onDestinationEnd);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 10–32why, one step back10};1112// `source.pipe(destination)` makes `destination` end when `source` ends.13// But it does not propagate aborts or errors. This function does it.14const onSourceFinish = async (source, destination) => {15 if (isStandardStream(source) || isStandardStream(destination)) {16 return;17 }1819 try {20 await finished(source, {cleanup: true, readable: true, writable: false});21 } catch {}2223 endDestinationStream(destination);24};2526export const endDestinationStream = destination => {27 if (destination.writable) {28 destination.end();29 }30};3132// We do the same thing in the other direction as well.Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
line 33why, one step back33const onDestinationFinish = async (source, destination, onDestinationEnd) => {100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 34–40why, one step back34 if (isStandardStream(source) || isStandardStream(destination)) {35 return;36 }3738 try {39 await finished(destination, {cleanup: true, readable: false, writable: true});40 } catch {}Fix `.pipe()` and stream conversion edge casesSindre Sorhusbefore adoptiond3ec10d
lines 41–44why, one step back4142 if (onDestinationEnd !== undefined) {43 await onDestinationEnd();44 }100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 45–53why, one step back4546 abortSourceStream(source);47};4849export const abortSourceStream = source => {50 if (source.readable) {51 source.destroy();52 }53};