@ts-pf/protocol 0.2.1 → 0.5.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.
@@ -0,0 +1,149 @@
1
+ /** Any JSON value, as written in a fixture. */
2
+ export type JsonValue = null | boolean | number | string | JsonValue[] | {
3
+ [key: string]: JsonValue;
4
+ };
5
+ /** One fixture file. `protocol` is the wire major; `revision` the PROTOCOL.md revision it was written against. */
6
+ export type ConformanceFile<TCase> = {
7
+ protocol: number;
8
+ revision: string;
9
+ binding: 'http' | 'http-stream' | 'ws';
10
+ description: string;
11
+ cases: TCase[];
12
+ };
13
+ /** The `error` of a failure envelope. */
14
+ export type ConformanceError = {
15
+ code: string;
16
+ message: string;
17
+ data?: JsonValue;
18
+ };
19
+ /** The request the client must send. `bodyOneOf` lists equally valid bodies (no input: `{}` or `{"input":null}`). */
20
+ export type ConformanceRequest = {
21
+ method: 'POST';
22
+ path: string;
23
+ headers: Record<string, string>;
24
+ body?: JsonValue;
25
+ bodyOneOf?: JsonValue[];
26
+ };
27
+ export type HttpExpect = {
28
+ kind: 'success';
29
+ output?: JsonValue;
30
+ } | ({
31
+ kind: 'error';
32
+ } & ConformanceError) | {
33
+ kind: 'transport-failure';
34
+ } | {
35
+ kind: 'version-mismatch';
36
+ };
37
+ export type HttpStreamEnd = {
38
+ kind: 'eof';
39
+ } | ({
40
+ kind: 'error';
41
+ } & ConformanceError) | {
42
+ kind: 'transport-failure';
43
+ };
44
+ export type HttpStreamExpect = HttpExpect | {
45
+ kind: 'stream';
46
+ items: Array<{
47
+ output?: JsonValue;
48
+ }>;
49
+ end: HttpStreamEnd;
50
+ };
51
+ /** One request / response pair of `http.json` (`TExpect = HttpExpect`) or `http-stream.json` (`HttpStreamExpect`). */
52
+ export type HttpCase<TExpect = HttpExpect> = {
53
+ id: string;
54
+ description: string;
55
+ request: {
56
+ procedure: string[];
57
+ input?: JsonValue;
58
+ };
59
+ expectRequest: ConformanceRequest;
60
+ response: {
61
+ status: number;
62
+ headers: Record<string, string>;
63
+ body: string;
64
+ };
65
+ expect: TExpect;
66
+ };
67
+ export type WsExpectation = {
68
+ call: string;
69
+ outcome: 'value';
70
+ output?: JsonValue;
71
+ } | {
72
+ call: string;
73
+ outcome: 'error';
74
+ error: ConformanceError;
75
+ } | {
76
+ call: string;
77
+ outcome: 'stream';
78
+ } | {
79
+ call: string;
80
+ outcome: 'local-failure';
81
+ reference?: string;
82
+ } | {
83
+ stream: string;
84
+ outcome: 'item';
85
+ output?: JsonValue;
86
+ } | {
87
+ stream: string;
88
+ outcome: 'done';
89
+ } | {
90
+ stream: string;
91
+ outcome: 'error';
92
+ error: ConformanceError;
93
+ } | {
94
+ stream: string;
95
+ outcome: 'local-failure';
96
+ reference?: string;
97
+ } | {
98
+ handshake: 'error';
99
+ error: ConformanceError;
100
+ } | {
101
+ handshake: 'local-failure';
102
+ reference?: string;
103
+ } | {
104
+ closed: {
105
+ code?: number;
106
+ };
107
+ };
108
+ export type WsStep = {
109
+ send: {
110
+ [key: string]: JsonValue;
111
+ };
112
+ } | {
113
+ receive: {
114
+ [key: string]: JsonValue;
115
+ };
116
+ } | {
117
+ receiveText: string;
118
+ } | {
119
+ receiveBinary: string;
120
+ } | {
121
+ serverClose: {
122
+ code?: number;
123
+ };
124
+ } | {
125
+ call: string;
126
+ path: string[];
127
+ input?: JsonValue;
128
+ inputStream?: JsonValue[];
129
+ } | {
130
+ abort: string;
131
+ } | {
132
+ expect: WsExpectation;
133
+ };
134
+ /** One frame script of `ws.json`, from the client's side. */
135
+ export type WsCase = {
136
+ id: string;
137
+ description: string;
138
+ options?: {
139
+ meta?: JsonValue;
140
+ };
141
+ steps: WsStep[];
142
+ };
143
+ /** `conformance/http.json`: unary JSON over HTTP. */
144
+ export declare const httpConformance: ConformanceFile<HttpCase>;
145
+ /** `conformance/http-stream.json`: `application/jsonl` output streams. */
146
+ export declare const httpStreamConformance: ConformanceFile<HttpCase<HttpStreamExpect>>;
147
+ /** `conformance/ws.json`: WebSocket frame scripts (also stdio / MessagePort, minus binary and close codes). */
148
+ export declare const wsConformance: ConformanceFile<WsCase>;
149
+ //# sourceMappingURL=conformance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conformance.d.ts","sourceRoot":"","sources":["../src/conformance.ts"],"names":[],"mappings":"AAQA,+CAA+C;AAC/C,MAAM,MAAM,SAAS,GACjB,IAAI,GACJ,OAAO,GACP,MAAM,GACN,MAAM,GACN,SAAS,EAAE,GACX;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAA;AAEhC,kHAAkH;AAClH,MAAM,MAAM,eAAe,CAAC,KAAK,IAAI;IACnC,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,OAAO,EAAE,MAAM,GAAG,aAAa,GAAG,IAAI,CAAA;IACtC,WAAW,EAAE,MAAM,CAAA;IACnB,KAAK,EAAE,KAAK,EAAE,CAAA;CACf,CAAA;AAED,yCAAyC;AACzC,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,SAAS,CAAA;CACjB,CAAA;AAED,qHAAqH;AACrH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC/B,IAAI,CAAC,EAAE,SAAS,CAAA;IAChB,SAAS,CAAC,EAAE,SAAS,EAAE,CAAA;CACxB,CAAA;AAED,MAAM,MAAM,UAAU,GAClB;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,MAAM,CAAC,EAAE,SAAS,CAAA;CAAE,GACvC,CAAC;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,GAAG,gBAAgB,CAAC,GACtC;IAAE,IAAI,EAAE,mB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SAAS,EAAE,CAAA;CAC1B,GACD;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,GACjB;IAAE,MAAM,EAAE,aAAa,CAAA;CAAE,CAAA;AAE7B,6DAA6D;AAC7D,MAAM,MAAM,MAAM,GAAG;IACnB,EAAE,EAAE,MAAM,CAAA;IACV,WAAW,EAAE,MAAM,CAAA;IACnB,OAAO,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,SAAS,CAAA;KAAE,CAAA;IAC9B,KAAK,EAAE,MAAM,EAAE,CAAA;CAChB,CAAA;AAED,qDAAqD;AACrD,eAAO,MAAM,eAAe,EAAW,eAAe,CAAC,QAAQ,CAAC,CAAA;AAEhE,0EAA0E;AAC1E,eAAO,MAAM,qBAAqB,EAAiB,eAAe,CAChE,QAAQ,CAAC,gBAAgB,CAAC,CAC3B,CAAA;AAED,+GAA+G;AAC/G,eAAO,MAAM,aAAa,EAAS,eAAe,CAAC,MAAM,CAAC,CAAA"}
@@ -0,0 +1,14 @@
1
+ // Language-neutral conformance fixtures for PROTOCOL.md (see
2
+ // conformance/README.md). The JSON files are the source of truth and ship in
3
+ // the package; this module only re-exports them with types, for TypeScript
4
+ // runners. Other runtimes read the JSON files directly.
5
+ import http from '../conformance/http.json' with { type: 'json' };
6
+ import httpStream from '../conformance/http-stream.json' with { type: 'json' };
7
+ import ws from '../conformance/ws.json' with { type: 'json' };
8
+ /** `conformance/http.json`: unary JSON over HTTP. */
9
+ export const httpConformance = http;
10
+ /** `conformance/http-stream.json`: `application/jsonl` output streams. */
11
+ export const httpStreamConformance = httpStream;
12
+ /** `conformance/ws.json`: WebSocket frame scripts (also stdio / MessagePort, minus binary and close codes). */
13
+ export const wsConformance = ws;
14
+ //# sourceMappingURL=conformance.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conformance.js","sourceRoot":"","sources":["../src/conformance.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,6EAA6E;AAC7E,2EAA2E;AAC3E,wDAAwD;AACxD,OAAO,IAAI,MAAM,0BAA0B,CAAC,OAAO,IAAI,EAAE,MAAM,EAAE,CAAA;AACjE,OAAO,UAAU,MAAM,iCAAiC,CAAC,OAAO,IAAI,EAAE,MAAM,EAAE,CAAA;AAC9E,OAAO,EAAE,MAAM,wBAAwB,CAAC,OAAO,IAAI,EAAE,MAAM,EAAE,CAAA;AAqG7D,qDAAqD;AACrD,MAAM,CAAC,MAAM,eAAe,GAAG,IAAiC,CAAA;AAEhE,0EAA0E;AAC1E,MAAM,CAAC,MAAM,qBAAqB,GAAG,UAEpC,CAAA;AAED,+GAA+G;AAC/G,MAAM,CAAC,MAAM,aAAa,GAAG,EAA6B,CAAA"}
package/dist/error.d.ts CHANGED
@@ -24,6 +24,7 @@ export declare class PFError<TCode extends string = string, TData = unknown> ext
24
24
  data?: TData;
