Skip to content

Commit

Permalink
Fix wrong length computation
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Aug 10, 2023
1 parent 558b93a commit 35327d0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ const getStreamContents = async (stream, {convertChunk, getSize, getContents}, {
for await (const chunk of stream) {
const chunkType = getChunkType(chunk);
const convertedChunk = convertChunk[chunkType](chunk, textDecoder);
length += getSize(convertedChunk);
const chunkSize = getSize(convertedChunk);

if (length > maxBuffer) {
if (length + chunkSize > maxBuffer) {
throw new MaxBufferError();
}

length += chunkSize;
chunks.push(convertedChunk);
}

Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ test('maxBuffer unit is each array element with getStreamAsArray()', checkMaxBuf

test('set error.bufferedData when `maxBuffer` is hit', async t => {
const maxBuffer = fixtureLength - 1;
const {bufferedData} = await t.throwsAsync(setup([...fixtureString], {maxBuffer}), {instanceOf: MaxBufferError});
t.true(fixtureString.startsWith(bufferedData));
const {bufferedData} = await t.throwsAsync(setupBuffer([...fixtureString], {maxBuffer}), {instanceOf: MaxBufferError});
t.true(fixtureString.startsWith(bufferedData.toString()));
t.true(bufferedData.length <= maxBuffer);
});

Expand Down

0 comments on commit 35327d0

Please sign in to comment.