Skip to content

Commit 2ecfd83

Browse files
feat: add compatibility for env BODY_SIZE_LIMIT
1 parent 6dbb081 commit 2ecfd83

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@ export default {
6060

6161
### out
6262

63-
The directory to build the server to. It defaults to `build` — i.e. `bun run start` would start the server locally after it has been created.
63+
The directory to build the server to. Default: `build` — i.e. `bun run start` would start the server locally after it has been created.
6464

6565
### precompress
6666

67-
Enables precompressing using gzip and brotli for assets and prerendered pages. It defaults to `false`.
67+
Enables precompressing using gzip and brotli for assets and prerendered pages. Default: `false`.
6868

6969
#### brotli
7070

71-
Enable brotli precompressing. It defaults to `false`.
71+
Enable brotli precompressing. Default: `false`.
7272

7373
#### gzip
7474

75-
Enable gzip precompressing. It defaults to `false`.
75+
Enable gzip precompressing. Default: `false`.
7676

7777
### development
7878

@@ -90,6 +90,18 @@ Specify the port and host to listen on.
9090

9191
Default: `0.0.0.0`, `3000`
9292

93+
### `BODY_SIZE_LIMIT`
94+
95+
The maximum request body size to accept in bytes including while streaming.
96+
97+
Accepted inputs:
98+
99+
- Raw number in bytes, e.g. `1048576`.
100+
- String with a unit suffix (`K`, `M`, `G`), e.g. `1M`.
101+
- Literal string `"Infinity"` or number `0` to disable body size limit.
102+
103+
Default: `512K`
104+
93105
### `ORIGIN`, `PROTOCOL_HEADER` and `HOST_HEADER`
94106

95107
HTTP doesn't give SvelteKit a reliable way to know the URL that is currently being requested. The simplest way to tell SvelteKit where the app is being served is to set the `ORIGIN` environment variable.

src/server/env.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ const env = createEnv({
88
// Server port number
99
PORT: z.coerce.number().int().gte(1).lte(65535).default(3000),
1010
// Maximum request body size (optional)
11-
BODY_SIZE_LIMIT: z.coerce.number().int().gt(0).optional(),
11+
BODY_SIZE_LIMIT: z
12+
.union([
13+
z.coerce.number().int().gte(0),
14+
z.string().refine((val) => /^(\d+)(K|M|G)?$/i.test(val), {}),
15+
z.literal("Infinity").transform(() => Number.POSITIVE_INFINITY),
16+
])
17+
.default("512K"),
1218
// Application origin URL
1319
ORIGIN: z.string().url().default("http://localhost:3000"),
1420
// Header for forwarded protocol

src/server/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import env from "./env";
22
import { buildKitServer } from "./kit-server";
33
import { buildRoutes } from "./routes";
44
import { handleSSRRequest } from "./routes/ssr";
5-
import { getManifestFile } from "./utils";
5+
import { getManifestFile, parseEnvBytes } from "./utils";
66

77
async function getBunServeConfig(): Promise<Parameters<typeof Bun.serve>[0]> {
88
const { manifest } = await getManifestFile();
@@ -11,7 +11,7 @@ async function getBunServeConfig(): Promise<Parameters<typeof Bun.serve>[0]> {
1111
return {
1212
port: env.PORT,
1313
hostname: env.HOST,
14-
maxRequestBodySize: env.BODY_SIZE_LIMIT,
14+
maxRequestBodySize: parseEnvBytes(env.BODY_SIZE_LIMIT),
1515
development: env.DEV_MODE,
1616
routes: await buildRoutes(),
1717
async fetch(req, srv) {

src/server/utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,24 @@ export function get_origin(headers: Headers): string {
4040

4141
return `${protocol}://${host}`;
4242
}
43+
44+
export function parseEnvBytes(input: string | number): number {
45+
if (typeof input === "number") return input === 0 ? Number.POSITIVE_INFINITY : input;
46+
47+
// biome-ignore lint/style/noNonNullAssertion: guarded by `zod`
48+
const match = input.match(/^(\d+)(?:(K|M|G))?$/i)!;
49+
50+
const size = Number.parseInt(match[1] || "0", 10);
51+
const unit = match[2]?.toUpperCase() || "";
52+
53+
switch (unit) {
54+
case "K":
55+
return size * 1024;
56+
case "M":
57+
return size * 1024 * 1024;
58+
case "G":
59+
return size * 1024 * 1024 * 1024;
60+
default:
61+
return size;
62+
}
63+
}

0 commit comments

Comments
 (0)