diff --git a/.changeset/fair-rivers-repair.md b/.changeset/fair-rivers-repair.md new file mode 100644 index 000000000000..186008088d37 --- /dev/null +++ b/.changeset/fair-rivers-repair.md @@ -0,0 +1,5 @@ +--- +"@sveltejs/adapter-node": patch +--- + +Allow streaming when `BODY_SIZE_LIMIT` is set diff --git a/packages/adapter-node/README.md b/packages/adapter-node/README.md index 2d5a93f210a8..2e8ddced201d 100644 --- a/packages/adapter-node/README.md +++ b/packages/adapter-node/README.md @@ -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 diff --git a/packages/kit/src/exports/node/index.js b/packages/kit/src/exports/node/index.js index a04adee3f587..b73c5c91b421 100644 --- a/packages/kit/src/exports/node/index.js +++ b/packages/kit/src/exports/node/index.js @@ -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.` ); @@ -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; }