25
25
  };
26
26
  }
27
+ /** `true` for a `PFError` from this or any other copy of `@ts-pf/protocol`. */
27
28
  export declare function isPFError(value: unknown): value is PFError;
28
29
  export declare function localFailure(message: string, cause?: unknown): PFError;
29
30
  export declare const ProtocolErrorCode: {
@@ -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;;;;;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,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K,EAAE,KAAK,CAAC;IAoB3C,MAAM,IAAI;QAAE,IAAI,EAAE,KAAK,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,KAAK,CAAA;KAAE;CAUzD;AAWD,+EAA+E;AAC/E,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,OAAO,CAO1D;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
@@ -34,8 +34,19 @@ export class PFError extends Error {
34
34
  return json;
35
35
  }
36
36
  }
37
+ // Cross-copy brand. Two copies of this package (version skew, a bundler that
38
+ // inlines one) make `instanceof` fail across the boundary, so a typed error
39
+ // would reach `toWireError` as an unknown throw. The registry symbol is the
40
+ // same in every copy. It lives on the prototype, not on each instance, so
41
+ // construction stays as cheap as before and instances gain no own property.
42
+ const PF_ERROR_BRAND = Symbol.for('ts-pf.PFError');
43
+ Object.defineProperty(PFError.prototype, PF_ERROR_BRAND, { value: true });
44
+ /** `true` for a `PFError` from this or any other copy of `@ts-pf/protocol`. */
37
45
  export function isPFError(value) {
38
- return value instanceof PFError;
46
+ return (value instanceof PFError ||
47
+ (typeof value === 'object' &&
48
+ value !== null &&
49
+ value[PF_ERROR_BRAND] === true));
39
50
  }
