# Bun

The default. `bun .output/server/index.mjs` starts the server itself and listens on `PORT` (default 3000) and `HOST`; importing the entry starts nothing. Started with `node`, the entry prints one line and exits with code 1: build with `--preset node` to run on Node. It runs [WebSockets](/server#websockets), as `node` does. It is the only preset that runs the `sqlite` storage driver, and the only one that runs the `bun-sql` and `bun-sqlite` connections of [`@hamur/db`](/database#connections). The `redis` storage driver also runs on `node` and `cloudflare`, and `s3` on every preset.

## Reference

|                      |                   |
| -------------------- | ----------------- |
| Preset               | `bun`             |
| Runs on              | Bun               |
| `event.runtime.name` | `bun`             |
| Server target        | `bun`             |
| Lifetime, startup    | `process`, `load` |

### Output

```text
.output/
  hamur.json
  public/                    client assets and public/ files, with .gz, .br and .zst copies when build.compressPublic is on
  server/
    index.mjs                the server entry
    public-assets.json       size and strong ETag of every public file
    node_modules/            traced externals, with package.json (only when the build traced any)
```

### Config keys

| Key                         | Default               | What it does                                                                                               |
| --------------------------- | --------------------- | ---------------------------------------------------------------------------------------------------------- |
| `server.https`              | unset                 | `{ key, cert, ca?, passphrase? }` makes the server serve TLS; env `HAMUR_SSL_KEY` and `HAMUR_SSL_CERT` win |
| `server.unix`               | unset                 | listen on this Unix socket instead of `PORT` and `HOST`; env `HAMUR_UNIX_SOCKET` wins                      |
| `server.maxRequestBodySize` | `134217728` (128 MiB) | the largest request body the server accepts                                                                |
| `server.staticRoutesMax`    | 50 MiB                | bytes of hashed `/_hamur/` files registered as `Bun.serve` `routes` (`bun` only)                           |
| `build.compressPublic`      | `false`               | write `.br`, `.zst` and `.gz` copies of text, JSON, SVG, font and WASM files of 1 KiB or more              |

### Detection

None. Name it with `--preset`, `preset` in the config or `HAMUR_PRESET`; no environment variable selects it.

### Deploy command

No `hamur deploy` command: it exits 1 for this preset. Run `bun .output/server/index.mjs` on the host, or build an image with [`hamur build --docker`](/deploy/platforms/docker). The preview command of the preset, which `hamur preview` runs or prints, is `bun .output/server/index.mjs`.

### Limits

- Runs: WebSockets and `server.syncedState`, `server.pubsub`, streamed answers, `server.metrics`, image transforms at run time, private `app/env.ts` entries read at run time.
- Does not run: Durable Object classes.
- `scheduledTasks` run in process in UTC, `tasks.runOnStart` runs, and `tasks.queue` does not apply (`enqueue` runs the task in process, not durably). The request body limit is `server.maxRequestBodySize`, 128 MiB.

The levels are the `features` of the preset: `hamur build` warns or fails when the app uses a feature the preset does not run (see [Preset features](/deploy#preset-features)).

````sh [deploy-bun.sh]
hamur build
PORT=8080 HOST=0.0.0.0 bun .output/server/index.mjs
````

`hamur preview` runs the same command.

## HTTPS and Unix sockets

The `bun` and `node` servers can terminate TLS themselves. Set `HAMUR_SSL_KEY` and `HAMUR_SSL_CERT` at start, or `server.https: { key, cert, ca?, passphrase? }` in the config; the variables win. Each value is PEM text, or a file path when it does not start with `-----BEGIN`. A key without a cert fails the start (`server.https needs both a key and a cert (cert is missing; set HAMUR_SSL_CERT or server.https.cert)`), and a path that cannot be read fails it too: `server.https.cert: cannot read /etc/x.pem: <message>`. `bun` passes the pair to `Bun.serve` as `tls` and `node` uses `node:https`; WebSocket routes work over `wss://` on both.

`HAMUR_UNIX_SOCKET=/run/app.sock`, or `server.unix`, listens on that Unix socket instead of `PORT` and `HOST`. A socket file left by a process that died is removed at start, but only after a connection to it is refused; a file that is not a socket is never touched. A socket another server is listening on is kept, and the new server stops with `unix socket /run/app.sock is in use`. With `node.cluster`, the primary checks the file once and the workers share the socket.

## Hashed assets as routes

At start the `bun` server registers the hashed files below `/_hamur/` as `Bun.serve` `routes` with prebuilt responses, so Bun answers them from memory before the handler runs. The answer is the handler's: the same status, headers (the strong `ETag`, the immutable `Cache-Control`, the security headers) and body, a `304` on a matching `If-None-Match`, and `HEAD`. `server.staticRoutesMax` is the most bytes registered in total (default `52428800`, 50 MiB; `0` registers none); a file that would pass it stays on the handler. Files with precompressed variants, files a route rule can match (so a `headers` rule on `/_hamur/**` still applies), and every method but `GET` and `HEAD` stay on the handler too. A route is answered before hooks, server middleware, the request log and `Range` are looked at, so the build leaves the routes out when `server.csp`, `server.requestLog`, `server.tracing`, `server.metrics` or `server.serverTiming` is on. The [Bun executable](/deploy/presets/bun-compile) serves these files through the handler.

## Shutdown

The `bun` and `node` servers drain on the first `SIGTERM` or `SIGINT`: they stop accepting connections and give requests in flight up to 10 seconds, then close the connections that are left, run the `close` [server hook](/server#plugins-and-hooks) (also for up to 10 seconds) and exit with code 0. A second signal exits with code 1 at once.

Other details of these two servers: the `node` server sets `keepAliveTimeout` to 65 seconds, above the idle timeout of common load balancers. Its `requestTimeout`, `headersTimeout` (66 seconds by default) and `maxConnections` come from the `node` config key; a `HEAD` request is answered with the headers only, even on a streaming route, and a client that leaves mid-upload is not logged as an error. The `bun` server sets no idle timeout on a `text/event-stream` response. Files in `public/` answer a `Range` request with 206 and `Accept-Ranges: bytes`.
