@terreno/syncdb 58.0.0-beta.3 → 58.0.0-beta.4
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/CHANGELOG.md +99 -0
- package/dist/api/client.d.ts +6 -0
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +8 -0
- package/dist/api/client.js.map +1 -1
- package/dist/api/client.test.js +52 -0
- package/dist/api/client.test.js.map +1 -1
- package/dist/app/app.d.ts +6 -5
- package/dist/app/app.d.ts.map +1 -1
- package/dist/app/app.js +6 -5
- package/dist/app/app.js.map +1 -1
- package/dist/auth/session.d.ts +6 -0
- package/dist/auth/session.d.ts.map +1 -1
- package/dist/auth/session.js +21 -1
- package/dist/auth/session.js.map +1 -1
- package/dist/auth/session.test.js +148 -0
- package/dist/auth/session.test.js.map +1 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +2 -0
- package/dist/client.js.map +1 -1
- package/dist/client.test.js +1 -0
- package/dist/client.test.js.map +1 -1
- package/dist/isolated/appEntryWithoutMcp.isolated.d.ts +2 -0
- package/dist/isolated/appEntryWithoutMcp.isolated.d.ts.map +1 -0
- package/dist/isolated/appEntryWithoutMcp.isolated.js +21 -0
- package/dist/isolated/appEntryWithoutMcp.isolated.js.map +1 -0
- package/dist/storage/schema.d.ts.map +1 -1
- package/dist/storage/schema.js +6 -0
- package/dist/storage/schema.js.map +1 -1
- package/dist/storage/store.d.ts +6 -0
- package/dist/storage/store.d.ts.map +1 -1
- package/dist/storage/store.js +25 -10
- package/dist/storage/store.js.map +1 -1
- package/dist/storage/store.test.js +18 -0
- package/dist/storage/store.test.js.map +1 -1
- package/dist/storage/types.d.ts +11 -0
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/sync/deltaApplier.d.ts +3 -2
- package/dist/sync/deltaApplier.d.ts.map +1 -1
- package/dist/sync/deltaApplier.js +13 -3
- package/dist/sync/deltaApplier.js.map +1 -1
- package/dist/sync/deltaApplier.test.js +52 -0
- package/dist/sync/deltaApplier.test.js.map +1 -1
- package/dist/sync/replayCoordinator.d.ts.map +1 -1
- package/dist/sync/replayCoordinator.js +4 -0
- package/dist/sync/replayCoordinator.js.map +1 -1
- package/dist/sync/replayCoordinator.test.js +1 -0
- package/dist/sync/replayCoordinator.test.js.map +1 -1
- package/dist/sync/windowHydrate.d.ts +9 -7
- package/dist/sync/windowHydrate.d.ts.map +1 -1
- package/dist/sync/windowHydrate.js +20 -10
- package/dist/sync/windowHydrate.js.map +1 -1
- package/dist/sync/windowHydrate.test.js +189 -2
- package/dist/sync/windowHydrate.test.js.map +1 -1
- package/package.json +8 -4
- package/src/api/client.test.ts +73 -0
- package/src/api/client.ts +15 -0
- package/src/app/app.ts +6 -10
- package/src/auth/session.test.ts +177 -0
- package/src/auth/session.ts +26 -1
- package/src/client.test.ts +1 -0
- package/src/client.ts +2 -0
- package/src/isolated/appEntryWithoutMcp.isolated.ts +23 -0
- package/src/storage/schema.ts +6 -0
- package/src/storage/store.test.ts +19 -0
- package/src/storage/store.ts +33 -10
- package/src/storage/types.ts +11 -0
- package/src/sync/deltaApplier.test.ts +55 -0
- package/src/sync/deltaApplier.ts +15 -3
- package/src/sync/replayCoordinator.test.ts +1 -0
- package/src/sync/replayCoordinator.ts +4 -0
- package/src/sync/windowHydrate.test.ts +225 -3
- package/src/sync/windowHydrate.ts +23 -13
|
@@ -2,11 +2,64 @@ import {describe, it} from "bun:test";
|
|
|
2
2
|
import {assert} from "chai";
|
|
3
3
|
import {DateTime} from "luxon";
|
|
4
4
|
|
|
5
|
-
import {createSyncStore} from "../storage/store";
|
|
5
|
+
import {createSyncStore, type SyncStore} from "../storage/store";
|
|
6
|
+
import type {SyncEntitiesResponse} from "../types";
|
|
7
|
+
import {applyDelta} from "./deltaApplier";
|
|
6
8
|
import {hydrateWindowEntities} from "./windowHydrate";
|
|
7
9
|
|
|
8
10
|
const STREAM = "todos|admin";
|
|
9
11
|
|
|
12
|
+
interface HeldFetch {
|
|
13
|
+
promise: Promise<SyncEntitiesResponse>;
|
|
14
|
+
resolve: (response: SyncEntitiesResponse) => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const holdFetch = (): HeldFetch => {
|
|
18
|
+
let resolve: (response: SyncEntitiesResponse) => void = () => {};
|
|
19
|
+
const promise = new Promise<SyncEntitiesResponse>((res) => {
|
|
20
|
+
resolve = res;
|
|
21
|
+
});
|
|
22
|
+
return {promise, resolve};
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// Two overlapping window hydrates (e.g. a re-query while the first is in flight), both holding a
|
|
26
|
+
// REST page read before a PATCH; the second /sync/entities read lands after the server committed it.
|
|
27
|
+
const runOverlappingHydrates = async (store: SyncStore): Promise<void> => {
|
|
28
|
+
const first = holdFetch();
|
|
29
|
+
const second = holdFetch();
|
|
30
|
+
const call1 = hydrateWindowEntities({
|
|
31
|
+
channel: {fetchEntities: () => first.promise},
|
|
32
|
+
collection: "todos",
|
|
33
|
+
ids: ["t1"],
|
|
34
|
+
restRows: {t1: {title: "old"}},
|
|
35
|
+
store,
|
|
36
|
+
});
|
|
37
|
+
const call2 = hydrateWindowEntities({
|
|
38
|
+
channel: {fetchEntities: () => second.promise},
|
|
39
|
+
collection: "todos",
|
|
40
|
+
ids: ["t1"],
|
|
41
|
+
restRows: {t1: {title: "old"}},
|
|
42
|
+
store,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
first.resolve({entities: [{data: {title: "old"}, deleted: false, id: "t1", seq: 263}]});
|
|
46
|
+
await call1;
|
|
47
|
+
second.resolve({entities: [{data: {title: "new"}, deleted: false, id: "t1", seq: 264}]});
|
|
48
|
+
await call2;
|
|
49
|
+
|
|
50
|
+
applyDelta({
|
|
51
|
+
delta: {
|
|
52
|
+
collection: "todos",
|
|
53
|
+
data: {title: "new"},
|
|
54
|
+
id: "t1",
|
|
55
|
+
method: "update",
|
|
56
|
+
seq: 264,
|
|
57
|
+
stream: STREAM,
|
|
58
|
+
},
|
|
59
|
+
store,
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
10
63
|
describe("hydrateWindowEntities", () => {
|
|
11
64
|
it("hydrates 2 of 3 ids and ignores the unknown id", async () => {
|
|
12
65
|
const store = createSyncStore({collections: ["todos"]});
|
|
@@ -62,7 +115,8 @@ describe("hydrateWindowEntities", () => {
|
|
|
62
115
|
assert.deepEqual(fetched, [["a", "b"]]);
|
|
63
116
|
const restRow = store.getEntity<{title: string}>({collection: "todos", id: "a"});
|
|
64
117
|
const entityRow = store.getEntity<{title: string}>({collection: "todos", id: "b"});
|
|
65
|
-
|
|
118
|
+
// The fetched entity replaces the REST row: `/sync/entities` is read after the REST page.
|
|
119
|
+
assert.equal(restRow?.data.title, "canonical-a");
|
|
66
120
|
assert.equal(restRow?.seq, 7);
|
|
67
121
|
assert.equal(entityRow?.data.title, "from-entities");
|
|
68
122
|
assert.equal(entityRow?.seq, 9);
|
|
@@ -113,7 +167,7 @@ describe("hydrateWindowEntities", () => {
|
|
|
113
167
|
});
|
|
114
168
|
|
|
115
169
|
const row = store.getEntity<{title: string}>({collection: "todos", id: "a"});
|
|
116
|
-
assert.equal(row?.data.title, "
|
|
170
|
+
assert.equal(row?.data.title, "gone");
|
|
117
171
|
assert.equal(row?.seq, 12);
|
|
118
172
|
assert.isTrue(row?.deleted);
|
|
119
173
|
});
|
|
@@ -176,4 +230,172 @@ describe("hydrateWindowEntities", () => {
|
|
|
176
230
|
assert.equal(row?.seq, 3);
|
|
177
231
|
assert.equal(row?.pendingMutationId, "m-pending");
|
|
178
232
|
});
|
|
233
|
+
|
|
234
|
+
describe("when the store already holds a server-seq entity", () => {
|
|
235
|
+
const seed = (store: ReturnType<typeof createSyncStore>, seq: number, title: string): void => {
|
|
236
|
+
store.upsertEntity({collection: "todos", data: {title}, id: "t1", seq, stream: STREAM});
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
it("never overwrites a stored seq>0 entity with a stale REST row, and keeps it on equal fetched seq", async () => {
|
|
240
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
241
|
+
seed(store, 5, "new");
|
|
242
|
+
let titleDuringFetch: string | undefined;
|
|
243
|
+
|
|
244
|
+
await hydrateWindowEntities({
|
|
245
|
+
channel: {
|
|
246
|
+
fetchEntities: async () => {
|
|
247
|
+
titleDuringFetch = store.getEntity<{title: string}>({collection: "todos", id: "t1"})
|
|
248
|
+
?.data.title;
|
|
249
|
+
return {entities: [{data: {title: "new"}, deleted: false, id: "t1", seq: 5}]};
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
collection: "todos",
|
|
253
|
+
ids: ["t1"],
|
|
254
|
+
restRows: {t1: {title: "old"}},
|
|
255
|
+
store,
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
assert.equal(titleDuringFetch, "new");
|
|
259
|
+
const row = store.getEntity<{title: string}>({collection: "todos", id: "t1"});
|
|
260
|
+
assert.equal(row?.data.title, "new");
|
|
261
|
+
assert.equal(row?.seq, 5);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("keeps stored canonical data when the fetched seq equals the stored seq", async () => {
|
|
265
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
266
|
+
seed(store, 5, "new");
|
|
267
|
+
|
|
268
|
+
await hydrateWindowEntities({
|
|
269
|
+
channel: {
|
|
270
|
+
fetchEntities: async () => ({
|
|
271
|
+
entities: [{data: {title: "same-seq-other"}, deleted: false, id: "t1", seq: 5}],
|
|
272
|
+
}),
|
|
273
|
+
},
|
|
274
|
+
collection: "todos",
|
|
275
|
+
ids: ["t1"],
|
|
276
|
+
restRows: {t1: {title: "old"}},
|
|
277
|
+
store,
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
const row = store.getEntity<{title: string}>({collection: "todos", id: "t1"});
|
|
281
|
+
assert.equal(row?.data.title, "new");
|
|
282
|
+
assert.equal(row?.seq, 5);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("keeps the stored entity when the fetched seq is older", async () => {
|
|
286
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
287
|
+
seed(store, 5, "new");
|
|
288
|
+
|
|
289
|
+
await hydrateWindowEntities({
|
|
290
|
+
channel: {
|
|
291
|
+
fetchEntities: async () => ({
|
|
292
|
+
entities: [{data: {title: "stale"}, deleted: false, id: "t1", seq: 4}],
|
|
293
|
+
}),
|
|
294
|
+
},
|
|
295
|
+
collection: "todos",
|
|
296
|
+
ids: ["t1"],
|
|
297
|
+
restRows: {t1: {title: "old"}},
|
|
298
|
+
store,
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
const row = store.getEntity<{title: string}>({collection: "todos", id: "t1"});
|
|
302
|
+
assert.equal(row?.data.title, "new");
|
|
303
|
+
assert.equal(row?.seq, 5);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("takes fetched data and seq when the fetched seq is newer than the stored seq", async () => {
|
|
307
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
308
|
+
seed(store, 2, "older");
|
|
309
|
+
|
|
310
|
+
await hydrateWindowEntities({
|
|
311
|
+
channel: {
|
|
312
|
+
fetchEntities: async () => ({
|
|
313
|
+
entities: [{data: {title: "canon-new"}, deleted: false, id: "t1", seq: 6}],
|
|
314
|
+
}),
|
|
315
|
+
},
|
|
316
|
+
collection: "todos",
|
|
317
|
+
ids: ["t1"],
|
|
318
|
+
restRows: {t1: {title: "rest-new"}},
|
|
319
|
+
store,
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
const row = store.getEntity<{title: string}>({collection: "todos", id: "t1"});
|
|
323
|
+
assert.equal(row?.seq, 6);
|
|
324
|
+
assert.equal(row?.data.title, "canon-new");
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
// Replaces "keeps REST data once the fetched seq lands": /sync/entities is always read after the
|
|
329
|
+
// REST page, so a fetched seq newer than the stored one carries data at least as new as REST.
|
|
330
|
+
it("fills an id the store lacks from REST, then takes the fetched data once a newer seq lands", async () => {
|
|
331
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
332
|
+
|
|
333
|
+
await hydrateWindowEntities({
|
|
334
|
+
channel: {
|
|
335
|
+
fetchEntities: async () => ({
|
|
336
|
+
entities: [{data: {title: "canon"}, deleted: false, id: "t1", seq: 3}],
|
|
337
|
+
}),
|
|
338
|
+
},
|
|
339
|
+
collection: "todos",
|
|
340
|
+
ids: ["t1"],
|
|
341
|
+
restRows: {t1: {title: "rest"}},
|
|
342
|
+
store,
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
const row = store.getEntity<{title: string}>({collection: "todos", id: "t1"});
|
|
346
|
+
assert.equal(row?.seq, 3);
|
|
347
|
+
assert.equal(row?.data.title, "canon");
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
describe("two overlapping hydrates race a PATCH", () => {
|
|
351
|
+
it("ends at the fetched seq 264 data when the store started without the entity", async () => {
|
|
352
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
353
|
+
|
|
354
|
+
await runOverlappingHydrates(store);
|
|
355
|
+
|
|
356
|
+
const row = store.getEntity<{title: string}>({collection: "todos", id: "t1"});
|
|
357
|
+
assert.equal(row?.data.title, "new");
|
|
358
|
+
assert.equal(row?.seq, 264);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it("ends at the fetched seq 264 data when the store held a REST-only fill at seq 0", async () => {
|
|
362
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
363
|
+
store.upsertEntity({collection: "todos", data: {title: "old"}, id: "t1", stream: STREAM});
|
|
364
|
+
|
|
365
|
+
await runOverlappingHydrates(store);
|
|
366
|
+
|
|
367
|
+
const row = store.getEntity<{title: string}>({collection: "todos", id: "t1"});
|
|
368
|
+
assert.equal(row?.data.title, "new");
|
|
369
|
+
assert.equal(row?.seq, 264);
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it("leaves a pending-mutation row untouched even when REST and a newer fetched seq differ", async () => {
|
|
374
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
375
|
+
store.upsertEntity({
|
|
376
|
+
collection: "todos",
|
|
377
|
+
data: {title: "local"},
|
|
378
|
+
id: "t1",
|
|
379
|
+
pendingMutationId: "m1",
|
|
380
|
+
seq: 5,
|
|
381
|
+
stream: STREAM,
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
await hydrateWindowEntities({
|
|
385
|
+
channel: {
|
|
386
|
+
fetchEntities: async () => ({
|
|
387
|
+
entities: [{data: {title: "canon-new"}, deleted: false, id: "t1", seq: 9}],
|
|
388
|
+
}),
|
|
389
|
+
},
|
|
390
|
+
collection: "todos",
|
|
391
|
+
ids: ["t1"],
|
|
392
|
+
restRows: {t1: {title: "rest"}},
|
|
393
|
+
store,
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
const row = store.getEntity<{title: string}>({collection: "todos", id: "t1"});
|
|
397
|
+
assert.equal(row?.data.title, "local");
|
|
398
|
+
assert.equal(row?.seq, 5);
|
|
399
|
+
assert.equal(row?.pendingMutationId, "m1");
|
|
400
|
+
});
|
|
179
401
|
});
|
|
@@ -11,9 +11,9 @@ export interface HydrateWindowEntitiesArgs {
|
|
|
11
11
|
collection: string;
|
|
12
12
|
ids: string[];
|
|
13
13
|
/**
|
|
14
|
-
* Optional REST membership payloads keyed by id.
|
|
15
|
-
* for fast UI; every requested id is still fetched from
|
|
16
|
-
* before this call resolves
|
|
14
|
+
* Optional REST membership payloads keyed by id. For ids the store does not hold yet they
|
|
15
|
+
* upsert immediately for fast UI; every requested id is still fetched from
|
|
16
|
+
* `GET /sync/entities` before this call resolves, and that canonical entity replaces them.
|
|
17
17
|
*/
|
|
18
18
|
restRows?: Record<string, unknown>;
|
|
19
19
|
}
|
|
@@ -23,10 +23,12 @@ export interface HydrateWindowEntitiesResult {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
|
-
* Upsert a window of known ids into TinyBase. REST rows render immediately
|
|
27
|
-
* canonical seq
|
|
28
|
-
*
|
|
29
|
-
*
|
|
26
|
+
* Upsert a window of known ids into TinyBase. REST rows render immediately for ids the store
|
|
27
|
+
* lacks; the canonical entity (data, seq, deleted) from `GET /sync/entities` then replaces any
|
|
28
|
+
* stored version older than it, and never a version at least as new (a live delta that landed
|
|
29
|
+
* first). Overlapping calls stay safe: a stale REST page can never be stored under a newer seq.
|
|
30
|
+
* Server ids that `/sync/entities` does not return are ignored (no empty rows) unless a REST
|
|
31
|
+
* row already landed.
|
|
30
32
|
*/
|
|
31
33
|
export const hydrateWindowEntities = async ({
|
|
32
34
|
store,
|
|
@@ -52,6 +54,11 @@ export const hydrateWindowEntities = async ({
|
|
|
52
54
|
if (existing?.pendingMutationId) {
|
|
53
55
|
continue;
|
|
54
56
|
}
|
|
57
|
+
// REST rows only fill ids the store lacks. An entity with a server seq (from a live delta or
|
|
58
|
+
// an earlier hydrate) is at least as new as a REST page read before it, so it is kept.
|
|
59
|
+
if (existing && existing.seq > 0) {
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
55
62
|
store.upsertEntity({
|
|
56
63
|
collection,
|
|
57
64
|
data: restData,
|
|
@@ -72,17 +79,20 @@ export const hydrateWindowEntities = async ({
|
|
|
72
79
|
if (existing?.pendingMutationId) {
|
|
73
80
|
continue;
|
|
74
81
|
}
|
|
75
|
-
// A live `{collection}|admin` delta can land while this fetch is in flight
|
|
76
|
-
//
|
|
77
|
-
// seq
|
|
78
|
-
|
|
82
|
+
// A live `{collection}|admin` delta can land while this fetch is in flight, and an entity
|
|
83
|
+
// the store already holds at this seq is the same version. Writing the fetched snapshot
|
|
84
|
+
// back would rewind data, seq or deleted and make the next mutate conflict against a stale
|
|
85
|
+
// baseline.
|
|
86
|
+
if (existing && existing.seq > 0 && existing.seq >= entity.seq) {
|
|
79
87
|
hydrated.add(entity.id);
|
|
80
88
|
continue;
|
|
81
89
|
}
|
|
82
|
-
const restData = restRows?.[entity.id];
|
|
83
90
|
store.upsertEntity({
|
|
84
91
|
collection,
|
|
85
|
-
|
|
92
|
+
// `/sync/entities` is read after the REST page, so a newer fetched entity is never older
|
|
93
|
+
// than a REST row; keeping REST data here could store a pre-update title under the new
|
|
94
|
+
// seq, and the live delta for that seq would then be skipped as already applied.
|
|
95
|
+
data: entity.data,
|
|
86
96
|
deleted: entity.deleted,
|
|
87
97
|
id: entity.id,
|
|
88
98
|
pendingMutationId: "",
|