From 9e5e5cffe90345876b1ba4eec5a5b40894f48bd1 Mon Sep 17 00:00:00 2001 From: hgshreyas Date: Sun, 9 Aug 2026 17:27:47 +0530 Subject: [PATCH 1/2] feat: add concurrent connection limits Signed-off-by: hgshreyas --- CHANGELOG.md | 5 +++++ README.md | 2 +- src/bun.ts | 2 ++ src/deno.ts | 2 ++ src/http/server.ts | 1 + src/http/types.ts | 1 + test/http.test.ts | 11 +++++++++-- website/src/content/docs/guides/runtimes.md | 6 ++++++ website/src/content/docs/reference/createapp.md | 1 + 9 files changed, 28 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 792476e..9621b1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ npm treats versions as semver and semver forbids leading zeros. ## [Unreleased] +### Added + +- `limits.maxConnections` caps concurrent Node sockets (default `1000`) through + `server.maxConnections`; Deno and Bun have no equivalent runtime setting and require a + platform or reverse-proxy connection cap. ## [26.8.0-beta.0] - 2026-08-02 ### Added diff --git a/README.md b/README.md index fa99449..cfd8c33 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ Beta shipped the parts a real API needs, without growing the runtime dependency - **Real body parsing.** JSON and urlencoded out of the box; `multipart/form-data` file uploads (`@body()` → `{ fields, files }`) via the optional [`busboy`](https://github.com/mscdex/busboy) peer dependency (`npm i busboy`) — a multipart request without it returns a clear `501`. All size-capped (`413`) against DoS. - **Errors, your way.** Throw typed errors (`Unauthorized`, `NotFound`, `HttpError(status, msg, body?)`) anywhere; they convert centrally to `4xx`/`5xx`. Render them however you like — HTML, RFC 7807, content-negotiated — with a single `createApp({ onError })` hook that covers every error response. - **HTML without a view layer.** `@Html` returns a string, serves a file, or renders one with the built-in `{{ }}` template engine — swap in EJS or handlebars with `createApp({ viewEngine })`. `createApp({ static: true })` serves `./public` with path-traversal guards and no mime dependency. -- **Ceilings that are already on.** `limits` caps body size (`413`), multipart parts, and the request / headers / keep-alive timeouts — defaulting to `30s` / `10s` / `5s`, tighter than Node's own two-minute default. Configurable, and safe before you configure anything. +- **Ceilings that are already on.** `limits` caps body size (`413`), multipart parts, concurrent Node connections (default 1000), and the request / headers / keep-alive timeouts — defaulting to `30s` / `10s` / `5s`, tighter than Node's own two-minute default. Deno and Bun expose no equivalent active-socket cap, so enforce one at the deployment platform or reverse proxy there. Configurable, and safe before you configure anything. - **Graceful shutdown.** `app.close()` drains in-flight requests, closes live streams and mesh links. - **Testable by construction.** `createApp({ overrides: { db: fakeDb } })` swaps any node in one line. - **Dual ESM + CommonJS.** Ships both builds behind an `exports` map — `import` and `require` both resolve, with matching type declarations. diff --git a/src/bun.ts b/src/bun.ts index 4656034..cdb34c3 100644 --- a/src/bun.ts +++ b/src/bun.ts @@ -56,6 +56,8 @@ function bunSocket(ws: BunServerWebSocket): WsSocket { * Serve a Green Tea app on Bun: HTTP + SSE via app.fetch, WebSocket via Bun's server-level * websocket handler. The ServerWebSocket only exists in open(), so per-connection state * (inbound channel, abort, WsRequest) is stashed on ws.data during the upgrade. + * Bun.serve exposes no equivalent to Node's server.maxConnections; enforce connection caps at + * the deployment platform or reverse proxy. */ export function serveBun(app: App, options?: BunServeShortOptions): BunServer { return Bun.serve({ diff --git a/src/deno.ts b/src/deno.ts index 73771c6..12a3809 100644 --- a/src/deno.ts +++ b/src/deno.ts @@ -31,6 +31,8 @@ declare const Deno: { /** * Serve a Green Tea app on Deno: HTTP + SSE via app.fetch, WebSocket via Deno.upgradeWebSocket. * WebSocket messages flow through app.upgrade over the shared neutral core. + * Deno.serve exposes no equivalent to Node's server.maxConnections; enforce connection caps at + * the deployment platform or reverse proxy. */ export function serveDeno(app: App, options?: DenoServeOptions): DenoHttpServer { return Deno.serve(options ?? {}, (request, info) => { diff --git a/src/http/server.ts b/src/http/server.ts index bc496f9..8abeac8 100644 --- a/src/http/server.ts +++ b/src/http/server.ts @@ -59,6 +59,7 @@ export function createHttpServer( const server: http.Server = (opts?.tls ? nodeHttps.createServer(opts.tls as https.ServerOptions, handler) : nodeHttp.createServer(handler)) as unknown as http.Server; + server.maxConnections = opts?.limits?.maxConnections ?? 1000; server.requestTimeout = opts?.limits?.requestTimeoutMs ?? 30_000; server.headersTimeout = opts?.limits?.headersTimeoutMs ?? 10_000; server.keepAliveTimeout = opts?.limits?.keepAliveTimeoutMs ?? 5_000; diff --git a/src/http/types.ts b/src/http/types.ts index 7ea2406..2e5e49e 100644 --- a/src/http/types.ts +++ b/src/http/types.ts @@ -8,6 +8,7 @@ import type { StaticResolver } from '../views'; /** Per-request size and timeout ceilings applied by the server. */ export interface RequestLimits { maxBodyBytes?: number; // default 1_000_000 + maxConnections?: number; requestTimeoutMs?: number; // default 30_000 headersTimeoutMs?: number; // default 10_000 keepAliveTimeoutMs?: number; // default 5_000 diff --git a/test/http.test.ts b/test/http.test.ts index 468592e..da8b805 100644 --- a/test/http.test.ts +++ b/test/http.test.ts @@ -115,15 +115,22 @@ describe('request hardening', () => { server.close(); }); - it('sets the configured server timeouts', async () => { + it('sets the configured server timeouts and connection limit', async () => { const server = createHttpServer([], [], undefined, undefined, - { limits: { requestTimeoutMs: 12345, headersTimeoutMs: 6789, keepAliveTimeoutMs: 4321 } }); + { limits: { maxConnections: 2468, requestTimeoutMs: 12345, headersTimeoutMs: 6789, keepAliveTimeoutMs: 4321 } }); + expect(server.maxConnections).toBe(2468); expect(server.requestTimeout).toBe(12345); expect(server.headersTimeout).toBe(6789); expect(server.keepAliveTimeout).toBe(4321); server.close(); }); + it('caps concurrent connections by default', () => { + const server = createHttpServer([]); + expect(server.maxConnections).toBe(1000); + server.close(); + }); + it('requestTimeout does NOT kill an in-flight SSE stream (streaming regression)', async () => { async function* feed() { for (let n = 1; n <= 3; n++) { yield { n }; await new Promise((r) => setTimeout(r, 80)); } } const server = createHttpServer( diff --git a/website/src/content/docs/guides/runtimes.md b/website/src/content/docs/guides/runtimes.md index bf07f79..6c9696f 100644 --- a/website/src/content/docs/guides/runtimes.md +++ b/website/src/content/docs/guides/runtimes.md @@ -34,6 +34,12 @@ same `@Ws` handlers, same rooms/channels everywhere. `app.listen()`, TLS and per remain Node-only; on Deno/Bun/edge you get `app.fetch` + the runtime's adapter. **Mesh (alpha) runs on Node, Deno and Bun** — teapot and teacup, in any combination — but not on edge. +**Connection-limit enforcement is Node-only:** `limits.maxConnections` defaults to `1000` and +maps to Node's `server.maxConnections` when the app runs through `app.listen()`. `Deno.serve` and +`Bun.serve` expose no equivalent active-socket cap, so `serveDeno` and `serveBun` cannot enforce +this option. Apply the connection limit at the deployment platform or reverse proxy on those +runtimes. + **Body-size enforcement differs slightly:** on `app.fetch`, an oversized body is read in full before the `413` is returned, whereas Node aborts mid-stream once the limit is hit — so on non-Node runtimes, also bound request size at the platform/runtime layer. diff --git a/website/src/content/docs/reference/createapp.md b/website/src/content/docs/reference/createapp.md index 00b8b94..42861d9 100644 --- a/website/src/content/docs/reference/createapp.md +++ b/website/src/content/docs/reference/createapp.md @@ -35,6 +35,7 @@ const app = createApp(options); | Field | Default | |---|---| | `maxBodyBytes` | `1_000_000` (→ `413` when exceeded) | +| `maxConnections` | `1000` (Node only; no Deno/Bun equivalent) | | `requestTimeoutMs` | `30_000` | | `headersTimeoutMs` | `10_000` | | `keepAliveTimeoutMs` | `5_000` | From 8c781c12584c5b2b9fed4037ecff72985484d480 Mon Sep 17 00:00:00 2001 From: hgshreyas Date: Mon, 10 Aug 2026 16:14:07 +0530 Subject: [PATCH 2/2] fix: treat non-positive connection limits as unlimited Signed-off-by: hgshreyas --- CHANGELOG.md | 7 ++++--- README.md | 2 +- src/http/server.ts | 3 ++- src/http/types.ts | 4 ++-- test/http.test.ts | 8 ++++++++ website/src/content/docs/guides/runtimes.md | 11 ++++++----- website/src/content/docs/reference/createapp.md | 2 +- 7 files changed, 24 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9621b1a..bece1f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,9 +10,10 @@ npm treats versions as semver and semver forbids leading zeros. ### Added -- `limits.maxConnections` caps concurrent Node sockets (default `1000`) through - `server.maxConnections`; Deno and Bun have no equivalent runtime setting and require a - platform or reverse-proxy connection cap. +- `limits.maxConnections` changes Node's previously unlimited concurrent socket count to a + default cap of `1000`; values `<= 0` leave Node unlimited. Deno and Bun have no equivalent + runtime setting and require a platform or reverse-proxy connection cap. + ## [26.8.0-beta.0] - 2026-08-02 ### Added diff --git a/README.md b/README.md index cfd8c33..060ae12 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ Beta shipped the parts a real API needs, without growing the runtime dependency - **Real body parsing.** JSON and urlencoded out of the box; `multipart/form-data` file uploads (`@body()` → `{ fields, files }`) via the optional [`busboy`](https://github.com/mscdex/busboy) peer dependency (`npm i busboy`) — a multipart request without it returns a clear `501`. All size-capped (`413`) against DoS. - **Errors, your way.** Throw typed errors (`Unauthorized`, `NotFound`, `HttpError(status, msg, body?)`) anywhere; they convert centrally to `4xx`/`5xx`. Render them however you like — HTML, RFC 7807, content-negotiated — with a single `createApp({ onError })` hook that covers every error response. - **HTML without a view layer.** `@Html` returns a string, serves a file, or renders one with the built-in `{{ }}` template engine — swap in EJS or handlebars with `createApp({ viewEngine })`. `createApp({ static: true })` serves `./public` with path-traversal guards and no mime dependency. -- **Ceilings that are already on.** `limits` caps body size (`413`), multipart parts, concurrent Node connections (default 1000), and the request / headers / keep-alive timeouts — defaulting to `30s` / `10s` / `5s`, tighter than Node's own two-minute default. Deno and Bun expose no equivalent active-socket cap, so enforce one at the deployment platform or reverse proxy there. Configurable, and safe before you configure anything. +- **Ceilings that are already on.** `limits` caps body size (`413`), multipart parts, concurrent Node connections (default `1000`; `<= 0` leaves connections unlimited), and the request / headers / keep-alive timeouts — defaulting to `30s` / `10s` / `5s`, tighter than Node's own two-minute default. When Node's connection cap is reached, excess sockets are destroyed without an HTTP response. Deno and Bun expose no equivalent active-socket cap, so enforce one at the deployment platform or reverse proxy there. Configurable, and safe before you configure anything. - **Graceful shutdown.** `app.close()` drains in-flight requests, closes live streams and mesh links. - **Testable by construction.** `createApp({ overrides: { db: fakeDb } })` swaps any node in one line. - **Dual ESM + CommonJS.** Ships both builds behind an `exports` map — `import` and `require` both resolve, with matching type declarations. diff --git a/src/http/server.ts b/src/http/server.ts index 8abeac8..e0c48dd 100644 --- a/src/http/server.ts +++ b/src/http/server.ts @@ -59,7 +59,8 @@ export function createHttpServer( const server: http.Server = (opts?.tls ? nodeHttps.createServer(opts.tls as https.ServerOptions, handler) : nodeHttp.createServer(handler)) as unknown as http.Server; - server.maxConnections = opts?.limits?.maxConnections ?? 1000; + const maxConnections = opts?.limits?.maxConnections ?? 1000; + if (maxConnections > 0) server.maxConnections = maxConnections; server.requestTimeout = opts?.limits?.requestTimeoutMs ?? 30_000; server.headersTimeout = opts?.limits?.headersTimeoutMs ?? 10_000; server.keepAliveTimeout = opts?.limits?.keepAliveTimeoutMs ?? 5_000; diff --git a/src/http/types.ts b/src/http/types.ts index 2e5e49e..fbb5806 100644 --- a/src/http/types.ts +++ b/src/http/types.ts @@ -5,10 +5,10 @@ import type { TlsOptions, SecurityOptions, CorsOptions } from '../security'; import type { WsRequest, WsSocket } from './ws-core'; import type { StaticResolver } from '../views'; -/** Per-request size and timeout ceilings applied by the server. */ +/** Per-request and per-server resource ceilings applied by the server. */ export interface RequestLimits { maxBodyBytes?: number; // default 1_000_000 - maxConnections?: number; + maxConnections?: number; // default 1000; <= 0 means unlimited (Node only) requestTimeoutMs?: number; // default 30_000 headersTimeoutMs?: number; // default 10_000 keepAliveTimeoutMs?: number; // default 5_000 diff --git a/test/http.test.ts b/test/http.test.ts index da8b805..fc322f2 100644 --- a/test/http.test.ts +++ b/test/http.test.ts @@ -131,6 +131,14 @@ describe('request hardening', () => { server.close(); }); + it.each([0, -1])('treats maxConnections=%i as unlimited', (maxConnections) => { + const server = createHttpServer([], [], undefined, undefined, { + limits: { maxConnections }, + }); + expect(server.maxConnections).toBeUndefined(); + server.close(); + }); + it('requestTimeout does NOT kill an in-flight SSE stream (streaming regression)', async () => { async function* feed() { for (let n = 1; n <= 3; n++) { yield { n }; await new Promise((r) => setTimeout(r, 80)); } } const server = createHttpServer( diff --git a/website/src/content/docs/guides/runtimes.md b/website/src/content/docs/guides/runtimes.md index 6c9696f..75e972d 100644 --- a/website/src/content/docs/guides/runtimes.md +++ b/website/src/content/docs/guides/runtimes.md @@ -34,11 +34,12 @@ same `@Ws` handlers, same rooms/channels everywhere. `app.listen()`, TLS and per remain Node-only; on Deno/Bun/edge you get `app.fetch` + the runtime's adapter. **Mesh (alpha) runs on Node, Deno and Bun** — teapot and teacup, in any combination — but not on edge. -**Connection-limit enforcement is Node-only:** `limits.maxConnections` defaults to `1000` and -maps to Node's `server.maxConnections` when the app runs through `app.listen()`. `Deno.serve` and -`Bun.serve` expose no equivalent active-socket cap, so `serveDeno` and `serveBun` cannot enforce -this option. Apply the connection limit at the deployment platform or reverse proxy on those -runtimes. +**Connection-limit enforcement is Node-only:** `limits.maxConnections` defaults to `1000`; values +`<= 0` leave Node's connection count unlimited. Positive values map to Node's +`server.maxConnections` when the app runs through `app.listen()`. When the cap is reached, Node +destroys excess sockets without sending an HTTP response. `Deno.serve` and `Bun.serve` expose no +equivalent active-socket cap, so `serveDeno` and `serveBun` cannot enforce this option. Apply the +connection limit at the deployment platform or reverse proxy on those runtimes. **Body-size enforcement differs slightly:** on `app.fetch`, an oversized body is read in full before the `413` is returned, whereas Node aborts mid-stream once the limit is hit — so on diff --git a/website/src/content/docs/reference/createapp.md b/website/src/content/docs/reference/createapp.md index 42861d9..c8f9109 100644 --- a/website/src/content/docs/reference/createapp.md +++ b/website/src/content/docs/reference/createapp.md @@ -35,7 +35,7 @@ const app = createApp(options); | Field | Default | |---|---| | `maxBodyBytes` | `1_000_000` (→ `413` when exceeded) | -| `maxConnections` | `1000` (Node only; no Deno/Bun equivalent) | +| `maxConnections` | `1000` (Node only; `<= 0` means unlimited) | | `requestTimeoutMs` | `30_000` | | `headersTimeoutMs` | `10_000` | | `keepAliveTimeoutMs` | `5_000` |