@terreno/syncdb 57.16.2 → 57.18.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 +1 -0
- package/dist/auth/betterAuthAdapter.d.ts +15 -4
- package/dist/auth/betterAuthAdapter.d.ts.map +1 -1
- package/dist/auth/betterAuthAdapter.js +130 -12
- package/dist/auth/betterAuthAdapter.js.map +1 -1
- package/dist/auth/betterAuthAdapter.test.js +321 -0
- package/dist/auth/betterAuthAdapter.test.js.map +1 -1
- package/dist/client.d.ts +6 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1 -0
- package/dist/client.js.map +1 -1
- package/dist/client.test.js +226 -0
- package/dist/client.test.js.map +1 -1
- package/dist/sync/replayCoordinator.test.js +262 -1
- package/dist/sync/replayCoordinator.test.js.map +1 -1
- package/package.json +1 -1
- package/src/auth/betterAuthAdapter.test.ts +384 -0
- package/src/auth/betterAuthAdapter.ts +155 -14
- package/src/client.test.ts +256 -0
- package/src/client.ts +7 -0
- package/src/sync/replayCoordinator.test.ts +313 -1
package/dist/client.test.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import "fake-indexeddb/auto";
|
|
2
2
|
import { describe, expect, it } from "bun:test";
|
|
3
|
+
import { createServer } from "node:http";
|
|
3
4
|
import { assert } from "chai";
|
|
5
|
+
import { Server } from "socket.io";
|
|
6
|
+
import { betterAuthAdapter } from "./auth/betterAuthAdapter";
|
|
4
7
|
import { createSyncDb } from "./client";
|
|
5
8
|
import { createLocalKeyProvider, DEFAULT_KEY_CACHE_DB_NAME } from "./crypto/keyProviders";
|
|
6
9
|
import { getConflict } from "./mutations/conflicts";
|
|
@@ -189,6 +192,38 @@ describe("createSyncDb", () => {
|
|
|
189
192
|
totalThisDrain: 0,
|
|
190
193
|
});
|
|
191
194
|
});
|
|
195
|
+
it("opens the default socket with the configured socketTransports", async () => {
|
|
196
|
+
const httpServer = createServer();
|
|
197
|
+
const io = new Server(httpServer);
|
|
198
|
+
const firstTransports = [];
|
|
199
|
+
io.on("connection", (socket) => {
|
|
200
|
+
firstTransports.push(socket.conn.transport.name);
|
|
201
|
+
});
|
|
202
|
+
await new Promise((resolve) => httpServer.listen(0, "127.0.0.1", resolve));
|
|
203
|
+
const { port } = httpServer.address();
|
|
204
|
+
const harness = makeHarness();
|
|
205
|
+
const client = createSyncDb({
|
|
206
|
+
...harness.config,
|
|
207
|
+
baseUrl: `http://127.0.0.1:${port}`,
|
|
208
|
+
socketTransports: ["websocket"],
|
|
209
|
+
transport: undefined,
|
|
210
|
+
});
|
|
211
|
+
try {
|
|
212
|
+
await client.start();
|
|
213
|
+
for (let attempt = 0; attempt < 50 && firstTransports.length === 0; attempt++) {
|
|
214
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
215
|
+
}
|
|
216
|
+
expect(firstTransports).toEqual(["websocket"]);
|
|
217
|
+
}
|
|
218
|
+
finally {
|
|
219
|
+
await client.stop();
|
|
220
|
+
io.disconnectSockets(true);
|
|
221
|
+
httpServer.closeAllConnections();
|
|
222
|
+
await new Promise((resolve) => {
|
|
223
|
+
io.close(() => resolve());
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
});
|
|
192
227
|
it("start() rejects without an authenticated user", async () => {
|
|
193
228
|
// startAuthRetryAttempts: 1 disables retrying so this stays instant — the
|
|
194
229
|
// no-retry-attempts contract is tested here; retry behavior is tested below.
|
|
@@ -2283,6 +2318,44 @@ describe("createSyncDb", () => {
|
|
|
2283
2318
|
await client.stop();
|
|
2284
2319
|
});
|
|
2285
2320
|
});
|
|
2321
|
+
describe("auth change while the session read fails (betterAuthAdapter)", () => {
|
|
2322
|
+
it("keeps the user and mutate() working when the device goes offline before the auth-change handler reads the session", async () => {
|
|
2323
|
+
// start() subscribes to the session atom, which hands over the current session at
|
|
2324
|
+
// once; the handler that emission queues runs after start() resolves. When the
|
|
2325
|
+
// network drops in between, its get-session read fails. That must not count as a
|
|
2326
|
+
// sign-out.
|
|
2327
|
+
const listeners = new Set();
|
|
2328
|
+
// Only start()'s own read gets through; the network is gone for every later one.
|
|
2329
|
+
let sessionReads = 0;
|
|
2330
|
+
const session = { data: { session: { token: "tok" }, user: { id: "u1" } } };
|
|
2331
|
+
const harness = makeHarness({
|
|
2332
|
+
authProvider: betterAuthAdapter({
|
|
2333
|
+
getSession: async () => {
|
|
2334
|
+
sessionReads += 1;
|
|
2335
|
+
if (sessionReads > 1) {
|
|
2336
|
+
throw new TypeError("Failed to fetch");
|
|
2337
|
+
}
|
|
2338
|
+
return session;
|
|
2339
|
+
},
|
|
2340
|
+
useSession: {
|
|
2341
|
+
subscribe: (listener) => {
|
|
2342
|
+
listeners.add(listener);
|
|
2343
|
+
listener(session);
|
|
2344
|
+
return () => listeners.delete(listener);
|
|
2345
|
+
},
|
|
2346
|
+
},
|
|
2347
|
+
}),
|
|
2348
|
+
});
|
|
2349
|
+
const client = createSyncDb(harness.config);
|
|
2350
|
+
await client.start();
|
|
2351
|
+
await waitUntil(() => sessionReads > 1);
|
|
2352
|
+
await flush();
|
|
2353
|
+
expect(client.getSyncStatus().paused).toBeUndefined();
|
|
2354
|
+
const { id } = client.mutate({ collection: "todos", data: { title: "x" }, operation: "create" });
|
|
2355
|
+
expect(id).toBeTruthy();
|
|
2356
|
+
await client.stop();
|
|
2357
|
+
});
|
|
2358
|
+
});
|
|
2286
2359
|
describe("signOut() (A4)", () => {
|
|
2287
2360
|
it("serializes against a concurrent auth change and leaves no destroyed persister behind", async () => {
|
|
2288
2361
|
const harness = makeHarness();
|
|
@@ -3133,4 +3206,157 @@ describe("snapshot paging concurrency", () => {
|
|
|
3133
3206
|
await client.stop();
|
|
3134
3207
|
});
|
|
3135
3208
|
});
|
|
3209
|
+
describe("debug event stream: reconcile decisions", () => {
|
|
3210
|
+
/** Labels of every recorded reconcile event, in order. */
|
|
3211
|
+
const reconcileLabels = (client) => (client.debug?.getEvents() ?? [])
|
|
3212
|
+
.filter((event) => event.type === "reconcile")
|
|
3213
|
+
.map((event) => event.label);
|
|
3214
|
+
it("records a skip for a stream whose collection is not configured and a join for the rest", async () => {
|
|
3215
|
+
const harness = makeHarness({ debug: true });
|
|
3216
|
+
const unconfigured = "projects|tenant:org1";
|
|
3217
|
+
harness.http.state.streams = [
|
|
3218
|
+
{ collection: "todos", stream: DEFAULT_STREAM },
|
|
3219
|
+
{ collection: "projects", stream: unconfigured },
|
|
3220
|
+
];
|
|
3221
|
+
const client = createSyncDb(harness.config);
|
|
3222
|
+
await client.start();
|
|
3223
|
+
await flush();
|
|
3224
|
+
const skipped = client.debug
|
|
3225
|
+
?.getEvents()
|
|
3226
|
+
.find((event) => event.label.startsWith("stream skipped"));
|
|
3227
|
+
expect(skipped).toMatchObject({
|
|
3228
|
+
detail: { collection: "projects", stream: unconfigured },
|
|
3229
|
+
direction: "system",
|
|
3230
|
+
label: `stream skipped (unconfigured collection): ${unconfigured}`,
|
|
3231
|
+
type: "reconcile",
|
|
3232
|
+
});
|
|
3233
|
+
expect(reconcileLabels(client)).toContain(`stream join: ${DEFAULT_STREAM}`);
|
|
3234
|
+
await client.stop();
|
|
3235
|
+
});
|
|
3236
|
+
it("records a leave with the number of entities it purged", async () => {
|
|
3237
|
+
const harness = makeHarness({ debug: true });
|
|
3238
|
+
const leaving = "todos|tenant:org1";
|
|
3239
|
+
harness.http.state.streams = [
|
|
3240
|
+
{ collection: "todos", stream: DEFAULT_STREAM },
|
|
3241
|
+
{ collection: "todos", stream: leaving },
|
|
3242
|
+
];
|
|
3243
|
+
harness.http.state.pages[leaving] = {
|
|
3244
|
+
cursor: 2,
|
|
3245
|
+
entities: [
|
|
3246
|
+
{ data: { title: "org doc" }, deleted: false, id: "og1", seq: 1 },
|
|
3247
|
+
{ data: { title: "org doc 2" }, deleted: false, id: "og2", seq: 2 },
|
|
3248
|
+
],
|
|
3249
|
+
frontierSeq: 2,
|
|
3250
|
+
hasMore: false,
|
|
3251
|
+
oldestRetainedSeq: 0,
|
|
3252
|
+
stream: leaving,
|
|
3253
|
+
};
|
|
3254
|
+
const client = createSyncDb(harness.config);
|
|
3255
|
+
await client.start();
|
|
3256
|
+
await flush();
|
|
3257
|
+
harness.http.state.streams = [{ collection: "todos", stream: DEFAULT_STREAM }];
|
|
3258
|
+
await client.reconcile();
|
|
3259
|
+
const leave = client.debug?.getEvents().find((event) => event.label.startsWith("stream leave"));
|
|
3260
|
+
expect(leave).toMatchObject({
|
|
3261
|
+
detail: { purged: 2, stream: leaving },
|
|
3262
|
+
label: `stream leave: purged ${leaving} (2 entities)`,
|
|
3263
|
+
});
|
|
3264
|
+
await client.stop();
|
|
3265
|
+
});
|
|
3266
|
+
it("records how many legacy snapshot cursors it migrated", async () => {
|
|
3267
|
+
const harness = makeHarness({ collections: ["todos", "notes"], debug: true });
|
|
3268
|
+
const client = createSyncDb(harness.config);
|
|
3269
|
+
client.store.raw.setRow(CURSORS_TABLE, "snapshot:todos", { seq: 42, updatedAt: "old" });
|
|
3270
|
+
client.store.raw.setRow(CURSORS_TABLE, "snapshot:notes", { seq: 7, updatedAt: "old" });
|
|
3271
|
+
await client.start();
|
|
3272
|
+
await flush();
|
|
3273
|
+
const migration = client.debug?.getEvents().find((event) => event.label.startsWith("migrated"));
|
|
3274
|
+
expect(migration).toMatchObject({
|
|
3275
|
+
detail: { migrated: 2 },
|
|
3276
|
+
label: "migrated 2 legacy snapshot cursors",
|
|
3277
|
+
});
|
|
3278
|
+
await client.stop();
|
|
3279
|
+
});
|
|
3280
|
+
it("records tombstone compaction only when a reconcile actually removes rows", async () => {
|
|
3281
|
+
const retentionMs = 90 * 24 * 60 * 60 * 1_000;
|
|
3282
|
+
const harness = makeHarness({ debug: true, tombstoneRetentionMs: retentionMs });
|
|
3283
|
+
const client = createSyncDb(harness.config);
|
|
3284
|
+
await client.start();
|
|
3285
|
+
await flush();
|
|
3286
|
+
harness.transport.deliverDelta({
|
|
3287
|
+
collection: "todos",
|
|
3288
|
+
deleted: true,
|
|
3289
|
+
id: "old-tombstone",
|
|
3290
|
+
method: "delete",
|
|
3291
|
+
seq: 1,
|
|
3292
|
+
stream: DEFAULT_STREAM,
|
|
3293
|
+
});
|
|
3294
|
+
await client.reconcile();
|
|
3295
|
+
expect(reconcileLabels(client).some((label) => label.startsWith("compacted"))).toBe(false);
|
|
3296
|
+
harness.clock.value += retentionMs + 1_000;
|
|
3297
|
+
await client.reconcile();
|
|
3298
|
+
const compaction = client.debug
|
|
3299
|
+
?.getEvents()
|
|
3300
|
+
.find((event) => event.label.startsWith("compacted"));
|
|
3301
|
+
expect(compaction).toMatchObject({ detail: { removed: 1 }, label: "compacted 1 tombstone(s)" });
|
|
3302
|
+
await client.stop();
|
|
3303
|
+
});
|
|
3304
|
+
it("records the fallback to per-stream paging when the server lacks snapshot batch", async () => {
|
|
3305
|
+
const harness = makeHarness({ debug: true });
|
|
3306
|
+
harness.http.channel.fetchSnapshotBatch = async () => ({ type: "unsupported" });
|
|
3307
|
+
const client = createSyncDb(harness.config);
|
|
3308
|
+
await client.start();
|
|
3309
|
+
await flush();
|
|
3310
|
+
expect(reconcileLabels(client)).toContain("snapshot batch unsupported by server; paging per stream");
|
|
3311
|
+
// The per-stream fallback still bootstrapped the stream.
|
|
3312
|
+
expect(harness.http.state.fetchCount).toBeGreaterThan(0);
|
|
3313
|
+
await client.stop();
|
|
3314
|
+
});
|
|
3315
|
+
it("brackets forceResync with start and end events that carry the purge totals", async () => {
|
|
3316
|
+
const harness = makeHarness({ debug: true });
|
|
3317
|
+
harness.http.state.pages[DEFAULT_STREAM] = {
|
|
3318
|
+
cursor: 1,
|
|
3319
|
+
entities: [{ data: { title: "stale" }, deleted: false, id: "local-1", seq: 1 }],
|
|
3320
|
+
frontierSeq: 1,
|
|
3321
|
+
hasMore: false,
|
|
3322
|
+
oldestRetainedSeq: 0,
|
|
3323
|
+
stream: DEFAULT_STREAM,
|
|
3324
|
+
};
|
|
3325
|
+
const client = createSyncDb(harness.config);
|
|
3326
|
+
await client.start();
|
|
3327
|
+
await flush();
|
|
3328
|
+
harness.http.state.pages[DEFAULT_STREAM] = emptyPage(DEFAULT_STREAM, 5);
|
|
3329
|
+
const result = await client.forceResync();
|
|
3330
|
+
assert.equal(result.ok, true);
|
|
3331
|
+
const events = client.debug?.getEvents() ?? [];
|
|
3332
|
+
const start = events.find((event) => event.label === "force resync start");
|
|
3333
|
+
const end = events.find((event) => event.label.startsWith("force resync end"));
|
|
3334
|
+
expect(start).toMatchObject({ phase: "start", type: "reconcile" });
|
|
3335
|
+
expect(end).toMatchObject({
|
|
3336
|
+
detail: { purged: result.purged, repaired: result.repaired, streams: result.streams },
|
|
3337
|
+
label: `force resync end (${result.streams} streams, ${result.purged} purged, ${result.repaired} repaired)`,
|
|
3338
|
+
phase: "end",
|
|
3339
|
+
});
|
|
3340
|
+
// The injected clock never advanced, so the measured duration is exact.
|
|
3341
|
+
expect(end?.durationMs).toBe(0);
|
|
3342
|
+
expect(start?.id ?? Number.POSITIVE_INFINITY).toBeLessThan(end?.id ?? 0);
|
|
3343
|
+
await client.stop();
|
|
3344
|
+
});
|
|
3345
|
+
it("records the streams a subscribe confirmation caught up", async () => {
|
|
3346
|
+
const harness = makeHarness({ debug: true });
|
|
3347
|
+
const client = createSyncDb(harness.config);
|
|
3348
|
+
await client.start();
|
|
3349
|
+
await flush();
|
|
3350
|
+
harness.transport.confirmSubscribed({ collection: "todos", streams: ["todos|owner:new"] });
|
|
3351
|
+
await waitUntil(() => reconcileLabels(client).some((label) => label.startsWith("subscribe catch-up")));
|
|
3352
|
+
const catchUp = client.debug
|
|
3353
|
+
?.getEvents()
|
|
3354
|
+
.find((event) => event.label.startsWith("subscribe catch-up"));
|
|
3355
|
+
expect(catchUp).toMatchObject({
|
|
3356
|
+
detail: { streams: ["todos|owner:new"] },
|
|
3357
|
+
label: "subscribe catch-up: todos|owner:new",
|
|
3358
|
+
});
|
|
3359
|
+
await client.stop();
|
|
3360
|
+
});
|
|
3361
|
+
});
|
|
3136
3362
|
//# sourceMappingURL=client.test.js.map
|