@powerhousedao/reactor-browser 6.2.3-staging.0 → 6.3.0-dev.1
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-But1Xljo.d.ts → attachment-service-B-AmTrs2.d.ts} +605 -56
- package/dist/attachment-service-B-AmTrs2.d.ts.map +1 -0
- package/dist/auth-DRbzRdeC.d.ts +32 -0
- package/dist/auth-DRbzRdeC.d.ts.map +1 -0
- package/dist/{client-BiJAJ9dW.js → client-BFnMV-EO.js} +39 -22
- package/dist/client-BFnMV-EO.js.map +1 -0
- package/dist/{client-CngU4mtn.d.ts → client-BP0hXjqq.d.ts} +3 -2
- package/dist/client-BP0hXjqq.d.ts.map +1 -0
- package/dist/client-wwavHFfs.js +135 -0
- package/dist/client-wwavHFfs.js.map +1 -0
- package/dist/{dispatch-qRXS70fh.js → dispatch-Cylwl2Dk.js} +363 -44
- package/dist/dispatch-Cylwl2Dk.js.map +1 -0
- package/dist/{document-by-id-B_8DA4yZ.js → document-by-id-DdvqanGR.js} +2 -2
- package/dist/{document-by-id-B_8DA4yZ.js.map → document-by-id-DdvqanGR.js.map} +1 -1
- package/dist/document-model-modules-BG-61uke.js +710 -0
- package/dist/document-model-modules-BG-61uke.js.map +1 -0
- package/dist/{document-operations-Bt-1-ThH.js → document-operations-DRMMwmGZ.js} +2160 -181
- package/dist/document-operations-DRMMwmGZ.js.map +1 -0
- package/dist/global-iARBhxNI.d.ts +612 -0
- package/dist/global-iARBhxNI.d.ts.map +1 -0
- package/dist/{index-BABMK8cO.d.ts → index-CNS_9THu.d.ts} +14 -4
- package/dist/index-CNS_9THu.d.ts.map +1 -0
- package/dist/index.d.ts +560 -79
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +790 -317
- 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/{relational-ChC_AWAK.js → relational-DZuyKp2y.js} +65 -5
- package/dist/relational-DZuyKp2y.js.map +1 -0
- package/dist/{renown-C9pYZRqf.js → renown-B3MUaiq7.js} +56 -25
- package/dist/renown-B3MUaiq7.js.map +1 -0
- package/dist/renown-CBoTdGrg.js +394 -0
- package/dist/renown-CBoTdGrg.js.map +1 -0
- package/dist/{client-VV9M73s_.js → schema-B5lftDPf.js} +295 -195
- package/dist/schema-B5lftDPf.js.map +1 -0
- package/dist/{types-B7-_l_wE.d.ts → schema-C6ECceaN.d.ts} +272 -82
- package/dist/schema-C6ECceaN.d.ts.map +1 -0
- package/dist/src/ai/index.d.ts +269 -24
- package/dist/src/ai/index.d.ts.map +1 -1
- package/dist/src/ai/index.js +1028 -168
- package/dist/src/ai/index.js.map +1 -1
- package/dist/src/analytics/index.js +3 -3
- package/dist/src/graphql/client.d.ts +2 -2
- package/dist/src/graphql/client.js +1 -1
- package/dist/src/graphql-client/entry.d.ts +6 -4
- package/dist/src/graphql-client/entry.js +4 -4
- package/dist/src/relational/index.d.ts +8 -1
- 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 +2 -2
- package/dist/src/renown/index.js +3 -3
- package/dist/src/rpc/index.d.ts +201 -144
- package/dist/src/rpc/index.d.ts.map +1 -1
- package/dist/src/rpc/index.js +562 -634
- package/dist/src/rpc/index.js.map +1 -1
- package/dist/switchboard-directory-vH6qtkh-.js +435 -0
- package/dist/switchboard-directory-vH6qtkh-.js.map +1 -0
- package/dist/switchboards-D-_JBUbF.d.ts +75 -0
- package/dist/switchboards-D-_JBUbF.d.ts.map +1 -0
- package/dist/types-BWx6FPO4.d.ts +63 -0
- package/dist/types-BWx6FPO4.d.ts.map +1 -0
- package/package.json +19 -17
- package/dist/attachment-service-But1Xljo.d.ts.map +0 -1
- package/dist/client-BiJAJ9dW.js.map +0 -1
- package/dist/client-CngU4mtn.d.ts.map +0 -1
- package/dist/client-VV9M73s_.js.map +0 -1
- package/dist/dispatch-qRXS70fh.js.map +0 -1
- package/dist/document-model-modules-D6I-UUXq.js +0 -386
- package/dist/document-model-modules-D6I-UUXq.js.map +0 -1
- package/dist/document-operations-Bt-1-ThH.js.map +0 -1
- package/dist/global-fql2bmb0.d.ts +0 -438
- package/dist/global-fql2bmb0.d.ts.map +0 -1
- package/dist/index-BABMK8cO.d.ts.map +0 -1
- package/dist/inspector-proxy-DCowaeeH.d.ts +0 -264
- package/dist/inspector-proxy-DCowaeeH.d.ts.map +0 -1
- package/dist/make-ph-event-functions-FzpvQtj6.js +0 -53
- package/dist/make-ph-event-functions-FzpvQtj6.js.map +0 -1
- package/dist/relational-ChC_AWAK.js.map +0 -1
- package/dist/renown-C9pYZRqf.js.map +0 -1
- package/dist/renown-Def5iO8k.js +0 -97
- package/dist/renown-Def5iO8k.js.map +0 -1
- package/dist/switchboard-CFyD_5_e.js +0 -323
- package/dist/switchboard-CFyD_5_e.js.map +0 -1
- package/dist/types-B7-_l_wE.d.ts.map +0 -1
package/dist/src/rpc/index.js
CHANGED
|
@@ -1,43 +1,7 @@
|
|
|
1
|
-
import { t as RegistryClient } from "../../client-
|
|
2
|
-
import { DriveCollectionId, ReactorEventTypes, SyncEventTypes, createPeerAgreement } from "@powerhousedao/reactor";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
//#region src/rpc/listeners.ts
|
|
6
|
-
var KeyedListeners = class {
|
|
7
|
-
map = /* @__PURE__ */ new Map();
|
|
8
|
-
add(key, fn) {
|
|
9
|
-
let set = this.map.get(key);
|
|
10
|
-
if (!set) {
|
|
11
|
-
set = /* @__PURE__ */ new Set();
|
|
12
|
-
this.map.set(key, set);
|
|
13
|
-
}
|
|
14
|
-
set.add(fn);
|
|
15
|
-
return () => {
|
|
16
|
-
const current = this.map.get(key);
|
|
17
|
-
if (!current) return;
|
|
18
|
-
current.delete(fn);
|
|
19
|
-
if (current.size === 0) this.map.delete(key);
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
emit(key, ...a) {
|
|
23
|
-
const set = this.map.get(key);
|
|
24
|
-
if (!set) return;
|
|
25
|
-
for (const fn of [...set]) fn(...a);
|
|
26
|
-
}
|
|
27
|
-
emitAll(...a) {
|
|
28
|
-
for (const set of [...this.map.values()]) for (const fn of [...set]) fn(...a);
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
var Listeners = class {
|
|
32
|
-
keyed = new KeyedListeners();
|
|
33
|
-
add(fn) {
|
|
34
|
-
return this.keyed.add(null, fn);
|
|
35
|
-
}
|
|
36
|
-
emit(...a) {
|
|
37
|
-
this.keyed.emit(null, ...a);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
//#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 { init, parse } from "es-module-lexer";
|
|
41
5
|
//#region src/rpc/op-channel.ts
|
|
42
6
|
const RPC_DEFAULT_TIMEOUT_MS = 3e4;
|
|
43
7
|
function toVoid(promise) {
|
|
@@ -56,11 +20,53 @@ function opChannel(router, kind, timeoutMs = RPC_DEFAULT_TIMEOUT_MS) {
|
|
|
56
20
|
};
|
|
57
21
|
}
|
|
58
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
|
|
59
65
|
//#region src/rpc/admin-client.ts
|
|
60
66
|
/** Worker lifecycle channel (info/restart/clearStorage/migrate) over the shared router. */
|
|
61
67
|
function createWorkerAdminClient(router) {
|
|
62
68
|
let migrationState = { status: "idle" };
|
|
63
|
-
const migrationListeners = new Listeners();
|
|
69
|
+
const migrationListeners = new Listeners$1();
|
|
64
70
|
router.on("migration", (msg) => {
|
|
65
71
|
migrationState = msg.state;
|
|
66
72
|
migrationListeners.emit();
|
|
@@ -80,309 +86,40 @@ function createWorkerAdminClient(router) {
|
|
|
80
86
|
};
|
|
81
87
|
}
|
|
82
88
|
//#endregion
|
|
83
|
-
//#region src/rpc/error-info.ts
|
|
84
|
-
const MAX_STACK_LENGTH = 8192;
|
|
85
|
-
const MAX_CAUSE_DEPTH = 8;
|
|
86
|
-
function toErrorInfo(err, depth = 0) {
|
|
87
|
-
if (depth > MAX_CAUSE_DEPTH) return {
|
|
88
|
-
name: "Error",
|
|
89
|
-
message: "cause chain truncated"
|
|
90
|
-
};
|
|
91
|
-
if (err instanceof Error) {
|
|
92
|
-
const info = {
|
|
93
|
-
name: err.name || "Error",
|
|
94
|
-
message: err.message
|
|
95
|
-
};
|
|
96
|
-
if (typeof err.stack === "string") info.stack = err.stack.length > MAX_STACK_LENGTH ? err.stack.slice(0, MAX_STACK_LENGTH) : err.stack;
|
|
97
|
-
if (err.cause !== void 0) info.cause = toErrorInfo(err.cause, depth + 1);
|
|
98
|
-
return info;
|
|
99
|
-
}
|
|
100
|
-
if (typeof err === "string") return {
|
|
101
|
-
name: "Error",
|
|
102
|
-
message: err
|
|
103
|
-
};
|
|
104
|
-
if (err && typeof err === "object") {
|
|
105
|
-
const record = err;
|
|
106
|
-
return {
|
|
107
|
-
name: typeof record.name === "string" ? record.name : "Error",
|
|
108
|
-
message: typeof record.message === "string" ? record.message : safeStringify(err)
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
return {
|
|
112
|
-
name: "Error",
|
|
113
|
-
message: safeStringify(err)
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
function safeStringify(value) {
|
|
117
|
-
if (value === null || value === void 0) return String(value);
|
|
118
|
-
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return String(value);
|
|
119
|
-
try {
|
|
120
|
-
return JSON.stringify(value);
|
|
121
|
-
} catch {
|
|
122
|
-
return "[unserializable]";
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
function fromErrorInfo(info) {
|
|
126
|
-
const err = new Error(info.message);
|
|
127
|
-
Object.defineProperty(err, "name", {
|
|
128
|
-
value: info.name,
|
|
129
|
-
configurable: true,
|
|
130
|
-
writable: true
|
|
131
|
-
});
|
|
132
|
-
if (info.stack !== void 0) err.stack = info.stack;
|
|
133
|
-
if (info.cause !== void 0) Object.defineProperty(err, "cause", {
|
|
134
|
-
value: fromErrorInfo(info.cause),
|
|
135
|
-
configurable: true,
|
|
136
|
-
writable: true
|
|
137
|
-
});
|
|
138
|
-
return err;
|
|
139
|
-
}
|
|
140
|
-
//#endregion
|
|
141
|
-
//#region src/rpc/paging.ts
|
|
142
|
-
function isPageableResult(value) {
|
|
143
|
-
return value !== null && typeof value === "object" && typeof value.next === "function";
|
|
144
|
-
}
|
|
145
|
-
function hasNextToken(value) {
|
|
146
|
-
return value !== null && typeof value === "object" && typeof value.nextToken === "string";
|
|
147
|
-
}
|
|
148
|
-
function dehydratePage(value, registerCursor) {
|
|
149
|
-
if (!isPageableResult(value)) return value;
|
|
150
|
-
const { next, ...rest } = value;
|
|
151
|
-
return {
|
|
152
|
-
...rest,
|
|
153
|
-
nextToken: registerCursor(next)
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
function rehydratePage(value, fetchPage) {
|
|
157
|
-
if (!hasNextToken(value)) return value;
|
|
158
|
-
const { nextToken, ...rest } = value;
|
|
159
|
-
return {
|
|
160
|
-
...rest,
|
|
161
|
-
next: () => fetchPage(nextToken)
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
//#endregion
|
|
165
|
-
//#region src/rpc/subscription.ts
|
|
166
|
-
var SubscriptionStore = class {
|
|
167
|
-
map = /* @__PURE__ */ new Map();
|
|
168
|
-
set(id, unsubscribe) {
|
|
169
|
-
this.map.set(id, unsubscribe);
|
|
170
|
-
}
|
|
171
|
-
get(id) {
|
|
172
|
-
return this.map.get(id);
|
|
173
|
-
}
|
|
174
|
-
delete(id) {
|
|
175
|
-
this.map.delete(id);
|
|
176
|
-
}
|
|
177
|
-
end(id) {
|
|
178
|
-
const unsubscribe = this.map.get(id);
|
|
179
|
-
if (unsubscribe) {
|
|
180
|
-
this.map.delete(id);
|
|
181
|
-
unsubscribe();
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
drain() {
|
|
185
|
-
for (const unsubscribe of this.map.values()) unsubscribe();
|
|
186
|
-
this.map.clear();
|
|
187
|
-
}
|
|
188
|
-
};
|
|
189
|
-
function createCorrelatedSubscriptions(router, config) {
|
|
190
|
-
let counter = 0;
|
|
191
|
-
const subs = /* @__PURE__ */ new Map();
|
|
192
|
-
router.on(config.eventKind, (message) => {
|
|
193
|
-
const sub = subs.get(message.id);
|
|
194
|
-
if (sub) config.onEvent(sub, message);
|
|
195
|
-
});
|
|
196
|
-
router.on(config.errKind, (message) => {
|
|
197
|
-
const id = message.id;
|
|
198
|
-
const sub = subs.get(id);
|
|
199
|
-
if (sub) {
|
|
200
|
-
subs.delete(id);
|
|
201
|
-
router.post(config.close(id));
|
|
202
|
-
config.onError(sub, message);
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
return { subscribe(sub, open) {
|
|
206
|
-
const id = `${config.idPrefix}${++counter}`;
|
|
207
|
-
subs.set(id, sub);
|
|
208
|
-
router.post(open(id));
|
|
209
|
-
return () => {
|
|
210
|
-
subs.delete(id);
|
|
211
|
-
router.post(config.close(id));
|
|
212
|
-
};
|
|
213
|
-
} };
|
|
214
|
-
}
|
|
215
|
-
//#endregion
|
|
216
|
-
//#region src/rpc/client-proxy.ts
|
|
217
|
-
function isAbortSignal(value) {
|
|
218
|
-
return typeof value === "object" && value !== null && typeof value.aborted === "boolean" && typeof value.addEventListener === "function" && typeof value.removeEventListener === "function";
|
|
219
|
-
}
|
|
220
|
-
function createReactorClientProxy(router, options = {}) {
|
|
221
|
-
const registry = options.registry;
|
|
222
|
-
function fetchPage(token) {
|
|
223
|
-
return router.request((id) => ({
|
|
224
|
-
k: "page",
|
|
225
|
-
id,
|
|
226
|
-
token
|
|
227
|
-
}), { transform: rehydrate });
|
|
228
|
-
}
|
|
229
|
-
function rehydrate(value) {
|
|
230
|
-
return rehydratePage(value, fetchPage);
|
|
231
|
-
}
|
|
232
|
-
const changeSubs = createCorrelatedSubscriptions(router, {
|
|
233
|
-
idPrefix: "sub",
|
|
234
|
-
eventKind: "event",
|
|
235
|
-
errKind: "sub-err",
|
|
236
|
-
onEvent: (callback, msg) => callback(msg.change),
|
|
237
|
-
onError: (_callback, msg) => console.error("Reactor subscription failed:", fromErrorInfo(msg.error)),
|
|
238
|
-
close: (id) => ({
|
|
239
|
-
k: "unsub",
|
|
240
|
-
id
|
|
241
|
-
})
|
|
242
|
-
});
|
|
243
|
-
router.on("reload", (msg) => {
|
|
244
|
-
options.onReload?.(msg.reason, msg.workerGen);
|
|
245
|
-
});
|
|
246
|
-
const call = (method, args) => {
|
|
247
|
-
let abortAt;
|
|
248
|
-
for (let i = 0; i < args.length; i++) if (isAbortSignal(args[i])) abortAt = i;
|
|
249
|
-
let wire = args;
|
|
250
|
-
if (abortAt !== void 0) {
|
|
251
|
-
wire = args.slice();
|
|
252
|
-
wire[abortAt] = void 0;
|
|
253
|
-
}
|
|
254
|
-
return router.request((id) => ({
|
|
255
|
-
k: "req",
|
|
256
|
-
id,
|
|
257
|
-
method,
|
|
258
|
-
args: wire,
|
|
259
|
-
abortAt
|
|
260
|
-
}), {
|
|
261
|
-
transform: rehydrate,
|
|
262
|
-
setup: (id) => {
|
|
263
|
-
if (abortAt === void 0) return;
|
|
264
|
-
const signal = args[abortAt];
|
|
265
|
-
const sendAbort = () => router.post({
|
|
266
|
-
k: "abort",
|
|
267
|
-
targetId: id
|
|
268
|
-
});
|
|
269
|
-
if (signal.aborted) {
|
|
270
|
-
sendAbort();
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
273
|
-
signal.addEventListener("abort", sendAbort, { once: true });
|
|
274
|
-
return () => signal.removeEventListener("abort", sendAbort);
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
};
|
|
278
|
-
const subscribe = (search, callback, view) => changeSubs.subscribe(callback, (id) => ({
|
|
279
|
-
k: "sub",
|
|
280
|
-
id,
|
|
281
|
-
search,
|
|
282
|
-
view
|
|
283
|
-
}));
|
|
284
|
-
const forwarders = /* @__PURE__ */ new Map();
|
|
285
|
-
const forwarder = (method) => {
|
|
286
|
-
let fn = forwarders.get(method);
|
|
287
|
-
if (!fn) {
|
|
288
|
-
fn = (...args) => call(method, args);
|
|
289
|
-
forwarders.set(method, fn);
|
|
290
|
-
}
|
|
291
|
-
return fn;
|
|
292
|
-
};
|
|
293
|
-
const drives = new Proxy({}, { get: (_target, prop) => typeof prop === "string" ? forwarder(`drives.${prop}`) : void 0 });
|
|
294
|
-
return new Proxy({}, { get(_target, prop) {
|
|
295
|
-
if (typeof prop !== "string" || prop === "then") return;
|
|
296
|
-
if (prop === "drives") return drives;
|
|
297
|
-
if (prop === "subscribe") return subscribe;
|
|
298
|
-
if (registry) {
|
|
299
|
-
if (prop === "getDocumentModelModule") return (documentType) => Promise.resolve().then(() => registry.getModule(documentType));
|
|
300
|
-
if (prop === "getDocumentModelModuleForDocument") return (document) => Promise.resolve().then(() => registry.getModule(document.header.documentType, normalizeDocumentModelVersion(document.state.document?.version)));
|
|
301
|
-
if (prop === "getDocumentModelModules") return (_namespace, paging) => Promise.resolve({
|
|
302
|
-
results: registry.getAllModules(),
|
|
303
|
-
options: paging ?? {
|
|
304
|
-
cursor: "",
|
|
305
|
-
limit: Number.MAX_SAFE_INTEGER
|
|
306
|
-
}
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
return forwarder(prop);
|
|
310
|
-
} });
|
|
311
|
-
}
|
|
312
|
-
//#endregion
|
|
313
89
|
//#region src/rpc/inspector-proxy.ts
|
|
90
|
+
/** Every row of `INSPECTOR_OPS`; the host decides which tiers it serves. */
|
|
314
91
|
function createInspectorProxy(router) {
|
|
315
92
|
const ops = opChannel(router, "inspector-op");
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
getCatchUpStatus: () => ops.call("catchUp.status"),
|
|
323
|
-
sweepCatchUp: () => ops.call("catchUp.sweepNow"),
|
|
324
|
-
validateDocument: (documentId, branch) => ops.call("integrity.validate", [documentId, branch]),
|
|
325
|
-
rebuildKeyframes: (documentId, branch) => ops.call("integrity.rebuildKeyframes", [documentId, branch]),
|
|
326
|
-
rebuildSnapshots: (documentId, branch) => ops.call("integrity.rebuildSnapshots", [documentId, branch]),
|
|
327
|
-
queryReactorDb: (sql, params) => ops.call("db.query", [sql, params ?? []])
|
|
328
|
-
};
|
|
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;
|
|
329
99
|
}
|
|
330
100
|
//#endregion
|
|
331
|
-
//#region src/rpc/
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
if (k === "sub") return "sub-err";
|
|
336
|
-
if (k === "sub-live") return "live-err";
|
|
337
|
-
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);
|
|
338
105
|
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
id,
|
|
347
|
-
value
|
|
348
|
-
});
|
|
349
|
-
};
|
|
350
|
-
const err = (id, error) => {
|
|
351
|
-
transport.post({
|
|
352
|
-
k: "err",
|
|
353
|
-
id,
|
|
354
|
-
error: toErrorInfo(error)
|
|
355
|
-
});
|
|
356
|
-
};
|
|
357
|
-
return {
|
|
358
|
-
ok,
|
|
359
|
-
err,
|
|
360
|
-
errForKind(message, error) {
|
|
361
|
-
if (!("id" in message)) return;
|
|
362
|
-
transport.post({
|
|
363
|
-
k: responseErrorKind(message.k),
|
|
364
|
-
id: message.id,
|
|
365
|
-
error: toErrorInfo(error)
|
|
366
|
-
});
|
|
367
|
-
},
|
|
368
|
-
async run(id, body, mapOk = defaultOk) {
|
|
369
|
-
try {
|
|
370
|
-
ok(id, mapOk(await body()));
|
|
371
|
-
} catch (error) {
|
|
372
|
-
err(id, error);
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
};
|
|
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);
|
|
376
113
|
}
|
|
377
114
|
//#endregion
|
|
378
115
|
//#region src/rpc/live-query-proxy.ts
|
|
379
116
|
function createLiveQueryProxy(router) {
|
|
380
|
-
const subs = createCorrelatedSubscriptions(router, {
|
|
117
|
+
const subs = createCorrelatedSubscriptions$1(router, {
|
|
381
118
|
idPrefix: "live",
|
|
382
119
|
eventKind: "event-live",
|
|
383
120
|
errKind: "live-err",
|
|
384
121
|
onEvent: (sub, msg) => sub.onResults(msg.results),
|
|
385
|
-
onError: (sub, msg) => sub.onError?.(fromErrorInfo(msg.error)),
|
|
122
|
+
onError: (sub, msg) => sub.onError?.(fromErrorInfo$1(msg.error)),
|
|
386
123
|
close: (id) => ({
|
|
387
124
|
k: "unsub-live",
|
|
388
125
|
id
|
|
@@ -428,7 +165,6 @@ function createRelationalPgliteProxy(router) {
|
|
|
428
165
|
}
|
|
429
166
|
//#endregion
|
|
430
167
|
//#region src/rpc/sync-manager-proxy.ts
|
|
431
|
-
const SYNC_STATUS_CHANGED_EVENT = 90001;
|
|
432
168
|
const SEED_MAX_ATTEMPTS = 3;
|
|
433
169
|
const SEED_RETRY_DELAY_MS = 500;
|
|
434
170
|
const DEFAULT_SNAPSHOT = {
|
|
@@ -484,9 +220,9 @@ function channelUrl(meta) {
|
|
|
484
220
|
var SyncManagerProxy = class {
|
|
485
221
|
ops;
|
|
486
222
|
connectionStates = /* @__PURE__ */ new Map();
|
|
487
|
-
connectionListeners = new KeyedListeners();
|
|
223
|
+
connectionListeners = new KeyedListeners$1();
|
|
488
224
|
syncStatuses = /* @__PURE__ */ new Map();
|
|
489
|
-
syncStatusListeners = new Listeners();
|
|
225
|
+
syncStatusListeners = new Listeners$1();
|
|
490
226
|
remotes = [];
|
|
491
227
|
basis;
|
|
492
228
|
seedPromise = null;
|
|
@@ -502,18 +238,22 @@ var SyncManagerProxy = class {
|
|
|
502
238
|
this.syncStatuses.set(e.documentId, e.status);
|
|
503
239
|
this.syncStatusListeners.emit(e.documentId, e.status);
|
|
504
240
|
});
|
|
241
|
+
busProxy.subscribe(SYNC_REMOTES_CHANGED_EVENT, (_type, event) => {
|
|
242
|
+
this.applyRemotes(event.remotes);
|
|
243
|
+
});
|
|
505
244
|
this.ensureSeeded();
|
|
506
245
|
}
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
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 {
|
|
510
250
|
await this.ensureSeeded();
|
|
511
251
|
return;
|
|
512
252
|
} catch (error) {
|
|
513
|
-
|
|
514
|
-
if (
|
|
253
|
+
if (!isWaiting()) failures++;
|
|
254
|
+
if (failures >= SEED_MAX_ATTEMPTS) throw error;
|
|
255
|
+
await new Promise((resolve) => setTimeout(resolve, SEED_RETRY_DELAY_MS));
|
|
515
256
|
}
|
|
516
|
-
throw lastError;
|
|
517
257
|
}
|
|
518
258
|
shutdown() {
|
|
519
259
|
return {
|
|
@@ -585,6 +325,31 @@ var SyncManagerProxy = class {
|
|
|
585
325
|
onSyncStatusChange(callback) {
|
|
586
326
|
return this.syncStatusListeners.add(callback);
|
|
587
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
|
+
}
|
|
588
353
|
callSyncOp(method, args) {
|
|
589
354
|
return this.ops.call(method, args);
|
|
590
355
|
}
|
|
@@ -620,7 +385,9 @@ var SyncManagerProxy = class {
|
|
|
620
385
|
};
|
|
621
386
|
}
|
|
622
387
|
async refreshRemotes() {
|
|
623
|
-
|
|
388
|
+
this.applyRemotes(await this.callSyncOp("list", []));
|
|
389
|
+
}
|
|
390
|
+
applyRemotes(wire) {
|
|
624
391
|
const names = /* @__PURE__ */ new Set();
|
|
625
392
|
this.remotes = wire.map((w) => {
|
|
626
393
|
const meta = rehydrateMeta(w.meta);
|
|
@@ -661,13 +428,17 @@ const FORWARDED_EVENT_TYPES = [
|
|
|
661
428
|
SyncEventTypes.CONNECTION_STATE_CHANGED,
|
|
662
429
|
ReactorEventTypes.MODEL_LOADED
|
|
663
430
|
];
|
|
664
|
-
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
|
+
];
|
|
665
436
|
//#endregion
|
|
666
437
|
//#region src/rpc/event-bus-proxy.ts
|
|
667
438
|
/** Tab-side IEventBus over bus-event; emit() is unsupported (the worker emits). */
|
|
668
439
|
var ReactorEventBusProxy = class {
|
|
669
440
|
forwardedTypes = new Set(FORWARDED_BUS_EVENT_TYPES);
|
|
670
|
-
subscribers = new KeyedListeners();
|
|
441
|
+
subscribers = new KeyedListeners$1();
|
|
671
442
|
constructor(router) {
|
|
672
443
|
router.on("bus-event", (msg) => {
|
|
673
444
|
this.subscribers.emit(msg.eventType, msg.eventType, msg.event);
|
|
@@ -688,7 +459,7 @@ function createReactorEventBusProxy(router) {
|
|
|
688
459
|
//#endregion
|
|
689
460
|
//#region src/rpc/connect-reactor.ts
|
|
690
461
|
function connectReactorClient(router, hello, onReload, registry) {
|
|
691
|
-
const client = createReactorClientProxy(router, {
|
|
462
|
+
const client = createReactorClientProxy$1(router, {
|
|
692
463
|
onReload,
|
|
693
464
|
registry
|
|
694
465
|
});
|
|
@@ -708,146 +479,13 @@ function postReactorIdentity(router, user) {
|
|
|
708
479
|
});
|
|
709
480
|
}
|
|
710
481
|
//#endregion
|
|
711
|
-
//#region src/rpc/host-server.ts
|
|
712
|
-
const MAX_PENDING_PAGES = 1e3;
|
|
713
|
-
var ReactorHostServer = class {
|
|
714
|
-
client;
|
|
715
|
-
transport;
|
|
716
|
-
reply;
|
|
717
|
-
aborters = /* @__PURE__ */ new Map();
|
|
718
|
-
subscriptions = new SubscriptionStore();
|
|
719
|
-
pages = /* @__PURE__ */ new Map();
|
|
720
|
-
pageCounter = 0;
|
|
721
|
-
detach = () => {};
|
|
722
|
-
constructor(client, transport) {
|
|
723
|
-
this.client = client;
|
|
724
|
-
this.transport = transport;
|
|
725
|
-
this.reply = hostResponder(transport);
|
|
726
|
-
}
|
|
727
|
-
start() {
|
|
728
|
-
this.detach = this.transport.onMessage((message) => {
|
|
729
|
-
this.handleMessage(message);
|
|
730
|
-
});
|
|
731
|
-
}
|
|
732
|
-
stop() {
|
|
733
|
-
this.detach();
|
|
734
|
-
this.subscriptions.drain();
|
|
735
|
-
this.aborters.clear();
|
|
736
|
-
this.pages.clear();
|
|
737
|
-
}
|
|
738
|
-
async handleMessage(message) {
|
|
739
|
-
try {
|
|
740
|
-
switch (message.k) {
|
|
741
|
-
case "req":
|
|
742
|
-
await this.handleRequest(message);
|
|
743
|
-
return;
|
|
744
|
-
case "page":
|
|
745
|
-
await this.handlePage(message);
|
|
746
|
-
return;
|
|
747
|
-
case "abort":
|
|
748
|
-
this.aborters.get(message.targetId)?.abort();
|
|
749
|
-
return;
|
|
750
|
-
case "sub":
|
|
751
|
-
this.handleSubscribe(message);
|
|
752
|
-
return;
|
|
753
|
-
case "unsub":
|
|
754
|
-
this.subscriptions.end(message.id);
|
|
755
|
-
return;
|
|
756
|
-
}
|
|
757
|
-
} catch (error) {
|
|
758
|
-
this.reply.errForKind(message, error);
|
|
759
|
-
}
|
|
760
|
-
}
|
|
761
|
-
async handleRequest(message) {
|
|
762
|
-
let args = message.args;
|
|
763
|
-
let controller;
|
|
764
|
-
if (message.abortAt !== void 0) {
|
|
765
|
-
controller = new AbortController();
|
|
766
|
-
this.aborters.set(message.id, controller);
|
|
767
|
-
args = message.args.slice();
|
|
768
|
-
args[message.abortAt] = controller.signal;
|
|
769
|
-
}
|
|
770
|
-
try {
|
|
771
|
-
const value = await this.resolveMethod(message.method)(...args);
|
|
772
|
-
this.reply.ok(message.id, this.prepareResult(value));
|
|
773
|
-
} catch (error) {
|
|
774
|
-
this.reply.err(message.id, error);
|
|
775
|
-
} finally {
|
|
776
|
-
if (controller) this.aborters.delete(message.id);
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
async handlePage(message) {
|
|
780
|
-
const next = this.pages.get(message.token);
|
|
781
|
-
this.pages.delete(message.token);
|
|
782
|
-
if (!next) {
|
|
783
|
-
this.reply.err(message.id, /* @__PURE__ */ new Error("Paged cursor already consumed or expired"));
|
|
784
|
-
return;
|
|
785
|
-
}
|
|
786
|
-
try {
|
|
787
|
-
const value = await next();
|
|
788
|
-
this.reply.ok(message.id, this.prepareResult(value));
|
|
789
|
-
} catch (error) {
|
|
790
|
-
this.reply.err(message.id, error);
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
handleSubscribe(message) {
|
|
794
|
-
const unsubscribe = this.client.subscribe(message.search, (change) => this.transport.post({
|
|
795
|
-
k: "event",
|
|
796
|
-
id: message.id,
|
|
797
|
-
change
|
|
798
|
-
}), message.view);
|
|
799
|
-
this.subscriptions.set(message.id, unsubscribe);
|
|
800
|
-
}
|
|
801
|
-
prepareResult(value) {
|
|
802
|
-
return dehydratePage(value, (next) => {
|
|
803
|
-
const token = `p${++this.pageCounter}`;
|
|
804
|
-
this.pages.set(token, next);
|
|
805
|
-
if (this.pages.size > MAX_PENDING_PAGES) {
|
|
806
|
-
const oldest = this.pages.keys().next().value;
|
|
807
|
-
if (oldest !== void 0) this.pages.delete(oldest);
|
|
808
|
-
}
|
|
809
|
-
return token;
|
|
810
|
-
});
|
|
811
|
-
}
|
|
812
|
-
resolveMethod(path) {
|
|
813
|
-
if (path.startsWith("drives.")) {
|
|
814
|
-
const name = path.slice(7);
|
|
815
|
-
const fn = this.client.drives[name];
|
|
816
|
-
if (typeof fn !== "function") throw new Error(`Unknown drive method: ${name}`);
|
|
817
|
-
return fn.bind(this.client.drives);
|
|
818
|
-
}
|
|
819
|
-
const fn = this.client[path];
|
|
820
|
-
if (typeof fn !== "function") throw new Error(`Unknown reactor method: ${path}`);
|
|
821
|
-
return fn.bind(this.client);
|
|
822
|
-
}
|
|
823
|
-
};
|
|
824
|
-
//#endregion
|
|
825
|
-
//#region src/rpc/transport.ts
|
|
826
|
-
function createPortTransport(port) {
|
|
827
|
-
port.start();
|
|
828
|
-
port.addEventListener("messageerror", (event) => {
|
|
829
|
-
console.error("[rpc transport] failed to deserialize message", event);
|
|
830
|
-
});
|
|
831
|
-
return {
|
|
832
|
-
post(message) {
|
|
833
|
-
port.postMessage(message);
|
|
834
|
-
},
|
|
835
|
-
onMessage(listener) {
|
|
836
|
-
const handler = (event) => {
|
|
837
|
-
listener(event.data);
|
|
838
|
-
};
|
|
839
|
-
port.addEventListener("message", handler);
|
|
840
|
-
return () => port.removeEventListener("message", handler);
|
|
841
|
-
},
|
|
842
|
-
close() {
|
|
843
|
-
port.close();
|
|
844
|
-
}
|
|
845
|
-
};
|
|
846
|
-
}
|
|
847
|
-
//#endregion
|
|
848
482
|
//#region src/rpc/reactor-host.ts
|
|
849
483
|
function isDataMessage(msg) {
|
|
850
|
-
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();
|
|
851
489
|
}
|
|
852
490
|
function versionsCompatible(a, b) {
|
|
853
491
|
return a.appBuildId === b.appBuildId && a.rpcProtocolVersion === b.rpcProtocolVersion && (a.featureFlags ?? "") === (b.featureFlags ?? "");
|
|
@@ -857,16 +495,26 @@ function workerGenForVersion(version) {
|
|
|
857
495
|
const suffix = flags === "" ? "" : `-${hashFlags(flags)}`;
|
|
858
496
|
return `v${version.rpcProtocolVersion}-${version.appBuildId}${suffix}`;
|
|
859
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
|
+
}
|
|
860
504
|
/** Names what differs, so a reload is diagnosable from the tab's console. */
|
|
861
505
|
function mismatchReason(baseline, incoming) {
|
|
862
|
-
if ((baseline.featureFlags ?? "") !== (incoming.featureFlags ?? "")) return
|
|
863
|
-
return
|
|
506
|
+
if ((baseline.featureFlags ?? "") !== (incoming.featureFlags ?? "")) return `${FLAGS_CHANGED} (worker: ${baseline.featureFlags || "none"}, tab: ${incoming.featureFlags || "none"})`;
|
|
507
|
+
return VERSION_MISMATCH;
|
|
864
508
|
}
|
|
865
509
|
function hashFlags(flags) {
|
|
866
510
|
let hash = 0;
|
|
867
511
|
for (let i = 0; i < flags.length; i++) hash = hash * 31 + flags.charCodeAt(i) | 0;
|
|
868
512
|
return (hash >>> 0).toString(36);
|
|
869
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
|
+
}
|
|
870
518
|
var ReactorHost = class {
|
|
871
519
|
options;
|
|
872
520
|
disposers = /* @__PURE__ */ new Set();
|
|
@@ -877,6 +525,8 @@ var ReactorHost = class {
|
|
|
877
525
|
ownerId;
|
|
878
526
|
bootedAtMs;
|
|
879
527
|
migrationState = null;
|
|
528
|
+
retirement = null;
|
|
529
|
+
reloadSent = false;
|
|
880
530
|
constructor(options) {
|
|
881
531
|
this.options = options;
|
|
882
532
|
this.ownerId = options.ownerId ?? crypto.randomUUID();
|
|
@@ -887,11 +537,12 @@ var ReactorHost = class {
|
|
|
887
537
|
let server = null;
|
|
888
538
|
let ready = false;
|
|
889
539
|
const buffer = [];
|
|
890
|
-
const liveSubs = new SubscriptionStore();
|
|
891
|
-
const reply = hostResponder(transport);
|
|
540
|
+
const liveSubs = new SubscriptionStore$1();
|
|
541
|
+
const reply = hostResponder$1(transport);
|
|
892
542
|
const ensureServer = async (construct) => {
|
|
893
543
|
if (server) return;
|
|
894
|
-
|
|
544
|
+
const client = await this.resolveClient(construct);
|
|
545
|
+
server = new ReactorHostServer$1(client, transport);
|
|
895
546
|
};
|
|
896
547
|
const drainBuffer = async () => {
|
|
897
548
|
while (buffer.length > 0) {
|
|
@@ -912,9 +563,15 @@ var ReactorHost = class {
|
|
|
912
563
|
return;
|
|
913
564
|
}
|
|
914
565
|
if (this.migrationState?.status === "migrating" && isDataMessage(msg)) {
|
|
566
|
+
closeTransferredPort(msg);
|
|
915
567
|
reply.errForKind(msg, /* @__PURE__ */ new Error("migration in progress"));
|
|
916
568
|
return;
|
|
917
569
|
}
|
|
570
|
+
if (this.retirement && isDataMessage(msg)) {
|
|
571
|
+
closeTransferredPort(msg);
|
|
572
|
+
reply.errForKind(msg, retiredError());
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
918
575
|
if (msg.k === "hello") {
|
|
919
576
|
this.handleHello(msg, transport, reply, ensureServer, drainBuffer);
|
|
920
577
|
return;
|
|
@@ -932,7 +589,7 @@ var ReactorHost = class {
|
|
|
932
589
|
return;
|
|
933
590
|
}
|
|
934
591
|
if (msg.k === "sync-op") {
|
|
935
|
-
this.
|
|
592
|
+
this.handleSyncOp(msg, reply);
|
|
936
593
|
return;
|
|
937
594
|
}
|
|
938
595
|
if (msg.k === "db-op") {
|
|
@@ -943,6 +600,14 @@ var ReactorHost = class {
|
|
|
943
600
|
this.handleOp(msg, this.options.onInspectorOp, "inspector", reply);
|
|
944
601
|
return;
|
|
945
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
|
+
}
|
|
946
611
|
if (msg.k === "sub-live") {
|
|
947
612
|
this.handleLiveSubscribe(msg, transport, reply, liveSubs);
|
|
948
613
|
return;
|
|
@@ -952,7 +617,7 @@ var ReactorHost = class {
|
|
|
952
617
|
return;
|
|
953
618
|
}
|
|
954
619
|
if (msg.k === "admin") {
|
|
955
|
-
this.handleAdmin(msg, reply);
|
|
620
|
+
this.handleAdmin(msg, reply, transport);
|
|
956
621
|
return;
|
|
957
622
|
}
|
|
958
623
|
if (ready && server) server.handleMessage(msg);
|
|
@@ -963,6 +628,10 @@ var ReactorHost = class {
|
|
|
963
628
|
k: "migration",
|
|
964
629
|
state: this.migrationState
|
|
965
630
|
});
|
|
631
|
+
if (this.retirement && this.reloadSent) transport.post({
|
|
632
|
+
k: "reload",
|
|
633
|
+
...this.retirement
|
|
634
|
+
});
|
|
966
635
|
if (this.options.client) ensureServer().then(drainBuffer).catch((error) => {
|
|
967
636
|
console.error("ReactorHost buffer drain failed", error);
|
|
968
637
|
});
|
|
@@ -977,7 +646,7 @@ var ReactorHost = class {
|
|
|
977
646
|
return dispose;
|
|
978
647
|
}
|
|
979
648
|
connectPort(port) {
|
|
980
|
-
return this.connect(createPortTransport(port));
|
|
649
|
+
return this.connect(createPortTransport$1(port));
|
|
981
650
|
}
|
|
982
651
|
broadcastBusEvent(eventType, event) {
|
|
983
652
|
for (const transport of this.clients) transport.post({
|
|
@@ -993,6 +662,37 @@ var ReactorHost = class {
|
|
|
993
662
|
workerGen
|
|
994
663
|
});
|
|
995
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
|
+
}
|
|
996
696
|
setMigrationState(state) {
|
|
997
697
|
this.migrationState = state;
|
|
998
698
|
for (const transport of this.clients) transport.post({
|
|
@@ -1000,10 +700,22 @@ var ReactorHost = class {
|
|
|
1000
700
|
state
|
|
1001
701
|
});
|
|
1002
702
|
}
|
|
703
|
+
get retired() {
|
|
704
|
+
return this.retirement !== null;
|
|
705
|
+
}
|
|
1003
706
|
get connectionCount() {
|
|
1004
707
|
return this.disposers.size;
|
|
1005
708
|
}
|
|
1006
|
-
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
|
+
}
|
|
1007
719
|
if (message.method === "restart") {
|
|
1008
720
|
this.options.onAdminRestart?.();
|
|
1009
721
|
reply.ok(message.id);
|
|
@@ -1023,7 +735,7 @@ var ReactorHost = class {
|
|
|
1023
735
|
bootedAtMs: this.bootedAtMs,
|
|
1024
736
|
connectedClients: this.connectionCount,
|
|
1025
737
|
appBuildId: this.baseline?.appBuildId ?? this.options.appBuildId ?? "unknown",
|
|
1026
|
-
rpcProtocolVersion: this.baseline?.rpcProtocolVersion ??
|
|
738
|
+
rpcProtocolVersion: this.baseline?.rpcProtocolVersion ?? RPC_PROTOCOL_VERSION$1,
|
|
1027
739
|
featureFlags: this.baseline?.featureFlags ?? ""
|
|
1028
740
|
};
|
|
1029
741
|
reply.ok(message.id, info);
|
|
@@ -1034,12 +746,16 @@ var ReactorHost = class {
|
|
|
1034
746
|
});
|
|
1035
747
|
}
|
|
1036
748
|
resolveClient(construct) {
|
|
749
|
+
if (this.retirement) return Promise.reject(retiredError());
|
|
1037
750
|
if (!this.clientPromise) {
|
|
1038
751
|
const build = this.options.build;
|
|
1039
752
|
if (!build) return Promise.reject(/* @__PURE__ */ new Error("ReactorHost has no client or builder"));
|
|
1040
753
|
const pending = build(construct);
|
|
1041
754
|
this.clientPromise = pending;
|
|
1042
755
|
this.buildFailure = null;
|
|
756
|
+
pending.then(() => {
|
|
757
|
+
if (this.retirement && this.reloadSent) this.stopRetired(this.retirement.reason);
|
|
758
|
+
}, () => void 0);
|
|
1043
759
|
pending.catch((error) => {
|
|
1044
760
|
if (this.clientPromise === pending) {
|
|
1045
761
|
this.clientPromise = null;
|
|
@@ -1064,11 +780,7 @@ var ReactorHost = class {
|
|
|
1064
780
|
async handleHello(message, transport, reply, ensureServer, drainBuffer) {
|
|
1065
781
|
if (this.baseline) {
|
|
1066
782
|
if (!versionsCompatible(this.baseline, message.version)) {
|
|
1067
|
-
|
|
1068
|
-
k: "reload",
|
|
1069
|
-
reason: mismatchReason(this.baseline, message.version),
|
|
1070
|
-
workerGen: workerGenForVersion(message.version)
|
|
1071
|
-
});
|
|
783
|
+
this.retireAndReload(mismatchReason(this.baseline, message.version), `${workerGenForVersion(message.version)}-${this.ownerId.slice(0, 8)}`);
|
|
1072
784
|
reply.ok(message.id, { ok: false });
|
|
1073
785
|
return;
|
|
1074
786
|
}
|
|
@@ -1081,7 +793,7 @@ var ReactorHost = class {
|
|
|
1081
793
|
}
|
|
1082
794
|
async handleRegister(message, reply) {
|
|
1083
795
|
await reply.run(message.id, async () => {
|
|
1084
|
-
await this.options.registerPackages?.(message.specs);
|
|
796
|
+
await this.options.registerPackages?.(message.specs, message.sources);
|
|
1085
797
|
});
|
|
1086
798
|
}
|
|
1087
799
|
async handleUnregister(message, reply) {
|
|
@@ -1096,6 +808,65 @@ var ReactorHost = class {
|
|
|
1096
808
|
return handler(message.method, message.args);
|
|
1097
809
|
}, (value) => value);
|
|
1098
810
|
}
|
|
811
|
+
/** An `add` or `remove` changes the remotes every tab lists. */
|
|
812
|
+
async handleSyncOp(message, reply) {
|
|
813
|
+
const handler = this.options.onSyncOp;
|
|
814
|
+
if (!this.requireHandler(handler, message, reply, "sync")) return;
|
|
815
|
+
const changesRemotes = message.method === "add" || message.method === "remove";
|
|
816
|
+
await reply.run(message.id, async () => {
|
|
817
|
+
await this.awaitClientReady();
|
|
818
|
+
const value = await handler(message.method, message.args);
|
|
819
|
+
if (changesRemotes) await this.broadcastRemotes();
|
|
820
|
+
return value;
|
|
821
|
+
}, (value) => value);
|
|
822
|
+
}
|
|
823
|
+
/** Closes the moved port on every failure; the sender can no longer close it. */
|
|
824
|
+
async handleAdoptSyncPeer(message, reply) {
|
|
825
|
+
const handler = this.options.onAdoptSyncPeer;
|
|
826
|
+
if (!handler) {
|
|
827
|
+
closeTransferredPort(message);
|
|
828
|
+
reply.errForKind(message, /* @__PURE__ */ new Error("ReactorHost has no adopt-sync-peer handler"));
|
|
829
|
+
return;
|
|
830
|
+
}
|
|
831
|
+
await reply.run(message.id, async () => {
|
|
832
|
+
try {
|
|
833
|
+
await this.awaitClientReady();
|
|
834
|
+
await handler({
|
|
835
|
+
peerId: message.peerId,
|
|
836
|
+
channelName: message.channelName,
|
|
837
|
+
collectionIdKey: message.collectionIdKey,
|
|
838
|
+
remoteName: message.remoteName,
|
|
839
|
+
filter: message.filter
|
|
840
|
+
}, message.port);
|
|
841
|
+
} catch (error) {
|
|
842
|
+
closeTransferredPort(message);
|
|
843
|
+
throw error;
|
|
844
|
+
}
|
|
845
|
+
await this.broadcastRemotes();
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
async handleRemoveSyncPeer(message, reply) {
|
|
849
|
+
const handler = this.options.onRemoveSyncPeer;
|
|
850
|
+
if (!this.requireHandler(handler, message, reply, "remove-sync-peer")) return;
|
|
851
|
+
await reply.run(message.id, async () => {
|
|
852
|
+
await this.awaitClientReady();
|
|
853
|
+
await handler({
|
|
854
|
+
peerId: message.peerId,
|
|
855
|
+
channelName: message.channelName,
|
|
856
|
+
remoteName: message.remoteName
|
|
857
|
+
});
|
|
858
|
+
await this.broadcastRemotes();
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
async broadcastRemotes() {
|
|
862
|
+
const list = this.options.onSyncOp;
|
|
863
|
+
if (!list) return;
|
|
864
|
+
try {
|
|
865
|
+
this.broadcastBusEvent(SYNC_REMOTES_CHANGED_EVENT, { remotes: await list("list", []) });
|
|
866
|
+
} catch (error) {
|
|
867
|
+
console.error("ReactorHost could not list remotes for its tabs", error);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
1099
870
|
async handleLiveSubscribe(message, transport, reply, liveSubs) {
|
|
1100
871
|
const handler = this.options.onLiveQuery;
|
|
1101
872
|
if (!this.requireHandler(handler, message, reply, "live-query")) return;
|
|
@@ -1122,10 +893,179 @@ var ReactorHost = class {
|
|
|
1122
893
|
}
|
|
1123
894
|
};
|
|
1124
895
|
//#endregion
|
|
896
|
+
//#region src/rpc/module-graph.ts
|
|
897
|
+
/** A bare specifier's URL in the shared table: an exact entry, else the longest trailing-slash prefix, as an import map does. */
|
|
898
|
+
function resolveSharedSpecifier(specifier, imports) {
|
|
899
|
+
if (Object.hasOwn(imports, specifier)) return imports[specifier];
|
|
900
|
+
let best;
|
|
901
|
+
for (const key of Object.keys(imports)) if (key.endsWith("/") && specifier.startsWith(key) && (best === void 0 || key.length > best.length)) best = key;
|
|
902
|
+
return best === void 0 ? void 0 : imports[best] + specifier.slice(best.length);
|
|
903
|
+
}
|
|
904
|
+
function isRelative(specifier) {
|
|
905
|
+
return specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/");
|
|
906
|
+
}
|
|
907
|
+
function isUrl(specifier) {
|
|
908
|
+
return /^[a-z][a-z0-9+.-]*:/i.test(specifier);
|
|
909
|
+
}
|
|
910
|
+
async function fetchModuleSource(url) {
|
|
911
|
+
const response = await fetch(url);
|
|
912
|
+
if (!response.ok) throw new Error(`${response.status} ${response.statusText} for ${url}`);
|
|
913
|
+
return response.text();
|
|
914
|
+
}
|
|
915
|
+
function blobModuleUrl(source) {
|
|
916
|
+
return URL.createObjectURL(new Blob([source], { type: "text/javascript" }));
|
|
917
|
+
}
|
|
918
|
+
var ModuleGraph = class {
|
|
919
|
+
sharedImports;
|
|
920
|
+
fetchSource;
|
|
921
|
+
createModuleUrl;
|
|
922
|
+
warn;
|
|
923
|
+
parsed = /* @__PURE__ */ new Map();
|
|
924
|
+
built = /* @__PURE__ */ new Map();
|
|
925
|
+
constructor(options = {}) {
|
|
926
|
+
this.sharedImports = options.sharedImports ?? {};
|
|
927
|
+
this.fetchSource = options.fetchSource ?? fetchModuleSource;
|
|
928
|
+
this.createModuleUrl = options.createModuleUrl ?? blobModuleUrl;
|
|
929
|
+
this.warn = options.warn ?? ((message) => console.warn(message));
|
|
930
|
+
}
|
|
931
|
+
/** The URL to import in place of `entryUrl`; `entryUrl` itself when nothing needed a rewrite. */
|
|
932
|
+
async resolve(entryUrl) {
|
|
933
|
+
const modules = await this.collect(entryUrl);
|
|
934
|
+
this.assertMapped(entryUrl, modules);
|
|
935
|
+
const rewrite = this.needsRewrite(modules);
|
|
936
|
+
this.assertAcyclic(entryUrl, modules, rewrite);
|
|
937
|
+
return this.build(entryUrl, modules, rewrite);
|
|
938
|
+
}
|
|
939
|
+
async collect(entryUrl) {
|
|
940
|
+
const modules = /* @__PURE__ */ new Map();
|
|
941
|
+
const seen = /* @__PURE__ */ new Set();
|
|
942
|
+
const visit = async (url) => {
|
|
943
|
+
if (seen.has(url)) return;
|
|
944
|
+
seen.add(url);
|
|
945
|
+
const module = await this.parse(url);
|
|
946
|
+
modules.set(url, module);
|
|
947
|
+
await Promise.all(module.deps.map(visit));
|
|
948
|
+
};
|
|
949
|
+
await visit(entryUrl);
|
|
950
|
+
return modules;
|
|
951
|
+
}
|
|
952
|
+
parse(url) {
|
|
953
|
+
let pending = this.parsed.get(url);
|
|
954
|
+
if (!pending) {
|
|
955
|
+
pending = this.readModule(url);
|
|
956
|
+
pending.catch(() => this.parsed.delete(url));
|
|
957
|
+
this.parsed.set(url, pending);
|
|
958
|
+
}
|
|
959
|
+
return pending;
|
|
960
|
+
}
|
|
961
|
+
async readModule(url) {
|
|
962
|
+
const source = await this.fetchSource(url);
|
|
963
|
+
await init;
|
|
964
|
+
const [imports] = parse(source, url);
|
|
965
|
+
const deps = [];
|
|
966
|
+
const bare = [];
|
|
967
|
+
for (const specifier of imports) {
|
|
968
|
+
if (specifier.d === -2) continue;
|
|
969
|
+
const name = specifier.n;
|
|
970
|
+
if (name === void 0) {
|
|
971
|
+
this.warn(`[module-graph] ${url} has a dynamic import with a computed specifier; it is left as is`);
|
|
972
|
+
continue;
|
|
973
|
+
}
|
|
974
|
+
if (isRelative(name)) deps.push(new URL(name, url).href);
|
|
975
|
+
else if (!isUrl(name)) bare.push(name);
|
|
976
|
+
}
|
|
977
|
+
return {
|
|
978
|
+
url,
|
|
979
|
+
source,
|
|
980
|
+
imports,
|
|
981
|
+
deps: [...new Set(deps)],
|
|
982
|
+
bare
|
|
983
|
+
};
|
|
984
|
+
}
|
|
985
|
+
assertAcyclic(entryUrl, modules, rewrite) {
|
|
986
|
+
const state = /* @__PURE__ */ new Map();
|
|
987
|
+
const stack = [];
|
|
988
|
+
const visit = (url) => {
|
|
989
|
+
const seen = state.get(url);
|
|
990
|
+
if (seen === "done") return;
|
|
991
|
+
if (seen === "visiting") {
|
|
992
|
+
const cycle = [...stack.slice(stack.indexOf(url)), url];
|
|
993
|
+
throw new Error(`Can't load ${entryUrl} in the worker: its modules import each other (${cycle.join(" -> ")})`);
|
|
994
|
+
}
|
|
995
|
+
state.set(url, "visiting");
|
|
996
|
+
stack.push(url);
|
|
997
|
+
for (const dep of modules.get(url).deps) if (rewrite.has(dep)) visit(dep);
|
|
998
|
+
stack.pop();
|
|
999
|
+
state.set(url, "done");
|
|
1000
|
+
};
|
|
1001
|
+
for (const url of [entryUrl, ...rewrite]) if (rewrite.has(url)) visit(url);
|
|
1002
|
+
}
|
|
1003
|
+
assertMapped(entryUrl, modules) {
|
|
1004
|
+
for (const module of modules.values()) for (const name of module.bare) if (resolveSharedSpecifier(name, this.sharedImports) === void 0) throw new Error(`Can't load ${entryUrl} in the worker: ${module.url} imports "${name}", which the host doesn't share with the worker`);
|
|
1005
|
+
}
|
|
1006
|
+
needsRewrite(modules) {
|
|
1007
|
+
const importers = /* @__PURE__ */ new Map();
|
|
1008
|
+
for (const module of modules.values()) for (const dep of module.deps) importers.set(dep, [...importers.get(dep) ?? [], module.url]);
|
|
1009
|
+
const rewrite = /* @__PURE__ */ new Set();
|
|
1010
|
+
const queue = [...modules.values()].filter((module) => module.bare.length > 0).map((module) => module.url);
|
|
1011
|
+
while (queue.length > 0) {
|
|
1012
|
+
const url = queue.pop();
|
|
1013
|
+
if (rewrite.has(url)) continue;
|
|
1014
|
+
rewrite.add(url);
|
|
1015
|
+
queue.push(...importers.get(url) ?? []);
|
|
1016
|
+
}
|
|
1017
|
+
return rewrite;
|
|
1018
|
+
}
|
|
1019
|
+
build(url, modules, rewrite) {
|
|
1020
|
+
if (!rewrite.has(url)) return Promise.resolve(url);
|
|
1021
|
+
let pending = this.built.get(url);
|
|
1022
|
+
if (!pending) {
|
|
1023
|
+
pending = this.rewrite(modules.get(url), modules, rewrite);
|
|
1024
|
+
pending.catch(() => this.built.delete(url));
|
|
1025
|
+
this.built.set(url, pending);
|
|
1026
|
+
}
|
|
1027
|
+
return pending;
|
|
1028
|
+
}
|
|
1029
|
+
async rewrite(module, modules, rewrite) {
|
|
1030
|
+
const depUrls = /* @__PURE__ */ new Map();
|
|
1031
|
+
await Promise.all(module.deps.map(async (dep) => {
|
|
1032
|
+
depUrls.set(dep, await this.build(dep, modules, rewrite));
|
|
1033
|
+
}));
|
|
1034
|
+
let out = "";
|
|
1035
|
+
let last = 0;
|
|
1036
|
+
for (const specifier of module.imports) {
|
|
1037
|
+
const name = specifier.n;
|
|
1038
|
+
if (specifier.d === -2 || name === void 0 || isUrl(name)) continue;
|
|
1039
|
+
const target = isRelative(name) ? depUrls.get(new URL(name, module.url).href) : resolveSharedSpecifier(name, this.sharedImports);
|
|
1040
|
+
const replacement = specifier.d > -1 ? JSON.stringify(target) : target;
|
|
1041
|
+
out += module.source.slice(last, specifier.s) + replacement;
|
|
1042
|
+
last = specifier.e;
|
|
1043
|
+
}
|
|
1044
|
+
return this.createModuleUrl(out + module.source.slice(last));
|
|
1045
|
+
}
|
|
1046
|
+
};
|
|
1047
|
+
//#endregion
|
|
1125
1048
|
//#region src/rpc/worker-package-loader.ts
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1049
|
+
/** One line for a failed load: the package, its URL and why. */
|
|
1050
|
+
function describeLoadFailure(failure) {
|
|
1051
|
+
const reason = failure.error instanceof Error ? failure.error.message : String(failure.error);
|
|
1052
|
+
return `${failure.name} (${failure.url}): ${reason}`;
|
|
1053
|
+
}
|
|
1054
|
+
function summarize(results) {
|
|
1055
|
+
const types = /* @__PURE__ */ new Set();
|
|
1056
|
+
const failures = [];
|
|
1057
|
+
for (const result of results) if (result.status === "replaced") for (const type of result.types) types.add(type);
|
|
1058
|
+
else if (result.status === "failed") failures.push(result.failure);
|
|
1059
|
+
return {
|
|
1060
|
+
types: [...types],
|
|
1061
|
+
failures
|
|
1062
|
+
};
|
|
1063
|
+
}
|
|
1064
|
+
const SOURCE_KEY_PREFIX = "src:";
|
|
1065
|
+
function packageName(key) {
|
|
1066
|
+
if (key.startsWith(SOURCE_KEY_PREFIX)) return key.slice(4);
|
|
1067
|
+
const at = key.lastIndexOf("@");
|
|
1068
|
+
return at > 0 ? key.slice(0, at) : key;
|
|
1129
1069
|
}
|
|
1130
1070
|
function moduleKey(module) {
|
|
1131
1071
|
return `${module.documentModel.global.id}@${module.version ?? 1}`;
|
|
@@ -1135,20 +1075,47 @@ function isDocumentModelModule(value) {
|
|
|
1135
1075
|
const candidate = value;
|
|
1136
1076
|
return typeof candidate.reducer === "function" && typeof candidate.documentModel?.global?.id === "string";
|
|
1137
1077
|
}
|
|
1078
|
+
function isUpgradeManifest(value) {
|
|
1079
|
+
if (typeof value !== "object" || value === null) return false;
|
|
1080
|
+
const candidate = value;
|
|
1081
|
+
return typeof candidate.documentType === "string" && typeof candidate.upgrades === "object" && candidate.upgrades !== null;
|
|
1082
|
+
}
|
|
1083
|
+
function manifestsOf(namespace) {
|
|
1084
|
+
const out = [];
|
|
1085
|
+
for (const value of Object.values(namespace)) {
|
|
1086
|
+
if (isUpgradeManifest(value)) {
|
|
1087
|
+
out.push(value);
|
|
1088
|
+
continue;
|
|
1089
|
+
}
|
|
1090
|
+
if (Array.isArray(value)) {
|
|
1091
|
+
for (const inner of value) if (isUpgradeManifest(inner)) out.push(inner);
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
return out;
|
|
1095
|
+
}
|
|
1138
1096
|
var WorkerPackageLoader = class {
|
|
1139
1097
|
cdnUrl;
|
|
1140
1098
|
importPackage;
|
|
1141
1099
|
resolvePackages;
|
|
1142
|
-
|
|
1143
|
-
importSource;
|
|
1100
|
+
moduleGraph;
|
|
1144
1101
|
modulesByKey = /* @__PURE__ */ new Map();
|
|
1145
|
-
|
|
1102
|
+
manifestsByType = /* @__PURE__ */ new Map();
|
|
1103
|
+
keysBySource = /* @__PURE__ */ new Map();
|
|
1104
|
+
manifestTypesBySource = /* @__PURE__ */ new Map();
|
|
1105
|
+
loadedUrls = /* @__PURE__ */ new Map();
|
|
1146
1106
|
failures = [];
|
|
1107
|
+
pendingLoads = /* @__PURE__ */ new Map();
|
|
1108
|
+
generationBySource = /* @__PURE__ */ new Map();
|
|
1109
|
+
generation = 0;
|
|
1147
1110
|
constructor(options) {
|
|
1148
1111
|
this.cdnUrl = options.cdnUrl.replace(/\/$/, "");
|
|
1149
1112
|
this.importPackage = options.importPackage;
|
|
1150
|
-
this.
|
|
1151
|
-
|
|
1113
|
+
this.moduleGraph = new ModuleGraph({
|
|
1114
|
+
sharedImports: options.sharedImports,
|
|
1115
|
+
fetchSource: options.fetchSource,
|
|
1116
|
+
createModuleUrl: options.createModuleUrl,
|
|
1117
|
+
warn: options.warn
|
|
1118
|
+
});
|
|
1152
1119
|
const registryClient = new RegistryClient(options.cdnUrl);
|
|
1153
1120
|
this.resolvePackages = options.resolvePackages ?? ((documentType) => registryClient.getPackagesByDocumentType(documentType));
|
|
1154
1121
|
}
|
|
@@ -1156,6 +1123,25 @@ var WorkerPackageLoader = class {
|
|
|
1156
1123
|
await Promise.all([...new Set(specs)].map((spec) => this.loadPackage(spec)));
|
|
1157
1124
|
return this.models;
|
|
1158
1125
|
}
|
|
1126
|
+
/** Loads registry packages added or updated after boot; returns the types whose modules changed, and the failures. */
|
|
1127
|
+
async updatePackages(specs) {
|
|
1128
|
+
return summarize(await Promise.all([...new Set(specs)].map((spec) => this.loadPackage(spec))));
|
|
1129
|
+
}
|
|
1130
|
+
/** Loads URL-addressed packages (local project packages). Idempotent. */
|
|
1131
|
+
async loadSources(sources) {
|
|
1132
|
+
await Promise.all(sources.map((source) => this.loadFromUrl(sourceKey(source), source.url)));
|
|
1133
|
+
}
|
|
1134
|
+
/**
|
|
1135
|
+
* Replaces previously loaded sources with freshly imported ones (a vetra
|
|
1136
|
+
* watch rebuild re-sends the same source under a cache-busted URL). Returns
|
|
1137
|
+
* every document type touched - removed, re-added, or new - so the caller
|
|
1138
|
+
* can replace the registry's version families. A source whose import fails
|
|
1139
|
+
* keeps its previous modules and is reported in `failures`; one superseded
|
|
1140
|
+
* by a newer reload of the same source contributes nothing.
|
|
1141
|
+
*/
|
|
1142
|
+
async reloadSources(sources) {
|
|
1143
|
+
return summarize(await Promise.all(sources.map((source) => this.replaceSource(sourceKey(source), source.url))));
|
|
1144
|
+
}
|
|
1159
1145
|
async load(documentType) {
|
|
1160
1146
|
const existing = this.latestForType(documentType);
|
|
1161
1147
|
if (existing) return existing;
|
|
@@ -1169,6 +1155,9 @@ var WorkerPackageLoader = class {
|
|
|
1169
1155
|
get models() {
|
|
1170
1156
|
return [...new Set(this.modulesByKey.values())];
|
|
1171
1157
|
}
|
|
1158
|
+
get upgradeManifests() {
|
|
1159
|
+
return [...this.manifestsByType.values()];
|
|
1160
|
+
}
|
|
1172
1161
|
get loadFailures() {
|
|
1173
1162
|
return [...this.failures];
|
|
1174
1163
|
}
|
|
@@ -1187,146 +1176,85 @@ var WorkerPackageLoader = class {
|
|
|
1187
1176
|
const cause = failures.length === 1 ? failures[0].error : new AggregateError(failures.map((failure) => failure.error));
|
|
1188
1177
|
return new Error(`Failed to import package(s) [${packageNames.join(", ")}] for document model: ${documentType}`, { cause });
|
|
1189
1178
|
}
|
|
1190
|
-
|
|
1191
|
-
if (this.loadedSpecs.has(spec)) return;
|
|
1179
|
+
loadPackage(spec) {
|
|
1192
1180
|
const name = packageName(spec);
|
|
1193
|
-
const url = `${this.cdnUrl}/${
|
|
1181
|
+
const url = `${this.cdnUrl}/${spec}/browser/document-models/index.js`;
|
|
1182
|
+
const loaded = this.loadedUrls.get(name);
|
|
1183
|
+
if (loaded !== void 0 && (spec === name || loaded === url)) return Promise.resolve({ status: "unchanged" });
|
|
1184
|
+
return this.loadFromUrl(name, url, loaded !== void 0);
|
|
1185
|
+
}
|
|
1186
|
+
loadFromUrl(key, url, replace = false) {
|
|
1187
|
+
if (!replace && this.loadedUrls.has(key)) return Promise.resolve({ status: "unchanged" });
|
|
1188
|
+
const pendingKey = `${key} ${url}`;
|
|
1189
|
+
let pending = this.pendingLoads.get(pendingKey);
|
|
1190
|
+
if (!pending) {
|
|
1191
|
+
pending = this.replaceSource(key, url).finally(() => this.pendingLoads.delete(pendingKey));
|
|
1192
|
+
this.pendingLoads.set(pendingKey, pending);
|
|
1193
|
+
}
|
|
1194
|
+
return pending;
|
|
1195
|
+
}
|
|
1196
|
+
async replaceSource(key, url) {
|
|
1197
|
+
const generation = ++this.generation;
|
|
1198
|
+
this.generationBySource.set(key, generation);
|
|
1199
|
+
let namespace;
|
|
1194
1200
|
try {
|
|
1195
|
-
|
|
1196
|
-
const rewritten = source !== void 0 ? rewritePackageSource(source, url, this.sharedImports) : source;
|
|
1197
|
-
let namespace;
|
|
1198
|
-
if (rewritten !== void 0 && rewritten !== source) {
|
|
1199
|
-
if (!this.importSource) throw new Error("importSource is required to load a package that imports shared deps");
|
|
1200
|
-
namespace = await this.importSource(rewritten);
|
|
1201
|
-
} else namespace = await this.importPackage(url);
|
|
1202
|
-
for (const value of Object.values(namespace)) if (isDocumentModelModule(value)) this.modulesByKey.set(moduleKey(value), value);
|
|
1203
|
-
this.loadedSpecs.add(spec);
|
|
1201
|
+
namespace = await this.importNamespace(key, url);
|
|
1204
1202
|
} catch (error) {
|
|
1205
|
-
this.
|
|
1206
|
-
|
|
1203
|
+
if (this.generationBySource.get(key) !== generation) return { status: "stale" };
|
|
1204
|
+
const failure = {
|
|
1205
|
+
name: packageName(key),
|
|
1207
1206
|
url,
|
|
1208
1207
|
error
|
|
1209
|
-
}
|
|
1208
|
+
};
|
|
1209
|
+
this.failures.push(failure);
|
|
1210
|
+
return {
|
|
1211
|
+
status: "failed",
|
|
1212
|
+
failure
|
|
1213
|
+
};
|
|
1210
1214
|
}
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
* res/err settlement, and per-request timeout/transform/cleanup.
|
|
1218
|
-
*/
|
|
1219
|
-
var RpcCorrelator = class {
|
|
1220
|
-
poster;
|
|
1221
|
-
prefix;
|
|
1222
|
-
counter = 0;
|
|
1223
|
-
pending = /* @__PURE__ */ new Map();
|
|
1224
|
-
constructor(poster, prefix = "r") {
|
|
1225
|
-
this.poster = poster;
|
|
1226
|
-
this.prefix = prefix;
|
|
1227
|
-
}
|
|
1228
|
-
nextId() {
|
|
1229
|
-
return `${this.prefix}${++this.counter}`;
|
|
1230
|
-
}
|
|
1231
|
-
/** Settle the pending request a res/err refers to; a missing id is a no-op. */
|
|
1232
|
-
handleMessage(message) {
|
|
1233
|
-
if (message.k !== "res" && message.k !== "err") return;
|
|
1234
|
-
const entry = this.pending.get(message.id);
|
|
1235
|
-
if (!entry) return;
|
|
1236
|
-
this.pending.delete(message.id);
|
|
1237
|
-
if (entry.timer) clearTimeout(entry.timer);
|
|
1238
|
-
entry.cleanup?.();
|
|
1239
|
-
if (message.k === "res") entry.resolve(entry.transform ? entry.transform(message.value) : message.value);
|
|
1240
|
-
else entry.reject(fromErrorInfo(message.error));
|
|
1241
|
-
}
|
|
1242
|
-
/** Send a request, resolving on its res/err. `build` stamps the generated id. */
|
|
1243
|
-
request(build, options = {}) {
|
|
1244
|
-
const id = this.nextId();
|
|
1245
|
-
const message = build(id);
|
|
1246
|
-
const method = message.method;
|
|
1247
|
-
const label = typeof method === "string" ? `${message.k} "${method}"` : message.k;
|
|
1248
|
-
const promise = new Promise((resolve, reject) => {
|
|
1249
|
-
let timer;
|
|
1250
|
-
if (options.timeoutMs !== void 0) {
|
|
1251
|
-
const timeoutMs = options.timeoutMs;
|
|
1252
|
-
timer = setTimeout(() => {
|
|
1253
|
-
const entry = this.pending.get(id);
|
|
1254
|
-
if (entry) {
|
|
1255
|
-
this.pending.delete(id);
|
|
1256
|
-
entry.cleanup?.();
|
|
1257
|
-
reject(/* @__PURE__ */ new Error(`Reactor worker did not respond to ${label} within ${timeoutMs}ms; the worker may have failed to load`));
|
|
1258
|
-
}
|
|
1259
|
-
}, timeoutMs);
|
|
1260
|
-
}
|
|
1261
|
-
this.pending.set(id, {
|
|
1262
|
-
resolve,
|
|
1263
|
-
reject,
|
|
1264
|
-
timer,
|
|
1265
|
-
transform: options.transform
|
|
1266
|
-
});
|
|
1267
|
-
});
|
|
1268
|
-
this.poster.post(message);
|
|
1269
|
-
const cleanup = options.setup?.(id);
|
|
1270
|
-
if (cleanup) {
|
|
1271
|
-
const entry = this.pending.get(id);
|
|
1272
|
-
if (entry) entry.cleanup = cleanup;
|
|
1273
|
-
else cleanup();
|
|
1215
|
+
if (this.generationBySource.get(key) !== generation) return { status: "stale" };
|
|
1216
|
+
const types = /* @__PURE__ */ new Set();
|
|
1217
|
+
for (const removedKey of this.keysBySource.get(key) ?? []) {
|
|
1218
|
+
const removed = this.modulesByKey.get(removedKey);
|
|
1219
|
+
if (removed) types.add(removed.documentModel.global.id);
|
|
1220
|
+
this.modulesByKey.delete(removedKey);
|
|
1274
1221
|
}
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
* Owns the single transport.onMessage and dispatches each message to the one
|
|
1282
|
-
* consumer that owns its kind. Composes the shared RpcCorrelator (res/err) and
|
|
1283
|
-
* exposes request/nextId; attach/detach swap the transport for reconnect.
|
|
1284
|
-
*/
|
|
1285
|
-
var MessageRouter = class {
|
|
1286
|
-
handlers = /* @__PURE__ */ new Map();
|
|
1287
|
-
correlator;
|
|
1288
|
-
transport = null;
|
|
1289
|
-
detachTransport = () => {};
|
|
1290
|
-
constructor(prefix = "r") {
|
|
1291
|
-
this.correlator = new RpcCorrelator(this, prefix);
|
|
1292
|
-
const settle = (message) => this.correlator.handleMessage(message);
|
|
1293
|
-
this.handlers.set("res", settle);
|
|
1294
|
-
this.handlers.set("err", settle);
|
|
1295
|
-
}
|
|
1296
|
-
attach(transport) {
|
|
1297
|
-
this.detachTransport();
|
|
1298
|
-
this.transport = transport;
|
|
1299
|
-
this.detachTransport = transport.onMessage((message) => {
|
|
1300
|
-
const msg = message;
|
|
1301
|
-
this.handlers.get(msg.k)?.(msg);
|
|
1302
|
-
});
|
|
1303
|
-
}
|
|
1304
|
-
detach() {
|
|
1305
|
-
this.detachTransport();
|
|
1306
|
-
this.detachTransport = () => {};
|
|
1307
|
-
this.transport = null;
|
|
1308
|
-
}
|
|
1309
|
-
post(message) {
|
|
1310
|
-
if (!this.transport) throw new Error("MessageRouter.post called before attach");
|
|
1311
|
-
this.transport.post(message);
|
|
1312
|
-
}
|
|
1313
|
-
/** Register the sole owner of a message kind; throws on a duplicate. */
|
|
1314
|
-
on(kind, handler) {
|
|
1315
|
-
if (this.handlers.has(kind)) throw new Error(`MessageRouter already has a handler for "${kind}"`);
|
|
1316
|
-
const route = handler;
|
|
1317
|
-
this.handlers.set(kind, route);
|
|
1318
|
-
return () => {
|
|
1319
|
-
if (this.handlers.get(kind) === route) this.handlers.delete(kind);
|
|
1222
|
+
for (const type of this.manifestTypesBySource.get(key) ?? []) this.manifestsByType.delete(type);
|
|
1223
|
+
for (const added of this.registerNamespace(key, namespace)) types.add(added);
|
|
1224
|
+
this.loadedUrls.set(key, url);
|
|
1225
|
+
return {
|
|
1226
|
+
status: "replaced",
|
|
1227
|
+
types
|
|
1320
1228
|
};
|
|
1321
1229
|
}
|
|
1322
|
-
|
|
1323
|
-
return this.
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1230
|
+
async importNamespace(key, url) {
|
|
1231
|
+
if (key.startsWith(SOURCE_KEY_PREFIX)) return this.importPackage(url);
|
|
1232
|
+
return this.importPackage(await this.moduleGraph.resolve(url));
|
|
1233
|
+
}
|
|
1234
|
+
/** Registers a namespace as the key's whole contribution; returns its module types. */
|
|
1235
|
+
registerNamespace(key, namespace) {
|
|
1236
|
+
const keys = /* @__PURE__ */ new Set();
|
|
1237
|
+
const manifestTypes = /* @__PURE__ */ new Set();
|
|
1238
|
+
const types = /* @__PURE__ */ new Set();
|
|
1239
|
+
for (const value of Object.values(namespace)) if (isDocumentModelModule(value)) {
|
|
1240
|
+
const k = moduleKey(value);
|
|
1241
|
+
this.modulesByKey.set(k, value);
|
|
1242
|
+
keys.add(k);
|
|
1243
|
+
types.add(value.documentModel.global.id);
|
|
1244
|
+
}
|
|
1245
|
+
for (const manifest of manifestsOf(namespace)) {
|
|
1246
|
+
this.manifestsByType.set(manifest.documentType, manifest);
|
|
1247
|
+
manifestTypes.add(manifest.documentType);
|
|
1248
|
+
}
|
|
1249
|
+
this.keysBySource.set(key, keys);
|
|
1250
|
+
this.manifestTypesBySource.set(key, manifestTypes);
|
|
1251
|
+
return types;
|
|
1327
1252
|
}
|
|
1328
1253
|
};
|
|
1254
|
+
function sourceKey(source) {
|
|
1255
|
+
return `${SOURCE_KEY_PREFIX}${source.name}`;
|
|
1256
|
+
}
|
|
1329
1257
|
//#endregion
|
|
1330
|
-
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 };
|
|
1258
|
+
export { FORWARDED_BUS_EVENT_TYPES, FORWARDED_EVENT_TYPES, KeyedListeners, Listeners, MessageRouter, ModuleGraph, RETIRED_WORKER_RELOAD_REASON, RPC_DEFAULT_TIMEOUT_MS, RPC_PROTOCOL_VERSION, ReactorEventBusProxy, ReactorHost, ReactorHostServer, RpcCorrelator, SYNC_REMOTES_CHANGED_EVENT, SYNC_STATUS_CHANGED_EVENT, SubscriptionStore, SyncManagerProxy, WorkerPackageLoader, connectReactorClient, createCorrelatedSubscriptions, createInspectorProxy, createLiveQueryProxy, createPortTransport, createReactorClientProxy, createReactorEventBusProxy, createRelationalPgliteProxy, createSyncManagerProxy, createWorkerAdminClient, describeLoadFailure, dispatchInspectorOp, dispatchSyncInspectionOp, fromErrorInfo, hostResponder, isFingerprintMismatchReload, isSyncInspectionOp, localSyncPeerHandlers, opChannel, postReactorIdentity, resolveSharedSpecifier, sendAdoptSyncPeer, sendRemoveSyncPeer, toErrorInfo, toVoid };
|
|
1331
1259
|
|
|
1332
1260
|
//# sourceMappingURL=index.js.map
|