@ts-pf/server-http 0.3.2 → 0.3.4
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. 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.
|
|
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. 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. `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
|
Do not import this package from `@ts-pf/client-http`.
|
|
24
24
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-limit-plugin.d.ts","sourceRoot":"","sources":["../src/request-limit-plugin.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,MAAM,MAAM,yBAAyB,GAAG;IACtC,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAED,qBAAa,kBAAmB,YAAW,aAAa;IACtD,QAAQ,CAAC,IAAI,mBAAkB;IAC/B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAQ;gBAExB,OAAO,EAAE,yBAAyB;IAI9C,SAAS,CAAC,EAAE,OAAO,EAAE,EAAE;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,GAAG,SAAS;
|
|
1
|
+
{"version":3,"file":"request-limit-plugin.d.ts","sourceRoot":"","sources":["../src/request-limit-plugin.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,MAAM,MAAM,yBAAyB,GAAG;IACtC,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAED,qBAAa,kBAAmB,YAAW,aAAa;IACtD,QAAQ,CAAC,IAAI,mBAAkB;IAC/B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAQ;gBAExB,OAAO,EAAE,yBAAyB;IAI9C,SAAS,CAAC,EAAE,OAAO,EAAE,EAAE;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO,GAAG,SAAS;CAgClE"}
|
|
@@ -25,7 +25,12 @@ export class RequestLimitPlugin {
|
|
|
25
25
|
controller.enqueue(chunk);
|
|
26
26
|
},
|
|
27
27
|
}));
|
|
28
|
-
|
|
28
|
+
// Built from the URL, not `new Request(request, init)`: userland Node
|
|
29
|
+
// bridges (srvx) hand over Request-shaped objects that undici cannot clone.
|
|
30
|
+
return new Request(request.url, {
|
|
31
|
+
method: request.method,
|
|
32
|
+
headers: request.headers,
|
|
33
|
+
signal: request.signal,
|
|
29
34
|
body: limited,
|
|
30
35
|
duplex: 'half',
|
|
31
36
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-limit-plugin.js","sourceRoot":"","sources":["../src/request-limit-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAOzC,MAAM,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zB,OAAO,EAAE,wBAAwB;KAClC,CAAC,CAAA;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ts-pf/server-http",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
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.2.
|
|
54
|
-
"@ts-pf/protocol": "0.2.
|
|
55
|
-
"@ts-pf/server": "0.4.
|
|
53
|
+
"@ts-pf/http": "0.2.2",
|
|
54
|
+
"@ts-pf/protocol": "0.2.2",
|
|
55
|
+
"@ts-pf/server": "0.4.3"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@ts-pf/contract": "0.2.
|
|
58
|
+
"@ts-pf/contract": "0.2.3",
|
|
59
59
|
"zod": "^4.0.0"
|
|
60
60
|
}
|
|
61
61
|
}
|
|
@@ -40,6 +40,10 @@ return result.response
|
|
|
40
40
|
|
|
41
41
|
`plugins` is HTTP-only (`HandlerPlugin`). RPC call interceptors go on `{ interceptors }` from `@ts-pf/server`. `validation` is the same `ProcedureValidation` as `runProcedure`: input checks default on, output and stream output items run only with `{ validation: { output: true } }`. Set it on the constructor, not on `handle()`. `CORSPlugin` answers `OPTIONS` preflight.
|
|
42
42
|
|
|
43
|
+
`RequestLimitPlugin` rewraps any request with a body (to count its bytes), so the context factory, plugin hooks, and `onError` see a plain `Request`; bodyless requests keep your host's object. Read host extras such as srvx's `request.ip` / `request.runtime` from the request your host passed, typed as srvx's `ServerRequest`: `context: (req) => ({ req, ip: request.ip })`.
|
|
44
|
+
|
|
45
|
+
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.
|
|
46
|
+
|
|
43
47
|
## Errors
|
|
44
48
|
|
|
45
49
|
Unknown throws become `500 INTERNAL` with no stack on the wire. By default `FetchHandler` logs them with `console.error` (the original thrown value, tagged `[ts-pf/server-http]` with the procedure path). It also logs `INTERNAL` `PFError`s — an output schema failure under `validation.output`, invalid declared error `data`, or an explicit `INTERNAL` throw — naming those causes. Declared errors and every other protocol code (`VALIDATION`, `NOT_FOUND`, `BAD_REQUEST`, `METHOD_NOT_ALLOWED`, `PAYLOAD_TOO_LARGE`) are expected and are not logged.
|
|
@@ -62,6 +66,52 @@ Errors thrown after a streamed response has started (`StreamCodec` / `SseCodec`
|
|
|
62
66
|
|
|
63
67
|
A JSON envelope object with keys but no `input` key (`{"id":1}` instead of `{"input":{"id":1}}`) is `400 BAD_REQUEST` `Request body must be {"input": ...}`, whichever codec carries it (plain JSON, the multipart `rpc` part, or a JSONL input line). `{}` and `{"input":null}` still mean no input.
|
|
64
68
|
|
|
69
|
+
## Cacheable reads and public REST (userland)
|
|
70
|
+
|
|
71
|
+
The wire is POST only, so a CDN or browser cannot cache a call by URL. When a read needs HTTP caching, bridge it in userland: `HandlerPlugin.onRequest` runs before the method check, so it can turn `GET {prefix}/{path}?input=<JSON>` into the POST `FetchHandler` serves. Opt in per procedure in the contract, and only for procedures with no side effects:
|
|
72
|
+
|
|
73
|
+
```ts no-check
|
|
74
|
+
find: procedure
|
|
75
|
+
.meta({ http: { method: 'GET', cacheControl: 'public, max-age=60' } })
|
|
76
|
+
.input(z.object({ id: z.number() }))
|
|
77
|
+
|
|
78
|
+
new FetchHandler(app, {
|
|
79
|
+
plugins: [
|
|
80
|
+
new CORSPlugin({ origin }),
|
|
81
|
+
new GetBridgePlugin(app, '/rpc'), // last, so no later onRequest re-wraps its request
|
|
82
|
+
],
|
|
83
|
+
})
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`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.
|
|
87
|
+
|
|
88
|
+
- `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.
|
|
89
|
+
- A GET runs from a plain link, cookies included: never opt in a procedure that writes.
|
|
90
|
+
- GET is CORS-safelisted, so `CORSPlugin` needs no `allowMethods` change. Your own headers (`Authorization`, custom) still make it preflight, and browsers cache a preflight per URL, so each distinct `?input=` preflights once.
|
|
91
|
+
- The wire and the `http` meta key are what a future first-party binding would use, so the bridge can be dropped without touching contracts or URLs.
|
|
92
|
+
|
|
93
|
+
A public REST API for third parties is a userland router next to `FetchHandler`, calling the same `app` through `createLocalClient` so middleware, validation, and typed errors run:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { httpStatus } from '@ts-pf/http'
|
|
97
|
+
import { isPFError } from '@ts-pf/protocol'
|
|
98
|
+
import { createLocalClient } from '@ts-pf/server'
|
|
99
|
+
|
|
100
|
+
// GET /v1/planets/:id, routed by your server before handler.handle()
|
|
101
|
+
export async function getPlanet(id: string): Promise<Response> {
|
|
102
|
+
const api = createLocalClient(app, { context })
|
|
103
|
+
try {
|
|
104
|
+
return Response.json(await api.planet.find({ id: Number(id) }))
|
|
105
|
+
} catch (error) {
|
|
106
|
+
if (!isPFError(error)) throw error
|
|
107
|
+
return Response.json(
|
|
108
|
+
{ code: error.code, message: error.message },
|
|
109
|
+
{ status: httpStatus(error) },
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
65
115
|
## API
|
|
66
116
|
|
|
67
117
|
- `FetchHandler`, `FetchHandlerOnError`, `HandleResult`, `HandlerPlugin`
|
|
@@ -75,6 +125,7 @@ A JSON envelope object with keys but no `input` key (`{"id":1}` instead of `{"in
|
|
|
75
125
|
|
|
76
126
|
## Don't
|
|
77
127
|
|
|
78
|
-
- Serve REST, OpenAPI, or `catalog.json` from `FetchHandler`.
|
|
128
|
+
- Serve REST, OpenAPI, or `catalog.json` from `FetchHandler`. REST routes are userland, beside it.
|
|
129
|
+
- Bridge GET for a procedure with side effects, or mark per-user data `public`.
|
|
79
130
|
- Port `HandlerPlugin` onto WS / stdio / MessagePort.
|
|
80
131
|
- Put file size caps here — those are `MultipartCodec` options.
|