Skip to content

Commit 7b55c40

Browse files
authored
Increase the upload limit to 150 MB and update the error message in logs (#443)
2 parents fd9fd19 + 0d00689 commit 7b55c40

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

src/routes/publish.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
import { encode as toBase32 } from '../utils/base32';
2525
import { PayloadTooLargeError } from '../utils/errors';
2626

27-
const MAX_BODY_LENGTH = 50 * 1024 * 1024; // 50MB
27+
const MAX_BODY_LENGTH = 150 * 1024 * 1024; // 150MB
2828

2929
const randomBytes = promisify(crypto.randomBytes);
3030

@@ -55,9 +55,9 @@ export function publishRoutes() {
5555
if (expectedLength !== undefined && expectedLength > MAX_BODY_LENGTH) {
5656
log.verbose(
5757
'length-header-check',
58-
'The length specified in the header Content-Length is too big.'
58+
`The length specified in the header Content-Length (${expectedLength}) is too big.`
5959
);
60-
throw new PayloadTooLargeError(MAX_BODY_LENGTH);
60+
throw new PayloadTooLargeError(MAX_BODY_LENGTH, expectedLength);
6161
}
6262

6363
const profileToken = await generateTokenForProfile();

src/utils/errors.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ export class PayloadTooLargeError extends Error {
2727
status = 413;
2828
expose = true;
2929

30-
constructor(maxPayloadSize: number) {
31-
super(
32-
`The length is bigger than the configured maximum ${maxPayloadSize}.`
33-
);
30+
constructor(maxPayloadSize: number, receivedPayloadSize?: number) {
31+
const message = receivedPayloadSize
32+
? `The received length ${receivedPayloadSize} is bigger than the configured maximum ${maxPayloadSize}.`
33+
: `The length is bigger than the configured maximum ${maxPayloadSize}.`;
34+
super(message);
3435
}
3536
}

test/api/publish.test.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,28 @@ describe('publishing endpoints', () => {
109109
expectBucketHasProfile(profileToken);
110110
});
111111

112+
it('also works for length between 50MB and 150MB', async () => {
113+
const payload = gzipSync(`{"foo": "${'#'.repeat(55 * 1024 * 1024)}"}`, {
114+
level: 0,
115+
});
116+
const req = getPreconfiguredRequest();
117+
await req
118+
.set('Content-Length', String(payload.length))
119+
.send(payload)
120+
.expect(200);
121+
});
122+
112123
it('returns an error when the length header is too big', async () => {
113124
jest
114125
.spyOn(process.stdout, 'write')
115126
.mockImplementation((_: string | Uint8Array) => true);
116127
const req = getPreconfiguredRequest();
117128
await req
118129
.set('Content-Length', String(1024 * 1024 * 1024))
119-
.expect(413, /The length is bigger than the configured maximum/);
130+
.expect(
131+
413,
132+
/The received length \d+ is bigger than the configured maximum/
133+
);
120134
expect(process.stdout.write).toHaveBeenCalledWith(
121135
expect.stringContaining('server_error')
122136
);
@@ -131,8 +145,8 @@ describe('publishing endpoints', () => {
131145
// When using the low-level API "write", Node generates a "chunked encoding"
132146
// request without a Content-Length. This is exactly what we want to check
133147
// here.
134-
// We generate a Buffer of ~51MB, but our limit is 50MB.
135-
const payload = gzipSync(`{"foo": "${'#'.repeat(51 * 1024 * 1024)}"}`, {
148+
// We generate a Buffer of ~151MB, but our limit is 150MB.
149+
const payload = gzipSync(`{"foo": "${'#'.repeat(151 * 1024 * 1024)}"}`, {
136150
level: 0,
137151
});
138152
await req.write(payload);

0 commit comments

Comments
 (0)