Storage and host handler
Storage ports, adapters and the HTTP routes. Server and host integration in the DocStudio developer reference.
Public from docstudio/server (Node only): createMemoryStorage, createFileStorage, createHostHandler, assetResolverFor, resolverForDocument, DEFAULT_STORAGE_LIMITS, DEFAULT_HOST_LIMITS and the structural types. Decisions: ADR 0032 and 0033.
Storage ports
Storage groups three asynchronous ports. Every method returns { ok: true, value } or { ok: false, code, message } and never throws for caller errors. Codes: INVALID_ID, INVALID_VALUE, NOT_FOUND, CONFLICT, ALREADY_EXISTS, TOO_LARGE, LIMIT_EXCEEDED, IO_ERROR.
| Port | Operations | Rules |
|---|---|---|
TemplateStore |
list, get, save, delete |
save(id, template, expectedRevision): 0 creates; a stale revision is CONFLICT; the template must be JSON and within the size limit |
ComponentStorage |
list, get, publish |
Versions are immutable: republishing a version is ALREADY_EXISTS; there is no update or delete; the hash is the core content hash |
AssetStore |
list, get, put, delete |
PNG, JPEG, GIF or WebP only; the bytes must match the declared type; empty and oversized assets are refused |
Identifiers match [A-Za-z0-9][A-Za-z0-9._-]{0,127}. Returned values are copies. Defaults: 5 MB per template, 5 MB per asset, 1,000 characters of notes, 10,000 items.
Adapters
createMemoryStorage({ limits?, now? })keeps everything in the process.await createFileStorage({ directory, limits? })keepstemplates/<id>.json,components/<name>/<version>.jsonandassets/<id>.binwith<id>.jsonmetadata. Writes go to an exclusive temporary file and are renamed into place, so a failed write leaves the previous value. Saves for one id are serialized inside the process. Symbolic links are never followed and a store whose subdirectory is a link is refused. Several processes must not share one directory.
Both adapters pass describeStorageConformance (src/server/storage-conformance.fixture.ts), which a host can run against its own adapter.
Host handler
createHostHandler({ storage, pdf, authorize?, ready?, limits? }) returns a Node request listener typed structurally. authorize(request) runs before every /api route; a false result is 401 with no body, and a throwing authorizer is a generic 500.
| Route | Behaviour |
|---|---|
GET /healthz, GET /readyz |
Liveness, and readiness from the ready callback (503 when false). No authorization |
GET /api/templates, GET, PUT, DELETE /api/templates/:id |
PUT needs content-type: application/json and if-match: <revision> (0 to create); the template is validated |
GET, POST /api/components, GET /api/components/:name/:version |
POST takes { name, version, notes, template }, validates the component and answers 201; republish is 409 |
GET /api/assets, GET, PUT, DELETE /api/assets/:id |
PUT takes the raw bytes with the image media type; GET serves the exact bytes with nosniff and a sandbox policy |
POST /api/pdf |
Takes { templateId, data?, options? }, loads the template and the pinned components it needs (bounded depth and count), compiles, resolves the stored assets it names and returns the PDF |
Statuses: INVALID_ID, INVALID_VALUE and bad JSON 400; NOT_FOUND 404; CONFLICT and ALREADY_EXISTS 409; wrong method 405; wrong content type 415; TOO_LARGE 413; LIMIT_EXCEEDED 429; IO_ERROR 500. Error bodies are { ok: false, error: { code, message }, diagnostics? } with fixed messages; storage messages, paths and document content are never included. Every response carries cache-control: no-store and x-content-type-options: nosniff.
The PDF service accepts an optional second argument to render, an AssetResolver for that request only, which is how stored assets reach the renderer without a global cache.
Authentication, TLS, CORS and rate limiting per client are the host's responsibility.