@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
|
@@ -220,6 +220,29 @@ describe("generateSyncDbSdk", () => {
|
|
|
220
220
|
}
|
|
221
221
|
});
|
|
222
222
|
|
|
223
|
+
it("applies retries: true and numeric overrides", async () => {
|
|
224
|
+
const dir = await mkdtemp(join(tmpdir(), "syncdb-codegen-"));
|
|
225
|
+
const out = join(dir, "syncDbSdk.ts");
|
|
226
|
+
try {
|
|
227
|
+
const enabled = await generateSyncDbSdk({
|
|
228
|
+
config: {overrides: {todos: {retries: true}}},
|
|
229
|
+
format: false,
|
|
230
|
+
out,
|
|
231
|
+
schema: fixturePath,
|
|
232
|
+
});
|
|
233
|
+
expect(enabled).toContain("retries: true");
|
|
234
|
+
const numbered = await generateSyncDbSdk({
|
|
235
|
+
config: {overrides: {todos: {retries: 4}}},
|
|
236
|
+
format: false,
|
|
237
|
+
out,
|
|
238
|
+
schema: fixturePath,
|
|
239
|
+
});
|
|
240
|
+
expect(numbered).toContain("retries: 4");
|
|
241
|
+
} finally {
|
|
242
|
+
await rm(dir, {force: true, recursive: true});
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
|
|
223
246
|
it("formats the generated file when format is true", async () => {
|
|
224
247
|
const dir = await mkdtemp(join(tmpdir(), "syncdb-codegen-"));
|
|
225
248
|
const out = join(dir, "syncDbSdk.ts");
|
|
@@ -236,7 +259,7 @@ describe("generateSyncDbSdk", () => {
|
|
|
236
259
|
}
|
|
237
260
|
});
|
|
238
261
|
|
|
239
|
-
it("rejects collection names
|
|
262
|
+
it("rejects unsafe sync collection names", async () => {
|
|
240
263
|
const spec = await loadSpec(fixturePath);
|
|
241
264
|
const discovered = discoverCollections({spec});
|
|
242
265
|
const first = discovered[0];
|
|
@@ -252,7 +275,17 @@ describe("generateSyncDbSdk", () => {
|
|
|
252
275
|
},
|
|
253
276
|
],
|
|
254
277
|
})
|
|
255
|
-
).toThrow(/not a
|
|
278
|
+
).toThrow(/not a valid sync collection name/);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("rejects invalid retries overrides", async () => {
|
|
282
|
+
const spec = await loadSpec(fixturePath);
|
|
283
|
+
const first = discoverCollections({spec})[0];
|
|
284
|
+
expect(() =>
|
|
285
|
+
emitSdk({
|
|
286
|
+
collections: [{...first, retries: Number.NaN}],
|
|
287
|
+
})
|
|
288
|
+
).toThrow(/Invalid retries/);
|
|
256
289
|
});
|
|
257
290
|
});
|
|
258
291
|
|
|
@@ -1,32 +1,59 @@
|
|
|
1
|
-
import {describe,
|
|
1
|
+
import {describe, it} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
2
3
|
|
|
3
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
assertSyncCollectionName,
|
|
6
|
+
assertTsIdentifier,
|
|
7
|
+
emitTsPropertyKey,
|
|
8
|
+
emitTsString,
|
|
9
|
+
} from "./safeIdentifiers";
|
|
4
10
|
|
|
5
11
|
describe("assertTsIdentifier", () => {
|
|
6
12
|
it("accepts TypeScript identifiers", () => {
|
|
7
|
-
|
|
8
|
-
|
|
13
|
+
assert.equal(assertTsIdentifier({label: "name", value: "Todo"}), "Todo");
|
|
14
|
+
assert.equal(assertTsIdentifier({label: "name", value: "_id"}), "_id");
|
|
9
15
|
});
|
|
10
16
|
|
|
11
17
|
it("rejects quote and statement injection", () => {
|
|
12
|
-
|
|
13
|
-
assertTsIdentifier({label: "collection", value: 'todos"; process.exit(1); //'})
|
|
14
|
-
|
|
18
|
+
assert.throws(
|
|
19
|
+
() => assertTsIdentifier({label: "collection", value: 'todos"; process.exit(1); //'}),
|
|
20
|
+
/not a TypeScript identifier/
|
|
21
|
+
);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe("assertSyncCollectionName", () => {
|
|
26
|
+
it("accepts hyphenated collection names", () => {
|
|
27
|
+
assert.equal(
|
|
28
|
+
assertSyncCollectionName({label: "collection", value: "notification-preferences"}),
|
|
29
|
+
"notification-preferences"
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("rejects unsafe collection names", () => {
|
|
34
|
+
assert.throws(
|
|
35
|
+
() =>
|
|
36
|
+
assertSyncCollectionName({
|
|
37
|
+
label: "collection",
|
|
38
|
+
value: 'notifications"; process.exit(1); //',
|
|
39
|
+
}),
|
|
40
|
+
/not a valid sync collection name/
|
|
41
|
+
);
|
|
15
42
|
});
|
|
16
43
|
});
|
|
17
44
|
|
|
18
45
|
describe("emitTsString", () => {
|
|
19
46
|
it("JSON-escapes quotes so they cannot close a generated string", () => {
|
|
20
|
-
|
|
47
|
+
assert.equal(emitTsString('todos"; process.exit(1); //'), '"todos\\"; process.exit(1); //"');
|
|
21
48
|
});
|
|
22
49
|
});
|
|
23
50
|
|
|
24
51
|
describe("emitTsPropertyKey", () => {
|
|
25
52
|
it("emits bare identifiers", () => {
|
|
26
|
-
|
|
53
|
+
assert.equal(emitTsPropertyKey("title"), "title");
|
|
27
54
|
});
|
|
28
55
|
|
|
29
56
|
it("quotes keys that are not identifiers", () => {
|
|
30
|
-
|
|
57
|
+
assert.equal(emitTsPropertyKey('foo"; bar'), '"foo\\"; bar"');
|
|
31
58
|
});
|
|
32
59
|
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const TS_IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
2
|
+
const SYNC_COLLECTION = /^[A-Za-z0-9][A-Za-z0-9_-]*$/;
|
|
2
3
|
|
|
3
4
|
export const assertTsIdentifier = ({label, value}: {label: string; value: string}): string => {
|
|
4
5
|
if (!TS_IDENTIFIER.test(value)) {
|
|
@@ -7,6 +8,22 @@ export const assertTsIdentifier = ({label, value}: {label: string; value: string
|
|
|
7
8
|
return value;
|
|
8
9
|
};
|
|
9
10
|
|
|
11
|
+
/** Sync collection tags may use hyphens (e.g. notification-preferences) but must be safe in JSON strings. */
|
|
12
|
+
export const assertSyncCollectionName = ({
|
|
13
|
+
label,
|
|
14
|
+
value,
|
|
15
|
+
}: {
|
|
16
|
+
label: string;
|
|
17
|
+
value: string;
|
|
18
|
+
}): string => {
|
|
19
|
+
if (!SYNC_COLLECTION.test(value)) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
`Invalid ${label}: ${JSON.stringify(value)} is not a valid sync collection name`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
};
|
|
26
|
+
|
|
10
27
|
export const emitTsString = (value: string): string => JSON.stringify(value);
|
|
11
28
|
|
|
12
29
|
export const emitTsPropertyKey = (key: string): string => {
|
package/src/codegen/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
interface TerrenoSyncExtension {
|
|
2
2
|
collection: string;
|
|
3
3
|
scope: string;
|
|
4
4
|
}
|
|
@@ -18,7 +18,7 @@ export interface OpenApiSchema {
|
|
|
18
18
|
description?: string;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
interface OpenApiOperation {
|
|
22
22
|
"x-terreno-sync"?: TerrenoSyncExtension;
|
|
23
23
|
requestBody?: {
|
|
24
24
|
content?: Record<string, {schema?: OpenApiSchema}>;
|
|
@@ -31,7 +31,7 @@ export interface OpenApiOperation {
|
|
|
31
31
|
>;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
interface OpenApiPathItem {
|
|
35
35
|
get?: OpenApiOperation;
|
|
36
36
|
post?: OpenApiOperation;
|
|
37
37
|
patch?: OpenApiOperation;
|
|
@@ -118,4 +118,10 @@ describe("attachDebugChannel", () => {
|
|
|
118
118
|
|
|
119
119
|
expect(debuggerLog.getEvents()).toHaveLength(0);
|
|
120
120
|
});
|
|
121
|
+
|
|
122
|
+
it("uses BroadcastChannel when the runtime provides it", () => {
|
|
123
|
+
const log = createSyncDebugLog();
|
|
124
|
+
const bridge = attachDebugChannel({log, name: "factory-probe"});
|
|
125
|
+
bridge?.close();
|
|
126
|
+
});
|
|
121
127
|
});
|
package/src/debug/debugLog.ts
CHANGED
|
@@ -148,6 +148,7 @@ const emptyByType = (): Record<SyncDebugEventType, number> => ({
|
|
|
148
148
|
/**
|
|
149
149
|
* Create an in-memory debug log backed by a circular buffer.
|
|
150
150
|
*/
|
|
151
|
+
/** @internal */
|
|
151
152
|
export const createSyncDebugLog = (options: SyncDebugLogOptions = {}): SyncDebugLog => {
|
|
152
153
|
const capacity = Math.max(1, options.capacity ?? DEFAULT_CAPACITY);
|
|
153
154
|
const clock = options.clock ?? ((): string => DateTime.now().toISO() ?? new Date().toISOString());
|
package/src/index.ts
CHANGED
|
@@ -85,6 +85,10 @@ export {
|
|
|
85
85
|
betterAuthAdapter,
|
|
86
86
|
DEFAULT_AUTH_POLL_INTERVAL_MS,
|
|
87
87
|
} from "./auth/betterAuthAdapter";
|
|
88
|
+
export {
|
|
89
|
+
type BetterAuthReactClientLike,
|
|
90
|
+
bridgeBetterAuthReactClient,
|
|
91
|
+
} from "./auth/betterAuthReactClient";
|
|
88
92
|
export type {
|
|
89
93
|
BetterAuthClientLike,
|
|
90
94
|
BetterAuthGetSessionResult,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Isolated: mock expo-sqlite as missing or incomplete so loadExpoSqlite error
|
|
3
|
+
* paths do not leak into the rest of the suite.
|
|
4
|
+
*/
|
|
5
|
+
import {describe, expect, it, mock} from "bun:test";
|
|
6
|
+
|
|
7
|
+
mock.module("expo-sqlite", () => ({}));
|
|
8
|
+
mock.module("tinybase/persisters/persister-expo-sqlite", () => ({
|
|
9
|
+
createExpoSqlitePersister: () => ({}),
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
import {createDefaultPersisterFactory} from "../persisters/defaultPersisterFactory.native";
|
|
13
|
+
import {createSyncStore} from "../storage/store";
|
|
14
|
+
|
|
15
|
+
describe("createDefaultPersisterFactory (native errors)", () => {
|
|
16
|
+
it("throws when expo-sqlite has no openDatabaseSync", () => {
|
|
17
|
+
const factory = createDefaultPersisterFactory();
|
|
18
|
+
const store = createSyncStore({collections: ["todos"]});
|
|
19
|
+
expect(() => factory({databaseName: "terreno-syncdb.db", store: store.raw})).toThrow(
|
|
20
|
+
/expo-sqlite peer/
|
|
21
|
+
);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -266,6 +266,26 @@ describe("state machine transitions", () => {
|
|
|
266
266
|
expect(outbox.getMutation({mutationId: retry.mutationId})?.baseVersion).toBe(3);
|
|
267
267
|
});
|
|
268
268
|
|
|
269
|
+
it("requeue preserves an admin-window mutation mode", () => {
|
|
270
|
+
const outbox = makeOutbox();
|
|
271
|
+
outbox.enqueue({
|
|
272
|
+
args: {title: "Admin retry"},
|
|
273
|
+
collection: "todos",
|
|
274
|
+
entityId: "t1",
|
|
275
|
+
mutationId: "m1",
|
|
276
|
+
mutationMode: "adminWindow",
|
|
277
|
+
operation: "update",
|
|
278
|
+
userId: "user-1",
|
|
279
|
+
});
|
|
280
|
+
outbox.markInFlight({mutationId: "m1"});
|
|
281
|
+
outbox.markConflicted({mutationId: "m1"});
|
|
282
|
+
|
|
283
|
+
const retry = outbox.requeue({baseVersion: 9, mutationId: "m1"});
|
|
284
|
+
|
|
285
|
+
expect(retry.mutationMode).toBe("adminWindow");
|
|
286
|
+
expect(outbox.getMutation({mutationId: retry.mutationId})?.mutationMode).toBe("adminWindow");
|
|
287
|
+
});
|
|
288
|
+
|
|
269
289
|
it("requeue copies maxAttempts onto the cloned row", () => {
|
|
270
290
|
const outbox = makeOutbox();
|
|
271
291
|
outbox.enqueue({
|
package/src/mutations/outbox.ts
CHANGED
|
@@ -3,11 +3,12 @@ import type {Row} from "tinybase";
|
|
|
3
3
|
|
|
4
4
|
import type {SyncStore} from "../storage/store";
|
|
5
5
|
import {CONFLICTS_TABLE, OUTBOX_TABLE, type OutboxRow} from "../storage/types";
|
|
6
|
-
import type {OutboxMutation, OutboxStatus, SyncMutationOperation} from "../types";
|
|
6
|
+
import type {OutboxMutation, OutboxStatus, SyncMutationMode, SyncMutationOperation} from "../types";
|
|
7
7
|
|
|
8
8
|
const defaultNow = (): string => DateTime.now().toISO();
|
|
9
9
|
|
|
10
10
|
/** Default number of failed rows retained by `prune()` for debugging/UI. */
|
|
11
|
+
/** @internal */
|
|
11
12
|
export const DEFAULT_KEEP_FAILED = 50;
|
|
12
13
|
|
|
13
14
|
/** Generate a stable client mutation id (idempotency key). */
|
|
@@ -46,12 +47,13 @@ const rowToMutation = (mutationId: string, row: Partial<OutboxRow>): OutboxMutat
|
|
|
46
47
|
errorNackCount: row.errorNackCount ?? 0,
|
|
47
48
|
maxAttempts: typeof row.maxAttempts === "number" ? row.maxAttempts : undefined,
|
|
48
49
|
mutationId,
|
|
50
|
+
mutationMode: row.mutationMode,
|
|
49
51
|
operation: (row.operation ?? "update") as SyncMutationOperation,
|
|
50
52
|
status: (row.status ?? "queued") as OutboxStatus,
|
|
51
53
|
userId: row.userId ?? "",
|
|
52
54
|
});
|
|
53
55
|
|
|
54
|
-
|
|
56
|
+
interface EnqueueArgs {
|
|
55
57
|
collection: string;
|
|
56
58
|
operation: SyncMutationOperation;
|
|
57
59
|
entityId: string;
|
|
@@ -68,9 +70,11 @@ export interface EnqueueArgs {
|
|
|
68
70
|
userId: string;
|
|
69
71
|
/** Optional explicit id (defaults to a generated UUID; useful in tests). */
|
|
70
72
|
mutationId?: string;
|
|
73
|
+
/** Admin-window marker persisted for replay after restart. */
|
|
74
|
+
mutationMode?: SyncMutationMode;
|
|
71
75
|
}
|
|
72
76
|
|
|
73
|
-
|
|
77
|
+
interface RecoverStartupStateResult {
|
|
74
78
|
/** mutationIds that were stranded `inFlight` and moved back to `queued`. */
|
|
75
79
|
recoveredInFlight: string[];
|
|
76
80
|
/** entityIds whose stale `pendingMutationId` was cleared (acked-with-pending). */
|
|
@@ -255,6 +259,9 @@ export const createOutbox = ({
|
|
|
255
259
|
if (args.maxAttempts !== undefined) {
|
|
256
260
|
row.maxAttempts = args.maxAttempts;
|
|
257
261
|
}
|
|
262
|
+
if (args.mutationMode !== undefined) {
|
|
263
|
+
row.mutationMode = args.mutationMode;
|
|
264
|
+
}
|
|
258
265
|
store.raw.setRow(OUTBOX_TABLE, mutationId, row as unknown as Row);
|
|
259
266
|
return rowToMutation(mutationId, row);
|
|
260
267
|
};
|
|
@@ -353,6 +360,7 @@ export const createOutbox = ({
|
|
|
353
360
|
enqueueOrder: row.enqueueOrder ?? 0,
|
|
354
361
|
entityId: row.entityId ?? "",
|
|
355
362
|
errorNackCount: 0,
|
|
363
|
+
...(row.mutationMode !== undefined ? {mutationMode: row.mutationMode} : {}),
|
|
356
364
|
operation: row.operation ?? "update",
|
|
357
365
|
status: "queued",
|
|
358
366
|
userId: row.userId ?? "",
|
package/src/storage/idb.ts
CHANGED
package/src/storage/schema.ts
CHANGED
|
@@ -84,6 +84,7 @@ export const buildTablesSchema = ({collections}: {collections: string[]}): Table
|
|
|
84
84
|
entityId: {type: "string"},
|
|
85
85
|
errorNackCount: {type: "number"},
|
|
86
86
|
maxAttempts: {type: "number"},
|
|
87
|
+
mutationMode: {type: "string"},
|
|
87
88
|
operation: {type: "string"},
|
|
88
89
|
status: {type: "string"},
|
|
89
90
|
userId: {type: "string"},
|
package/src/storage/store.ts
CHANGED
|
@@ -39,7 +39,7 @@ const rowToEntity = <TData>(id: string, row: Partial<EntityRow>): SyncEntity<TDa
|
|
|
39
39
|
stream: row.stream ? row.stream : undefined,
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
interface UpsertEntityArgs {
|
|
43
43
|
collection: string;
|
|
44
44
|
id: string;
|
|
45
45
|
data: unknown;
|
|
@@ -53,7 +53,7 @@ export interface UpsertEntityArgs {
|
|
|
53
53
|
stream?: string;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
interface CompactTombstonesResult {
|
|
57
57
|
/** Number of tombstone rows removed (across all configured collections). */
|
|
58
58
|
removed: number;
|
|
59
59
|
}
|
package/src/storage/types.ts
CHANGED
|
@@ -66,14 +66,6 @@ export interface EntityRow {
|
|
|
66
66
|
stream: string;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
/** Primitive row shape for the `_knownStreams` table; rowId = stream key. */
|
|
70
|
-
export interface KnownStreamRow {
|
|
71
|
-
/** The collection tag the stream belongs to. */
|
|
72
|
-
collection: string;
|
|
73
|
-
/** When the stream was first bootstrapped. */
|
|
74
|
-
addedAt: string;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
69
|
/** Primitive row shape for the `_outbox` table; rowId = mutationId. */
|
|
78
70
|
export interface OutboxRow {
|
|
79
71
|
/** JSON-encoded mutation args. */
|
|
@@ -98,6 +90,7 @@ export interface OutboxRow {
|
|
|
98
90
|
operation: SyncMutationOperation;
|
|
99
91
|
status: OutboxStatus;
|
|
100
92
|
userId: string;
|
|
93
|
+
mutationMode?: "adminWindow";
|
|
101
94
|
}
|
|
102
95
|
|
|
103
96
|
/** Primitive row shape for the `_cursors` table; rowId = stream key. */
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
2
3
|
|
|
3
4
|
import {createSyncStore, type SyncStore} from "../storage/store";
|
|
4
5
|
import {NEEDS_REPAIR_TABLE} from "../storage/types";
|
|
@@ -235,3 +236,72 @@ describe("applyDelta", () => {
|
|
|
235
236
|
);
|
|
236
237
|
});
|
|
237
238
|
});
|
|
239
|
+
|
|
240
|
+
describe("applyDelta window |admin (Task 3.5)", () => {
|
|
241
|
+
const ADMIN_STREAM = "todos|admin";
|
|
242
|
+
|
|
243
|
+
it("is a no-op for an unknown id (no surprise insert) and still advances the cursor", () => {
|
|
244
|
+
const store = makeStore();
|
|
245
|
+
const result = applyDelta({
|
|
246
|
+
delta: makeDelta({
|
|
247
|
+
data: {title: "surprise"},
|
|
248
|
+
id: "unknown",
|
|
249
|
+
method: "create",
|
|
250
|
+
seq: 4,
|
|
251
|
+
stream: ADMIN_STREAM,
|
|
252
|
+
}),
|
|
253
|
+
store,
|
|
254
|
+
});
|
|
255
|
+
assert.isFalse(result.applied);
|
|
256
|
+
assert.isUndefined(store.getEntity({collection: "todos", id: "unknown"}));
|
|
257
|
+
assert.equal(getCursor({store, stream: ADMIN_STREAM}), 4);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("updates a known id on the admin stream", () => {
|
|
261
|
+
const store = makeStore();
|
|
262
|
+
store.upsertEntity({
|
|
263
|
+
collection: "todos",
|
|
264
|
+
data: {title: "old"},
|
|
265
|
+
id: "t1",
|
|
266
|
+
seq: 1,
|
|
267
|
+
stream: ADMIN_STREAM,
|
|
268
|
+
});
|
|
269
|
+
const result = applyDelta({
|
|
270
|
+
delta: makeDelta({
|
|
271
|
+
data: {title: "new"},
|
|
272
|
+
method: "update",
|
|
273
|
+
seq: 2,
|
|
274
|
+
stream: ADMIN_STREAM,
|
|
275
|
+
}),
|
|
276
|
+
store,
|
|
277
|
+
});
|
|
278
|
+
assert.isTrue(result.applied);
|
|
279
|
+
assert.equal(
|
|
280
|
+
store.getEntity<{title: string}>({collection: "todos", id: "t1"})?.data.title,
|
|
281
|
+
"new"
|
|
282
|
+
);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("delete delta removes a known id and ignores an unknown id", () => {
|
|
286
|
+
const store = makeStore();
|
|
287
|
+
store.upsertEntity({
|
|
288
|
+
collection: "todos",
|
|
289
|
+
data: {title: "gone"},
|
|
290
|
+
id: "t1",
|
|
291
|
+
seq: 1,
|
|
292
|
+
stream: ADMIN_STREAM,
|
|
293
|
+
});
|
|
294
|
+
const known = applyDelta({
|
|
295
|
+
delta: makeDelta({deleted: true, id: "t1", method: "delete", seq: 2, stream: ADMIN_STREAM}),
|
|
296
|
+
store,
|
|
297
|
+
});
|
|
298
|
+
const unknown = applyDelta({
|
|
299
|
+
delta: makeDelta({deleted: true, id: "nope", method: "delete", seq: 3, stream: ADMIN_STREAM}),
|
|
300
|
+
store,
|
|
301
|
+
});
|
|
302
|
+
assert.isTrue(known.applied);
|
|
303
|
+
assert.isTrue(store.getEntity({collection: "todos", id: "t1"})?.deleted);
|
|
304
|
+
assert.isFalse(unknown.applied);
|
|
305
|
+
assert.isUndefined(store.getEntity({collection: "todos", id: "nope"}));
|
|
306
|
+
});
|
|
307
|
+
});
|
package/src/sync/deltaApplier.ts
CHANGED
|
@@ -22,6 +22,8 @@ export interface ApplyDeltaResult {
|
|
|
22
22
|
* optimistic state is protected until conflict resolution decides;
|
|
23
23
|
* - create/update writes data + seq; tombstones set `deleted: true` (keeping
|
|
24
24
|
* the last known data for conflict UIs);
|
|
25
|
+
* - `{collection}|admin` window streams skip inserts for unknown ids (no surprise
|
|
26
|
+
* rows); Refresh is a REST re-query plus `hydrateWindow`, not a snapshot;
|
|
25
27
|
* - the cursor for `delta.stream` advances in every case (including skips), so
|
|
26
28
|
* a skipped delta is never re-fetched on catch-up.
|
|
27
29
|
*/
|
|
@@ -66,6 +68,14 @@ export const applyDelta = ({
|
|
|
66
68
|
return {applied: false, seqJump};
|
|
67
69
|
}
|
|
68
70
|
|
|
71
|
+
// Admin window (`{collection}|admin`): apply update/delete only for ids already
|
|
72
|
+
// in the local window. Unknown ids wait for REST membership + hydrateWindow
|
|
73
|
+
// (Refresh / load more). Cursor still advances so live seq is not replayed.
|
|
74
|
+
if (!existing && delta.stream.endsWith("|admin")) {
|
|
75
|
+
advanceCursor();
|
|
76
|
+
return {applied: false, seqJump};
|
|
77
|
+
}
|
|
78
|
+
|
|
69
79
|
const deleted = delta.deleted === true || delta.method === "delete";
|
|
70
80
|
store.upsertEntity({
|
|
71
81
|
collection: delta.collection,
|
package/src/sync/entityRepair.ts
CHANGED
|
@@ -12,6 +12,7 @@ export const MAX_REPAIR_FETCH_IDS = 100;
|
|
|
12
12
|
* needs-repair mark) when the entity is still pending or conflicted so a
|
|
13
13
|
* reconcile-time repair pass cannot clobber optimistic local state.
|
|
14
14
|
*/
|
|
15
|
+
/** @internal */
|
|
15
16
|
export const applyRepairedEntity = ({
|
|
16
17
|
store,
|
|
17
18
|
collection,
|
|
@@ -57,6 +57,27 @@ describe("createHttpChannel", () => {
|
|
|
57
57
|
expect(headersOf(requests[0]).Authorization).toBe("Bearer token-123");
|
|
58
58
|
});
|
|
59
59
|
|
|
60
|
+
it("attaches X-Organization-Id when an organizationIdProvider returns an id", async () => {
|
|
61
|
+
const {fetchImpl, requests} = makeFetch(() =>
|
|
62
|
+
json({
|
|
63
|
+
cursor: 0,
|
|
64
|
+
entities: [],
|
|
65
|
+
frontierSeq: 0,
|
|
66
|
+
hasMore: false,
|
|
67
|
+
oldestRetainedSeq: 0,
|
|
68
|
+
stream: "todos|owner:u1",
|
|
69
|
+
})
|
|
70
|
+
);
|
|
71
|
+
const channel = createHttpChannel({
|
|
72
|
+
authProvider,
|
|
73
|
+
baseUrl: "http://api",
|
|
74
|
+
fetchImpl,
|
|
75
|
+
organizationIdProvider: () => "org-selected",
|
|
76
|
+
});
|
|
77
|
+
await channel.fetchSnapshotPage({cursor: 0, stream: "todos|owner:u1"});
|
|
78
|
+
expect(headersOf(requests[0])["X-Organization-Id"]).toBe("org-selected");
|
|
79
|
+
});
|
|
80
|
+
|
|
60
81
|
it("forwards the legacyCursor token (C3) when provided", async () => {
|
|
61
82
|
const page = {
|
|
62
83
|
cursor: 0,
|
|
@@ -220,6 +241,67 @@ describe("createHttpChannel", () => {
|
|
|
220
241
|
});
|
|
221
242
|
});
|
|
222
243
|
|
|
244
|
+
describe("fetchEntities", () => {
|
|
245
|
+
it("GETs /sync/entities with collection and ids", async () => {
|
|
246
|
+
const body = {
|
|
247
|
+
entities: [{data: {title: "a"}, deleted: false, id: "a", seq: 1}],
|
|
248
|
+
};
|
|
249
|
+
const {fetchImpl, requests} = makeFetch(() => json(body));
|
|
250
|
+
const channel = createHttpChannel({authProvider, baseUrl: "http://api", fetchImpl});
|
|
251
|
+
await expect(channel.fetchEntities({collection: "todos", ids: ["a", "b"]})).resolves.toEqual(
|
|
252
|
+
body
|
|
253
|
+
);
|
|
254
|
+
expect(requests[0]?.input).toBe("http://api/sync/entities?collection=todos&ids=a%2Cb");
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("rejects on non-ok statuses", async () => {
|
|
258
|
+
const {fetchImpl} = makeFetch(() => json({}, 500));
|
|
259
|
+
const channel = createHttpChannel({authProvider, baseUrl: "http://api", fetchImpl});
|
|
260
|
+
await expect(channel.fetchEntities({collection: "todos", ids: ["a"]})).rejects.toThrow(
|
|
261
|
+
"status 500"
|
|
262
|
+
);
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
describe("sendMutationBatch", () => {
|
|
267
|
+
const batch = {
|
|
268
|
+
mutations: [
|
|
269
|
+
{
|
|
270
|
+
collection: "todos",
|
|
271
|
+
data: {title: "hi"},
|
|
272
|
+
id: "t1",
|
|
273
|
+
mutationId: "m1",
|
|
274
|
+
operation: "create" as const,
|
|
275
|
+
},
|
|
276
|
+
],
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
it("maps 200 results to a results payload", async () => {
|
|
280
|
+
const results = [{ack: {id: "t1", mutationId: "m1", seq: 1}, type: "ack" as const}];
|
|
281
|
+
const {fetchImpl} = makeFetch(() => json({results}));
|
|
282
|
+
const channel = createHttpChannel({authProvider, baseUrl: "http://api", fetchImpl});
|
|
283
|
+
await expect(channel.sendMutationBatch?.(batch)).resolves.toEqual({results, type: "results"});
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("maps 404 to unsupported", async () => {
|
|
287
|
+
const {fetchImpl} = makeFetch(() => json({}, 404));
|
|
288
|
+
const channel = createHttpChannel({authProvider, baseUrl: "http://api", fetchImpl});
|
|
289
|
+
await expect(channel.sendMutationBatch?.(batch)).resolves.toEqual({type: "unsupported"});
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("rejects on non-JSON responses", async () => {
|
|
293
|
+
const {fetchImpl} = makeFetch(() => new Response("nope", {status: 500}));
|
|
294
|
+
const channel = createHttpChannel({authProvider, baseUrl: "http://api", fetchImpl});
|
|
295
|
+
await expect(channel.sendMutationBatch?.(batch)).rejects.toThrow("non-JSON");
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("rejects when results is missing", async () => {
|
|
299
|
+
const {fetchImpl} = makeFetch(() => json({}));
|
|
300
|
+
const channel = createHttpChannel({authProvider, baseUrl: "http://api", fetchImpl});
|
|
301
|
+
await expect(channel.sendMutationBatch?.(batch)).rejects.toThrow("status 200");
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
|
|
223
305
|
it("uses global fetch when no fetchImpl is provided (network error path)", async () => {
|
|
224
306
|
const channel = createHttpChannel({
|
|
225
307
|
authProvider,
|
package/src/sync/httpChannel.ts
CHANGED
|
@@ -82,6 +82,8 @@ export interface HttpChannelConfig {
|
|
|
82
82
|
authProvider: Pick<AuthProvider, "getToken">;
|
|
83
83
|
/** Fetch implementation override for tests/SSR (defaults to global fetch). */
|
|
84
84
|
fetchImpl?: FetchLike;
|
|
85
|
+
/** Selected organization id attached as `X-Organization-Id` when present. */
|
|
86
|
+
organizationIdProvider?: () => string | undefined;
|
|
85
87
|
}
|
|
86
88
|
|
|
87
89
|
/** Create an {@link HttpChannel} speaking @terreno/api's `/sync/*` routes. */
|
|
@@ -89,15 +91,18 @@ export const createHttpChannel = ({
|
|
|
89
91
|
baseUrl,
|
|
90
92
|
authProvider,
|
|
91
93
|
fetchImpl,
|
|
94
|
+
organizationIdProvider,
|
|
92
95
|
}: HttpChannelConfig): HttpChannel => {
|
|
93
96
|
const fetcher: FetchLike = fetchImpl ?? ((input, init) => globalThis.fetch(input, init));
|
|
94
97
|
|
|
95
98
|
const request = async (path: string, init?: RequestInit): Promise<Response> => {
|
|
96
99
|
const token = await authProvider.getToken();
|
|
100
|
+
const organizationId = organizationIdProvider?.()?.trim();
|
|
97
101
|
const headers: Record<string, string> = {
|
|
98
102
|
Accept: "application/json",
|
|
99
103
|
...(init?.body !== undefined ? {"Content-Type": "application/json"} : {}),
|
|
100
104
|
...(token ? {Authorization: `Bearer ${token}`} : {}),
|
|
105
|
+
...(organizationId ? {"X-Organization-Id": organizationId} : {}),
|
|
101
106
|
};
|
|
102
107
|
const response = await fetcher(`${baseUrl}${path}`, {...init, headers});
|
|
103
108
|
if (response.status === 401) {
|
|
@@ -32,15 +32,18 @@ const debugBody = (mutation: OutboxMutation): Record<string, unknown> | string =
|
|
|
32
32
|
};
|
|
33
33
|
|
|
34
34
|
/** Error-nack retries beyond this attempt count become terminal failures. */
|
|
35
|
+
/** @internal */
|
|
35
36
|
export const MAX_ERROR_NACK_ATTEMPTS = 5;
|
|
36
37
|
|
|
37
38
|
/** Base delay for the error-nack exponential backoff (doubles per attempt). */
|
|
39
|
+
/** @internal */
|
|
38
40
|
export const ERROR_NACK_BASE_BACKOFF_MS = 1_000;
|
|
39
41
|
|
|
40
42
|
/** Base delay for transport-failure backoff (unlimited retries, same cap/jitter shape). */
|
|
41
|
-
|
|
43
|
+
const TRANSPORT_FAILURE_BASE_BACKOFF_MS = 1_000;
|
|
42
44
|
|
|
43
45
|
/** Cap applied to every jittered backoff (error-nack and transport-failure alike). */
|
|
46
|
+
/** @internal */
|
|
44
47
|
export const MAX_BACKOFF_MS = 30_000;
|
|
45
48
|
|
|
46
49
|
/**
|
|
@@ -51,7 +54,7 @@ export const MAX_BACKOFF_MS = 30_000;
|
|
|
51
54
|
* produce one stray `unsupported`) must never permanently downgrade the
|
|
52
55
|
* session to single-sends.
|
|
53
56
|
*/
|
|
54
|
-
|
|
57
|
+
const BATCH_UNSUPPORTED_LATCH_THRESHOLD = 2;
|
|
55
58
|
|
|
56
59
|
/**
|
|
57
60
|
* FIX 5: while latched `batchUnsupported`, re-probe batch support on this
|
|
@@ -59,9 +62,10 @@ export const BATCH_UNSUPPORTED_LATCH_THRESHOLD = 2;
|
|
|
59
62
|
* sends to an upgraded instance, or the original instance could itself be
|
|
60
63
|
* upgraded, without the connection ever dropping.
|
|
61
64
|
*/
|
|
65
|
+
/** @internal */
|
|
62
66
|
export const BATCH_UNSUPPORTED_REPROBE_INTERVAL_MS = 60_000;
|
|
63
67
|
|
|
64
|
-
|
|
68
|
+
interface ReplayResult {
|
|
65
69
|
/** Set when replay stopped early because the server rejected our auth. */
|
|
66
70
|
paused?: "auth";
|
|
67
71
|
}
|
|
@@ -384,6 +388,9 @@ export const createReplayCoordinator = ({
|
|
|
384
388
|
request.data = {};
|
|
385
389
|
}
|
|
386
390
|
}
|
|
391
|
+
if (mutation.mutationMode !== undefined) {
|
|
392
|
+
request.mutationMode = mutation.mutationMode;
|
|
393
|
+
}
|
|
387
394
|
return request;
|
|
388
395
|
};
|
|
389
396
|
|