docs: describe the shutdown deadline, and who owns it per runtime

Closes #18. The three places that still described close() without a timeout —
the createapp reference table, the README bullet and the dependency-injection
inline comment — now say that in-flight work gets 10 seconds, then a warning
and a force-close.

Also documents what #18 deliberately deferred, now that #15 has decided it.
runtimes.md gains the per-runtime story: app.close() is Node-only, the servers
serveDeno()/serveBun() return carry the same close({ timeoutMs }), and edge has
nothing to close. Including the row that matters — Node and Bun cut the
remainder after the deadline, Deno returns and lets the process end it — since
a deadline that means two things is worse than one that admits it.

createApp({ shutdownTimeoutMs }) from #19 gets its row in the options table.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-08-16 13:17:19 -05:00
1 parent 586e14da72
commit e4a2047a1e
4 files changed
+31 -3

No files matched your search

+1 -1
View File
@@ -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.
@@ -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
@@ -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.
@@ -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<http.Server>` | boots providers, then serves |
| `close()` | `Promise<void>` | drains in-flight, closes streams + mesh links |
| `close({ timeoutMs? })` | `Promise<void>` | 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<void>` | resolves the graph and mesh links without booting providers |
| `fetch(request)` | `Promise<Response>` | Web-Standards HTTP/SSE handler for Node, Deno, Bun, and edge |
| `upgrade(request, socket)` | `Promise<void>` | neutral WebSocket upgrade used by non-Node adapters |