@terreno/syncdb 57.3.0 → 57.6.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 +3 -3
- package/dist/auth/betterAuthAdapter.js +1 -1
- package/dist/auth/betterAuthAdapter.js.map +1 -1
- 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 +89 -12
- package/dist/client.js.map +1 -1
- package/dist/client.test.js +308 -12
- 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/crypto/keyProviders.js +1 -1
- package/dist/crypto/keyProviders.js.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/debug/devtools.d.ts +6 -0
- package/dist/debug/devtools.d.ts.map +1 -0
- package/dist/debug/devtools.js +111 -0
- package/dist/debug/devtools.js.map +1 -0
- package/dist/debug/devtools.test.d.ts +2 -0
- package/dist/debug/devtools.test.d.ts.map +1 -0
- package/dist/debug/devtools.test.js +168 -0
- package/dist/debug/devtools.test.js.map +1 -0
- 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/react/collectionHooks.js +2 -2
- package/dist/react/collectionHooks.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/store.test.js +6 -6
- package/dist/storage/store.test.js.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 +44 -16
- package/dist/sync/replayCoordinator.js.map +1 -1
- package/dist/sync/replayCoordinator.test.js +29 -10
- package/dist/sync/replayCoordinator.test.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 +73 -9
- package/dist/sync/socketTransport.js.map +1 -1
- package/dist/sync/socketTransport.test.js +41 -1
- package/dist/sync/socketTransport.test.js.map +1 -1
- package/dist/sync/transport.d.ts +9 -1
- package/dist/sync/transport.d.ts.map +1 -1
- package/dist/sync/transport.js +1 -1
- package/dist/sync/transport.js.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 +9 -4
- 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 +321 -3
- package/src/client.ts +125 -8
- 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/debug/devtools.test.ts +234 -0
- package/src/debug/devtools.ts +196 -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/react/collectionHooks.ts +2 -2
- 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.test.ts +28 -0
- package/src/sync/replayCoordinator.ts +53 -14
- package/src/sync/socketTransport.test.ts +45 -2
- package/src/sync/socketTransport.ts +106 -20
- package/src/sync/transport.ts +7 -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
|
@@ -20,7 +20,7 @@ interface TestServer {
|
|
|
20
20
|
/** Sockets that have connected, most recent last. */
|
|
21
21
|
sockets: ServerSocket[];
|
|
22
22
|
/** sync:subscribe payloads received, in order. */
|
|
23
|
-
subscribes: {collections: string[]}[];
|
|
23
|
+
subscribes: {collections: string[]; mode?: string}[];
|
|
24
24
|
/** Handler answering sync:mutate; replace per test. */
|
|
25
25
|
mutateHandler: (request: SyncMutateRequest, socket: ServerSocket) => void;
|
|
26
26
|
/** Handler answering sync:mutateBatch; replace per test (no-op = unsupported). */
|
|
@@ -53,7 +53,7 @@ const startServer = async (): Promise<TestServer> => {
|
|
|
53
53
|
};
|
|
54
54
|
io.on("connection", (socket) => {
|
|
55
55
|
server.sockets.push(socket);
|
|
56
|
-
socket.on("sync:subscribe", (payload: {collections: string[]}) => {
|
|
56
|
+
socket.on("sync:subscribe", (payload: {collections: string[]; mode?: string}) => {
|
|
57
57
|
server.subscribes.push(payload);
|
|
58
58
|
});
|
|
59
59
|
socket.on("sync:mutate", (request: SyncMutateRequest) => {
|
|
@@ -142,6 +142,14 @@ describe("createSocketTransport", () => {
|
|
|
142
142
|
expect(server.sockets).toHaveLength(1);
|
|
143
143
|
});
|
|
144
144
|
|
|
145
|
+
it("subscribe emits window mode for window collections", async () => {
|
|
146
|
+
const subscriber = makeTransport();
|
|
147
|
+
subscriber.subscribe(["todos"], {mode: "window"});
|
|
148
|
+
await subscriber.connect();
|
|
149
|
+
await waitUntil(() => server.subscribes.length === 1);
|
|
150
|
+
expect(server.subscribes).toEqual([{collections: ["todos"], mode: "window"}]);
|
|
151
|
+
});
|
|
152
|
+
|
|
145
153
|
it("subscribe emits sync:subscribe when connected and replays it on connect", async () => {
|
|
146
154
|
const subscriber = makeTransport();
|
|
147
155
|
// Subscribed before connect: sent once the connection opens.
|
|
@@ -341,6 +349,41 @@ describe("createSocketTransport", () => {
|
|
|
341
349
|
expect(seen).toHaveLength(1);
|
|
342
350
|
});
|
|
343
351
|
|
|
352
|
+
it("coalesces deltas delivered across tasks until the next animation frame", async () => {
|
|
353
|
+
const originalRequestAnimationFrame = globalThis.requestAnimationFrame;
|
|
354
|
+
let flushFrame: FrameRequestCallback | undefined;
|
|
355
|
+
globalThis.requestAnimationFrame = (callback: FrameRequestCallback): number => {
|
|
356
|
+
flushFrame = callback;
|
|
357
|
+
return 1;
|
|
358
|
+
};
|
|
359
|
+
try {
|
|
360
|
+
const receiver = makeTransport();
|
|
361
|
+
const batches: SyncDelta[][] = [];
|
|
362
|
+
receiver.onDeltaBatch?.((deltas) => batches.push(deltas));
|
|
363
|
+
await receiver.connect();
|
|
364
|
+
|
|
365
|
+
const first: SyncDelta = {
|
|
366
|
+
collection: "todos",
|
|
367
|
+
data: {title: "first"},
|
|
368
|
+
id: "t1",
|
|
369
|
+
method: "create",
|
|
370
|
+
seq: 1,
|
|
371
|
+
stream: "todos|owner:u1",
|
|
372
|
+
};
|
|
373
|
+
const second: SyncDelta = {...first, data: {title: "second"}, id: "t2", seq: 2};
|
|
374
|
+
server.sockets[0]?.emit("sync:delta", first);
|
|
375
|
+
await waitUntil(() => flushFrame !== undefined);
|
|
376
|
+
server.sockets[0]?.emit("sync:delta", second);
|
|
377
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
378
|
+
|
|
379
|
+
expect(batches).toEqual([]);
|
|
380
|
+
flushFrame?.(0);
|
|
381
|
+
expect(batches).toEqual([[first, second]]);
|
|
382
|
+
} finally {
|
|
383
|
+
globalThis.requestAnimationFrame = originalRequestAnimationFrame;
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
|
|
344
387
|
it("sendMutationBatch resolves results from the Socket.io ack callback (FIX 5)", async () => {
|
|
345
388
|
server.mutateBatchHandler = (request, socket, ack) => {
|
|
346
389
|
socket.emit("sync:batchReceived", {batchId: request.batchId});
|
|
@@ -28,7 +28,7 @@ import {
|
|
|
28
28
|
* just slow) and the client waits the full {@link batchTimeoutMs} instead of
|
|
29
29
|
* falling back (FIX 5).
|
|
30
30
|
*/
|
|
31
|
-
|
|
31
|
+
const BATCH_UNSUPPORTED_GRACE_MS = 2_000;
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
34
|
* Compute the batch send timeout once a `sync:batchReceived` receipt has
|
|
@@ -37,7 +37,7 @@ export const BATCH_UNSUPPORTED_GRACE_MS = 2_000;
|
|
|
37
37
|
* timed out prematurely just because it's slower than a single mutation
|
|
38
38
|
* (FIX 5).
|
|
39
39
|
*/
|
|
40
|
-
|
|
40
|
+
const batchTimeoutMs = (mutationCount: number, perMutationTimeoutMs: number): number =>
|
|
41
41
|
Math.max(perMutationTimeoutMs, mutationCount * 1_000);
|
|
42
42
|
|
|
43
43
|
export interface SocketTransportConfig {
|
|
@@ -54,6 +54,8 @@ export interface SocketTransportConfig {
|
|
|
54
54
|
* dev server does not fail the first handshake.
|
|
55
55
|
*/
|
|
56
56
|
transports?: string[];
|
|
57
|
+
/** Selected organization id attached to mutate payloads when present. */
|
|
58
|
+
organizationIdProvider?: () => string | undefined;
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
interface PendingMutation {
|
|
@@ -86,15 +88,28 @@ interface PendingMutation {
|
|
|
86
88
|
* subscribed collections because server-side subscription state is
|
|
87
89
|
* per-connection.
|
|
88
90
|
*/
|
|
91
|
+
const withOrganizationId = <T extends object>(
|
|
92
|
+
payload: T,
|
|
93
|
+
organizationIdProvider?: () => string | undefined
|
|
94
|
+
): T & {organizationId?: string} => {
|
|
95
|
+
const organizationId = organizationIdProvider?.()?.trim();
|
|
96
|
+
if (!organizationId) {
|
|
97
|
+
return payload;
|
|
98
|
+
}
|
|
99
|
+
return {...payload, organizationId};
|
|
100
|
+
};
|
|
101
|
+
|
|
89
102
|
export const createSocketTransport = ({
|
|
90
103
|
baseUrl,
|
|
91
104
|
authProvider,
|
|
92
105
|
timeoutMs = DEFAULT_MUTATION_TIMEOUT_MS,
|
|
93
106
|
batchUnsupportedGraceMs = BATCH_UNSUPPORTED_GRACE_MS,
|
|
94
107
|
transports = ["polling", "websocket"],
|
|
108
|
+
organizationIdProvider,
|
|
95
109
|
}: SocketTransportConfig): SyncTransport => {
|
|
96
110
|
const deltaListeners = new Set<(delta: SyncDelta) => void>();
|
|
97
111
|
const subscribedListeners = new Set<(subscribed: SyncSubscribed) => void>();
|
|
112
|
+
const deltaBatchListeners = new Set<(deltas: SyncDelta[]) => void>();
|
|
98
113
|
const statusListeners = new Set<(status: TransportStatus) => void>();
|
|
99
114
|
const pending = new Map<string, PendingMutation>();
|
|
100
115
|
const pendingBatches = new Map<
|
|
@@ -112,6 +127,34 @@ export const createSocketTransport = ({
|
|
|
112
127
|
>();
|
|
113
128
|
let nextBatchId = 1;
|
|
114
129
|
const subscribed = new Set<string>();
|
|
130
|
+
const subscribeMode = new Map<string, "window">();
|
|
131
|
+
const inboundDeltaBatches = new Map<(deltas: SyncDelta[]) => void, SyncDelta[]>();
|
|
132
|
+
let isInboundDeltaBatchScheduled = false;
|
|
133
|
+
|
|
134
|
+
const scheduleInboundDeltaBatchFlush = (): void => {
|
|
135
|
+
if (isInboundDeltaBatchScheduled) {
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
isInboundDeltaBatchScheduled = true;
|
|
139
|
+
// Socket.IO can deliver a high-volume stream in separate tasks. Deferring to
|
|
140
|
+
// the next paint coalesces that stream into one store transaction, so React
|
|
141
|
+
// does not render once per delta. Non-DOM environments retain microtask
|
|
142
|
+
// semantics for SSR and transport tests.
|
|
143
|
+
if (typeof globalThis.requestAnimationFrame === "function") {
|
|
144
|
+
globalThis.requestAnimationFrame(flushInboundDeltaBatch);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
queueMicrotask(flushInboundDeltaBatch);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const flushInboundDeltaBatch = (): void => {
|
|
151
|
+
isInboundDeltaBatchScheduled = false;
|
|
152
|
+
const batches = [...inboundDeltaBatches];
|
|
153
|
+
inboundDeltaBatches.clear();
|
|
154
|
+
for (const [listener, deltas] of batches) {
|
|
155
|
+
listener(deltas);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
115
158
|
|
|
116
159
|
const socket: Socket = io(baseUrl, {
|
|
117
160
|
auth: (callback) => {
|
|
@@ -176,18 +219,31 @@ export const createSocketTransport = ({
|
|
|
176
219
|
for (const listener of deltaListeners) {
|
|
177
220
|
listener(delta);
|
|
178
221
|
}
|
|
179
|
-
|
|
180
|
-
socket.on("sync:subscribed", (payload: {collection?: unknown; streams?: unknown}) => {
|
|
181
|
-
if (typeof payload?.collection !== "string" || !Array.isArray(payload.streams)) {
|
|
222
|
+
if (deltaBatchListeners.size === 0) {
|
|
182
223
|
return;
|
|
183
224
|
}
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
listener({collection: payload.collection, streams});
|
|
225
|
+
for (const listener of deltaBatchListeners) {
|
|
226
|
+
const batch = inboundDeltaBatches.get(listener) ?? [];
|
|
227
|
+
batch.push(delta);
|
|
228
|
+
inboundDeltaBatches.set(listener, batch);
|
|
189
229
|
}
|
|
230
|
+
scheduleInboundDeltaBatchFlush();
|
|
190
231
|
});
|
|
232
|
+
socket.on(
|
|
233
|
+
"sync:subscribed",
|
|
234
|
+
(payload: {collection?: unknown; streams?: unknown; mode?: unknown}) => {
|
|
235
|
+
if (typeof payload?.collection !== "string" || !Array.isArray(payload.streams)) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const streams = payload.streams.filter(
|
|
239
|
+
(stream: unknown): stream is string => typeof stream === "string"
|
|
240
|
+
);
|
|
241
|
+
const mode = payload.mode === "window" ? ("window" as const) : undefined;
|
|
242
|
+
for (const listener of subscribedListeners) {
|
|
243
|
+
listener({collection: payload.collection, streams, ...(mode ? {mode} : {})});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
);
|
|
191
247
|
socket.on("sync:ack", (ack: SyncAck) => {
|
|
192
248
|
settle(ack.mutationId, {ack, type: "ack"});
|
|
193
249
|
});
|
|
@@ -213,7 +269,18 @@ export const createSocketTransport = ({
|
|
|
213
269
|
socket.on("connect", () => {
|
|
214
270
|
// Server-side subscriptions are per-connection: re-subscribe on reconnect.
|
|
215
271
|
if (subscribed.size > 0) {
|
|
216
|
-
|
|
272
|
+
const windowed = [...subscribed].filter(
|
|
273
|
+
(collection) => subscribeMode.get(collection) === "window"
|
|
274
|
+
);
|
|
275
|
+
const full = [...subscribed].filter(
|
|
276
|
+
(collection) => subscribeMode.get(collection) !== "window"
|
|
277
|
+
);
|
|
278
|
+
if (full.length > 0) {
|
|
279
|
+
socket.emit("sync:subscribe", {collections: full});
|
|
280
|
+
}
|
|
281
|
+
if (windowed.length > 0) {
|
|
282
|
+
socket.emit("sync:subscribe", {collections: windowed, mode: "window"});
|
|
283
|
+
}
|
|
217
284
|
}
|
|
218
285
|
notifyStatus(true);
|
|
219
286
|
});
|
|
@@ -257,14 +324,22 @@ export const createSocketTransport = ({
|
|
|
257
324
|
rejectAllPending("Transport disconnected");
|
|
258
325
|
};
|
|
259
326
|
|
|
260
|
-
const subscribe = (collections: string[]): void => {
|
|
327
|
+
const subscribe = (collections: string[], options?: {mode?: "window"}): void => {
|
|
261
328
|
for (const collection of collections) {
|
|
262
329
|
subscribed.add(collection);
|
|
330
|
+
if (options?.mode === "window") {
|
|
331
|
+
subscribeMode.set(collection, "window");
|
|
332
|
+
} else {
|
|
333
|
+
subscribeMode.delete(collection);
|
|
334
|
+
}
|
|
263
335
|
}
|
|
264
336
|
// Only emit when connected; the connect handler (re)subscribes otherwise —
|
|
265
337
|
// Socket.io would buffer a disconnected emit and duplicate the subscribe.
|
|
266
338
|
if (socket.connected && collections.length > 0) {
|
|
267
|
-
socket.emit(
|
|
339
|
+
socket.emit(
|
|
340
|
+
"sync:subscribe",
|
|
341
|
+
options?.mode === "window" ? {collections, mode: "window"} : {collections}
|
|
342
|
+
);
|
|
268
343
|
}
|
|
269
344
|
};
|
|
270
345
|
|
|
@@ -279,13 +354,17 @@ export const createSocketTransport = ({
|
|
|
279
354
|
pending.set(request.mutationId, {reject, resolve, timer});
|
|
280
355
|
// The server both emits sync:ack/sync:nack and invokes this Socket.io
|
|
281
356
|
// ack callback; whichever arrives first settles (settle is idempotent).
|
|
282
|
-
socket.emit(
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
357
|
+
socket.emit(
|
|
358
|
+
"sync:mutate",
|
|
359
|
+
withOrganizationId(request, organizationIdProvider),
|
|
360
|
+
(response: {ack?: SyncAck; nack?: SyncNack}) => {
|
|
361
|
+
if (response?.ack) {
|
|
362
|
+
settle(request.mutationId, {ack: response.ack, type: "ack"});
|
|
363
|
+
} else if (response?.nack) {
|
|
364
|
+
settle(request.mutationId, {nack: response.nack, type: "nack"});
|
|
365
|
+
}
|
|
287
366
|
}
|
|
288
|
-
|
|
367
|
+
);
|
|
289
368
|
});
|
|
290
369
|
|
|
291
370
|
const sendMutationBatch = (
|
|
@@ -358,7 +437,7 @@ export const createSocketTransport = ({
|
|
|
358
437
|
|
|
359
438
|
socket.emit(
|
|
360
439
|
"sync:mutateBatch",
|
|
361
|
-
{...request, batchId},
|
|
440
|
+
withOrganizationId({...request, batchId}, organizationIdProvider),
|
|
362
441
|
(response: {
|
|
363
442
|
results?: ({type: "ack"; ack: SyncAck} | {type: "nack"; nack: SyncNack})[];
|
|
364
443
|
}) => {
|
|
@@ -387,6 +466,13 @@ export const createSocketTransport = ({
|
|
|
387
466
|
deltaListeners.delete(callback);
|
|
388
467
|
};
|
|
389
468
|
},
|
|
469
|
+
onDeltaBatch: (callback: (deltas: SyncDelta[]) => void): (() => void) => {
|
|
470
|
+
deltaBatchListeners.add(callback);
|
|
471
|
+
return () => {
|
|
472
|
+
deltaBatchListeners.delete(callback);
|
|
473
|
+
inboundDeltaBatches.delete(callback);
|
|
474
|
+
};
|
|
475
|
+
},
|
|
390
476
|
onStatusChange: (callback: (status: TransportStatus) => void): (() => void) => {
|
|
391
477
|
statusListeners.add(callback);
|
|
392
478
|
return () => {
|
package/src/sync/transport.ts
CHANGED
|
@@ -53,7 +53,7 @@ export interface SyncTransport {
|
|
|
53
53
|
/** Close the connection and reject any in-flight mutation sends. */
|
|
54
54
|
disconnect: () => void;
|
|
55
55
|
/** Subscribe to delta streams for the given collections (idempotent server-side). */
|
|
56
|
-
subscribe: (collections: string[]) => void;
|
|
56
|
+
subscribe: (collections: string[], options?: {mode?: "window"}) => void;
|
|
57
57
|
/**
|
|
58
58
|
* Send a mutation and resolve with the server's ack/nack, correlated by
|
|
59
59
|
* `mutationId`. Rejects when no reply arrives within the transport's
|
|
@@ -78,6 +78,12 @@ export interface SyncTransport {
|
|
|
78
78
|
* doubles) satisfy the contract; callers must treat its absence as "never confirmed".
|
|
79
79
|
*/
|
|
80
80
|
onSubscribed?: (callback: (subscribed: SyncSubscribed) => void) => () => void;
|
|
81
|
+
/**
|
|
82
|
+
* Optional burst-aware inbound subscription. Transports that receive rapid
|
|
83
|
+
* `sync:delta` events can deliver one arrival-ordered batch so the client
|
|
84
|
+
* commits it atomically. `onDelta` remains the compatibility path.
|
|
85
|
+
*/
|
|
86
|
+
onDeltaBatch?: (callback: (deltas: SyncDelta[]) => void) => () => void;
|
|
81
87
|
/** Subscribe to connection status changes. Returns an unsubscribe function. */
|
|
82
88
|
onStatusChange: (callback: (status: TransportStatus) => void) => () => void;
|
|
83
89
|
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import {describe, it} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
3
|
+
import {DateTime} from "luxon";
|
|
4
|
+
|
|
5
|
+
import {createSyncStore} from "../storage/store";
|
|
6
|
+
import {hydrateWindowEntities} from "./windowHydrate";
|
|
7
|
+
|
|
8
|
+
const STREAM = "todos|admin";
|
|
9
|
+
|
|
10
|
+
describe("hydrateWindowEntities", () => {
|
|
11
|
+
it("hydrates 2 of 3 ids and ignores the unknown id", async () => {
|
|
12
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
13
|
+
const requested: string[] = [];
|
|
14
|
+
const result = await hydrateWindowEntities({
|
|
15
|
+
channel: {
|
|
16
|
+
fetchEntities: async ({ids}) => {
|
|
17
|
+
requested.push(...ids);
|
|
18
|
+
return {
|
|
19
|
+
entities: [
|
|
20
|
+
{data: {title: "one"}, deleted: false, id: "a", seq: 4},
|
|
21
|
+
{data: {title: "two"}, deleted: false, id: "b", seq: 5},
|
|
22
|
+
],
|
|
23
|
+
};
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
collection: "todos",
|
|
27
|
+
ids: ["a", "b", "missing"],
|
|
28
|
+
store,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
assert.deepEqual(requested, ["a", "b", "missing"]);
|
|
32
|
+
assert.deepEqual(result.hydratedIds.sort(), ["a", "b"]);
|
|
33
|
+
const rowA = store.getEntity<{title: string}>({collection: "todos", id: "a"});
|
|
34
|
+
const rowB = store.getEntity<{title: string}>({collection: "todos", id: "b"});
|
|
35
|
+
assert.equal(rowA?.data.title, "one");
|
|
36
|
+
assert.equal(rowB?.seq, 5);
|
|
37
|
+
assert.isUndefined(store.getEntity({collection: "todos", id: "missing"}));
|
|
38
|
+
assert.equal(rowA?.stream, STREAM);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("upserts REST rows immediately and fetches every id for seq/deleted metadata", async () => {
|
|
42
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
43
|
+
const fetched: string[][] = [];
|
|
44
|
+
await hydrateWindowEntities({
|
|
45
|
+
channel: {
|
|
46
|
+
fetchEntities: async ({ids}) => {
|
|
47
|
+
fetched.push(ids);
|
|
48
|
+
return {
|
|
49
|
+
entities: [
|
|
50
|
+
{data: {title: "canonical-a"}, deleted: false, id: "a", seq: 7},
|
|
51
|
+
{data: {title: "from-entities"}, deleted: false, id: "b", seq: 9},
|
|
52
|
+
],
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
collection: "todos",
|
|
57
|
+
ids: ["a", "b"],
|
|
58
|
+
restRows: {a: {title: "from-rest"}},
|
|
59
|
+
store,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
assert.deepEqual(fetched, [["a", "b"]]);
|
|
63
|
+
const restRow = store.getEntity<{title: string}>({collection: "todos", id: "a"});
|
|
64
|
+
const entityRow = store.getEntity<{title: string}>({collection: "todos", id: "b"});
|
|
65
|
+
assert.equal(restRow?.data.title, "from-rest");
|
|
66
|
+
assert.equal(restRow?.seq, 7);
|
|
67
|
+
assert.equal(entityRow?.data.title, "from-entities");
|
|
68
|
+
assert.equal(entityRow?.seq, 9);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("exposes REST row data before entities fetch resolves", async () => {
|
|
72
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
73
|
+
const updatedAt = DateTime.utc().toISO();
|
|
74
|
+
let restRowDuringFetch: {data?: {title?: string; updated?: string}; seq?: number} | undefined;
|
|
75
|
+
|
|
76
|
+
await hydrateWindowEntities({
|
|
77
|
+
channel: {
|
|
78
|
+
fetchEntities: async () => {
|
|
79
|
+
restRowDuringFetch = store.getEntity<{title: string; updated?: string}>({
|
|
80
|
+
collection: "todos",
|
|
81
|
+
id: "a",
|
|
82
|
+
});
|
|
83
|
+
return {
|
|
84
|
+
entities: [{data: {title: "canonical"}, deleted: false, id: "a", seq: 11}],
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
collection: "todos",
|
|
89
|
+
ids: ["a"],
|
|
90
|
+
restRows: {a: {title: "from-rest", updated: updatedAt}},
|
|
91
|
+
store,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
assert.equal(restRowDuringFetch?.data?.title, "from-rest");
|
|
95
|
+
assert.equal(restRowDuringFetch?.data?.updated, updatedAt);
|
|
96
|
+
assert.equal(restRowDuringFetch?.seq, 0);
|
|
97
|
+
const row = store.getEntity<{title: string; updated?: string}>({collection: "todos", id: "a"});
|
|
98
|
+
assert.equal(row?.seq, 11);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("applies deleted metadata from entities onto REST rows", async () => {
|
|
102
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
103
|
+
await hydrateWindowEntities({
|
|
104
|
+
channel: {
|
|
105
|
+
fetchEntities: async () => ({
|
|
106
|
+
entities: [{data: {title: "gone"}, deleted: true, id: "a", seq: 12}],
|
|
107
|
+
}),
|
|
108
|
+
},
|
|
109
|
+
collection: "todos",
|
|
110
|
+
ids: ["a"],
|
|
111
|
+
restRows: {a: {title: "from-rest"}},
|
|
112
|
+
store,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const row = store.getEntity<{title: string}>({collection: "todos", id: "a"});
|
|
116
|
+
assert.equal(row?.data.title, "from-rest");
|
|
117
|
+
assert.equal(row?.seq, 12);
|
|
118
|
+
assert.isTrue(row?.deleted);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("keeps a newer live delta when the entities snapshot is already stale", async () => {
|
|
122
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
123
|
+
const result = await hydrateWindowEntities({
|
|
124
|
+
channel: {
|
|
125
|
+
fetchEntities: async () => {
|
|
126
|
+
// A `{collection}|admin` delta lands while the entities request is in flight.
|
|
127
|
+
store.upsertEntity({
|
|
128
|
+
collection: "todos",
|
|
129
|
+
data: {title: "live-delta"},
|
|
130
|
+
id: "a",
|
|
131
|
+
seq: 20,
|
|
132
|
+
stream: STREAM,
|
|
133
|
+
});
|
|
134
|
+
return {
|
|
135
|
+
entities: [{data: {title: "stale-snapshot"}, deleted: true, id: "a", seq: 12}],
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
collection: "todos",
|
|
140
|
+
ids: ["a"],
|
|
141
|
+
store,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const row = store.getEntity<{title: string}>({collection: "todos", id: "a"});
|
|
145
|
+
assert.deepEqual(result.hydratedIds, ["a"]);
|
|
146
|
+
assert.equal(row?.data.title, "live-delta");
|
|
147
|
+
assert.equal(row?.seq, 20);
|
|
148
|
+
assert.isNotTrue(row?.deleted);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("skips REST and entities upserts when a pending optimistic mutation owns the row", async () => {
|
|
152
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
153
|
+
store.upsertEntity({
|
|
154
|
+
collection: "todos",
|
|
155
|
+
data: {title: "pending-local"},
|
|
156
|
+
id: "a",
|
|
157
|
+
pendingMutationId: "m-pending",
|
|
158
|
+
seq: 3,
|
|
159
|
+
stream: STREAM,
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
await hydrateWindowEntities({
|
|
163
|
+
channel: {
|
|
164
|
+
fetchEntities: async () => ({
|
|
165
|
+
entities: [{data: {title: "server"}, deleted: false, id: "a", seq: 99}],
|
|
166
|
+
}),
|
|
167
|
+
},
|
|
168
|
+
collection: "todos",
|
|
169
|
+
ids: ["a"],
|
|
170
|
+
restRows: {a: {title: "from-rest"}},
|
|
171
|
+
store,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
const row = store.getEntity<{title: string}>({collection: "todos", id: "a"});
|
|
175
|
+
assert.equal(row?.data.title, "pending-local");
|
|
176
|
+
assert.equal(row?.seq, 3);
|
|
177
|
+
assert.equal(row?.pendingMutationId, "m-pending");
|
|
178
|
+
});
|
|
179
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type {SyncStore} from "../storage/store";
|
|
2
|
+
import {MAX_REPAIR_FETCH_IDS} from "./entityRepair";
|
|
3
|
+
import type {HttpChannel} from "./httpChannel";
|
|
4
|
+
|
|
5
|
+
/** Stream key window subscribers join (`{collection}|admin`). */
|
|
6
|
+
const adminWindowStream = (collection: string): string => `${collection}|admin`;
|
|
7
|
+
|
|
8
|
+
export interface HydrateWindowEntitiesArgs {
|
|
9
|
+
store: SyncStore;
|
|
10
|
+
channel: Pick<HttpChannel, "fetchEntities">;
|
|
11
|
+
collection: string;
|
|
12
|
+
ids: string[];
|
|
13
|
+
/**
|
|
14
|
+
* Optional REST membership payloads keyed by id. Those rows upsert immediately
|
|
15
|
+
* for fast UI; every requested id is still fetched from `GET /sync/entities`
|
|
16
|
+
* before this call resolves so seq/deleted metadata is canonical.
|
|
17
|
+
*/
|
|
18
|
+
restRows?: Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface HydrateWindowEntitiesResult {
|
|
22
|
+
hydratedIds: string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Upsert a window of known ids into TinyBase. REST rows render immediately;
|
|
27
|
+
* canonical seq/deleted metadata comes from `GET /sync/entities` for every
|
|
28
|
+
* requested id before this resolves. Server ids that `/sync/entities` does not
|
|
29
|
+
* return are ignored (no empty rows) unless a REST row already landed.
|
|
30
|
+
*/
|
|
31
|
+
export const hydrateWindowEntities = async ({
|
|
32
|
+
store,
|
|
33
|
+
channel,
|
|
34
|
+
collection,
|
|
35
|
+
ids,
|
|
36
|
+
restRows,
|
|
37
|
+
}: HydrateWindowEntitiesArgs): Promise<HydrateWindowEntitiesResult> => {
|
|
38
|
+
const uniqueIds = [...new Set(ids.filter((id) => id.length > 0))];
|
|
39
|
+
if (uniqueIds.length === 0) {
|
|
40
|
+
return {hydratedIds: []};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const stream = adminWindowStream(collection);
|
|
44
|
+
const hydrated = new Set<string>();
|
|
45
|
+
|
|
46
|
+
for (const id of uniqueIds) {
|
|
47
|
+
const restData = restRows?.[id];
|
|
48
|
+
if (restData === undefined) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const existing = store.getEntity({collection, id});
|
|
52
|
+
if (existing?.pendingMutationId) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
store.upsertEntity({
|
|
56
|
+
collection,
|
|
57
|
+
data: restData,
|
|
58
|
+
id,
|
|
59
|
+
stream,
|
|
60
|
+
});
|
|
61
|
+
hydrated.add(id);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
for (let i = 0; i < uniqueIds.length; i += MAX_REPAIR_FETCH_IDS) {
|
|
65
|
+
const chunk = uniqueIds.slice(i, i + MAX_REPAIR_FETCH_IDS);
|
|
66
|
+
const response = await channel.fetchEntities({collection, ids: chunk});
|
|
67
|
+
for (const entity of response.entities) {
|
|
68
|
+
if (!uniqueIds.includes(entity.id)) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const existing = store.getEntity({collection, id: entity.id});
|
|
72
|
+
if (existing?.pendingMutationId) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
// A live `{collection}|admin` delta can land while this fetch is in flight. Its seq is
|
|
76
|
+
// newer than the snapshot being applied here, so writing the snapshot back would rewind
|
|
77
|
+
// seq/deleted and make the next mutate conflict against a stale baseline.
|
|
78
|
+
if (existing && existing.seq > entity.seq) {
|
|
79
|
+
hydrated.add(entity.id);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const restData = restRows?.[entity.id];
|
|
83
|
+
store.upsertEntity({
|
|
84
|
+
collection,
|
|
85
|
+
data: restData !== undefined ? (existing?.data ?? restData) : entity.data,
|
|
86
|
+
deleted: entity.deleted,
|
|
87
|
+
id: entity.id,
|
|
88
|
+
pendingMutationId: "",
|
|
89
|
+
seq: entity.seq,
|
|
90
|
+
stream,
|
|
91
|
+
});
|
|
92
|
+
hydrated.add(entity.id);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return {hydratedIds: [...hydrated]};
|
|
97
|
+
};
|
|
@@ -209,7 +209,7 @@ export const createFakeTransport = (): FakeTransport => {
|
|
|
209
209
|
setDefaultResponder: (responder?: FakeMutationResponder): void => {
|
|
210
210
|
defaultResponder = responder ?? autoAck;
|
|
211
211
|
},
|
|
212
|
-
subscribe: (collections: string[]): void => {
|
|
212
|
+
subscribe: (collections: string[], _options?: {mode?: "window"}): void => {
|
|
213
213
|
for (const collection of collections) {
|
|
214
214
|
subscribed.add(collection);
|
|
215
215
|
}
|
package/src/types.ts
CHANGED
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
|
|
9
9
|
export type SyncMutationOperation = "create" | "update" | "delete";
|
|
10
10
|
|
|
11
|
+
/** Explicit client marker for admin-panel windowed sync writes. */
|
|
12
|
+
export type SyncMutationMode = "adminWindow";
|
|
13
|
+
|
|
11
14
|
/** A change event delivered by the server via `sync:delta`. */
|
|
12
15
|
export interface SyncDelta {
|
|
13
16
|
/** Collection tag (e.g. "todos"). */
|
|
@@ -36,12 +39,18 @@ export interface SyncDelta {
|
|
|
36
39
|
*
|
|
37
40
|
* Emitted after the server has joined the socket to every `sync:{stream}` room for the
|
|
38
41
|
* collection, so it is the first moment live deltas are guaranteed to reach this client.
|
|
42
|
+
* `mode: "window"` means the client must not page `GET /sync/snapshot` for those streams
|
|
43
|
+
* (admin fan-in `{collection}|admin`; hydrate via REST + `/sync/entities` instead).
|
|
39
44
|
*/
|
|
45
|
+
type SyncSubscribeMode = "window";
|
|
46
|
+
|
|
40
47
|
export interface SyncSubscribed {
|
|
41
48
|
/** Collection tag the confirmation is for. */
|
|
42
49
|
collection: string;
|
|
43
50
|
/** Stream keys now joined for that collection. */
|
|
44
51
|
streams: string[];
|
|
52
|
+
/** When `window`, skip snapshot/reconcile paging for this collection. */
|
|
53
|
+
mode?: SyncSubscribeMode;
|
|
45
54
|
}
|
|
46
55
|
|
|
47
56
|
/** A client mutation sent via `sync:mutate` or `POST /sync/mutate`. */
|
|
@@ -56,6 +65,11 @@ export interface SyncMutateRequest {
|
|
|
56
65
|
data?: Record<string, unknown>;
|
|
57
66
|
/** The seq the client last saw for this document; enables LWW conflict detection. */
|
|
58
67
|
baseVersion?: number;
|
|
68
|
+
/**
|
|
69
|
+
* When `"adminWindow"`, the server applies AdminApp write semantics instead of
|
|
70
|
+
* product sync permissions.
|
|
71
|
+
*/
|
|
72
|
+
mutationMode?: SyncMutationMode;
|
|
59
73
|
}
|
|
60
74
|
|
|
61
75
|
/** Successful mutation acknowledgement. */
|
|
@@ -198,6 +212,8 @@ export interface OutboxMutation {
|
|
|
198
212
|
/** The user this mutation belongs to; replay skips mutations from other users. */
|
|
199
213
|
userId: string;
|
|
200
214
|
createdAt: string;
|
|
215
|
+
/** Persisted admin-window marker for durable outbox replay. */
|
|
216
|
+
mutationMode?: SyncMutationMode;
|
|
201
217
|
}
|
|
202
218
|
|
|
203
219
|
/** An unresolved conflict between a local mutation and the canonical server state. */
|