diff --git a/.changeset/great-hats-cough.md b/.changeset/great-hats-cough.md new file mode 100644 index 000000000000..5cc8c28221dd --- /dev/null +++ b/.changeset/great-hats-cough.md @@ -0,0 +1,5 @@ +--- +"@sveltejs/adapter-node": minor +--- + +feat: add unit suffixes to the `BODY_SIZE_LIMIT` environment variable diff --git a/documentation/docs/25-build-and-deploy/40-adapter-node.md b/documentation/docs/25-build-and-deploy/40-adapter-node.md index 009c2e6d0650..a701477ef0b5 100644 --- a/documentation/docs/25-build-and-deploy/40-adapter-node.md +++ b/documentation/docs/25-build-and-deploy/40-adapter-node.md @@ -139,6 +139,8 @@ The number of seconds to wait before forcefully closing any remaining connection When using systemd socket activation, `IDLE_TIMEOUT` specifies the number of seconds after which the app is automatically put to sleep when receiving no requests. If not set, the app runs continuously. See [Socket activation](#socket-activation) for more details. +The body size variable can also specify unit suffixes for kilobytes (`K`), megabytes (`M`), and gigabytes (`G`). For example `512K` and `1M` + ## Options The adapter can be configured with various options: diff --git a/packages/adapter-node/src/handler.js b/packages/adapter-node/src/handler.js index 5586e5169622..51690b783095 100644 --- a/packages/adapter-node/src/handler.js +++ b/packages/adapter-node/src/handler.js @@ -19,7 +19,21 @@ const address_header = env('ADDRESS_HEADER', '').toLowerCase(); const protocol_header = env('PROTOCOL_HEADER', '').toLowerCase(); const host_header = env('HOST_HEADER', 'host').toLowerCase(); const port_header = env('PORT_HEADER', '').toLowerCase(); -const body_size_limit = Number(env('BODY_SIZE_LIMIT', '524288')); + +/** + * @param {string} bytes + */ +function parse_body_size_limit(bytes) { + const multiplier = + { + K: 1024, + M: 1024 * 1024, + G: 1024 * 1024 * 1024 + }[bytes[bytes.length - 1]?.toUpperCase()] ?? 1; + return Number(multiplier != 1 ? bytes.substring(0, bytes.length - 1) : bytes) * multiplier; +} + +const body_size_limit = parse_body_size_limit(env('BODY_SIZE_LIMIT', '512K')); if (isNaN(body_size_limit)) { throw new Error(