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

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,332 @@
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 = 3;
64
+ /**
65
+ * A package the worker loads by URL rather than by registry spec: local
66
+ * project packages, whose models the registry does not serve. The URL must be
67
+ * a worker-importable models entry - the dev server's transformed
68
+ * `document-models/index.js` in dev, a prebuilt bundle under
69
+ * `__reactor_worker__/packages/` in production.
70
+ */
71
+ type WorkerPackageSource = {
72
+ name: string;
73
+ version?: string;
74
+ url: string;
75
+ };
76
+ type VersionFingerprint = {
77
+ appBuildId: string;
78
+ rpcProtocolVersion: number;
79
+ models: {
80
+ id: string;
81
+ version: number;
82
+ }[];
83
+ featureFlags?: string;
84
+ };
85
+ type RpcHello = {
86
+ k: "hello";
87
+ id: CorrelationId;
88
+ version: VersionFingerprint;
89
+ construct?: unknown;
90
+ packages?: string[];
91
+ };
92
+ type RpcRegisterPackages = {
93
+ k: "register-packages";
94
+ id: CorrelationId;
95
+ specs: string[];
96
+ /** URL-addressed packages; the worker REPLACES a source it already loaded. */
97
+ sources?: WorkerPackageSource[];
98
+ };
99
+ type RpcUnregisterPackages = {
100
+ k: "unregister-packages";
101
+ id: CorrelationId;
102
+ names: string[];
103
+ };
104
+ type RpcReload = {
105
+ k: "reload";
106
+ reason: string;
107
+ workerGen?: string;
108
+ };
109
+ type RpcAdmin = {
110
+ k: "admin";
111
+ id: CorrelationId;
112
+ method: "info" | "restart" | "clearStorage" | "migrate";
113
+ };
114
+ type WorkerMigrationState = {
115
+ status: "idle" | "needed" | "migrating" | "failed";
116
+ legacyMajor?: number;
117
+ phase?: "clone" | "dump" | "restore";
118
+ error?: string;
119
+ };
120
+ type RpcMigration = {
121
+ k: "migration";
122
+ state: WorkerMigrationState;
123
+ };
124
+ type WorkerInspectorInfo = {
125
+ namespace: string;
126
+ ownerId: string;
127
+ bootedAtMs: number;
128
+ connectedClients: number;
129
+ appBuildId: string;
130
+ rpcProtocolVersion: number;
131
+ /** Flags this worker is enforcing, from the fingerprint that built it. */
132
+ featureFlags?: string;
133
+ };
134
+ type ReactorIdentity = {
135
+ address: string;
136
+ chainId: number;
137
+ networkId: string;
138
+ };
139
+ type RpcIdentity = {
140
+ k: "identity";
141
+ user: ReactorIdentity | null;
142
+ };
143
+ type RpcBusEvent = {
144
+ k: "bus-event";
145
+ eventType: number;
146
+ event: unknown;
147
+ };
148
+ type OpKind = "sync-op" | "db-op" | "inspector-op";
149
+ type MethodCallMessage<K extends OpKind> = {
150
+ k: K;
151
+ id: CorrelationId;
152
+ method: string;
153
+ args: unknown[];
154
+ };
155
+ type RpcSyncOp = MethodCallMessage<"sync-op">;
156
+ type RpcDbOp = MethodCallMessage<"db-op">;
157
+ type RpcInspectorOp = MethodCallMessage<"inspector-op">;
158
+ type RpcLiveSubscribe = {
159
+ k: "sub-live";
160
+ id: CorrelationId;
161
+ sql: string;
162
+ params: unknown[];
163
+ };
164
+ type RpcLiveEvent = {
165
+ k: "event-live";
166
+ id: CorrelationId;
167
+ results: unknown;
168
+ };
169
+ type RpcLiveUnsub = {
170
+ k: "unsub-live";
171
+ id: CorrelationId;
172
+ };
173
+ type RpcPing = {
174
+ k: "ping";
175
+ id: CorrelationId;
176
+ };
177
+ type RpcPong = {
178
+ k: "pong";
179
+ id: CorrelationId;
180
+ ownerId: string;
181
+ bootedAtMs: number;
182
+ };
183
+ type ClientMessage = RpcRequest | RpcAbort | RpcSubscribe | RpcUnsub | RpcNextPage | RpcHello | RpcRegisterPackages | RpcUnregisterPackages | RpcIdentity | RpcAdmin | RpcSyncOp | RpcDbOp | RpcInspectorOp | RpcLiveSubscribe | RpcLiveUnsub | RpcPing;
184
+ type OwnerMessage = RpcResponse | RpcError | RpcSubError | RpcLiveError | RpcEvent | RpcReload | RpcBusEvent | RpcLiveEvent | RpcMigration | RpcPong;
185
+ type RpcMessage = ClientMessage | OwnerMessage;
186
+ //#endregion
187
+ //#region src/rpc/rpc-correlator.d.ts
188
+ type RpcRequestOptions = {
189
+ /** Reject if no response arrives within this many ms; omit for no timeout. */
190
+ timeoutMs?: number;
191
+ /** Maps a successful response value (e.g. paged-result rehydration). */
192
+ transform?: (value: unknown) => unknown;
193
+ /** Runs after the post; may return a cleanup invoked when the request settles. */
194
+ setup?: (id: CorrelationId) => (() => void) | void;
195
+ };
196
+ /** Anything that can send a client message (the router). */
197
+ type RpcPoster = {
198
+ post(message: ClientMessage): void;
199
+ };
200
+ /**
201
+ * Request/response correlation shared by every RPC proxy: one pending map,
202
+ * res/err settlement, and per-request timeout/transform/cleanup.
203
+ */
204
+ export declare class RpcCorrelator {
205
+ private readonly poster;
206
+ private readonly prefix;
207
+ private counter;
208
+ private readonly pending;
209
+ constructor(poster: RpcPoster, prefix?: string);
210
+ nextId(): CorrelationId;
211
+ /** Settle the pending request a res/err refers to; a missing id is a no-op. */
212
+ handleMessage(message: OwnerMessage): void;
213
+ /** Send a request, resolving on its res/err. `build` stamps the generated id. */
214
+ request(build: (id: CorrelationId) => ClientMessage, options?: RpcRequestOptions): Promise<unknown>;
215
+ }
216
+ //#endregion
217
+ //#region src/rpc/transport.d.ts
218
+ interface IRpcTransport {
219
+ post(message: RpcMessage): void;
220
+ onMessage(listener: (message: RpcMessage) => void): () => void;
221
+ close(): void;
222
+ }
223
+ export declare function createPortTransport(port: MessagePort): IRpcTransport;
224
+ //#endregion
225
+ //#region src/rpc/message-router.d.ts
226
+ type OwnerKind = OwnerMessage["k"];
227
+ /**
228
+ * Owns the single transport.onMessage and dispatches each message to the one
229
+ * consumer that owns its kind. Composes the shared RpcCorrelator (res/err) and
230
+ * exposes request/nextId; attach/detach swap the transport for reconnect.
231
+ */
232
+ export declare class MessageRouter {
233
+ private readonly handlers;
234
+ private readonly correlator;
235
+ private transport;
236
+ private detachTransport;
237
+ constructor(prefix?: string);
238
+ attach(transport: IRpcTransport): void;
239
+ detach(): void;
240
+ post(message: ClientMessage): void;
241
+ /** Register the sole owner of a message kind; throws on a duplicate. */
242
+ on<K extends OwnerKind>(kind: K, handler: (message: Extract<OwnerMessage, {
243
+ k: K;
244
+ }>) => void): () => void;
245
+ request(build: (id: CorrelationId) => ClientMessage, options?: RpcRequestOptions): Promise<unknown>;
246
+ nextId(): CorrelationId;
247
+ }
248
+ //#endregion
249
+ //#region src/rpc/client-proxy.d.ts
250
+ type ReactorClientProxyOptions = {
251
+ onReload?: (reason: string, workerGen?: string) => void;
252
+ /** Tab-local registry for module lookups (reducers/editors can't cross the worker boundary). */
253
+ registry?: IDocumentModelRegistry;
254
+ };
255
+ export declare function createReactorClientProxy(router: MessageRouter, options?: ReactorClientProxyOptions): IReactorClient;
256
+ //#endregion
257
+ //#region src/rpc/error-info.d.ts
258
+ export declare function toErrorInfo(err: unknown, depth?: number): ErrorInfo;
259
+ export declare function fromErrorInfo(info: ErrorInfo): Error;
260
+ //#endregion
261
+ //#region src/rpc/host-reply.d.ts
262
+ interface IHostResponder {
263
+ ok(id: CorrelationId, value?: unknown): void;
264
+ err(id: CorrelationId, error: unknown): void;
265
+ errForKind(message: ClientMessage, error: unknown): void;
266
+ run(id: CorrelationId, body: () => Promise<unknown>, mapOk?: (value: unknown) => unknown): Promise<void>;
267
+ }
268
+ export declare function hostResponder(transport: IRpcTransport): IHostResponder;
269
+ //#endregion
270
+ //#region src/rpc/host-server.d.ts
271
+ export declare class ReactorHostServer {
272
+ private readonly client;
273
+ private readonly transport;
274
+ private readonly reply;
275
+ private readonly aborters;
276
+ private readonly subscriptions;
277
+ private readonly pages;
278
+ private pageCounter;
279
+ private detach;
280
+ constructor(client: IReactorClient, transport: IRpcTransport);
281
+ start(): void;
282
+ stop(): void;
283
+ handleMessage(message: ClientMessage): Promise<void>;
284
+ private handleRequest;
285
+ private handlePage;
286
+ private handleSubscribe;
287
+ private prepareResult;
288
+ private resolveMethod;
289
+ }
290
+ //#endregion
291
+ //#region src/rpc/listeners.d.ts
292
+ export declare class KeyedListeners<K, A extends unknown[] = []> {
293
+ private readonly map;
294
+ add(key: K, fn: (...a: A) => void): () => void;
295
+ emit(key: K, ...a: A): void;
296
+ emitAll(...a: A): void;
297
+ }
298
+ export declare class Listeners<A extends unknown[] = []> {
299
+ private readonly keyed;
300
+ add(fn: (...a: A) => void): () => void;
301
+ emit(...a: A): void;
302
+ }
303
+ //#endregion
304
+ //#region src/rpc/subscription.d.ts
305
+ export declare class SubscriptionStore {
306
+ private readonly map;
307
+ set(id: CorrelationId, unsubscribe: () => void): void;
308
+ get(id: CorrelationId): (() => void) | undefined;
309
+ delete(id: CorrelationId): void;
310
+ end(id: CorrelationId): void;
311
+ drain(): void;
312
+ }
313
+ interface ICorrelatedSubscriptions<Sub> {
314
+ subscribe(sub: Sub, open: (id: CorrelationId) => ClientMessage): () => void;
315
+ }
316
+ interface CorrelatedConfig<Sub, EK extends OwnerMessage["k"], ErrK extends OwnerMessage["k"]> {
317
+ idPrefix: string;
318
+ eventKind: EK;
319
+ errKind: ErrK;
320
+ onEvent: (sub: Sub, message: Extract<OwnerMessage, {
321
+ k: EK;
322
+ }>) => void;
323
+ onError: (sub: Sub, message: Extract<OwnerMessage, {
324
+ k: ErrK;
325
+ }>) => void;
326
+ close: (id: CorrelationId) => ClientMessage;
327
+ }
328
+ export declare function createCorrelatedSubscriptions<Sub, EK extends OwnerMessage["k"], ErrK extends OwnerMessage["k"]>(router: MessageRouter, config: CorrelatedConfig<Sub, EK, ErrK>): ICorrelatedSubscriptions<Sub>;
329
+ //#endregion
330
+ 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, WorkerPackageSource };
331
+ export {};
332
+ //# 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;EAAa;EAAU,IAAI;EAAe,OAAO;;;KAGjD;EAAgB;EAAc,IAAI;EAAe,OAAO;;;KAGxD;EACV;EACA,IAAI;EACJ,OAAO;;KAGG;EAAa;EAAY,UAAU;;KAEnC;EACV;EACA,IAAI;EACJ;EACA;;KAGU;EAAa;EAAY,IAAI;EAAe;;KAE5C;EAAa;EAAY,IAAI;;KAE7B;EAAgB;EAAW,IAAI;EAAe;;qBAI7C;;;;;;;;KASD;EACV;EACA;EACA;;KAGU;EACV;EACA;EACA;IAAU;IAAY;;EAGtB;;KAGU;EACV;EACA,IAAI;EACJ,SAAS;EACT;EACA;;KAGU;EACV;EACA,IAAI;EACJ;;EAEA,UAAU;;KAGA;EACV;EACA,IAAI;EACJ;;KAKU;EAAc;EAAa;EAAgB;;KAG3C;EACV;EACA,IAAI;EACJ;;KAGU;EACV;EACA;EACA;EACA;;KAKU;EAAiB;EAAgB,OAAO;;KAExC;EACV;EACA;EACA;EACA;EACA;EACA;;EAEA;;KAIU;EACV;EACA;EACA;;KAGU;EAAgB;EAAe,MAAM;;KAIrC;EAAgB;EAAgB;EAAmB;;KAEnD;KAEA,kBAAkB,UAAU;EACtC,GAAG;EACH,IAAI;EACJ;EACA;;KAMU,YAAY;KAEZ,UAAU;KAEV,iBAAiB;KAEjB;EACV;EACA,IAAI;EACJ;EACA;;KAGU;EACV;EACA,IAAI;EACJ;;KAGU;EAAiB;EAAiB,IAAI;;KAKtC;EAAY;EAAW,IAAI;;KAE3B;EACV;EACA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