@ts-pf/protocol 0.1.1 → 0.2.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/LICENSE +21 -0
- package/PROTOCOL.md +38 -12
- package/README.md +53 -3
- package/dist/defined.d.ts +18 -0
- package/dist/defined.d.ts.map +1 -0
- package/dist/defined.js +18 -0
- package/dist/defined.js.map +1 -0
- package/dist/envelope.d.ts +8 -1
- package/dist/envelope.d.ts.map +1 -1
- package/dist/envelope.js +8 -1
- package/dist/envelope.js.map +1 -1
- package/dist/error.d.ts +7 -1
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +3 -1
- package/dist/error.js.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/is-async-iterable.d.ts +9 -0
- package/dist/is-async-iterable.d.ts.map +1 -0
- package/dist/is-async-iterable.js +15 -0
- package/dist/is-async-iterable.js.map +1 -0
- package/dist/wire-error.d.ts +10 -0
- package/dist/wire-error.d.ts.map +1 -0
- package/dist/wire-error.js +15 -0
- package/dist/wire-error.js.map +1 -0
- package/package.json +4 -1
- package/skills/ts-pf-protocol/SKILL.md +10 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ts-pf contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/PROTOCOL.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# ts-pf RPC Protocol
|
|
1
|
+
# ts-pf RPC Protocol 1 (revision 1.1)
|
|
2
2
|
|
|
3
3
|
Canonical JSON envelope language for ts-pf. Same `{ "input" }`, `{ "ok": true, "output" }`, `{ "ok": false, "error": { "code", "message", "data?" } }` on every pipe. Discriminator is `error.code`. Implementable from TypeScript, Kotlin, Swift, or any HTTP client.
|
|
4
4
|
|
|
@@ -42,6 +42,10 @@ Procedure path is the nested router keys joined by `/`. Example: `planet.find`
|
|
|
42
42
|
|
|
43
43
|
v1 accepts **POST only**. Other methods return HTTP 405 with `error.code: "METHOD_NOT_ALLOWED"`.
|
|
44
44
|
|
|
45
|
+
Servers read the request header `x-ts-pf-protocol`. Absent → accept (plain HTTP clients). Present and equal to `1` (after trimming surrounding whitespace) → accept. Any other value → HTTP 400 with `error.code: "BAD_REQUEST"` and `message: "Unsupported protocol version"`; the response still carries `x-ts-pf-protocol: 1`. The check runs after the method check and before the body is read.
|
|
46
|
+
|
|
47
|
+
Clients send `x-ts-pf-protocol: 1` on every request. A response whose `x-ts-pf-protocol` is present and not `1` must not be decoded; the TypeScript client rejects it with `INTERNAL`, the HTTP status, and message `Unsupported protocol version (x-ts-pf-protocol: <value>)` — it is not a local failure, because the response came from a server. A response without the header follows the `Non-RPC response` rule below.
|
|
48
|
+
|
|
45
49
|
Success:
|
|
46
50
|
|
|
47
51
|
```
|
|
@@ -67,15 +71,15 @@ x-ts-pf-protocol: 1
|
|
|
67
71
|
| Code | Status | When |
|
|
68
72
|
|---|---|---|
|
|
69
73
|
| `BAD_REQUEST` | 400 | Invalid JSON or envelope |
|
|
70
|
-
| `VALIDATION` | 422 | Input schema failed. `error.data.issues` is `{ message, path }[]` |
|
|
74
|
+
| `VALIDATION` | 422 | Input schema failed, or a stream item schema failed after the stream started. `error.data.issues` is `{ message, path }[]` |
|
|
71
75
|
| `NOT_FOUND` | 404 | Prefix matched, procedure missing |
|
|
72
76
|
| `METHOD_NOT_ALLOWED` | 405 | Non-POST |
|
|
73
77
|
| `PAYLOAD_TOO_LARGE` | 413 | Request body larger than the configured handler limit |
|
|
74
|
-
| `INTERNAL` | 500 | Unknown throw.
|
|
78
|
+
| `INTERNAL` | 500 | Unknown throw. A unary output schema failure is also 500 (server bug). Output schema checks are optional. |
|
|
75
79
|
|
|
76
|
-
The discriminator is `error.code`. HTTP status is unary transport only; it is not in the JSON body.
|
|
80
|
+
The discriminator is `error.code`. HTTP status is unary transport only; it is not in the JSON body. The TypeScript `PFError.status` is optional transport metadata, never identity (see [`status` on the TypeScript client](#status-on-the-typescript-client)).
|
|
77
81
|
|
|
78
|
-
The table above is a **closed** set. Procedure-declared codes are application-defined strings with optional `data`. `VALIDATION` is the only protocol code with a specified `data` shape: `{ issues: { message, path }[] }` where `path` is `(string | number)[]`. Other protocol codes omit `data`. `INTERNAL` never includes a stack or output-schema issues.
|
|
82
|
+
The table above is a **closed** set. Procedure-declared codes are application-defined strings with optional `data`. `VALIDATION` is the only protocol code with a specified `data` shape: `{ issues: { message, path }[] }` where `path` is `(string | number)[]`. Other protocol codes omit `data`. `INTERNAL` never includes a stack or output-schema issues. Servers may skip output schema checks. When a server checks a unary output schema and it fails, the code is `INTERNAL` and `data` is omitted. When a server checks a stream item and it fails, the code is `VALIDATION` with `issues`, in-band after the stream has started.
|
|
79
83
|
|
|
80
84
|
Unknown `code` values are valid JSON; clients must catch-all. A procedure may reuse `NOT_FOUND` for a missing entity. Do not redeclare `VALIDATION`, `INTERNAL`, `BAD_REQUEST`, `METHOD_NOT_ALLOWED`, or `PAYLOAD_TOO_LARGE`.
|
|
81
85
|
|
|
@@ -83,13 +87,29 @@ After a stream starts, HTTP status stays 200; the same `{ ok: false, error }` ob
|
|
|
83
87
|
|
|
84
88
|
TypeScript `PFError`, `instanceof`, and `asResult` are JS conveniences. They are not required to consume the API. Any HTTP client parses this envelope.
|
|
85
89
|
|
|
86
|
-
Contract-declared error codes use the status on the error definition.
|
|
90
|
+
Contract-declared error codes use the status on the error definition when the HTTP binding sends a unary failure; a declared code with no `status` is sent as HTTP 400 (`httpStatus()` in `@ts-pf/http`).
|
|
91
|
+
|
|
92
|
+
### `status` on the TypeScript client
|
|
93
|
+
|
|
94
|
+
`PFError.status` is `number | undefined`. It is set only where a transport really has one. Switch on `error.code`; treat `status` as a hint and guard it (`err.status ?? 400`).
|
|
95
|
+
|
|
96
|
+
| Transport | `status` on the rejected `PFError` |
|
|
97
|
+
|---|---|
|
|
98
|
+
| HTTP unary failure (`FetchLink`) | The HTTP response status (declared `NOT_FOUND { status: 404 }` → `404`; declared code without `status` → `400`; protocol codes → the table above) |
|
|
99
|
+
| HTTP after a stream started (JSONL line / SSE `event: error`) | `undefined` (the response was already 200) |
|
|
100
|
+
| WebSocket / stdio / MessagePort | `undefined` (no HTTP status on a frame; `errorFromEnvelope` does not synthesize one) |
|
|
101
|
+
| Local failure (`localFailure`, `isLocalFailure`) | `0` with `local: true` |
|
|
102
|
+
| In-process `createLocalClient` | The declared `ErrorDef.status` when there is one, else `undefined` |
|
|
87
103
|
|
|
88
|
-
The TypeScript client maps **local** failures (never got a protocol result) to `INTERNAL` with `local: true` and `status: 0`. `local` and `status` are TypeScript client conventions, not protocol fields, and are **not** on the wire. `isLocalFailure` is `local === true`. Abort uses message `Request aborted`. Network throws keep `error.message` and set `Error.cause` to the thrown value (Node’s `ECONNREFUSED` is then `error.cause.cause`). HTTP responses **without** `x-ts-pf-protocol` that fail `decodeResponse` become `INTERNAL` with the HTTP status and message `Non-RPC response (HTTP …)` — do not surface codec `BAD_REQUEST` 400 for proxy HTML. Codec `PFError` is rethrown only when `x-ts-pf-protocol` is present (truncated JSONL/SSE at decode time, malformed ts-pf JSON). `toJSON()` still omits `status`, `cause`, and `local`.
|
|
104
|
+
The TypeScript client maps **local** failures (never got a protocol result) to `INTERNAL` with `local: true` and `status: 0`. `local` and `status` are TypeScript client conventions, not protocol fields, and are **not** on the wire. `isLocalFailure` is `local === true`. Abort uses message `Request aborted`. Network throws keep `error.message` and set `Error.cause` to the thrown value (Node’s `ECONNREFUSED` is then `error.cause.cause`). HTTP responses **without** `x-ts-pf-protocol` that fail `decodeResponse` become `INTERNAL` with the HTTP status and message `Non-RPC response (HTTP …)` — do not surface codec `BAD_REQUEST` 400 for proxy HTML. Codec `PFError` is rethrown only when `x-ts-pf-protocol` is present and equal to `1` (truncated JSONL/SSE at decode time, malformed ts-pf JSON); a different value is never decoded (see [HTTP Fetch binding](#http-fetch-binding-default)). `toJSON()` still omits `status`, `cause`, and `local`.
|
|
89
105
|
|
|
90
106
|
## Versioning
|
|
91
107
|
|
|
92
|
-
Header `x-ts-pf-protocol`
|
|
108
|
+
Header `x-ts-pf-protocol` and `v` on `hello` / `hello-ok` carry the **wire major**, currently `1` (`PROTOCOL_MAJOR`; the header is its decimal string, `PROTOCOL_VERSION`). JSON is the default content type and the envelope above does not change for JSON calls. Other content types are codec-defined (see [Binary attachments](#binary-attachments-optional), [Message streams](#message-streams-optional), and [SSE](#sse-optional)). A codec may stream the HTTP body (`ReadableStream`) without changing this envelope. Optional [message transports](#message-transports-optional) carry the same major as `v: 1` on `hello` instead of this header.
|
|
109
|
+
|
|
110
|
+
**Revisions.** This document has a revision number (this is 1.1). Revisions are additive and keep the wire major: a peer cannot tell 1.0 from 1.1 on the wire. Additive means a 1.x sender emits a new key, frame type, header, or content type only when the user opted in, and a 1.0 receiver that does not understand it fails **that call** (`BAD_REQUEST`), never the connection and never silently. Receivers must not reject a value that 1.0 defined as valid. Bump the wire major only when the JSON envelope, a required frame field, or the handshake changes.
|
|
111
|
+
|
|
112
|
+
**Content negotiation.** Clients MAY send `Accept` listing the response media types they can decode (`application/json`, `application/jsonl`, `text/event-stream`). Servers MAY use it to choose the framing of a streamed response and MUST ignore it when they cannot satisfy it (never 406; the JSON failure envelope is always `application/json`). `Content-Type` on the request selects the request codec as today.
|
|
93
113
|
|
|
94
114
|
## Binary attachments (optional)
|
|
95
115
|
|
|
@@ -272,7 +292,7 @@ These bindings are not HTTP:
|
|
|
272
292
|
- `METHOD_NOT_ALLOWED` is Fetch-only. There is no HTTP method on a frame.
|
|
273
293
|
- Procedure-declared `.errors()` `status` is Fetch-only. Every client, including TypeScript, switches on `error.code`.
|
|
274
294
|
|
|
275
|
-
`v: 1` on `hello` / `hello-ok` is the same
|
|
295
|
+
`v: 1` on `hello` / `hello-ok` is the same wire major as header `x-ts-pf-protocol: 1`. Different carrier, same number.
|
|
276
296
|
|
|
277
297
|
### Framing
|
|
278
298
|
|
|
@@ -404,6 +424,12 @@ A hostile peer cannot cancel an in-flight call by reusing its id or sending `{ "
|
|
|
404
424
|
|
|
405
425
|
The connection keeps reading frames while a call runs. Two in-flight ids complete independently.
|
|
406
426
|
|
|
427
|
+
#### Backpressure
|
|
428
|
+
|
|
429
|
+
No frame acknowledges `item` or `in-item`, and no frame carries a window; flow control in wire major 1 is the transport's. A sender SHOULD NOT pull the next value from the producing iterator until the transport has drained the previous frame of that id (`bufferedAmount` under a high-water mark, `write()` accepted or `drain` fired). A receiver MAY stop reading the transport while a per-id queue is over a local high-water mark; pausing is connection-wide, so one slow consumer delays every id on that connection. On a transport the receiver cannot pause (`MessagePort`, a browser `WebSocket`) receiver memory is bounded only by the peer's behaviour; a receiver may `cancel` a stream it cannot keep up with.
|
|
430
|
+
|
|
431
|
+
A sender MAY give up on a peer that does not drain within a timeout. That is a server-side condition, not a protocol code: the server sends `{ "type": "result", "id", "ok": false, "error": { "code": "INTERNAL", "message": "Peer did not drain within <n> ms" } }` as the terminal frame for that id (after the items already handed to the transport; no `done`), stops the procedure as if cancelled, and keeps the connection open. A client that gives up on a server that does not drain its `in-item` frames fails the call locally and sends `cancel`. A 1.x receiver already ignores an unknown frame type with an in-flight `id` (table above), so a future credit frame is harmless to it; one for an id that has already terminated would draw `BAD_REQUEST`, so a later major must never send credit after a terminal frame.
|
|
432
|
+
|
|
407
433
|
### Size limits
|
|
408
434
|
|
|
409
435
|
Option omitted = **unlimited in both directions** (same as Fetch without a request-limit plugin).
|
|
@@ -553,7 +579,7 @@ Nested streams and `File` / `Blob` values inside items are `BAD_REQUEST`.
|
|
|
553
579
|
{ "type": "in-done", "id": "1" }
|
|
554
580
|
```
|
|
555
581
|
|
|
556
|
-
The server must accept `in-item` for that id as soon as `call` is accepted (the next-turn item must not be lost). Backpressure is
|
|
582
|
+
The server must accept `in-item` for that id as soon as `call` is accepted (the next-turn item must not be lost). Backpressure is the transport's; see "Backpressure" under Multiplex. No credit-based flow control in v1. Cancel of the call aborts both sides.
|
|
557
583
|
|
|
558
584
|
### Cancel and mid-stream errors
|
|
559
585
|
|
|
@@ -579,7 +605,7 @@ Clients distinguish three ways:
|
|
|
579
605
|
2. `error.code` — declared / protocol codes from the envelope
|
|
580
606
|
3. `INTERNAL` without `local` — protocol `INTERNAL`
|
|
581
607
|
|
|
582
|
-
Declared `.errors()` `status` is Fetch-only. Mid-stream Fetch JSONL keeps HTTP 200. Message transports do **not** reconstruct HTTP status from `error.code`; the TypeScript `PFError.status`
|
|
608
|
+
Declared `.errors()` `status` is Fetch-only. Mid-stream Fetch JSONL keeps HTTP 200. Message transports do **not** reconstruct HTTP status from `error.code`; the TypeScript `PFError.status` is `undefined` on this pipe (there is no default). Non-JS clients ignore `status` and switch on `error.code`. Do not copy `status: 200`.
|
|
583
609
|
|
|
584
610
|
| Event | TypeScript `PFError` | `isLocalFailure` |
|
|
585
611
|
|---|---|---|
|
|
@@ -589,7 +615,7 @@ Declared `.errors()` `status` is Fetch-only. Mid-stream Fetch JSONL keeps HTTP 2
|
|
|
589
615
|
| Hello timeout / connect fail / hello stringify fail | `INTERNAL`, `local: true`, status `0`, message `Network error`, `cause` set; close | true |
|
|
590
616
|
| Malformed frame from peer with no usable id (connection closes) | Inflight: `INTERNAL`, `local: true`, status `0`, message `Invalid response` or `Connection closed`, `cause` set | true |
|
|
591
617
|
| First frame for an id is illegal | `INTERNAL`, `local: true`, status `0`, message `Invalid response`; that call only | true |
|
|
592
|
-
| Protocol `result` `{ "ok": false, "error" }` | code / message / data from the envelope; `status`
|
|
618
|
+
| Protocol `result` `{ "ok": false, "error" }` | code / message / data from the envelope; `status` is `undefined` | false |
|
|
593
619
|
| `NOT_FOUND` / `VALIDATION` / `BAD_REQUEST` from the server | as envelope | false |
|
|
594
620
|
| Version mismatch `hello-error` | `BAD_REQUEST` from the envelope (if received) or `local: true` if the connection closed first | depends |
|
|
595
621
|
| Client refused to send oversize / could not stringify a `call` | `PAYLOAD_TOO_LARGE` or `INTERNAL` (stringify) | false |
|
package/README.md
CHANGED
|
@@ -1,7 +1,57 @@
|
|
|
1
1
|
# @ts-pf/protocol
|
|
2
2
|
|
|
3
|
-
Portable JSON envelope, `PFError`, and `PROTOCOL_VERSION
|
|
3
|
+
Portable JSON envelope, `PFError`, `localFailure`, and `PROTOCOL_VERSION` for [ts-pf](https://github.com/crobinson42/ts-pf#readme). Every transport (Fetch, WebSocket, stdio, MessagePort) carries these same shapes. No HTTP server, no schemas, no codecs.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Install
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
```sh
|
|
8
|
+
npm i @ts-pf/contract @ts-pf/server @ts-pf/server-http @ts-pf/client @ts-pf/client-http zod
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`@ts-pf/protocol` is a dependency of `@ts-pf/server` and `@ts-pf/client`; install it directly when you import `PFError` yourself.
|
|
12
|
+
|
|
13
|
+
## Use
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { PFError, isPFError, localFailure, PROTOCOL_VERSION } from '@ts-pf/protocol'
|
|
17
|
+
|
|
18
|
+
// Server: throw from a handler or middleware. `status` is an HTTP hint that
|
|
19
|
+
// FetchHandler uses for Response.status; the envelope carries only
|
|
20
|
+
// { code, message, data? }. Declared errors use `errors.CODE(data)` instead.
|
|
21
|
+
export function requireAdmin(user: { role: string }) {
|
|
22
|
+
if (user.role !== 'admin') {
|
|
23
|
+
throw new PFError({ code: 'FORBIDDEN', status: 403, message: 'Admins only' })
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Client: every failure surfaces as a PFError again (or use asResult()).
|
|
28
|
+
export function describeFailure(error: unknown): string {
|
|
29
|
+
if (!isPFError(error)) return 'not an RPC failure'
|
|
30
|
+
if (error.local) return `never reached the server: ${error.message}`
|
|
31
|
+
return `${error.code}: ${error.message}`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Wire shape, identical for curl and non-TS clients:
|
|
35
|
+
// POST /rpc/planet/find { "input": { "id": 1 } }
|
|
36
|
+
// 200 { "ok": true, "output": { "id": 1, "name": "Earth" } }
|
|
37
|
+
// 404 { "ok": false, "error": { "code": "NOT_FOUND", "message": "…", "data": { "id": 9 } } }
|
|
38
|
+
PROTOCOL_VERSION // x-ts-pf-protocol header value on every RPC request and response; FetchHandler / FetchLink reject other values
|
|
39
|
+
localFailure('Request aborted') // INTERNAL with local: true, status 0 (never on the wire)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The discriminator is JSON `error.code`. `status`, `cause`, and `local` never appear in the envelope (`toJSON` omits them).
|
|
43
|
+
|
|
44
|
+
Two helpers the transports share, for codecs and adapters of your own: `toWireError(error)` is the `error` object of a failure envelope for any thrown value — a `PFError`'s `toJSON()`, anything else a fixed `{ code: 'INTERNAL', message: 'Internal server error' }` with no stack or message leak (log the original yourself). `isAsyncIterable(value)` is the one answer to "is this a stream" (a non-null object or function with a `Symbol.asyncIterator` function; a `ReadableStream` counts on Node, so exclude it at a site that wants bytes). `defined(obj)` returns a shallow copy without the keys whose value is `undefined`, typed with those keys optional — the one helper for forwarding optional fields under `exactOptionalPropertyTypes` (`{ ...required, ...defined({ signal }) }`).
|
|
45
|
+
|
|
46
|
+
Wire spec: [`PROTOCOL.md`](https://github.com/crobinson42/ts-pf/blob/main/packages/protocol/PROTOCOL.md) (also shipped in this package). Happy path: [root README](https://github.com/crobinson42/ts-pf#readme).
|
|
47
|
+
|
|
48
|
+
## API
|
|
49
|
+
|
|
50
|
+
- `PFError`, `PFErrorInit`, `isPFError`, `localFailure`, `ProtocolErrorCode`
|
|
51
|
+
- `PROTOCOL_MAJOR` (`1`, the wire major: `v` on message `hello`), `PROTOCOL_VERSION` (`'1'`, its string: the `x-ts-pf-protocol` header value), `RpcRequest`, `RpcResponse`, `RpcSuccess`, `RpcFailure`, `PFResultPromise`
|
|
52
|
+
- `toWireError`, `isAsyncIterable`
|
|
53
|
+
- `defined`, `Defined` — `defined(obj)` drops the keys whose value is `undefined`, so `{ ...required, ...defined({ signal }) }` builds an `exactOptionalPropertyTypes` option bag without one spread per field
|
|
54
|
+
|
|
55
|
+
## Agent skills
|
|
56
|
+
|
|
57
|
+
This package ships [`ts-pf-protocol`](https://github.com/crobinson42/ts-pf/blob/main/packages/protocol/skills/ts-pf-protocol/SKILL.md). Sync with `npx skills experimental_sync -y`.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `T` with every key that may be `undefined` made optional and narrowed, and
|
|
3
|
+
* every other key kept as is. The static shape of `defined(obj)`.
|
|
4
|
+
*/
|
|
5
|
+
export type Defined<T extends object> = {
|
|
6
|
+
[K in keyof T as undefined extends T[K] ? never : K]: T[K];
|
|
7
|
+
} & {
|
|
8
|
+
[K in keyof T as undefined extends T[K] ? K : never]?: Exclude<T[K], undefined>;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Shallow copy of `obj` without the own enumerable string keys whose value is
|
|
12
|
+
* `undefined`. Under `exactOptionalPropertyTypes` an option bag may not carry
|
|
13
|
+
* `signal: undefined` where `signal?: AbortSignal` is declared, so forwarding
|
|
14
|
+
* optional fields becomes `{ ...required, ...defined({ signal, headers }) }`
|
|
15
|
+
* instead of one `...(x ? { x } : {})` spread per field.
|
|
16
|
+
*/
|
|
17
|
+
export declare function defined<T extends object>(obj: T): Defined<T>;
|
|
18
|
+
//# sourceMappingURL=defined.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defined.d.ts","sourceRoot":"","sources":["../src/defined.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,SAAS,MAAM,IAAI;KACrC,CAAC,IAAI,MAAM,CAAC,IAAI,SAAS,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAC3D,GAAG;KACD,CAAC,IAAI,MAAM,CAAC,IAAI,SAAS,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC,EAAE,OAAO,CAC5D,CAAC,CAAC,CAAC,CAAC,EACJ,SAAS,CACV;CACF,CAAA;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAS5D"}
|
package/dist/defined.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shallow copy of `obj` without the own enumerable string keys whose value is
|
|
3
|
+
* `undefined`. Under `exactOptionalPropertyTypes` an option bag may not carry
|
|
4
|
+
* `signal: undefined` where `signal?: AbortSignal` is declared, so forwarding
|
|
5
|
+
* optional fields becomes `{ ...required, ...defined({ signal, headers }) }`
|
|
6
|
+
* instead of one `...(x ? { x } : {})` spread per field.
|
|
7
|
+
*/
|
|
8
|
+
export function defined(obj) {
|
|
9
|
+
const out = {};
|
|
10
|
+
for (const key of Object.keys(obj)) {
|
|
11
|
+
const value = obj[key];
|
|
12
|
+
if (value !== undefined) {
|
|
13
|
+
out[key] = value;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=defined.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defined.js","sourceRoot":"","sources":["../src/defined.ts"],"names":[],"mappings":"AAaA;;;;;;GAMG;AACH,MAAM,UAAU,OAAO,CAAmB,GAAM;IAC9C,MAAM,GAAG,GAA4B,EAAE,CAAA;IACvC,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;QACnC,MAAM,KAAK,GAAI,GAA+B,CAAC,GAAG,CAAC,CAAA;QACnD,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;QAClB,CAAC;IACH,CAAC;IACD,OAAO,GAAiB,CAAA;AAC1B,CAAC"}
|
package/dist/envelope.d.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Wire major: `v` on message `hello` / `hello-ok`. The HTTP header
|
|
3
|
+
* `x-ts-pf-protocol` carries its decimal string, `PROTOCOL_VERSION`. Bump only
|
|
4
|
+
* when the JSON envelope, a required frame field, or the handshake changes.
|
|
5
|
+
*/
|
|
6
|
+
export declare const PROTOCOL_MAJOR: 1;
|
|
7
|
+
/** `x-ts-pf-protocol` header value: the decimal string of `PROTOCOL_MAJOR`. */
|
|
8
|
+
export declare const PROTOCOL_VERSION: `${typeof PROTOCOL_MAJOR}`;
|
|
2
9
|
export type RpcRequest = {
|
|
3
10
|
input?: unknown;
|
|
4
11
|
};
|
package/dist/envelope.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"envelope.d.ts","sourceRoot":"","sources":["../src/envelope.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"envelope.d.ts","sourceRoot":"","sources":["../src/envelope.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,cAAc,EAAG,CAAU,CAAA;AAExC,+EAA+E;AAC/E,eAAO,MAAM,gBAAgB,EAAE,GAAG,OAAO,cAAc,EAAwB,CAAA;AAE/E,MAAM,MAAM,UAAU,GAAG;IAAE,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,CAAA;AAE5C,MAAM,MAAM,UAAU,CAAC,CAAC,GAAG,OAAO,IAAI;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,MAAM,EAAE,CAAC,CAAA;CAAE,CAAA;AAE7D,MAAM,MAAM,UAAU,GAAG;IACvB,EAAE,EAAE,KAAK,CAAA;IACT,KAAK,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,OAAO,CAAA;KAAE,CAAA;CACzD,CAAA;AAED,MAAM,MAAM,WAAW,CAAC,CAAC,GAAG,OAAO,IAAI,UAAU,CAAC,CAAC,CAAC,GAAG,UAAU,CAAA;AAEjE,MAAM,WAAW,eAAe,CAAC,CAAC,EAAE,CAAC,CAAE,SAAQ,OAAO,CAAC,CAAC,CAAC;IACvD,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC,CAAA;CACxB"}
|
package/dist/envelope.js
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Wire major: `v` on message `hello` / `hello-ok`. The HTTP header
|
|
3
|
+
* `x-ts-pf-protocol` carries its decimal string, `PROTOCOL_VERSION`. Bump only
|
|
4
|
+
* when the JSON envelope, a required frame field, or the handshake changes.
|
|
5
|
+
*/
|
|
6
|
+
export const PROTOCOL_MAJOR = 1;
|
|
7
|
+
/** `x-ts-pf-protocol` header value: the decimal string of `PROTOCOL_MAJOR`. */
|
|
8
|
+
export const PROTOCOL_VERSION = `${PROTOCOL_MAJOR}`;
|
|
2
9
|
//# sourceMappingURL=envelope.js.map
|
package/dist/envelope.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"envelope.js","sourceRoot":"","sources":["../src/envelope.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"envelope.js","sourceRoot":"","sources":["../src/envelope.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAU,CAAA;AAExC,+EAA+E;AAC/E,MAAM,CAAC,MAAM,gBAAgB,GAA+B,GAAG,cAAc,EAAE,CAAA"}
|
package/dist/error.d.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
export type PFErrorInit<TCode extends string = string, TData = unknown> = {
|
|
2
2
|
code: TCode;
|
|
3
|
+
/**
|
|
4
|
+
* Transport metadata, not identity. Set by the server from a declared
|
|
5
|
+
* `ErrorDef.status` and by `FetchLink` from the HTTP response. Absent on
|
|
6
|
+
* message transports, on mid-stream failures, and when nothing declared
|
|
7
|
+
* one; `httpStatus()` in `@ts-pf/http` supplies the HTTP default (400).
|
|
8
|
+
*/
|
|
3
9
|
status?: number;
|
|
4
10
|
message?: string;
|
|
5
11
|
data?: TData;
|
|
@@ -8,7 +14,7 @@ export type PFErrorInit<TCode extends string = string, TData = unknown> = {
|
|
|
8
14
|
};
|
|
9
15
|
export declare class PFError<TCode extends string = string, TData = unknown> extends Error {
|
|
10
16
|
readonly code: TCode;
|
|
11
|
-
readonly status
|
|
17
|
+
readonly status?: number;
|
|
12
18
|
readonly data?: TData;
|
|
13
19
|
readonly local?: true;
|
|
14
20
|
constructor(init: PFErrorInit<TCode, TData>);
|
package/dist/error.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM,EAAE,KAAK,GAAG,OAAO,IAAI;IACxE,IAAI,EAAE,KAAK,CAAA;IACX,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,IAAI,CAAC,EAAE,KAAK,CAAA;IACZ,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,KAAK,CAAC,EAAE,IAAI,CAAA;CACb,CAAA;AAED,qBAAa,OAAO,CAClB,KAAK,SAAS,MAAM,GAAG,MAAM,EAC7B,KAAK,GAAG,OAAO,CACf,SAAQ,KAAK;IACb,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAA;IACpB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM,EAAE,KAAK,GAAG,OAAO,IAAI;IACxE,IAAI,EAAE,KAAK,CAAA;IACX;;;;;OAKG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,IAAI,CAAC,EAAE,KAAK,CAAA;IACZ,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,KAAK,CAAC,EAAE,IAAI,CAAA;CACb,CAAA;AAED,qBAAa,OAAO,CAClB,KAAK,SAAS,MAAM,GAAG,MAAM,EAC7B,KAAK,GAAG,OAAO,CACf,SAAQ,KAAK;IACb,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAA;IACpB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,IAAI,CAAC,EAAE,KAAK,CAAA;IACrB,QAAQ,CAAC,KAAK,CAAC,EAAE,IAAI,CAAA;gBAET,IAAI,EAAE,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC;IAoB3C,MAAM,IAAI;QAAE,IAAI,EAAE,KAAK,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,KAAK,CAAA;KAAE;CAUzD;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,OAAO,CAE1D;AAED,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO,GAAG,OAAO,CAQtE;AAED,eAAO,MAAM,iBAAiB;;;;;;;CAOpB,CAAA"}
|
package/dist/error.js
CHANGED
|
@@ -13,7 +13,9 @@ export class PFError extends Error {
|
|
|
13
13
|
}
|
|
14
14
|
this.name = 'PFError';
|
|
15
15
|
this.code = init.code;
|
|
16
|
-
|
|
16
|
+
if (init.status !== undefined) {
|
|
17
|
+
this.status = init.status;
|
|
18
|
+
}
|
|
17
19
|
if (init.data !== undefined) {
|
|
18
20
|
this.data = init.data;
|
|
19
21
|
}
|
package/dist/error.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAeA,MAAM,OAAO,OAGX,SAAQ,KAAK;IACJ,IAAI,CAAO;IACX,MAAM,CAAS;IACf,IAAI,CAAQ;IACZ,KAAK,CAAO;IAErB,YAAY,IAA+B;QACzC,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,IAAI,CAAA;QACzC,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;YACpB,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CAAA;QACvC,CAAC;aAAM,CAAC;YACN,KAAK,CAAC,OAAO,CAAC,CAAA;QAChB,CAAC;QACD,IAAI,CAAC,IAAI,GAAG,SAAS,CAAA;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAA;QACrB,IAAI,IAAI,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YAC9B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAA;QAC3B,CAAC;QACD,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAA;QACvB,CAAC;QACD,IAAI,IAAI,CAAC,KAAK,KAAK,IAAI,EAAE,CAAC;YACxB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAA;QACnB,CAAC;IACH,CAAC;IAED,MAAM;QACJ,MAAM,IAAI,GAAmD;YAC3D,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,CAAA;QACD,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAA;QACvB,CAAC;QACD,OAAO,IAAI,CAAA;IACb,CAAC;CACF;AAED,MAAM,UAAU,SAAS,CAAC,KAAc;IACtC,OAAO,KAAK,YAAY,OAAO,CAAA;AACjC,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,OAAe,EAAE,KAAe;IAC3D,OAAO,IAAI,OAAO,CAAC;QACjB,IAAI,EAAE,UAAU;QAChB,MAAM,EAAE,CAAC;QACT,KAAK,EAAE,IAAI;QACX,OAAO;QACP,GAAG,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC1C,CAAC,CAAA;AACJ,CAAC;AAED,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,WAAW,EAAE,aAAa;IAC1B,UAAU,EAAE,YAAY;IACxB,SAAS,EAAE,WAAW;IACtB,QAAQ,EAAE,UAAU;IACpB,kBAAkB,EAAE,oBAAoB;IACxC,iBAAiB,EAAE,mBAAmB;CAC9B,CAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
export { type
|
|
1
|
+
export { type Defined, defined } from './defined.js';
|
|
2
|
+
export { type PFResultPromise, PROTOCOL_MAJOR, PROTOCOL_VERSION, type RpcFailure, type RpcRequest, type RpcResponse, type RpcSuccess, } from './envelope.js';
|
|
2
3
|
export { isPFError, localFailure, PFError, type PFErrorInit, ProtocolErrorCode, } from './error.js';
|
|
4
|
+
export { isAsyncIterable } from './is-async-iterable.js';
|
|
5
|
+
export { toWireError } from './wire-error.js';
|
|
3
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,eAAe,EACpB,gBAAgB,EAChB,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,WAAW,EAChB,KAAK,UAAU,GAChB,MAAM,eAAe,CAAA;AACtB,OAAO,EACL,SAAS,EACT,YAAY,EACZ,OAAO,EACP,KAAK,WAAW,EAChB,iBAAiB,GAClB,MAAM,YAAY,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACpD,OAAO,EACL,KAAK,eAAe,EACpB,cAAc,EACd,gBAAgB,EAChB,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,WAAW,EAChB,KAAK,UAAU,GAChB,MAAM,eAAe,CAAA;AACtB,OAAO,EACL,SAAS,EACT,YAAY,EACZ,OAAO,EACP,KAAK,WAAW,EAChB,iBAAiB,GAClB,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { defined } from './defined.js';
|
|
2
|
+
export { PROTOCOL_MAJOR, PROTOCOL_VERSION, } from './envelope.js';
|
|
2
3
|
export { isPFError, localFailure, PFError, ProtocolErrorCode, } from './error.js';
|
|
4
|
+
export { isAsyncIterable } from './is-async-iterable.js';
|
|
5
|
+
export { toWireError } from './wire-error.js';
|
|
3
6
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,gBAAgB,GAKjB,MAAM,eAAe,CAAA;AACtB,OAAO,EACL,SAAS,EACT,YAAY,EACZ,OAAO,EAEP,iBAAiB,GAClB,MAAM,YAAY,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,OAAO,EAAE,MAAM,cAAc,CAAA;AACpD,OAAO,EAEL,cAAc,EACd,gBAAgB,GAKjB,MAAM,eAAe,CAAA;AACtB,OAAO,EACL,SAAS,EACT,YAAY,EACZ,OAAO,EAEP,iBAAiB,GAClB,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `true` when `value` can drive `for await`: a non-null object or function
|
|
3
|
+
* whose `Symbol.asyncIterator` is a function. Nothing is excluded — a
|
|
4
|
+
* `ReadableStream` is async-iterable on Node and modern browsers, so a wire
|
|
5
|
+
* site that must treat one as bytes rather than as a stream of items writes
|
|
6
|
+
* that exclusion itself (`isAsyncIterable(x) && !(x instanceof ReadableStream)`).
|
|
7
|
+
*/
|
|
8
|
+
export declare function isAsyncIterable(value: unknown): value is AsyncIterable<unknown>;
|
|
9
|
+
//# sourceMappingURL=is-async-iterable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-async-iterable.d.ts","sourceRoot":"","sources":["../src/is-async-iterable.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,aAAa,CAAC,OAAO,CAAC,CAYjC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `true` when `value` can drive `for await`: a non-null object or function
|
|
3
|
+
* whose `Symbol.asyncIterator` is a function. Nothing is excluded — a
|
|
4
|
+
* `ReadableStream` is async-iterable on Node and modern browsers, so a wire
|
|
5
|
+
* site that must treat one as bytes rather than as a stream of items writes
|
|
6
|
+
* that exclusion itself (`isAsyncIterable(x) && !(x instanceof ReadableStream)`).
|
|
7
|
+
*/
|
|
8
|
+
export function isAsyncIterable(value) {
|
|
9
|
+
if (value === null ||
|
|
10
|
+
(typeof value !== 'object' && typeof value !== 'function')) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return (typeof value[Symbol.asyncIterator] === 'function');
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=is-async-iterable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-async-iterable.js","sourceRoot":"","sources":["../src/is-async-iterable.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAC7B,KAAc;IAEd,IACE,KAAK,KAAK,IAAI;QACd,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,UAAU,CAAC,EAC1D,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAAC;IACD,OAAO,CACL,OAAQ,KAA8C,CACpD,MAAM,CAAC,aAAa,CACrB,KAAK,UAAU,CACjB,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RpcFailure } from './envelope.js';
|
|
2
|
+
/**
|
|
3
|
+
* The `error` object of a failure envelope for any thrown value. A `PFError`
|
|
4
|
+
* is its `toJSON()` (`{ code, message, data? }`, never `status` / `cause` /
|
|
5
|
+
* `local`). Anything else becomes a fixed `INTERNAL` with no stack and no
|
|
6
|
+
* message, so server internals never reach the wire — log the original
|
|
7
|
+
* value at the site instead of this result.
|
|
8
|
+
*/
|
|
9
|
+
export declare function toWireError(error: unknown): RpcFailure['error'];
|
|
10
|
+
//# sourceMappingURL=wire-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wire-error.d.ts","sourceRoot":"","sources":["../src/wire-error.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAG/C;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC,CAK/D"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { isPFError } from './error.js';
|
|
2
|
+
/**
|
|
3
|
+
* The `error` object of a failure envelope for any thrown value. A `PFError`
|
|
4
|
+
* is its `toJSON()` (`{ code, message, data? }`, never `status` / `cause` /
|
|
5
|
+
* `local`). Anything else becomes a fixed `INTERNAL` with no stack and no
|
|
6
|
+
* message, so server internals never reach the wire — log the original
|
|
7
|
+
* value at the site instead of this result.
|
|
8
|
+
*/
|
|
9
|
+
export function toWireError(error) {
|
|
10
|
+
if (isPFError(error)) {
|
|
11
|
+
return error.toJSON();
|
|
12
|
+
}
|
|
13
|
+
return { code: 'INTERNAL', message: 'Internal server error' };
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=wire-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wire-error.js","sourceRoot":"","sources":["../src/wire-error.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAEtC;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,IAAI,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;QACrB,OAAO,KAAK,CAAC,MAAM,EAAE,CAAA;IACvB,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,uBAAuB,EAAE,CAAA;AAC/D,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ts-pf/protocol",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Portable JSON RPC envelope, PFError, and PROTOCOL_VERSION",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ts-pf",
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
"envelope"
|
|
16
16
|
],
|
|
17
17
|
"license": "MIT",
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=22"
|
|
20
|
+
},
|
|
18
21
|
"repository": {
|
|
19
22
|
"type": "git",
|
|
20
23
|
"url": "git+https://github.com/crobinson42/ts-pf.git",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: ts-pf-protocol
|
|
3
|
-
description: Use when throwing or handling ts-pf PFError, localFailure, PROTOCOL_VERSION, or the JSON RPC envelope. Triggers: @ts-pf/protocol, PFError, isPFError, localFailure, ProtocolErrorCode.
|
|
3
|
+
description: Use when throwing or handling ts-pf PFError, localFailure, PROTOCOL_MAJOR / PROTOCOL_VERSION, or the JSON RPC envelope, or writing a codec / adapter that needs toWireError or isAsyncIterable. Triggers: @ts-pf/protocol, PFError, isPFError, localFailure, ProtocolErrorCode, PROTOCOL_MAJOR, PROTOCOL_VERSION, toWireError, isAsyncIterable.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# @ts-pf/protocol
|
|
@@ -19,15 +19,22 @@ Wire spec ships as `PROTOCOL.md` in this package.
|
|
|
19
19
|
import { PFError, isPFError, localFailure } from '@ts-pf/protocol'
|
|
20
20
|
|
|
21
21
|
throw new PFError({ code: 'UNAUTHORIZED', status: 401, message: 'Nope' })
|
|
22
|
+
new PFError({ code: 'TEAPOT' }).status // undefined — no default
|
|
22
23
|
// localFailure('Request aborted') → INTERNAL, local: true, status: 0 (not on the wire)
|
|
23
24
|
```
|
|
24
25
|
|
|
25
|
-
Envelope: `{ input }`, `{ ok: true, output }`, `{ ok: false, error: { code, message, data? } }`. Discriminator is JSON `error.code`.
|
|
26
|
+
Envelope: `{ input }`, `{ ok: true, output }`, `{ ok: false, error: { code, message, data? } }`. Discriminator is JSON `error.code`.
|
|
27
|
+
|
|
28
|
+
`PFError.status?: number` is transport metadata, not identity. It is the HTTP response status on a unary Fetch failure, `undefined` on message transports and after a stream has started, and `0` on local failures. Guard it (`err.status ?? 400`); switch on `code`. The HTTP default (400 for a code with no status) is applied by `httpStatus()` in `@ts-pf/http`, not by the constructor.
|
|
29
|
+
|
|
30
|
+
Writing a codec or transport adapter: `toWireError(error)` is the failure envelope's `error` for any thrown value (`PFError` → `toJSON()`; anything else → fixed `{ code: 'INTERNAL', message: 'Internal server error' }`, no stack, no message leak — log the original value yourself). `isAsyncIterable(value)` is the shared "is this a stream" predicate (object or function with a `Symbol.asyncIterator` function). A `ReadableStream` is async-iterable on Node; a site that wants bytes excludes it explicitly. `defined({ signal, headers })` is the shared way to forward optional fields under `exactOptionalPropertyTypes`: it returns a copy without the `undefined`-valued keys, typed so those keys are optional (`{ ...required, ...defined({ signal }) }`).
|
|
26
31
|
|
|
27
32
|
## API
|
|
28
33
|
|
|
29
34
|
- `PFError`, `PFErrorInit`, `isPFError`, `localFailure`, `ProtocolErrorCode`
|
|
30
|
-
- `PROTOCOL_VERSION`, `RpcRequest`, `RpcResponse`, `RpcSuccess`, `RpcFailure`, `PFResultPromise`
|
|
35
|
+
- `PROTOCOL_MAJOR` (`1`, the wire major: `v` on message `hello`), `PROTOCOL_VERSION` (`'1'`, its string: the `x-ts-pf-protocol` header value), `RpcRequest`, `RpcResponse`, `RpcSuccess`, `RpcFailure`, `PFResultPromise`
|
|
36
|
+
- `toWireError`, `isAsyncIterable`
|
|
37
|
+
- `defined`, `Defined` — `defined(obj)` drops the keys whose value is `undefined`, so `{ ...required, ...defined({ signal }) }` builds an `exactOptionalPropertyTypes` option bag without one spread per field
|
|
31
38
|
|
|
32
39
|
## Pair with
|
|
33
40
|
|