Skip to content

@xtandard/kit/server

@xtandard/kit/server — the HTTP side of a panel: createPanelHandler (healthcheck, API, OpenAPI, static UI, SPA fallback, CORS, close()), the index.html renderer, base-path and CORS helpers, and the plumbing for product API routers (json, error, match, intParam, mapError, authenticate with the scoped-token short-circuit, authorize).

node:fs is only loaded when static files are served, so the panel API runs on Workers too.

import { … } from "@xtandard/kit/server";
Name Summary
applyCorsHeaders Attach Access-Control-* headers to response for an allowed cross-origin request.
authenticate Authenticate a request.
authorize Authorize an authenticated request. A scoped principal is decided by scope alone — the host’s provider is not consulted (defence in depth: a permissive host policy can’t widen a token).
createPanelHandler Build the panel handler.
error A JSON error response: { error: message, ...extra }.
intervalTask A PanelTask that runs run every every (a Duration). The timer is unref()ed so it never keeps the process alive; a run that rejects is reported, never thrown. every <= 0 makes a task that does nothing.
intParam Parse an integer query param; undefined when absent or empty.
json A JSON response.
looksLikeAsset True if the path has a file extension (so a miss should 404 rather than fall through to the SPA).
mapError Map an error to a response:
match Match pattern (with :name segments) against path; returns the decoded params, or null. A malformed % escape is no match (404), never a thrown URIError (500).
methodNotAllowed 405 with the Allow header listing the methods the path supports.
noContent An empty 204.
normalizeBasePath Normalize a base path to either "" (root) or /segment with no trailing slash.
preflightResponse Build the 204 response for a CORS preflight (OPTIONS).
renderIndexHtml Read, inject into, and return the SPA index HTML from uiDir, or the fallback page when uiDir is undefined or has no index.html.
requireOneOf Throw a clear error when none of keys is set on options — the “requires storage or a prebuilt core” guard every product’s panel needs.
resolveUiDir Locate a product’s built SPA. Pass the product module’s import.meta.url: compiled (the npm-consumer case) the bundle is the sibling ./ui of the module’s dist/ chunk; run from TypeScript source (examples, file: links) it is <repo>/dist/ui. Falls back to the compiled-layout path so the “build the UI” hint still fires when nothing is built yet.
serveStaticAsset Try to serve path (base-path-stripped, leading /) as a static file from uiDir. Returns a Response, or null if the file does not exist or the path escapes uiDir. Files under assets/ (content-hashed by the bundler) are cached for a year; everything else is no-cache.
stripBasePath Strip the (normalized) base path from a pathname. Returns a path that always starts with /. If the pathname does not start with the base path it is returned unchanged (the host framework may already have stripped the mount).
Name Summary
HttpError Throw inside a router to short-circuit with error(status, message, extra).
Name Summary
Authenticated The outcome of authenticate.
AuthenticateOptions Options for authenticate.
AuthorizeOptions Options for authorize.
BootstrapConfig Bootstrap config injected into the page. Products add their own fields.
CorsOptions CORS configuration for the panel handler.
IntervalTaskOptions Options for intervalTask.
IntParamOptions Options for intParam.
PanelHandler What createPanelHandler returns.
PanelOptions Options for createPanelHandler.
PanelTask Background work owned by a panel (a dispatcher, a sweep timer…).
RenderIndexHtmlOptions Options for renderIndexHtml.
ScopedAuth How a product verifies its scoped (portal / viewer) tokens.
Name Summary
ErrorMap Domain errors by name → response. Names, not classes, so an error thrown from another bundle’s copy of the class still maps (see isNamed).
ErrorMapping How one named domain error maps to a response.
Name Summary
JSON_CONTENT_TYPE Content type of every JSON response.