diff --git a/README.md b/README.md index cb5fa76..fa20e91 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ Beta shipped the parts a real API needs, without growing the runtime dependency - **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`; `<= 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. +- **Graceful shutdown, with a deadline.** `app.close()` drains in-flight requests and closes live streams and mesh links, then warns and force-closes whatever is still open after 10 seconds — so a stuck handler cannot hold a deploy open forever. Change the number per call with `close({ timeoutMs })` or for the whole app with `createApp({ shutdownTimeoutMs })`. `app.close()` is Node-only; on Deno and Bun the server `serveDeno()`/`serveBun()` returns carries the same `close({ timeoutMs })`. - **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/website/src/content/docs/guides/dependency-injection.md b/website/src/content/docs/guides/dependency-injection.md index 06dacaf..9382bc2 100644 --- a/website/src/content/docs/guides/dependency-injection.md +++ b/website/src/content/docs/guides/dependency-injection.md @@ -53,7 +53,8 @@ const app = createApp({ modules: [ApiModule] }); await app.listen(3000); // GET /api/users/9 (header x-token) → { "requested": "9", "you": { "id": "...", "name": "Diego" } } -await app.close(); // graceful shutdown: drains in-flight requests, closes streams + mesh links +await app.close(); // graceful shutdown: drains in-flight, closes streams + mesh links, + // then force-closes whatever is left after 10s — app.close({ timeoutMs }) ``` ## `@Provider` — app-scope, memoized diff --git a/website/src/content/docs/guides/runtimes.md b/website/src/content/docs/guides/runtimes.md index 75e972d..c790da8 100644 --- a/website/src/content/docs/guides/runtimes.md +++ b/website/src/content/docs/guides/runtimes.md @@ -41,6 +41,32 @@ destroys excess sockets without sending an HTTP response. `Deno.serve` and `Bun. 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. +**Shutdown belongs to whoever owns the server.** `app.close()` is Node-only. On Deno and Bun the +app is served through `app.fetch` and never through `app.listen()`, so `app.close()` has no server +to drain and returns immediately — it warns if you passed `timeoutMs`. The server `serveDeno()` and +`serveBun()` return carries the same `close({ timeoutMs })` instead: + +```ts +const server = serveBun(app, { port: 3000 }); // or serveDeno(app, { port: 3000 }) +await server.close({ timeoutMs: 5_000 }); +``` + +On Cloudflare Workers there is nothing to close: `edgeHandler` is a handler, not a server, and the +platform owns the lifecycle. + +One difference the deadline cannot hide: + +| Runtime | After the deadline | +|---|---| +| Node | warns, then `closeAllConnections()` — the remainder is cut | +| Bun | warns, then `stop(true)` — the remainder is cut | +| Deno | warns, then **returns anyway**; the remainder ends with the process | + +Deno offers no force-close that composes with a drain already under way — aborting the signal its +server was started with throws `BadResource` once `shutdown()` is pending. So on Deno the deadline +bounds how long `close()` waits, not when the connections die. Exit the process after `close()` +resolves and the effect is the same; keep serving and it is not. + **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 c8f9109..77726c0 100644 --- a/website/src/content/docs/reference/createapp.md +++ b/website/src/content/docs/reference/createapp.md @@ -26,6 +26,7 @@ const app = createApp(options); | `views` | `string` | `process.cwd()` | base dir `@Html('file.html')` paths resolve against — relative paths join it, absolute paths are used as-is ([HTML & views](/docs/guides/html/)) | | `viewEngine` | `(source: string, data: unknown) => string` | built-in `render` | swap in your own template engine for `@Html(..., { template: true })`; template mode only ([HTML & views](/docs/guides/html/)) | | `static` | `boolean \| string` | — | serve a directory as a `GET`/`HEAD` fallback (after declared routes, before `404`); `true` → `./public`, a string → that dir; path-traversal-safe; needs a filesystem — Node/Deno/Bun only ([HTML & views](/docs/guides/html/)) | +| `shutdownTimeoutMs` | `number` | `10_000` | how long `close()` gives in-flight work before forcing the rest shut; `close({ timeoutMs })` still wins per call. Set it here when `close()` is reached from a signal handler or shutdown hook you do not own ([runtimes](/docs/guides/runtimes/)) | | `mesh` | `MeshConfig` | — | distributed DI — **requires `experimental: true`** ([mesh](/docs/guides/mesh/)) | | `experimental` | `boolean` | `false` | opt in to alpha features (currently gates `mesh`) | | `warnGraphDepth` | `number \| false` | `20` | warn when one route resolves to more than this many steps; `false` disables the design warning | @@ -64,7 +65,7 @@ const app = createApp(options); | Member | Returns | Notes | |---|---|---| | `listen(port)` | `Promise` | boots providers, then serves | -| `close()` | `Promise` | drains in-flight, closes streams + mesh links | +| `close({ timeoutMs? })` | `Promise` | drains in-flight, closes streams + mesh links; after the deadline (default `10s`, or `createApp({ shutdownTimeoutMs })`) it warns and force-closes what is left. Node-only — see [runtimes](/docs/guides/runtimes/) | | `ready()` | `Promise` | resolves the graph and mesh links without booting providers | | `fetch(request)` | `Promise` | Web-Standards HTTP/SSE handler for Node, Deno, Bun, and edge | | `upgrade(request, socket)` | `Promise` | neutral WebSocket upgrade used by non-Node adapters |