max-buffer.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import {MaxBufferError} from 'get-stream';
  2. import {getStreamName} from '../utils/standard-stream.js';
  3. import {getFdSpecificValue} from '../arguments/specific.js';
  4. // When the `maxBuffer` option is hit, a MaxBufferError is thrown.
  5. // The stream is aborted, then specific information is kept for the error message.
  6. export const handleMaxBuffer = ({error, stream, readableObjectMode, lines, encoding, fdNumber}) => {
  7. if (!(error instanceof MaxBufferError)) {
  8. throw error;
  9. }
  10. if (fdNumber === 'all') {
  11. return error;
  12. }
  13. const unit = getMaxBufferUnit(readableObjectMode, lines, encoding);
  14. error.maxBufferInfo = {fdNumber, unit};
  15. stream.destroy();
  16. throw error;
  17. };
  18. const getMaxBufferUnit = (readableObjectMode, lines, encoding) => {
  19. if (readableObjectMode) {
  20. return 'objects';
  21. }
  22. if (lines) {
  23. return 'lines';
  24. }
  25. if (encoding === 'buffer') {
  26. return 'bytes';
  27. }
  28. return 'characters';
  29. };
  30. // Check the `maxBuffer` option with `result.ipcOutput`
  31. export const checkIpcMaxBuffer = (subprocess, ipcOutput, maxBuffer) => {
  32. if (ipcOutput.length !== maxBuffer) {
  33. return;
  34. }
  35. const error = new MaxBufferError();
  36. error.maxBufferInfo = {fdNumber: 'ipc'};
  37. throw error;
  38. };
  39. // Error message when `maxBuffer` is hit
  40. export const getMaxBufferMessage = (error, maxBuffer) => {
  41. const {streamName, threshold, unit} = getMaxBufferInfo(error, maxBuffer);
  42. return `Command's ${streamName} was larger than ${threshold} ${unit}`;
  43. };
  44. const getMaxBufferInfo = (error, maxBuffer) => {
  45. if (error?.maxBufferInfo === undefined) {
  46. return {streamName: 'output', threshold: maxBuffer[1], unit: 'bytes'};
  47. }
  48. const {maxBufferInfo: {fdNumber, unit}} = error;
  49. delete error.maxBufferInfo;
  50. const threshold = getFdSpecificValue(maxBuffer, fdNumber);
  51. if (fdNumber === 'ipc') {
  52. return {streamName: 'IPC output', threshold, unit: 'messages'};
  53. }
  54. return {streamName: getStreamName(fdNumber), threshold, unit};
  55. };
  56. // The only way to apply `maxBuffer` with `spawnSync()` is to use the native `maxBuffer` option Node.js provides.
  57. // However, this has multiple limitations, and cannot behave the exact same way as the async behavior.
  58. // When the `maxBuffer` is hit, a `ENOBUFS` error is thrown.
  59. export const isMaxBufferSync = (resultError, output, maxBuffer) => resultError?.code === 'ENOBUFS'
  60. && output !== null
  61. && output.some(result => result !== null && result.length > getMaxBufferSync(maxBuffer));
  62. // When `maxBuffer` is hit, ensure the result is truncated
  63. export const truncateMaxBufferSync = (result, isMaxBuffer, maxBuffer) => {
  64. if (!isMaxBuffer) {
  65. return result;
  66. }
  67. const maxBufferValue = getMaxBufferSync(maxBuffer);
  68. return result.length > maxBufferValue ? result.slice(0, maxBufferValue) : result;
  69. };
  70. // `spawnSync()` does not allow differentiating `maxBuffer` per file descriptor, so we always use `stdout`
  71. export const getMaxBufferSync = ([, stdoutMaxBuffer]) => stdoutMaxBuffer;