@snap-rail/protocol 0.0.0-stage → 0.1.3
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 +202 -0
- package/lib/contract.js +54 -0
- package/lib/index.js +182 -0
- package/lib/rpc.js +67 -0
- package/lib/types/client.d.ts +66 -0
- package/lib/types/client.js +93 -0
- package/lib/types/contract.d.ts +99 -0
- package/lib/types/contract.js +45 -0
- package/lib/types/frames.d.ts +26 -0
- package/lib/types/frames.js +20 -0
- package/lib/types/index.d.ts +15 -0
- package/lib/types/index.js +15 -0
- package/lib/types/methods.d.ts +100 -0
- package/lib/types/methods.js +18 -0
- package/lib/types/rpc.d.ts +132 -0
- package/lib/types/rpc.js +57 -0
- package/lib/types/schemas.d.ts +25 -0
- package/lib/types/schemas.js +43 -0
- package/lib/types/surfaces.d.ts +43 -0
- package/lib/types/surfaces.js +43 -0
- package/package.json +25 -3
- package/README.md +0 -3
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Single-source wire contracts: one {@link defineContract} declaration per
|
|
3
|
+
* domain yields everything a domain's wire surface needs — the rows that
|
|
4
|
+
* merge into the protocol's open maps (via `interface RpcMethodMap extends
|
|
5
|
+
* MethodRows<typeof contract> {}` in the same module), the request schemas
|
|
6
|
+
* that ride with registration, and the frame payload schemas for
|
|
7
|
+
* registration and client parsing. The hand-written trio (Api interface +
|
|
8
|
+
* merge rows + schema table) said the same thing three times; here it is
|
|
9
|
+
* said once.
|
|
10
|
+
*
|
|
11
|
+
* A row's response value type is declared through the phantom `res` field
|
|
12
|
+
* (the builders lie about it so the type is reachable without a runtime
|
|
13
|
+
* schema); runtime rows carry only `request` and, optionally, `response`.
|
|
14
|
+
*
|
|
15
|
+
* @module @snap-rail/protocol/contract
|
|
16
|
+
*/
|
|
17
|
+
import type { z } from 'zod';
|
|
18
|
+
import type { RpcId, RpcResponse } from './rpc.ts';
|
|
19
|
+
/** One method row of a domain contract. */
|
|
20
|
+
export interface MethodRow<Req extends z.ZodType = z.ZodType, Res = unknown> {
|
|
21
|
+
/** The request schema — the trust-boundary validation at the host. */
|
|
22
|
+
request: Req;
|
|
23
|
+
/** Optional runtime response schema: adds the dispatch-time response
|
|
24
|
+
* self-check and the JSON Schema projection in `rpc.describe`. Absent in
|
|
25
|
+
* most rows: the response value type rides the phantom instead. */
|
|
26
|
+
response?: z.ZodType<Res>;
|
|
27
|
+
/** Phantom carrying the declared response value type; never present at
|
|
28
|
+
* runtime — the `method()` builders stamp it on the row's type only. */
|
|
29
|
+
readonly res: Res;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Open one method row: the request schema is mandatory (it is the
|
|
33
|
+
* trust-boundary validator), the response value type is declared through
|
|
34
|
+
* `.ret<T>()` or, with a runtime schema, `.res(schema)`.
|
|
35
|
+
*
|
|
36
|
+
* @param request - the request schema.
|
|
37
|
+
*/
|
|
38
|
+
export declare function method<Req extends z.ZodType>(request: Req): {
|
|
39
|
+
/** Declare the response value type without a runtime schema. */
|
|
40
|
+
ret<Res>(): MethodRow<Req, Res>;
|
|
41
|
+
/** Declare the response value type and a schema that validates it. */
|
|
42
|
+
res<Res>(response: z.ZodType<Res>): MethodRow<Req, Res>;
|
|
43
|
+
};
|
|
44
|
+
/** The runtime shape every domain contract shares (what the gateway serves). */
|
|
45
|
+
export interface DomainContract {
|
|
46
|
+
/** The wire domain the contract belongs to (diagnostic metadata). */
|
|
47
|
+
readonly domain: string;
|
|
48
|
+
/** Method rows keyed by wire name (`domain.resource.verb`). */
|
|
49
|
+
readonly methods: Readonly<Record<string, MethodRow>>;
|
|
50
|
+
/** Frame payload schemas keyed by wire name (`domain/event`). */
|
|
51
|
+
readonly frames: Readonly<Record<string, z.ZodType>>;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Declare one domain's wire surface. Keys are full wire names; every
|
|
55
|
+
* conventional call site passes both tables (`methods: {}` when the domain
|
|
56
|
+
* only pushes frames, `frames: {}` when it only serves methods).
|
|
57
|
+
*
|
|
58
|
+
* @param domain - the wire domain (diagnostic metadata; claims are separate).
|
|
59
|
+
* @param def - the method and frame tables.
|
|
60
|
+
*/
|
|
61
|
+
export declare function defineContract<const M extends Readonly<Record<string, MethodRow>>, const F extends Readonly<Record<string, z.ZodType>>>(domain: string, def: {
|
|
62
|
+
methods: M;
|
|
63
|
+
frames: F;
|
|
64
|
+
}): {
|
|
65
|
+
readonly domain: string;
|
|
66
|
+
readonly methods: M;
|
|
67
|
+
readonly frames: F;
|
|
68
|
+
};
|
|
69
|
+
/** The wire method signature one contract row yields (an `RpcMethodMap` row). */
|
|
70
|
+
export type WireMethodOf<Row extends MethodRow> = (payload: z.output<Row['request']>) => Promise<RpcResponse<Row['res']>>;
|
|
71
|
+
/**
|
|
72
|
+
* The `RpcMethodMap` rows a contract yields. Merge them from the contract's
|
|
73
|
+
* own module so importers of that module see the rows (the open-world rule):
|
|
74
|
+
*
|
|
75
|
+
* ```ts
|
|
76
|
+
* declare module '@snap-rail/protocol' {
|
|
77
|
+
* interface RpcMethodMap extends MethodRows<typeof pluginsContract> {}
|
|
78
|
+
* }
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export type MethodRows<C extends DomainContract> = {
|
|
82
|
+
[K in keyof C['methods'] & string]: WireMethodOf<C['methods'][K]>;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* The `FrameMap` rows a contract yields (payload = schema output); merged
|
|
86
|
+
* with `interface FrameMap extends FrameRows<typeof contract> {}`.
|
|
87
|
+
*/
|
|
88
|
+
export type FrameRows<C extends DomainContract> = {
|
|
89
|
+
[K in keyof C['frames'] & string]: z.output<C['frames'][K]>;
|
|
90
|
+
};
|
|
91
|
+
/** One method handler as {@link ContractHandlers} requires: parsed payload
|
|
92
|
+
* in, response value out (business failures throw `RpcBusinessError`). */
|
|
93
|
+
export type ContractHandlerOf<Row extends MethodRow> = (payload: z.output<Row['request']>, rpcId: RpcId) => Row['res'] | Promise<Row['res']>;
|
|
94
|
+
/** The handler table `ctx.rpc.serveContract` requires for one contract,
|
|
95
|
+
* keyed by the same wire names as the contract's method rows. */
|
|
96
|
+
export type ContractHandlers<C extends DomainContract> = {
|
|
97
|
+
[K in keyof C['methods'] & string]: ContractHandlerOf<C['methods'][K]>;
|
|
98
|
+
};
|
|
99
|
+
//# sourceMappingURL=contract.d.ts.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Single-source wire contracts: one {@link defineContract} declaration per
|
|
3
|
+
* domain yields everything a domain's wire surface needs — the rows that
|
|
4
|
+
* merge into the protocol's open maps (via `interface RpcMethodMap extends
|
|
5
|
+
* MethodRows<typeof contract> {}` in the same module), the request schemas
|
|
6
|
+
* that ride with registration, and the frame payload schemas for
|
|
7
|
+
* registration and client parsing. The hand-written trio (Api interface +
|
|
8
|
+
* merge rows + schema table) said the same thing three times; here it is
|
|
9
|
+
* said once.
|
|
10
|
+
*
|
|
11
|
+
* A row's response value type is declared through the phantom `res` field
|
|
12
|
+
* (the builders lie about it so the type is reachable without a runtime
|
|
13
|
+
* schema); runtime rows carry only `request` and, optionally, `response`.
|
|
14
|
+
*
|
|
15
|
+
* @module @snap-rail/protocol/contract
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Open one method row: the request schema is mandatory (it is the
|
|
19
|
+
* trust-boundary validator), the response value type is declared through
|
|
20
|
+
* `.ret<T>()` or, with a runtime schema, `.res(schema)`.
|
|
21
|
+
*
|
|
22
|
+
* @param request - the request schema.
|
|
23
|
+
*/
|
|
24
|
+
export function method(request) {
|
|
25
|
+
return {
|
|
26
|
+
ret() {
|
|
27
|
+
return { request };
|
|
28
|
+
},
|
|
29
|
+
res(response) {
|
|
30
|
+
return { request, response };
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Declare one domain's wire surface. Keys are full wire names; every
|
|
36
|
+
* conventional call site passes both tables (`methods: {}` when the domain
|
|
37
|
+
* only pushes frames, `frames: {}` when it only serves methods).
|
|
38
|
+
*
|
|
39
|
+
* @param domain - the wire domain (diagnostic metadata; claims are separate).
|
|
40
|
+
* @param def - the method and frame tables.
|
|
41
|
+
*/
|
|
42
|
+
export function defineContract(domain, def) {
|
|
43
|
+
return { domain, methods: def.methods, frames: def.frames };
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=contract.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The frame table base: every push frame (a host-initiated `ServerRequest`)
|
|
3
|
+
* is keyed by its wire frame name in `FrameMap`, the twin of
|
|
4
|
+
* {@link RpcMethodMap} for the host-initiated quadrant. This module holds
|
|
5
|
+
* the OPEN merge base only — every domain's rows live with its owner's
|
|
6
|
+
* contract module (field → `@snap-rail/equipment` `./wire`, ModbusTCP →
|
|
7
|
+
* `@snap-rail/driver-modbus/contract`, station →
|
|
8
|
+
* `@snap-rail/station-rpc/contract`, production stats →
|
|
9
|
+
* `@snap-rail/process-production/contract`). A program sees the rows of
|
|
10
|
+
* every contract it imports — the open-world rule.
|
|
11
|
+
*
|
|
12
|
+
* Naming: frames are `domain/event` — kebab-case segments joined by `/` —
|
|
13
|
+
* while methods are `domain.resource.verb` joined by `.`. The first frame
|
|
14
|
+
* segment is the owning domain, the same vocabulary method domains use;
|
|
15
|
+
* `ctx.rpc.claimDomain` guards both.
|
|
16
|
+
*
|
|
17
|
+
* @module @snap-rail/protocol/frames
|
|
18
|
+
*/
|
|
19
|
+
/** Payload type per frame name; open for declaration merging. */
|
|
20
|
+
export interface FrameMap {
|
|
21
|
+
}
|
|
22
|
+
/** Frame names on the wire. */
|
|
23
|
+
export type FrameName = keyof FrameMap & string;
|
|
24
|
+
/** The payload type of a frame, derived from its row. */
|
|
25
|
+
export type FramePayload<K extends FrameName> = FrameMap[K];
|
|
26
|
+
//# sourceMappingURL=frames.d.ts.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The frame table base: every push frame (a host-initiated `ServerRequest`)
|
|
3
|
+
* is keyed by its wire frame name in `FrameMap`, the twin of
|
|
4
|
+
* {@link RpcMethodMap} for the host-initiated quadrant. This module holds
|
|
5
|
+
* the OPEN merge base only — every domain's rows live with its owner's
|
|
6
|
+
* contract module (field → `@snap-rail/equipment` `./wire`, ModbusTCP →
|
|
7
|
+
* `@snap-rail/driver-modbus/contract`, station →
|
|
8
|
+
* `@snap-rail/station-rpc/contract`, production stats →
|
|
9
|
+
* `@snap-rail/process-production/contract`). A program sees the rows of
|
|
10
|
+
* every contract it imports — the open-world rule.
|
|
11
|
+
*
|
|
12
|
+
* Naming: frames are `domain/event` — kebab-case segments joined by `/` —
|
|
13
|
+
* while methods are `domain.resource.verb` joined by `.`. The first frame
|
|
14
|
+
* segment is the owning domain, the same vocabulary method domains use;
|
|
15
|
+
* `ctx.rpc.claimDomain` guards both.
|
|
16
|
+
*
|
|
17
|
+
* @module @snap-rail/protocol/frames
|
|
18
|
+
*/
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=frames.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The channel-independent RPC contract between host and clients: the
|
|
3
|
+
* four-quadrant message model, the open method and frame merge bases,
|
|
4
|
+
* boundary schemas, and the client carrier family.
|
|
5
|
+
*
|
|
6
|
+
* @module @snap-rail/protocol
|
|
7
|
+
*/
|
|
8
|
+
export * from './rpc.ts';
|
|
9
|
+
export * from './methods.ts';
|
|
10
|
+
export * from './surfaces.ts';
|
|
11
|
+
export * from './frames.ts';
|
|
12
|
+
export * from './contract.ts';
|
|
13
|
+
export * from './schemas.ts';
|
|
14
|
+
export * from './client.ts';
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The channel-independent RPC contract between host and clients: the
|
|
3
|
+
* four-quadrant message model, the open method and frame merge bases,
|
|
4
|
+
* boundary schemas, and the client carrier family.
|
|
5
|
+
*
|
|
6
|
+
* @module @snap-rail/protocol
|
|
7
|
+
*/
|
|
8
|
+
export * from "./rpc.js";
|
|
9
|
+
export * from "./methods.js";
|
|
10
|
+
export * from "./surfaces.js";
|
|
11
|
+
export * from "./frames.js";
|
|
12
|
+
export * from "./contract.js";
|
|
13
|
+
export * from "./schemas.js";
|
|
14
|
+
export * from "./client.js";
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The platform domain's method rows plus the open bases themselves: the
|
|
3
|
+
* wire's platform surface is `host.*` and `window.*` (transport-adjacent,
|
|
4
|
+
* owned by the gateway and the shell). Every other domain's rows live with
|
|
5
|
+
* their owners and merge in from their contract modules:
|
|
6
|
+
* field → `@snap-rail/equipment` (`./wire`), ModbusTCP →
|
|
7
|
+
* `@snap-rail/driver-modbus/contract`, station → `@snap-rail/station-rpc/contract`,
|
|
8
|
+
* plugin admin → `@snap-rail/app-boot/contract`. A program sees the rows of
|
|
9
|
+
* every contract it imports — the open-world rule.
|
|
10
|
+
*
|
|
11
|
+
* Reserved-method discipline: the map holds only implemented methods; an
|
|
12
|
+
* unknown method fails loud at dispatch (`bad-request`) — no
|
|
13
|
+
* not-implemented fallback code.
|
|
14
|
+
*
|
|
15
|
+
* @module @snap-rail/protocol/methods
|
|
16
|
+
*/
|
|
17
|
+
import type { RpcResponse } from './rpc.ts';
|
|
18
|
+
/** Host-level introspection. */
|
|
19
|
+
export interface HostApi {
|
|
20
|
+
/** Describe the running application: identity for handshakes and About panels. */
|
|
21
|
+
describe(payload: {}): Promise<RpcResponse<{
|
|
22
|
+
name: string;
|
|
23
|
+
version: string;
|
|
24
|
+
bin: string;
|
|
25
|
+
}>>;
|
|
26
|
+
}
|
|
27
|
+
/** One wire domain as `rpc.describe` serves it. */
|
|
28
|
+
export interface DomainInfo {
|
|
29
|
+
/** The claimed prefix (`field`, `field.modbus`). */
|
|
30
|
+
prefix: string;
|
|
31
|
+
/** The claiming plugin's fiber name. */
|
|
32
|
+
owner: string;
|
|
33
|
+
}
|
|
34
|
+
/** One registered method as `rpc.describe` serves it. */
|
|
35
|
+
export interface MethodInfo {
|
|
36
|
+
name: string;
|
|
37
|
+
/** The request schema as JSON Schema (omitted when not representable). */
|
|
38
|
+
requestSchema?: unknown;
|
|
39
|
+
/** The optional response schema as JSON Schema. */
|
|
40
|
+
responseSchema?: unknown;
|
|
41
|
+
}
|
|
42
|
+
/** One registered frame as `rpc.describe` serves it. */
|
|
43
|
+
export interface FrameInfo {
|
|
44
|
+
name: string;
|
|
45
|
+
/** The payload schema as JSON Schema (omitted when not representable). */
|
|
46
|
+
payloadSchema?: unknown;
|
|
47
|
+
}
|
|
48
|
+
/** Wire-level capability discovery. */
|
|
49
|
+
export interface RpcIntrospectApi {
|
|
50
|
+
/** Every live domain claim, method route, and frame registration — the
|
|
51
|
+
* surface an external tool (settings UI, agent, future plugin installer)
|
|
52
|
+
* lists before calling. */
|
|
53
|
+
describe(payload: {}): Promise<RpcResponse<{
|
|
54
|
+
domains: readonly DomainInfo[];
|
|
55
|
+
methods: readonly MethodInfo[];
|
|
56
|
+
frames: readonly FrameInfo[];
|
|
57
|
+
}>>;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Native window controls for frameless shells. Controls drive the main
|
|
61
|
+
* window.
|
|
62
|
+
*/
|
|
63
|
+
export interface WindowApi {
|
|
64
|
+
/** Apply a window action (`minimize`, `toggle-maximize`, `close`); `false` when no window exists to act on. */
|
|
65
|
+
control(payload: {
|
|
66
|
+
action: 'minimize' | 'toggle-maximize' | 'close';
|
|
67
|
+
}): Promise<RpcResponse<{
|
|
68
|
+
applied: boolean;
|
|
69
|
+
}>>;
|
|
70
|
+
/** Native zip picker for the plugin installer; `null` when cancelled. */
|
|
71
|
+
pickZip(payload: {
|
|
72
|
+
title?: string | undefined;
|
|
73
|
+
}): Promise<RpcResponse<string | null>>;
|
|
74
|
+
/** Native zip saver (generated-plugin export); `null` when cancelled. */
|
|
75
|
+
saveZip(payload: {
|
|
76
|
+
title?: string | undefined;
|
|
77
|
+
defaultFileName: string;
|
|
78
|
+
}): Promise<RpcResponse<string | null>>;
|
|
79
|
+
/** Relaunch the app (suite switches need a fresh boot to remount). */
|
|
80
|
+
relaunch(payload: Record<string, never>): Promise<RpcResponse<{
|
|
81
|
+
applied: true;
|
|
82
|
+
}>>;
|
|
83
|
+
}
|
|
84
|
+
/** Every unary method callable by a client, keyed by its wire method name.
|
|
85
|
+
* Open for declaration merging — see the module note for where rows live. */
|
|
86
|
+
export interface RpcMethodMap {
|
|
87
|
+
'host.describe': HostApi['describe'];
|
|
88
|
+
'rpc.describe': RpcIntrospectApi['describe'];
|
|
89
|
+
'window.control': WindowApi['control'];
|
|
90
|
+
'window.pick-zip': WindowApi['pickZip'];
|
|
91
|
+
'window.save-zip': WindowApi['saveZip'];
|
|
92
|
+
'window.relaunch': WindowApi['relaunch'];
|
|
93
|
+
}
|
|
94
|
+
/** Method names on the wire. */
|
|
95
|
+
export type MethodName = keyof RpcMethodMap & string;
|
|
96
|
+
/** The request payload type of a method, derived from its signature. */
|
|
97
|
+
export type RequestPayload<K extends MethodName> = Parameters<RpcMethodMap[K]>[0];
|
|
98
|
+
/** The success value type of a method, derived from its signature. */
|
|
99
|
+
export type ResponseValue<K extends MethodName> = Awaited<ReturnType<RpcMethodMap[K]>> extends RpcResponse<infer T> ? T : never;
|
|
100
|
+
//# sourceMappingURL=methods.d.ts.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The platform domain's method rows plus the open bases themselves: the
|
|
3
|
+
* wire's platform surface is `host.*` and `window.*` (transport-adjacent,
|
|
4
|
+
* owned by the gateway and the shell). Every other domain's rows live with
|
|
5
|
+
* their owners and merge in from their contract modules:
|
|
6
|
+
* field → `@snap-rail/equipment` (`./wire`), ModbusTCP →
|
|
7
|
+
* `@snap-rail/driver-modbus/contract`, station → `@snap-rail/station-rpc/contract`,
|
|
8
|
+
* plugin admin → `@snap-rail/app-boot/contract`. A program sees the rows of
|
|
9
|
+
* every contract it imports — the open-world rule.
|
|
10
|
+
*
|
|
11
|
+
* Reserved-method discipline: the map holds only implemented methods; an
|
|
12
|
+
* unknown method fails loud at dispatch (`bad-request`) — no
|
|
13
|
+
* not-implemented fallback code.
|
|
14
|
+
*
|
|
15
|
+
* @module @snap-rail/protocol/methods
|
|
16
|
+
*/
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=methods.js.map
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The four-quadrant message model: every logical message is classified by
|
|
3
|
+
* "who initiates × request/response", decoupled from the physical channel.
|
|
4
|
+
* Swapping the carrier (in-process, Electron IPC, a future WebSocket) leaves
|
|
5
|
+
* the quadrants unchanged; direction is a property of the message, never
|
|
6
|
+
* inferred from the channel.
|
|
7
|
+
*
|
|
8
|
+
* rpcId discipline: whoever initiates mints; a response always echoes the
|
|
9
|
+
* corresponding request's rpcId and never mints a new id. Pure-push frames
|
|
10
|
+
* mint a fresh id per push; answerable frames (a future approval/question
|
|
11
|
+
* family) reuse one stable id until answered.
|
|
12
|
+
*
|
|
13
|
+
* @module @snap-rail/protocol/rpc
|
|
14
|
+
*/
|
|
15
|
+
import type { Branded } from '@snap-rail/util';
|
|
16
|
+
/** Identifier correlating a request with its response. */
|
|
17
|
+
export type RpcId = Branded<string, 'RpcId'>;
|
|
18
|
+
/** Mint an rpc identifier. Only carriers and frame pumps mint; business code never does. */
|
|
19
|
+
export declare function RpcId(value: string): RpcId;
|
|
20
|
+
/** ① A client-initiated request: call a host method.
|
|
21
|
+
* @param rpcId - minted by the client.
|
|
22
|
+
* @param method - the method key; also the discriminator handlers dispatch on.
|
|
23
|
+
* @param payload - the business payload, validated per method at the host.
|
|
24
|
+
*/
|
|
25
|
+
export interface ClientRequest {
|
|
26
|
+
type: 'client-request';
|
|
27
|
+
rpcId: RpcId;
|
|
28
|
+
method: string;
|
|
29
|
+
payload: unknown;
|
|
30
|
+
}
|
|
31
|
+
/** ② The response to a {@link ClientRequest}. Always echoes its rpcId.
|
|
32
|
+
* @param rpcId - echoed from the request; never minted here.
|
|
33
|
+
* @param result - business outcome; methods do not throw business errors.
|
|
34
|
+
*/
|
|
35
|
+
export interface ServerResponse {
|
|
36
|
+
type: 'server-response';
|
|
37
|
+
rpcId: RpcId;
|
|
38
|
+
result: RpcResult<unknown>;
|
|
39
|
+
}
|
|
40
|
+
/** ③ A host-initiated request: a push frame (or a future answerable request).
|
|
41
|
+
* @param rpcId - minted by the host frame pump; fresh per push frame.
|
|
42
|
+
* @param method - the frame type.
|
|
43
|
+
* @param payload - the frame payload.
|
|
44
|
+
*/
|
|
45
|
+
export interface ServerRequest {
|
|
46
|
+
type: 'server-request';
|
|
47
|
+
rpcId: RpcId;
|
|
48
|
+
method: string;
|
|
49
|
+
payload: unknown;
|
|
50
|
+
}
|
|
51
|
+
/** ④ The response to a {@link ServerRequest}. Always echoes its rpcId. */
|
|
52
|
+
export interface ClientResponse {
|
|
53
|
+
type: 'client-response';
|
|
54
|
+
rpcId: RpcId;
|
|
55
|
+
result: RpcResult<unknown>;
|
|
56
|
+
}
|
|
57
|
+
/** Every wire message; narrowed via `switch (message.type)`. */
|
|
58
|
+
export type RpcMessage = ClientRequest | ServerResponse | ServerRequest | ClientResponse;
|
|
59
|
+
/** Business outcome of a method. Transport failures are carrier exceptions; the two layers never mix. */
|
|
60
|
+
export type RpcResult<T> = {
|
|
61
|
+
ok: true;
|
|
62
|
+
value: T;
|
|
63
|
+
} | {
|
|
64
|
+
ok: false;
|
|
65
|
+
error: RpcError;
|
|
66
|
+
};
|
|
67
|
+
/** Details required per error code; adding a code = one map row (compile-checked). */
|
|
68
|
+
export interface RpcErrorDetailsMap {
|
|
69
|
+
'bad-request': {
|
|
70
|
+
issues: readonly string[];
|
|
71
|
+
};
|
|
72
|
+
'not-found': {
|
|
73
|
+
what: string;
|
|
74
|
+
};
|
|
75
|
+
'conflict': {
|
|
76
|
+
what: string;
|
|
77
|
+
};
|
|
78
|
+
'unavailable': {
|
|
79
|
+
what: string;
|
|
80
|
+
};
|
|
81
|
+
'internal': {
|
|
82
|
+
hint?: string;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/** Discriminated error union; `code` discriminates, `details` narrows. */
|
|
86
|
+
export type RpcError = {
|
|
87
|
+
[K in keyof RpcErrorDetailsMap]: {
|
|
88
|
+
code: K;
|
|
89
|
+
details: RpcErrorDetailsMap[K];
|
|
90
|
+
};
|
|
91
|
+
}[keyof RpcErrorDetailsMap];
|
|
92
|
+
/** Narrow request form seen by typed signatures; carriers complete it into a full form. */
|
|
93
|
+
export type RpcRequest<P> = {
|
|
94
|
+
rpcId: RpcId;
|
|
95
|
+
payload: P;
|
|
96
|
+
};
|
|
97
|
+
/** Narrow response form seen by typed signatures. */
|
|
98
|
+
export type RpcResponse<T> = {
|
|
99
|
+
rpcId: RpcId;
|
|
100
|
+
result: RpcResult<T>;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Construct a success result.
|
|
104
|
+
* @param value - the method's return value.
|
|
105
|
+
*/
|
|
106
|
+
export declare function ok<T>(value: T): RpcResult<T>;
|
|
107
|
+
/**
|
|
108
|
+
* Construct a business-error result.
|
|
109
|
+
* @param code - the error code.
|
|
110
|
+
* @param details - code-specific details (required).
|
|
111
|
+
*/
|
|
112
|
+
export declare function err<K extends keyof RpcErrorDetailsMap>(code: K, details: RpcErrorDetailsMap[K]): {
|
|
113
|
+
ok: false;
|
|
114
|
+
error: Extract<RpcError, {
|
|
115
|
+
code: K;
|
|
116
|
+
}>;
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Thrown by method handlers to fail with a business error; the gateway
|
|
120
|
+
* converts it into `{ ok: false, error }` on the wire. Carrying it as a
|
|
121
|
+
* throw keeps handler happy paths free of result wrapping.
|
|
122
|
+
*/
|
|
123
|
+
export declare class RpcBusinessError extends Error {
|
|
124
|
+
readonly error: RpcError;
|
|
125
|
+
constructor(error: RpcError);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Type guard narrowing an unknown value to {@link RpcError}.
|
|
129
|
+
* @param value - candidate error.
|
|
130
|
+
*/
|
|
131
|
+
export declare function isRpcError(value: unknown): value is RpcError;
|
|
132
|
+
//# sourceMappingURL=rpc.d.ts.map
|
package/lib/types/rpc.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The four-quadrant message model: every logical message is classified by
|
|
3
|
+
* "who initiates × request/response", decoupled from the physical channel.
|
|
4
|
+
* Swapping the carrier (in-process, Electron IPC, a future WebSocket) leaves
|
|
5
|
+
* the quadrants unchanged; direction is a property of the message, never
|
|
6
|
+
* inferred from the channel.
|
|
7
|
+
*
|
|
8
|
+
* rpcId discipline: whoever initiates mints; a response always echoes the
|
|
9
|
+
* corresponding request's rpcId and never mints a new id. Pure-push frames
|
|
10
|
+
* mint a fresh id per push; answerable frames (a future approval/question
|
|
11
|
+
* family) reuse one stable id until answered.
|
|
12
|
+
*
|
|
13
|
+
* @module @snap-rail/protocol/rpc
|
|
14
|
+
*/
|
|
15
|
+
/** Mint an rpc identifier. Only carriers and frame pumps mint; business code never does. */
|
|
16
|
+
export function RpcId(value) {
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Construct a success result.
|
|
21
|
+
* @param value - the method's return value.
|
|
22
|
+
*/
|
|
23
|
+
export function ok(value) {
|
|
24
|
+
return { ok: true, value };
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Construct a business-error result.
|
|
28
|
+
* @param code - the error code.
|
|
29
|
+
* @param details - code-specific details (required).
|
|
30
|
+
*/
|
|
31
|
+
export function err(code, details) {
|
|
32
|
+
return { ok: false, error: { code, details } };
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Thrown by method handlers to fail with a business error; the gateway
|
|
36
|
+
* converts it into `{ ok: false, error }` on the wire. Carrying it as a
|
|
37
|
+
* throw keeps handler happy paths free of result wrapping.
|
|
38
|
+
*/
|
|
39
|
+
export class RpcBusinessError extends Error {
|
|
40
|
+
error;
|
|
41
|
+
constructor(error) {
|
|
42
|
+
super(`rpc business error: ${error.code}`);
|
|
43
|
+
this.error = error;
|
|
44
|
+
this.name = 'RpcBusinessError';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Type guard narrowing an unknown value to {@link RpcError}.
|
|
49
|
+
* @param value - candidate error.
|
|
50
|
+
*/
|
|
51
|
+
export function isRpcError(value) {
|
|
52
|
+
if (value === null || typeof value !== 'object')
|
|
53
|
+
return false;
|
|
54
|
+
const record = value;
|
|
55
|
+
return typeof record.code === 'string' && 'details' in record;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=rpc.js.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Envelope validation at the trust boundary: the type/rpcId/method
|
|
3
|
+
* structure of every inbound message. Per-method payload schemas travel
|
|
4
|
+
* with each registration (`ctx.rpc.method`) — there is no central method
|
|
5
|
+
* table. Host responses are produced by trusted code and are not
|
|
6
|
+
* re-validated in phase 1.
|
|
7
|
+
*
|
|
8
|
+
* @module @snap-rail/protocol/schemas
|
|
9
|
+
*/
|
|
10
|
+
import { type ClientRequest, type ServerRequest } from './rpc.ts';
|
|
11
|
+
/**
|
|
12
|
+
* Parse an inbound client request envelope.
|
|
13
|
+
* @param raw - the untrusted wire value.
|
|
14
|
+
* @returns the parsed envelope with a branded rpcId.
|
|
15
|
+
* @throws a zod error naming every envelope violation.
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseClientRequest(raw: unknown): ClientRequest;
|
|
18
|
+
/**
|
|
19
|
+
* Parse an inbound host frame on a client.
|
|
20
|
+
* @param raw - the untrusted wire value.
|
|
21
|
+
* @returns the parsed frame.
|
|
22
|
+
* @throws a zod error naming every envelope violation.
|
|
23
|
+
*/
|
|
24
|
+
export declare function parseServerRequest(raw: unknown): ServerRequest;
|
|
25
|
+
//# sourceMappingURL=schemas.d.ts.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Envelope validation at the trust boundary: the type/rpcId/method
|
|
3
|
+
* structure of every inbound message. Per-method payload schemas travel
|
|
4
|
+
* with each registration (`ctx.rpc.method`) — there is no central method
|
|
5
|
+
* table. Host responses are produced by trusted code and are not
|
|
6
|
+
* re-validated in phase 1.
|
|
7
|
+
*
|
|
8
|
+
* @module @snap-rail/protocol/schemas
|
|
9
|
+
*/
|
|
10
|
+
import { z } from 'zod';
|
|
11
|
+
import { RpcId } from "./rpc.js";
|
|
12
|
+
const rpcIdSchema = z.string().min(1).transform(RpcId);
|
|
13
|
+
const clientRequestShape = z.object({
|
|
14
|
+
type: z.literal('client-request'),
|
|
15
|
+
rpcId: rpcIdSchema,
|
|
16
|
+
method: z.string().min(1),
|
|
17
|
+
payload: z.unknown(),
|
|
18
|
+
});
|
|
19
|
+
/**
|
|
20
|
+
* Parse an inbound client request envelope.
|
|
21
|
+
* @param raw - the untrusted wire value.
|
|
22
|
+
* @returns the parsed envelope with a branded rpcId.
|
|
23
|
+
* @throws a zod error naming every envelope violation.
|
|
24
|
+
*/
|
|
25
|
+
export function parseClientRequest(raw) {
|
|
26
|
+
return clientRequestShape.parse(raw);
|
|
27
|
+
}
|
|
28
|
+
const serverRequestShape = z.object({
|
|
29
|
+
type: z.literal('server-request'),
|
|
30
|
+
rpcId: rpcIdSchema,
|
|
31
|
+
method: z.string().min(1),
|
|
32
|
+
payload: z.unknown(),
|
|
33
|
+
});
|
|
34
|
+
/**
|
|
35
|
+
* Parse an inbound host frame on a client.
|
|
36
|
+
* @param raw - the untrusted wire value.
|
|
37
|
+
* @returns the parsed frame.
|
|
38
|
+
* @throws a zod error naming every envelope violation.
|
|
39
|
+
*/
|
|
40
|
+
export function parseServerRequest(raw) {
|
|
41
|
+
return serverRequestShape.parse(raw);
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=schemas.js.map
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The surface vocabulary: one renderer window role. `main` is the operator
|
|
3
|
+
* window (the app's only always-present surface); `ball` is the auxiliary
|
|
4
|
+
* always-on-top surface a plugin drives through the Shell seam — the floating
|
|
5
|
+
* ball.
|
|
6
|
+
*
|
|
7
|
+
* A surface is an identity, not a layout. Every surface boots the same
|
|
8
|
+
* renderer with its own client entry (`snapRail.client.surfaces`) and its own
|
|
9
|
+
* seed set; a plugin face binds to exactly one surface and reads
|
|
10
|
+
* `ctx.client.surface` to know which. Auxiliary surfaces never subscribe to
|
|
11
|
+
* equipment points directly — they consume host-aggregated frames, because a
|
|
12
|
+
* hidden surface must not keep a data path alive and a shown one must not
|
|
13
|
+
* need a second subscription graph.
|
|
14
|
+
*
|
|
15
|
+
* The whitelist is closed on purpose: a plugin may not name an arbitrary URL
|
|
16
|
+
* or spawn an arbitrary window — the Shell owns every window and only these
|
|
17
|
+
* two roles exist.
|
|
18
|
+
*
|
|
19
|
+
* @module @snap-rail/protocol/surfaces
|
|
20
|
+
*/
|
|
21
|
+
/** Every surface id, in boot order (`main` first). */
|
|
22
|
+
export declare const SURFACE_IDS: readonly ["main", "ball"];
|
|
23
|
+
/** The always-on-top auxiliary surfaces: windows the Shell creates lazily. */
|
|
24
|
+
export declare const AUX_SURFACE_IDS: readonly ["ball"];
|
|
25
|
+
/** One renderer window role. */
|
|
26
|
+
export type SurfaceId = (typeof SURFACE_IDS)[number];
|
|
27
|
+
/** One auxiliary surface role. */
|
|
28
|
+
export type AuxSurfaceId = (typeof AUX_SURFACE_IDS)[number];
|
|
29
|
+
/**
|
|
30
|
+
* Whether a wire value names a surface.
|
|
31
|
+
*
|
|
32
|
+
* @param value - the candidate (any wire value).
|
|
33
|
+
* @returns true when the value is a {@link SurfaceId}.
|
|
34
|
+
*/
|
|
35
|
+
export declare function isSurfaceId(value: unknown): value is SurfaceId;
|
|
36
|
+
/**
|
|
37
|
+
* Whether a wire value names an auxiliary surface.
|
|
38
|
+
*
|
|
39
|
+
* @param value - the candidate (any wire value).
|
|
40
|
+
* @returns true when the value is an {@link AuxSurfaceId}.
|
|
41
|
+
*/
|
|
42
|
+
export declare function isAuxSurfaceId(value: unknown): value is AuxSurfaceId;
|
|
43
|
+
//# sourceMappingURL=surfaces.d.ts.map
|