Increase size of tarball upload (#20767)

- check size while reading stream instead of checking after reading all
stream
- move MAX_TARBALL_UPLOAD_SIZE_BYTES to config variables
- increase MAX_TARBALL_UPLOAD_SIZE_BYTES default from 50Mb to 100Mb
This commit is contained in:
martmull
2026-05-20 14:39:11 +02:00
committed by GitHub
parent 3d49c17e34
commit 127fb2a470
5 changed files with 101 additions and 23 deletions
@@ -76,4 +76,45 @@ describe('streamToBuffer', () => {
await expect(promise).rejects.toThrow('Stream closed before end');
});
});
describe('maxSizeBytes', () => {
it('should accept stream within size limit', async () => {
const data = 'Hello, World!';
const stream = Readable.from([Buffer.from(data)]);
const result = await streamToBuffer(stream, 100);
expect(result.toString()).toBe(data);
});
it('should reject when stream exceeds maxSizeBytes', async () => {
const stream = new PassThrough();
const promise = streamToBuffer(stream, 10);
stream.write(Buffer.from('12345'));
stream.write(Buffer.from('678901'));
await expect(promise).rejects.toThrow(
'Stream exceeds maximum allowed size of 10 bytes',
);
});
it('should reject on a single chunk exceeding maxSizeBytes', async () => {
const stream = Readable.from([Buffer.from('this is too long')]);
await expect(streamToBuffer(stream, 5)).rejects.toThrow(
'Stream exceeds maximum allowed size of 5 bytes',
);
});
it('should accept stream exactly at maxSizeBytes', async () => {
const data = '12345';
const stream = Readable.from([Buffer.from(data)]);
const result = await streamToBuffer(stream, 5);
expect(result.toString()).toBe(data);
});
});
});
@@ -1,7 +1,11 @@
import { type Readable } from 'stream';
export const streamToBuffer = async (stream: Readable): Promise<Buffer> => {
export const streamToBuffer = async (
stream: Readable,
maxSizeBytes?: number,
): Promise<Buffer> => {
const chunks: Buffer[] = [];
let totalSize = 0;
return new Promise((resolve, reject) => {
if (stream.readableEnded) {
@@ -27,6 +31,21 @@ export const streamToBuffer = async (stream: Readable): Promise<Buffer> => {
const onData = (chunk: Buffer) => {
if (!isResolved) {
totalSize += chunk.length;
if (maxSizeBytes !== undefined && totalSize > maxSizeBytes) {
isResolved = true;
cleanup();
stream.destroy();
reject(
new Error(
`Stream exceeds maximum allowed size of ${maxSizeBytes} bytes`,
),
);
return;
}
chunks.push(chunk);
}
};