@powerhousedao/reactor-browser 6.2.3-dev.7 → 6.2.3-dev.70
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/{attachment-service-D3EhiAJ7.d.ts → attachment-service-CAf9N-15.d.ts} +365 -66
- package/dist/attachment-service-CAf9N-15.d.ts.map +1 -0
- package/dist/{client-DjK30EQ-.js → client-BFnMV-EO.js} +39 -22
- package/dist/client-BFnMV-EO.js.map +1 -0
- package/dist/{client-BIHN_UyR.d.ts → client-BnGewICj.d.ts} +3 -3
- package/dist/client-BnGewICj.d.ts.map +1 -0
- package/dist/{client-D66jVnot.js → client-CXDpILj6.js} +232 -32
- package/dist/client-CXDpILj6.js.map +1 -0
- package/dist/dispatch-Dd_WEz35.js +870 -0
- package/dist/dispatch-Dd_WEz35.js.map +1 -0
- package/dist/document-by-id-DqAyjr9P.js +16 -0
- package/dist/document-by-id-DqAyjr9P.js.map +1 -0
- package/dist/document-model-modules-DROQT_yu.js +431 -0
- package/dist/document-model-modules-DROQT_yu.js.map +1 -0
- package/dist/{document-operations-WeE6wIDt.js → document-operations-C6khz2If.js} +958 -204
- package/dist/document-operations-C6khz2If.js.map +1 -0
- package/dist/{global-OuINvEGT.d.ts → global-D-VZTZRz.d.ts} +113 -20
- package/dist/global-D-VZTZRz.d.ts.map +1 -0
- package/dist/{index-B33TKEqp.d.ts → index-XfcUiODQ.d.ts} +19 -98
- package/dist/index-XfcUiODQ.d.ts.map +1 -0
- package/dist/{index-BIVDXw3K.d.ts → index-rtDmzViP.d.ts} +3 -3
- package/dist/index-rtDmzViP.d.ts.map +1 -0
- package/dist/index.d.ts +693 -391
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +469 -72
- package/dist/index.js.map +1 -1
- package/dist/inspector-proxy-DODqs15F.d.ts +25 -0
- package/dist/inspector-proxy-DODqs15F.d.ts.map +1 -0
- package/dist/make-ph-event-functions-B5GU5xcR.js +180 -0
- package/dist/make-ph-event-functions-B5GU5xcR.js.map +1 -0
- package/dist/navigation-BqphkrOB.js +302 -0
- package/dist/navigation-BqphkrOB.js.map +1 -0
- package/dist/{pglite-CntadC_p-9NDG14dS.d.ts → pglite-CntadC_p-DTkdF8sr.d.ts} +11 -33
- package/dist/pglite-CntadC_p-DTkdF8sr.d.ts.map +1 -0
- package/dist/{relational-D1RB45sA.js → relational-DZuyKp2y.js} +66 -5
- package/dist/relational-DZuyKp2y.js.map +1 -0
- package/dist/{renown-rqK8sokh.js → renown-BtjgHBjH.js} +33 -28
- package/dist/{renown-rqK8sokh.js.map → renown-BtjgHBjH.js.map} +1 -1
- package/dist/{renown-BHxwP7xU.js → renown-BxkZYzbQ.js} +2 -2
- package/dist/{renown-BHxwP7xU.js.map → renown-BxkZYzbQ.js.map} +1 -1
- package/dist/src/ai/index.d.ts +51 -62
- package/dist/src/ai/index.d.ts.map +1 -1
- package/dist/src/ai/index.js +6 -9
- package/dist/src/ai/index.js.map +1 -1
- package/dist/src/analytics/index.d.ts +36 -44
- package/dist/src/analytics/index.d.ts.map +1 -1
- package/dist/src/analytics/index.js +4 -4
- package/dist/src/analytics/index.js.map +1 -1
- package/dist/src/document-model.d.ts +3 -4
- package/dist/src/document-model.d.ts.map +1 -1
- package/dist/src/document-model.js.map +1 -1
- package/dist/src/graphql/client.d.ts +4 -3
- package/dist/src/graphql/client.js +1 -1
- package/dist/src/graphql-client/entry.d.ts +5 -4
- package/dist/src/graphql-client/entry.js +4 -4
- package/dist/src/relational/index.d.ts +15 -9
- package/dist/src/relational/index.d.ts.map +1 -1
- package/dist/src/relational/index.js +1 -1
- package/dist/src/renown/index.d.ts +3 -2
- package/dist/src/renown/index.js +2 -2
- package/dist/src/rpc/index.d.ts +178 -138
- package/dist/src/rpc/index.d.ts.map +1 -1
- package/dist/src/rpc/index.js +420 -619
- package/dist/src/rpc/index.js.map +1 -1
- package/dist/{store-BJYcr_FL.js → store-DInXcjal.js} +1 -1
- package/dist/{store-BJYcr_FL.js.map → store-DInXcjal.js.map} +1 -1
- package/dist/{switchboard-CaXQ4-5Q.js → switchboard-cwUp3FxP.js} +98 -11
- package/dist/switchboard-cwUp3FxP.js.map +1 -0
- package/dist/{types-Dcv1ZLoW.d.ts → types-0Fh1tNjy.d.ts} +242 -17
- package/dist/types-0Fh1tNjy.d.ts.map +1 -0
- package/dist/{types-6vDzbRRS.d.ts → types-520nWrB3.d.ts} +2 -2
- package/dist/types-520nWrB3.d.ts.map +1 -0
- package/package.json +20 -21
- package/dist/attachment-service-D3EhiAJ7.d.ts.map +0 -1
- package/dist/client-BIHN_UyR.d.ts.map +0 -1
- package/dist/client-D66jVnot.js.map +0 -1
- package/dist/client-DjK30EQ-.js.map +0 -1
- package/dist/document-by-id-B4FIzG1w.js +0 -519
- package/dist/document-by-id-B4FIzG1w.js.map +0 -1
- package/dist/document-model-modules-bTP87nyr.js +0 -419
- package/dist/document-model-modules-bTP87nyr.js.map +0 -1
- package/dist/document-operations-WeE6wIDt.js.map +0 -1
- package/dist/global-OuINvEGT.d.ts.map +0 -1
- package/dist/index-B33TKEqp.d.ts.map +0 -1
- package/dist/index-BIVDXw3K.d.ts.map +0 -1
- package/dist/inspector-proxy-6h5VKAwt.d.ts +0 -257
- package/dist/inspector-proxy-6h5VKAwt.d.ts.map +0 -1
- package/dist/make-ph-event-functions-DBq3iWYn.js +0 -53
- package/dist/make-ph-event-functions-DBq3iWYn.js.map +0 -1
- package/dist/pglite-CntadC_p-9NDG14dS.d.ts.map +0 -1
- package/dist/relational-D1RB45sA.js.map +0 -1
- package/dist/switchboard-CaXQ4-5Q.js.map +0 -1
- package/dist/types-6vDzbRRS.d.ts.map +0 -1
- package/dist/types-Dcv1ZLoW.d.ts.map +0 -1
package/dist/src/rpc/index.js
CHANGED
|
@@ -1,42 +1,7 @@
|
|
|
1
|
-
import { t as RegistryClient } from "../../client-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
var KeyedListeners = class {
|
|
6
|
-
map = /* @__PURE__ */ new Map();
|
|
7
|
-
add(key, fn) {
|
|
8
|
-
let set = this.map.get(key);
|
|
9
|
-
if (!set) {
|
|
10
|
-
set = /* @__PURE__ */ new Set();
|
|
11
|
-
this.map.set(key, set);
|
|
12
|
-
}
|
|
13
|
-
set.add(fn);
|
|
14
|
-
return () => {
|
|
15
|
-
const current = this.map.get(key);
|
|
16
|
-
if (!current) return;
|
|
17
|
-
current.delete(fn);
|
|
18
|
-
if (current.size === 0) this.map.delete(key);
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
emit(key, ...a) {
|
|
22
|
-
const set = this.map.get(key);
|
|
23
|
-
if (!set) return;
|
|
24
|
-
for (const fn of [...set]) fn(...a);
|
|
25
|
-
}
|
|
26
|
-
emitAll(...a) {
|
|
27
|
-
for (const set of [...this.map.values()]) for (const fn of [...set]) fn(...a);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
var Listeners = class {
|
|
31
|
-
keyed = new KeyedListeners();
|
|
32
|
-
add(fn) {
|
|
33
|
-
return this.keyed.add(null, fn);
|
|
34
|
-
}
|
|
35
|
-
emit(...a) {
|
|
36
|
-
this.keyed.emit(null, ...a);
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
//#endregion
|
|
1
|
+
import { l as SYNC_REMOTES_CHANGED_EVENT, t as RegistryClient, u as SYNC_STATUS_CHANGED_EVENT } from "../../client-BFnMV-EO.js";
|
|
2
|
+
import { DriveCollectionId, INSPECTOR_OPS, ReactorEventTypes, SYNC_INSPECTION_OPS, SyncEventTypes, collectionIdFromKey, createPeerAgreement, dispatchTableOp, messagePortTransport, methodOf, opKeyOf, registerLocalPeer, removeLocalPeer } from "@powerhousedao/reactor";
|
|
3
|
+
import { KeyedListeners, KeyedListeners as KeyedListeners$1, Listeners, Listeners as Listeners$1, MessageRouter, RPC_PROTOCOL_VERSION, RPC_PROTOCOL_VERSION as RPC_PROTOCOL_VERSION$1, ReactorHostServer, ReactorHostServer as ReactorHostServer$1, RpcCorrelator, SubscriptionStore, SubscriptionStore as SubscriptionStore$1, createCorrelatedSubscriptions, createCorrelatedSubscriptions as createCorrelatedSubscriptions$1, createPortTransport, createPortTransport as createPortTransport$1, createReactorClientProxy, createReactorClientProxy as createReactorClientProxy$1, fromErrorInfo, fromErrorInfo as fromErrorInfo$1, hostResponder, hostResponder as hostResponder$1, toErrorInfo } from "@powerhousedao/reactor/rpc";
|
|
4
|
+
import { rewritePackageSource } from "@powerhousedao/shared/connect";
|
|
40
5
|
//#region src/rpc/op-channel.ts
|
|
41
6
|
const RPC_DEFAULT_TIMEOUT_MS = 3e4;
|
|
42
7
|
function toVoid(promise) {
|
|
@@ -55,11 +20,53 @@ function opChannel(router, kind, timeoutMs = RPC_DEFAULT_TIMEOUT_MS) {
|
|
|
55
20
|
};
|
|
56
21
|
}
|
|
57
22
|
//#endregion
|
|
23
|
+
//#region src/rpc/adopt-sync-peer.ts
|
|
24
|
+
/** Moves `port` to the host, which adopts the peer as a local remote over it. */
|
|
25
|
+
function sendAdoptSyncPeer(router, params, port) {
|
|
26
|
+
return toVoid(router.request((id) => ({
|
|
27
|
+
k: "adopt-sync-peer",
|
|
28
|
+
id,
|
|
29
|
+
peerId: params.peerId,
|
|
30
|
+
channelName: params.channelName,
|
|
31
|
+
collectionIdKey: params.collectionIdKey,
|
|
32
|
+
remoteName: params.remoteName,
|
|
33
|
+
filter: params.filter,
|
|
34
|
+
port
|
|
35
|
+
}), { transfer: [port] }));
|
|
36
|
+
}
|
|
37
|
+
/** Removes the peer's remote and closes its port in the host's registry. */
|
|
38
|
+
function sendRemoveSyncPeer(router, params) {
|
|
39
|
+
return toVoid(router.request((id) => ({
|
|
40
|
+
k: "remove-sync-peer",
|
|
41
|
+
id,
|
|
42
|
+
peerId: params.peerId,
|
|
43
|
+
channelName: params.channelName,
|
|
44
|
+
remoteName: params.remoteName
|
|
45
|
+
})));
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/rpc/local-sync-peer-handlers.ts
|
|
49
|
+
/** The host's adopt/remove handlers over the reactor's local peer registry. */
|
|
50
|
+
function localSyncPeerHandlers(syncManager, registry) {
|
|
51
|
+
return {
|
|
52
|
+
onAdoptSyncPeer: async (params, port) => {
|
|
53
|
+
await registerLocalPeer(syncManager, registry, {
|
|
54
|
+
peerId: params.peerId,
|
|
55
|
+
channelName: params.channelName,
|
|
56
|
+
collectionId: collectionIdFromKey(params.collectionIdKey),
|
|
57
|
+
remoteName: params.remoteName,
|
|
58
|
+
filter: params.filter
|
|
59
|
+
}, messagePortTransport(port));
|
|
60
|
+
},
|
|
61
|
+
onRemoveSyncPeer: (params) => removeLocalPeer(syncManager, registry, params)
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
58
65
|
//#region src/rpc/admin-client.ts
|
|
59
66
|
/** Worker lifecycle channel (info/restart/clearStorage/migrate) over the shared router. */
|
|
60
67
|
function createWorkerAdminClient(router) {
|
|
61
68
|
let migrationState = { status: "idle" };
|
|
62
|
-
const migrationListeners = new Listeners();
|
|
69
|
+
const migrationListeners = new Listeners$1();
|
|
63
70
|
router.on("migration", (msg) => {
|
|
64
71
|
migrationState = msg.state;
|
|
65
72
|
migrationListeners.emit();
|
|
@@ -79,307 +86,40 @@ function createWorkerAdminClient(router) {
|
|
|
79
86
|
};
|
|
80
87
|
}
|
|
81
88
|
//#endregion
|
|
82
|
-
//#region src/rpc/error-info.ts
|
|
83
|
-
const MAX_STACK_LENGTH = 8 * 1024;
|
|
84
|
-
const MAX_CAUSE_DEPTH = 8;
|
|
85
|
-
function toErrorInfo(err, depth = 0) {
|
|
86
|
-
if (depth > MAX_CAUSE_DEPTH) return {
|
|
87
|
-
name: "Error",
|
|
88
|
-
message: "cause chain truncated"
|
|
89
|
-
};
|
|
90
|
-
if (err instanceof Error) {
|
|
91
|
-
const info = {
|
|
92
|
-
name: err.name || "Error",
|
|
93
|
-
message: err.message
|
|
94
|
-
};
|
|
95
|
-
if (typeof err.stack === "string") info.stack = err.stack.length > MAX_STACK_LENGTH ? err.stack.slice(0, MAX_STACK_LENGTH) : err.stack;
|
|
96
|
-
if (err.cause !== void 0) info.cause = toErrorInfo(err.cause, depth + 1);
|
|
97
|
-
return info;
|
|
98
|
-
}
|
|
99
|
-
if (typeof err === "string") return {
|
|
100
|
-
name: "Error",
|
|
101
|
-
message: err
|
|
102
|
-
};
|
|
103
|
-
if (err && typeof err === "object") {
|
|
104
|
-
const record = err;
|
|
105
|
-
return {
|
|
106
|
-
name: typeof record.name === "string" ? record.name : "Error",
|
|
107
|
-
message: typeof record.message === "string" ? record.message : safeStringify(err)
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
return {
|
|
111
|
-
name: "Error",
|
|
112
|
-
message: safeStringify(err)
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
function safeStringify(value) {
|
|
116
|
-
if (value === null || value === void 0) return String(value);
|
|
117
|
-
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return String(value);
|
|
118
|
-
try {
|
|
119
|
-
return JSON.stringify(value);
|
|
120
|
-
} catch {
|
|
121
|
-
return "[unserializable]";
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
function fromErrorInfo(info) {
|
|
125
|
-
const err = new Error(info.message);
|
|
126
|
-
Object.defineProperty(err, "name", {
|
|
127
|
-
value: info.name,
|
|
128
|
-
configurable: true,
|
|
129
|
-
writable: true
|
|
130
|
-
});
|
|
131
|
-
if (info.stack !== void 0) err.stack = info.stack;
|
|
132
|
-
if (info.cause !== void 0) Object.defineProperty(err, "cause", {
|
|
133
|
-
value: fromErrorInfo(info.cause),
|
|
134
|
-
configurable: true,
|
|
135
|
-
writable: true
|
|
136
|
-
});
|
|
137
|
-
return err;
|
|
138
|
-
}
|
|
139
|
-
//#endregion
|
|
140
|
-
//#region src/rpc/paging.ts
|
|
141
|
-
function isPageableResult(value) {
|
|
142
|
-
return value !== null && typeof value === "object" && typeof value.next === "function";
|
|
143
|
-
}
|
|
144
|
-
function hasNextToken(value) {
|
|
145
|
-
return value !== null && typeof value === "object" && typeof value.nextToken === "string";
|
|
146
|
-
}
|
|
147
|
-
function dehydratePage(value, registerCursor) {
|
|
148
|
-
if (!isPageableResult(value)) return value;
|
|
149
|
-
const { next, ...rest } = value;
|
|
150
|
-
return {
|
|
151
|
-
...rest,
|
|
152
|
-
nextToken: registerCursor(next)
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
function rehydratePage(value, fetchPage) {
|
|
156
|
-
if (!hasNextToken(value)) return value;
|
|
157
|
-
const { nextToken, ...rest } = value;
|
|
158
|
-
return {
|
|
159
|
-
...rest,
|
|
160
|
-
next: () => fetchPage(nextToken)
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
//#endregion
|
|
164
|
-
//#region src/rpc/subscription.ts
|
|
165
|
-
var SubscriptionStore = class {
|
|
166
|
-
map = /* @__PURE__ */ new Map();
|
|
167
|
-
set(id, unsubscribe) {
|
|
168
|
-
this.map.set(id, unsubscribe);
|
|
169
|
-
}
|
|
170
|
-
get(id) {
|
|
171
|
-
return this.map.get(id);
|
|
172
|
-
}
|
|
173
|
-
delete(id) {
|
|
174
|
-
this.map.delete(id);
|
|
175
|
-
}
|
|
176
|
-
end(id) {
|
|
177
|
-
const unsubscribe = this.map.get(id);
|
|
178
|
-
if (unsubscribe) {
|
|
179
|
-
this.map.delete(id);
|
|
180
|
-
unsubscribe();
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
drain() {
|
|
184
|
-
for (const unsubscribe of this.map.values()) unsubscribe();
|
|
185
|
-
this.map.clear();
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
function createCorrelatedSubscriptions(router, config) {
|
|
189
|
-
let counter = 0;
|
|
190
|
-
const subs = /* @__PURE__ */ new Map();
|
|
191
|
-
router.on(config.eventKind, (message) => {
|
|
192
|
-
const sub = subs.get(message.id);
|
|
193
|
-
if (sub) config.onEvent(sub, message);
|
|
194
|
-
});
|
|
195
|
-
router.on(config.errKind, (message) => {
|
|
196
|
-
const id = message.id;
|
|
197
|
-
const sub = subs.get(id);
|
|
198
|
-
if (sub) {
|
|
199
|
-
subs.delete(id);
|
|
200
|
-
router.post(config.close(id));
|
|
201
|
-
config.onError(sub, message);
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
return { subscribe(sub, open) {
|
|
205
|
-
const id = `${config.idPrefix}${++counter}`;
|
|
206
|
-
subs.set(id, sub);
|
|
207
|
-
router.post(open(id));
|
|
208
|
-
return () => {
|
|
209
|
-
subs.delete(id);
|
|
210
|
-
router.post(config.close(id));
|
|
211
|
-
};
|
|
212
|
-
} };
|
|
213
|
-
}
|
|
214
|
-
//#endregion
|
|
215
|
-
//#region src/rpc/client-proxy.ts
|
|
216
|
-
function isAbortSignal(value) {
|
|
217
|
-
return typeof value === "object" && value !== null && typeof value.aborted === "boolean" && typeof value.addEventListener === "function" && typeof value.removeEventListener === "function";
|
|
218
|
-
}
|
|
219
|
-
function createReactorClientProxy(router, options = {}) {
|
|
220
|
-
const registry = options.registry;
|
|
221
|
-
function fetchPage(token) {
|
|
222
|
-
return router.request((id) => ({
|
|
223
|
-
k: "page",
|
|
224
|
-
id,
|
|
225
|
-
token
|
|
226
|
-
}), { transform: rehydrate });
|
|
227
|
-
}
|
|
228
|
-
function rehydrate(value) {
|
|
229
|
-
return rehydratePage(value, fetchPage);
|
|
230
|
-
}
|
|
231
|
-
const changeSubs = createCorrelatedSubscriptions(router, {
|
|
232
|
-
idPrefix: "sub",
|
|
233
|
-
eventKind: "event",
|
|
234
|
-
errKind: "sub-err",
|
|
235
|
-
onEvent: (callback, msg) => callback(msg.change),
|
|
236
|
-
onError: (_callback, msg) => console.error("Reactor subscription failed:", fromErrorInfo(msg.error)),
|
|
237
|
-
close: (id) => ({
|
|
238
|
-
k: "unsub",
|
|
239
|
-
id
|
|
240
|
-
})
|
|
241
|
-
});
|
|
242
|
-
router.on("reload", (msg) => {
|
|
243
|
-
options.onReload?.(msg.reason, msg.workerGen);
|
|
244
|
-
});
|
|
245
|
-
const call = (method, args) => {
|
|
246
|
-
let abortAt;
|
|
247
|
-
for (let i = 0; i < args.length; i++) if (isAbortSignal(args[i])) abortAt = i;
|
|
248
|
-
let wire = args;
|
|
249
|
-
if (abortAt !== void 0) {
|
|
250
|
-
wire = args.slice();
|
|
251
|
-
wire[abortAt] = void 0;
|
|
252
|
-
}
|
|
253
|
-
return router.request((id) => ({
|
|
254
|
-
k: "req",
|
|
255
|
-
id,
|
|
256
|
-
method,
|
|
257
|
-
args: wire,
|
|
258
|
-
abortAt
|
|
259
|
-
}), {
|
|
260
|
-
transform: rehydrate,
|
|
261
|
-
setup: (id) => {
|
|
262
|
-
if (abortAt === void 0) return;
|
|
263
|
-
const signal = args[abortAt];
|
|
264
|
-
const sendAbort = () => router.post({
|
|
265
|
-
k: "abort",
|
|
266
|
-
targetId: id
|
|
267
|
-
});
|
|
268
|
-
if (signal.aborted) {
|
|
269
|
-
sendAbort();
|
|
270
|
-
return;
|
|
271
|
-
}
|
|
272
|
-
signal.addEventListener("abort", sendAbort, { once: true });
|
|
273
|
-
return () => signal.removeEventListener("abort", sendAbort);
|
|
274
|
-
}
|
|
275
|
-
});
|
|
276
|
-
};
|
|
277
|
-
const subscribe = (search, callback, view) => changeSubs.subscribe(callback, (id) => ({
|
|
278
|
-
k: "sub",
|
|
279
|
-
id,
|
|
280
|
-
search,
|
|
281
|
-
view
|
|
282
|
-
}));
|
|
283
|
-
const forwarders = /* @__PURE__ */ new Map();
|
|
284
|
-
const forwarder = (method) => {
|
|
285
|
-
let fn = forwarders.get(method);
|
|
286
|
-
if (!fn) {
|
|
287
|
-
fn = (...args) => call(method, args);
|
|
288
|
-
forwarders.set(method, fn);
|
|
289
|
-
}
|
|
290
|
-
return fn;
|
|
291
|
-
};
|
|
292
|
-
const drives = new Proxy({}, { get: (_target, prop) => typeof prop === "string" ? forwarder(`drives.${prop}`) : void 0 });
|
|
293
|
-
return new Proxy({}, { get(_target, prop) {
|
|
294
|
-
if (typeof prop !== "string" || prop === "then") return;
|
|
295
|
-
if (prop === "drives") return drives;
|
|
296
|
-
if (prop === "subscribe") return subscribe;
|
|
297
|
-
if (registry) {
|
|
298
|
-
if (prop === "getDocumentModelModule") return (documentType) => Promise.resolve().then(() => registry.getModule(documentType));
|
|
299
|
-
if (prop === "getDocumentModelModuleForDocument") return (document) => Promise.resolve().then(() => registry.getModule(document.header.documentType, normalizeDocumentModelVersion(document.state.document?.version)));
|
|
300
|
-
if (prop === "getDocumentModelModules") return (_namespace, paging) => Promise.resolve({
|
|
301
|
-
results: registry.getAllModules(),
|
|
302
|
-
options: paging ?? {
|
|
303
|
-
cursor: "",
|
|
304
|
-
limit: Number.MAX_SAFE_INTEGER
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
return forwarder(prop);
|
|
309
|
-
} });
|
|
310
|
-
}
|
|
311
|
-
//#endregion
|
|
312
89
|
//#region src/rpc/inspector-proxy.ts
|
|
90
|
+
/** Every row of `INSPECTOR_OPS`; the host decides which tiers it serves. */
|
|
313
91
|
function createInspectorProxy(router) {
|
|
314
92
|
const ops = opChannel(router, "inspector-op");
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
validateDocument: (documentId, branch) => ops.call("integrity.validate", [documentId, branch]),
|
|
322
|
-
rebuildKeyframes: (documentId, branch) => ops.call("integrity.rebuildKeyframes", [documentId, branch]),
|
|
323
|
-
rebuildSnapshots: (documentId, branch) => ops.call("integrity.rebuildSnapshots", [documentId, branch]),
|
|
324
|
-
queryReactorDb: (sql, params) => ops.call("db.query", [sql, params ?? []])
|
|
325
|
-
};
|
|
93
|
+
const proxy = {};
|
|
94
|
+
for (const [key, spec] of Object.entries(INSPECTOR_OPS)) proxy[key] = (...args) => ops.call(spec.rpc, args);
|
|
95
|
+
const queryDb = (sql, params) => ops.call(INSPECTOR_OPS.queryDb.rpc, [sql, params ?? []]);
|
|
96
|
+
proxy.queryDb = queryDb;
|
|
97
|
+
proxy.queryReactorDb = queryDb;
|
|
98
|
+
return proxy;
|
|
326
99
|
}
|
|
327
100
|
//#endregion
|
|
328
|
-
//#region src/rpc/
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
if (k === "sub") return "sub-err";
|
|
333
|
-
if (k === "sub-live") return "live-err";
|
|
334
|
-
return "err";
|
|
101
|
+
//#region src/rpc/inspector-ops.ts
|
|
102
|
+
/** Host side of `inspector-op`; refuses tiers `access` does not grant. */
|
|
103
|
+
function dispatchInspectorOp(targets, access, method, args) {
|
|
104
|
+
return dispatchTableOp(INSPECTOR_OPS, (key) => key === "queryDb" ? methodOf(targets.dbQuery, key) : methodOf(targets.inspector, key), access, method, args);
|
|
335
105
|
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
id,
|
|
344
|
-
value
|
|
345
|
-
});
|
|
346
|
-
};
|
|
347
|
-
const err = (id, error) => {
|
|
348
|
-
transport.post({
|
|
349
|
-
k: "err",
|
|
350
|
-
id,
|
|
351
|
-
error: toErrorInfo(error)
|
|
352
|
-
});
|
|
353
|
-
};
|
|
354
|
-
return {
|
|
355
|
-
ok,
|
|
356
|
-
err,
|
|
357
|
-
errForKind(message, error) {
|
|
358
|
-
if (!("id" in message)) return;
|
|
359
|
-
transport.post({
|
|
360
|
-
k: responseErrorKind(message.k),
|
|
361
|
-
id: message.id,
|
|
362
|
-
error: toErrorInfo(error)
|
|
363
|
-
});
|
|
364
|
-
},
|
|
365
|
-
async run(id, body, mapOk = defaultOk) {
|
|
366
|
-
try {
|
|
367
|
-
ok(id, mapOk(await body()));
|
|
368
|
-
} catch (error) {
|
|
369
|
-
err(id, error);
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
};
|
|
106
|
+
/** Whether `method` is one of the `sync-op` rows `dispatchSyncInspectionOp` serves. */
|
|
107
|
+
function isSyncInspectionOp(method) {
|
|
108
|
+
return opKeyOf(SYNC_INSPECTION_OPS, method) !== void 0;
|
|
109
|
+
}
|
|
110
|
+
/** Host side of the inspection and repair rows of `sync-op`. */
|
|
111
|
+
function dispatchSyncInspectionOp(targets, access, method, args) {
|
|
112
|
+
return dispatchTableOp(SYNC_INSPECTION_OPS, (key) => SYNC_INSPECTION_OPS[key].tier === "read" ? methodOf(targets.inspector, key) : methodOf(targets.admin, key), access, method, args);
|
|
373
113
|
}
|
|
374
114
|
//#endregion
|
|
375
115
|
//#region src/rpc/live-query-proxy.ts
|
|
376
116
|
function createLiveQueryProxy(router) {
|
|
377
|
-
const subs = createCorrelatedSubscriptions(router, {
|
|
117
|
+
const subs = createCorrelatedSubscriptions$1(router, {
|
|
378
118
|
idPrefix: "live",
|
|
379
119
|
eventKind: "event-live",
|
|
380
120
|
errKind: "live-err",
|
|
381
121
|
onEvent: (sub, msg) => sub.onResults(msg.results),
|
|
382
|
-
onError: (sub, msg) => sub.onError?.(fromErrorInfo(msg.error)),
|
|
122
|
+
onError: (sub, msg) => sub.onError?.(fromErrorInfo$1(msg.error)),
|
|
383
123
|
close: (id) => ({
|
|
384
124
|
k: "unsub-live",
|
|
385
125
|
id
|
|
@@ -425,7 +165,6 @@ function createRelationalPgliteProxy(router) {
|
|
|
425
165
|
}
|
|
426
166
|
//#endregion
|
|
427
167
|
//#region src/rpc/sync-manager-proxy.ts
|
|
428
|
-
const SYNC_STATUS_CHANGED_EVENT = 90001;
|
|
429
168
|
const SEED_MAX_ATTEMPTS = 3;
|
|
430
169
|
const SEED_RETRY_DELAY_MS = 500;
|
|
431
170
|
const DEFAULT_SNAPSHOT = {
|
|
@@ -470,7 +209,8 @@ function rehydrateMeta(wire) {
|
|
|
470
209
|
collectionId: DriveCollectionId.forDrive(wire.collectionId.driveId, wire.collectionId.branch),
|
|
471
210
|
channelConfig: wire.channelConfig,
|
|
472
211
|
filter: wire.filter,
|
|
473
|
-
options: wire.options
|
|
212
|
+
options: wire.options,
|
|
213
|
+
peer: wire.peer
|
|
474
214
|
};
|
|
475
215
|
}
|
|
476
216
|
function channelUrl(meta) {
|
|
@@ -480,10 +220,11 @@ function channelUrl(meta) {
|
|
|
480
220
|
var SyncManagerProxy = class {
|
|
481
221
|
ops;
|
|
482
222
|
connectionStates = /* @__PURE__ */ new Map();
|
|
483
|
-
connectionListeners = new KeyedListeners();
|
|
223
|
+
connectionListeners = new KeyedListeners$1();
|
|
484
224
|
syncStatuses = /* @__PURE__ */ new Map();
|
|
485
|
-
syncStatusListeners = new Listeners();
|
|
225
|
+
syncStatusListeners = new Listeners$1();
|
|
486
226
|
remotes = [];
|
|
227
|
+
basis;
|
|
487
228
|
seedPromise = null;
|
|
488
229
|
constructor(router, busProxy) {
|
|
489
230
|
this.ops = opChannel(router, "sync-op");
|
|
@@ -497,18 +238,22 @@ var SyncManagerProxy = class {
|
|
|
497
238
|
this.syncStatuses.set(e.documentId, e.status);
|
|
498
239
|
this.syncStatusListeners.emit(e.documentId, e.status);
|
|
499
240
|
});
|
|
241
|
+
busProxy.subscribe(SYNC_REMOTES_CHANGED_EVENT, (_type, event) => {
|
|
242
|
+
this.applyRemotes(event.remotes);
|
|
243
|
+
});
|
|
500
244
|
this.ensureSeeded();
|
|
501
245
|
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
246
|
+
/** Attempts that fail while `isWaiting()` (the worker waits on a store lock) are not counted. */
|
|
247
|
+
async startup({ isWaiting = () => false } = {}) {
|
|
248
|
+
let failures = 0;
|
|
249
|
+
for (;;) try {
|
|
505
250
|
await this.ensureSeeded();
|
|
506
251
|
return;
|
|
507
252
|
} catch (error) {
|
|
508
|
-
|
|
509
|
-
if (
|
|
253
|
+
if (!isWaiting()) failures++;
|
|
254
|
+
if (failures >= SEED_MAX_ATTEMPTS) throw error;
|
|
255
|
+
await new Promise((resolve) => setTimeout(resolve, SEED_RETRY_DELAY_MS));
|
|
510
256
|
}
|
|
511
|
-
throw lastError;
|
|
512
257
|
}
|
|
513
258
|
shutdown() {
|
|
514
259
|
return {
|
|
@@ -537,6 +282,24 @@ var SyncManagerProxy = class {
|
|
|
537
282
|
await this.refreshRemotes();
|
|
538
283
|
return this.getByName(name);
|
|
539
284
|
}
|
|
285
|
+
async setPeerManifest(id, manifest) {
|
|
286
|
+
await this.callSyncOp("setPeerManifest", [id, manifest]);
|
|
287
|
+
await this.refreshRemotes();
|
|
288
|
+
}
|
|
289
|
+
/** Fetched once; the worker's flags do not change at runtime. */
|
|
290
|
+
localManifest() {
|
|
291
|
+
return this.agreementBasis().local;
|
|
292
|
+
}
|
|
293
|
+
async listHolds(filter) {
|
|
294
|
+
return await this.callSyncOp("listHolds", [filter]);
|
|
295
|
+
}
|
|
296
|
+
agreement() {
|
|
297
|
+
return createPeerAgreement(this.agreementBasis(), this.remotes.map((remote) => remote.meta));
|
|
298
|
+
}
|
|
299
|
+
agreementBasis() {
|
|
300
|
+
if (!this.basis) throw new Error("Peer agreement has not been fetched from the worker yet");
|
|
301
|
+
return this.basis;
|
|
302
|
+
}
|
|
540
303
|
async bindRemote(id, boundAddress) {
|
|
541
304
|
await this.callSyncOp("bindRemote", [id, boundAddress]);
|
|
542
305
|
await this.refreshRemotes();
|
|
@@ -562,6 +325,31 @@ var SyncManagerProxy = class {
|
|
|
562
325
|
onSyncStatusChange(callback) {
|
|
563
326
|
return this.syncStatusListeners.add(callback);
|
|
564
327
|
}
|
|
328
|
+
async inspectRemote(remoteName) {
|
|
329
|
+
return await this.tableOp("inspectRemote", [remoteName]);
|
|
330
|
+
}
|
|
331
|
+
async inspectRemotes() {
|
|
332
|
+
return await this.tableOp("inspectRemotes", []);
|
|
333
|
+
}
|
|
334
|
+
async listDeadLetters(remoteName, cursor, limit) {
|
|
335
|
+
return await this.tableOp("listDeadLetters", [
|
|
336
|
+
remoteName,
|
|
337
|
+
cursor,
|
|
338
|
+
limit
|
|
339
|
+
]);
|
|
340
|
+
}
|
|
341
|
+
async resetChannel(remoteName) {
|
|
342
|
+
await this.tableOp("resetChannel", [remoteName]);
|
|
343
|
+
}
|
|
344
|
+
async requeueDeadLetter(remoteName, id) {
|
|
345
|
+
await this.tableOp("requeueDeadLetter", [remoteName, id]);
|
|
346
|
+
}
|
|
347
|
+
async clearDeadLetter(remoteName, id) {
|
|
348
|
+
await this.tableOp("clearDeadLetter", [remoteName, id]);
|
|
349
|
+
}
|
|
350
|
+
tableOp(key, args) {
|
|
351
|
+
return this.ops.call(SYNC_INSPECTION_OPS[key].rpc, args);
|
|
352
|
+
}
|
|
565
353
|
callSyncOp(method, args) {
|
|
566
354
|
return this.ops.call(method, args);
|
|
567
355
|
}
|
|
@@ -591,11 +379,15 @@ var SyncManagerProxy = class {
|
|
|
591
379
|
},
|
|
592
380
|
notePoll: () => {},
|
|
593
381
|
lastHolderPollUtcMs: () => void 0,
|
|
382
|
+
setLocalManifest: () => {},
|
|
383
|
+
onPeerManifest: () => () => {},
|
|
594
384
|
config: { url }
|
|
595
385
|
};
|
|
596
386
|
}
|
|
597
387
|
async refreshRemotes() {
|
|
598
|
-
|
|
388
|
+
this.applyRemotes(await this.callSyncOp("list", []));
|
|
389
|
+
}
|
|
390
|
+
applyRemotes(wire) {
|
|
599
391
|
const names = /* @__PURE__ */ new Set();
|
|
600
392
|
this.remotes = wire.map((w) => {
|
|
601
393
|
const meta = rehydrateMeta(w.meta);
|
|
@@ -610,6 +402,9 @@ var SyncManagerProxy = class {
|
|
|
610
402
|
this.notifyConnection();
|
|
611
403
|
}
|
|
612
404
|
ensureSeeded() {
|
|
405
|
+
if (!this.basis) this.callSyncOp("peerAgreementBasis", []).then((basis) => {
|
|
406
|
+
this.basis = basis;
|
|
407
|
+
}).catch(() => {});
|
|
613
408
|
if (!this.seedPromise) {
|
|
614
409
|
const pending = this.refreshRemotes();
|
|
615
410
|
this.seedPromise = pending;
|
|
@@ -633,13 +428,17 @@ const FORWARDED_EVENT_TYPES = [
|
|
|
633
428
|
SyncEventTypes.CONNECTION_STATE_CHANGED,
|
|
634
429
|
ReactorEventTypes.MODEL_LOADED
|
|
635
430
|
];
|
|
636
|
-
const FORWARDED_BUS_EVENT_TYPES = [
|
|
431
|
+
const FORWARDED_BUS_EVENT_TYPES = [
|
|
432
|
+
...FORWARDED_EVENT_TYPES,
|
|
433
|
+
SYNC_STATUS_CHANGED_EVENT,
|
|
434
|
+
SYNC_REMOTES_CHANGED_EVENT
|
|
435
|
+
];
|
|
637
436
|
//#endregion
|
|
638
437
|
//#region src/rpc/event-bus-proxy.ts
|
|
639
438
|
/** Tab-side IEventBus over bus-event; emit() is unsupported (the worker emits). */
|
|
640
439
|
var ReactorEventBusProxy = class {
|
|
641
440
|
forwardedTypes = new Set(FORWARDED_BUS_EVENT_TYPES);
|
|
642
|
-
subscribers = new KeyedListeners();
|
|
441
|
+
subscribers = new KeyedListeners$1();
|
|
643
442
|
constructor(router) {
|
|
644
443
|
router.on("bus-event", (msg) => {
|
|
645
444
|
this.subscribers.emit(msg.eventType, msg.eventType, msg.event);
|
|
@@ -660,7 +459,7 @@ function createReactorEventBusProxy(router) {
|
|
|
660
459
|
//#endregion
|
|
661
460
|
//#region src/rpc/connect-reactor.ts
|
|
662
461
|
function connectReactorClient(router, hello, onReload, registry) {
|
|
663
|
-
const client = createReactorClientProxy(router, {
|
|
462
|
+
const client = createReactorClientProxy$1(router, {
|
|
664
463
|
onReload,
|
|
665
464
|
registry
|
|
666
465
|
});
|
|
@@ -680,146 +479,13 @@ function postReactorIdentity(router, user) {
|
|
|
680
479
|
});
|
|
681
480
|
}
|
|
682
481
|
//#endregion
|
|
683
|
-
//#region src/rpc/host-server.ts
|
|
684
|
-
const MAX_PENDING_PAGES = 1e3;
|
|
685
|
-
var ReactorHostServer = class {
|
|
686
|
-
client;
|
|
687
|
-
transport;
|
|
688
|
-
reply;
|
|
689
|
-
aborters = /* @__PURE__ */ new Map();
|
|
690
|
-
subscriptions = new SubscriptionStore();
|
|
691
|
-
pages = /* @__PURE__ */ new Map();
|
|
692
|
-
pageCounter = 0;
|
|
693
|
-
detach = () => {};
|
|
694
|
-
constructor(client, transport) {
|
|
695
|
-
this.client = client;
|
|
696
|
-
this.transport = transport;
|
|
697
|
-
this.reply = hostResponder(transport);
|
|
698
|
-
}
|
|
699
|
-
start() {
|
|
700
|
-
this.detach = this.transport.onMessage((message) => {
|
|
701
|
-
this.handleMessage(message);
|
|
702
|
-
});
|
|
703
|
-
}
|
|
704
|
-
stop() {
|
|
705
|
-
this.detach();
|
|
706
|
-
this.subscriptions.drain();
|
|
707
|
-
this.aborters.clear();
|
|
708
|
-
this.pages.clear();
|
|
709
|
-
}
|
|
710
|
-
async handleMessage(message) {
|
|
711
|
-
try {
|
|
712
|
-
switch (message.k) {
|
|
713
|
-
case "req":
|
|
714
|
-
await this.handleRequest(message);
|
|
715
|
-
return;
|
|
716
|
-
case "page":
|
|
717
|
-
await this.handlePage(message);
|
|
718
|
-
return;
|
|
719
|
-
case "abort":
|
|
720
|
-
this.aborters.get(message.targetId)?.abort();
|
|
721
|
-
return;
|
|
722
|
-
case "sub":
|
|
723
|
-
this.handleSubscribe(message);
|
|
724
|
-
return;
|
|
725
|
-
case "unsub":
|
|
726
|
-
this.subscriptions.end(message.id);
|
|
727
|
-
return;
|
|
728
|
-
}
|
|
729
|
-
} catch (error) {
|
|
730
|
-
this.reply.errForKind(message, error);
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
async handleRequest(message) {
|
|
734
|
-
let args = message.args;
|
|
735
|
-
let controller;
|
|
736
|
-
if (message.abortAt !== void 0) {
|
|
737
|
-
controller = new AbortController();
|
|
738
|
-
this.aborters.set(message.id, controller);
|
|
739
|
-
args = message.args.slice();
|
|
740
|
-
args[message.abortAt] = controller.signal;
|
|
741
|
-
}
|
|
742
|
-
try {
|
|
743
|
-
const value = await this.resolveMethod(message.method)(...args);
|
|
744
|
-
this.reply.ok(message.id, this.prepareResult(value));
|
|
745
|
-
} catch (error) {
|
|
746
|
-
this.reply.err(message.id, error);
|
|
747
|
-
} finally {
|
|
748
|
-
if (controller) this.aborters.delete(message.id);
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
async handlePage(message) {
|
|
752
|
-
const next = this.pages.get(message.token);
|
|
753
|
-
this.pages.delete(message.token);
|
|
754
|
-
if (!next) {
|
|
755
|
-
this.reply.err(message.id, /* @__PURE__ */ new Error("Paged cursor already consumed or expired"));
|
|
756
|
-
return;
|
|
757
|
-
}
|
|
758
|
-
try {
|
|
759
|
-
const value = await next();
|
|
760
|
-
this.reply.ok(message.id, this.prepareResult(value));
|
|
761
|
-
} catch (error) {
|
|
762
|
-
this.reply.err(message.id, error);
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
handleSubscribe(message) {
|
|
766
|
-
const unsubscribe = this.client.subscribe(message.search, (change) => this.transport.post({
|
|
767
|
-
k: "event",
|
|
768
|
-
id: message.id,
|
|
769
|
-
change
|
|
770
|
-
}), message.view);
|
|
771
|
-
this.subscriptions.set(message.id, unsubscribe);
|
|
772
|
-
}
|
|
773
|
-
prepareResult(value) {
|
|
774
|
-
return dehydratePage(value, (next) => {
|
|
775
|
-
const token = `p${++this.pageCounter}`;
|
|
776
|
-
this.pages.set(token, next);
|
|
777
|
-
if (this.pages.size > MAX_PENDING_PAGES) {
|
|
778
|
-
const oldest = this.pages.keys().next().value;
|
|
779
|
-
if (oldest !== void 0) this.pages.delete(oldest);
|
|
780
|
-
}
|
|
781
|
-
return token;
|
|
782
|
-
});
|
|
783
|
-
}
|
|
784
|
-
resolveMethod(path) {
|
|
785
|
-
if (path.startsWith("drives.")) {
|
|
786
|
-
const name = path.slice(7);
|
|
787
|
-
const fn = this.client.drives[name];
|
|
788
|
-
if (typeof fn !== "function") throw new Error(`Unknown drive method: ${name}`);
|
|
789
|
-
return fn.bind(this.client.drives);
|
|
790
|
-
}
|
|
791
|
-
const fn = this.client[path];
|
|
792
|
-
if (typeof fn !== "function") throw new Error(`Unknown reactor method: ${path}`);
|
|
793
|
-
return fn.bind(this.client);
|
|
794
|
-
}
|
|
795
|
-
};
|
|
796
|
-
//#endregion
|
|
797
|
-
//#region src/rpc/transport.ts
|
|
798
|
-
function createPortTransport(port) {
|
|
799
|
-
port.start();
|
|
800
|
-
port.addEventListener("messageerror", (event) => {
|
|
801
|
-
console.error("[rpc transport] failed to deserialize message", event);
|
|
802
|
-
});
|
|
803
|
-
return {
|
|
804
|
-
post(message) {
|
|
805
|
-
port.postMessage(message);
|
|
806
|
-
},
|
|
807
|
-
onMessage(listener) {
|
|
808
|
-
const handler = (event) => {
|
|
809
|
-
listener(event.data);
|
|
810
|
-
};
|
|
811
|
-
port.addEventListener("message", handler);
|
|
812
|
-
return () => port.removeEventListener("message", handler);
|
|
813
|
-
},
|
|
814
|
-
close() {
|
|
815
|
-
port.close();
|
|
816
|
-
}
|
|
817
|
-
};
|
|
818
|
-
}
|
|
819
|
-
//#endregion
|
|
820
482
|
//#region src/rpc/reactor-host.ts
|
|
821
483
|
function isDataMessage(msg) {
|
|
822
|
-
return msg.k === "req" || msg.k === "sub" || msg.k === "page" || msg.k === "sync-op" || msg.k === "db-op" || msg.k === "inspector-op" || msg.k === "sub-live";
|
|
484
|
+
return msg.k === "req" || msg.k === "sub" || msg.k === "page" || msg.k === "sync-op" || msg.k === "db-op" || msg.k === "inspector-op" || msg.k === "adopt-sync-peer" || msg.k === "remove-sync-peer" || msg.k === "sub-live";
|
|
485
|
+
}
|
|
486
|
+
/** A rejected adopt still owns the port it moved here, so close it. */
|
|
487
|
+
function closeTransferredPort(msg) {
|
|
488
|
+
if (msg.k === "adopt-sync-peer") msg.port.close();
|
|
823
489
|
}
|
|
824
490
|
function versionsCompatible(a, b) {
|
|
825
491
|
return a.appBuildId === b.appBuildId && a.rpcProtocolVersion === b.rpcProtocolVersion && (a.featureFlags ?? "") === (b.featureFlags ?? "");
|
|
@@ -829,25 +495,38 @@ function workerGenForVersion(version) {
|
|
|
829
495
|
const suffix = flags === "" ? "" : `-${hashFlags(flags)}`;
|
|
830
496
|
return `v${version.rpcProtocolVersion}-${version.appBuildId}${suffix}`;
|
|
831
497
|
}
|
|
498
|
+
const VERSION_MISMATCH = "reactor version mismatch";
|
|
499
|
+
const FLAGS_CHANGED = "reactor enforcement flags changed";
|
|
500
|
+
/** Whether a reload reason comes from a build fingerprint mismatch. */
|
|
501
|
+
function isFingerprintMismatchReload(reason) {
|
|
502
|
+
return reason === VERSION_MISMATCH || reason.startsWith(FLAGS_CHANGED);
|
|
503
|
+
}
|
|
832
504
|
/** Names what differs, so a reload is diagnosable from the tab's console. */
|
|
833
505
|
function mismatchReason(baseline, incoming) {
|
|
834
|
-
if ((baseline.featureFlags ?? "") !== (incoming.featureFlags ?? "")) return
|
|
835
|
-
return
|
|
506
|
+
if ((baseline.featureFlags ?? "") !== (incoming.featureFlags ?? "")) return `${FLAGS_CHANGED} (worker: ${baseline.featureFlags || "none"}, tab: ${incoming.featureFlags || "none"})`;
|
|
507
|
+
return VERSION_MISMATCH;
|
|
836
508
|
}
|
|
837
509
|
function hashFlags(flags) {
|
|
838
510
|
let hash = 0;
|
|
839
511
|
for (let i = 0; i < flags.length; i++) hash = hash * 31 + flags.charCodeAt(i) | 0;
|
|
840
512
|
return (hash >>> 0).toString(36);
|
|
841
513
|
}
|
|
514
|
+
const RETIRED_WORKER_RELOAD_REASON = "worker retired";
|
|
515
|
+
function retiredError() {
|
|
516
|
+
return /* @__PURE__ */ new Error("This worker was retired; reloading into the current one");
|
|
517
|
+
}
|
|
842
518
|
var ReactorHost = class {
|
|
843
519
|
options;
|
|
844
520
|
disposers = /* @__PURE__ */ new Set();
|
|
845
521
|
clients = /* @__PURE__ */ new Set();
|
|
846
522
|
clientPromise = null;
|
|
523
|
+
buildFailure = null;
|
|
847
524
|
baseline = null;
|
|
848
525
|
ownerId;
|
|
849
526
|
bootedAtMs;
|
|
850
527
|
migrationState = null;
|
|
528
|
+
retirement = null;
|
|
529
|
+
reloadSent = false;
|
|
851
530
|
constructor(options) {
|
|
852
531
|
this.options = options;
|
|
853
532
|
this.ownerId = options.ownerId ?? crypto.randomUUID();
|
|
@@ -858,11 +537,12 @@ var ReactorHost = class {
|
|
|
858
537
|
let server = null;
|
|
859
538
|
let ready = false;
|
|
860
539
|
const buffer = [];
|
|
861
|
-
const liveSubs = new SubscriptionStore();
|
|
862
|
-
const reply = hostResponder(transport);
|
|
540
|
+
const liveSubs = new SubscriptionStore$1();
|
|
541
|
+
const reply = hostResponder$1(transport);
|
|
863
542
|
const ensureServer = async (construct) => {
|
|
864
543
|
if (server) return;
|
|
865
|
-
|
|
544
|
+
const client = await this.resolveClient(construct);
|
|
545
|
+
server = new ReactorHostServer$1(client, transport);
|
|
866
546
|
};
|
|
867
547
|
const drainBuffer = async () => {
|
|
868
548
|
while (buffer.length > 0) {
|
|
@@ -883,9 +563,15 @@ var ReactorHost = class {
|
|
|
883
563
|
return;
|
|
884
564
|
}
|
|
885
565
|
if (this.migrationState?.status === "migrating" && isDataMessage(msg)) {
|
|
566
|
+
closeTransferredPort(msg);
|
|
886
567
|
reply.errForKind(msg, /* @__PURE__ */ new Error("migration in progress"));
|
|
887
568
|
return;
|
|
888
569
|
}
|
|
570
|
+
if (this.retirement && isDataMessage(msg)) {
|
|
571
|
+
closeTransferredPort(msg);
|
|
572
|
+
reply.errForKind(msg, retiredError());
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
889
575
|
if (msg.k === "hello") {
|
|
890
576
|
this.handleHello(msg, transport, reply, ensureServer, drainBuffer);
|
|
891
577
|
return;
|
|
@@ -914,6 +600,14 @@ var ReactorHost = class {
|
|
|
914
600
|
this.handleOp(msg, this.options.onInspectorOp, "inspector", reply);
|
|
915
601
|
return;
|
|
916
602
|
}
|
|
603
|
+
if (msg.k === "adopt-sync-peer") {
|
|
604
|
+
this.handleAdoptSyncPeer(msg, reply);
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
if (msg.k === "remove-sync-peer") {
|
|
608
|
+
this.handleRemoveSyncPeer(msg, reply);
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
917
611
|
if (msg.k === "sub-live") {
|
|
918
612
|
this.handleLiveSubscribe(msg, transport, reply, liveSubs);
|
|
919
613
|
return;
|
|
@@ -923,7 +617,7 @@ var ReactorHost = class {
|
|
|
923
617
|
return;
|
|
924
618
|
}
|
|
925
619
|
if (msg.k === "admin") {
|
|
926
|
-
this.handleAdmin(msg, reply);
|
|
620
|
+
this.handleAdmin(msg, reply, transport);
|
|
927
621
|
return;
|
|
928
622
|
}
|
|
929
623
|
if (ready && server) server.handleMessage(msg);
|
|
@@ -934,6 +628,10 @@ var ReactorHost = class {
|
|
|
934
628
|
k: "migration",
|
|
935
629
|
state: this.migrationState
|
|
936
630
|
});
|
|
631
|
+
if (this.retirement && this.reloadSent) transport.post({
|
|
632
|
+
k: "reload",
|
|
633
|
+
...this.retirement
|
|
634
|
+
});
|
|
937
635
|
if (this.options.client) ensureServer().then(drainBuffer).catch((error) => {
|
|
938
636
|
console.error("ReactorHost buffer drain failed", error);
|
|
939
637
|
});
|
|
@@ -948,7 +646,7 @@ var ReactorHost = class {
|
|
|
948
646
|
return dispose;
|
|
949
647
|
}
|
|
950
648
|
connectPort(port) {
|
|
951
|
-
return this.connect(createPortTransport(port));
|
|
649
|
+
return this.connect(createPortTransport$1(port));
|
|
952
650
|
}
|
|
953
651
|
broadcastBusEvent(eventType, event) {
|
|
954
652
|
for (const transport of this.clients) transport.post({
|
|
@@ -964,6 +662,37 @@ var ReactorHost = class {
|
|
|
964
662
|
workerGen
|
|
965
663
|
});
|
|
966
664
|
}
|
|
665
|
+
retireAndReload(reason, workerGen) {
|
|
666
|
+
if (this.retirement) return;
|
|
667
|
+
this.retirement = {
|
|
668
|
+
reason,
|
|
669
|
+
workerGen
|
|
670
|
+
};
|
|
671
|
+
const drain = this.options.drainBeforeReload;
|
|
672
|
+
if (!drain || !isFingerprintMismatchReload(reason)) {
|
|
673
|
+
this.sendReload(reason, workerGen);
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
this.drainThenReload(drain, reason, workerGen);
|
|
677
|
+
}
|
|
678
|
+
async drainThenReload(drain, reason, workerGen) {
|
|
679
|
+
try {
|
|
680
|
+
await drain();
|
|
681
|
+
} catch (error) {
|
|
682
|
+
console.error("ReactorHost drain before reload failed", error);
|
|
683
|
+
}
|
|
684
|
+
this.sendReload(reason, workerGen);
|
|
685
|
+
}
|
|
686
|
+
sendReload(reason, workerGen) {
|
|
687
|
+
this.reloadSent = true;
|
|
688
|
+
this.broadcastReload(reason, workerGen);
|
|
689
|
+
this.stopRetired(reason);
|
|
690
|
+
}
|
|
691
|
+
stopRetired(reason) {
|
|
692
|
+
this.options.onRetire?.(reason).catch((error) => {
|
|
693
|
+
console.error("ReactorHost retirement cleanup failed", error);
|
|
694
|
+
});
|
|
695
|
+
}
|
|
967
696
|
setMigrationState(state) {
|
|
968
697
|
this.migrationState = state;
|
|
969
698
|
for (const transport of this.clients) transport.post({
|
|
@@ -971,10 +700,22 @@ var ReactorHost = class {
|
|
|
971
700
|
state
|
|
972
701
|
});
|
|
973
702
|
}
|
|
703
|
+
get retired() {
|
|
704
|
+
return this.retirement !== null;
|
|
705
|
+
}
|
|
974
706
|
get connectionCount() {
|
|
975
707
|
return this.disposers.size;
|
|
976
708
|
}
|
|
977
|
-
handleAdmin(message, reply) {
|
|
709
|
+
handleAdmin(message, reply, transport) {
|
|
710
|
+
if (this.retirement && message.method !== "info") {
|
|
711
|
+
if (this.reloadSent) transport.post({
|
|
712
|
+
k: "reload",
|
|
713
|
+
reason: RETIRED_WORKER_RELOAD_REASON,
|
|
714
|
+
workerGen: this.retirement.workerGen
|
|
715
|
+
});
|
|
716
|
+
reply.err(message.id, retiredError());
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
978
719
|
if (message.method === "restart") {
|
|
979
720
|
this.options.onAdminRestart?.();
|
|
980
721
|
reply.ok(message.id);
|
|
@@ -994,7 +735,7 @@ var ReactorHost = class {
|
|
|
994
735
|
bootedAtMs: this.bootedAtMs,
|
|
995
736
|
connectedClients: this.connectionCount,
|
|
996
737
|
appBuildId: this.baseline?.appBuildId ?? this.options.appBuildId ?? "unknown",
|
|
997
|
-
rpcProtocolVersion: this.baseline?.rpcProtocolVersion ??
|
|
738
|
+
rpcProtocolVersion: this.baseline?.rpcProtocolVersion ?? RPC_PROTOCOL_VERSION$1,
|
|
998
739
|
featureFlags: this.baseline?.featureFlags ?? ""
|
|
999
740
|
};
|
|
1000
741
|
reply.ok(message.id, info);
|
|
@@ -1005,19 +746,31 @@ var ReactorHost = class {
|
|
|
1005
746
|
});
|
|
1006
747
|
}
|
|
1007
748
|
resolveClient(construct) {
|
|
749
|
+
if (this.retirement) return Promise.reject(retiredError());
|
|
1008
750
|
if (!this.clientPromise) {
|
|
1009
751
|
const build = this.options.build;
|
|
1010
752
|
if (!build) return Promise.reject(/* @__PURE__ */ new Error("ReactorHost has no client or builder"));
|
|
1011
753
|
const pending = build(construct);
|
|
1012
754
|
this.clientPromise = pending;
|
|
1013
|
-
|
|
1014
|
-
|
|
755
|
+
this.buildFailure = null;
|
|
756
|
+
pending.then(() => {
|
|
757
|
+
if (this.retirement && this.reloadSent) this.stopRetired(this.retirement.reason);
|
|
758
|
+
}, () => void 0);
|
|
759
|
+
pending.catch((error) => {
|
|
760
|
+
if (this.clientPromise === pending) {
|
|
761
|
+
this.clientPromise = null;
|
|
762
|
+
this.buildFailure = { error };
|
|
763
|
+
}
|
|
1015
764
|
});
|
|
1016
765
|
}
|
|
1017
766
|
return this.clientPromise;
|
|
1018
767
|
}
|
|
1019
768
|
async awaitClientReady() {
|
|
1020
|
-
if (this.clientPromise)
|
|
769
|
+
if (this.clientPromise) {
|
|
770
|
+
await this.clientPromise;
|
|
771
|
+
return;
|
|
772
|
+
}
|
|
773
|
+
if (this.buildFailure) throw this.buildFailure.error;
|
|
1021
774
|
}
|
|
1022
775
|
requireHandler(handler, message, reply, label) {
|
|
1023
776
|
if (handler) return true;
|
|
@@ -1027,11 +780,7 @@ var ReactorHost = class {
|
|
|
1027
780
|
async handleHello(message, transport, reply, ensureServer, drainBuffer) {
|
|
1028
781
|
if (this.baseline) {
|
|
1029
782
|
if (!versionsCompatible(this.baseline, message.version)) {
|
|
1030
|
-
|
|
1031
|
-
k: "reload",
|
|
1032
|
-
reason: mismatchReason(this.baseline, message.version),
|
|
1033
|
-
workerGen: workerGenForVersion(message.version)
|
|
1034
|
-
});
|
|
783
|
+
this.retireAndReload(mismatchReason(this.baseline, message.version), `${workerGenForVersion(message.version)}-${this.ownerId.slice(0, 8)}`);
|
|
1035
784
|
reply.ok(message.id, { ok: false });
|
|
1036
785
|
return;
|
|
1037
786
|
}
|
|
@@ -1044,7 +793,7 @@ var ReactorHost = class {
|
|
|
1044
793
|
}
|
|
1045
794
|
async handleRegister(message, reply) {
|
|
1046
795
|
await reply.run(message.id, async () => {
|
|
1047
|
-
await this.options.registerPackages?.(message.specs);
|
|
796
|
+
await this.options.registerPackages?.(message.specs, message.sources);
|
|
1048
797
|
});
|
|
1049
798
|
}
|
|
1050
799
|
async handleUnregister(message, reply) {
|
|
@@ -1059,6 +808,53 @@ var ReactorHost = class {
|
|
|
1059
808
|
return handler(message.method, message.args);
|
|
1060
809
|
}, (value) => value);
|
|
1061
810
|
}
|
|
811
|
+
/** Closes the moved port on every failure; the sender can no longer close it. */
|
|
812
|
+
async handleAdoptSyncPeer(message, reply) {
|
|
813
|
+
const handler = this.options.onAdoptSyncPeer;
|
|
814
|
+
if (!handler) {
|
|
815
|
+
closeTransferredPort(message);
|
|
816
|
+
reply.errForKind(message, /* @__PURE__ */ new Error("ReactorHost has no adopt-sync-peer handler"));
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
await reply.run(message.id, async () => {
|
|
820
|
+
try {
|
|
821
|
+
await this.awaitClientReady();
|
|
822
|
+
await handler({
|
|
823
|
+
peerId: message.peerId,
|
|
824
|
+
channelName: message.channelName,
|
|
825
|
+
collectionIdKey: message.collectionIdKey,
|
|
826
|
+
remoteName: message.remoteName,
|
|
827
|
+
filter: message.filter
|
|
828
|
+
}, message.port);
|
|
829
|
+
} catch (error) {
|
|
830
|
+
closeTransferredPort(message);
|
|
831
|
+
throw error;
|
|
832
|
+
}
|
|
833
|
+
await this.broadcastRemotes();
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
async handleRemoveSyncPeer(message, reply) {
|
|
837
|
+
const handler = this.options.onRemoveSyncPeer;
|
|
838
|
+
if (!this.requireHandler(handler, message, reply, "remove-sync-peer")) return;
|
|
839
|
+
await reply.run(message.id, async () => {
|
|
840
|
+
await this.awaitClientReady();
|
|
841
|
+
await handler({
|
|
842
|
+
peerId: message.peerId,
|
|
843
|
+
channelName: message.channelName,
|
|
844
|
+
remoteName: message.remoteName
|
|
845
|
+
});
|
|
846
|
+
await this.broadcastRemotes();
|
|
847
|
+
});
|
|
848
|
+
}
|
|
849
|
+
async broadcastRemotes() {
|
|
850
|
+
const list = this.options.onSyncOp;
|
|
851
|
+
if (!list) return;
|
|
852
|
+
try {
|
|
853
|
+
this.broadcastBusEvent(SYNC_REMOTES_CHANGED_EVENT, { remotes: await list("list", []) });
|
|
854
|
+
} catch (error) {
|
|
855
|
+
console.error("ReactorHost could not list remotes for its tabs", error);
|
|
856
|
+
}
|
|
857
|
+
}
|
|
1062
858
|
async handleLiveSubscribe(message, transport, reply, liveSubs) {
|
|
1063
859
|
const handler = this.options.onLiveQuery;
|
|
1064
860
|
if (!this.requireHandler(handler, message, reply, "live-query")) return;
|
|
@@ -1086,9 +882,11 @@ var ReactorHost = class {
|
|
|
1086
882
|
};
|
|
1087
883
|
//#endregion
|
|
1088
884
|
//#region src/rpc/worker-package-loader.ts
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
885
|
+
const SOURCE_KEY_PREFIX = "src:";
|
|
886
|
+
function packageName(key) {
|
|
887
|
+
if (key.startsWith(SOURCE_KEY_PREFIX)) return key.slice(4);
|
|
888
|
+
const at = key.lastIndexOf("@");
|
|
889
|
+
return at > 0 ? key.slice(0, at) : key;
|
|
1092
890
|
}
|
|
1093
891
|
function moduleKey(module) {
|
|
1094
892
|
return `${module.documentModel.global.id}@${module.version ?? 1}`;
|
|
@@ -1098,16 +896,44 @@ function isDocumentModelModule(value) {
|
|
|
1098
896
|
const candidate = value;
|
|
1099
897
|
return typeof candidate.reducer === "function" && typeof candidate.documentModel?.global?.id === "string";
|
|
1100
898
|
}
|
|
899
|
+
function isUpgradeManifest(value) {
|
|
900
|
+
if (typeof value !== "object" || value === null) return false;
|
|
901
|
+
const candidate = value;
|
|
902
|
+
return typeof candidate.documentType === "string" && typeof candidate.upgrades === "object" && candidate.upgrades !== null;
|
|
903
|
+
}
|
|
904
|
+
function manifestsOf(namespace) {
|
|
905
|
+
const out = [];
|
|
906
|
+
for (const value of Object.values(namespace)) {
|
|
907
|
+
if (isUpgradeManifest(value)) {
|
|
908
|
+
out.push(value);
|
|
909
|
+
continue;
|
|
910
|
+
}
|
|
911
|
+
if (Array.isArray(value)) {
|
|
912
|
+
for (const inner of value) if (isUpgradeManifest(inner)) out.push(inner);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
return out;
|
|
916
|
+
}
|
|
1101
917
|
var WorkerPackageLoader = class {
|
|
1102
918
|
cdnUrl;
|
|
1103
919
|
importPackage;
|
|
1104
920
|
resolvePackages;
|
|
921
|
+
sharedImports;
|
|
922
|
+
importSource;
|
|
1105
923
|
modulesByKey = /* @__PURE__ */ new Map();
|
|
924
|
+
manifestsByType = /* @__PURE__ */ new Map();
|
|
925
|
+
keysBySource = /* @__PURE__ */ new Map();
|
|
926
|
+
manifestTypesBySource = /* @__PURE__ */ new Map();
|
|
1106
927
|
loadedSpecs = /* @__PURE__ */ new Set();
|
|
1107
928
|
failures = [];
|
|
929
|
+
pendingLoads = /* @__PURE__ */ new Map();
|
|
930
|
+
generationBySource = /* @__PURE__ */ new Map();
|
|
931
|
+
generation = 0;
|
|
1108
932
|
constructor(options) {
|
|
1109
933
|
this.cdnUrl = options.cdnUrl.replace(/\/$/, "");
|
|
1110
934
|
this.importPackage = options.importPackage;
|
|
935
|
+
this.sharedImports = options.sharedImports;
|
|
936
|
+
this.importSource = options.importSource;
|
|
1111
937
|
const registryClient = new RegistryClient(options.cdnUrl);
|
|
1112
938
|
this.resolvePackages = options.resolvePackages ?? ((documentType) => registryClient.getPackagesByDocumentType(documentType));
|
|
1113
939
|
}
|
|
@@ -1115,6 +941,29 @@ var WorkerPackageLoader = class {
|
|
|
1115
941
|
await Promise.all([...new Set(specs)].map((spec) => this.loadPackage(spec)));
|
|
1116
942
|
return this.models;
|
|
1117
943
|
}
|
|
944
|
+
/** Loads URL-addressed packages (local project packages). Idempotent. */
|
|
945
|
+
async loadSources(sources) {
|
|
946
|
+
await Promise.all(sources.map((source) => this.loadFromUrl(sourceKey(source), source.url)));
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* Replaces previously loaded sources with freshly imported ones (a vetra
|
|
950
|
+
* watch rebuild re-sends the same source under a cache-busted URL). Returns
|
|
951
|
+
* every document type touched - removed, re-added, or new - so the caller
|
|
952
|
+
* can replace the registry's version families. A source whose import fails
|
|
953
|
+
* keeps its previous modules and is reported in `failures`; one superseded
|
|
954
|
+
* by a newer reload of the same source contributes nothing.
|
|
955
|
+
*/
|
|
956
|
+
async reloadSources(sources) {
|
|
957
|
+
const results = await Promise.all(sources.map((source) => this.replaceSource(sourceKey(source), source.url)));
|
|
958
|
+
const types = /* @__PURE__ */ new Set();
|
|
959
|
+
const failures = [];
|
|
960
|
+
for (const result of results) if (result.status === "replaced") for (const type of result.types) types.add(type);
|
|
961
|
+
else if (result.status === "failed") failures.push(result.failure);
|
|
962
|
+
return {
|
|
963
|
+
types: [...types],
|
|
964
|
+
failures
|
|
965
|
+
};
|
|
966
|
+
}
|
|
1118
967
|
async load(documentType) {
|
|
1119
968
|
const existing = this.latestForType(documentType);
|
|
1120
969
|
if (existing) return existing;
|
|
@@ -1128,6 +977,9 @@ var WorkerPackageLoader = class {
|
|
|
1128
977
|
get models() {
|
|
1129
978
|
return [...new Set(this.modulesByKey.values())];
|
|
1130
979
|
}
|
|
980
|
+
get upgradeManifests() {
|
|
981
|
+
return [...this.manifestsByType.values()];
|
|
982
|
+
}
|
|
1131
983
|
get loadFailures() {
|
|
1132
984
|
return [...this.failures];
|
|
1133
985
|
}
|
|
@@ -1147,137 +999,86 @@ var WorkerPackageLoader = class {
|
|
|
1147
999
|
return new Error(`Failed to import package(s) [${packageNames.join(", ")}] for document model: ${documentType}`, { cause });
|
|
1148
1000
|
}
|
|
1149
1001
|
async loadPackage(spec) {
|
|
1150
|
-
if (this.loadedSpecs.has(spec)) return;
|
|
1151
1002
|
const name = packageName(spec);
|
|
1152
1003
|
const url = `${this.cdnUrl}/${name}/browser/document-models/index.js`;
|
|
1004
|
+
await this.loadFromUrl(spec, url);
|
|
1005
|
+
}
|
|
1006
|
+
loadFromUrl(key, url) {
|
|
1007
|
+
if (this.loadedSpecs.has(key)) return Promise.resolve();
|
|
1008
|
+
let pending = this.pendingLoads.get(key);
|
|
1009
|
+
if (!pending) {
|
|
1010
|
+
pending = this.replaceSource(key, url).finally(() => this.pendingLoads.delete(key)).then(() => void 0);
|
|
1011
|
+
this.pendingLoads.set(key, pending);
|
|
1012
|
+
}
|
|
1013
|
+
return pending;
|
|
1014
|
+
}
|
|
1015
|
+
async replaceSource(key, url) {
|
|
1016
|
+
const generation = ++this.generation;
|
|
1017
|
+
this.generationBySource.set(key, generation);
|
|
1018
|
+
let namespace;
|
|
1153
1019
|
try {
|
|
1154
|
-
|
|
1155
|
-
for (const value of Object.values(namespace)) if (isDocumentModelModule(value)) this.modulesByKey.set(moduleKey(value), value);
|
|
1156
|
-
this.loadedSpecs.add(spec);
|
|
1020
|
+
namespace = await this.importNamespace(url);
|
|
1157
1021
|
} catch (error) {
|
|
1158
|
-
this.
|
|
1159
|
-
|
|
1022
|
+
if (this.generationBySource.get(key) !== generation) return { status: "stale" };
|
|
1023
|
+
const failure = {
|
|
1024
|
+
name: packageName(key),
|
|
1160
1025
|
url,
|
|
1161
1026
|
error
|
|
1162
|
-
}
|
|
1027
|
+
};
|
|
1028
|
+
this.failures.push(failure);
|
|
1029
|
+
return {
|
|
1030
|
+
status: "failed",
|
|
1031
|
+
failure
|
|
1032
|
+
};
|
|
1163
1033
|
}
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
* res/err settlement, and per-request timeout/transform/cleanup.
|
|
1171
|
-
*/
|
|
1172
|
-
var RpcCorrelator = class {
|
|
1173
|
-
counter = 0;
|
|
1174
|
-
pending = /* @__PURE__ */ new Map();
|
|
1175
|
-
constructor(poster, prefix = "r") {
|
|
1176
|
-
this.poster = poster;
|
|
1177
|
-
this.prefix = prefix;
|
|
1178
|
-
}
|
|
1179
|
-
nextId() {
|
|
1180
|
-
return `${this.prefix}${++this.counter}`;
|
|
1181
|
-
}
|
|
1182
|
-
/** Settle the pending request a res/err refers to; a missing id is a no-op. */
|
|
1183
|
-
handleMessage(message) {
|
|
1184
|
-
if (message.k !== "res" && message.k !== "err") return;
|
|
1185
|
-
const entry = this.pending.get(message.id);
|
|
1186
|
-
if (!entry) return;
|
|
1187
|
-
this.pending.delete(message.id);
|
|
1188
|
-
if (entry.timer) clearTimeout(entry.timer);
|
|
1189
|
-
entry.cleanup?.();
|
|
1190
|
-
if (message.k === "res") entry.resolve(entry.transform ? entry.transform(message.value) : message.value);
|
|
1191
|
-
else entry.reject(fromErrorInfo(message.error));
|
|
1192
|
-
}
|
|
1193
|
-
/** Send a request, resolving on its res/err. `build` stamps the generated id. */
|
|
1194
|
-
request(build, options = {}) {
|
|
1195
|
-
const id = this.nextId();
|
|
1196
|
-
const message = build(id);
|
|
1197
|
-
const method = message.method;
|
|
1198
|
-
const label = typeof method === "string" ? `${message.k} "${method}"` : message.k;
|
|
1199
|
-
const promise = new Promise((resolve, reject) => {
|
|
1200
|
-
let timer;
|
|
1201
|
-
if (options.timeoutMs !== void 0) {
|
|
1202
|
-
const timeoutMs = options.timeoutMs;
|
|
1203
|
-
timer = setTimeout(() => {
|
|
1204
|
-
const entry = this.pending.get(id);
|
|
1205
|
-
if (entry) {
|
|
1206
|
-
this.pending.delete(id);
|
|
1207
|
-
entry.cleanup?.();
|
|
1208
|
-
reject(/* @__PURE__ */ new Error(`Reactor worker did not respond to ${label} within ${timeoutMs}ms; the worker may have failed to load`));
|
|
1209
|
-
}
|
|
1210
|
-
}, timeoutMs);
|
|
1211
|
-
}
|
|
1212
|
-
this.pending.set(id, {
|
|
1213
|
-
resolve,
|
|
1214
|
-
reject,
|
|
1215
|
-
timer,
|
|
1216
|
-
transform: options.transform
|
|
1217
|
-
});
|
|
1218
|
-
});
|
|
1219
|
-
this.poster.post(message);
|
|
1220
|
-
const cleanup = options.setup?.(id);
|
|
1221
|
-
if (cleanup) {
|
|
1222
|
-
const entry = this.pending.get(id);
|
|
1223
|
-
if (entry) entry.cleanup = cleanup;
|
|
1224
|
-
else cleanup();
|
|
1034
|
+
if (this.generationBySource.get(key) !== generation) return { status: "stale" };
|
|
1035
|
+
const types = /* @__PURE__ */ new Set();
|
|
1036
|
+
for (const removedKey of this.keysBySource.get(key) ?? []) {
|
|
1037
|
+
const removed = this.modulesByKey.get(removedKey);
|
|
1038
|
+
if (removed) types.add(removed.documentModel.global.id);
|
|
1039
|
+
this.modulesByKey.delete(removedKey);
|
|
1225
1040
|
}
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
* Owns the single transport.onMessage and dispatches each message to the one
|
|
1233
|
-
* consumer that owns its kind. Composes the shared RpcCorrelator (res/err) and
|
|
1234
|
-
* exposes request/nextId; attach/detach swap the transport for reconnect.
|
|
1235
|
-
*/
|
|
1236
|
-
var MessageRouter = class {
|
|
1237
|
-
handlers = /* @__PURE__ */ new Map();
|
|
1238
|
-
correlator;
|
|
1239
|
-
transport = null;
|
|
1240
|
-
detachTransport = () => {};
|
|
1241
|
-
constructor(prefix = "r") {
|
|
1242
|
-
this.correlator = new RpcCorrelator(this, prefix);
|
|
1243
|
-
const settle = (message) => this.correlator.handleMessage(message);
|
|
1244
|
-
this.handlers.set("res", settle);
|
|
1245
|
-
this.handlers.set("err", settle);
|
|
1246
|
-
}
|
|
1247
|
-
attach(transport) {
|
|
1248
|
-
this.detachTransport();
|
|
1249
|
-
this.transport = transport;
|
|
1250
|
-
this.detachTransport = transport.onMessage((message) => {
|
|
1251
|
-
const msg = message;
|
|
1252
|
-
this.handlers.get(msg.k)?.(msg);
|
|
1253
|
-
});
|
|
1254
|
-
}
|
|
1255
|
-
detach() {
|
|
1256
|
-
this.detachTransport();
|
|
1257
|
-
this.detachTransport = () => {};
|
|
1258
|
-
this.transport = null;
|
|
1259
|
-
}
|
|
1260
|
-
post(message) {
|
|
1261
|
-
if (!this.transport) throw new Error("MessageRouter.post called before attach");
|
|
1262
|
-
this.transport.post(message);
|
|
1263
|
-
}
|
|
1264
|
-
/** Register the sole owner of a message kind; throws on a duplicate. */
|
|
1265
|
-
on(kind, handler) {
|
|
1266
|
-
if (this.handlers.has(kind)) throw new Error(`MessageRouter already has a handler for "${kind}"`);
|
|
1267
|
-
const route = handler;
|
|
1268
|
-
this.handlers.set(kind, route);
|
|
1269
|
-
return () => {
|
|
1270
|
-
if (this.handlers.get(kind) === route) this.handlers.delete(kind);
|
|
1041
|
+
for (const type of this.manifestTypesBySource.get(key) ?? []) this.manifestsByType.delete(type);
|
|
1042
|
+
for (const added of this.registerNamespace(key, namespace)) types.add(added);
|
|
1043
|
+
this.loadedSpecs.add(key);
|
|
1044
|
+
return {
|
|
1045
|
+
status: "replaced",
|
|
1046
|
+
types
|
|
1271
1047
|
};
|
|
1272
1048
|
}
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1049
|
+
async importNamespace(url) {
|
|
1050
|
+
const source = this.sharedImports !== void 0 && Object.keys(this.sharedImports).length > 0 ? await (await fetch(url)).text() : void 0;
|
|
1051
|
+
const rewritten = source !== void 0 ? rewritePackageSource(source, url, this.sharedImports) : source;
|
|
1052
|
+
if (rewritten !== void 0 && rewritten !== source) {
|
|
1053
|
+
if (!this.importSource) throw new Error("importSource is required to load a package that imports shared deps");
|
|
1054
|
+
return this.importSource(rewritten);
|
|
1055
|
+
}
|
|
1056
|
+
return this.importPackage(url);
|
|
1057
|
+
}
|
|
1058
|
+
/** Registers a namespace as the key's whole contribution; returns its module types. */
|
|
1059
|
+
registerNamespace(key, namespace) {
|
|
1060
|
+
const keys = /* @__PURE__ */ new Set();
|
|
1061
|
+
const manifestTypes = /* @__PURE__ */ new Set();
|
|
1062
|
+
const types = /* @__PURE__ */ new Set();
|
|
1063
|
+
for (const value of Object.values(namespace)) if (isDocumentModelModule(value)) {
|
|
1064
|
+
const k = moduleKey(value);
|
|
1065
|
+
this.modulesByKey.set(k, value);
|
|
1066
|
+
keys.add(k);
|
|
1067
|
+
types.add(value.documentModel.global.id);
|
|
1068
|
+
}
|
|
1069
|
+
for (const manifest of manifestsOf(namespace)) {
|
|
1070
|
+
this.manifestsByType.set(manifest.documentType, manifest);
|
|
1071
|
+
manifestTypes.add(manifest.documentType);
|
|
1072
|
+
}
|
|
1073
|
+
this.keysBySource.set(key, keys);
|
|
1074
|
+
this.manifestTypesBySource.set(key, manifestTypes);
|
|
1075
|
+
return types;
|
|
1278
1076
|
}
|
|
1279
1077
|
};
|
|
1078
|
+
function sourceKey(source) {
|
|
1079
|
+
return `${SOURCE_KEY_PREFIX}${source.name}`;
|
|
1080
|
+
}
|
|
1280
1081
|
//#endregion
|
|
1281
|
-
export { FORWARDED_BUS_EVENT_TYPES, FORWARDED_EVENT_TYPES, KeyedListeners, Listeners, MessageRouter, RPC_DEFAULT_TIMEOUT_MS, RPC_PROTOCOL_VERSION, ReactorEventBusProxy, ReactorHost, ReactorHostServer, RpcCorrelator, SYNC_STATUS_CHANGED_EVENT, SubscriptionStore, SyncManagerProxy, WorkerPackageLoader, connectReactorClient, createCorrelatedSubscriptions, createInspectorProxy, createLiveQueryProxy, createPortTransport, createReactorClientProxy, createReactorEventBusProxy, createRelationalPgliteProxy, createSyncManagerProxy, createWorkerAdminClient, fromErrorInfo, hostResponder, opChannel, postReactorIdentity, toErrorInfo, toVoid };
|
|
1082
|
+
export { FORWARDED_BUS_EVENT_TYPES, FORWARDED_EVENT_TYPES, KeyedListeners, Listeners, MessageRouter, RETIRED_WORKER_RELOAD_REASON, RPC_DEFAULT_TIMEOUT_MS, RPC_PROTOCOL_VERSION, ReactorEventBusProxy, ReactorHost, ReactorHostServer, RpcCorrelator, SYNC_STATUS_CHANGED_EVENT, SubscriptionStore, SyncManagerProxy, WorkerPackageLoader, connectReactorClient, createCorrelatedSubscriptions, createInspectorProxy, createLiveQueryProxy, createPortTransport, createReactorClientProxy, createReactorEventBusProxy, createRelationalPgliteProxy, createSyncManagerProxy, createWorkerAdminClient, dispatchInspectorOp, dispatchSyncInspectionOp, fromErrorInfo, hostResponder, isFingerprintMismatchReload, isSyncInspectionOp, localSyncPeerHandlers, opChannel, postReactorIdentity, sendAdoptSyncPeer, sendRemoveSyncPeer, toErrorInfo, toVoid };
|
|
1282
1083
|
|
|
1283
1084
|
//# sourceMappingURL=index.js.map
|