Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate error.bufferedData if too large #68

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,35 @@ const getStreamContents = async (stream, {convertChunk, getContents}, {maxBuffer

return getContents(chunks, length);
} catch (error) {
try {
error.bufferedData = getContents(chunks, length);
// This throws when the buffered data is larger than the maximum length
// for a string or buffer
} catch {}

error.bufferedData = getBufferedData(chunks, getContents, length);
throw error;
}
};

const getBufferedData = (chunks, getContents, length) => {
try {
return getContents(chunks, length);
} catch {
return truncateBufferedValue(chunks, getContents);
}
};

// If the input is larger than the maximum length for a string or a buffer,
// it will fail. We retry it with increasingly smaller inputs, so that
// `error.bufferedData` is still set, albeit with a truncated value, since that
// is still useful for debugging.
const truncateBufferedValue = (chunks, getContents) => {
let chunksCount = chunks.length;
do {
chunksCount = Math.floor(chunksCount / SPLIT_FACTOR);
try {
return getContents(chunks.slice(0, chunksCount));
} catch {}
} while (chunksCount > 0);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chunksCount > 0 should never be false, since when chunksCount = 0 is called, chunks.slice(0, 0) will be an empty string/buffer, which is guaranteed not to throw.
So, one could use while (true) instead. I have used chunksCount > 0 instead to avoid some linting warning, and as an additional safety net to avoid infinite loops, just in case.

};

const SPLIT_FACTOR = 2;

const convertChunkToBuffer = chunk => Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);

const getContentsAsBuffer = (chunks, length) => Buffer.concat(chunks, length);
Expand Down
6 changes: 4 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ test.serial('handles streams larger than buffer max length', async t => {
const chunkCount = Math.floor(BufferConstants.MAX_LENGTH / chunkSize * 2);
const chunk = Buffer.alloc(chunkSize);
const chunks = Array.from({length: chunkCount}, () => chunk);
await t.throwsAsync(setupBuffer(chunks));
const {bufferedData} = await t.throwsAsync(setupBuffer(chunks));
t.is(bufferedData[0], 0);
});

test.serial('handles streams larger than string max length', async t => {
Expand All @@ -87,7 +88,8 @@ test.serial('handles streams larger than string max length', async t => {
const chunkCount = Math.floor(BufferConstants.MAX_STRING_LENGTH / chunkSize * 2);
const chunk = '.'.repeat(chunkSize);
const chunks = Array.from({length: chunkCount}, () => chunk);
await t.throwsAsync(setup(chunks));
const {bufferedData} = await t.throwsAsync(setup(chunks));
t.is(bufferedData[0], '.');
});

// Tests related to big buffers/strings can be slow. We run them serially and
Expand Down