direction.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import process from 'node:process';
  2. import {
  3. isStream as isNodeStream,
  4. isReadableStream as isNodeReadableStream,
  5. isWritableStream as isNodeWritableStream,
  6. } from 'is-stream';
  7. import {isWritableStream} from './type.js';
  8. // For `stdio[fdNumber]` beyond stdin/stdout/stderr, we need to guess whether the value passed is intended for inputs or outputs.
  9. // This allows us to know whether to pipe _into_ or _from_ the stream.
  10. // When `stdio[fdNumber]` is a single value, this guess is fairly straightforward.
  11. // However, when it is an array instead, we also need to make sure the different values are not incompatible with each other.
  12. export const getStreamDirection = (stdioItems, fdNumber, optionName) => {
  13. const directions = stdioItems.map(stdioItem => getStdioItemDirection(stdioItem, fdNumber));
  14. if (directions.includes('input') && directions.includes('output')) {
  15. throw new TypeError(`The \`${optionName}\` option must not be an array of both readable and writable values.`);
  16. }
  17. return directions.find(Boolean) ?? DEFAULT_DIRECTION;
  18. };
  19. const getStdioItemDirection = ({type, value}, fdNumber) => KNOWN_DIRECTIONS[fdNumber] ?? guessStreamDirection[type](value);
  20. // `stdin`/`stdout`/`stderr` have a known direction
  21. const KNOWN_DIRECTIONS = ['input', 'output', 'output'];
  22. const anyDirection = () => undefined;
  23. const alwaysInput = () => 'input';
  24. // `string` can only be added through the `input` option, i.e. does not need to be handled here
  25. const guessStreamDirection = {
  26. generator: anyDirection,
  27. asyncGenerator: anyDirection,
  28. fileUrl: anyDirection,
  29. filePath: anyDirection,
  30. iterable: alwaysInput,
  31. asyncIterable: alwaysInput,
  32. uint8Array: alwaysInput,
  33. webStream: value => isWritableStream(value) ? 'output' : 'input',
  34. nodeStream(value) {
  35. if (!isNodeReadableStream(value, {checkOpen: false})) {
  36. return 'output';
  37. }
  38. return isNodeWritableStream(value, {checkOpen: false}) ? undefined : 'input';
  39. },
  40. webTransform: anyDirection,
  41. duplex: anyDirection,
  42. native(value) {
  43. const standardStreamDirection = getStandardStreamDirection(value);
  44. if (standardStreamDirection !== undefined) {
  45. return standardStreamDirection;
  46. }
  47. if (isNodeStream(value, {checkOpen: false})) {
  48. return guessStreamDirection.nodeStream(value);
  49. }
  50. },
  51. };
  52. const getStandardStreamDirection = value => {
  53. if ([0, process.stdin].includes(value)) {
  54. return 'input';
  55. }
  56. if ([1, 2, process.stdout, process.stderr].includes(value)) {
  57. return 'output';
  58. }
  59. };
  60. // When ambiguous, we initially keep the direction as `undefined`.
  61. // This allows arrays of `stdio` values to resolve the ambiguity.
  62. // For example, `stdio[3]: DuplexStream` is ambiguous, but `stdio[3]: [DuplexStream, WritableStream]` is not.
  63. // When the ambiguity remains, we default to `output` since it is the most common use case for additional file descriptors.
  64. const DEFAULT_DIRECTION = 'output';