Skip to content
Myr

max-buffer.js

This file

Path

lib/stream/max-buffer.js

55 lines · 1.6 KiB · JavaScript

Last changed by

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

History of this file

Take it

Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e

line 1why, one step back
1import {MaxBufferError} from 'get-stream';

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 2why, one step back
2import {getStreamName} from '../utils.js';

Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e

line 3why, one step back
3

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 4why, one step back
4export const handleMaxBuffer = ({error, stream, readableObjectMode, lines, encoding, fdNumber, isAll}) => {

Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e

line 5why, one step back
5 if (!(error instanceof MaxBufferError)) {

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 6why, one step back
6 throw error;

Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e

lines 7–8why, one step back
7 }
8

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

lines 9–12why, one step back
9 if (isAll) {
10 return error;11 }12

Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e

line 13why, one step back
13 const unit = getMaxBufferUnit(readableObjectMode, lines, encoding);

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 14why, one step back
14 error.maxBufferInfo = {fdNumber, unit};

Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e

line 15why, one step back
15 stream.destroy();

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 16why, one step back
16 throw error;

Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e

lines 17–35why, one step back
17};
1819const getMaxBufferUnit = (readableObjectMode, lines, encoding) => {20 if (readableObjectMode) {21 return 'objects';22 }2324 if (lines) {25 return 'lines';26 }2728 if (encoding === 'buffer') {29 return 'bytes';30 }3132 return 'characters';33};3435export const getMaxBufferMessage = (error, maxBuffer) => {

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

lines 36–37why, one step back
36 const {streamName, threshold, unit} = getMaxBufferInfo(error, maxBuffer);
37 return `Command's ${streamName} was larger than ${threshold} ${unit}`;

Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e

lines 38–39why, one step back
38};
39

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 40why, one step back
40const getMaxBufferInfo = (error, maxBuffer) => {

Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e

line 41why, one step back
41 if (error?.maxBufferInfo === undefined) {

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 42why, one step back
42 return {streamName: 'output', threshold: maxBuffer[1], unit: 'bytes'};

Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e

lines 43–44why, one step back
43 }
44

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 45why, one step back
45 const {maxBufferInfo: {fdNumber, unit}} = error;

Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e

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

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

line 47why, one step back
47 return {streamName: getStreamName(fdNumber), threshold: maxBuffer[fdNumber], unit};

Add `error.isMaxBuffer` (#963)ehmickybefore adoption9e6e19e

line 48why, one step back
48};

Allow setting different `maxBuffer` values for `stdout`/`stderr` (#966)ehmickybefore adoptionfdffcb6

lines 49–55why, one step back
49
50export const isMaxBufferSync = (resultError, output, maxBuffer) => resultError?.code === 'ENOBUFS'51 && output !== null52 && output.some(result => result !== null && result.length > getMaxBufferSync(maxBuffer));5354// `spawnSync()` does not allow differentiating `maxBuffer` per file descriptor, so we always use `stdout`55export const getMaxBufferSync = maxBuffer => maxBuffer[1];