@terreno/syncdb 57.3.0 → 57.4.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/dist/auth/betterAuthReactClient.d.ts +17 -0
- package/dist/auth/betterAuthReactClient.d.ts.map +1 -0
- package/dist/auth/betterAuthReactClient.js +11 -0
- package/dist/auth/betterAuthReactClient.js.map +1 -0
- package/dist/auth/betterAuthReactClient.test.d.ts +2 -0
- package/dist/auth/betterAuthReactClient.test.d.ts.map +1 -0
- package/dist/auth/betterAuthReactClient.test.js +43 -0
- package/dist/auth/betterAuthReactClient.test.js.map +1 -0
- package/dist/client.adminWindowMutation.test.d.ts +2 -0
- package/dist/client.adminWindowMutation.test.d.ts.map +1 -0
- package/dist/client.adminWindowMutation.test.js +65 -0
- package/dist/client.adminWindowMutation.test.js.map +1 -0
- package/dist/client.d.ts +22 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +46 -4
- package/dist/client.js.map +1 -1
- package/dist/client.test.js +232 -2
- package/dist/client.test.js.map +1 -1
- package/dist/codegen/emitSdk.js +3 -3
- package/dist/codegen/emitSdk.js.map +1 -1
- package/dist/codegen/generate.test.js +32 -2
- package/dist/codegen/generate.test.js.map +1 -1
- package/dist/codegen/safeIdentifiers.d.ts +5 -0
- package/dist/codegen/safeIdentifiers.d.ts.map +1 -1
- package/dist/codegen/safeIdentifiers.js +8 -0
- package/dist/codegen/safeIdentifiers.js.map +1 -1
- package/dist/codegen/safeIdentifiers.test.js +20 -8
- package/dist/codegen/safeIdentifiers.test.js.map +1 -1
- package/dist/codegen/types.d.ts +4 -3
- package/dist/codegen/types.d.ts.map +1 -1
- package/dist/debug/debugChannel.test.js +5 -0
- package/dist/debug/debugChannel.test.js.map +1 -1
- package/dist/debug/debugLog.d.ts +1 -0
- package/dist/debug/debugLog.d.ts.map +1 -1
- package/dist/debug/debugLog.js +1 -0
- package/dist/debug/debugLog.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.d.ts +2 -0
- package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.d.ts.map +1 -0
- package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.js +19 -0
- package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.js.map +1 -0
- package/dist/mutations/outbox.d.ts +7 -3
- package/dist/mutations/outbox.d.ts.map +1 -1
- package/dist/mutations/outbox.js +6 -0
- package/dist/mutations/outbox.js.map +1 -1
- package/dist/mutations/outbox.test.js +17 -0
- package/dist/mutations/outbox.test.js.map +1 -1
- package/dist/storage/idb.d.ts +1 -0
- package/dist/storage/idb.d.ts.map +1 -1
- package/dist/storage/idb.js +1 -0
- package/dist/storage/idb.js.map +1 -1
- package/dist/storage/schema.d.ts.map +1 -1
- package/dist/storage/schema.js +1 -0
- package/dist/storage/schema.js.map +1 -1
- package/dist/storage/store.d.ts +3 -2
- package/dist/storage/store.d.ts.map +1 -1
- package/dist/storage/types.d.ts +1 -7
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/sync/deltaApplier.d.ts +2 -0
- package/dist/sync/deltaApplier.d.ts.map +1 -1
- package/dist/sync/deltaApplier.js +9 -0
- package/dist/sync/deltaApplier.js.map +1 -1
- package/dist/sync/deltaApplier.test.js +63 -0
- package/dist/sync/deltaApplier.test.js.map +1 -1
- package/dist/sync/entityRepair.d.ts +1 -0
- package/dist/sync/entityRepair.d.ts.map +1 -1
- package/dist/sync/entityRepair.js +1 -0
- package/dist/sync/entityRepair.js.map +1 -1
- package/dist/sync/httpChannel.d.ts +3 -1
- package/dist/sync/httpChannel.d.ts.map +1 -1
- package/dist/sync/httpChannel.js +3 -1
- package/dist/sync/httpChannel.js.map +1 -1
- package/dist/sync/httpChannel.test.js +68 -0
- package/dist/sync/httpChannel.test.js.map +1 -1
- package/dist/sync/replayCoordinator.d.ts +6 -12
- package/dist/sync/replayCoordinator.d.ts.map +1 -1
- package/dist/sync/replayCoordinator.js +9 -2
- package/dist/sync/replayCoordinator.js.map +1 -1
- package/dist/sync/socketTransport.d.ts +3 -45
- package/dist/sync/socketTransport.d.ts.map +1 -1
- package/dist/sync/socketTransport.js +31 -9
- package/dist/sync/socketTransport.js.map +1 -1
- package/dist/sync/socketTransport.test.js +7 -0
- package/dist/sync/socketTransport.test.js.map +1 -1
- package/dist/sync/transport.d.ts +3 -1
- package/dist/sync/transport.d.ts.map +1 -1
- package/dist/sync/windowHydrate.d.ts +25 -0
- package/dist/sync/windowHydrate.d.ts.map +1 -0
- package/dist/sync/windowHydrate.js +67 -0
- package/dist/sync/windowHydrate.js.map +1 -0
- package/dist/sync/windowHydrate.test.d.ts +2 -0
- package/dist/sync/windowHydrate.test.d.ts.map +1 -0
- package/dist/sync/windowHydrate.test.js +164 -0
- package/dist/sync/windowHydrate.test.js.map +1 -0
- package/dist/testing/fakeTransport.js +1 -1
- package/dist/testing/fakeTransport.js.map +1 -1
- package/dist/types.d.ts +15 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/auth/betterAuthReactClient.test.ts +50 -0
- package/src/auth/betterAuthReactClient.ts +33 -0
- package/src/client.adminWindowMutation.test.ts +69 -0
- package/src/client.test.ts +248 -2
- package/src/client.ts +85 -3
- package/src/codegen/emitSdk.ts +3 -3
- package/src/codegen/generate.test.ts +35 -2
- package/src/codegen/safeIdentifiers.test.ts +37 -10
- package/src/codegen/safeIdentifiers.ts +17 -0
- package/src/codegen/types.ts +3 -3
- package/src/debug/debugChannel.test.ts +6 -0
- package/src/debug/debugLog.ts +1 -0
- package/src/index.ts +4 -0
- package/src/isolated/defaultPersisterFactoryNativeErrors.isolated.ts +23 -0
- package/src/mutations/outbox.test.ts +20 -0
- package/src/mutations/outbox.ts +11 -3
- package/src/storage/idb.ts +1 -0
- package/src/storage/schema.ts +1 -0
- package/src/storage/store.ts +2 -2
- package/src/storage/types.ts +1 -8
- package/src/sync/deltaApplier.test.ts +70 -0
- package/src/sync/deltaApplier.ts +10 -0
- package/src/sync/entityRepair.ts +1 -0
- package/src/sync/httpChannel.test.ts +82 -0
- package/src/sync/httpChannel.ts +5 -0
- package/src/sync/replayCoordinator.ts +10 -3
- package/src/sync/socketTransport.test.ts +10 -2
- package/src/sync/socketTransport.ts +64 -22
- package/src/sync/transport.ts +1 -1
- package/src/sync/windowHydrate.test.ts +179 -0
- package/src/sync/windowHydrate.ts +97 -0
- package/src/testing/fakeTransport.ts +1 -1
- package/src/types.ts +16 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {describe, it} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
3
|
+
import {bridgeBetterAuthReactClient} from "./betterAuthReactClient";
|
|
4
|
+
|
|
5
|
+
const sessionResult = {data: {session: {token: "token-1"}, user: {id: "user-1"}}};
|
|
6
|
+
|
|
7
|
+
describe("bridgeBetterAuthReactClient", () => {
|
|
8
|
+
it("exposes the session atom as a subscribe surface", async () => {
|
|
9
|
+
const listeners: Array<(value: unknown) => void> = [];
|
|
10
|
+
let unsubscribed = false;
|
|
11
|
+
const reactClient = {
|
|
12
|
+
$store: {
|
|
13
|
+
atoms: {
|
|
14
|
+
session: {
|
|
15
|
+
subscribe: (listener: (value: unknown) => void): (() => void) => {
|
|
16
|
+
listeners.push(listener);
|
|
17
|
+
return (): void => {
|
|
18
|
+
unsubscribed = true;
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
getSession: async () => sessionResult,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const bridged = bridgeBetterAuthReactClient(reactClient);
|
|
28
|
+
|
|
29
|
+
assert.deepEqual(await bridged.getSession(), sessionResult);
|
|
30
|
+
assert.isFunction(bridged.useSession?.subscribe);
|
|
31
|
+
|
|
32
|
+
const received: unknown[] = [];
|
|
33
|
+
const unsubscribe = bridged.useSession?.subscribe?.((value) => {
|
|
34
|
+
received.push(value);
|
|
35
|
+
});
|
|
36
|
+
assert.equal(listeners.length, 1);
|
|
37
|
+
listeners[0]?.(sessionResult);
|
|
38
|
+
assert.deepEqual(received, [sessionResult]);
|
|
39
|
+
|
|
40
|
+
unsubscribe?.();
|
|
41
|
+
assert.isTrue(unsubscribed);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("omits useSession when the client exposes no session atom", async () => {
|
|
45
|
+
const bridged = bridgeBetterAuthReactClient({getSession: async () => sessionResult});
|
|
46
|
+
|
|
47
|
+
assert.isUndefined(bridged.useSession);
|
|
48
|
+
assert.deepEqual(await bridged.getSession(), sessionResult);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type {BetterAuthClientLike, BetterAuthGetSessionResult} from "./types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The Better Auth *react* client delivers session changes through a nanostore atom
|
|
5
|
+
* (`$store.atoms.session`), but its `useSession` is a React hook, not the `.subscribe`
|
|
6
|
+
* surface {@link betterAuthAdapter} looks for. Without the bridge the adapter falls back to
|
|
7
|
+
* polling `getSession()`, producing constant `/api/auth/get-session` traffic.
|
|
8
|
+
*
|
|
9
|
+
* Returns a client the adapter can subscribe to, or a `getSession`-only client when the
|
|
10
|
+
* atom is missing (a future Better Auth client shape change), which keeps the polling
|
|
11
|
+
* fallback as the safety net.
|
|
12
|
+
*/
|
|
13
|
+
/** The only member of the Better Auth react client this bridge reads statically. */
|
|
14
|
+
export interface BetterAuthReactClientLike {
|
|
15
|
+
getSession: () => Promise<BetterAuthGetSessionResult>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface SessionAtomLike {
|
|
19
|
+
subscribe: (listener: (value: unknown) => void) => () => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const bridgeBetterAuthReactClient = (
|
|
23
|
+
authClient: BetterAuthReactClientLike
|
|
24
|
+
): BetterAuthClientLike => {
|
|
25
|
+
const sessionAtom = (authClient as unknown as {$store?: {atoms?: {session?: SessionAtomLike}}})
|
|
26
|
+
.$store?.atoms?.session;
|
|
27
|
+
return {
|
|
28
|
+
getSession: () => authClient.getSession(),
|
|
29
|
+
...(sessionAtom
|
|
30
|
+
? {useSession: {subscribe: (listener): (() => void) => sessionAtom.subscribe(listener)}}
|
|
31
|
+
: {}),
|
|
32
|
+
};
|
|
33
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {afterEach, describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {createSyncDb} from "./client";
|
|
4
|
+
import {createFakeTransport} from "./testing/fakeTransport";
|
|
5
|
+
|
|
6
|
+
const makeAuthProvider = (userId: string | null = "user-1") => {
|
|
7
|
+
const listeners = new Set<() => void>();
|
|
8
|
+
const state = {userId};
|
|
9
|
+
return {
|
|
10
|
+
provider: {
|
|
11
|
+
getToken: async () => "token",
|
|
12
|
+
getUserId: async () => state.userId,
|
|
13
|
+
onAuthChange: (callback: () => void): (() => void) => {
|
|
14
|
+
listeners.add(callback);
|
|
15
|
+
return () => {
|
|
16
|
+
listeners.delete(callback);
|
|
17
|
+
};
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
describe("createSyncDb admin-window mutation mode", () => {
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
// no shared state
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("tags outbox rows with mutationMode adminWindow for window collections", async () => {
|
|
29
|
+
const transport = createFakeTransport();
|
|
30
|
+
const {provider} = makeAuthProvider();
|
|
31
|
+
const client = createSyncDb({
|
|
32
|
+
authProvider: provider,
|
|
33
|
+
baseUrl: "http://localhost:4000",
|
|
34
|
+
collections: ["todos"],
|
|
35
|
+
name: "admin-window-mutate-test",
|
|
36
|
+
transport,
|
|
37
|
+
windowCollections: ["todos"],
|
|
38
|
+
});
|
|
39
|
+
await client.start();
|
|
40
|
+
const {mutationId} = client.mutate({
|
|
41
|
+
collection: "todos",
|
|
42
|
+
data: {title: "window write"},
|
|
43
|
+
operation: "create",
|
|
44
|
+
});
|
|
45
|
+
expect(client.outbox.getMutation({mutationId})?.mutationMode).toBe("adminWindow");
|
|
46
|
+
await client.stop();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("does not tag product collection mutations", async () => {
|
|
50
|
+
const transport = createFakeTransport();
|
|
51
|
+
const {provider} = makeAuthProvider();
|
|
52
|
+
const client = createSyncDb({
|
|
53
|
+
authProvider: provider,
|
|
54
|
+
baseUrl: "http://localhost:4000",
|
|
55
|
+
collections: ["todos", "notes"],
|
|
56
|
+
name: "product-mutate-test",
|
|
57
|
+
transport,
|
|
58
|
+
windowCollections: ["todos"],
|
|
59
|
+
});
|
|
60
|
+
await client.start();
|
|
61
|
+
const {mutationId} = client.mutate({
|
|
62
|
+
collection: "notes",
|
|
63
|
+
data: {title: "product write"},
|
|
64
|
+
operation: "create",
|
|
65
|
+
});
|
|
66
|
+
expect(client.outbox.getMutation({mutationId})?.mutationMode).toBeUndefined();
|
|
67
|
+
await client.stop();
|
|
68
|
+
});
|
|
69
|
+
});
|
package/src/client.test.ts
CHANGED
|
@@ -197,9 +197,9 @@ const makeObservableTransport = (base: FakeTransport): ObservableTransport => {
|
|
|
197
197
|
events.push("disconnect");
|
|
198
198
|
base.disconnect();
|
|
199
199
|
},
|
|
200
|
-
subscribe: (collections: string[]): void => {
|
|
200
|
+
subscribe: (collections: string[], options?: {mode?: "window"}): void => {
|
|
201
201
|
events.push("subscribe");
|
|
202
|
-
base.subscribe(collections);
|
|
202
|
+
base.subscribe(collections, options);
|
|
203
203
|
},
|
|
204
204
|
},
|
|
205
205
|
};
|
|
@@ -496,6 +496,21 @@ describe("createSyncDb", () => {
|
|
|
496
496
|
await expect(client.reconcile()).resolves.toBeUndefined();
|
|
497
497
|
await client.stop();
|
|
498
498
|
});
|
|
499
|
+
|
|
500
|
+
it("window collection reconcile interval does not GET /sync/snapshot", async () => {
|
|
501
|
+
const snapshotStreams: string[] = [];
|
|
502
|
+
const harness = makeHarness({reconcileIntervalMs: 20, windowCollections: ["todos"]});
|
|
503
|
+
const fetchSnapshotPage = harness.http.channel.fetchSnapshotPage;
|
|
504
|
+
harness.http.channel.fetchSnapshotPage = async (args) => {
|
|
505
|
+
snapshotStreams.push(args.stream);
|
|
506
|
+
return fetchSnapshotPage(args);
|
|
507
|
+
};
|
|
508
|
+
const client = createSyncDb(harness.config);
|
|
509
|
+
await client.start();
|
|
510
|
+
await new Promise((resolve) => setTimeout(resolve, 70));
|
|
511
|
+
assert.deepEqual(snapshotStreams, []);
|
|
512
|
+
await client.stop();
|
|
513
|
+
});
|
|
499
514
|
});
|
|
500
515
|
|
|
501
516
|
describe("subscribe confirmation catch-up", () => {
|
|
@@ -554,6 +569,30 @@ describe("createSyncDb", () => {
|
|
|
554
569
|
await client.stop();
|
|
555
570
|
});
|
|
556
571
|
|
|
572
|
+
it("window subscribe confirmation does not GET /sync/snapshot", async () => {
|
|
573
|
+
const harness = makeHarness({windowCollections: ["todos"]});
|
|
574
|
+
const snapshotStreams: string[] = [];
|
|
575
|
+
const fetchSnapshotPage = harness.http.channel.fetchSnapshotPage;
|
|
576
|
+
harness.http.channel.fetchSnapshotPage = async (args) => {
|
|
577
|
+
snapshotStreams.push(args.stream);
|
|
578
|
+
return fetchSnapshotPage(args);
|
|
579
|
+
};
|
|
580
|
+
const client = createSyncDb(harness.config);
|
|
581
|
+
await client.start();
|
|
582
|
+
await flush();
|
|
583
|
+
snapshotStreams.length = 0;
|
|
584
|
+
|
|
585
|
+
harness.transport.confirmSubscribed({
|
|
586
|
+
collection: "todos",
|
|
587
|
+
mode: "window",
|
|
588
|
+
streams: ["todos|admin"],
|
|
589
|
+
});
|
|
590
|
+
await flush();
|
|
591
|
+
|
|
592
|
+
assert.deepEqual(snapshotStreams, []);
|
|
593
|
+
await client.stop();
|
|
594
|
+
});
|
|
595
|
+
|
|
557
596
|
it("does not page while simulated-offline", async () => {
|
|
558
597
|
const harness = makeHarness();
|
|
559
598
|
const client = createSyncDb(harness.config);
|
|
@@ -2986,4 +3025,211 @@ describe("createSyncDb", () => {
|
|
|
2986
3025
|
}
|
|
2987
3026
|
});
|
|
2988
3027
|
});
|
|
3028
|
+
|
|
3029
|
+
describe("forceResync", () => {
|
|
3030
|
+
it("skips when there is no HTTP channel", async () => {
|
|
3031
|
+
const harness = makeHarness();
|
|
3032
|
+
const client = createSyncDb({
|
|
3033
|
+
...harness.config,
|
|
3034
|
+
httpChannel: undefined,
|
|
3035
|
+
});
|
|
3036
|
+
const result = await client.forceResync();
|
|
3037
|
+
assert.deepEqual(result, {
|
|
3038
|
+
ok: false,
|
|
3039
|
+
purged: 0,
|
|
3040
|
+
reason: "noHttpChannel",
|
|
3041
|
+
repaired: 0,
|
|
3042
|
+
streams: 0,
|
|
3043
|
+
});
|
|
3044
|
+
});
|
|
3045
|
+
|
|
3046
|
+
it("skips while simulated offline", async () => {
|
|
3047
|
+
const harness = makeHarness();
|
|
3048
|
+
const client = createSyncDb(harness.config);
|
|
3049
|
+
await client.start();
|
|
3050
|
+
client.goOffline();
|
|
3051
|
+
const result = await client.forceResync();
|
|
3052
|
+
assert.equal(result.ok, false);
|
|
3053
|
+
assert.equal(result.reason, "offline");
|
|
3054
|
+
await client.stop();
|
|
3055
|
+
});
|
|
3056
|
+
|
|
3057
|
+
it("skips while auth-paused", async () => {
|
|
3058
|
+
const harness = makeHarness();
|
|
3059
|
+
harness.http.state.streamsError = new AuthRequiredError();
|
|
3060
|
+
const client = createSyncDb(harness.config);
|
|
3061
|
+
await client.start();
|
|
3062
|
+
await flush();
|
|
3063
|
+
const result = await client.forceResync();
|
|
3064
|
+
assert.equal(result.ok, false);
|
|
3065
|
+
assert.equal(result.reason, "authPaused");
|
|
3066
|
+
await client.stop();
|
|
3067
|
+
});
|
|
3068
|
+
|
|
3069
|
+
it("skips when the server and local store have no streams", async () => {
|
|
3070
|
+
const harness = makeHarness();
|
|
3071
|
+
harness.http.state.streams = [];
|
|
3072
|
+
const client = createSyncDb(harness.config);
|
|
3073
|
+
await client.start();
|
|
3074
|
+
await flush();
|
|
3075
|
+
const result = await client.forceResync();
|
|
3076
|
+
assert.equal(result.ok, false);
|
|
3077
|
+
assert.equal(result.reason, "noStreams");
|
|
3078
|
+
await client.stop();
|
|
3079
|
+
});
|
|
3080
|
+
|
|
3081
|
+
it("purges local rows and re-bootstraps from the snapshot", async () => {
|
|
3082
|
+
const harness = makeHarness();
|
|
3083
|
+
harness.http.state.pages[DEFAULT_STREAM] = {
|
|
3084
|
+
cursor: 1,
|
|
3085
|
+
entities: [{data: {title: "stale"}, deleted: false, id: "local-1", seq: 1}],
|
|
3086
|
+
frontierSeq: 1,
|
|
3087
|
+
hasMore: false,
|
|
3088
|
+
oldestRetainedSeq: 0,
|
|
3089
|
+
stream: DEFAULT_STREAM,
|
|
3090
|
+
};
|
|
3091
|
+
const client = createSyncDb(harness.config);
|
|
3092
|
+
await client.start();
|
|
3093
|
+
await flush();
|
|
3094
|
+
assert.deepEqual(client.store.getEntity({collection: "todos", id: "local-1"})?.data, {
|
|
3095
|
+
title: "stale",
|
|
3096
|
+
});
|
|
3097
|
+
|
|
3098
|
+
harness.http.state.pages[DEFAULT_STREAM] = {
|
|
3099
|
+
cursor: 9,
|
|
3100
|
+
entities: [{data: {title: "fresh"}, deleted: false, id: "server-1", seq: 9}],
|
|
3101
|
+
frontierSeq: 9,
|
|
3102
|
+
hasMore: false,
|
|
3103
|
+
oldestRetainedSeq: 0,
|
|
3104
|
+
stream: DEFAULT_STREAM,
|
|
3105
|
+
};
|
|
3106
|
+
const result = await client.forceResync();
|
|
3107
|
+
assert.equal(result.ok, true);
|
|
3108
|
+
assert.isAtLeast(result.streams, 1);
|
|
3109
|
+
assert.isAtLeast(result.purged, 1);
|
|
3110
|
+
assert.equal(client.store.getEntity({collection: "todos", id: "local-1"}), undefined);
|
|
3111
|
+
assert.deepEqual(client.store.getEntity({collection: "todos", id: "server-1"})?.data, {
|
|
3112
|
+
title: "fresh",
|
|
3113
|
+
});
|
|
3114
|
+
await client.stop();
|
|
3115
|
+
});
|
|
3116
|
+
|
|
3117
|
+
it("falls back to locally known streams when discovery 401s", async () => {
|
|
3118
|
+
const harness = makeHarness();
|
|
3119
|
+
harness.http.state.pages[DEFAULT_STREAM] = {
|
|
3120
|
+
cursor: 2,
|
|
3121
|
+
entities: [{data: {title: "kept"}, deleted: false, id: "k1", seq: 2}],
|
|
3122
|
+
frontierSeq: 2,
|
|
3123
|
+
hasMore: false,
|
|
3124
|
+
oldestRetainedSeq: 0,
|
|
3125
|
+
stream: DEFAULT_STREAM,
|
|
3126
|
+
};
|
|
3127
|
+
const client = createSyncDb(harness.config);
|
|
3128
|
+
await client.start();
|
|
3129
|
+
await flush();
|
|
3130
|
+
harness.http.state.streamsError = new AuthRequiredError();
|
|
3131
|
+
const result = await client.forceResync();
|
|
3132
|
+
assert.equal(result.ok, true);
|
|
3133
|
+
assert.equal(result.streams, 1);
|
|
3134
|
+
assert.deepEqual(client.store.getEntity({collection: "todos", id: "k1"})?.data, {
|
|
3135
|
+
title: "kept",
|
|
3136
|
+
});
|
|
3137
|
+
await client.stop();
|
|
3138
|
+
});
|
|
3139
|
+
|
|
3140
|
+
it("pauses and reports authPaused when a snapshot page 401s mid-resync", async () => {
|
|
3141
|
+
const harness = makeHarness();
|
|
3142
|
+
const client = createSyncDb(harness.config);
|
|
3143
|
+
await client.start();
|
|
3144
|
+
await flush();
|
|
3145
|
+
harness.http.channel.fetchSnapshotPage = async () => {
|
|
3146
|
+
throw new AuthRequiredError();
|
|
3147
|
+
};
|
|
3148
|
+
const result = await client.forceResync();
|
|
3149
|
+
assert.equal(result.ok, false);
|
|
3150
|
+
assert.equal(result.reason, "authPaused");
|
|
3151
|
+
assert.equal(client.getSyncStatus().paused, "auth");
|
|
3152
|
+
await client.stop();
|
|
3153
|
+
});
|
|
3154
|
+
|
|
3155
|
+
it("rethrows a non-auth discovery error", async () => {
|
|
3156
|
+
const harness = makeHarness();
|
|
3157
|
+
const client = createSyncDb(harness.config);
|
|
3158
|
+
await client.start();
|
|
3159
|
+
await flush();
|
|
3160
|
+
harness.http.state.streamsError = new Error("resync boom");
|
|
3161
|
+
await expect(client.forceResync()).rejects.toThrow("resync boom");
|
|
3162
|
+
await client.stop();
|
|
3163
|
+
});
|
|
3164
|
+
|
|
3165
|
+
it("repairs marked entities after re-bootstrap", async () => {
|
|
3166
|
+
const harness = makeHarness();
|
|
3167
|
+
harness.http.state.pages[DEFAULT_STREAM] = {
|
|
3168
|
+
cursor: 1,
|
|
3169
|
+
entities: [{data: {title: "broken"}, deleted: false, id: "r1", seq: 1}],
|
|
3170
|
+
frontierSeq: 1,
|
|
3171
|
+
hasMore: false,
|
|
3172
|
+
oldestRetainedSeq: 0,
|
|
3173
|
+
stream: DEFAULT_STREAM,
|
|
3174
|
+
};
|
|
3175
|
+
const client = createSyncDb(harness.config);
|
|
3176
|
+
await client.start();
|
|
3177
|
+
await flush();
|
|
3178
|
+
client.store.markNeedsRepair({
|
|
3179
|
+
collection: "todos",
|
|
3180
|
+
entityId: "r1",
|
|
3181
|
+
missedSeq: 1,
|
|
3182
|
+
stream: DEFAULT_STREAM,
|
|
3183
|
+
});
|
|
3184
|
+
harness.http.channel.fetchEntities = async () => ({
|
|
3185
|
+
entities: [
|
|
3186
|
+
{
|
|
3187
|
+
data: {title: "repaired"},
|
|
3188
|
+
deleted: false,
|
|
3189
|
+
id: "r1",
|
|
3190
|
+
seq: 2,
|
|
3191
|
+
stream: DEFAULT_STREAM,
|
|
3192
|
+
},
|
|
3193
|
+
],
|
|
3194
|
+
});
|
|
3195
|
+
const result = await client.forceResync();
|
|
3196
|
+
assert.equal(result.ok, true);
|
|
3197
|
+
await client.stop();
|
|
3198
|
+
});
|
|
3199
|
+
|
|
3200
|
+
it("abandons the resync when the user changes during discovery", async () => {
|
|
3201
|
+
const harness = makeHarness();
|
|
3202
|
+
const client = createSyncDb(harness.config);
|
|
3203
|
+
await client.start();
|
|
3204
|
+
await flush();
|
|
3205
|
+
const originalFetch = harness.http.channel.fetchStreams;
|
|
3206
|
+
harness.http.channel.fetchStreams = async () => {
|
|
3207
|
+
harness.auth.setUserId("other-user");
|
|
3208
|
+
harness.auth.emitAuthChange();
|
|
3209
|
+
await flush();
|
|
3210
|
+
return originalFetch();
|
|
3211
|
+
};
|
|
3212
|
+
const result = await client.forceResync();
|
|
3213
|
+
assert.equal(result.ok, false);
|
|
3214
|
+
assert.equal(result.reason, "superseded");
|
|
3215
|
+
await client.stop();
|
|
3216
|
+
});
|
|
3217
|
+
|
|
3218
|
+
it("abandons the resync when the user changes during bootstrap", async () => {
|
|
3219
|
+
const harness = makeHarness();
|
|
3220
|
+
const client = createSyncDb(harness.config);
|
|
3221
|
+
await client.start();
|
|
3222
|
+
await flush();
|
|
3223
|
+
harness.http.channel.fetchSnapshotPage = async ({stream}) => {
|
|
3224
|
+
harness.auth.setUserId("other-user");
|
|
3225
|
+
harness.auth.emitAuthChange();
|
|
3226
|
+
await flush();
|
|
3227
|
+
return emptyPage(stream);
|
|
3228
|
+
};
|
|
3229
|
+
const result = await client.forceResync();
|
|
3230
|
+
assert.equal(result.ok, false);
|
|
3231
|
+
assert.equal(result.reason, "superseded");
|
|
3232
|
+
await client.stop();
|
|
3233
|
+
});
|
|
3234
|
+
});
|
|
2989
3235
|
});
|
package/src/client.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {AuthRequiredError, createHttpChannel, type HttpChannel} from "./sync/htt
|
|
|
22
22
|
import {createReplayCoordinator, type ReplayCoordinator} from "./sync/replayCoordinator";
|
|
23
23
|
import {createSocketTransport} from "./sync/socketTransport";
|
|
24
24
|
import type {SendMutationBatchResult, SendMutationResult, SyncTransport} from "./sync/transport";
|
|
25
|
+
import {type HydrateWindowEntitiesResult, hydrateWindowEntities} from "./sync/windowHydrate";
|
|
25
26
|
import type {
|
|
26
27
|
AuthProvider,
|
|
27
28
|
ConflictResolutionStrategy,
|
|
@@ -67,6 +68,12 @@ export interface SyncDbConfig {
|
|
|
67
68
|
baseUrl?: string;
|
|
68
69
|
/** Transport override (tests inject a fake; default is the socket transport). */
|
|
69
70
|
transport?: SyncTransport;
|
|
71
|
+
/**
|
|
72
|
+
* Optional selected organization id for `X-Organization-Id` on HTTP sync
|
|
73
|
+
* calls and the `organizationId` field on socket mutate payloads. Read at
|
|
74
|
+
* send time so admin org switches apply without reconnecting.
|
|
75
|
+
*/
|
|
76
|
+
organizationIdProvider?: () => string | undefined;
|
|
70
77
|
/** HTTP channel override (default is built from baseUrl when present). */
|
|
71
78
|
httpChannel?: HttpChannel;
|
|
72
79
|
/** Persister factory override (default is the platform default factory). */
|
|
@@ -83,6 +90,12 @@ export interface SyncDbConfig {
|
|
|
83
90
|
idbSetImpl?: DefaultPersisterFactoryConfig["idbSetImpl"];
|
|
84
91
|
/** Periodic reconcile interval in ms; 0 disables (default 5 minutes). */
|
|
85
92
|
reconcileIntervalMs?: number;
|
|
93
|
+
/**
|
|
94
|
+
* Collections that subscribe in admin window mode: join `{collection}|admin`,
|
|
95
|
+
* skip `GET /sync/snapshot` paging (startup, catch-up, and the reconcile timer).
|
|
96
|
+
* Hydrate rows via REST membership + `/sync/entities` instead.
|
|
97
|
+
*/
|
|
98
|
+
windowCollections?: string[];
|
|
86
99
|
/** Rate limit for seq-jump-triggered reconciles per stream (default 30s). */
|
|
87
100
|
seqJumpReconcileMinIntervalMs?: number;
|
|
88
101
|
/** Millisecond clock, injectable for deterministic rate-limit tests. */
|
|
@@ -229,6 +242,15 @@ export interface SyncDb {
|
|
|
229
242
|
mutate: (args: MutateArgs) => {mutationId: string; id: string};
|
|
230
243
|
/** Snapshot-from-cursor catch-up for every collection (no-op without HTTP). */
|
|
231
244
|
reconcile: () => Promise<void>;
|
|
245
|
+
/**
|
|
246
|
+
* Upsert a window of known ids (admin membership). Missing rows are fetched
|
|
247
|
+
* from `GET /sync/entities`; ids the server does not return are ignored.
|
|
248
|
+
*/
|
|
249
|
+
hydrateWindow: (args: {
|
|
250
|
+
collection: string;
|
|
251
|
+
ids: string[];
|
|
252
|
+
restRows?: Record<string, unknown>;
|
|
253
|
+
}) => Promise<HydrateWindowEntitiesResult>;
|
|
232
254
|
/**
|
|
233
255
|
* Purge every known stream locally and re-bootstrap from cursor 0. Use when
|
|
234
256
|
* devices have diverged and a full server snapshot is needed without wiping
|
|
@@ -295,17 +317,43 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
|
|
|
295
317
|
authProvider: config.authProvider,
|
|
296
318
|
// biome-ignore lint/style/noNonNullAssertion: guarded above — no transport implies baseUrl.
|
|
297
319
|
baseUrl: config.baseUrl!,
|
|
320
|
+
organizationIdProvider: config.organizationIdProvider,
|
|
298
321
|
});
|
|
299
322
|
const httpChannel =
|
|
300
323
|
config.httpChannel ??
|
|
301
324
|
(config.baseUrl
|
|
302
|
-
? createHttpChannel({
|
|
325
|
+
? createHttpChannel({
|
|
326
|
+
authProvider: config.authProvider,
|
|
327
|
+
baseUrl: config.baseUrl,
|
|
328
|
+
organizationIdProvider: config.organizationIdProvider,
|
|
329
|
+
})
|
|
303
330
|
: undefined);
|
|
304
331
|
|
|
305
332
|
const store = createSyncStore({
|
|
306
333
|
collections: config.collections,
|
|
307
334
|
now: () => DateTime.fromMillis(now()).toISO() ?? new Date(now()).toISOString(),
|
|
308
335
|
});
|
|
336
|
+
const windowCollectionSet = new Set(config.windowCollections ?? []);
|
|
337
|
+
const isWindowCollection = (collection: string): boolean => windowCollectionSet.has(collection);
|
|
338
|
+
const skipSnapshotPaging = ({
|
|
339
|
+
collection,
|
|
340
|
+
stream,
|
|
341
|
+
mode,
|
|
342
|
+
}: {
|
|
343
|
+
collection: string;
|
|
344
|
+
stream: string;
|
|
345
|
+
mode?: "window";
|
|
346
|
+
}): boolean => isWindowCollection(collection) || mode === "window" || stream.endsWith("|admin");
|
|
347
|
+
const subscribeConfiguredCollections = (): void => {
|
|
348
|
+
const windowed = config.collections.filter((collection) => isWindowCollection(collection));
|
|
349
|
+
const full = config.collections.filter((collection) => !isWindowCollection(collection));
|
|
350
|
+
if (full.length > 0) {
|
|
351
|
+
transport.subscribe(full);
|
|
352
|
+
}
|
|
353
|
+
if (windowed.length > 0) {
|
|
354
|
+
transport.subscribe(windowed, {mode: "window"});
|
|
355
|
+
}
|
|
356
|
+
};
|
|
309
357
|
const outbox = createOutbox({store});
|
|
310
358
|
const debugLog = resolveDebugLog(config.debug);
|
|
311
359
|
// Mirror the debug log across browser windows/tabs (web only) so a debugger
|
|
@@ -649,6 +697,9 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
|
|
|
649
697
|
return;
|
|
650
698
|
}
|
|
651
699
|
for (const [stream, collection] of streams) {
|
|
700
|
+
if (skipSnapshotPaging({collection, stream})) {
|
|
701
|
+
continue;
|
|
702
|
+
}
|
|
652
703
|
await bootstrapStream({channel: httpChannel, collection, store, stream});
|
|
653
704
|
if (isSuperseded()) {
|
|
654
705
|
return;
|
|
@@ -739,9 +790,11 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
|
|
|
739
790
|
*/
|
|
740
791
|
const catchUpSubscribedStreams = async ({
|
|
741
792
|
collection,
|
|
793
|
+
mode,
|
|
742
794
|
streams,
|
|
743
795
|
}: {
|
|
744
796
|
collection: string;
|
|
797
|
+
mode?: "window";
|
|
745
798
|
streams: string[];
|
|
746
799
|
}): Promise<void> => {
|
|
747
800
|
if (!httpChannel || simulatedOffline || authPaused || streams.length === 0) {
|
|
@@ -750,6 +803,9 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
|
|
|
750
803
|
if (!store.collections.includes(collection)) {
|
|
751
804
|
return;
|
|
752
805
|
}
|
|
806
|
+
if (mode === "window" || isWindowCollection(collection)) {
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
753
809
|
const myGeneration = generation;
|
|
754
810
|
const myUserId = currentUserId;
|
|
755
811
|
const isSuperseded = (): boolean => generation !== myGeneration || currentUserId !== myUserId;
|
|
@@ -759,6 +815,9 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
|
|
|
759
815
|
if (isSuperseded()) {
|
|
760
816
|
return;
|
|
761
817
|
}
|
|
818
|
+
if (skipSnapshotPaging({collection, mode, stream})) {
|
|
819
|
+
continue;
|
|
820
|
+
}
|
|
762
821
|
store.addKnownStream({collection, stream});
|
|
763
822
|
await bootstrapStream({channel: httpChannel, collection, store, stream});
|
|
764
823
|
}
|
|
@@ -1280,7 +1339,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
|
|
|
1280
1339
|
warn("transport reconnect after user change failed; continuing offline")(error);
|
|
1281
1340
|
}
|
|
1282
1341
|
bindDeltaHandler();
|
|
1283
|
-
|
|
1342
|
+
subscribeConfiguredCollections();
|
|
1284
1343
|
};
|
|
1285
1344
|
|
|
1286
1345
|
const handleStatusChange = ({
|
|
@@ -1516,7 +1575,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
|
|
|
1516
1575
|
if (generation !== myGeneration) {
|
|
1517
1576
|
return;
|
|
1518
1577
|
}
|
|
1519
|
-
|
|
1578
|
+
subscribeConfiguredCollections();
|
|
1520
1579
|
// C2: run an initial stream discovery + per-stream bootstrap on start (not only via
|
|
1521
1580
|
// the reconnect status event) so a client that starts offline-then-online, or with a
|
|
1522
1581
|
// warm socket, still backfills newly-joined streams and drains legacy cursors.
|
|
@@ -1647,6 +1706,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
|
|
|
1647
1706
|
entityId,
|
|
1648
1707
|
...(maxAttempts !== undefined ? {maxAttempts} : {}),
|
|
1649
1708
|
mutationId,
|
|
1709
|
+
...(windowCollectionSet.has(collection) ? {mutationMode: "adminWindow" as const} : {}),
|
|
1650
1710
|
operation,
|
|
1651
1711
|
userId: currentUserId,
|
|
1652
1712
|
});
|
|
@@ -1774,6 +1834,27 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
|
|
|
1774
1834
|
return statuses;
|
|
1775
1835
|
};
|
|
1776
1836
|
|
|
1837
|
+
const hydrateWindow = async ({
|
|
1838
|
+
collection,
|
|
1839
|
+
ids,
|
|
1840
|
+
restRows,
|
|
1841
|
+
}: {
|
|
1842
|
+
collection: string;
|
|
1843
|
+
ids: string[];
|
|
1844
|
+
restRows?: Record<string, unknown>;
|
|
1845
|
+
}): Promise<HydrateWindowEntitiesResult> => {
|
|
1846
|
+
if (!httpChannel) {
|
|
1847
|
+
throw new Error("hydrateWindow requires an HTTP channel");
|
|
1848
|
+
}
|
|
1849
|
+
return hydrateWindowEntities({
|
|
1850
|
+
channel: httpChannel,
|
|
1851
|
+
collection,
|
|
1852
|
+
ids,
|
|
1853
|
+
restRows,
|
|
1854
|
+
store,
|
|
1855
|
+
});
|
|
1856
|
+
};
|
|
1857
|
+
|
|
1777
1858
|
const getSyncStatus = (): SyncStatus => {
|
|
1778
1859
|
const drainProgress = currentUserId
|
|
1779
1860
|
? coordinator.getDrainProgress({userId: currentUserId})
|
|
@@ -1866,6 +1947,7 @@ export const createSyncDb = (config: SyncDbConfig): SyncDb => {
|
|
|
1866
1947
|
getSyncStatus,
|
|
1867
1948
|
goOffline,
|
|
1868
1949
|
goOnline,
|
|
1950
|
+
hydrateWindow,
|
|
1869
1951
|
mutate,
|
|
1870
1952
|
onStatusChange,
|
|
1871
1953
|
outbox,
|
package/src/codegen/emitSdk.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {emitInterface} from "./emitTypes";
|
|
2
2
|
import {friendlyHookNames} from "./hookNames";
|
|
3
|
-
import {assertTsIdentifier, emitTsString} from "./safeIdentifiers";
|
|
3
|
+
import {assertSyncCollectionName, assertTsIdentifier, emitTsString} from "./safeIdentifiers";
|
|
4
4
|
import type {CodegenConfigFile, DiscoveredCollection} from "./types";
|
|
5
5
|
|
|
6
6
|
const GENERATED_HEADER = `// GENERATED by @terreno/syncdb (terreno-syncdb-codegen). Do not edit.
|
|
@@ -38,7 +38,7 @@ export const emitSdk = ({
|
|
|
38
38
|
const names: string[] = [];
|
|
39
39
|
|
|
40
40
|
for (const collection of collections) {
|
|
41
|
-
const collectionName =
|
|
41
|
+
const collectionName = assertSyncCollectionName({
|
|
42
42
|
label: "collection",
|
|
43
43
|
value: collection.collection,
|
|
44
44
|
});
|
|
@@ -60,7 +60,7 @@ export const emitSdk = ({
|
|
|
60
60
|
typeBlocks.push(emitInterface({name: updateName, schema: collection.updateSchema}));
|
|
61
61
|
|
|
62
62
|
const hooks = friendlyHookNames({
|
|
63
|
-
collection:
|
|
63
|
+
collection: collection.collection,
|
|
64
64
|
entityName,
|
|
65
65
|
});
|
|
66
66
|
const list = assertTsIdentifier({label: "hook name", value: hooks.list});
|