Skip to content

Commit a9bcd35

Browse files
repsac-bybenmccannRich-Harris
authored
Allow streaming with body_size_limit (#6702)
* Allow streaming with body_size_limit * More detailed error message * fix formatting * Clarify error message * Update packages/kit/src/exports/node/index.js * Create fair-rivers-repair.md Co-authored-by: Ben McCann <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent c24894b commit a9bcd35

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

.changeset/fair-rivers-repair.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/adapter-node": patch
3+
---
4+
5+
Allow streaming when `BODY_SIZE_LIMIT` is set

packages/adapter-node/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Instead, we read from the _right_, accounting for the number of trusted proxies.
7171
7272
### `BODY_SIZE_LIMIT`
7373

74-
The maximum request body size to accept in bytes. Defaults to 512kb. This option does not allow streaming. You can disable this option with a value of 0 and implement a custom check in [`handle`](https://kit.svelte.dev/docs/hooks#server-hooks-handle) if you need something more advanced.
74+
The maximum request body size to accept in bytes including while streaming. Defaults to 512kb. You can disable this option with a value of 0 and implement a custom check in [`handle`](https://kit.svelte.dev/docs/hooks#server-hooks-handle) if you need something more advanced.
7575

7676
## Options
7777

packages/kit/src/exports/node/index.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,22 @@ function get_raw_body(req, body_size_limit) {
1111
return null;
1212
}
1313

14-
const length = Number(h['content-length']);
14+
const content_length = Number(h['content-length']);
1515

1616
// check if no request body
1717
if (
18-
(req.httpVersionMajor === 1 && isNaN(length) && h['transfer-encoding'] == null) ||
19-
length === 0
18+
(req.httpVersionMajor === 1 && isNaN(content_length) && h['transfer-encoding'] == null) ||
19+
content_length === 0
2020
) {
2121
return null;
2222
}
2323

24+
let length = content_length;
25+
2426
if (body_size_limit) {
2527
if (!length) {
26-
throw new Error(
27-
`Received content-length of ${length}. content-length must be provided when body size limit is specified.`
28-
);
29-
}
30-
if (length > body_size_limit) {
28+
length = body_size_limit;
29+
} else if (length > body_size_limit) {
3130
throw new Error(
3231
`Received content-length of ${length}, but only accept up to ${body_size_limit} bytes.`
3332
);
@@ -59,7 +58,13 @@ function get_raw_body(req, body_size_limit) {
5958

6059
size += chunk.length;
6160
if (size > length) {
62-
controller.error(new Error('content-length exceeded'));
61+
req.destroy(
62+
new Error(
63+
`request body size exceeded ${
64+
content_length ? "'content-length'" : 'BODY_SIZE_LIMIT'
65+
} of ${length}`
66+
)
67+
);
6368
return;
6469
}
6570

0 commit comments

Comments
 (0)