max-buffer.js
This file
Path
lib/io/max-buffer.js
71 lines · 2.4 KiB · JavaScript
Last changed by
Add more code comments (#984)ehmickybefore adoptionfdd8a9f
History of this fileTake it
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–3why, one step back1import {MaxBufferError} from 'get-stream';2import {getStreamName} from '../utils/standard-stream.js';3Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 4–5why, one step back4// When the `maxBuffer` option is hit, a MaxBufferError is thrown.5// The stream is aborted, then specific information is kept for the error message.Rename files (#977)ehmickybefore adoption4b2e316
lines 6–36why, one step back6export const handleMaxBuffer = ({error, stream, readableObjectMode, lines, encoding, fdNumber}) => {7 if (!(error instanceof MaxBufferError)) {8 throw error;9 }1011 if (fdNumber === 'all') {12 return error;13 }1415 const unit = getMaxBufferUnit(readableObjectMode, lines, encoding);16 error.maxBufferInfo = {fdNumber, unit};17 stream.destroy();18 throw error;19};2021const getMaxBufferUnit = (readableObjectMode, lines, encoding) => {22 if (readableObjectMode) {23 return 'objects';24 }2526 if (lines) {27 return 'lines';28 }2930 if (encoding === 'buffer') {31 return 'bytes';32 }3334 return 'characters';35};36Add more code comments (#984)ehmickybefore adoptionfdd8a9f
line 37why, one step back37// Error message when `maxBuffer` is hitRename files (#977)ehmickybefore adoption4b2e316
lines 38–52why, one step back38export const getMaxBufferMessage = (error, maxBuffer) => {39 const {streamName, threshold, unit} = getMaxBufferInfo(error, maxBuffer);40 return `Command's ${streamName} was larger than ${threshold} ${unit}`;41};4243const getMaxBufferInfo = (error, maxBuffer) => {44 if (error?.maxBufferInfo === undefined) {45 return {streamName: 'output', threshold: maxBuffer[1], unit: 'bytes'};46 }4748 const {maxBufferInfo: {fdNumber, unit}} = error;49 delete error.maxBufferInfo;50 return {streamName: getStreamName(fdNumber), threshold: maxBuffer[fdNumber], unit};51};52Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 53–55why, one step back53// The only way to apply `maxBuffer` with `spawnSync()` is to use the native `maxBuffer` option Node.js provides.54// However, this has multiple limitations, and cannot behave the exact same way as the async behavior.55// When the `maxBuffer` is hit, a `ENOBUFS` error is thrown.Rename files (#977)ehmickybefore adoption4b2e316
lines 56–59why, one step back56export const isMaxBufferSync = (resultError, output, maxBuffer) => resultError?.code === 'ENOBUFS'57 && output !== null58 && output.some(result => result !== null && result.length > getMaxBufferSync(maxBuffer));59Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
lines 60–69why, one step back60// When `maxBuffer` is hit, ensure the result is truncated61export const truncateMaxBufferSync = (result, isMaxBuffer, maxBuffer) => {62 if (!isMaxBuffer) {63 return result;64 }6566 const maxBufferValue = getMaxBufferSync(maxBuffer);67 return result.length > maxBufferValue ? result.slice(0, maxBufferValue) : result;68};69Rename files (#977)ehmickybefore adoption4b2e316
line 70why, one step back70// `spawnSync()` does not allow differentiating `maxBuffer` per file descriptor, so we always use `stdout`Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478
line 71why, one step back71export const getMaxBufferSync = ([, stdoutMaxBuffer]) => stdoutMaxBuffer;