Skip to content

Commit

Permalink
feat: add unit suffix support for BODY_SIZE_LIMIT (#11680)
Browse files Browse the repository at this point in the history
  • Loading branch information
cooolbros authored Jun 19, 2024
1 parent 50a6dc1 commit 6b545e2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/great-hats-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/adapter-node": minor
---

feat: add unit suffixes to the `BODY_SIZE_LIMIT` environment variable
2 changes: 2 additions & 0 deletions documentation/docs/25-build-and-deploy/40-adapter-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 15 additions & 1 deletion packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 6b545e2

Please sign in to comment.