max-buffer.js
This file
Path
lib/io/max-buffer.js
102 lines · 4.4 KiB · JavaScript
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
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';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 4–30why, one step back45// When the `maxBuffer` option is hit, a MaxBufferError is thrown.6// The stream is aborted, then specific information is kept for the error message.7export 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 }30TweaksSindre Sorhusbefore adoption698a1b8
line 31why, one step back31 return encoding === 'buffer' ? 'bytes' : 'characters';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 32–33why, one step back32};33Fix `maxBuffer` being ignored or wrongly appliedSindre Sorhusbefore adoption9547e51
lines 34–37why, one step back34// Check the `maxBuffer` option with `result.ipcOutput`, before buffering one more message.35// Like the stream-based `maxBuffer`, the threshold is hit when the buffered amount would exceed it, which also applies to non-integer values.36export const checkIpcMaxBuffer = (ipcOutput, maxBuffer) => {37 if (ipcOutput.length + 1 <= maxBuffer) {Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 38–45why, one step back38 return;39 }4041 const error = new MaxBufferError();42 error.maxBufferInfo = {fdNumber: 'ipc'};43 throw error;44};45100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 46–56why, one step back46// Error message when `maxBuffer` is hit47export const getMaxBufferMessage = (error, maxBuffer) => {48 const {streamName, threshold, unit} = getMaxBufferInfo(error, maxBuffer);49 return `Command's ${streamName} was larger than ${threshold} ${unit}`;50};5152const getMaxBufferInfo = (error, maxBuffer) => {53 if (error?.maxBufferInfo === undefined) {54 return {streamName: 'output', threshold: maxBuffer[1], unit: 'bytes'};55 }56Fix `maxBuffer` being ignored or wrongly appliedSindre Sorhusbefore adoption9547e51
line 57why, one step back57 const {maxBufferInfo: {fdNumber, unit, threshold = getFdSpecificValue(maxBuffer, fdNumber)}} = error;100% test coverage (#1018)ehmickybefore adoption9f65b86
line 58why, one step back58 delete error.maxBufferInfo;Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 59why, one step back59TweaksSindre Sorhusbefore adoption698a1b8
line 60why, one step back60 return fdNumber === 'ipc' ? {streamName: 'IPC output', threshold, unit: 'messages'} : {streamName: getStreamName(fdNumber), threshold, unit};100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 61–62why, one step back61};62Fix `maxBuffer` being ignored or wrongly appliedSindre Sorhusbefore adoption9547e51
lines 63–66why, one step back63// The native `maxBuffer` option passed to `spawnSync()` does not allow differentiating values per file descriptor, so we use `stdout`'s.64// Since each other file descriptor's own limit is capped by `stdout`'s, this is also the highest one.65// Node.js applies it to the total output of all piped file descriptors, not to each of them. When it is hit, the subprocess is killed and a `ENOBUFS` error is thrown.66export const getMaxBufferSync = ([, stdoutMaxBuffer]) => stdoutMaxBuffer;100% test coverage (#1018)ehmickybefore adoption9f65b86
line 67why, one step back67Fix `maxBuffer` being ignored or wrongly appliedSindre Sorhusbefore adoption9547e51
lines 68–75why, one step back68// File descriptors with a limit lower than `spawnSync()`'s native `maxBuffer` are enforced from the output, after the subprocess exits.69// Since no native error was thrown, we create the `ENOBUFS` error ourselves.70// Like with asynchronous methods, file descriptors which are not buffered are not limited by their own value. However, when they are piped, e.g. to a file, they still count towards the native limit, which is `stdout`'s value.71export const handleMaxBufferSync = ({resultError, output, maxBuffer, buffer}) => {72 const fdNumber = findMaxBufferSync(output, maxBuffer, buffer);73 if (fdNumber === undefined) {74 // The native limit can be hit by the total output without any single file descriptor being over its own limit75 return {resultError, isMaxBuffer: resultError?.code === 'ENOBUFS'};100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 76–77why, one step back76 }77Fix `maxBuffer` being ignored or wrongly appliedSindre Sorhusbefore adoption9547e51
lines 78–81why, one step back78 const threshold = getMaxBufferValueSync(maxBuffer, fdNumber);79 const error = resultError ?? Object.assign(new MaxBufferError(), {code: 'ENOBUFS'});80 error.maxBufferInfo = {fdNumber, unit: 'bytes', threshold};81 return {resultError: error, isMaxBuffer: true};100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 82–83why, one step back82};83Fix `maxBuffer` being ignored or wrongly appliedSindre Sorhusbefore adoption9547e51
lines 84–102why, one step back84const findMaxBufferSync = (output, maxBuffer, buffer) => {85 if (output === null) {86 return;87 }8889 const fdNumber = output.findIndex((result, index) => result !== null90 && buffer[index]91 && result.length > getMaxBufferValueSync(maxBuffer, index));92 return fdNumber === -1 ? undefined : fdNumber;93};9495// `spawnSync()` uses `maxBuffer.stdout` as its native `maxBuffer`, so other file descriptors' limits can only be lower than that96const getMaxBufferValueSync = (maxBuffer, fdNumber) => Math.min(maxBuffer[1], maxBuffer[fdNumber]);9798// When `maxBuffer` is hit, ensure each buffered file descriptor's result is truncated at its own limit99export const truncateMaxBufferSync = (result, fdNumber, {maxBuffer, buffer}) => {100 const maxBufferValue = getMaxBufferValueSync(maxBuffer, fdNumber);101 return buffer[fdNumber] && result.length > maxBufferValue ? result.slice(0, maxBufferValue) : result;102};