all-async.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import mergeStreams from '@sindresorhus/merge-streams';
  2. import {waitForSubprocessStream} from './stdio.js';
  3. // `all` interleaves `stdout` and `stderr`
  4. export const makeAllStream = ({stdout, stderr}, {all}) => all && (stdout || stderr)
  5. ? mergeStreams([stdout, stderr].filter(Boolean))
  6. : undefined;
  7. // Read the contents of `subprocess.all` and|or wait for its completion
  8. export const waitForAllStream = ({subprocess, encoding, buffer, maxBuffer, lines, stripFinalNewline, verboseInfo, streamInfo}) => waitForSubprocessStream({
  9. ...getAllStream(subprocess, buffer),
  10. fdNumber: 'all',
  11. encoding,
  12. maxBuffer: maxBuffer[1] + maxBuffer[2],
  13. lines: lines[1] || lines[2],
  14. allMixed: getAllMixed(subprocess),
  15. stripFinalNewline,
  16. verboseInfo,
  17. streamInfo,
  18. });
  19. const getAllStream = ({stdout, stderr, all}, [, bufferStdout, bufferStderr]) => {
  20. const buffer = bufferStdout || bufferStderr;
  21. if (!buffer) {
  22. return {stream: all, buffer};
  23. }
  24. if (!bufferStdout) {
  25. return {stream: stderr, buffer};
  26. }
  27. if (!bufferStderr) {
  28. return {stream: stdout, buffer};
  29. }
  30. return {stream: all, buffer};
  31. };
  32. // When `subprocess.stdout` is in objectMode but not `subprocess.stderr` (or the opposite), we need to use both:
  33. // - `getStreamAsArray()` for the chunks in objectMode, to return as an array without changing each chunk
  34. // - `getStreamAsArrayBuffer()` or `getStream()` for the chunks not in objectMode, to convert them from Buffers to string or Uint8Array
  35. // We do this by emulating the Buffer -> string|Uint8Array conversion performed by `get-stream` with our own, which is identical.
  36. const getAllMixed = ({all, stdout, stderr}) => all
  37. && stdout
  38. && stderr
  39. && stdout.readableObjectMode !== stderr.readableObjectMode;