Skip to main content

@flow-state-dev/node

Interfaces​

NetworkBindGuardOptions​

Options for assertNetworkBindIsAuthenticated.

Properties​

allowUnauthenticated?​
allowUnauthenticated?: boolean;

Skip the guard (e.g. the operator authenticates via a path the rail can't see).

host​
host: string;

The host the server is about to bind.


PageHtmlOptions​

Options for createPageHtmlTransform.

Properties​

devtoolConfig?​
devtoolConfig?: DevToolConnectionConfig;

DevTool connection config (userId / bearer token). Loopback hosts only.

host​
host: string;

The host the server binds. The DevTool config is written only for a loopback host.

pageMeta?​
pageMeta?: Record<string, string>;

<meta name content> tags, written on any host. Not for secrets.

pageScript?​
pageScript?: string;

Inline script body, written after the meta tags on any host. Not for secrets.


ServeHandle​

Handle returned by serve for lifecycle control.

Properties​

port​
readonly port: number;

The bound port (resolved, useful when binding port 0).

server​
readonly server: Server;

The underlying node:http server.

Methods​

close()​
close(): Promise<void>;

Stop accepting connections, drain in-flight requests (force-closing after the grace window), then dispose the router and the FlowState (unless disposeOnClose is false). Idempotent.

Returns​

Promise<void>


ServeOptions​

Options for serve. All have sensible defaults for PaaS hosting.

Properties​

allowUnauthenticated?​
allowUnauthenticated?: boolean;

Explicitly permit default-resolver flows on a network interface.

basePath?​
basePath?: string;

API mount prefix. Default "/api/flows" (matches createFlowApiRouter).

devtoolConfig?​
devtoolConfig?: DevToolConnectionConfig;

DevTool connection config to inject into the served index.html as window.__FSD_DEVTOOL_CONFIG__ (userId / bearer token). Set only by fsdev dev from the app's fsdev.config.ts; the token is exposed only to the loopback page this dev server serves. Omit for production serving.

disposeOnClose?​
disposeOnClose?: boolean;

Whether close() disposes the router and the FlowState after draining. Default true. Set false when several servers share one runtime, so the caller disposes it once, after every server has drained.

handleSignals?​
handleSignals?: boolean;

Whether serve installs its own SIGTERM/SIGINT handlers (which call close()). Default true. Set false when the caller runs its own signal handling and drives handle.close() itself, so teardown lives in one path.

healthPath?​
healthPath?: string;

Health endpoint path. Default "/healthz". 200 once ready, 503 before.

host?​
host?: string;

Host to bind. Default "0.0.0.0" (required for most PaaS).

pageHandler?​
pageHandler?: PageHandler;

A Connect-style (req, res, next) handler for non-API GET requests, such as a dev server's middleware. Tried after a real file in staticDir and after a dedicated adapter route, before the SPA fallback (a 404 without staticDir). It answers by writing res, or passes with next(); next(err) is a 500. A handler that renders its own HTML applies createPageHtmlTransform with the same options to match staticDir.

pageMeta?​
pageMeta?: Record<string, string>;

Extra <meta name content> tags written into every HTML page served from staticDir (each .html file and the SPA fallback), on any host. For values a page reads on boot, such as another server's address. Not for secrets: unlike devtoolConfig, these are written on a network bind too.

pageScript?​
pageScript?: string;

An inline script written into every HTML page served from staticDir, after the pageMeta tags so it can read them, on any host. For a dev server's page behaviour, such as reloading when the server restarts. Not for secrets. A </ in it is escaped.

port?​
port?: number;

Port to bind. Default: process.env.PORT, then 3000.

push?​
push?: boolean;

Whether to serve push delivery: the WebSocket a client's client.subscribe hears change notices on, at <basePath>/users/:userId/stream. Default true. With false, GET /capabilities reports push: null and clients read on their timer.

pushSecret?​
pushSecret?: string | (() => string);

The key push tickets are signed with. Needed only when several processes serve one address: give every one the same key, so a ticket minted on one is accepted by another. Default: process.env.FSD_PUSH_SECRET, else a random key for this process.

shutdownGraceMs?​
shutdownGraceMs?: number;

SIGTERM/SIGINT grace window (ms) before connections are force-closed. Default 10000.

staticDir?​
staticDir?: string;

Static asset dir served for non-API routes, with index.html SPA fallback.

Type Aliases​

PageHandler​

type PageHandler = (req, res, next) => void;

A Connect-style request handler: answer by writing res, or call next() to pass.

Parameters​

req​

IncomingMessage

res​

ServerResponse

next​

(err?) => void

Returns​

void

Functions​

assertNetworkBindIsAuthenticated()​

function assertNetworkBindIsAuthenticated(app, opts): Promise<void>;

Refuse to bind a network-exposed host when any served flow would run unauthenticated — i.e. its effective principal resolver is the framework default defaultBodyUserIdPrincipalResolver. Loopback binds and allowUnauthenticated short-circuit before any runtime resolution.

Resolves the router (getRouter() is memoized, so the serve() that follows reuses it rather than re-initializing stores) and inspects the resolver each registered flow would get.

Because the caller awaits this before binding, store initialization happens before the port is listening on a non-loopback host — this forgoes serve()'s bind-immediately-then-/healthz-503 cold start for that window, in exchange for failing fast when the app can't start (a deliberate trade for a production command). The precise, init-free alternative is to surface the unauthenticated flow set on FlowState.meta framework-side, so the guard reads a declared set instead of resolving the runtime — not done here because it moves a host-level concern into the framework's public metadata.

Parameters​

app​

FlowState<FlowStateSettings> | FlowApiRouter

opts​

NetworkBindGuardOptions

Returns​

Promise<void>

Throws​

Error naming the unauthenticated flow kinds when host is non-loopback, allowUnauthenticated is not set, and at least one served flow is on the default resolver.


createPageHtmlTransform()​

function createPageHtmlTransform(options): ((html) => string) | undefined;

Build the transform a host applies to each HTML page it serves, or undefined when there is nothing to write: no pageMeta entries, no pageScript, and no devtoolConfig or a non-loopback host. A host that gets undefined serves its HTML byte for byte.

The meta tags go just before </head>, then the page script (so it can read them), then the config script. Meta names and values are attribute-escaped; a </ in the script is written </ so it can't close the element.

Parameters​

options​

PageHtmlOptions

Returns​

((html) => string) | undefined


isLoopbackHost()​

function isLoopbackHost(host): boolean;

Whether host is a loopback interface (127.0.0.0/8, ::1, localhost), reachable only from the same machine and therefore safe to serve without authentication. Any other host is network-exposed.

Parameters​

host​

string

Returns​

boolean


serve()​

function serve(app, options?): Promise<ServeHandle>;

Stand up a long-lived HTTP server for app and resolve once it is listening.

Network binds await the authentication guard before listening, and the same guard runs on each request so a flow registered afterwards is refused. Loopback binds and explicit opt-outs bind immediately and serve /healthz (503 until the router's stores finish initializing, 200 after), so PaaS health checks pass during a cold start. Pass a FlowState to have close() dispose its stores; pass a raw FlowApiRouter to manage store lifecycle yourself.

Parameters​

app​

FlowState<FlowStateSettings> | FlowApiRouter

options?​

ServeOptions

Returns​

Promise<ServeHandle>

References​

createServerApp​

Re-exports createServerApp


ServerApp​

Re-exports ServerApp


ServerAppOptions​

Re-exports ServerAppOptions