streaming.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import {finished} from 'node:stream/promises';
  2. import mergeStreams from '@sindresorhus/merge-streams';
  3. import {incrementMaxListeners} from '../utils/max-listeners.js';
  4. import {pipeStreams} from '../io/pipeline.js';
  5. // The piping behavior is like Bash.
  6. // In particular, when one subprocess exits, the other is not terminated by a signal.
  7. // Instead, its stdout (for the source) or stdin (for the destination) closes.
  8. // If the subprocess uses it, it will make it error with SIGPIPE or EPIPE (for the source) or end (for the destination).
  9. // If it does not use it, it will continue running.
  10. // This allows for subprocesses to gracefully exit and lower the coupling between subprocesses.
  11. export const pipeSubprocessStream = (sourceStream, destinationStream, maxListenersController) => {
  12. const mergedStream = MERGED_STREAMS.has(destinationStream)
  13. ? pipeMoreSubprocessStream(sourceStream, destinationStream)
  14. : pipeFirstSubprocessStream(sourceStream, destinationStream);
  15. incrementMaxListeners(sourceStream, SOURCE_LISTENERS_PER_PIPE, maxListenersController.signal);
  16. incrementMaxListeners(destinationStream, DESTINATION_LISTENERS_PER_PIPE, maxListenersController.signal);
  17. cleanupMergedStreamsMap(destinationStream);
  18. return mergedStream;
  19. };
  20. // We use `merge-streams` to allow for multiple sources to pipe to the same destination.
  21. const pipeFirstSubprocessStream = (sourceStream, destinationStream) => {
  22. const mergedStream = mergeStreams([sourceStream]);
  23. pipeStreams(mergedStream, destinationStream);
  24. MERGED_STREAMS.set(destinationStream, mergedStream);
  25. return mergedStream;
  26. };
  27. const pipeMoreSubprocessStream = (sourceStream, destinationStream) => {
  28. const mergedStream = MERGED_STREAMS.get(destinationStream);
  29. mergedStream.add(sourceStream);
  30. return mergedStream;
  31. };
  32. const cleanupMergedStreamsMap = async destinationStream => {
  33. try {
  34. await finished(destinationStream, {cleanup: true, readable: false, writable: true});
  35. } catch {}
  36. MERGED_STREAMS.delete(destinationStream);
  37. };
  38. const MERGED_STREAMS = new WeakMap();
  39. // Number of listeners set up on `sourceStream` by each `sourceStream.pipe(destinationStream)`
  40. // Those are added by `merge-streams`
  41. const SOURCE_LISTENERS_PER_PIPE = 2;
  42. // Number of listeners set up on `destinationStream` by each `sourceStream.pipe(destinationStream)`
  43. // Those are added by `finished()` in `cleanupMergedStreamsMap()`
  44. const DESTINATION_LISTENERS_PER_PIPE = 1;