@terreno/syncdb 57.13.0 → 57.15.0
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/README.md +6 -3
- package/dist/client.d.ts +12 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +304 -56
- package/dist/client.js.map +1 -1
- package/dist/client.test.js +255 -0
- package/dist/client.test.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/persisters/encryptedIndexedDbPersister.d.ts +4 -3
- package/dist/persisters/encryptedIndexedDbPersister.d.ts.map +1 -1
- package/dist/persisters/encryptedIndexedDbPersister.js +21 -16
- package/dist/persisters/encryptedIndexedDbPersister.js.map +1 -1
- package/dist/persisters/encryptedIndexedDbPersister.test.js +40 -1
- package/dist/persisters/encryptedIndexedDbPersister.test.js.map +1 -1
- package/dist/sync/bootstrap.d.ts +34 -3
- package/dist/sync/bootstrap.d.ts.map +1 -1
- package/dist/sync/bootstrap.js +224 -71
- package/dist/sync/bootstrap.js.map +1 -1
- package/dist/sync/bootstrap.test.js +210 -2
- package/dist/sync/bootstrap.test.js.map +1 -1
- package/dist/sync/httpChannel.d.ts +14 -1
- package/dist/sync/httpChannel.d.ts.map +1 -1
- package/dist/sync/httpChannel.js +34 -0
- package/dist/sync/httpChannel.js.map +1 -1
- package/dist/sync/httpChannel.test.js +54 -0
- package/dist/sync/httpChannel.test.js.map +1 -1
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/client.test.ts +314 -1
- package/src/client.ts +362 -54
- package/src/index.ts +1 -0
- package/src/persisters/encryptedIndexedDbPersister.test.ts +44 -1
- package/src/persisters/encryptedIndexedDbPersister.ts +24 -17
- package/src/sync/bootstrap.test.ts +269 -3
- package/src/sync/bootstrap.ts +336 -82
- package/src/sync/httpChannel.test.ts +67 -0
- package/src/sync/httpChannel.ts +50 -0
- package/src/types.ts +12 -0
package/dist/sync/bootstrap.js
CHANGED
|
@@ -65,96 +65,249 @@ const applySnapshotEntity = ({ store, collection, stream, entity, }) => {
|
|
|
65
65
|
store.clearNeedsRepair({ collection, entityId: entity.id });
|
|
66
66
|
return true;
|
|
67
67
|
};
|
|
68
|
-
/**
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
68
|
+
/** Entities written per store transaction before yielding to the host event loop. */
|
|
69
|
+
const DEFAULT_APPLY_CHUNK_SIZE = 500;
|
|
70
|
+
/** Most streams named in one `POST /sync/snapshot/batch` request (server cap). */
|
|
71
|
+
const MAX_SNAPSHOT_BATCH_STREAMS = 500;
|
|
72
|
+
const defaultYieldToHost = () => new Promise((resolve) => {
|
|
73
|
+
setTimeout(resolve, 0);
|
|
74
|
+
});
|
|
75
|
+
const initialPagingState = ({ store, stream, collection, }) => {
|
|
75
76
|
// Until a snapshot pass has reached the stream head, resume from snapshot progress
|
|
76
77
|
// (`snapshotSeq`) rather than the applied-seq cursor: a live delta can push the latter
|
|
77
78
|
// to the head mid-bootstrap, and resuming there would skip every unpaged seq below it
|
|
78
79
|
// forever. Once bootstrapped, the applied-seq cursor IS the correct catch-up point.
|
|
79
80
|
const bootstrapped = isStreamBootstrapped({ store, stream });
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
81
|
+
return {
|
|
82
|
+
collection,
|
|
83
|
+
cursor: bootstrapped ? getCursor({ store, stream }) : getSnapshotCursor({ store, stream }),
|
|
84
|
+
hasMore: true,
|
|
85
|
+
legacyCursor: undefined,
|
|
86
|
+
reachedHead: false,
|
|
87
|
+
retentionChecked: false,
|
|
88
|
+
stream,
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Advance `state` past one fetched page (C7 retention check, C3 legacy stratum, C1
|
|
93
|
+
* frontier clamp) and describe the writes that page needs.
|
|
94
|
+
*/
|
|
95
|
+
const planSnapshotPage = ({ state, page, }) => {
|
|
96
|
+
const { collection, stream } = state;
|
|
97
|
+
const base = { collection, completesStream: false, entities: page.entities, stream };
|
|
98
|
+
// C7: a stored cursor below the retained floor means compacted tombstones were
|
|
99
|
+
// missed — purge and re-bootstrap this stream from 0 exactly once.
|
|
100
|
+
if (!state.retentionChecked) {
|
|
101
|
+
state.retentionChecked = true;
|
|
102
|
+
if (state.cursor > 0 && state.cursor < page.oldestRetainedSeq) {
|
|
103
|
+
state.cursor = 0;
|
|
104
|
+
state.legacyCursor = undefined;
|
|
105
|
+
return {
|
|
106
|
+
...base,
|
|
107
|
+
entities: [],
|
|
108
|
+
progress: { collection, cursor: 0, fetched: 0, hasMore: true, stream },
|
|
109
|
+
purge: true,
|
|
110
|
+
};
|
|
99
111
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
return count;
|
|
111
|
-
});
|
|
112
|
-
const advanced = page.legacyCursor !== legacyCursor;
|
|
113
|
-
legacyCursor = page.legacyCursor;
|
|
114
|
-
hasMore = advanced;
|
|
115
|
-
onProgress?.({
|
|
116
|
-
applied,
|
|
112
|
+
}
|
|
113
|
+
if (page.legacyCursor !== undefined) {
|
|
114
|
+
// C3: still draining the seq-0 stratum — echo the token, cursor stays 0.
|
|
115
|
+
const advanced = page.legacyCursor !== state.legacyCursor;
|
|
116
|
+
state.legacyCursor = page.legacyCursor;
|
|
117
|
+
state.hasMore = advanced;
|
|
118
|
+
return {
|
|
119
|
+
...base,
|
|
120
|
+
progress: {
|
|
117
121
|
collection,
|
|
118
122
|
cursor: 0,
|
|
119
123
|
fetched: page.entities.length,
|
|
120
|
-
hasMore,
|
|
124
|
+
hasMore: state.hasMore,
|
|
121
125
|
stream,
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
126
|
+
},
|
|
127
|
+
purge: false,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
// Legacy stratum done (or never present) — proceed by seq.
|
|
131
|
+
state.legacyCursor = undefined;
|
|
132
|
+
// C1: never advance past the stable frontier; the server already clamped page.cursor.
|
|
133
|
+
const clampedCursor = Math.min(page.cursor, page.frontierSeq);
|
|
134
|
+
const madeProgress = clampedCursor > state.cursor;
|
|
135
|
+
// Guard against a server reporting hasMore without advancing (would loop forever).
|
|
136
|
+
state.hasMore = page.hasMore && madeProgress;
|
|
137
|
+
// Only the server saying "no more pages" proves the head was reached; exiting via the
|
|
138
|
+
// guard above means the pass is incomplete and must resume from snapshot progress.
|
|
139
|
+
state.reachedHead = !page.hasMore;
|
|
140
|
+
state.cursor = Math.max(state.cursor, clampedCursor);
|
|
141
|
+
return {
|
|
142
|
+
...base,
|
|
143
|
+
advanceTo: madeProgress ? clampedCursor : undefined,
|
|
144
|
+
completesStream: !state.hasMore && state.reachedHead,
|
|
145
|
+
progress: {
|
|
146
|
+
collection,
|
|
147
|
+
cursor: state.cursor,
|
|
148
|
+
fetched: page.entities.length,
|
|
149
|
+
hasMore: state.hasMore,
|
|
150
|
+
stream,
|
|
151
|
+
},
|
|
152
|
+
purge: false,
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Write a planned page. Entities land in transactions of `chunkSize` with a yield to the
|
|
157
|
+
* host between them, so a large page never blocks the UI thread in one piece. The cursor
|
|
158
|
+
* advance rides in the last chunk's transaction: if the app dies mid-page, the page is
|
|
159
|
+
* re-fetched, and the seq guards make re-applying it a no-op.
|
|
160
|
+
*
|
|
161
|
+
* Returns false when `isSuperseded` fired during a yield: a sign-out or user switch may
|
|
162
|
+
* have wiped the store, so the rest of the page must not be written into it.
|
|
163
|
+
*/
|
|
164
|
+
const writePagePlan = async ({ store, plan, chunkSize, yieldToHost, isSuperseded, onProgress, now, }) => {
|
|
165
|
+
const { collection, stream, entities, advanceTo } = plan;
|
|
166
|
+
if (plan.purge) {
|
|
167
|
+
store.purgeStream({ stream });
|
|
168
|
+
store.addKnownStream({ collection, stream });
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
let applied = 0;
|
|
172
|
+
let offset = 0;
|
|
173
|
+
do {
|
|
174
|
+
const chunk = entities.slice(offset, offset + chunkSize);
|
|
175
|
+
offset += chunkSize;
|
|
176
|
+
const isLastChunk = offset >= entities.length;
|
|
177
|
+
// E4: one transaction per chunk, so TinyBase fires listeners (and the autosave
|
|
178
|
+
// persister's did-finish-transaction write) once per chunk rather than once per row.
|
|
179
|
+
applied += store.raw.transaction(() => {
|
|
136
180
|
let count = 0;
|
|
137
|
-
for (const entity of
|
|
181
|
+
for (const entity of chunk) {
|
|
138
182
|
if (applySnapshotEntity({ collection, entity, store, stream })) {
|
|
139
183
|
count += 1;
|
|
140
184
|
}
|
|
141
185
|
}
|
|
142
|
-
if (
|
|
143
|
-
setCursor({ now, seq:
|
|
144
|
-
setSnapshotCursor({ seq:
|
|
186
|
+
if (isLastChunk && advanceTo !== undefined) {
|
|
187
|
+
setCursor({ now, seq: advanceTo, store, stream });
|
|
188
|
+
setSnapshotCursor({ seq: advanceTo, store, stream });
|
|
145
189
|
}
|
|
146
190
|
return count;
|
|
147
191
|
});
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (reachedHead) {
|
|
192
|
+
if (!isLastChunk) {
|
|
193
|
+
await yieldToHost();
|
|
194
|
+
if (isSuperseded?.()) {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
} while (offset < entities.length);
|
|
199
|
+
if (plan.completesStream) {
|
|
157
200
|
markStreamBootstrapped({ store, stream });
|
|
158
201
|
}
|
|
202
|
+
onProgress?.({ ...plan.progress, applied });
|
|
203
|
+
return true;
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* Page a single stream from its current cursor to the server's head. Handles the C3
|
|
207
|
+
* legacy stratum (echoing `legacyCursor`), C1 frontier clamping, and C7 retention-gap
|
|
208
|
+
* re-bootstrap.
|
|
209
|
+
*/
|
|
210
|
+
export const bootstrapStream = async ({ store, channel, stream, collection, limit, onProgress, now, isSuperseded, applyChunkSize = DEFAULT_APPLY_CHUNK_SIZE, yieldToHost = defaultYieldToHost, }) => {
|
|
211
|
+
const state = initialPagingState({ collection, store, stream });
|
|
212
|
+
while (state.hasMore) {
|
|
213
|
+
const page = await channel.fetchSnapshotPage({
|
|
214
|
+
cursor: state.cursor,
|
|
215
|
+
legacyCursor: state.legacyCursor,
|
|
216
|
+
limit,
|
|
217
|
+
stream,
|
|
218
|
+
});
|
|
219
|
+
if (isSuperseded?.()) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const plan = planSnapshotPage({ page, state });
|
|
223
|
+
const written = await writePagePlan({
|
|
224
|
+
chunkSize: applyChunkSize,
|
|
225
|
+
isSuperseded,
|
|
226
|
+
now,
|
|
227
|
+
onProgress,
|
|
228
|
+
plan,
|
|
229
|
+
store,
|
|
230
|
+
yieldToHost,
|
|
231
|
+
});
|
|
232
|
+
if (!written) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
/**
|
|
238
|
+
* Page many streams through `POST /sync/snapshot/batch`: one round trip per round
|
|
239
|
+
* instead of one per stream per page. `limit` is the server's entity budget for the
|
|
240
|
+
* whole response, shared across the requested streams. Rounds are pipelined: the next
|
|
241
|
+
* request leaves as soon as a response arrives (its cursors come from the response,
|
|
242
|
+
* not the store), and the current round is written while it is in flight.
|
|
243
|
+
*
|
|
244
|
+
* Returns false when `isSuperseded` fired and the pass stopped early.
|
|
245
|
+
*/
|
|
246
|
+
export const bootstrapStreamsBatch = async ({ store, fetchSnapshotBatch, streams, limit, onProgress, now, isSuperseded, applyChunkSize = DEFAULT_APPLY_CHUNK_SIZE, yieldToHost = defaultYieldToHost, }) => {
|
|
247
|
+
const states = new Map();
|
|
248
|
+
for (const { stream, collection } of streams) {
|
|
249
|
+
states.set(stream, initialPagingState({ collection, store, stream }));
|
|
250
|
+
}
|
|
251
|
+
const requestRound = () => {
|
|
252
|
+
const pending = [...states.values()]
|
|
253
|
+
.filter((state) => state.hasMore)
|
|
254
|
+
.slice(0, MAX_SNAPSHOT_BATCH_STREAMS);
|
|
255
|
+
if (pending.length === 0) {
|
|
256
|
+
return undefined;
|
|
257
|
+
}
|
|
258
|
+
return fetchSnapshotBatch({
|
|
259
|
+
limit,
|
|
260
|
+
streams: pending.map((state) => ({
|
|
261
|
+
cursor: state.cursor,
|
|
262
|
+
legacyCursor: state.legacyCursor,
|
|
263
|
+
stream: state.stream,
|
|
264
|
+
})),
|
|
265
|
+
}).then((response) => {
|
|
266
|
+
const answered = new Set(response.pages.map((page) => page.stream));
|
|
267
|
+
for (const state of pending) {
|
|
268
|
+
if (!answered.has(state.stream)) {
|
|
269
|
+
throw new Error(`Snapshot batch response is missing stream ${state.stream}`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return response;
|
|
273
|
+
});
|
|
274
|
+
};
|
|
275
|
+
let inFlight = requestRound();
|
|
276
|
+
try {
|
|
277
|
+
while (inFlight !== undefined) {
|
|
278
|
+
const { pages } = await inFlight;
|
|
279
|
+
inFlight = undefined;
|
|
280
|
+
if (isSuperseded?.()) {
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
const plans = [];
|
|
284
|
+
for (const page of pages) {
|
|
285
|
+
const state = states.get(page.stream);
|
|
286
|
+
if (state) {
|
|
287
|
+
plans.push(planSnapshotPage({ page, state }));
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
inFlight = requestRound();
|
|
291
|
+
for (const plan of plans) {
|
|
292
|
+
const written = await writePagePlan({
|
|
293
|
+
chunkSize: applyChunkSize,
|
|
294
|
+
isSuperseded,
|
|
295
|
+
now,
|
|
296
|
+
onProgress,
|
|
297
|
+
plan,
|
|
298
|
+
store,
|
|
299
|
+
yieldToHost,
|
|
300
|
+
});
|
|
301
|
+
if (!written || isSuperseded?.()) {
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
finally {
|
|
309
|
+
// An early exit abandons the prefetched round; never leave its rejection unhandled.
|
|
310
|
+
inFlight?.catch(() => { });
|
|
311
|
+
}
|
|
159
312
|
};
|
|
160
313
|
//# sourceMappingURL=bootstrap.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bootstrap.js","sourceRoot":"","sources":["../../src/sync/bootstrap.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;
|
|
1
|
+
{"version":3,"file":"bootstrap.js","sourceRoot":"","sources":["../../src/sync/bootstrap.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AASH,OAAO,EACL,SAAS,EACT,iBAAiB,EACjB,oBAAoB,EACpB,sBAAsB,EACtB,SAAS,EACT,iBAAiB,GAClB,MAAM,UAAU,CAAC;AAiBlB;;;GAGG;AACH,MAAM,mBAAmB,GAAG,CAAC,EAC3B,KAAK,EACL,UAAU,EACV,MAAM,EACN,MAAM,GAMP,EAAW,EAAE;IACZ,MAAM,QAAQ,GAAG,KAAK,CAAC,SA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C,gBAAgB,CAAC,EAAC,IAAI,EAAE,KAAK,EAAC,CAAC,CAAC,CAAC;gBAC9C,CAAC;YACH,CAAC;YACD,QAAQ,GAAG,YAAY,EAAE,CAAC;YAC1B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;gBACzB,MAAM,OAAO,GAAG,MAAM,aAAa,CAAC;oBAClC,SAAS,EAAE,cAAc;oBACzB,YAAY;oBACZ,GAAG;oBACH,UAAU;oBACV,IAAI;oBACJ,KAAK;oBACL,WAAW;iBACZ,CAAC,CAAC;gBACH,IAAI,CAAC,OAAO,IAAI,YAAY,EAAE,EAAE,EAAE,CAAC;oBACjC,OAAO,KAAK,CAAC;gBACf,CAAC;YACH,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;YAAS,CAAC;QACT,oFAAoF;QACpF,QAAQ,EAAE,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;IAC5B,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, expect, it } from "bun:test";
|
|
2
2
|
import { createSyncStore } from "../storage/store";
|
|
3
|
-
import { bootstrapStream } from "./bootstrap";
|
|
4
|
-
import { getCursor, markStreamBootstrapped, setCursor, setSnapshotCursor } from "./cursor";
|
|
3
|
+
import { bootstrapStream, bootstrapStreamsBatch } from "./bootstrap";
|
|
4
|
+
import { getCursor, getSnapshotCursor, isStreamBootstrapped, markStreamBootstrapped, setCursor, setSnapshotCursor, } from "./cursor";
|
|
5
5
|
import { applyDelta } from "./deltaApplier";
|
|
6
6
|
const STREAM = "todos|owner:u1";
|
|
7
7
|
const makeStore = () => createSyncStore({ collections: ["notes", "todos"] });
|
|
@@ -375,6 +375,38 @@ describe("bootstrapStream", () => {
|
|
|
375
375
|
await bootstrapStream({ channel: second, collection: "todos", store, stream: STREAM });
|
|
376
376
|
expect(second.calls.map((call) => call.cursor)).toEqual([40]);
|
|
377
377
|
});
|
|
378
|
+
it("writes nothing when superseded while a page was in flight", async () => {
|
|
379
|
+
const store = makeStore();
|
|
380
|
+
const channel = makeChannel({ 0: page({ cursor: 1, entities: entitiesFor(1, 1) }) });
|
|
381
|
+
await bootstrapStream({
|
|
382
|
+
channel,
|
|
383
|
+
collection: "todos",
|
|
384
|
+
isSuperseded: () => true,
|
|
385
|
+
store,
|
|
386
|
+
stream: STREAM,
|
|
387
|
+
});
|
|
388
|
+
expect(store.getEntity({ collection: "todos", id: "e1" })).toBeUndefined();
|
|
389
|
+
expect(getCursor({ store, stream: STREAM })).toBe(0);
|
|
390
|
+
});
|
|
391
|
+
it("stops mid-page when superseded during a chunk yield", async () => {
|
|
392
|
+
const store = makeStore();
|
|
393
|
+
const channel = makeChannel({ 0: page({ cursor: 4, entities: entitiesFor(1, 4) }) });
|
|
394
|
+
let superseded = false;
|
|
395
|
+
await bootstrapStream({
|
|
396
|
+
applyChunkSize: 2,
|
|
397
|
+
channel,
|
|
398
|
+
collection: "todos",
|
|
399
|
+
isSuperseded: () => superseded,
|
|
400
|
+
store,
|
|
401
|
+
stream: STREAM,
|
|
402
|
+
yieldToHost: async () => {
|
|
403
|
+
superseded = true;
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
expect(store.getEntity({ collection: "todos", id: "e2" })).toBeDefined();
|
|
407
|
+
expect(store.getEntity({ collection: "todos", id: "e3" })).toBeUndefined();
|
|
408
|
+
expect(getCursor({ store, stream: STREAM })).toBe(0);
|
|
409
|
+
});
|
|
378
410
|
it("reports progress per page and forwards the limit", async () => {
|
|
379
411
|
const store = makeStore();
|
|
380
412
|
store.upsertEntity({
|
|
@@ -444,4 +476,180 @@ describe("bootstrapStream", () => {
|
|
|
444
476
|
expect(store.listEntities({ collection: "todos" })).toHaveLength(5);
|
|
445
477
|
});
|
|
446
478
|
});
|
|
479
|
+
const NOTES_STREAM = "notes|owner:u1";
|
|
480
|
+
/** Entities with seqs `from..to` (inclusive) for a snapshot page. */
|
|
481
|
+
const entitiesFor = (from, to) => Array.from({ length: to - from + 1 }, (_unused, index) => ({
|
|
482
|
+
data: { title: `row ${from + index}` },
|
|
483
|
+
deleted: false,
|
|
484
|
+
id: `e${from + index}`,
|
|
485
|
+
seq: from + index,
|
|
486
|
+
}));
|
|
487
|
+
/**
|
|
488
|
+
* Batch fetch stub: each stream's page comes from `pageFor(stream, cursor)`; every
|
|
489
|
+
* request is recorded along with the order of fetches relative to other events.
|
|
490
|
+
*/
|
|
491
|
+
const makeBatchFetch = (pageFor, events = []) => {
|
|
492
|
+
const requests = [];
|
|
493
|
+
return {
|
|
494
|
+
fetchSnapshotBatch: async (request) => {
|
|
495
|
+
requests.push(request);
|
|
496
|
+
events.push(`fetch:${request.streams.map((item) => item.stream).join(",")}`);
|
|
497
|
+
return { pages: request.streams.map(pageFor) };
|
|
498
|
+
},
|
|
499
|
+
requests,
|
|
500
|
+
};
|
|
501
|
+
};
|
|
502
|
+
describe("bootstrapStreamsBatch", () => {
|
|
503
|
+
const streams = [
|
|
504
|
+
{ collection: "todos", stream: STREAM },
|
|
505
|
+
{ collection: "notes", stream: NOTES_STREAM },
|
|
506
|
+
];
|
|
507
|
+
it("pages every stream to its head, re-requesting only streams with more", async () => {
|
|
508
|
+
const store = makeStore();
|
|
509
|
+
const { fetchSnapshotBatch, requests } = makeBatchFetch(({ stream, cursor }) => {
|
|
510
|
+
if (stream === NOTES_STREAM) {
|
|
511
|
+
return page({ cursor: 1, entities: entitiesFor(1, 1), stream });
|
|
512
|
+
}
|
|
513
|
+
return cursor === 0
|
|
514
|
+
? page({ cursor: 2, entities: entitiesFor(1, 2), frontierSeq: 3, hasMore: true })
|
|
515
|
+
: page({ cursor: 3, entities: entitiesFor(3, 3) });
|
|
516
|
+
});
|
|
517
|
+
const completed = await bootstrapStreamsBatch({
|
|
518
|
+
fetchSnapshotBatch,
|
|
519
|
+
limit: 2000,
|
|
520
|
+
store,
|
|
521
|
+
streams,
|
|
522
|
+
});
|
|
523
|
+
expect(completed).toBe(true);
|
|
524
|
+
expect(requests).toEqual([
|
|
525
|
+
{
|
|
526
|
+
limit: 2000,
|
|
527
|
+
streams: [
|
|
528
|
+
{ cursor: 0, legacyCursor: undefined, stream: STREAM },
|
|
529
|
+
{ cursor: 0, legacyCursor: undefined, stream: NOTES_STREAM },
|
|
530
|
+
],
|
|
531
|
+
},
|
|
532
|
+
{ limit: 2000, streams: [{ cursor: 2, legacyCursor: undefined, stream: STREAM }] },
|
|
533
|
+
]);
|
|
534
|
+
expect(store.getEntity({ collection: "todos", id: "e3" })?.stream).toBe(STREAM);
|
|
535
|
+
expect(store.getEntity({ collection: "notes", id: "e1" })?.stream).toBe(NOTES_STREAM);
|
|
536
|
+
expect(getCursor({ store, stream: STREAM })).toBe(3);
|
|
537
|
+
expect(getCursor({ store, stream: NOTES_STREAM })).toBe(1);
|
|
538
|
+
expect(isStreamBootstrapped({ store, stream: STREAM })).toBe(true);
|
|
539
|
+
expect(isStreamBootstrapped({ store, stream: NOTES_STREAM })).toBe(true);
|
|
540
|
+
});
|
|
541
|
+
it("sends the next round before writing the current one", async () => {
|
|
542
|
+
const store = makeStore();
|
|
543
|
+
const events = [];
|
|
544
|
+
const { fetchSnapshotBatch } = makeBatchFetch(({ cursor }) => cursor === 0
|
|
545
|
+
? page({ cursor: 1, entities: entitiesFor(1, 1), frontierSeq: 2, hasMore: true })
|
|
546
|
+
: page({ cursor: 2, entities: entitiesFor(2, 2) }), events);
|
|
547
|
+
await bootstrapStreamsBatch({
|
|
548
|
+
fetchSnapshotBatch,
|
|
549
|
+
onProgress: (progress) => events.push(`write:${progress.cursor}`),
|
|
550
|
+
store,
|
|
551
|
+
streams: [{ collection: "todos", stream: STREAM }],
|
|
552
|
+
});
|
|
553
|
+
expect(events).toEqual([`fetch:${STREAM}`, `fetch:${STREAM}`, "write:1", "write:2"]);
|
|
554
|
+
});
|
|
555
|
+
it("writes a large page in chunks, yielding between them, and advances the cursor last", async () => {
|
|
556
|
+
const store = makeStore();
|
|
557
|
+
const { fetchSnapshotBatch } = makeBatchFetch(() => page({ cursor: 5, entities: entitiesFor(1, 5) }));
|
|
558
|
+
let transactions = 0;
|
|
559
|
+
store.raw.addDidFinishTransactionListener(() => {
|
|
560
|
+
transactions += 1;
|
|
561
|
+
});
|
|
562
|
+
const cursorsAtYield = [];
|
|
563
|
+
await bootstrapStreamsBatch({
|
|
564
|
+
applyChunkSize: 2,
|
|
565
|
+
fetchSnapshotBatch,
|
|
566
|
+
store,
|
|
567
|
+
streams: [{ collection: "todos", stream: STREAM }],
|
|
568
|
+
yieldToHost: async () => {
|
|
569
|
+
cursorsAtYield.push(getCursor({ store, stream: STREAM }));
|
|
570
|
+
},
|
|
571
|
+
});
|
|
572
|
+
// Chunks of 2, 2, 1: two yields, and the cursor only moves in the final chunk.
|
|
573
|
+
expect(cursorsAtYield).toEqual([0, 0]);
|
|
574
|
+
expect(transactions).toBeGreaterThanOrEqual(3);
|
|
575
|
+
expect(getCursor({ store, stream: STREAM })).toBe(5);
|
|
576
|
+
expect(getSnapshotCursor({ store, stream: STREAM })).toBe(5);
|
|
577
|
+
expect(store.getEntity({ collection: "todos", id: "e5" })?.seq).toBe(5);
|
|
578
|
+
});
|
|
579
|
+
it("stops without writing when superseded while a round was in flight", async () => {
|
|
580
|
+
const store = makeStore();
|
|
581
|
+
const { fetchSnapshotBatch } = makeBatchFetch(() => page({ cursor: 1, entities: entitiesFor(1, 1) }));
|
|
582
|
+
const completed = await bootstrapStreamsBatch({
|
|
583
|
+
fetchSnapshotBatch,
|
|
584
|
+
isSuperseded: () => true,
|
|
585
|
+
store,
|
|
586
|
+
streams: [{ collection: "todos", stream: STREAM }],
|
|
587
|
+
});
|
|
588
|
+
expect(completed).toBe(false);
|
|
589
|
+
expect(store.getEntity({ collection: "todos", id: "e1" })).toBeUndefined();
|
|
590
|
+
expect(getCursor({ store, stream: STREAM })).toBe(0);
|
|
591
|
+
});
|
|
592
|
+
it("stops mid-page when superseded during a chunk yield, leaving the cursor unmoved", async () => {
|
|
593
|
+
const store = makeStore();
|
|
594
|
+
const { fetchSnapshotBatch } = makeBatchFetch(() => page({ cursor: 5, entities: entitiesFor(1, 5) }));
|
|
595
|
+
let superseded = false;
|
|
596
|
+
const completed = await bootstrapStreamsBatch({
|
|
597
|
+
applyChunkSize: 2,
|
|
598
|
+
fetchSnapshotBatch,
|
|
599
|
+
isSuperseded: () => superseded,
|
|
600
|
+
store,
|
|
601
|
+
streams: [{ collection: "todos", stream: STREAM }],
|
|
602
|
+
yieldToHost: async () => {
|
|
603
|
+
// A sign-out or user switch wipes the store while the page is being written.
|
|
604
|
+
superseded = true;
|
|
605
|
+
},
|
|
606
|
+
});
|
|
607
|
+
expect(completed).toBe(false);
|
|
608
|
+
expect(store.getEntity({ collection: "todos", id: "e2" })).toBeDefined();
|
|
609
|
+
expect(store.getEntity({ collection: "todos", id: "e3" })).toBeUndefined();
|
|
610
|
+
expect(getCursor({ store, stream: STREAM })).toBe(0);
|
|
611
|
+
expect(isStreamBootstrapped({ store, stream: STREAM })).toBe(false);
|
|
612
|
+
});
|
|
613
|
+
it("C7: purges a stream whose cursor fell below the retained floor and re-pages it from 0", async () => {
|
|
614
|
+
const store = makeStore();
|
|
615
|
+
seedBootstrappedStream({ seq: 4, store });
|
|
616
|
+
store.upsertEntity({
|
|
617
|
+
collection: "todos",
|
|
618
|
+
data: { title: "stale" },
|
|
619
|
+
id: "gone",
|
|
620
|
+
seq: 2,
|
|
621
|
+
stream: STREAM,
|
|
622
|
+
});
|
|
623
|
+
const { fetchSnapshotBatch, requests } = makeBatchFetch(({ cursor }) => cursor === 4
|
|
624
|
+
? page({ cursor: 9, entities: [], oldestRetainedSeq: 6 })
|
|
625
|
+
: page({ cursor: 9, entities: entitiesFor(9, 9), oldestRetainedSeq: 6 }));
|
|
626
|
+
await bootstrapStreamsBatch({
|
|
627
|
+
fetchSnapshotBatch,
|
|
628
|
+
store,
|
|
629
|
+
streams: [{ collection: "todos", stream: STREAM }],
|
|
630
|
+
});
|
|
631
|
+
expect(requests.map((request) => request.streams[0]?.cursor)).toEqual([4, 0]);
|
|
632
|
+
expect(store.getEntity({ collection: "todos", id: "gone" })).toBeUndefined();
|
|
633
|
+
expect(store.getEntity({ collection: "todos", id: "e9" })?.seq).toBe(9);
|
|
634
|
+
expect(getCursor({ store, stream: STREAM })).toBe(9);
|
|
635
|
+
});
|
|
636
|
+
it("rejects a response that leaves out a requested stream", async () => {
|
|
637
|
+
const store = makeStore();
|
|
638
|
+
await expect(bootstrapStreamsBatch({
|
|
639
|
+
fetchSnapshotBatch: async () => ({ pages: [page({ cursor: 0 })] }),
|
|
640
|
+
store,
|
|
641
|
+
streams,
|
|
642
|
+
})).rejects.toThrow(`missing stream ${NOTES_STREAM}`);
|
|
643
|
+
});
|
|
644
|
+
it("caps each request at the server's 500-stream limit", async () => {
|
|
645
|
+
const store = makeStore();
|
|
646
|
+
const many = Array.from({ length: 501 }, (_unused, index) => ({
|
|
647
|
+
collection: "todos",
|
|
648
|
+
stream: `todos|owner:u${index}`,
|
|
649
|
+
}));
|
|
650
|
+
const { fetchSnapshotBatch, requests } = makeBatchFetch(({ stream }) => page({ cursor: 0, stream }));
|
|
651
|
+
await bootstrapStreamsBatch({ fetchSnapshotBatch, store, streams: many });
|
|
652
|
+
expect(requests.map((request) => request.streams.length)).toEqual([500, 1]);
|
|
653
|
+
});
|
|
654
|
+
});
|
|
447
655
|
//# sourceMappingURL=bootstrap.test.js.map
|