max-buffer.js
This file
Path
lib/io/max-buffer.js
89 lines · 2.8 KiB · JavaScript
Last changed by
Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–2why, one step back1import {MaxBufferError} from 'get-stream';2import {getStreamName} from '../utils/standard-stream.js';Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
line 3why, one step back3import {getFdSpecificValue} from '../arguments/specific.js';Rename files (#977)ehmickybefore adoption4b2e316
line 4why, one step back4Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 5–6why, one step back5// When the `maxBuffer` option is hit, a MaxBufferError is thrown.6// The stream is aborted, then specific information is kept for the error message.Rename files (#977)ehmickybefore adoption4b2e316
lines 7–37why, one step back7export const handleMaxBuffer = ({error, stream, readableObjectMode, lines, encoding, fdNumber}) => {8 if (!(error instanceof MaxBufferError)) {9 throw error;10 }1112 if (fdNumber === 'all') {13 return error;14 }1516 const unit = getMaxBufferUnit(readableObjectMode, lines, encoding);17 error.maxBufferInfo = {fdNumber, unit};18 stream.destroy();19 throw error;20};2122const getMaxBufferUnit = (readableObjectMode, lines, encoding) => {23 if (readableObjectMode) {24 return 'objects';25 }2627 if (lines) {28 return 'lines';29 }3031 if (encoding === 'buffer') {32 return 'bytes';33 }3435 return 'characters';36};37Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
lines 38–40why, one step back38// Check the `maxBuffer` option with `result.ipcOutput`39export const checkIpcMaxBuffer = (subprocess, ipcOutput, maxBuffer) => {40 if (ipcOutput.length !== maxBuffer) {Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 41–48why, one step back41 return;42 }4344 const error = new MaxBufferError();45 error.maxBufferInfo = {fdNumber: 'ipc'};46 throw error;47};48Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 49why, one step back49// Error message when `maxBuffer` is hitRename files (#977)ehmickybefore adoption4b2e316
lines 50–61why, one step back50export const getMaxBufferMessage = (error, maxBuffer) => {51 const {streamName, threshold, unit} = getMaxBufferInfo(error, maxBuffer);52 return `Command's ${streamName} was larger than ${threshold} ${unit}`;53};5455const getMaxBufferInfo = (error, maxBuffer) => {56 if (error?.maxBufferInfo === undefined) {57 return {streamName: 'output', threshold: maxBuffer[1], unit: 'bytes'};58 }5960 const {maxBufferInfo: {fdNumber, unit}} = error;61 delete error.maxBufferInfo;Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 62why, one step back62Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
line 63why, one step back63 const threshold = getFdSpecificValue(maxBuffer, fdNumber);Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 64why, one step back64 if (fdNumber === 'ipc') {Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
line 65why, one step back65 return {streamName: 'IPC output', threshold, unit: 'messages'};Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 66–67why, one step back66 }67Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199
line 68why, one step back68 return {streamName: getStreamName(fdNumber), threshold, unit};Rename files (#977)ehmickybefore adoption4b2e316
lines 69–70why, one step back69};70Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 71–73why, one step back71// The only way to apply `maxBuffer` with `spawnSync()` is to use the native `maxBuffer` option Node.js provides.72// However, this has multiple limitations, and cannot behave the exact same way as the async behavior.73// When the `maxBuffer` is hit, a `ENOBUFS` error is thrown.Rename files (#977)ehmickybefore adoption4b2e316
lines 74–77why, one step back74export const isMaxBufferSync = (resultError, output, maxBuffer) => resultError?.code === 'ENOBUFS'75 && output !== null76 && output.some(result => result !== null && result.length > getMaxBufferSync(maxBuffer));77Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
lines 78–87why, one step back78// When `maxBuffer` is hit, ensure the result is truncated79export const truncateMaxBufferSync = (result, isMaxBuffer, maxBuffer) => {80 if (!isMaxBuffer) {81 return result;82 }8384 const maxBufferValue = getMaxBufferSync(maxBuffer);85 return result.length > maxBufferValue ? result.slice(0, maxBufferValue) : result;86};87Rename files (#977)ehmickybefore adoption4b2e316
line 88why, one step back88// `spawnSync()` does not allow differentiating `maxBuffer` per file descriptor, so we always use `stdout`Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
line 89why, one step back89export const getMaxBufferSync = ([, stdoutMaxBuffer]) => stdoutMaxBuffer;