Skip to content

Commit

Permalink
Do not count length when maxBuffer is Infinity
Browse files Browse the repository at this point in the history
This reflects the changes in nodejs/node#43822
  • Loading branch information
fasttime committed Sep 13, 2022
1 parent 31289a2 commit fb90dda
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
41 changes: 22 additions & 19 deletions lib/foreground-exec-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,26 +109,29 @@ function execFileWithStdIO
{
if (encoding)
stream.setEncoding(encoding);
stream.on
(
'data',
chunk =>
const listener =
maxBuffer === Infinity ?
chunk =>
{
chunks.push(chunk);
} :
chunk =>
{
const encoding = stream.readableEncoding;
const length = encoding ? Buffer.byteLength(chunk, encoding) : chunk.length;
const available = maxBuffer - chunks.byteLength;
if (length > available)
{
const encoding = stream.readableEncoding;
const length = encoding ? Buffer.byteLength(chunk, encoding) : chunk.length;
const available = maxBuffer - chunks.byteLength;
if (length > available)
{
chunks.push(chunk.slice(0, available));
error = new RangeError(`${name}, maxBuffer length exceeded`);
error.code = 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER';
kill();
}
else
chunks.push(chunk);
chunks.byteLength += length;
},
);
chunks.push(chunk.slice(0, available));
error = new RangeError(`${name}, maxBuffer length exceeded`);
error.code = 'ERR_CHILD_PROCESS_STDIO_MAXBUFFER';
kill();
}
else
chunks.push(chunk);
chunks.byteLength += length;
};
stream.on('data', listener);
}
return chunks;
}
Expand Down
30 changes: 30 additions & 0 deletions test/exec.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,21 @@ describe
() => testMaxBuffer('stdout', 'ascii', 'l\'archiviazione C'),
);

it
(
'on stdout with value `Infinity`',
async () =>
{
const { stdout } =
await c8js.exec
(
joinPath('test/fixtures/stdout-max-buffer-test.js'),
{ maxBuffer: Infinity, silent: true, tempDirectory },
);
assert.equal(stdout, 'l\'archiviazione è riuscita\n');
},
);

it
(
'on stderr with `buffer` encoding',
Expand All @@ -272,6 +287,21 @@ describe
'on stderr with `ascii` encoding',
() => testMaxBuffer('stderr', 'ascii', 'es liegt eine StC'),
);

it
(
'on stderr with value `Infinity`',
async () =>
{
const { stderr } =
await c8js.exec
(
joinPath('test/fixtures/stderr-max-buffer-test.js'),
{ maxBuffer: Infinity, silent: true, tempDirectory },
);
assert.equal(stderr, 'es liegt eine Störung vor\n');
},
);
},
);

Expand Down

0 comments on commit fb90dda

Please sign in to comment.