> ## Documentation Index
> Fetch the complete documentation index at: https://fairyfly.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Architecture

> Layers from QuickJS down to libxev, where each subsystem lives, and how the hot path stays allocation-free.

One binary, four layers, no plugins. Understanding the shape of the
codebase makes the rest of the documentation predictable.

## The layers

```
┌──────────────────────────────────────────────────┐
│ JavaScript source (.js files)                    │
└──────────────────┬───────────────────────────────┘
                   │ QuickJS parser
┌──────────────────▼───────────────────────────────┐
│ QuickJS bytecode + JS runtime (gc, value stack)  │
└──────────────────┬───────────────────────────────┘
                   │ C ABI (src/c.zig → quickjs_shim.zig)
┌──────────────────▼───────────────────────────────┐
│ Zig API layer                                    │
│   api/     console, fs, process, crypto, url,    │
│            fetch, websocket, sqlite, sql, ffi    │
│   net/     http_native (512-slot server),        │
│            http2_server, pg_client, async_fetch  │
│   event/   loop, timers, microtasks              │
│   types/   Headers, Request, Response, Blob,     │
│            FormData, PoolSlice                   │
│   worker/  worker, message_port, serialize       │
│   commands/ init, imprint, sever, start, test,   │
│            repl, compile, fmt, upgrade, watch    │
└──────────────────┬───────────────────────────────┘
                   │
┌──────────────────▼───────────────────────────────┐
│ libxev event loop (io_uring / epoll, kqueue)     │
└──────────────────────────────────────────────────┘
```

## Where things live

| Directory | Contents | Read when |
| - | - | - |
| `src/api/` | Every JS global | Looking up a global's behaviour |
| `src/crypto/` | Dedicated `crypto.bcrypt` worker pool (`bcrypt_pool.zig`) | Password hashing pool and job slots |
| `src/net/` | HTTP server, HTTP/2, fetch, Postgres, TLS, WebSocket | Networking limits or protocol details |
| `src/security/` | Permission policy (`permissions.zig`, `deny.zig`) | `--allow-*` flag parsing and scope checks |
| `src/event/` | Loop, timers, microtasks | Ordering or liveness questions |
| `src/types/` | `Headers`, `Request`, `Response`, `Blob`, `FormData` | Fetch API semantics |
| `src/worker/` | Workers + structured clone | Worker limits |
| `src/commands/` | CLI subcommands | CLI flags |
| `src/modules/` | ESM resolver | Import resolution rules |
| `vendor/` | QuickJS, BearSSL, SQLite, nghttp2 (gitignored) | Build problems |

## Threads

| Thread | Count | Work |
| - | - | - |
| JS thread | 1 | All JavaScript, all Promise settlements |
| Event-loop pool (`libxev`) | 4 | General loop work |
| `fs.*Async` pool | 4 + 16 job slots | File I/O; a 17th concurrent job waits |
| `crypto.bcrypt` pool | 2–4 (`min(4, max(2, cpu - 1))`) + 16 job slots | Off-loop password hashing; a 17th concurrent job rejects immediately |
| `ffi` pool | 4 | Each native call parks a pool thread |
| `fetch` workers | 16 (1 MB stack each) | One thread per slot; outbound connections |
| Postgres / TLS | none | Event-driven on the loop thread |
| `Worker` | up to 8 | Your CPU-bound JS, each with its own runtime |

There is no single shared pool — loop, `fs`, and `ffi` each own a
separate 4-thread pool. See [Concurrency](https://fairyfly.dev/concepts/concurrency.md) for
the full inventory.

## Hot-path design

* **SoA connection table** — 512 slots stored as parallel arrays with packed
  2-byte flags (`ConnFlags` is a `packed struct(u16)`), so the hot loop
  touches cache lines, not objects.
* **Static per-slot buffers** — 4 KB request read buffer (spills to the
  heap up to 1 MB per request, retaining up to 64 KB per slot within a 64 MB
  global budget) and 64 KB response staging per connection (spills to the
  heap up to 10 MB, retaining up to 256 KB for reuse).
* **`HeadersData` refcount** — one heap object with an atomic refcount,
  shared by `Request` / `Response` / `Headers` across thread boundaries.
* **Zero-copy URLs** — one allocation per URL; components are pooled slices.
* **Worker messages** — one small allocation plus a `memcpy` per message.
* **Postgres arenas** — per-job arena with a 32 KB retain cap plus a job
  freelist, so a warm query path allocates nothing.

## Build options

```sh theme={null}
zig build -Doptimize=ReleaseFast   # what `make build` uses
zig build -Dbearssl=false          # drop TLS (HTTPS/WSS/Postgres TLS unavailable)
zig build -Dffi=false              # keep the `ffi` global, but `dlopen`/`callback` throw (needs `--allow-ffi` at runtime regardless)
zig build -Dio_uring=false         # Linux: use epoll instead of io_uring
zig build -Dstrip=false            # keep debug symbols in ReleaseFast/ReleaseSmall builds
zig build -Dsanitize-c=true        # enable C UBSan for debugging C sources
zig build -Dversion=1.2.3          # version reported by `ff --version`
```

| Vendor | Version | Role |
| - | - | - |
| quickjs-ng | 0.16.2 | JS engine |
| BearSSL | 0.6 | TLS 1.2 for HTTPS, WSS, and Postgres (`verify-full`) |
| SQLite | 3.53.4 | `Database` |
| nghttp2 | 1.70.0 | HTTP/2 framing + HPACK |

All four are fetched by `./scripts/fetch-vendors.sh` — they are not
committed. See [Installation](https://fairyfly.dev/getting-started/installation.md).

## I/O backends

| Platform | Backend |
| - | - |
| macOS | kqueue |
| Linux | io\_uring by default; `-Dio_uring=false` → epoll |
| Docker's default seccomp profile | blocks io\_uring — use `--security-opt seccomp=unconfined` or an epoll build |

## See also

* [Event loop](https://fairyfly.dev/concepts/event-loop.md)
* [Concurrency](https://fairyfly.dev/concepts/concurrency.md)
* [HTTP Server](https://fairyfly.dev/guides/http-server.md)
* [Installation](https://fairyfly.dev/getting-started/installation.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.