Skip to content
Myr

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 file

Take it

Rename files (#977)ehmickybefore adoption4b2e316

lines 1–2why, one step back
1import {MaxBufferError} from 'get-stream';
2import {getStreamName} from '../utils/standard-stream.js';

Refactor `verbose` logic (#1126)ehmickybefore adoptionf9f1199

line 3why, one step back
3import {getFdSpecificValue} from '../arguments/specific.js';

Rename files (#977)ehmickybefore adoption4b2e316

line 4why, one step back
4

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

lines 5–6why, one step back
5// 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–30why, one step back
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 }30

TweaksSindre Sorhusbefore adoption698a1b8

line 31why, one step back
31 return encoding === 'buffer' ? 'bytes' : 'characters';

Rename files (#977)ehmickybefore adoption4b2e316

lines 32–33why, one step back
32};
33

Fix `maxBuffer` being ignored or wrongly appliedSindre Sorhusbefore adoption9547e51

lines 34–37why, one step back
34// 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 back
38 return;
39 }4041 const error = new MaxBufferError();42 error.maxBufferInfo = {fdNumber: 'ipc'};43 throw error;44};45

Add more code comments (#984)ehmickybefore adoptionfdd8a9f

line 46why, one step back
46// Error message when `maxBuffer` is hit

Rename files (#977)ehmickybefore adoption4b2e316

lines 47–56why, one step back
47export 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 }56

Fix `maxBuffer` being ignored or wrongly appliedSindre Sorhusbefore adoption9547e51

line 57why, one step back
57 const {maxBufferInfo: {fdNumber, unit, threshold = getFdSpecificValue(maxBuffer, fdNumber)}} = error;

Rename files (#977)ehmickybefore adoption4b2e316

line 58why, one step back
58 delete error.maxBufferInfo;

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 59why, one step back
59

TweaksSindre Sorhusbefore adoption698a1b8

line 60why, one step back
60 return fdNumber === 'ipc' ? {streamName: 'IPC output', threshold, unit: 'messages'} : {streamName: getStreamName(fdNumber), threshold, unit};

Rename files (#977)ehmickybefore adoption4b2e316

lines 61–62why, one step back
61};
62

Fix `maxBuffer` being ignored or wrongly appliedSindre Sorhusbefore adoption9547e51

lines 63–66why, one step back
63// 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;

Rename files (#977)ehmickybefore adoption4b2e316

line 67why, one step back
67

Fix `maxBuffer` being ignored or wrongly appliedSindre Sorhusbefore adoption9547e51

lines 68–75why, one step back
68// 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'};

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

lines 76–77why, one step back
76 }
77

Fix `maxBuffer` being ignored or wrongly appliedSindre Sorhusbefore adoption9547e51

lines 78–81why, one step back
78 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};

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

lines 82–83why, one step back
82};
83

Fix `maxBuffer` being ignored or wrongly appliedSindre Sorhusbefore adoption9547e51

lines 84–102why, one step back
84const 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};