@powerhousedao/reactor 6.2.3-dev.44 → 6.2.3-dev.45

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/dist/rpc.d.ts ADDED
@@ -0,0 +1,318 @@
1
+ import { Sr as IDocumentModelRegistry, pt as IReactorClient } from "./index-81SYQKda.js";
2
+ //#region src/rpc/protocol.d.ts
3
+ type CorrelationId = string;
4
+ type ErrorInfo = {
5
+ name: string;
6
+ message: string;
7
+ stack?: string;
8
+ cause?: ErrorInfo;
9
+ };
10
+ type RpcRequest = {
11
+ k: "req";
12
+ id: CorrelationId;
13
+ method: string;
14
+ args: unknown[];
15
+ abortAt?: number;
16
+ };
17
+ type RpcResponse = {
18
+ k: "res";
19
+ id: CorrelationId;
20
+ value: unknown;
21
+ };
22
+ type RpcError = {
23
+ k: "err";
24
+ id: CorrelationId;
25
+ error: ErrorInfo;
26
+ };
27
+ /** Failed document subscribe; routed to the change-subscription registry. */
28
+ type RpcSubError = {
29
+ k: "sub-err";
30
+ id: CorrelationId;
31
+ error: ErrorInfo;
32
+ };
33
+ /** Failed live query; routed to the live-query registry. */
34
+ type RpcLiveError = {
35
+ k: "live-err";
36
+ id: CorrelationId;
37
+ error: ErrorInfo;
38
+ };
39
+ type RpcAbort = {
40
+ k: "abort";
41
+ targetId: CorrelationId;
42
+ };
43
+ type RpcSubscribe = {
44
+ k: "sub";
45
+ id: CorrelationId;
46
+ search: unknown;
47
+ view?: unknown;
48
+ };
49
+ type RpcEvent = {
50
+ k: "event";
51
+ id: CorrelationId;
52
+ change: unknown;
53
+ };
54
+ type RpcUnsub = {
55
+ k: "unsub";
56
+ id: CorrelationId;
57
+ };
58
+ type RpcNextPage = {
59
+ k: "page";
60
+ id: CorrelationId;
61
+ token: string;
62
+ };
63
+ export declare const RPC_PROTOCOL_VERSION = 2;
64
+ type VersionFingerprint = {
65
+ appBuildId: string;
66
+ rpcProtocolVersion: number;
67
+ models: {
68
+ id: string;
69
+ version: number;
70
+ }[];
71
+ featureFlags?: string;
72
+ };
73
+ type RpcHello = {
74
+ k: "hello";
75
+ id: CorrelationId;
76
+ version: VersionFingerprint;
77
+ construct?: unknown;
78
+ packages?: string[];
79
+ };
80
+ type RpcRegisterPackages = {
81
+ k: "register-packages";
82
+ id: CorrelationId;
83
+ specs: string[];
84
+ };
85
+ type RpcUnregisterPackages = {
86
+ k: "unregister-packages";
87
+ id: CorrelationId;
88
+ names: string[];
89
+ };
90
+ type RpcReload = {
91
+ k: "reload";
92
+ reason: string;
93
+ workerGen?: string;
94
+ };
95
+ type RpcAdmin = {
96
+ k: "admin";
97
+ id: CorrelationId;
98
+ method: "info" | "restart" | "clearStorage" | "migrate";
99
+ };
100
+ type WorkerMigrationState = {
101
+ status: "idle" | "needed" | "migrating" | "failed";
102
+ legacyMajor?: number;
103
+ phase?: "clone" | "dump" | "restore";
104
+ error?: string;
105
+ };
106
+ type RpcMigration = {
107
+ k: "migration";
108
+ state: WorkerMigrationState;
109
+ };
110
+ type WorkerInspectorInfo = {
111
+ namespace: string;
112
+ ownerId: string;
113
+ bootedAtMs: number;
114
+ connectedClients: number;
115
+ appBuildId: string;
116
+ rpcProtocolVersion: number;
117
+ /** Flags this worker is enforcing, from the fingerprint that built it. */
118
+ featureFlags?: string;
119
+ };
120
+ type ReactorIdentity = {
121
+ address: string;
122
+ chainId: number;
123
+ networkId: string;
124
+ };
125
+ type RpcIdentity = {
126
+ k: "identity";
127
+ user: ReactorIdentity | null;
128
+ };
129
+ type RpcBusEvent = {
130
+ k: "bus-event";
131
+ eventType: number;
132
+ event: unknown;
133
+ };
134
+ type OpKind = "sync-op" | "db-op" | "inspector-op";
135
+ type MethodCallMessage<K extends OpKind> = {
136
+ k: K;
137
+ id: CorrelationId;
138
+ method: string;
139
+ args: unknown[];
140
+ };
141
+ type RpcSyncOp = MethodCallMessage<"sync-op">;
142
+ type RpcDbOp = MethodCallMessage<"db-op">;
143
+ type RpcInspectorOp = MethodCallMessage<"inspector-op">;
144
+ type RpcLiveSubscribe = {
145
+ k: "sub-live";
146
+ id: CorrelationId;
147
+ sql: string;
148
+ params: unknown[];
149
+ };
150
+ type RpcLiveEvent = {
151
+ k: "event-live";
152
+ id: CorrelationId;
153
+ results: unknown;
154
+ };
155
+ type RpcLiveUnsub = {
156
+ k: "unsub-live";
157
+ id: CorrelationId;
158
+ };
159
+ type RpcPing = {
160
+ k: "ping";
161
+ id: CorrelationId;
162
+ };
163
+ type RpcPong = {
164
+ k: "pong";
165
+ id: CorrelationId;
166
+ ownerId: string;
167
+ bootedAtMs: number;
168
+ };
169
+ type ClientMessage = RpcRequest | RpcAbort | RpcSubscribe | RpcUnsub | RpcNextPage | RpcHello | RpcRegisterPackages | RpcUnregisterPackages | RpcIdentity | RpcAdmin | RpcSyncOp | RpcDbOp | RpcInspectorOp | RpcLiveSubscribe | RpcLiveUnsub | RpcPing;
170
+ type OwnerMessage = RpcResponse | RpcError | RpcSubError | RpcLiveError | RpcEvent | RpcReload | RpcBusEvent | RpcLiveEvent | RpcMigration | RpcPong;
171
+ type RpcMessage = ClientMessage | OwnerMessage;
172
+ //#endregion
173
+ //#region src/rpc/rpc-correlator.d.ts
174
+ type RpcRequestOptions = {
175
+ /** Reject if no response arrives within this many ms; omit for no timeout. */
176
+ timeoutMs?: number;
177
+ /** Maps a successful response value (e.g. paged-result rehydration). */
178
+ transform?: (value: unknown) => unknown;
179
+ /** Runs after the post; may return a cleanup invoked when the request settles. */
180
+ setup?: (id: CorrelationId) => (() => void) | void;
181
+ };
182
+ /** Anything that can send a client message (the router). */
183
+ type RpcPoster = {
184
+ post(message: ClientMessage): void;
185
+ };
186
+ /**
187
+ * Request/response correlation shared by every RPC proxy: one pending map,
188
+ * res/err settlement, and per-request timeout/transform/cleanup.
189
+ */
190
+ export declare class RpcCorrelator {
191
+ private readonly poster;
192
+ private readonly prefix;
193
+ private counter;
194
+ private readonly pending;
195
+ constructor(poster: RpcPoster, prefix?: string);
196
+ nextId(): CorrelationId;
197
+ /** Settle the pending request a res/err refers to; a missing id is a no-op. */
198
+ handleMessage(message: OwnerMessage): void;
199
+ /** Send a request, resolving on its res/err. `build` stamps the generated id. */
200
+ request(build: (id: CorrelationId) => ClientMessage, options?: RpcRequestOptions): Promise<unknown>;
201
+ }
202
+ //#endregion
203
+ //#region src/rpc/transport.d.ts
204
+ interface IRpcTransport {
205
+ post(message: RpcMessage): void;
206
+ onMessage(listener: (message: RpcMessage) => void): () => void;
207
+ close(): void;
208
+ }
209
+ export declare function createPortTransport(port: MessagePort): IRpcTransport;
210
+ //#endregion
211
+ //#region src/rpc/message-router.d.ts
212
+ type OwnerKind = OwnerMessage["k"];
213
+ /**
214
+ * Owns the single transport.onMessage and dispatches each message to the one
215
+ * consumer that owns its kind. Composes the shared RpcCorrelator (res/err) and
216
+ * exposes request/nextId; attach/detach swap the transport for reconnect.
217
+ */
218
+ export declare class MessageRouter {
219
+ private readonly handlers;
220
+ private readonly correlator;
221
+ private transport;
222
+ private detachTransport;
223
+ constructor(prefix?: string);
224
+ attach(transport: IRpcTransport): void;
225
+ detach(): void;
226
+ post(message: ClientMessage): void;
227
+ /** Register the sole owner of a message kind; throws on a duplicate. */
228
+ on<K extends OwnerKind>(kind: K, handler: (message: Extract<OwnerMessage, {
229
+ k: K;
230
+ }>) => void): () => void;
231
+ request(build: (id: CorrelationId) => ClientMessage, options?: RpcRequestOptions): Promise<unknown>;
232
+ nextId(): CorrelationId;
233
+ }
234
+ //#endregion
235
+ //#region src/rpc/client-proxy.d.ts
236
+ type ReactorClientProxyOptions = {
237
+ onReload?: (reason: string, workerGen?: string) => void;
238
+ /** Tab-local registry for module lookups (reducers/editors can't cross the worker boundary). */
239
+ registry?: IDocumentModelRegistry;
240
+ };
241
+ export declare function createReactorClientProxy(router: MessageRouter, options?: ReactorClientProxyOptions): IReactorClient;
242
+ //#endregion
243
+ //#region src/rpc/error-info.d.ts
244
+ export declare function toErrorInfo(err: unknown, depth?: number): ErrorInfo;
245
+ export declare function fromErrorInfo(info: ErrorInfo): Error;
246
+ //#endregion
247
+ //#region src/rpc/host-reply.d.ts
248
+ interface IHostResponder {
249
+ ok(id: CorrelationId, value?: unknown): void;
250
+ err(id: CorrelationId, error: unknown): void;
251
+ errForKind(message: ClientMessage, error: unknown): void;
252
+ run(id: CorrelationId, body: () => Promise<unknown>, mapOk?: (value: unknown) => unknown): Promise<void>;
253
+ }
254
+ export declare function hostResponder(transport: IRpcTransport): IHostResponder;
255
+ //#endregion
256
+ //#region src/rpc/host-server.d.ts
257
+ export declare class ReactorHostServer {
258
+ private readonly client;
259
+ private readonly transport;
260
+ private readonly reply;
261
+ private readonly aborters;
262
+ private readonly subscriptions;
263
+ private readonly pages;
264
+ private pageCounter;
265
+ private detach;
266
+ constructor(client: IReactorClient, transport: IRpcTransport);
267
+ start(): void;
268
+ stop(): void;
269
+ handleMessage(message: ClientMessage): Promise<void>;
270
+ private handleRequest;
271
+ private handlePage;
272
+ private handleSubscribe;
273
+ private prepareResult;
274
+ private resolveMethod;
275
+ }
276
+ //#endregion
277
+ //#region src/rpc/listeners.d.ts
278
+ export declare class KeyedListeners<K, A extends unknown[] = []> {
279
+ private readonly map;
280
+ add(key: K, fn: (...a: A) => void): () => void;
281
+ emit(key: K, ...a: A): void;
282
+ emitAll(...a: A): void;
283
+ }
284
+ export declare class Listeners<A extends unknown[] = []> {
285
+ private readonly keyed;
286
+ add(fn: (...a: A) => void): () => void;
287
+ emit(...a: A): void;
288
+ }
289
+ //#endregion
290
+ //#region src/rpc/subscription.d.ts
291
+ export declare class SubscriptionStore {
292
+ private readonly map;
293
+ set(id: CorrelationId, unsubscribe: () => void): void;
294
+ get(id: CorrelationId): (() => void) | undefined;
295
+ delete(id: CorrelationId): void;
296
+ end(id: CorrelationId): void;
297
+ drain(): void;
298
+ }
299
+ interface ICorrelatedSubscriptions<Sub> {
300
+ subscribe(sub: Sub, open: (id: CorrelationId) => ClientMessage): () => void;
301
+ }
302
+ interface CorrelatedConfig<Sub, EK extends OwnerMessage["k"], ErrK extends OwnerMessage["k"]> {
303
+ idPrefix: string;
304
+ eventKind: EK;
305
+ errKind: ErrK;
306
+ onEvent: (sub: Sub, message: Extract<OwnerMessage, {
307
+ k: EK;
308
+ }>) => void;
309
+ onError: (sub: Sub, message: Extract<OwnerMessage, {
310
+ k: ErrK;
311
+ }>) => void;
312
+ close: (id: CorrelationId) => ClientMessage;
313
+ }
314
+ export declare function createCorrelatedSubscriptions<Sub, EK extends OwnerMessage["k"], ErrK extends OwnerMessage["k"]>(router: MessageRouter, config: CorrelatedConfig<Sub, EK, ErrK>): ICorrelatedSubscriptions<Sub>;
315
+ //#endregion
316
+ export type { ClientMessage, CorrelationId, ErrorInfo, ICorrelatedSubscriptions, IHostResponder, IRpcTransport, MethodCallMessage, OpKind, OwnerMessage, ReactorIdentity, RpcAbort, RpcAdmin, RpcBusEvent, RpcDbOp, RpcError, RpcEvent, RpcHello, RpcIdentity, RpcInspectorOp, RpcLiveError, RpcLiveEvent, RpcLiveSubscribe, RpcLiveUnsub, RpcMessage, RpcMigration, RpcNextPage, RpcPing, RpcPong, RpcPoster, RpcRegisterPackages, RpcReload, RpcRequest, RpcRequestOptions, RpcResponse, RpcSubError, RpcSubscribe, RpcSyncOp, RpcUnregisterPackages, RpcUnsub, VersionFingerprint, WorkerInspectorInfo, WorkerMigrationState };
317
+ export {};
318
+ //# sourceMappingURL=rpc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rpc.d.ts","names":[],"sources":["../src/rpc/protocol.ts","../src/rpc/rpc-correlator.ts","../src/rpc/transport.ts","../src/rpc/message-router.ts","../src/rpc/client-proxy.ts","../src/rpc/error-info.ts","../src/rpc/host-reply.ts","../src/rpc/host-server.ts","../src/rpc/listeners.ts","../src/rpc/subscription.ts"],"mappings":";;KAAY;KAEA;EACV;EACA;EACA;EACA,QAAQ;;KAGE;EACV;EACA,IAAI;EACJ;EACA;EACA;;KAGU;EAAgB;EAAU,IAAI;EAAe;;KAE7C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