From 8f8459ff8dfab233f712861d716f89e9892faf4c Mon Sep 17 00:00:00 2001 From: surefire Date: Fri, 9 Sep 2022 18:01:22 +0300 Subject: [PATCH 1/6] Allow streaming with body_size_limit --- packages/adapter-node/README.md | 2 +- packages/kit/src/exports/node/index.js | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/adapter-node/README.md b/packages/adapter-node/README.md index 2d5a93f210a8..ad9be17fede1 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. 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..0afb214f4057 100644 --- a/packages/kit/src/exports/node/index.js +++ b/packages/kit/src/exports/node/index.js @@ -11,7 +11,7 @@ function get_raw_body(req, body_size_limit) { return null; } - const length = Number(h['content-length']); + let length = Number(h['content-length']); // check if no request body if ( @@ -23,11 +23,8 @@ function get_raw_body(req, body_size_limit) { 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 +56,7 @@ 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')); return; } From f9e425477aec91e486ab76a2bed9a67b27dc710a Mon Sep 17 00:00:00 2001 From: surefire Date: Sat, 10 Sep 2022 07:19:37 +0300 Subject: [PATCH 2/6] More detailed error message --- packages/kit/src/exports/node/index.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/kit/src/exports/node/index.js b/packages/kit/src/exports/node/index.js index 0afb214f4057..27f6242034f9 100644 --- a/packages/kit/src/exports/node/index.js +++ b/packages/kit/src/exports/node/index.js @@ -11,16 +11,18 @@ function get_raw_body(req, body_size_limit) { return null; } - let 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) { length = body_size_limit; @@ -56,7 +58,9 @@ function get_raw_body(req, body_size_limit) { size += chunk.length; if (size > length) { - req.destroy(new Error('request body size exceeded')); + req.destroy( + new Error(`request body size exceeded ${content_length ? "specified 'content-length'" : 'allowed by the server'}: ${length}`) + ); return; } From d577133b26128f84562e5d31515641a060ca5473 Mon Sep 17 00:00:00 2001 From: surefire Date: Sat, 10 Sep 2022 07:22:03 +0300 Subject: [PATCH 3/6] fix formatting --- packages/kit/src/exports/node/index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/kit/src/exports/node/index.js b/packages/kit/src/exports/node/index.js index 27f6242034f9..f944edce602d 100644 --- a/packages/kit/src/exports/node/index.js +++ b/packages/kit/src/exports/node/index.js @@ -59,7 +59,11 @@ function get_raw_body(req, body_size_limit) { size += chunk.length; if (size > length) { req.destroy( - new Error(`request body size exceeded ${content_length ? "specified 'content-length'" : 'allowed by the server'}: ${length}`) + new Error( + `request body size exceeded ${ + content_length ? "specified 'content-length'" : 'allowed by the server' + }: ${length}` + ) ); return; } From 81c13aedd4746d74e15ac029d02bd38ea1f617eb Mon Sep 17 00:00:00 2001 From: surefire Date: Sat, 10 Sep 2022 20:38:48 +0300 Subject: [PATCH 4/6] Clarify error message --- packages/adapter-node/README.md | 2 +- packages/kit/src/exports/node/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/adapter-node/README.md b/packages/adapter-node/README.md index ad9be17fede1..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. 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 f944edce602d..f5eb5019a857 100644 --- a/packages/kit/src/exports/node/index.js +++ b/packages/kit/src/exports/node/index.js @@ -61,7 +61,7 @@ function get_raw_body(req, body_size_limit) { req.destroy( new Error( `request body size exceeded ${ - content_length ? "specified 'content-length'" : 'allowed by the server' + content_length ? "'content-length'" : 'BODY_SIZE_LIMIT' }: ${length}` ) ); From 7814a2727e16ff7f07560816f484ea7b38d7de9a Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sat, 10 Sep 2022 20:24:32 -0700 Subject: [PATCH 5/6] Update packages/kit/src/exports/node/index.js --- packages/kit/src/exports/node/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/exports/node/index.js b/packages/kit/src/exports/node/index.js index f5eb5019a857..b73c5c91b421 100644 --- a/packages/kit/src/exports/node/index.js +++ b/packages/kit/src/exports/node/index.js @@ -62,7 +62,7 @@ function get_raw_body(req, body_size_limit) { new Error( `request body size exceeded ${ content_length ? "'content-length'" : 'BODY_SIZE_LIMIT' - }: ${length}` + } of ${length}` ) ); return; From 7091c315a5e4789c8c3147b28a762d7f5b791988 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 13 Sep 2022 10:23:37 -0400 Subject: [PATCH 6/6] Create fair-rivers-repair.md --- .changeset/fair-rivers-repair.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fair-rivers-repair.md 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