40
51
  export function localFailure(message, cause) {
41
52
  return new PFError({
package/dist/error.js.map CHANGED
@@ -1 +1 @@
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;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.ts CHANGED
@@ -2,5 +2,6 @@ export { type Defined, defined } from './defined.js';
2
2
  export { type PFResultPromise, PROTOCOL_MAJOR, PROTOCOL_VERSION, type RpcFailure, type RpcRequest, type RpcResponse, type RpcSuccess, } from './envelope.js';
3
3
  export { isPFError, localFailure, PFError, type PFErrorInit, ProtocolErrorCode, } from './error.js';
4
4
  export { isAsyncIterable } from './is-async-iterable.js';
5
+ export { isUnexpectedError } from './is-unexpected-error.js';
5
6
  export { toWireError } from './wire-error.js';
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
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"}
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,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA"}
package/dist/index.js CHANGED
@@ -2,5 +2,6 @@ export { defined } from './defined.js';
2
2
  export { PROTOCOL_MAJOR, PROTOCOL_VERSION, } from './envelope.js';
3
3
  export { isPFError, localFailure, PFError, ProtocolErrorCode, } from './error.js';
4
4
  export { isAsyncIterable } from './is-async-iterable.js';
5
+ export { isUnexpectedError } from './is-unexpected-error.js';
5
6
  export { toWireError } from './wire-error.js';
6
7
  //# 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,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"}
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,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * `true` for a thrown value that is not a `PFError`, or a `PFError` whose code
3
+ * is `INTERNAL`: the failures nobody declared. This is what `FetchHandler`
4
+ * (and `@ts-pf/mcp`) log by default when no `onError` is given; call it in a
5
+ * custom `onError` to keep the same filter. A `cause` is not unwrapped.
6
+ */
7
+ export declare function isUnexpectedError(error: unknown): boolean;
8
+ //# sourceMappingURL=is-unexpected-error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-unexpected-error.d.ts","sourceRoot":"","sources":["../src/is-unexpected-error.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAEzD"}
@@ -0,0 +1,11 @@
1
+ import { isPFError } from './error.js';
2
+ /**
3
+ * `true` for a thrown value that is not a `PFError`, or a `PFError` whose code
4
+ * is `INTERNAL`: the failures nobody declared. This is what `FetchHandler`
5
+ * (and `@ts-pf/mcp`) log by default when no `onError` is given; call it in a
6
+ * custom `onError` to keep the same filter. A `cause` is not unwrapped.
7
+ */
8
+ export function isUnexpectedError(error) {
9
+ return !isPFError(error) || error.code === 'INTERNAL';
10
+ }
11
+ //# sourceMappingURL=is-unexpected-error.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-unexpected-error.js","sourceRoot":"","sources":["../src/is-unexpected-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAEtC;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,KAAc;IAC9C,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,IAAI,KAAK,UAAU,CAAA;AACvD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ts-pf/protocol",
3
- "version": "0.2.1",
3
+ "version": "0.5.0",
4
4
  "description": "Portable JSON RPC envelope, PFError, and PROTOCOL_VERSION",
5
5
  "keywords": [
6
6
  "ts-pf",
@@ -33,6 +33,10 @@
33
33
  ".": {
34
34
  "types": "./dist/index.d.ts",
35
35
  "import": "./dist/index.js"
36
+ },
37
+ "./conformance": {
38
+ "types": "./dist/conformance.d.ts",
39
+ "import": "./dist/conformance.js"
36
40
  }
37
41
  },
38
42
  "publishConfig": {
@@ -40,6 +44,7 @@
40
44
  },
41
45
  "files": [
42
46
  "dist",
47
+ "conformance",
43
48
  "PROTOCOL.md",
44
49
  "skills"
45
50
  ],
@@ -47,6 +52,6 @@
47
52
  "build": "tsc -p tsconfig.build.json",
48
53
  "test": "vitest run",
49
54
  "type-check": "tsc --noEmit",
50
- "lint": "biome check src tests"
55
+ "lint": "biome check src tests conformance"
51
56
  }
52
57
  }
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: ts-pf-protocol
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."
3
+ description: "Use when throwing or handling ts-pf PFError, localFailure, PROTOCOL_MAJOR / PROTOCOL_VERSION, or the JSON RPC envelope, filtering errors in an onError logger, or writing a codec / adapter that needs toWireError or isAsyncIterable, or testing a client in any language against the protocol conformance fixtures. Triggers: @ts-pf/protocol, PFError, isPFError, localFailure, ProtocolErrorCode, PROTOCOL_MAJOR, PROTOCOL_VERSION, toWireError, isAsyncIterable, isUnexpectedError, onError filter, @ts-pf/protocol/conformance, httpConformance, wsConformance, conformance fixtures."
4
4
  ---
5
5
 
6
6
  # @ts-pf/protocol
@@ -11,7 +11,7 @@ Install: `npm i @ts-pf/protocol`
11
11
 
12
12
  Link for agents: `npx skills experimental_sync -y`
13
13
 
14
- Wire spec ships as `PROTOCOL.md` in this package.
14
+ Wire spec ships as `PROTOCOL.md` in this package (revision 1.2). Building a client in another language: start from its "HTTP client checklist" / "WebSocket client checklist" and "Classifying an HTTP response" sections; TypeScript-only conventions (`local`, `status: 0`, `isLocalFailure`) are in its appendix. Prove the client with the language-neutral fixtures in this package's `conformance/` (`http.json`, `http-stream.json`, `ws.json`; plain JSON versioned with the spec revision; `conformance/README.md` says how to replay them). TypeScript runners import them typed from `@ts-pf/protocol/conformance`.
15
15
 
16
16
  ## Do
17
17
 
@@ -23,18 +23,22 @@ new PFError({ code: 'TEAPOT' }).status // undefined — no default
23
23
  // localFailure('Request aborted') → INTERNAL, local: true, status: 0 (not on the wire)
24
24
  ```
25
25
 
26
+ Detect failures with `isPFError(error)`, not `instanceof PFError`: it also matches a `PFError` from another installed copy of this package (version skew, a bundler that inlined one).
27
+
26
28
  Envelope: `{ input }`, `{ ok: true, output }`, `{ ok: false, error: { code, message, data? } }`. Discriminator is JSON `error.code`.
27
29
 
28
30
  `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
31
 
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 }) }`).
32
+ 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). The message `hello-error` for a throwing context factory uses the same mapping. `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 }) }`).
31
33
 
32
34
  ## API
33
35
 
34
36
  - `PFError`, `PFErrorInit`, `isPFError`, `localFailure`, `ProtocolErrorCode`
35
37
  - `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
