diff --git a/index.js b/index.js index 9770ad8..788df02 100644 --- a/index.js +++ b/index.js @@ -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); } diff --git a/test.js b/test.js index 20a7ded..c00507e 100644 --- a/test.js +++ b/test.js @@ -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); });