@party-stack/runtime 0.4.2 → 0.5.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/lib/BrowserAuthenticationError.d.ts +11 -0
- package/lib/BrowserAuthenticationError.d.ts.map +1 -0
- package/lib/BrowserAuthenticationError.js +15 -0
- package/lib/BrowserAuthenticationError.js.map +1 -0
- package/lib/MemoryRuntime.d.ts +11 -0
- package/lib/MemoryRuntime.d.ts.map +1 -0
- package/lib/MemoryRuntime.js +23 -0
- package/lib/MemoryRuntime.js.map +1 -0
- package/lib/NetworkConnectivity.d.ts +13 -0
- package/lib/NetworkConnectivity.d.ts.map +1 -0
- package/lib/NetworkConnectivity.js +29 -0
- package/lib/NetworkConnectivity.js.map +1 -0
- package/lib/Runtime.d.ts +34 -0
- package/lib/Runtime.d.ts.map +1 -0
- package/lib/Runtime.js +28 -0
- package/lib/Runtime.js.map +1 -0
- package/lib/RuntimePartition.d.ts +16 -0
- package/lib/RuntimePartition.d.ts.map +1 -0
- package/lib/RuntimePartition.js +13 -0
- package/lib/RuntimePartition.js.map +1 -0
- package/lib/Runtimes.d.ts +22 -0
- package/lib/Runtimes.d.ts.map +1 -0
- package/lib/Runtimes.js +24 -0
- package/lib/Runtimes.js.map +1 -0
- package/lib/_virtual/_rolldown/runtime.js +13 -0
- package/lib/capacity.d.ts +26 -0
- package/lib/capacity.d.ts.map +1 -0
- package/lib/capacity.js +28 -0
- package/lib/capacity.js.map +1 -0
- package/lib/encryptionKeys.d.ts +36 -0
- package/lib/encryptionKeys.d.ts.map +1 -0
- package/lib/encryptionKeys.js +84 -0
- package/lib/encryptionKeys.js.map +1 -0
- package/lib/index.d.ts +11 -10
- package/lib/index.js +10 -9
- package/lib/memory/MemoryBlobBytesStore.d.ts +6 -5
- package/lib/memory/MemoryBlobBytesStore.d.ts.map +1 -1
- package/lib/memory/MemoryBlobBytesStore.js +9 -9
- package/lib/memory/MemoryBlobBytesStore.js.map +1 -1
- package/lib/testing.d.ts +42 -0
- package/lib/testing.d.ts.map +1 -0
- package/lib/testing.js +138 -0
- package/lib/testing.js.map +1 -0
- package/lib/tracing.d.ts +48 -0
- package/lib/tracing.d.ts.map +1 -0
- package/lib/tracing.js +173 -0
- package/lib/tracing.js.map +1 -0
- package/lib/types.d.ts +23 -29
- package/lib/types.d.ts.map +1 -1
- package/package.json +13 -13
- package/lib/coordinator/createPersistedCollectionCoordinator.d.ts +0 -6
- package/lib/coordinator/createPersistedCollectionCoordinator.d.ts.map +0 -1
- package/lib/coordinator/createPersistedCollectionCoordinator.js +0 -514
- package/lib/coordinator/createPersistedCollectionCoordinator.js.map +0 -1
- package/lib/coordinator/index.d.ts +0 -2
- package/lib/coordinator/index.js +0 -1
- package/lib/effection/index.d.ts +0 -7
- package/lib/effection/index.d.ts.map +0 -1
- package/lib/effection/index.js +0 -28
- package/lib/effection/index.js.map +0 -1
- package/lib/utils/BrowserAuthenticationCancelledError.d.ts +0 -6
- package/lib/utils/BrowserAuthenticationCancelledError.d.ts.map +0 -1
- package/lib/utils/BrowserAuthenticationCancelledError.js +0 -11
- package/lib/utils/BrowserAuthenticationCancelledError.js.map +0 -1
- package/lib/utils/createCollectionPersistence.d.ts +0 -7
- package/lib/utils/createCollectionPersistence.d.ts.map +0 -1
- package/lib/utils/createCollectionPersistence.js +0 -24
- package/lib/utils/createCollectionPersistence.js.map +0 -1
- package/lib/utils/createDefaultRuntime.d.ts +0 -5
- package/lib/utils/createDefaultRuntime.d.ts.map +0 -1
- package/lib/utils/createDefaultRuntime.js +0 -19
- package/lib/utils/createDefaultRuntime.js.map +0 -1
- package/lib/utils/createLocalCollection.d.ts +0 -11
- package/lib/utils/createLocalCollection.d.ts.map +0 -1
- package/lib/utils/createLocalCollection.js +0 -22
- package/lib/utils/createLocalCollection.js.map +0 -1
- package/lib/utils/defineRuntime.d.ts +0 -9
- package/lib/utils/defineRuntime.d.ts.map +0 -1
- package/lib/utils/defineRuntime.js +0 -30
- package/lib/utils/defineRuntime.js.map +0 -1
|
@@ -1,514 +0,0 @@
|
|
|
1
|
-
import { isCoordinationHost } from "@party-stack/coordination";
|
|
2
|
-
import { SingleProcessCoordinator, eagerStorageTarget, managedStorageTarget, safeRandomUUID, toPersistedCollectionDurabilityError, toTransportedLoadSubsetOptions } from "@tanstack/db-sqlite-persistence-core";
|
|
3
|
-
//#region src/coordinator/createPersistedCollectionCoordinator.ts
|
|
4
|
-
const PERSISTENCE_SERVICE = "party-stack.persistence.v1";
|
|
5
|
-
const MAX_DEDUPLICATION_ENTRIES = 1e3;
|
|
6
|
-
const MAX_PENDING_RELAYS = 1e3;
|
|
7
|
-
const MAX_COLLECTION_POSITIONS = 128;
|
|
8
|
-
const coordinatorCache = /* @__PURE__ */ new WeakMap();
|
|
9
|
-
function isRecord(value) {
|
|
10
|
-
return typeof value === "object" && value !== null;
|
|
11
|
-
}
|
|
12
|
-
function relayKey(collectionId, message) {
|
|
13
|
-
const payload = isRecord(message.payload) ? message.payload : {};
|
|
14
|
-
return JSON.stringify([
|
|
15
|
-
collectionId,
|
|
16
|
-
message.senderId,
|
|
17
|
-
payload.type,
|
|
18
|
-
payload.txId,
|
|
19
|
-
payload.resetEpoch,
|
|
20
|
-
payload.term,
|
|
21
|
-
payload.seq
|
|
22
|
-
]);
|
|
23
|
-
}
|
|
24
|
-
function createPersistedCollectionCoordinator(coordination, adapter) {
|
|
25
|
-
let adapters = coordinatorCache.get(coordination);
|
|
26
|
-
if (!adapters) {
|
|
27
|
-
adapters = /* @__PURE__ */ new WeakMap();
|
|
28
|
-
coordinatorCache.set(coordination, adapters);
|
|
29
|
-
}
|
|
30
|
-
const existing = adapters.get(adapter);
|
|
31
|
-
if (existing) return existing;
|
|
32
|
-
const created = new CoordinationPersistenceShim(coordination, adapter);
|
|
33
|
-
adapters.set(adapter, created);
|
|
34
|
-
return created;
|
|
35
|
-
}
|
|
36
|
-
var CoordinationPersistenceShim = class {
|
|
37
|
-
coordination;
|
|
38
|
-
adapter;
|
|
39
|
-
nodeId = safeRandomUUID();
|
|
40
|
-
collectionAdapters = /* @__PURE__ */ new Map();
|
|
41
|
-
collectionSubscriptions = /* @__PURE__ */ new Map();
|
|
42
|
-
resumeChecks = /* @__PURE__ */ new Map();
|
|
43
|
-
localMutationBursts = /* @__PURE__ */ new Map();
|
|
44
|
-
stopResumeListeners;
|
|
45
|
-
checkingResume = false;
|
|
46
|
-
resumeRequested = false;
|
|
47
|
-
requestResumeCheck = () => {
|
|
48
|
-
this.resumeRequested = true;
|
|
49
|
-
if (this.checkingResume) return;
|
|
50
|
-
this.checkingResume = true;
|
|
51
|
-
(async () => {
|
|
52
|
-
try {
|
|
53
|
-
while (this.resumeRequested && this.resumeChecks.size > 0) {
|
|
54
|
-
this.resumeRequested = false;
|
|
55
|
-
await Promise.all([...this.resumeChecks.values()].flatMap((checks) => [...checks].map((check) => check())));
|
|
56
|
-
}
|
|
57
|
-
} finally {
|
|
58
|
-
this.checkingResume = false;
|
|
59
|
-
this.resumeRequested = false;
|
|
60
|
-
}
|
|
61
|
-
})();
|
|
62
|
-
};
|
|
63
|
-
startResumeListeners() {
|
|
64
|
-
if (this.stopResumeListeners || typeof window === "undefined" || typeof document === "undefined") return;
|
|
65
|
-
const page = window;
|
|
66
|
-
const visibility = document;
|
|
67
|
-
const onVisible = () => {
|
|
68
|
-
if (visibility.visibilityState === "visible") this.requestResumeCheck();
|
|
69
|
-
};
|
|
70
|
-
page.addEventListener("pageshow", this.requestResumeCheck);
|
|
71
|
-
page.addEventListener("focus", onVisible);
|
|
72
|
-
visibility.addEventListener("visibilitychange", onVisible);
|
|
73
|
-
this.stopResumeListeners = () => {
|
|
74
|
-
page.removeEventListener("pageshow", this.requestResumeCheck);
|
|
75
|
-
page.removeEventListener("focus", onVisible);
|
|
76
|
-
visibility.removeEventListener("visibilitychange", onVisible);
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
setAdapterForCollection(collectionId, adapter, cacheGenerationClaimId) {
|
|
80
|
-
if (cacheGenerationClaimId) this.claims.set(collectionId, cacheGenerationClaimId);
|
|
81
|
-
else this.claims.delete(collectionId);
|
|
82
|
-
this.collectionAdapters.set(collectionId, adapter);
|
|
83
|
-
this.positions.delete(collectionId);
|
|
84
|
-
}
|
|
85
|
-
adapterForCollection(collectionId, schemaVersion) {
|
|
86
|
-
const adapter = this.collectionAdapters.get(collectionId) ?? this.adapter;
|
|
87
|
-
if (schemaVersion !== void 0 && this.schemaVersion(adapter) !== schemaVersion) throw new Error(`Persistence schema mismatch for collection "${collectionId}". Reload the coordination host before writing through another schema version.`);
|
|
88
|
-
return adapter;
|
|
89
|
-
}
|
|
90
|
-
schemaVersion(adapter) {
|
|
91
|
-
return "schemaVersion" in adapter && typeof adapter.schemaVersion === "number" ? adapter.schemaVersion : void 0;
|
|
92
|
-
}
|
|
93
|
-
claims = /* @__PURE__ */ new Map();
|
|
94
|
-
storageTarget(collectionId, claimId = this.claims.get(collectionId)) {
|
|
95
|
-
if (claimId) this.claims.set(collectionId, claimId);
|
|
96
|
-
return claimId ? managedStorageTarget(collectionId, claimId) : eagerStorageTarget(collectionId);
|
|
97
|
-
}
|
|
98
|
-
positions = /* @__PURE__ */ new Map();
|
|
99
|
-
appliedEnvelopes = /* @__PURE__ */ new Map();
|
|
100
|
-
relayedMessages = /* @__PURE__ */ new Set();
|
|
101
|
-
service;
|
|
102
|
-
server;
|
|
103
|
-
pendingRelays = 0;
|
|
104
|
-
remoteSubsets = new SingleProcessCoordinator();
|
|
105
|
-
acquisitionIds = /* @__PURE__ */ new WeakMap();
|
|
106
|
-
remoteAcquisitions = /* @__PURE__ */ new Map();
|
|
107
|
-
constructor(coordination, adapter) {
|
|
108
|
-
this.coordination = coordination;
|
|
109
|
-
this.adapter = adapter;
|
|
110
|
-
this.service = coordination.service(PERSISTENCE_SERVICE);
|
|
111
|
-
this.server = isCoordinationHost(coordination) ? coordination.serve(PERSISTENCE_SERVICE, {
|
|
112
|
-
ensureLeadership: () => Promise.resolve(),
|
|
113
|
-
ensureRemoteSubset: async (input) => {
|
|
114
|
-
let options = this.remoteAcquisitions.get(input.acquisitionId)?.options;
|
|
115
|
-
if (!options) {
|
|
116
|
-
options = input.options;
|
|
117
|
-
this.remoteAcquisitions.set(input.acquisitionId, {
|
|
118
|
-
collectionId: input.collectionId,
|
|
119
|
-
options
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
try {
|
|
123
|
-
await this.remoteSubsets.requestEnsureRemoteSubset(input.collectionId, options);
|
|
124
|
-
} catch (error) {
|
|
125
|
-
if (this.remoteAcquisitions.get(input.acquisitionId)?.options === options) {
|
|
126
|
-
this.remoteAcquisitions.delete(input.acquisitionId);
|
|
127
|
-
await this.remoteSubsets.requestReleaseRemoteSubset(input.collectionId, options).catch(() => void 0);
|
|
128
|
-
}
|
|
129
|
-
throw error;
|
|
130
|
-
}
|
|
131
|
-
},
|
|
132
|
-
releaseRemoteSubset: (input) => {
|
|
133
|
-
const options = this.remoteAcquisitions.get(input.acquisitionId)?.options;
|
|
134
|
-
this.remoteAcquisitions.delete(input.acquisitionId);
|
|
135
|
-
return options ? this.remoteSubsets.requestReleaseRemoteSubset(input.collectionId, options) : Promise.resolve();
|
|
136
|
-
},
|
|
137
|
-
applyCommittedTx: (input) => this.applyCommittedTx(input),
|
|
138
|
-
ensurePersistedIndex: (input) => this.adapterForCollection(input.collectionId, input.schemaVersion).ensureIndex(this.storageTarget(input.collectionId, input.cacheGenerationClaimId), input.signature, input.spec),
|
|
139
|
-
applyLocalMutations: (input) => this.applyMutations(input),
|
|
140
|
-
pullSince: (input) => this.handlePullSince(input),
|
|
141
|
-
relayMessage: (input) => this.relayMessage(input)
|
|
142
|
-
}) : void 0;
|
|
143
|
-
}
|
|
144
|
-
getNodeId() {
|
|
145
|
-
return this.nodeId;
|
|
146
|
-
}
|
|
147
|
-
subscribe(collectionId, callback) {
|
|
148
|
-
let active = true;
|
|
149
|
-
let observed;
|
|
150
|
-
const unsubscribe = this.service.events.subscribe("message", (event) => {
|
|
151
|
-
if (!active) return;
|
|
152
|
-
if (event.collectionId === collectionId) {
|
|
153
|
-
const payload = event.message.payload;
|
|
154
|
-
const burst = this.localMutationBursts.get(collectionId);
|
|
155
|
-
if (burst && isRecord(payload) && payload.type === "tx:committed" && typeof payload.txId === "string" && !burst.pending.has(payload.txId)) burst.needsReload = true;
|
|
156
|
-
if (isRecord(payload) && payload.type === "tx:committed" && typeof payload.term === "number" && typeof payload.seq === "number" && typeof payload.latestRowVersion === "number" && (!observed || payload.term > observed.latestTerm || payload.term === observed.latestTerm && payload.seq > observed.latestSeq)) observed = {
|
|
157
|
-
latestTerm: payload.term,
|
|
158
|
-
latestSeq: payload.seq,
|
|
159
|
-
latestRowVersion: payload.latestRowVersion
|
|
160
|
-
};
|
|
161
|
-
callback(event.message);
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
const checkResume = async (force = false) => {
|
|
165
|
-
try {
|
|
166
|
-
if (!active) return;
|
|
167
|
-
const adapter = this.adapterForCollection(collectionId);
|
|
168
|
-
const current = await adapter.getStreamPosition?.(this.storageTarget(collectionId));
|
|
169
|
-
if (!active) return;
|
|
170
|
-
if (!force && current && observed && current.latestTerm === observed.latestTerm && current.latestSeq === observed.latestSeq && current.latestRowVersion === observed.latestRowVersion) return;
|
|
171
|
-
callback({
|
|
172
|
-
v: 1,
|
|
173
|
-
dbName: this.coordinationScope(),
|
|
174
|
-
collectionId,
|
|
175
|
-
senderId: `resume:${this.nodeId}`,
|
|
176
|
-
ts: Date.now(),
|
|
177
|
-
payload: {
|
|
178
|
-
type: "collection:reset",
|
|
179
|
-
schemaVersion: this.schemaVersion(adapter) ?? 1,
|
|
180
|
-
resetEpoch: 0
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
observed = current;
|
|
184
|
-
} catch (error) {
|
|
185
|
-
if (active) console.warn(`Failed to refresh resumed collection "${collectionId}".`, error);
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
const checks = this.resumeChecks.get(collectionId) ?? /* @__PURE__ */ new Set();
|
|
189
|
-
checks.add(checkResume);
|
|
190
|
-
this.resumeChecks.set(collectionId, checks);
|
|
191
|
-
this.startResumeListeners();
|
|
192
|
-
this.collectionSubscriptions.set(collectionId, (this.collectionSubscriptions.get(collectionId) ?? 0) + 1);
|
|
193
|
-
return () => {
|
|
194
|
-
if (!active) return;
|
|
195
|
-
active = false;
|
|
196
|
-
checks.delete(checkResume);
|
|
197
|
-
if (checks.size === 0) {
|
|
198
|
-
this.resumeChecks.delete(collectionId);
|
|
199
|
-
const burst = this.localMutationBursts.get(collectionId);
|
|
200
|
-
if (burst?.timer !== void 0) clearTimeout(burst.timer);
|
|
201
|
-
this.localMutationBursts.delete(collectionId);
|
|
202
|
-
}
|
|
203
|
-
if (this.resumeChecks.size === 0) {
|
|
204
|
-
this.stopResumeListeners?.();
|
|
205
|
-
this.stopResumeListeners = void 0;
|
|
206
|
-
}
|
|
207
|
-
try {
|
|
208
|
-
unsubscribe();
|
|
209
|
-
} finally {
|
|
210
|
-
const remaining = this.collectionSubscriptions.get(collectionId) - 1;
|
|
211
|
-
if (remaining > 0) this.collectionSubscriptions.set(collectionId, remaining);
|
|
212
|
-
else {
|
|
213
|
-
this.collectionSubscriptions.delete(collectionId);
|
|
214
|
-
this.collectionAdapters.delete(collectionId);
|
|
215
|
-
this.claims.delete(collectionId);
|
|
216
|
-
this.positions.delete(collectionId);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
publish(collectionId, message) {
|
|
222
|
-
if (this.server && isCoordinationHost(this.coordination) && this.coordination.isLeader) {
|
|
223
|
-
this.publishMessage({
|
|
224
|
-
collectionId,
|
|
225
|
-
message
|
|
226
|
-
});
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
if (this.pendingRelays >= MAX_PENDING_RELAYS) {
|
|
230
|
-
console.warn(`Dropping persistence relay for "${collectionId}" because the pending relay limit was reached.`);
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
this.pendingRelays += 1;
|
|
234
|
-
this.service.methods.relayMessage({
|
|
235
|
-
collectionId,
|
|
236
|
-
message
|
|
237
|
-
}).catch((error) => {
|
|
238
|
-
console.warn(`Failed to relay persistence message for "${collectionId}".`, error);
|
|
239
|
-
}).finally(() => {
|
|
240
|
-
this.pendingRelays -= 1;
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
isLeader() {
|
|
244
|
-
return isCoordinationHost(this.coordination) && this.coordination.isLeader;
|
|
245
|
-
}
|
|
246
|
-
ensureLeadership() {
|
|
247
|
-
return this.service.methods.ensureLeadership({ collectionId: "*" });
|
|
248
|
-
}
|
|
249
|
-
requestEnsureRemoteSubset(collectionId, options) {
|
|
250
|
-
let collectionIds = this.acquisitionIds.get(options);
|
|
251
|
-
if (!collectionIds) {
|
|
252
|
-
collectionIds = /* @__PURE__ */ new Map();
|
|
253
|
-
this.acquisitionIds.set(options, collectionIds);
|
|
254
|
-
}
|
|
255
|
-
let acquisitionId = collectionIds.get(collectionId);
|
|
256
|
-
if (!acquisitionId) {
|
|
257
|
-
acquisitionId = safeRandomUUID();
|
|
258
|
-
collectionIds.set(collectionId, acquisitionId);
|
|
259
|
-
}
|
|
260
|
-
return this.service.methods.ensureRemoteSubset({
|
|
261
|
-
collectionId,
|
|
262
|
-
options: toTransportedLoadSubsetOptions(options),
|
|
263
|
-
acquisitionId
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
requestReleaseRemoteSubset(collectionId, options) {
|
|
267
|
-
const collectionIds = this.acquisitionIds.get(options);
|
|
268
|
-
const acquisitionId = collectionIds?.get(collectionId);
|
|
269
|
-
if (!acquisitionId) return Promise.resolve();
|
|
270
|
-
collectionIds.delete(collectionId);
|
|
271
|
-
if (collectionIds.size === 0) this.acquisitionIds.delete(options);
|
|
272
|
-
return this.service.methods.releaseRemoteSubset({
|
|
273
|
-
collectionId,
|
|
274
|
-
options: {},
|
|
275
|
-
acquisitionId
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
registerRemoteSubsetOwner(collectionId, owner) {
|
|
279
|
-
const unregister = this.remoteSubsets.registerRemoteSubsetOwner(collectionId, owner);
|
|
280
|
-
let registered = true;
|
|
281
|
-
return () => {
|
|
282
|
-
if (!registered) return;
|
|
283
|
-
registered = false;
|
|
284
|
-
unregister();
|
|
285
|
-
for (const [id, acquisition] of this.remoteAcquisitions) if (acquisition.collectionId === collectionId) this.remoteAcquisitions.delete(id);
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
requestApplyCommittedTx(collectionId, tx, scopedAdapter) {
|
|
289
|
-
return this.isLeader() ? this.applyCommittedTx({
|
|
290
|
-
collectionId,
|
|
291
|
-
tx
|
|
292
|
-
}, scopedAdapter) : this.service.methods.applyCommittedTx({
|
|
293
|
-
collectionId,
|
|
294
|
-
tx,
|
|
295
|
-
schemaVersion: this.schemaVersion(scopedAdapter ?? this.adapterForCollection(collectionId)) ?? this.schemaVersion(this.adapterForCollection(collectionId))
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
async applyCommittedTx({ collectionId, tx, schemaVersion }, scopedAdapter) {
|
|
299
|
-
try {
|
|
300
|
-
await (scopedAdapter ?? this.adapterForCollection(collectionId, schemaVersion)).applyCommittedTx(this.storageTarget(collectionId, tx.cacheGenerationClaimId), tx);
|
|
301
|
-
} catch (error) {
|
|
302
|
-
throw toPersistedCollectionDurabilityError(collectionId, error);
|
|
303
|
-
}
|
|
304
|
-
this.positions.delete(collectionId);
|
|
305
|
-
return {
|
|
306
|
-
type: "rpc:applyCommittedTx:res",
|
|
307
|
-
rpcId: safeRandomUUID(),
|
|
308
|
-
ok: true,
|
|
309
|
-
term: tx.term,
|
|
310
|
-
seq: tx.seq,
|
|
311
|
-
latestRowVersion: tx.rowVersion
|
|
312
|
-
};
|
|
313
|
-
}
|
|
314
|
-
requestEnsurePersistedIndex(collectionId, signature, spec, scopedAdapter, localEnsureCompleted, cacheGenerationClaimId) {
|
|
315
|
-
if (this.isLeader()) return localEnsureCompleted ? Promise.resolve() : (scopedAdapter ?? this.adapterForCollection(collectionId)).ensureIndex(this.storageTarget(collectionId, cacheGenerationClaimId), signature, spec);
|
|
316
|
-
return this.service.methods.ensurePersistedIndex({
|
|
317
|
-
collectionId,
|
|
318
|
-
signature,
|
|
319
|
-
spec,
|
|
320
|
-
cacheGenerationClaimId,
|
|
321
|
-
schemaVersion: this.schemaVersion(scopedAdapter ?? this.adapterForCollection(collectionId)) ?? this.schemaVersion(this.adapterForCollection(collectionId))
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
async requestApplyLocalMutations(collectionId, mutations, cacheGenerationClaimId) {
|
|
325
|
-
const envelopeId = safeRandomUUID();
|
|
326
|
-
const burst = this.resumeChecks.has(collectionId) ? this.localMutationBursts.get(collectionId) ?? {
|
|
327
|
-
pending: /* @__PURE__ */ new Set(),
|
|
328
|
-
needsReload: false,
|
|
329
|
-
timer: void 0
|
|
330
|
-
} : void 0;
|
|
331
|
-
if (burst) {
|
|
332
|
-
if (burst.timer !== void 0) clearTimeout(burst.timer);
|
|
333
|
-
if (burst.pending.size > 0) burst.needsReload = true;
|
|
334
|
-
burst.pending.add(envelopeId);
|
|
335
|
-
this.localMutationBursts.set(collectionId, burst);
|
|
336
|
-
}
|
|
337
|
-
try {
|
|
338
|
-
return await this.service.methods.applyLocalMutations({
|
|
339
|
-
collectionId,
|
|
340
|
-
rpcId: safeRandomUUID(),
|
|
341
|
-
envelopeId,
|
|
342
|
-
schemaVersion: this.schemaVersion(this.adapterForCollection(collectionId)),
|
|
343
|
-
mutations,
|
|
344
|
-
cacheGenerationClaimId
|
|
345
|
-
});
|
|
346
|
-
} finally {
|
|
347
|
-
burst?.pending.delete(envelopeId);
|
|
348
|
-
if (burst && burst.pending.size === 0 && this.localMutationBursts.get(collectionId) === burst) burst.timer = setTimeout(() => {
|
|
349
|
-
if (this.localMutationBursts.get(collectionId) !== burst) return;
|
|
350
|
-
this.localMutationBursts.delete(collectionId);
|
|
351
|
-
if (burst.needsReload) for (const check of this.resumeChecks.get(collectionId) ?? []) check(true);
|
|
352
|
-
}, 0);
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
pullSince(collectionId, fromRowVersion, scopedAdapter, cacheGenerationClaimId) {
|
|
356
|
-
this.storageTarget(collectionId, cacheGenerationClaimId);
|
|
357
|
-
const input = {
|
|
358
|
-
collectionId,
|
|
359
|
-
rpcId: safeRandomUUID(),
|
|
360
|
-
fromRowVersion,
|
|
361
|
-
cacheGenerationClaimId,
|
|
362
|
-
schemaVersion: this.schemaVersion(this.adapterForCollection(collectionId))
|
|
363
|
-
};
|
|
364
|
-
return scopedAdapter && this.isLeader() ? this.handlePullSince(input, scopedAdapter) : this.service.methods.pullSince(input);
|
|
365
|
-
}
|
|
366
|
-
relayMessage(input) {
|
|
367
|
-
const key = relayKey(input.collectionId, input.message);
|
|
368
|
-
if (this.relayedMessages.has(key)) return Promise.resolve();
|
|
369
|
-
this.relayedMessages.add(key);
|
|
370
|
-
if (this.relayedMessages.size > MAX_DEDUPLICATION_ENTRIES) {
|
|
371
|
-
const oldest = this.relayedMessages.values().next().value;
|
|
372
|
-
if (oldest) this.relayedMessages.delete(oldest);
|
|
373
|
-
}
|
|
374
|
-
this.publishMessage(input);
|
|
375
|
-
return Promise.resolve();
|
|
376
|
-
}
|
|
377
|
-
publishMessage(input) {
|
|
378
|
-
this.server?.events.publish("message", input);
|
|
379
|
-
}
|
|
380
|
-
async position(collectionId) {
|
|
381
|
-
let position = this.positions.get(collectionId);
|
|
382
|
-
if (!position) {
|
|
383
|
-
const adapter = this.adapterForCollection(collectionId);
|
|
384
|
-
position = Promise.resolve(adapter.getStreamPosition ? adapter.getStreamPosition(this.storageTarget(collectionId)) : adapter.loadResumeSnapshot(this.storageTarget(collectionId), { includeRows: false })).then((current) => ({
|
|
385
|
-
term: (current?.latestTerm ?? 0) + 1,
|
|
386
|
-
seq: current?.latestSeq ?? 0,
|
|
387
|
-
rowVersion: current?.latestRowVersion ?? 0
|
|
388
|
-
}));
|
|
389
|
-
}
|
|
390
|
-
this.positions.delete(collectionId);
|
|
391
|
-
this.positions.set(collectionId, position);
|
|
392
|
-
while (this.positions.size > MAX_COLLECTION_POSITIONS) {
|
|
393
|
-
const oldest = this.positions.keys().next().value;
|
|
394
|
-
if (oldest !== void 0) this.positions.delete(oldest);
|
|
395
|
-
}
|
|
396
|
-
position.catch(() => {
|
|
397
|
-
if (this.positions.get(collectionId) === position) this.positions.delete(collectionId);
|
|
398
|
-
});
|
|
399
|
-
return position;
|
|
400
|
-
}
|
|
401
|
-
async applyMutations(input) {
|
|
402
|
-
const adapter = this.adapterForCollection(input.collectionId, input.schemaVersion);
|
|
403
|
-
const envelopeKey = JSON.stringify([input.collectionId, input.envelopeId]);
|
|
404
|
-
const previous = this.appliedEnvelopes.get(envelopeKey);
|
|
405
|
-
if (previous) return {
|
|
406
|
-
...previous,
|
|
407
|
-
rpcId: input.rpcId
|
|
408
|
-
};
|
|
409
|
-
this.storageTarget(input.collectionId, input.cacheGenerationClaimId);
|
|
410
|
-
const position = await this.position(input.collectionId);
|
|
411
|
-
const nextPosition = {
|
|
412
|
-
...position,
|
|
413
|
-
seq: position.seq + 1,
|
|
414
|
-
rowVersion: position.rowVersion + 1
|
|
415
|
-
};
|
|
416
|
-
const txId = input.envelopeId;
|
|
417
|
-
await adapter.applyCommittedTx(this.storageTarget(input.collectionId, input.cacheGenerationClaimId), {
|
|
418
|
-
cacheGenerationClaimId: input.cacheGenerationClaimId,
|
|
419
|
-
txId,
|
|
420
|
-
term: nextPosition.term,
|
|
421
|
-
seq: nextPosition.seq,
|
|
422
|
-
rowVersion: nextPosition.rowVersion,
|
|
423
|
-
mutations: input.mutations.map(({ mutationId, ...mutation }) => {
|
|
424
|
-
return mutation;
|
|
425
|
-
})
|
|
426
|
-
});
|
|
427
|
-
Object.assign(position, nextPosition);
|
|
428
|
-
const committed = {
|
|
429
|
-
v: 1,
|
|
430
|
-
dbName: this.coordinationScope(),
|
|
431
|
-
collectionId: input.collectionId,
|
|
432
|
-
senderId: this.nodeId,
|
|
433
|
-
ts: Date.now(),
|
|
434
|
-
payload: {
|
|
435
|
-
type: "tx:committed",
|
|
436
|
-
term: position.term,
|
|
437
|
-
seq: position.seq,
|
|
438
|
-
txId,
|
|
439
|
-
latestRowVersion: position.rowVersion,
|
|
440
|
-
requiresFullReload: false,
|
|
441
|
-
changedRows: input.mutations.filter((mutation) => mutation.type !== "delete").map((mutation) => ({
|
|
442
|
-
key: mutation.key,
|
|
443
|
-
value: mutation.value
|
|
444
|
-
})),
|
|
445
|
-
deletedKeys: input.mutations.filter((mutation) => mutation.type === "delete").map((mutation) => mutation.key),
|
|
446
|
-
rowMetadataMutations: input.mutations.flatMap((mutation) => mutation.type !== "delete" && (mutation.metadataChanged || mutation.metadata !== void 0) ? [mutation.metadata === void 0 ? {
|
|
447
|
-
type: "delete",
|
|
448
|
-
key: mutation.key
|
|
449
|
-
} : {
|
|
450
|
-
type: "set",
|
|
451
|
-
key: mutation.key,
|
|
452
|
-
value: mutation.metadata
|
|
453
|
-
}] : [])
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
this.publishMessage({
|
|
457
|
-
collectionId: input.collectionId,
|
|
458
|
-
message: committed
|
|
459
|
-
});
|
|
460
|
-
const response = {
|
|
461
|
-
type: "rpc:applyLocalMutations:res",
|
|
462
|
-
rpcId: input.rpcId,
|
|
463
|
-
ok: true,
|
|
464
|
-
term: position.term,
|
|
465
|
-
seq: position.seq,
|
|
466
|
-
latestRowVersion: position.rowVersion,
|
|
467
|
-
acceptedMutationIds: input.mutations.map((mutation) => mutation.mutationId)
|
|
468
|
-
};
|
|
469
|
-
this.appliedEnvelopes.set(envelopeKey, response);
|
|
470
|
-
if (this.appliedEnvelopes.size > MAX_DEDUPLICATION_ENTRIES) {
|
|
471
|
-
const oldest = this.appliedEnvelopes.keys().next().value;
|
|
472
|
-
if (oldest) this.appliedEnvelopes.delete(oldest);
|
|
473
|
-
}
|
|
474
|
-
return response;
|
|
475
|
-
}
|
|
476
|
-
async handlePullSince(input, scopedAdapter) {
|
|
477
|
-
const adapter = scopedAdapter ?? this.adapterForCollection(input.collectionId, input.schemaVersion);
|
|
478
|
-
const current = await adapter.getStreamPosition?.(this.storageTarget(input.collectionId, input.cacheGenerationClaimId));
|
|
479
|
-
const position = current ? {
|
|
480
|
-
term: current.latestTerm,
|
|
481
|
-
seq: current.latestSeq,
|
|
482
|
-
rowVersion: current.latestRowVersion
|
|
483
|
-
} : await this.position(input.collectionId);
|
|
484
|
-
const result = await adapter.pullSince?.(this.storageTarget(input.collectionId, input.cacheGenerationClaimId), input.fromRowVersion);
|
|
485
|
-
if (!result || result.requiresFullReload) return {
|
|
486
|
-
type: "rpc:pullSince:res",
|
|
487
|
-
rpcId: input.rpcId,
|
|
488
|
-
ok: true,
|
|
489
|
-
latestTerm: result?.latestTerm ?? position.term,
|
|
490
|
-
latestSeq: result?.latestSeq ?? position.seq,
|
|
491
|
-
latestRowVersion: result?.latestRowVersion ?? position.rowVersion,
|
|
492
|
-
requiresFullReload: true
|
|
493
|
-
};
|
|
494
|
-
return {
|
|
495
|
-
type: "rpc:pullSince:res",
|
|
496
|
-
rpcId: input.rpcId,
|
|
497
|
-
ok: true,
|
|
498
|
-
latestTerm: result?.latestTerm ?? position.term,
|
|
499
|
-
latestSeq: result?.latestSeq ?? position.seq,
|
|
500
|
-
latestRowVersion: result.latestRowVersion,
|
|
501
|
-
requiresFullReload: false,
|
|
502
|
-
changedKeys: result.changedKeys,
|
|
503
|
-
deletedKeys: result.deletedKeys,
|
|
504
|
-
deltas: result.deltas
|
|
505
|
-
};
|
|
506
|
-
}
|
|
507
|
-
coordinationScope() {
|
|
508
|
-
return "scope" in this.coordination && typeof this.coordination.scope === "string" ? this.coordination.scope : "party-stack";
|
|
509
|
-
}
|
|
510
|
-
};
|
|
511
|
-
//#endregion
|
|
512
|
-
export { createPersistedCollectionCoordinator };
|
|
513
|
-
|
|
514
|
-
//# sourceMappingURL=createPersistedCollectionCoordinator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createPersistedCollectionCoordinator.js","names":[],"sources":["../../src/coordinator/createPersistedCollectionCoordinator.ts"],"sourcesContent":["import {\n isCoordinationHost,\n type Coordination,\n type CoordinationServiceClient,\n type CoordinationServiceServer,\n} from \"@party-stack/coordination\";\nimport {\n safeRandomUUID,\n eagerStorageTarget,\n managedStorageTarget,\n SingleProcessCoordinator,\n toTransportedLoadSubsetOptions,\n toPersistedCollectionDurabilityError,\n} from \"@tanstack/db-sqlite-persistence-core\";\nimport type { LoadSubsetOptions } from \"@tanstack/db\";\nimport type {\n ApplyLocalMutationsResponse,\n ApplyCommittedTxResponse,\n HydrationPersistenceAdapter,\n PersistedCollectionCoordinator,\n PersistedIndexSpec,\n PersistedMutationEnvelope,\n PersistenceAdapter,\n PersistedTx,\n RemoteSubsetOwner,\n ProtocolEnvelope,\n PullSinceResponse,\n ReplayableTxDelta,\n PersistedStorageTarget,\n} from \"@tanstack/db-sqlite-persistence-core\";\n\nconst PERSISTENCE_SERVICE = \"party-stack.persistence.v1\";\nconst MAX_DEDUPLICATION_ENTRIES = 1_000;\nconst MAX_PENDING_RELAYS = 1_000;\nconst MAX_COLLECTION_POSITIONS = 128;\n\ninterface CollectionPosition {\n term: number;\n seq: number;\n rowVersion: number;\n}\n\ninterface EnsureLeadershipInput {\n collectionId: string;\n}\n\ninterface EnsureRemoteSubsetInput {\n collectionId: string;\n options: LoadSubsetOptions;\n acquisitionId: string;\n}\n\ninterface ApplyCommittedTxInput {\n schemaVersion?: number;\n collectionId: string;\n tx: PersistedTx;\n}\n\ninterface EnsurePersistedIndexInput {\n cacheGenerationClaimId?: string;\n schemaVersion?: number;\n collectionId: string;\n signature: string;\n spec: PersistedIndexSpec;\n}\n\ninterface ApplyLocalMutationsInput {\n cacheGenerationClaimId?: string;\n schemaVersion?: number;\n collectionId: string;\n rpcId: string;\n envelopeId: string;\n mutations: PersistedMutationEnvelope[];\n}\n\ninterface PullSinceInput {\n cacheGenerationClaimId?: string;\n schemaVersion?: number;\n collectionId: string;\n rpcId: string;\n fromRowVersion: number;\n}\n\ninterface RelayMessageInput {\n collectionId: string;\n message: ProtocolEnvelope<unknown>;\n}\n\ninterface PersistenceCoordinationService {\n methods: {\n ensureLeadership(input: EnsureLeadershipInput): Promise<void>;\n ensureRemoteSubset(input: EnsureRemoteSubsetInput): Promise<void>;\n releaseRemoteSubset(input: EnsureRemoteSubsetInput): Promise<void>;\n applyCommittedTx(input: ApplyCommittedTxInput): Promise<ApplyCommittedTxResponse>;\n ensurePersistedIndex(input: EnsurePersistedIndexInput): Promise<void>;\n applyLocalMutations(input: ApplyLocalMutationsInput): Promise<ApplyLocalMutationsResponse>;\n pullSince(input: PullSinceInput): Promise<PullSinceResponse>;\n relayMessage(input: RelayMessageInput): Promise<void>;\n };\n events: {\n message: RelayMessageInput;\n };\n}\n\ntype AdapterWithPullSince = PersistenceAdapter & {\n pullSince?: (\n target: PersistedStorageTarget,\n fromRowVersion: number\n ) => Promise<\n | {\n latestRowVersion: number;\n latestTerm?: number;\n latestSeq?: number;\n requiresFullReload: true;\n }\n | {\n latestRowVersion: number;\n latestTerm?: number;\n latestSeq?: number;\n requiresFullReload: false;\n changedKeys: Array<string | number>;\n deletedKeys: Array<string | number>;\n deltas?: Array<ReplayableTxDelta<Record<string, unknown>, string | number>>;\n }\n >;\n};\n\nconst coordinatorCache = new WeakMap<\n Coordination,\n WeakMap<PersistenceAdapter, PersistedCollectionCoordinator>\n>();\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null;\n}\n\nfunction relayKey(collectionId: string, message: ProtocolEnvelope<unknown>): string {\n const payload = isRecord(message.payload) ? message.payload : {};\n return JSON.stringify([\n collectionId,\n message.senderId,\n payload.type,\n payload.txId,\n payload.resetEpoch,\n payload.term,\n payload.seq,\n ]);\n}\n\nexport function createPersistedCollectionCoordinator(\n coordination: Coordination,\n adapter: PersistenceAdapter\n): PersistedCollectionCoordinator {\n let adapters = coordinatorCache.get(coordination);\n if (!adapters) {\n adapters = new WeakMap();\n coordinatorCache.set(coordination, adapters);\n }\n const existing = adapters.get(adapter);\n if (existing) return existing;\n\n const created = new CoordinationPersistenceShim(coordination, adapter);\n adapters.set(adapter, created);\n return created;\n}\n\nclass CoordinationPersistenceShim implements PersistedCollectionCoordinator {\n private readonly nodeId = safeRandomUUID();\n private readonly collectionAdapters = new Map<string, AdapterWithPullSince>();\n private readonly collectionSubscriptions = new Map<string, number>();\n private readonly resumeChecks = new Map<string, Set<(force?: boolean) => Promise<void>>>();\n private readonly localMutationBursts = new Map<\n string,\n {\n pending: Set<string>;\n needsReload: boolean;\n timer?: ReturnType<typeof setTimeout>;\n }\n >();\n private stopResumeListeners: (() => void) | undefined;\n private checkingResume = false;\n private resumeRequested = false;\n\n private requestResumeCheck = (): void => {\n this.resumeRequested = true;\n if (this.checkingResume) return;\n this.checkingResume = true;\n void (async () => {\n try {\n while (this.resumeRequested && this.resumeChecks.size > 0) {\n this.resumeRequested = false;\n await Promise.all(\n [...this.resumeChecks.values()].flatMap((checks) =>\n [...checks].map((check) => check())\n )\n );\n }\n } finally {\n this.checkingResume = false;\n this.resumeRequested = false;\n }\n })();\n };\n\n private startResumeListeners(): void {\n if (this.stopResumeListeners || typeof window === \"undefined\" || typeof document === \"undefined\")\n return;\n const page = window;\n const visibility = document;\n const onVisible = () => {\n if (visibility.visibilityState === \"visible\") this.requestResumeCheck();\n };\n page.addEventListener(\"pageshow\", this.requestResumeCheck);\n page.addEventListener(\"focus\", onVisible);\n visibility.addEventListener(\"visibilitychange\", onVisible);\n this.stopResumeListeners = () => {\n page.removeEventListener(\"pageshow\", this.requestResumeCheck);\n page.removeEventListener(\"focus\", onVisible);\n visibility.removeEventListener(\"visibilitychange\", onVisible);\n };\n }\n\n setAdapterForCollection(\n collectionId: string,\n adapter: HydrationPersistenceAdapter,\n cacheGenerationClaimId?: string\n ): void {\n if (cacheGenerationClaimId) this.claims.set(collectionId, cacheGenerationClaimId);\n else this.claims.delete(collectionId);\n this.collectionAdapters.set(collectionId, adapter);\n this.positions.delete(collectionId);\n }\n\n private adapterForCollection(collectionId: string, schemaVersion?: number): AdapterWithPullSince {\n const adapter = this.collectionAdapters.get(collectionId) ?? this.adapter;\n if (schemaVersion !== undefined && this.schemaVersion(adapter) !== schemaVersion) {\n throw new Error(\n `Persistence schema mismatch for collection \"${collectionId}\". Reload the coordination host before writing through another schema version.`\n );\n }\n return adapter;\n }\n\n private schemaVersion(adapter: PersistenceAdapter): number | undefined {\n return \"schemaVersion\" in adapter && typeof adapter.schemaVersion === \"number\"\n ? adapter.schemaVersion\n : undefined;\n }\n private readonly claims = new Map<string, string>();\n private storageTarget(\n collectionId: string,\n claimId = this.claims.get(collectionId)\n ): PersistedStorageTarget {\n if (claimId) this.claims.set(collectionId, claimId);\n return claimId ? managedStorageTarget(collectionId, claimId) : eagerStorageTarget(collectionId);\n }\n private readonly positions = new Map<string, Promise<CollectionPosition>>();\n private readonly appliedEnvelopes = new Map<string, Extract<ApplyLocalMutationsResponse, { ok: true }>>();\n private readonly relayedMessages = new Set<string>();\n private readonly service: CoordinationServiceClient<PersistenceCoordinationService>;\n private readonly server: CoordinationServiceServer<PersistenceCoordinationService> | undefined;\n private pendingRelays = 0;\n private readonly remoteSubsets = new SingleProcessCoordinator();\n private readonly acquisitionIds = new WeakMap<LoadSubsetOptions, Map<string, string>>();\n private readonly remoteAcquisitions = new Map<\n string,\n {\n collectionId: string;\n options: LoadSubsetOptions;\n }\n >();\n\n constructor(\n private readonly coordination: Coordination,\n private readonly adapter: AdapterWithPullSince\n ) {\n this.service = coordination.service<PersistenceCoordinationService>(PERSISTENCE_SERVICE);\n this.server = isCoordinationHost(coordination)\n ? coordination.serve<PersistenceCoordinationService>(PERSISTENCE_SERVICE, {\n ensureLeadership: () => Promise.resolve(),\n ensureRemoteSubset: async (input) => {\n let options = this.remoteAcquisitions.get(input.acquisitionId)?.options;\n if (!options) {\n options = input.options;\n this.remoteAcquisitions.set(input.acquisitionId, {\n collectionId: input.collectionId,\n options,\n });\n }\n try {\n await this.remoteSubsets.requestEnsureRemoteSubset(input.collectionId, options);\n } catch (error) {\n // Owner invocation transfers a lease even if loading fails.\n // Release it before dropping our strong reference to its options.\n if (this.remoteAcquisitions.get(input.acquisitionId)?.options === options) {\n this.remoteAcquisitions.delete(input.acquisitionId);\n await this.remoteSubsets\n .requestReleaseRemoteSubset(input.collectionId, options)\n .catch(() => undefined);\n }\n throw error;\n }\n },\n releaseRemoteSubset: (input) => {\n const options = this.remoteAcquisitions.get(input.acquisitionId)?.options;\n this.remoteAcquisitions.delete(input.acquisitionId);\n return options\n ? this.remoteSubsets.requestReleaseRemoteSubset(input.collectionId, options)\n : Promise.resolve();\n },\n applyCommittedTx: (input) => this.applyCommittedTx(input),\n ensurePersistedIndex: (input) =>\n this.adapterForCollection(input.collectionId, input.schemaVersion).ensureIndex(\n this.storageTarget(input.collectionId, input.cacheGenerationClaimId),\n input.signature,\n input.spec\n ),\n applyLocalMutations: (input) => this.applyMutations(input),\n pullSince: (input) => this.handlePullSince(input),\n relayMessage: (input) => this.relayMessage(input),\n })\n : undefined;\n }\n\n getNodeId(): string {\n return this.nodeId;\n }\n\n subscribe(collectionId: string, callback: (message: ProtocolEnvelope<unknown>) => void): () => void {\n let active = true;\n let observed: { latestTerm: number; latestSeq: number; latestRowVersion: number } | undefined;\n const unsubscribe = this.service.events.subscribe(\"message\", (event) => {\n if (!active) return;\n if (event.collectionId === collectionId) {\n const payload = event.message.payload;\n const burst = this.localMutationBursts.get(collectionId);\n if (\n burst &&\n isRecord(payload) &&\n payload.type === \"tx:committed\" &&\n typeof payload.txId === \"string\" &&\n !burst.pending.has(payload.txId)\n ) {\n burst.needsReload = true;\n }\n if (\n isRecord(payload) &&\n payload.type === \"tx:committed\" &&\n typeof payload.term === \"number\" &&\n typeof payload.seq === \"number\" &&\n typeof payload.latestRowVersion === \"number\" &&\n (!observed ||\n payload.term > observed.latestTerm ||\n (payload.term === observed.latestTerm && payload.seq > observed.latestSeq))\n ) {\n observed = {\n latestTerm: payload.term,\n latestSeq: payload.seq,\n latestRowVersion: payload.latestRowVersion,\n };\n }\n callback(event.message);\n }\n });\n const checkResume = async (force = false) => {\n try {\n if (!active) return;\n const adapter = this.adapterForCollection(collectionId);\n const current = await adapter.getStreamPosition?.(this.storageTarget(collectionId));\n if (!active) return;\n if (\n !force &&\n current &&\n observed &&\n current.latestTerm === observed.latestTerm &&\n current.latestSeq === observed.latestSeq &&\n current.latestRowVersion === observed.latestRowVersion\n )\n return;\n // A local reset notification makes upstream reload active subsets atomically.\n // No storage reset or broadcast is performed. Before our first notification,\n // the hydration position is unknown, so a resume conservatively reloads once.\n callback({\n v: 1,\n dbName: this.coordinationScope(),\n collectionId,\n senderId: `resume:${this.nodeId}`,\n ts: Date.now(),\n payload: {\n type: \"collection:reset\",\n schemaVersion: this.schemaVersion(adapter) ?? 1,\n resetEpoch: 0,\n },\n });\n observed = current;\n } catch (error) {\n if (active) console.warn(`Failed to refresh resumed collection \"${collectionId}\".`, error);\n }\n };\n const checks = this.resumeChecks.get(collectionId) ?? new Set();\n checks.add(checkResume);\n this.resumeChecks.set(collectionId, checks);\n this.startResumeListeners();\n this.collectionSubscriptions.set(\n collectionId,\n (this.collectionSubscriptions.get(collectionId) ?? 0) + 1\n );\n return () => {\n if (!active) return;\n active = false;\n checks.delete(checkResume);\n if (checks.size === 0) {\n this.resumeChecks.delete(collectionId);\n const burst = this.localMutationBursts.get(collectionId);\n if (burst?.timer !== undefined) clearTimeout(burst.timer);\n this.localMutationBursts.delete(collectionId);\n }\n if (this.resumeChecks.size === 0) {\n this.stopResumeListeners?.();\n this.stopResumeListeners = undefined;\n }\n try {\n unsubscribe();\n } finally {\n const remaining = this.collectionSubscriptions.get(collectionId)! - 1;\n if (remaining > 0) {\n this.collectionSubscriptions.set(collectionId, remaining);\n } else {\n this.collectionSubscriptions.delete(collectionId);\n this.collectionAdapters.delete(collectionId);\n this.claims.delete(collectionId);\n this.positions.delete(collectionId);\n }\n }\n };\n }\n\n publish(collectionId: string, message: ProtocolEnvelope<unknown>): void {\n if (this.server && isCoordinationHost(this.coordination) && this.coordination.isLeader) {\n this.publishMessage({\n collectionId,\n message,\n });\n return;\n }\n if (this.pendingRelays >= MAX_PENDING_RELAYS) {\n console.warn(\n `Dropping persistence relay for \"${collectionId}\" because the pending relay limit was reached.`\n );\n return;\n }\n this.pendingRelays += 1;\n void this.service.methods\n .relayMessage({\n collectionId,\n message,\n })\n .catch((error: unknown) => {\n console.warn(`Failed to relay persistence message for \"${collectionId}\".`, error);\n })\n .finally(() => {\n this.pendingRelays -= 1;\n });\n }\n\n isLeader(): boolean {\n return isCoordinationHost(this.coordination) && this.coordination.isLeader;\n }\n\n ensureLeadership(): Promise<void> {\n return this.service.methods.ensureLeadership({\n collectionId: \"*\",\n });\n }\n\n requestEnsureRemoteSubset(collectionId: string, options: LoadSubsetOptions): Promise<void> {\n let collectionIds = this.acquisitionIds.get(options);\n if (!collectionIds) {\n collectionIds = new Map();\n this.acquisitionIds.set(options, collectionIds);\n }\n let acquisitionId = collectionIds.get(collectionId);\n if (!acquisitionId) {\n acquisitionId = safeRandomUUID();\n collectionIds.set(collectionId, acquisitionId);\n }\n return this.service.methods.ensureRemoteSubset({\n collectionId,\n options: toTransportedLoadSubsetOptions(options) as LoadSubsetOptions,\n acquisitionId,\n });\n }\n\n requestReleaseRemoteSubset(collectionId: string, options: LoadSubsetOptions): Promise<void> {\n const collectionIds = this.acquisitionIds.get(options);\n const acquisitionId = collectionIds?.get(collectionId);\n if (!acquisitionId) return Promise.resolve();\n collectionIds!.delete(collectionId);\n if (collectionIds!.size === 0) this.acquisitionIds.delete(options);\n return this.service.methods.releaseRemoteSubset({ collectionId, options: {}, acquisitionId });\n }\n\n registerRemoteSubsetOwner(collectionId: string, owner: RemoteSubsetOwner): () => void {\n const unregister = this.remoteSubsets.registerRemoteSubsetOwner(collectionId, owner);\n let registered = true;\n return () => {\n if (!registered) return;\n registered = false;\n unregister();\n for (const [id, acquisition] of this.remoteAcquisitions) {\n if (acquisition.collectionId === collectionId) this.remoteAcquisitions.delete(id);\n }\n };\n }\n\n requestApplyCommittedTx(\n collectionId: string,\n tx: PersistedTx,\n scopedAdapter?: HydrationPersistenceAdapter\n ): Promise<ApplyCommittedTxResponse> {\n return this.isLeader()\n ? this.applyCommittedTx({ collectionId, tx }, scopedAdapter)\n : this.service.methods.applyCommittedTx({\n collectionId,\n tx,\n schemaVersion:\n this.schemaVersion(scopedAdapter ?? this.adapterForCollection(collectionId)) ??\n this.schemaVersion(this.adapterForCollection(collectionId)),\n });\n }\n\n private async applyCommittedTx(\n { collectionId, tx, schemaVersion }: ApplyCommittedTxInput,\n scopedAdapter?: HydrationPersistenceAdapter\n ): Promise<ApplyCommittedTxResponse> {\n try {\n await (scopedAdapter ?? this.adapterForCollection(collectionId, schemaVersion)).applyCommittedTx(\n this.storageTarget(collectionId, tx.cacheGenerationClaimId),\n tx\n );\n } catch (error) {\n throw toPersistedCollectionDurabilityError(collectionId, error);\n }\n this.positions.delete(collectionId);\n return {\n type: \"rpc:applyCommittedTx:res\",\n rpcId: safeRandomUUID(),\n ok: true,\n term: tx.term,\n seq: tx.seq,\n latestRowVersion: tx.rowVersion,\n };\n }\n\n requestEnsurePersistedIndex(\n collectionId: string,\n signature: string,\n spec: PersistedIndexSpec,\n scopedAdapter?: HydrationPersistenceAdapter,\n localEnsureCompleted?: boolean,\n cacheGenerationClaimId?: string\n ): Promise<void> {\n if (this.isLeader()) {\n return localEnsureCompleted\n ? Promise.resolve()\n : (scopedAdapter ?? this.adapterForCollection(collectionId)).ensureIndex(\n this.storageTarget(collectionId, cacheGenerationClaimId),\n signature,\n spec\n );\n }\n return this.service.methods.ensurePersistedIndex({\n collectionId,\n signature,\n spec,\n cacheGenerationClaimId,\n schemaVersion:\n this.schemaVersion(scopedAdapter ?? this.adapterForCollection(collectionId)) ??\n this.schemaVersion(this.adapterForCollection(collectionId)),\n });\n }\n\n async requestApplyLocalMutations(\n collectionId: string,\n mutations: PersistedMutationEnvelope[],\n cacheGenerationClaimId?: string\n ): Promise<ApplyLocalMutationsResponse> {\n const envelopeId = safeRandomUUID();\n const burst = this.resumeChecks.has(collectionId)\n ? (this.localMutationBursts.get(collectionId) ?? {\n pending: new Set<string>(),\n needsReload: false,\n timer: undefined,\n })\n : undefined;\n if (burst) {\n if (burst.timer !== undefined) clearTimeout(burst.timer);\n if (burst.pending.size > 0) burst.needsReload = true;\n burst.pending.add(envelopeId);\n this.localMutationBursts.set(collectionId, burst);\n }\n try {\n return await this.service.methods.applyLocalMutations({\n collectionId,\n rpcId: safeRandomUUID(),\n envelopeId,\n schemaVersion: this.schemaVersion(this.adapterForCollection(collectionId)),\n mutations,\n cacheGenerationClaimId,\n });\n } finally {\n burst?.pending.delete(envelopeId);\n if (burst && burst.pending.size === 0 && this.localMutationBursts.get(collectionId) === burst) {\n // Upstream advances its observed sequence from local RPC acknowledgements\n // before queued commit messages can run. Reconcile overlapping foreign\n // commits after the local mutation burst, without changing RPC positions.\n burst.timer = setTimeout(() => {\n if (this.localMutationBursts.get(collectionId) !== burst) return;\n this.localMutationBursts.delete(collectionId);\n if (burst.needsReload) {\n for (const check of this.resumeChecks.get(collectionId) ?? []) void check(true);\n }\n }, 0);\n }\n }\n }\n\n pullSince(\n collectionId: string,\n fromRowVersion: number,\n scopedAdapter?: HydrationPersistenceAdapter,\n cacheGenerationClaimId?: string\n ): Promise<PullSinceResponse> {\n this.storageTarget(collectionId, cacheGenerationClaimId);\n const input = {\n collectionId,\n rpcId: safeRandomUUID(),\n fromRowVersion,\n cacheGenerationClaimId,\n schemaVersion: this.schemaVersion(this.adapterForCollection(collectionId)),\n };\n return scopedAdapter && this.isLeader()\n ? this.handlePullSince(input, scopedAdapter)\n : this.service.methods.pullSince(input);\n }\n\n private relayMessage(input: RelayMessageInput): Promise<void> {\n const key = relayKey(input.collectionId, input.message);\n if (this.relayedMessages.has(key)) {\n return Promise.resolve();\n }\n this.relayedMessages.add(key);\n if (this.relayedMessages.size > MAX_DEDUPLICATION_ENTRIES) {\n const oldest = this.relayedMessages.values().next().value;\n if (oldest) {\n this.relayedMessages.delete(oldest);\n }\n }\n this.publishMessage(input);\n return Promise.resolve();\n }\n\n private publishMessage(input: RelayMessageInput): void {\n this.server?.events.publish(\"message\", input);\n }\n\n private async position(collectionId: string): Promise<CollectionPosition> {\n let position = this.positions.get(collectionId);\n if (!position) {\n const adapter = this.adapterForCollection(collectionId);\n position = Promise.resolve(\n adapter.getStreamPosition\n ? adapter.getStreamPosition(this.storageTarget(collectionId))\n : adapter.loadResumeSnapshot(this.storageTarget(collectionId), { includeRows: false })\n ).then((current) => ({\n term: (current?.latestTerm ?? 0) + 1,\n seq: current?.latestSeq ?? 0,\n rowVersion: current?.latestRowVersion ?? 0,\n }));\n }\n this.positions.delete(collectionId);\n this.positions.set(collectionId, position);\n while (this.positions.size > MAX_COLLECTION_POSITIONS) {\n const oldest = this.positions.keys().next().value;\n if (oldest !== undefined) this.positions.delete(oldest);\n }\n void position.catch(() => {\n if (this.positions.get(collectionId) === position) this.positions.delete(collectionId);\n });\n return position;\n }\n\n private async applyMutations(input: ApplyLocalMutationsInput): Promise<ApplyLocalMutationsResponse> {\n const adapter = this.adapterForCollection(input.collectionId, input.schemaVersion);\n const envelopeKey = JSON.stringify([input.collectionId, input.envelopeId]);\n const previous = this.appliedEnvelopes.get(envelopeKey);\n if (previous) {\n return {\n ...previous,\n rpcId: input.rpcId,\n };\n }\n\n this.storageTarget(input.collectionId, input.cacheGenerationClaimId);\n const position = await this.position(input.collectionId);\n const nextPosition = { ...position, seq: position.seq + 1, rowVersion: position.rowVersion + 1 };\n const txId = input.envelopeId;\n await adapter.applyCommittedTx(this.storageTarget(input.collectionId, input.cacheGenerationClaimId), {\n cacheGenerationClaimId: input.cacheGenerationClaimId,\n txId,\n term: nextPosition.term,\n seq: nextPosition.seq,\n rowVersion: nextPosition.rowVersion,\n mutations: input.mutations.map(({ mutationId, ...mutation }) => {\n void mutationId;\n return mutation;\n }),\n });\n Object.assign(position, nextPosition);\n const committed: ProtocolEnvelope<unknown> = {\n v: 1,\n dbName: this.coordinationScope(),\n collectionId: input.collectionId,\n senderId: this.nodeId,\n ts: Date.now(),\n payload: {\n type: \"tx:committed\",\n term: position.term,\n seq: position.seq,\n txId,\n latestRowVersion: position.rowVersion,\n requiresFullReload: false,\n changedRows: input.mutations\n .filter((mutation) => mutation.type !== \"delete\")\n .map((mutation) => ({\n key: mutation.key,\n value: mutation.value,\n })),\n deletedKeys: input.mutations\n .filter((mutation) => mutation.type === \"delete\")\n .map((mutation) => mutation.key),\n rowMetadataMutations: input.mutations.flatMap((mutation) =>\n mutation.type !== \"delete\" &&\n (mutation.metadataChanged || mutation.metadata !== undefined)\n ? [\n mutation.metadata === undefined\n ? { type: \"delete\" as const, key: mutation.key }\n : { type: \"set\" as const, key: mutation.key, value: mutation.metadata },\n ]\n : []\n ),\n },\n };\n this.publishMessage({\n collectionId: input.collectionId,\n message: committed,\n });\n\n const response: Extract<ApplyLocalMutationsResponse, { ok: true }> = {\n type: \"rpc:applyLocalMutations:res\",\n rpcId: input.rpcId,\n ok: true,\n term: position.term,\n seq: position.seq,\n latestRowVersion: position.rowVersion,\n acceptedMutationIds: input.mutations.map((mutation) => mutation.mutationId),\n };\n this.appliedEnvelopes.set(envelopeKey, response);\n if (this.appliedEnvelopes.size > MAX_DEDUPLICATION_ENTRIES) {\n const oldest = this.appliedEnvelopes.keys().next().value;\n if (oldest) {\n this.appliedEnvelopes.delete(oldest);\n }\n }\n return response;\n }\n\n private async handlePullSince(\n input: PullSinceInput,\n scopedAdapter?: HydrationPersistenceAdapter\n ): Promise<PullSinceResponse> {\n const adapter: AdapterWithPullSince =\n scopedAdapter ?? this.adapterForCollection(input.collectionId, input.schemaVersion);\n const current = await adapter.getStreamPosition?.(\n this.storageTarget(input.collectionId, input.cacheGenerationClaimId)\n );\n const position = current\n ? { term: current.latestTerm, seq: current.latestSeq, rowVersion: current.latestRowVersion }\n : await this.position(input.collectionId);\n const result = await adapter.pullSince?.(\n this.storageTarget(input.collectionId, input.cacheGenerationClaimId),\n input.fromRowVersion\n );\n if (!result || result.requiresFullReload) {\n return {\n type: \"rpc:pullSince:res\",\n rpcId: input.rpcId,\n ok: true,\n latestTerm: result?.latestTerm ?? position.term,\n latestSeq: result?.latestSeq ?? position.seq,\n latestRowVersion: result?.latestRowVersion ?? position.rowVersion,\n requiresFullReload: true,\n };\n }\n return {\n type: \"rpc:pullSince:res\",\n rpcId: input.rpcId,\n ok: true,\n latestTerm: result?.latestTerm ?? position.term,\n latestSeq: result?.latestSeq ?? position.seq,\n latestRowVersion: result.latestRowVersion,\n requiresFullReload: false,\n changedKeys: result.changedKeys,\n deletedKeys: result.deletedKeys,\n deltas: result.deltas,\n };\n }\n\n private coordinationScope(): string {\n return \"scope\" in this.coordination && typeof this.coordination.scope === \"string\"\n ? this.coordination.scope\n : \"party-stack\";\n }\n}\n"],"mappings":";;;AA+BA,MAAM,sBAAsB;AAC5B,MAAM,4BAA4B;AAClC,MAAM,qBAAqB;AAC3B,MAAM,2BAA2B;AA6FjC,MAAM,mCAAmB,IAAI,QAG3B;AAEF,SAAS,SAAS,OAAkD;CAChE,OAAO,OAAO,UAAU,YAAY,UAAU;AAClD;AAEA,SAAS,SAAS,cAAsB,SAA4C;CAChF,MAAM,UAAU,SAAS,QAAQ,OAAO,IAAI,QAAQ,UAAU,CAAC;CAC/D,OAAO,KAAK,UAAU;EAClB;EACA,QAAQ;EACR,QAAQ;EACR,QAAQ;EACR,QAAQ;EACR,QAAQ;EACR,QAAQ;CACZ,CAAC;AACL;AAEA,SAAgB,qCACZ,cACA,SAC8B;CAC9B,IAAI,WAAW,iBAAiB,IAAI,YAAY;CAChD,IAAI,CAAC,UAAU;EACX,2BAAW,IAA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,OAAO,2BAA2B;GACxD,MAAM,SAAS,KAAK,iBAAiB,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;GACnD,IAAI,QACA,KAAK,iBAAiB,OAAO,MAAM;EAE3C;EACA,OAAO;CACX;CAEA,MAAc,gBACV,OACA,eAC0B;EAC1B,MAAM,UACF,iBAAiB,KAAK,qBAAqB,MAAM,cAAc,MAAM,aAAa;EACtF,MAAM,UAAU,MAAM,QAAQ,oBAC1B,KAAK,cAAc,MAAM,cAAc,MAAM,sBAAsB,CACvE;EACA,MAAM,WAAW,UACX;GAAE,MAAM,QAAQ;GAAY,KAAK,QAAQ;GAAW,YAAY,QAAQ;EAAiB,IACzF,MAAM,KAAK,SAAS,MAAM,YAAY;EAC5C,MAAM,SAAS,MAAM,QAAQ,YACzB,KAAK,cAAc,MAAM,cAAc,MAAM,sBAAsB,GACnE,MAAM,cACV;EACA,IAAI,CAAC,UAAU,OAAO,oBAClB,OAAO;GACH,MAAM;GACN,OAAO,MAAM;GACb,IAAI;GACJ,YAAY,QAAQ,cAAc,SAAS;GAC3C,WAAW,QAAQ,aAAa,SAAS;GACzC,kBAAkB,QAAQ,oBAAoB,SAAS;GACvD,oBAAoB;EACxB;EAEJ,OAAO;GACH,MAAM;GACN,OAAO,MAAM;GACb,IAAI;GACJ,YAAY,QAAQ,cAAc,SAAS;GAC3C,WAAW,QAAQ,aAAa,SAAS;GACzC,kBAAkB,OAAO;GACzB,oBAAoB;GACpB,aAAa,OAAO;GACpB,aAAa,OAAO;GACpB,QAAQ,OAAO;EACnB;CACJ;CAEA,oBAAoC;EAChC,OAAO,WAAW,KAAK,gBAAgB,OAAO,KAAK,aAAa,UAAU,WACpE,KAAK,aAAa,QAClB;CACV;AACJ"}
|
package/lib/coordinator/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { createPersistedCollectionCoordinator } from "./createPersistedCollectionCoordinator.js";
|
package/lib/effection/index.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { RuntimeAdapter, RuntimeAdapterProvider } from "../types.js";
|
|
2
|
-
import { Operation, Stream } from "effection";
|
|
3
|
-
//#region src/effection/index.d.ts
|
|
4
|
-
export declare function useRuntimeAdapter(provider: RuntimeAdapter | RuntimeAdapterProvider, owner: string, namespace: string): Operation<RuntimeAdapter>;
|
|
5
|
-
export declare function useConnectivityChanges(runtime: RuntimeAdapter): Operation<Stream<boolean, void>>;
|
|
6
|
-
//#endregion
|
|
7
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/effection/index.ts"],"mappings":";;;wBAGgB,kBACZ,UAAU,iBAAiB,wBAC3B,eACA,oBACD,UAAU;wBAcG,uBAAuB,SAAS,iBAAiB,UAAU"}
|
package/lib/effection/index.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { createSignal, resource, until } from "effection";
|
|
2
|
-
//#region src/effection/index.ts
|
|
3
|
-
function useRuntimeAdapter(provider, owner, namespace) {
|
|
4
|
-
return resource(function* (provide) {
|
|
5
|
-
const runtime = typeof provider === "function" ? yield* until(Promise.resolve(provider(owner, namespace))) : provider;
|
|
6
|
-
try {
|
|
7
|
-
yield* provide(runtime);
|
|
8
|
-
} finally {
|
|
9
|
-
yield* until(Promise.resolve(runtime.cleanup?.()));
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
function useConnectivityChanges(runtime) {
|
|
14
|
-
return resource(function* (provide) {
|
|
15
|
-
const signal = createSignal();
|
|
16
|
-
const unsubscribe = runtime.connectivity?.subscribe((isConnected) => signal.send(isConnected));
|
|
17
|
-
try {
|
|
18
|
-
yield* provide(signal);
|
|
19
|
-
} finally {
|
|
20
|
-
unsubscribe?.();
|
|
21
|
-
signal.close();
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
//#endregion
|
|
26
|
-
export { useConnectivityChanges, useRuntimeAdapter };
|
|
27
|
-
|
|
28
|
-
//# sourceMappingURL=index.js.map
|