Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Bun version is pinned by `packageManager` in `package.json`. The Vitest and Play
- Types: `bun run check:types` (sync + `tsc --noEmit` + `svelte-check --threshold warning`, so Svelte warnings fail CI)
- Unit tests: `bun run test`. For one file: `bun run test src/lib/server/__tests__/csrf.test.ts`. For one case, add `-t "<test name>"`
- Never use `bun test`: that is Bun's own runner, which can't compile Svelte and skips `vitest.config.ts`
- Production: `bun run build`, then `bun run start`, which runs `scripts/serve.ts` (see Deployment and origin)
- Production: `bun run build`, then `NODE_ENV=production bun scripts/serve.ts`, which is what the `start` script runs (see Deployment and origin). Not `bun run start`: under it one Ctrl+C reaches the server twice and stops it at once without the drain
- E2E, full as in CI: `bun run test:e2e` (`scripts/check-e2e.sh`: builds, diffs migrations, then runs two Playwright passes on fresh temp DBs)
- One E2E project, reusing an existing build: `E2E_SKIP_BUILD=1 bunx --no-install playwright test --project=app`. Project names are in `playwright.config.ts`. The fresh-setup spec needs `E2E_SEED_SETUP_PRE_ADMIN=1`

Expand Down Expand Up @@ -117,7 +117,7 @@ vi.mock("$lib/db/repositories/config", () => ({ getConfig: mocks.getConfig }));
- With `ORIGIN` it fronts the adapter: it binds `HOST`/`PORT`, runs the adapter on a private Unix socket, forwards the raw path (`forwardPath`, never `new URL(request.url)`), and overwrites `x-otpravkarr-origin-proto`/`-host`/`-peer` on every request. The origin never comes from the request's `Host` header. Keep that file small and tested (`scripts/serve.test.ts`); obzorarr and setun copy its pattern.
- App-level origin settings derive from `ORIGIN`: the CSRF hook and the settings lockout guard use `effectiveAllowedOrigins` (`src/lib/server/origins.ts`), which always includes `ORIGIN`; the stored `allowed_origins` only add origins.
- Behind a reverse proxy, `ADDRESS_HEADER=x-forwarded-for` (with `XFF_DEPTH` = the number of proxies, default 1) only when every request goes through it; `PROTOCOL_HEADER`/`HOST_HEADER` only without `ORIGIN`, behind a trusted proxy.
- `IDLE_TIMEOUT` maps to the adapter's `CONNECTION_IDLE_TIMEOUT` (an explicit `CONNECTION_IDLE_TIMEOUT` wins). With `ORIGIN`, the front applies that window to the client only: its timer is off from the end of the request body until the app answers, then re-armed (Bun 1.4.2 otherwise closes a request still waiting on the app). `SHUTDOWN_TIMEOUT` (default 30 s) bounds the drain of both the adapter and the front, and with `ORIGIN` the process exits at that deadline even if the app still has work in flight; a second `SIGTERM`/`SIGINT` exits 1, and the front removes its socket directory on every exit. With `ORIGIN`, `SIGHUP` (a closed terminal) starts the same shutdown and reaches the adapter as `SIGTERM`, after the load if it arrives during it; a repeated `SIGHUP` is not a second signal (under `bun run start` the hangup arrives twice). `BODY_SIZE_LIMIT` (default `512K`) is enforced by the adapter.
- `IDLE_TIMEOUT` maps to the adapter's `CONNECTION_IDLE_TIMEOUT` (an explicit `CONNECTION_IDLE_TIMEOUT` wins). With `ORIGIN`, the front applies that window to the client only: its timer is off from the end of the request body until the app answers, then re-armed (Bun 1.4.2 otherwise closes a request still waiting on the app). `SHUTDOWN_TIMEOUT` (default 30 s) bounds the drain of both the adapter and the front, and with `ORIGIN` the process exits at that deadline even if the app still has work in flight; a second `SIGTERM`/`SIGINT` exits 1, and the front removes its socket directory on every exit. With `ORIGIN`, `SIGHUP` (a closed terminal) starts the same shutdown and reaches the adapter as `SIGTERM`, after the load if it arrives during it; a repeated `SIGHUP` is not a second signal: under `bun run start` the hangup arrives twice (once under `NODE_ENV=production bun scripts/serve.ts`). A Ctrl+C or group `SIGTERM` also arrives twice under `bun run start`, and the second one forces exit 1 without the drain, with or without `ORIGIN`; run `scripts/serve.ts` directly. `BODY_SIZE_LIMIT` (default `512K`) is enforced by the adapter.
- Cookies are `Secure` only when `event.url` is `https:` (`isSecureRequest` and the option helpers in `src/lib/server/auth.ts`). Pass that `secure` value to every `cookies.set` and `cookies.delete`; a plain-HTTP origin cannot keep a `Secure` cookie.

