@ts-pf/server-http 0.7.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,7 +18,7 @@ const handler = new FetchHandler(app, {
18
18
  const result = await handler.handle(request, { prefix: '/rpc', context: { db } })
19
19
  ```
20
20
 
21
- 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.
21
+ 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.
22
22
 
23
23
  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`:
24
24
 
package/dist/handler.js CHANGED
@@ -1,4 +1,4 @@
1
- import { httpStatus, JSONCodec, PROTOCOL_HEADER, PROTOCOL_HTTP_STATUS, parseProcedurePath, } from '@ts-pf/http';
1
+ import { httpStatus, JsonCodec, PROTOCOL_HEADER, PROTOCOL_HTTP_STATUS, parseProcedurePath, } from '@ts-pf/http';
2
2
  import { isAsyncIterable, isPFError, isUnexpectedError, PFError, PROTOCOL_VERSION, toWireError, } from '@ts-pf/protocol';
3
3
  import { lookupProcedure, runProcedure, } from '@ts-pf/server';
4
4
  import { closeUnsentStream, isStreamProcedure, streamCodecError, } from './streams.js';
@@ -22,7 +22,7 @@ export class FetchHandler {
22
22
  carriesStreams;
23
23
  constructor(router, options) {
24
24
  this.router = router;
25
- this.codec = options?.codec ?? new JSONCodec();
25
+ this.codec = options?.codec ?? new JsonCodec();
26
26
  this.plugins = options?.plugins ?? [];
27
27
  this.interceptors = options?.interceptors ?? [];
28
28
  this.validation = options?.validation;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ts-pf/server-http",
3
- "version": "0.7.0",
3
+ "version": "1.0.0",
4
4
  "description": "Fetch Request/Response handler and HTTP HandlerPlugins",
5
5
  "keywords": [
6
6
  "ts-pf",
@@ -50,12 +50,12 @@
50
50
  "lint": "biome check src tests"
51
51
  },
52
52
  "dependencies": {
53
- "@ts-pf/http": "0.7.0",
54
- "@ts-pf/protocol": "0.7.0",
55
- "@ts-pf/server": "0.7.0"
53
+ "@ts-pf/http": "^1.0.0",
54
+ "@ts-pf/protocol": "^1.0.0",
55
+ "@ts-pf/server": "^1.0.0"
56
56
  },
57
57
  "devDependencies": {
58
- "@ts-pf/contract": "0.7.0",
58
+ "@ts-pf/contract": "^1.0.0",
59
59
  "zod": "^4.0.0"
60
60
  }
61
61
  }
@@ -65,7 +65,7 @@ new FetchHandler(app, {
65
65
 
66
66
  Errors thrown after a streamed response has started (`StreamCodec` / `SseCodec` output) are written in-band by the codec as `INTERNAL` items; they do not reach `onError` or the default log.
67
67
 
68
- `FetchHandler` checks the method, then the protocol version, then looks the procedure up, all before it reads the body. A non-POST is `405 METHOD_NOT_ALLOWED`. A request whose `x-ts-pf-protocol` header is present and not `1` (surrounding whitespace ignored) is `400 BAD_REQUEST` `Unsupported protocol version`; a missing header is accepted, so curl and non-ts-pf clients work as before. An unknown path under the prefix is `404 NOT_FOUND`. A procedure whose contract input or output is `stream()` on a codec that declares `streams: false` (the default `JSONCodec`, `MultipartCodec`) is `400 BAD_REQUEST` `Stream procedure requires a streaming codec` — use `StreamCodec` / `SseCodec`. In all four cases the body is never buffered or decoded, whatever it holds (invalid JSON, a multipart upload, an oversized stream). `RequestLimitPlugin`'s `Content-Length` check runs earlier, in `onRequest`, so a declared oversize body is still `413 PAYLOAD_TOO_LARGE` on any path and for any method; only its streamed-body cap is skipped on a 404 or a stream-codec 400 because nothing reads the stream.
68
+ `FetchHandler` checks the method, then the protocol version, then looks the procedure up, all before it reads the body. A non-POST is `405 METHOD_NOT_ALLOWED`. A request whose `x-ts-pf-protocol` header is present and not `1` (surrounding whitespace ignored) is `400 BAD_REQUEST` `Unsupported protocol version`; a missing header is accepted, so curl and non-ts-pf clients work as before. An unknown path under the prefix is `404 NOT_FOUND`. A procedure whose contract input or output is `stream()` on a codec that declares `streams: false` (the default `JsonCodec`, `MultipartCodec`) is `400 BAD_REQUEST` `Stream procedure requires a streaming codec` — use `StreamCodec` / `SseCodec`. In all four cases the body is never buffered or decoded, whatever it holds (invalid JSON, a multipart upload, an oversized stream). `RequestLimitPlugin`'s `Content-Length` check runs earlier, in `onRequest`, so a declared oversize body is still `413 PAYLOAD_TOO_LARGE` on any path and for any method; only its streamed-body cap is skipped on a 404 or a stream-codec 400 because nothing reads the stream.
69
69
 
70
70
  A `HandlerPlugin.onRequest` that throws a `PFError` is answered through the same error path (plugin `onError`, the `onError` option or default log, envelope + `x-ts-pf-protocol` + `httpStatus(error)`, `onResponse`); a non-`PFError` throw is `500 INTERNAL` (logged by default). It runs before the method check, protocol check, and lookup, on every request under the prefix, so the plugin's code wins for every method. There is no `filter` option — to keep a procedure off HTTP, throw `NOT_FOUND` for its path:
71
71
 
@@ -105,7 +105,7 @@ new FetchHandler(app, {
105
105
  })
106
106
  ```
107
107
 
108
- `GetBridgePlugin` (`examples/plugins/src/get-bridge.ts`, smoke-tested): `lookupProcedure(app, path)` → contract meta `http.method === 'GET'` → `new Request(url, { method: 'POST', headers, body: JSON.stringify({ input: JSON.parse(q) }), signal })`; `onResponse` sets the meta `Cache-Control` on success unless the call already set one, and `no-store` on failures and on any response with `Set-Cookie`. A GET to a procedure that did not opt in falls through to `405 METHOD_NOT_ALLOWED`. The client half is a Fetch `Interceptor` on `FetchLink` that resends the JSON POST for listed paths as a GET with sorted keys and keeps POST above ~8 KB of URL (`examples/plugins/src/get-link.ts`). The GET drops `x-ts-pf-protocol` (absent is accepted) so a cross-origin read stays a simple request.
108
+ `GetBridgePlugin` (`examples/plugins/src/get-bridge.ts`, smoke-tested): `lookupProcedure(app, path)` → contract meta `http.method === 'GET'` → `new Request(url, { method: 'POST', headers, body: JSON.stringify({ input: JSON.parse(q) }), signal })`; `onResponse` sets the meta `Cache-Control` on success unless the call already set one, and `no-store` on failures and on any response with `Set-Cookie`. A GET to a procedure that did not opt in falls through to `405 METHOD_NOT_ALLOWED`. The client half is a `FetchInterceptor` on `FetchLink` that resends the JSON POST for listed paths as a GET with sorted keys and keeps POST above ~8 KB of URL (`examples/plugins/src/get-link.ts`). The GET drops `x-ts-pf-protocol` (absent is accepted) so a cross-origin read stays a simple request.
109
109
 
110
110
  - `public` only for data that is the same for every caller. A shared-cache hit never reaches the server, so auth middleware does not run for later callers. Per-user reads use `private` (browser cache only). A per-call `Cache-Control` / `Vary` from `ResponseHeadersPlugin` (`context.resHeaders`) wins over the meta default.
111
111
  - A GET runs from a plain link, cookies included: never opt in a procedure that writes.
@@ -143,7 +143,7 @@ export async function getPlanet(id: string): Promise<Response> {
143
143
 
144
144
  - App: `ts-pf-server`
145
145
  - Client: `ts-pf-client-http`
146
- - Codecs: `ts-pf-file` / `ts-pf-stream` / `ts-pf-sse` via `{ codec }`
146
+ - Codecs: `ts-pf-file` / `ts-pf-stream` (JSONL and SSE) via `{ codec }`
147
147
 
148
148
  ## Don't
149
149
 
@@ -151,4 +151,4 @@ export async function getPlanet(id: string): Promise<Response> {
151
151
  - Bridge GET for a procedure with side effects, or mark per-user data `public`.
152
152
  - Port `HandlerPlugin` onto WS / stdio / MessagePort.
153
153
  - Put file size caps here — those are `MultipartCodec` options.
154
- - Serve `stream()` procedures through the default `JSONCodec` — they answer `400 BAD_REQUEST`.
154
+ - Serve `stream()` procedures through the default `JsonCodec` — they answer `400 BAD_REQUEST`.