Skip to content

@ts-pf/server-http

FetchHandler and HTTP HandlerPlugins (CORSPlugin, RequestLimitPlugin, header plugins). Calls lookupProcedure + runProcedure from @ts-pf/server. Analog of @ts-pf/message-server.

Agent skill: skills/ts-pf-server-http/. Sync with npx skills experimental_sync -y.

plugins is HTTP-only. RPC call interceptors (CallInterceptor from @ts-pf/server) go on { interceptors } — use applyPlugins to install CallPlugins such as DedupePlugin.

import { applyPlugins, DedupePlugin } from '@ts-pf/server'
import { CORSPlugin, FetchHandler } from '@ts-pf/server-http'
const handler = new FetchHandler(app, {
plugins: [new CORSPlugin()],
// Default key dedupes every unary call, writes included. Pass key to restrict to reads.
interceptors: applyPlugins([new DedupePlugin({ key: readKey })]),
})
const result = await handler.handle(request, { prefix: '/rpc', context: { db } })

result.matched is false only when the path is outside prefix: no plugin or context factory ran and the body is unread, so the same request can go to another handler (several handlers on different prefixes). Inside the prefix every request is matched: true, 404 / 405 / errors included. context may be a factory (req) => TCtx | Promise<TCtx>; it runs after the body decode, and a PFError it throws is encoded like a handler error.

Options: codec, plugins, interceptors, validation, and onError(error, { request, path, context }) (type FetchHandlerOnError). Without onError, unknown throws and INTERNAL errors are logged with console.error; passing it replaces that logging, and isUnexpectedError from @ts-pf/protocol keeps the same filter: onError: (e) => { if (isUnexpectedError(e)) logger.error(e) }. Mid-stream errors are in-band codec items and reach neither. A JSON envelope object with keys but no input key is 400 BAD_REQUEST from the codec; {} and {"input":null} still mean no input. A request x-ts-pf-protocol header that is present and not 1 is 400 BAD_REQUEST Unsupported protocol version before the body is read; a missing header is accepted. A procedure whose contract input or output is stream() on a codec with streams: false (the default JsonCodec, MultipartCodec) is 400 BAD_REQUEST Stream procedure requires a streaming codec, also before the body is read and before context, middleware, or the handler run; pass StreamCodec / SseCodec to serve it. A handler that returns an AsyncIterable the codec does not encode as a stream gets the same answer, and the iterator is closed. RequestLimitPlugin rewraps any request with a body, so the context factory and plugin hooks see a plain Request; read host extras (srvx request.ip) from the request your host passed.

A HandlerPlugin.onRequest that throws answers like any protocol error: the throw takes the normal error path (HandlerPlugin.onError, the onError option or the default log, the JSON envelope with x-ts-pf-protocol and httpStatus(error), then onResponse); a non-PFError throw is 500 INTERNAL and logged by default. There is no filter option; to keep a procedure off HTTP while another transport still serves it, throw NOT_FOUND:

import { PFError } from '@ts-pf/protocol'
import { FetchHandler, type HandlerPlugin } from '@ts-pf/server-http'
// `admin.*` is served over a message transport only; over HTTP it answers NOT_FOUND.
const hideAdmin: HandlerPlugin = {
name: 'hide-admin',
onRequest({ request }) {
if (new URL(request.url).pathname.startsWith('/rpc/admin/')) {
throw new PFError({ code: 'NOT_FOUND', message: 'Procedure not found' })
}
return undefined
},
}
new FetchHandler(app, { plugins: [hideAdmin] })

A custom HandlerPlugin that returns a new request builds it with new Request(request.url, { method, headers, signal, body, duplex: 'half' }) (cast the init as RequestInit & { duplex: 'half' } under DOM types), not new Request(request, init), which throws on srvx’s Node request. Always pass signal: request.signal. FetchHandler keeps each request you return, and the one it replaced, alive while a streamed response is; in Node, a collected request loses its abort. Don’t return a bare request.clone() (Node can lose its abort anyway), and if a hook builds more than one request, build each from the original request: one you don’t return is not kept.

Do not import this package from @ts-pf/client-http.

  • FetchHandler, FetchHandlerOnError, HandleResult, HandlerPlugin
  • CORSPlugin, RequestLimitPlugin, RequestHeadersPlugin, ResponseHeadersPlugin