## Commits
Expand Down
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@ The dev server binds to `PORT` (default `3000`) and fails fast if that port is b

## Production Startup

Use `bun run build`, then `bun run start` with the same pinned Bun version.
The start script sets `NODE_ENV=production` and runs `scripts/serve.ts`, which
starts the `@sveltejs/adapter-bun` server in `build/`. Ship `scripts/serve.ts`
with production `node_modules/`, `package.json` and the complete `build/`
directory; `build/server/migrations/` contains the SQL copied by the build
command.
Build, then start the server directly, with the same pinned Bun version:

```bash
bun run build
NODE_ENV=production bun scripts/serve.ts
```

This is what the `start` script runs. Run it directly: with Bun 1.4.2's default
shell, `bun run start` and `bun start` deliver one Ctrl+C twice, which skips the
`SHUTDOWN_TIMEOUT` drain and stops at once. `scripts/serve.ts` starts the
`@sveltejs/adapter-bun` server in `build/`. Ship `scripts/serve.ts` with
production `node_modules/`, `package.json` and the complete `build/` directory;
`build/server/migrations/` contains the SQL copied by the build command.
Keep the same `OTPRAVKARR_SECRET` and database across restarts. A configured
`DATABASE_PATH` must point to an existing database; the production guard refuses
to create a replacement if that path is missing.
Expand Down Expand Up @@ -81,7 +88,7 @@ that sends them; with `ORIGIN` the app supplies them itself.
| `PORT` | No | `3000` | Listen port |
| `ORIGIN` | Plain HTTP: **yes** | unset | The address people open in the browser, for example `http://192.168.1.10:3000`. Leave unset only behind an HTTPS reverse proxy that passes the original `Host`; see [Public origin](#public-origin-origin). Always allowed by the app's origin check; saved allowed origins add to it |
| `IDLE_TIMEOUT` | No | `10` | Seconds before an idle client connection closes (0–255; `0` disables). Mapped to the adapter's `CONNECTION_IDLE_TIMEOUT`, which wins if both are set. Event streams are exempt |
| `SHUTDOWN_TIMEOUT` | No | `30` | Seconds to drain in-flight requests on `SIGTERM`/`SIGINT` before closing them; with `ORIGIN` set, also on `SIGHUP` (closing the terminal), and the process exits at that deadline even if the app still has work in flight. A second `SIGTERM`/`SIGINT` exits at once; a repeated `SIGHUP` does not |
| `SHUTDOWN_TIMEOUT` | No | `30` | Seconds to drain in-flight requests on `SIGTERM`/`SIGINT` before closing them; with `ORIGIN` set, also on `SIGHUP` (closing the terminal), and the process exits at that deadline even if the app still has work in flight. With `NODE_ENV=production bun scripts/serve.ts`, a second `SIGTERM`/`SIGINT` exits at once (under `bun run start` the first Ctrl+C already does); a repeated `SIGHUP` does not |
| `BODY_SIZE_LIMIT` | No | `512K` | Maximum request body (`K`/`M`/`G` suffixes, `Infinity` to disable) |
| `ADDRESS_HEADER`, `XFF_DEPTH` | No | unset, `1` | Behind a reverse proxy, only when every request goes through it: `ADDRESS_HEADER=x-forwarded-for`, with `XFF_DEPTH` the number of proxies (the client address is read that many hops from the right). Used for rate limiting; without it the TCP peer is used |
| `PROTOCOL_HEADER`, `HOST_HEADER` | No | unset | Only without `ORIGIN`, behind a trusted proxy: the headers carrying the public scheme (`http`/`https`, for example `x-forwarded-proto`) and host. With `ORIGIN` set the app supplies them itself |
Expand Down