38
  - `toWireError`, `isAsyncIterable`
39
+ - `isUnexpectedError(error)` — `true` for a non-`PFError` or an `INTERNAL` `PFError` (no `cause` unwrapping): what `FetchHandler` and `@ts-pf/mcp` log by default when no `onError` is given, for a custom `onError` that keeps the same filter
37
40
  - `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
41
+ - `@ts-pf/protocol/conformance`: `httpConformance`, `httpStreamConformance`, `wsConformance` (the `conformance/*.json` fixtures, typed) and their types `ConformanceFile`, `HttpCase`, `HttpExpect`, `HttpStreamExpect`, `HttpStreamEnd`, `WsCase`, `WsStep`, `WsExpectation`, `ConformanceRequest`, `ConformanceError`, `JsonValue`
38
42
 
39
43
  ## Pair with
40
44
 
@@ -45,5 +49,6 @@ Writing a codec or transport adapter: `toWireError(error)` is the failure envelo
45
49
  ## Don't
46
50
 
47
51
  - `JSONCodec`, `RpcCodec`, `PROTOCOL_HEADER`, or `FetchHandler` here.
52
+ - `instanceof PFError` to detect a failure (misses a second copy of the package); use `isPFError`.
48
53
  - Put `status`, `cause`, or `local` in the JSON envelope (`toJSON` omits them).
49
54
  - Redeclare `VALIDATION`, `INTERNAL`, `BAD_REQUEST`, `METHOD_NOT_ALLOWED`, `PAYLOAD_TOO_LARGE` on `.errors()`.