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

Allow streaming with body_size_limit #6702

Merged
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
5 changes: 5 additions & 0 deletions .changeset/fair-rivers-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/adapter-node": patch
---

Allow streaming when `BODY_SIZE_LIMIT` is set
2 changes: 1 addition & 1 deletion packages/adapter-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Instead, we read from the _right_, accounting for the number of trusted proxies.

### `BODY_SIZE_LIMIT`

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.
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.

## Options

Expand Down
23 changes: 14 additions & 9 deletions packages/kit/src/exports/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@ function get_raw_body(req, body_size_limit) {
return null;
}

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

// check if no request body
if (
(req.httpVersionMajor === 1 && isNaN(length) && h['transfer-encoding'] == null) ||
length === 0
(req.httpVersionMajor === 1 && isNaN(content_length) && h['transfer-encoding'] == null) ||
content_length === 0
) {
return null;
}

let length = content_length;

if (body_size_limit) {
if (!length) {
throw new Error(
`Received content-length of ${length}. content-length must be provided when body size limit is specified.`
);
}
if (length > body_size_limit) {
length = body_size_limit;
} else if (length > body_size_limit) {
throw new Error(
`Received content-length of ${length}, but only accept up to ${body_size_limit} bytes.`
);
Expand Down Expand Up @@ -59,7 +58,13 @@ function get_raw_body(req, body_size_limit) {

size += chunk.length;
if (size > length) {
controller.error(new Error('content-length exceeded'));
req.destroy(
new Error(
`request body size exceeded ${
content_length ? "'content-length'" : 'BODY_SIZE_LIMIT'
} of ${length}`
)
);
return;
}

Expand Down