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 a06e4a29112a..9605c239badc 100644 --- a/documentation/docs/25-build-and-deploy/40-adapter-node.md +++ b/documentation/docs/25-build-and-deploy/40-adapter-node.md @@ -118,6 +118,8 @@ We instead read from the _right_, accounting for the number of trusted proxies. 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`](hooks#server-hooks-handle) if you need something more advanced. +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 6adbe7c35fd8..a13dc7c2b1d7 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(