@syncular/client 0.0.1-60
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/blobs/index.d.ts +7 -0
- package/dist/blobs/index.d.ts.map +1 -0
- package/dist/blobs/index.js +7 -0
- package/dist/blobs/index.js.map +1 -0
- package/dist/blobs/manager.d.ts +345 -0
- package/dist/blobs/manager.d.ts.map +1 -0
- package/dist/blobs/manager.js +749 -0
- package/dist/blobs/manager.js.map +1 -0
- package/dist/blobs/migrate.d.ts +14 -0
- package/dist/blobs/migrate.d.ts.map +1 -0
- package/dist/blobs/migrate.js +59 -0
- package/dist/blobs/migrate.js.map +1 -0
- package/dist/blobs/types.d.ts +62 -0
- package/dist/blobs/types.d.ts.map +1 -0
- package/dist/blobs/types.js +5 -0
- package/dist/blobs/types.js.map +1 -0
- package/dist/client.d.ts +338 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +834 -0
- package/dist/client.js.map +1 -0
- package/dist/conflicts.d.ts +31 -0
- package/dist/conflicts.d.ts.map +1 -0
- package/dist/conflicts.js +118 -0
- package/dist/conflicts.js.map +1 -0
- package/dist/create-client.d.ts +115 -0
- package/dist/create-client.d.ts.map +1 -0
- package/dist/create-client.js +162 -0
- package/dist/create-client.js.map +1 -0
- package/dist/engine/SyncEngine.d.ts +215 -0
- package/dist/engine/SyncEngine.d.ts.map +1 -0
- package/dist/engine/SyncEngine.js +1066 -0
- package/dist/engine/SyncEngine.js.map +1 -0
- package/dist/engine/index.d.ts +6 -0
- package/dist/engine/index.d.ts.map +1 -0
- package/dist/engine/index.js +6 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/engine/types.d.ts +230 -0
- package/dist/engine/types.d.ts.map +1 -0
- package/dist/engine/types.js +7 -0
- package/dist/engine/types.js.map +1 -0
- package/dist/handlers/create-handler.d.ts +110 -0
- package/dist/handlers/create-handler.d.ts.map +1 -0
- package/dist/handlers/create-handler.js +140 -0
- package/dist/handlers/create-handler.js.map +1 -0
- package/dist/handlers/registry.d.ts +15 -0
- package/dist/handlers/registry.d.ts.map +1 -0
- package/dist/handlers/registry.js +29 -0
- package/dist/handlers/registry.js.map +1 -0
- package/dist/handlers/types.d.ts +83 -0
- package/dist/handlers/types.d.ts.map +1 -0
- package/dist/handlers/types.js +5 -0
- package/dist/handlers/types.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/migrate.d.ts +19 -0
- package/dist/migrate.d.ts.map +1 -0
- package/dist/migrate.js +106 -0
- package/dist/migrate.js.map +1 -0
- package/dist/mutations.d.ts +138 -0
- package/dist/mutations.d.ts.map +1 -0
- package/dist/mutations.js +611 -0
- package/dist/mutations.js.map +1 -0
- package/dist/outbox.d.ts +112 -0
- package/dist/outbox.d.ts.map +1 -0
- package/dist/outbox.js +304 -0
- package/dist/outbox.js.map +1 -0
- package/dist/plugins/incrementing-version.d.ts +34 -0
- package/dist/plugins/incrementing-version.d.ts.map +1 -0
- package/dist/plugins/incrementing-version.js +83 -0
- package/dist/plugins/incrementing-version.js.map +1 -0
- package/dist/plugins/index.d.ts +3 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +3 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/plugins/types.d.ts +49 -0
- package/dist/plugins/types.d.ts.map +1 -0
- package/dist/plugins/types.js +15 -0
- package/dist/plugins/types.js.map +1 -0
- package/dist/proxy/connection.d.ts +33 -0
- package/dist/proxy/connection.d.ts.map +1 -0
- package/dist/proxy/connection.js +153 -0
- package/dist/proxy/connection.js.map +1 -0
- package/dist/proxy/dialect.d.ts +46 -0
- package/dist/proxy/dialect.d.ts.map +1 -0
- package/dist/proxy/dialect.js +58 -0
- package/dist/proxy/dialect.js.map +1 -0
- package/dist/proxy/driver.d.ts +42 -0
- package/dist/proxy/driver.d.ts.map +1 -0
- package/dist/proxy/driver.js +78 -0
- package/dist/proxy/driver.js.map +1 -0
- package/dist/proxy/index.d.ts +10 -0
- package/dist/proxy/index.d.ts.map +1 -0
- package/dist/proxy/index.js +10 -0
- package/dist/proxy/index.js.map +1 -0
- package/dist/proxy/mutations.d.ts +9 -0
- package/dist/proxy/mutations.d.ts.map +1 -0
- package/dist/proxy/mutations.js +11 -0
- package/dist/proxy/mutations.js.map +1 -0
- package/dist/pull-engine.d.ts +45 -0
- package/dist/pull-engine.d.ts.map +1 -0
- package/dist/pull-engine.js +391 -0
- package/dist/pull-engine.js.map +1 -0
- package/dist/push-engine.d.ts +18 -0
- package/dist/push-engine.d.ts.map +1 -0
- package/dist/push-engine.js +155 -0
- package/dist/push-engine.js.map +1 -0
- package/dist/query/FingerprintCollector.d.ts +18 -0
- package/dist/query/FingerprintCollector.d.ts.map +1 -0
- package/dist/query/FingerprintCollector.js +28 -0
- package/dist/query/FingerprintCollector.js.map +1 -0
- package/dist/query/QueryContext.d.ts +33 -0
- package/dist/query/QueryContext.d.ts.map +1 -0
- package/dist/query/QueryContext.js +16 -0
- package/dist/query/QueryContext.js.map +1 -0
- package/dist/query/fingerprint.d.ts +61 -0
- package/dist/query/fingerprint.d.ts.map +1 -0
- package/dist/query/fingerprint.js +91 -0
- package/dist/query/fingerprint.js.map +1 -0
- package/dist/query/index.d.ts +7 -0
- package/dist/query/index.d.ts.map +1 -0
- package/dist/query/index.js +7 -0
- package/dist/query/index.js.map +1 -0
- package/dist/query/tracked-select.d.ts +18 -0
- package/dist/query/tracked-select.d.ts.map +1 -0
- package/dist/query/tracked-select.js +90 -0
- package/dist/query/tracked-select.js.map +1 -0
- package/dist/schema.d.ts +83 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +7 -0
- package/dist/schema.js.map +1 -0
- package/dist/sync-loop.d.ts +32 -0
- package/dist/sync-loop.d.ts.map +1 -0
- package/dist/sync-loop.js +249 -0
- package/dist/sync-loop.js.map +1 -0
- package/dist/utils/id.d.ts +8 -0
- package/dist/utils/id.d.ts.map +1 -0
- package/dist/utils/id.js +19 -0
- package/dist/utils/id.js.map +1 -0
- package/package.json +58 -0
- package/src/blobs/index.ts +7 -0
- package/src/blobs/manager.ts +1027 -0
- package/src/blobs/migrate.ts +67 -0
- package/src/blobs/types.ts +84 -0
- package/src/client.ts +1222 -0
- package/src/conflicts.ts +180 -0
- package/src/create-client.ts +297 -0
- package/src/engine/SyncEngine.ts +1337 -0
- package/src/engine/index.ts +6 -0
- package/src/engine/types.ts +268 -0
- package/src/handlers/create-handler.ts +287 -0
- package/src/handlers/registry.ts +36 -0
- package/src/handlers/types.ts +102 -0
- package/src/index.ts +25 -0
- package/src/migrate.ts +122 -0
- package/src/mutations.ts +926 -0
- package/src/outbox.ts +397 -0
- package/src/plugins/incrementing-version.ts +133 -0
- package/src/plugins/index.ts +2 -0
- package/src/plugins/types.ts +63 -0
- package/src/proxy/connection.ts +191 -0
- package/src/proxy/dialect.ts +76 -0
- package/src/proxy/driver.ts +126 -0
- package/src/proxy/index.ts +10 -0
- package/src/proxy/mutations.ts +18 -0
- package/src/pull-engine.ts +518 -0
- package/src/push-engine.ts +201 -0
- package/src/query/FingerprintCollector.ts +29 -0
- package/src/query/QueryContext.ts +54 -0
- package/src/query/fingerprint.ts +109 -0
- package/src/query/index.ts +10 -0
- package/src/query/tracked-select.ts +139 -0
- package/src/schema.ts +94 -0
- package/src/sync-loop.ts +368 -0
- package/src/utils/id.ts +20 -0
package/src/sync-loop.ts
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @syncular/client - High-level sync loops
|
|
3
|
+
*
|
|
4
|
+
* Helpers that run push/pull repeatedly until the client is caught up.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
SyncPullResponse,
|
|
9
|
+
SyncPullSubscriptionResponse,
|
|
10
|
+
SyncPushRequest,
|
|
11
|
+
SyncPushResponse,
|
|
12
|
+
SyncSubscriptionRequest,
|
|
13
|
+
SyncTransport,
|
|
14
|
+
} from '@syncular/core';
|
|
15
|
+
import type { Kysely } from 'kysely';
|
|
16
|
+
import { upsertConflictsForRejectedCommit } from './conflicts';
|
|
17
|
+
import type { ClientTableRegistry } from './handlers/registry';
|
|
18
|
+
import {
|
|
19
|
+
getNextSendableOutboxCommit,
|
|
20
|
+
markOutboxCommitAcked,
|
|
21
|
+
markOutboxCommitFailed,
|
|
22
|
+
markOutboxCommitPending,
|
|
23
|
+
} from './outbox';
|
|
24
|
+
import type { SyncClientPluginContext } from './plugins/types';
|
|
25
|
+
import {
|
|
26
|
+
applyPullResponse,
|
|
27
|
+
buildPullRequest,
|
|
28
|
+
type SyncPullOnceOptions,
|
|
29
|
+
syncPullOnce,
|
|
30
|
+
} from './pull-engine';
|
|
31
|
+
import { type SyncPushOnceOptions, syncPushOnce } from './push-engine';
|
|
32
|
+
import type { SyncClientDb } from './schema';
|
|
33
|
+
|
|
34
|
+
interface SyncPushUntilSettledOptions extends SyncPushOnceOptions {
|
|
35
|
+
/** Max outbox commits to attempt per call. Default: 20 */
|
|
36
|
+
maxCommits?: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface SyncPushUntilSettledResult {
|
|
40
|
+
pushedCount: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function syncPushUntilSettled<DB extends SyncClientDb>(
|
|
44
|
+
db: Kysely<DB>,
|
|
45
|
+
transport: SyncTransport,
|
|
46
|
+
options: SyncPushUntilSettledOptions
|
|
47
|
+
): Promise<SyncPushUntilSettledResult> {
|
|
48
|
+
const maxCommits = Math.max(1, Math.min(1000, options.maxCommits ?? 20));
|
|
49
|
+
|
|
50
|
+
let pushedCount = 0;
|
|
51
|
+
for (let i = 0; i < maxCommits; i++) {
|
|
52
|
+
const res = await syncPushOnce(db, transport, {
|
|
53
|
+
clientId: options.clientId,
|
|
54
|
+
actorId: options.actorId,
|
|
55
|
+
plugins: options.plugins,
|
|
56
|
+
});
|
|
57
|
+
if (!res.pushed) break;
|
|
58
|
+
pushedCount += 1;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return { pushedCount };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface SyncPullUntilSettledOptions extends SyncPullOnceOptions {
|
|
65
|
+
/** Max pull rounds per call. Default: 20 */
|
|
66
|
+
maxRounds?: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface SyncPullUntilSettledResult {
|
|
70
|
+
response: SyncPullResponse;
|
|
71
|
+
rounds: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface TransportWithWsPush extends SyncTransport {
|
|
75
|
+
pushViaWs(request: SyncPushRequest): Promise<SyncPushResponse | null>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function hasPushViaWs(
|
|
79
|
+
transport: SyncTransport
|
|
80
|
+
): transport is TransportWithWsPush {
|
|
81
|
+
return 'pushViaWs' in transport && typeof transport.pushViaWs === 'function';
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function needsAnotherPull(res: SyncPullResponse): boolean {
|
|
85
|
+
for (const sub of res.subscriptions ?? []) {
|
|
86
|
+
if (sub.status !== 'active') continue;
|
|
87
|
+
if (sub.bootstrap) return true;
|
|
88
|
+
if ((sub.commits?.length ?? 0) > 0) return true;
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function mergePullResponse(
|
|
94
|
+
targetBySubId: Map<string, SyncPullSubscriptionResponse>,
|
|
95
|
+
res: SyncPullResponse
|
|
96
|
+
): void {
|
|
97
|
+
for (const sub of res.subscriptions ?? []) {
|
|
98
|
+
const prev = targetBySubId.get(sub.id);
|
|
99
|
+
if (!prev) {
|
|
100
|
+
const merged: SyncPullSubscriptionResponse = {
|
|
101
|
+
...sub,
|
|
102
|
+
commits: [...(sub.commits ?? [])],
|
|
103
|
+
snapshots: [...(sub.snapshots ?? [])],
|
|
104
|
+
};
|
|
105
|
+
targetBySubId.set(sub.id, merged);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const merged: SyncPullSubscriptionResponse = {
|
|
110
|
+
...prev,
|
|
111
|
+
...sub,
|
|
112
|
+
commits: [...(prev.commits ?? []), ...(sub.commits ?? [])],
|
|
113
|
+
snapshots: [...(prev.snapshots ?? []), ...(sub.snapshots ?? [])],
|
|
114
|
+
};
|
|
115
|
+
targetBySubId.set(sub.id, merged);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function syncPullUntilSettled<DB extends SyncClientDb>(
|
|
120
|
+
db: Kysely<DB>,
|
|
121
|
+
transport: SyncTransport,
|
|
122
|
+
shapes: ClientTableRegistry<DB>,
|
|
123
|
+
options: SyncPullUntilSettledOptions
|
|
124
|
+
): Promise<SyncPullUntilSettledResult> {
|
|
125
|
+
const maxRounds = Math.max(1, Math.min(1000, options.maxRounds ?? 20));
|
|
126
|
+
|
|
127
|
+
const aggregatedBySubId = new Map<string, SyncPullSubscriptionResponse>();
|
|
128
|
+
let rounds = 0;
|
|
129
|
+
|
|
130
|
+
for (let i = 0; i < maxRounds; i++) {
|
|
131
|
+
rounds += 1;
|
|
132
|
+
const res = await syncPullOnce(db, transport, shapes, options);
|
|
133
|
+
mergePullResponse(aggregatedBySubId, res);
|
|
134
|
+
|
|
135
|
+
if (!needsAnotherPull(res)) break;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
// Return an aggregate response so callers can see what was applied across
|
|
140
|
+
// all pull rounds (the last round is often empty by design).
|
|
141
|
+
response: {
|
|
142
|
+
ok: true,
|
|
143
|
+
subscriptions: Array.from(aggregatedBySubId.values()),
|
|
144
|
+
},
|
|
145
|
+
rounds,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface SyncOnceOptions {
|
|
150
|
+
clientId: string;
|
|
151
|
+
actorId?: string;
|
|
152
|
+
plugins?: SyncPushOnceOptions['plugins'];
|
|
153
|
+
subscriptions: Array<Omit<SyncSubscriptionRequest, 'cursor'>>;
|
|
154
|
+
limitCommits?: number;
|
|
155
|
+
limitSnapshotRows?: number;
|
|
156
|
+
maxSnapshotPages?: number;
|
|
157
|
+
dedupeRows?: boolean;
|
|
158
|
+
stateId?: string;
|
|
159
|
+
maxPushCommits?: number;
|
|
160
|
+
maxPullRounds?: number;
|
|
161
|
+
/** When 'ws', peek outbox first and skip push if empty. */
|
|
162
|
+
trigger?: 'ws' | 'local' | 'poll';
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export interface SyncOnceResult {
|
|
166
|
+
pushedCommits: number;
|
|
167
|
+
pullRounds: number;
|
|
168
|
+
pullResponse: SyncPullResponse;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Sync once using a WS-first push strategy for the first outbox commit.
|
|
173
|
+
*
|
|
174
|
+
* - If transport supports `pushViaWs` and the commit succeeds over WS, this call
|
|
175
|
+
* sends only an HTTP pull request.
|
|
176
|
+
* - Otherwise it falls back to combined HTTP push+pull.
|
|
177
|
+
*
|
|
178
|
+
* Remaining outbox commits are then settled via `syncPushUntilSettled`.
|
|
179
|
+
*/
|
|
180
|
+
async function syncOnceCombined<DB extends SyncClientDb>(
|
|
181
|
+
db: Kysely<DB>,
|
|
182
|
+
transport: SyncTransport,
|
|
183
|
+
shapes: ClientTableRegistry<DB>,
|
|
184
|
+
options: SyncOnceOptions
|
|
185
|
+
): Promise<SyncOnceResult> {
|
|
186
|
+
const pullOpts: SyncPullOnceOptions = {
|
|
187
|
+
clientId: options.clientId,
|
|
188
|
+
actorId: options.actorId,
|
|
189
|
+
plugins: options.plugins,
|
|
190
|
+
subscriptions: options.subscriptions,
|
|
191
|
+
limitCommits: options.limitCommits,
|
|
192
|
+
limitSnapshotRows: options.limitSnapshotRows,
|
|
193
|
+
maxSnapshotPages: options.maxSnapshotPages,
|
|
194
|
+
dedupeRows: options.dedupeRows,
|
|
195
|
+
stateId: options.stateId,
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
// Build pull request (reads subscription state)
|
|
199
|
+
const pullState = await buildPullRequest(db, pullOpts);
|
|
200
|
+
const { clientId } = pullState.request;
|
|
201
|
+
|
|
202
|
+
// Grab at most one outbox commit
|
|
203
|
+
const outbox = await getNextSendableOutboxCommit(db);
|
|
204
|
+
|
|
205
|
+
const plugins = options.plugins ?? [];
|
|
206
|
+
const ctx: SyncClientPluginContext = {
|
|
207
|
+
actorId: options.actorId ?? 'unknown',
|
|
208
|
+
clientId,
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// Build push request, running beforePush plugins
|
|
212
|
+
let pushRequest: SyncPushRequest | undefined;
|
|
213
|
+
if (outbox) {
|
|
214
|
+
pushRequest = {
|
|
215
|
+
clientId,
|
|
216
|
+
clientCommitId: outbox.client_commit_id,
|
|
217
|
+
operations: outbox.operations,
|
|
218
|
+
schemaVersion: outbox.schema_version,
|
|
219
|
+
};
|
|
220
|
+
for (const plugin of plugins) {
|
|
221
|
+
if (!plugin.beforePush) continue;
|
|
222
|
+
pushRequest = await plugin.beforePush(ctx, pushRequest);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Try WS push first for the first outbox commit (if realtime transport supports it).
|
|
227
|
+
// Fall back to HTTP push in the combined request when WS is unavailable or fails.
|
|
228
|
+
let wsPushResponse: SyncPushResponse | null = null;
|
|
229
|
+
if (pushRequest && hasPushViaWs(transport)) {
|
|
230
|
+
try {
|
|
231
|
+
wsPushResponse = await transport.pushViaWs(pushRequest);
|
|
232
|
+
} catch {
|
|
233
|
+
wsPushResponse = null;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const combined = await transport.sync({
|
|
238
|
+
clientId,
|
|
239
|
+
...(pushRequest && !wsPushResponse
|
|
240
|
+
? {
|
|
241
|
+
push: {
|
|
242
|
+
clientCommitId: pushRequest.clientCommitId,
|
|
243
|
+
operations: pushRequest.operations,
|
|
244
|
+
schemaVersion: pushRequest.schemaVersion,
|
|
245
|
+
},
|
|
246
|
+
}
|
|
247
|
+
: {}),
|
|
248
|
+
pull: {
|
|
249
|
+
limitCommits: pullState.request.limitCommits,
|
|
250
|
+
limitSnapshotRows: pullState.request.limitSnapshotRows,
|
|
251
|
+
maxSnapshotPages: pullState.request.maxSnapshotPages,
|
|
252
|
+
dedupeRows: pullState.request.dedupeRows,
|
|
253
|
+
subscriptions: pullState.request.subscriptions,
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
// Process push response
|
|
258
|
+
let pushedCommits = 0;
|
|
259
|
+
if (outbox && pushRequest) {
|
|
260
|
+
let pushRes = wsPushResponse ?? combined.push;
|
|
261
|
+
if (!pushRes) {
|
|
262
|
+
await markOutboxCommitPending(db, {
|
|
263
|
+
id: outbox.id,
|
|
264
|
+
error: 'MISSING_PUSH_RESPONSE',
|
|
265
|
+
});
|
|
266
|
+
throw new Error('Server returned no push response');
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Run afterPush plugins
|
|
270
|
+
for (const plugin of plugins) {
|
|
271
|
+
if (!plugin.afterPush) continue;
|
|
272
|
+
pushRes = await plugin.afterPush(ctx, {
|
|
273
|
+
request: pushRequest,
|
|
274
|
+
response: pushRes,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const responseJson = JSON.stringify(pushRes);
|
|
279
|
+
|
|
280
|
+
if (pushRes.status === 'applied' || pushRes.status === 'cached') {
|
|
281
|
+
await markOutboxCommitAcked(db, {
|
|
282
|
+
id: outbox.id,
|
|
283
|
+
commitSeq: pushRes.commitSeq ?? null,
|
|
284
|
+
responseJson,
|
|
285
|
+
});
|
|
286
|
+
pushedCommits = 1;
|
|
287
|
+
} else {
|
|
288
|
+
// Check if all errors are retriable
|
|
289
|
+
const errorResults = pushRes.results.filter((r) => r.status === 'error');
|
|
290
|
+
const allRetriable =
|
|
291
|
+
errorResults.length > 0 &&
|
|
292
|
+
errorResults.every((r) => r.retriable === true);
|
|
293
|
+
|
|
294
|
+
if (allRetriable) {
|
|
295
|
+
await markOutboxCommitPending(db, {
|
|
296
|
+
id: outbox.id,
|
|
297
|
+
error: 'Retriable',
|
|
298
|
+
responseJson,
|
|
299
|
+
});
|
|
300
|
+
pushedCommits = 1;
|
|
301
|
+
} else {
|
|
302
|
+
await upsertConflictsForRejectedCommit(db, {
|
|
303
|
+
outboxCommitId: outbox.id,
|
|
304
|
+
clientCommitId: outbox.client_commit_id,
|
|
305
|
+
response: pushRes,
|
|
306
|
+
});
|
|
307
|
+
await markOutboxCommitFailed(db, {
|
|
308
|
+
id: outbox.id,
|
|
309
|
+
error: 'REJECTED',
|
|
310
|
+
responseJson,
|
|
311
|
+
});
|
|
312
|
+
pushedCommits = 1;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Settle remaining outbox commits
|
|
317
|
+
const remaining = await syncPushUntilSettled(db, transport, {
|
|
318
|
+
clientId: options.clientId,
|
|
319
|
+
actorId: options.actorId,
|
|
320
|
+
plugins: options.plugins,
|
|
321
|
+
maxCommits: (options.maxPushCommits ?? 20) - 1,
|
|
322
|
+
});
|
|
323
|
+
pushedCommits += remaining.pushedCount;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Process pull response
|
|
327
|
+
let pullResponse: SyncPullResponse = { ok: true, subscriptions: [] };
|
|
328
|
+
let pullRounds = 0;
|
|
329
|
+
if (combined.pull) {
|
|
330
|
+
pullResponse = await applyPullResponse(
|
|
331
|
+
db,
|
|
332
|
+
transport,
|
|
333
|
+
shapes,
|
|
334
|
+
pullOpts,
|
|
335
|
+
pullState,
|
|
336
|
+
combined.pull
|
|
337
|
+
);
|
|
338
|
+
pullRounds = 1;
|
|
339
|
+
|
|
340
|
+
// Continue pulling if more data
|
|
341
|
+
if (needsAnotherPull(pullResponse)) {
|
|
342
|
+
const aggregatedBySubId = new Map<string, SyncPullSubscriptionResponse>();
|
|
343
|
+
mergePullResponse(aggregatedBySubId, pullResponse);
|
|
344
|
+
|
|
345
|
+
const more = await syncPullUntilSettled(db, transport, shapes, {
|
|
346
|
+
...pullOpts,
|
|
347
|
+
maxRounds: (options.maxPullRounds ?? 20) - 1,
|
|
348
|
+
});
|
|
349
|
+
pullRounds += more.rounds;
|
|
350
|
+
mergePullResponse(aggregatedBySubId, more.response);
|
|
351
|
+
pullResponse = {
|
|
352
|
+
ok: true,
|
|
353
|
+
subscriptions: Array.from(aggregatedBySubId.values()),
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
return { pushedCommits, pullRounds, pullResponse };
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export async function syncOnce<DB extends SyncClientDb>(
|
|
362
|
+
db: Kysely<DB>,
|
|
363
|
+
transport: SyncTransport,
|
|
364
|
+
shapes: ClientTableRegistry<DB>,
|
|
365
|
+
options: SyncOnceOptions
|
|
366
|
+
): Promise<SyncOnceResult> {
|
|
367
|
+
return syncOnceCombined(db, transport, shapes, options);
|
|
368
|
+
}
|
package/src/utils/id.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ID generation utilities
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generate a random UUID v4
|
|
7
|
+
*/
|
|
8
|
+
export function randomUUID(): string {
|
|
9
|
+
// Use crypto.randomUUID if available (modern browsers, Node, Bun)
|
|
10
|
+
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
|
|
11
|
+
return crypto.randomUUID();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Fallback implementation
|
|
15
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
16
|
+
const r = (Math.random() * 16) | 0;
|
|
17
|
+
const v = c === 'x' ? r : (r & 0x3) | 0x8;
|
|
18
|
+
return v.toString(16);
|
|
19
|
+
});
|
|
20
|
+
}
|