Linora

Public API and API service (apps/api)

Owner: WP-C. ADRs 0022 (API-first), 0011 (auth), 0023 (security), 0021 (topology), 0301-0305 (service design). Contracts: shared/src/api.ts (route table + schemas), schema.ts, auth-token.ts, realtime half in messages.ts/codec.ts. Generated spec: docs/api/openapi.json.

Current (wave 1)

Every route in API is implemented.

The API

One versioned surface, /v1, used by the web app, the MCP adapter, scripts and future integrations alike.

Area Routes (see API in shared/src/api.ts) Tier
Auth signup, login, logout, me, realtimeToken api
Sessions listSessions, revokeSession, revokeOtherSessions (browser session only; ADR 0307) api
Tokens listTokens, createToken (scopes, project allow-list, expiry), revokeToken api
Projects & members list/create/update/delete project, list/add/update/remove member, listAudit api
Files & pages list/create/get/update/delete file, allocClientId, create/update/delete page api
Content snapshot (gzip binary), applyOps (JSON or application/vnd.linora.ops) api / realtime
Queries summary, outline, getNodes, searchNodes, changes, render (PNG/SVG) docworker
Realtime WS /v1/realtime?file= : ops, ACK verdicts, broadcasts, presence realtime

Conventions:

Client rules (web app, MCP, scripts)

The op pipeline (one, whatever the transport)

POST /v1/files/:fileId/pages/:pageId/ops:

  1. Authenticate (session + CSRF, or PAT) and authorize (files:write scope, editor role on the file’s project). The page must belong to the file (cached page -> file map).
  2. Convert JSON ops to the binary op list (@linorahq/doc/json + the shared codec); a binary body is passed through byte for byte.
  3. Submit over a pooled internal realtime connection to the owning instance (ADR 0303).
    • The connection is authenticated with a realtime token minted for the calling user: same claims as a browser token; kind: 'agent' and scp for PATs.
    • It subscribes pages with mode: 'write': no tail, broadcasts or presence, invisible to peers.
  4. The realtime gateway runs the same stages as for any socket: role/scope, ScanBatch, owner StructIndex.check, sequence, journal, broadcast.
  5. Respond with {seq, applied, rejected: [{index, reason, hint}]} after the ACK.
    • reason is the Reject name (NODE_MISSING, …). hint is an op-aware fix suggestion from @linorahq/doc/json, the same text the MCP shows.
    • Realtime errors map to HTTP: busy -> 503 + Retry-After, rate_limited -> 429, read_only -> 403, protocol_error -> 400, too_large -> 413.

Service internals

apps/api/src/
  main.ts            process entry: config (refuses to start without secrets), /v1 server, metrics + health port, drain
  server.ts          createApi: App wiring, node:http server, security/CORS headers, error mapping, timers
  router.ts          route table -> matcher; pipeline: CORS -> IP limit -> authn -> session-only -> CSRF ->
                     user/PAT limits -> scope -> tenant/role -> body/query limits + schema -> handler
  context.ts         App (deps), Ctx (per request), authorize() (tenant isolation)
  config.ts          API env (ports, origins, cookies, limits, realtime/doc-worker URLs)
  http.ts            HttpError, cookies, bounded body reader + process-wide body budget, query coercion
  access.ts          membership/file/page caches (TTL, invalidated by linora_auth)
  ratelimit.ts       token buckets + global sync (RateSync), in-flight gate, TTL cache (bounded)
  events.ts          NOTIFY senders (in-transaction) + linora_auth listener (cache invalidation)
  audit.ts           audit_log writes, rate-aggregated login failures
  doc-interop.ts     the only @linorahq/doc bridge: JSON -> op list, page keys, empty snapshot, reject names/hints
  auth/              password.ts (argon2id, capped), credentials.ts (sessions, PATs, caches),
                     realtime-token.ts (claims + minting)
  routes/            auth, sessions (list/revoke own), tokens, projects (+members, audit), files (+pages)
  content/           snapshot.ts (streaming), ops.ts (applyOps), realtime-link.ts (writer-mode WS pool)
  queries/           routes.ts (proxy handlers), docworker.ts (rendezvous hash, svc token, streaming)

Token keys (ADR 0310): the API holds rt (user realtime tokens) and svc:api (doc-worker calls): LINORA_TOKEN_KEYS_RT + LINORA_TOKEN_KEYS_SVC_API, or the dev master LINORA_TOKEN_KEYS. Database: the linora_api role.

Env: LINORA_API_PORT (8787), LINORA_API_HOST, LINORA_ALLOWED_ORIGINS (normalised like an Origin header: lower-case, no default port), LINORA_COOKIE_SECURE, LINORA_COOKIE_INSECURE_ORIGINS (plain-HTTP, non-loopback origins that are also allowed; ADR 0905), LINORA_REALTIME_URL (public base in RealtimeTokenRes.url), LINORA_REALTIME_SAME_ORIGIN (1 = a token requested from an allowed origin carries that origin’s own ws(s) URL; others get LINORA_REALTIME_URL; ADR 0905), LINORA_REALTIME_INTERNAL_URL (default = public), LINORA_DOCWORKER_URLS, LINORA_DOCWORKER_LARGE_URLS, LINORA_DOCWORKER_LARGE_PAGE_BYTES (1 GiB), LINORA_TRUSTED_PROXIES (IPs/CIDRs whose X-Forwarded-For is honoured), LINORA_API_RATE_STORE (pg), LINORA_API_RATE_SYNC_MS (250), LINORA_API_DB_POOL (16), LINORA_API_RATE_{IP,USER,TOKEN}_{PER_SEC,BURST}, LINORA_API_AUTH_BURST, LINORA_ARGON2_CONCURRENCY, LINORA_ARGON2_QUEUE, LINORA_API_BODY_BUDGET, LINORA_API_CACHE_TTL_MS, LINORA_API_CACHE_MAX, LINORA_API_METRICS_PORT.

How it protects the targets

Acceptance (WP-C)