local-lite 1.0.4 → 2.0.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/adapters/local/better-sqlite3.js +10 -3
- package/dist/adapters/local/expo-sqlite.js +3 -3
- package/dist/adapters/local/node-sqlite.js +10 -3
- package/dist/adapters/remote/index.d.ts +5 -2
- package/dist/adapters/remote/self-hosted.d.ts +3 -3
- package/dist/adapters/remote/self-hosted.js +17 -4
- package/dist/adapters/remote/supabase.d.ts +1 -2
- package/dist/config/index.d.ts +0 -1
- package/dist/config/index.js +3 -10
- package/dist/config/types.d.ts +5 -10
- package/dist/core/client.d.ts +3 -12
- package/dist/core/client.js +166 -59
- package/dist/core/outbox-store.d.ts +1 -1
- package/dist/core/outbox-store.js +58 -21
- package/dist/core/query-builder.js +36 -45
- package/dist/core/sync-worker.d.ts +12 -2
- package/dist/core/sync-worker.js +84 -21
- package/dist/core/table-manager.js +19 -8
- package/dist/core/types.d.ts +1 -0
- package/dist/react/index.js +54 -2
- package/dist/server/sync.d.ts +38 -0
- package/dist/server/sync.js +214 -2
- package/dist/storage/supabase.d.ts +7 -2
- package/package.json +4 -5
- package/skills/local-lite-auth/SKILL.md +1 -1
- package/skills/local-lite-setup/SKILL.md +1 -1
- package/skills/local-lite-upgrade/SKILL.md +32 -9
package/dist/core/sync-worker.js
CHANGED
|
@@ -13,6 +13,10 @@ const REACT_NATIVE_PKG = ['react', 'native'].join('-');
|
|
|
13
13
|
const PUSH_BATCH_SIZE = 50;
|
|
14
14
|
/** Maximum incoming delta changes applied in a single database transaction. */
|
|
15
15
|
const DELTA_CHUNK_SIZE = 250;
|
|
16
|
+
/** Default timeout in milliseconds for remote push/pull requests. */
|
|
17
|
+
const DEFAULT_REQUEST_TIMEOUT_MS = 30000;
|
|
18
|
+
/** Safety cap on consecutive paginated pull pages per cycle. */
|
|
19
|
+
const MAX_PULL_PAGES = 100;
|
|
16
20
|
function stableScopeValue(value) {
|
|
17
21
|
if (Array.isArray(value))
|
|
18
22
|
return value.map(stableScopeValue);
|
|
@@ -42,6 +46,7 @@ class SyncWorker {
|
|
|
42
46
|
retryConfig;
|
|
43
47
|
pushIntervalMs;
|
|
44
48
|
pullIntervalMs;
|
|
49
|
+
requestTimeoutMs;
|
|
45
50
|
maxRetries;
|
|
46
51
|
autoPauseOnBackground;
|
|
47
52
|
isPaused = false;
|
|
@@ -62,6 +67,7 @@ class SyncWorker {
|
|
|
62
67
|
deltaQueue = Promise.resolve();
|
|
63
68
|
statusRun;
|
|
64
69
|
statusRerunRequested = false;
|
|
70
|
+
activeControllers = new Set();
|
|
65
71
|
// Telemetry metrics
|
|
66
72
|
totalPushedOperations = 0;
|
|
67
73
|
totalPulledDeltas = 0;
|
|
@@ -85,6 +91,8 @@ class SyncWorker {
|
|
|
85
91
|
this.schemaManager = options.schemaManager;
|
|
86
92
|
this.pushIntervalMs = options.pushIntervalMs ?? 5000;
|
|
87
93
|
this.pullIntervalMs = options.pullIntervalMs ?? 10000;
|
|
94
|
+
this.requestTimeoutMs =
|
|
95
|
+
options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
|
|
88
96
|
this.syncScope = options.syncScope;
|
|
89
97
|
this.autoPauseOnBackground = options.autoPauseOnBackground ?? true;
|
|
90
98
|
this.logger = (0, logger_js_1.createSyncLogger)(options.logger, options.logLevel);
|
|
@@ -101,7 +109,7 @@ class SyncWorker {
|
|
|
101
109
|
}
|
|
102
110
|
else if (typeof r === 'object' && r !== null) {
|
|
103
111
|
this.retryConfig = {
|
|
104
|
-
maxRetries: r.maxRetries ??
|
|
112
|
+
maxRetries: r.maxRetries ?? 5,
|
|
105
113
|
initialDelayMs: r.initialDelayMs ?? 1000,
|
|
106
114
|
backoffMultiplier: r.backoffMultiplier ?? 1.5,
|
|
107
115
|
maxDelayMs: r.maxDelayMs ?? 30000,
|
|
@@ -111,7 +119,7 @@ class SyncWorker {
|
|
|
111
119
|
}
|
|
112
120
|
else {
|
|
113
121
|
this.retryConfig = {
|
|
114
|
-
maxRetries:
|
|
122
|
+
maxRetries: 5,
|
|
115
123
|
initialDelayMs: 1000,
|
|
116
124
|
backoffMultiplier: 1.5,
|
|
117
125
|
maxDelayMs: 30000,
|
|
@@ -198,6 +206,10 @@ class SyncWorker {
|
|
|
198
206
|
this.networkUnsubscribe();
|
|
199
207
|
this.networkUnsubscribe = undefined;
|
|
200
208
|
}
|
|
209
|
+
for (const controller of this.activeControllers) {
|
|
210
|
+
controller.abort();
|
|
211
|
+
}
|
|
212
|
+
this.activeControllers.clear();
|
|
201
213
|
}
|
|
202
214
|
/**
|
|
203
215
|
* Pauses recurring interval timers and active sync loops to conserve battery and data.
|
|
@@ -340,6 +352,30 @@ class SyncWorker {
|
|
|
340
352
|
this.pullRemoteChanges().catch((err) => this.logger.error('Error pulling after sync scope change:', err));
|
|
341
353
|
}
|
|
342
354
|
}
|
|
355
|
+
async runWithAbortTimeout(timeoutMs, message, fn) {
|
|
356
|
+
const controller = new AbortController();
|
|
357
|
+
this.activeControllers.add(controller);
|
|
358
|
+
let timer;
|
|
359
|
+
try {
|
|
360
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
361
|
+
return await fn(controller.signal);
|
|
362
|
+
}
|
|
363
|
+
return await Promise.race([
|
|
364
|
+
fn(controller.signal),
|
|
365
|
+
new Promise((_, reject) => {
|
|
366
|
+
timer = setTimeout(() => {
|
|
367
|
+
controller.abort();
|
|
368
|
+
reject(new Error(message));
|
|
369
|
+
}, timeoutMs);
|
|
370
|
+
}),
|
|
371
|
+
]);
|
|
372
|
+
}
|
|
373
|
+
finally {
|
|
374
|
+
if (timer !== undefined)
|
|
375
|
+
clearTimeout(timer);
|
|
376
|
+
this.activeControllers.delete(controller);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
343
379
|
async isOnline() {
|
|
344
380
|
try {
|
|
345
381
|
return await Promise.resolve(this.network.isOnline());
|
|
@@ -422,7 +458,9 @@ class SyncWorker {
|
|
|
422
458
|
const recordIds = operations.map((o) => o.id);
|
|
423
459
|
// Mark records as PROCESSING
|
|
424
460
|
await outbox_store_js_1.OutboxStore.markProcessing(this.db, recordIds);
|
|
425
|
-
const pushRes = await this.remote.pushChanges
|
|
461
|
+
const pushRes = await this.runWithAbortTimeout(this.requestTimeoutMs, `Remote push timed out after ${this.requestTimeoutMs}ms`, (signal) => this.remote.pushChanges.length >= 2
|
|
462
|
+
? this.remote.pushChanges(operations, { signal })
|
|
463
|
+
: this.remote.pushChanges(operations));
|
|
426
464
|
const durationMs = Date.now() - startTime;
|
|
427
465
|
this.lastPushDurationMs = durationMs;
|
|
428
466
|
this.lastPushTimestamp = Date.now();
|
|
@@ -574,27 +612,44 @@ class SyncWorker {
|
|
|
574
612
|
: this.syncScope;
|
|
575
613
|
const cursorKey = getScopeCursorKey(scope);
|
|
576
614
|
await this.loadCursor(cursorKey);
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
615
|
+
let totalChangesInCycle = 0;
|
|
616
|
+
for (let page = 0; page < MAX_PULL_PAGES; page++) {
|
|
617
|
+
if (this.isPaused)
|
|
618
|
+
break;
|
|
619
|
+
const sinceTimestamp = this.lastPulledTimestamp;
|
|
620
|
+
const deltas = await this.runWithAbortTimeout(this.requestTimeoutMs, `Remote pull timed out after ${this.requestTimeoutMs}ms`, (signal) => {
|
|
621
|
+
const acceptsOptions = this.remote.fetchChanges.length >= 3;
|
|
622
|
+
if (scope !== undefined) {
|
|
623
|
+
return acceptsOptions
|
|
624
|
+
? this.remote.fetchChanges(sinceTimestamp, scope, { signal })
|
|
625
|
+
: this.remote.fetchChanges(sinceTimestamp, scope);
|
|
626
|
+
}
|
|
627
|
+
return acceptsOptions
|
|
628
|
+
? this.remote.fetchChanges(sinceTimestamp, undefined, { signal })
|
|
629
|
+
: this.remote.fetchChanges(sinceTimestamp);
|
|
630
|
+
});
|
|
631
|
+
const pageCount = deltas?.changes?.length || 0;
|
|
632
|
+
totalChangesInCycle += pageCount;
|
|
633
|
+
if (deltas) {
|
|
634
|
+
this.totalPulledDeltas += pageCount;
|
|
635
|
+
await this.applyDeltasLocally(deltas, { cursorKey });
|
|
636
|
+
}
|
|
637
|
+
const hasMore = Boolean(deltas?.hasMore);
|
|
638
|
+
if (!hasMore || pageCount === 0) {
|
|
639
|
+
break;
|
|
640
|
+
}
|
|
641
|
+
// Prevent infinite loop if a buggy adapter returns hasMore without advancing timestamp
|
|
642
|
+
if (deltas?.timestamp !== undefined && deltas.timestamp <= sinceTimestamp) {
|
|
643
|
+
break;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
581
646
|
const durationMs = Date.now() - startTime;
|
|
582
647
|
this.lastPullDurationMs = durationMs;
|
|
583
648
|
this.lastPullTimestamp = Date.now();
|
|
584
649
|
this.lastPullError = null;
|
|
585
|
-
const changesCount = deltas?.changes?.length || 0;
|
|
586
|
-
if (deltas) {
|
|
587
|
-
this.totalPulledDeltas += changesCount;
|
|
588
|
-
await this.applyDeltasLocally(deltas, { cursorKey });
|
|
589
|
-
if (deltas.timestamp && deltas.timestamp > sinceTimestamp) {
|
|
590
|
-
if (cursorKey === this.lastSyncedAtKey)
|
|
591
|
-
this.lastPulledTimestamp = deltas.timestamp;
|
|
592
|
-
await meta_store_js_1.MetaStore.setLastSyncedAt(this.db, deltas.timestamp, cursorKey);
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
650
|
const pullEvent = {
|
|
596
651
|
success: true,
|
|
597
|
-
changesCount,
|
|
652
|
+
changesCount: totalChangesInCycle,
|
|
598
653
|
durationMs,
|
|
599
654
|
timestamp: this.lastPulledTimestamp,
|
|
600
655
|
error: null,
|
|
@@ -660,8 +715,8 @@ class SyncWorker {
|
|
|
660
715
|
const isLastChunk = i + DELTA_CHUNK_SIZE >= totalChanges;
|
|
661
716
|
const queuedEvents = [];
|
|
662
717
|
await this.db.transaction(async (tx) => {
|
|
663
|
-
// Pre-fetch active pending outbox records in 1 query
|
|
664
|
-
const pendingOutboxMap = await outbox_store_js_1.OutboxStore.getActivePendingMap(tx);
|
|
718
|
+
// Pre-fetch active pending outbox records for this chunk's rows in 1 query to eliminate N+1 bridge roundtrips
|
|
719
|
+
const pendingOutboxMap = await outbox_store_js_1.OutboxStore.getActivePendingMap(tx, chunk.map((c) => c.rowId));
|
|
665
720
|
for (const change of chunk) {
|
|
666
721
|
const outboxKey = `${change.tableName}:${change.rowId}`;
|
|
667
722
|
// 1. Check in-memory map for pending local mutations for this (table_name, row_id)
|
|
@@ -764,7 +819,15 @@ class SyncWorker {
|
|
|
764
819
|
this.lastPulledTimestamp = 0;
|
|
765
820
|
this.cursorLoaded = true;
|
|
766
821
|
}
|
|
767
|
-
/**
|
|
822
|
+
/**
|
|
823
|
+
* Waits for any in-flight delta ingestion queue to drain.
|
|
824
|
+
*/
|
|
825
|
+
async flush() {
|
|
826
|
+
await this.deltaQueue;
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* Clears a stale push error after an explicit sync stop/reconfiguration.
|
|
830
|
+
*/
|
|
768
831
|
clearPushError() {
|
|
769
832
|
this.lastPushError = null;
|
|
770
833
|
this.emitStatus();
|
|
@@ -60,14 +60,25 @@ class TableManager {
|
|
|
60
60
|
// and direct UPDATE when updating an existing row so omitted NOT NULL columns do not fail.
|
|
61
61
|
const updateKeys = keys.filter((k) => k !== primaryKey);
|
|
62
62
|
if (operation === 'UPDATE' && updateKeys.length > 0) {
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
63
|
+
const setClause = updateKeys
|
|
64
|
+
.map((k) => `${(0, sql_js_1.escapeIdentifier)(k)} = ?`)
|
|
65
|
+
.join(', ');
|
|
66
|
+
const setValues = [...updateKeys.map((k) => rowData[k]), rowId];
|
|
67
|
+
const res = await tx.execute(`UPDATE ${safeTable} SET ${setClause} WHERE ${safePk} = ?`, setValues);
|
|
68
|
+
const meta = res?.[0];
|
|
69
|
+
const changes = typeof meta?.changes === 'number'
|
|
70
|
+
? meta.changes
|
|
71
|
+
: typeof meta?.rowsAffected === 'number'
|
|
72
|
+
? meta.rowsAffected
|
|
73
|
+
: undefined;
|
|
74
|
+
if (changes !== undefined) {
|
|
75
|
+
if (changes > 0)
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
const existing = await TableManager.fetchRow(tx, tableName, rowId, primaryKey, options);
|
|
80
|
+
if (existing)
|
|
81
|
+
return;
|
|
71
82
|
}
|
|
72
83
|
}
|
|
73
84
|
const cols = keys.map((k) => (0, sql_js_1.escapeIdentifier)(k)).join(', ');
|
package/dist/core/types.d.ts
CHANGED
package/dist/react/index.js
CHANGED
|
@@ -161,6 +161,42 @@ function clearLiveQueryCache() {
|
|
|
161
161
|
queryCache.clear();
|
|
162
162
|
inFlightQueries.clear();
|
|
163
163
|
}
|
|
164
|
+
function isPlainEqualityMatch(actual, expected) {
|
|
165
|
+
if (typeof expected === 'boolean') {
|
|
166
|
+
return actual === expected || actual === (expected ? 1 : 0);
|
|
167
|
+
}
|
|
168
|
+
return actual === expected;
|
|
169
|
+
}
|
|
170
|
+
function shouldInvalidateLiveQuery(event, options, currentRows, primaryKey) {
|
|
171
|
+
if (!event || !event.rowId || event.rowId === 'batch')
|
|
172
|
+
return true;
|
|
173
|
+
if (!options || !options.where || typeof options.where === 'function') {
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
const rowIdStr = String(event.rowId);
|
|
177
|
+
const inResult = currentRows.some((r) => r && r[primaryKey] !== undefined && String(r[primaryKey]) === rowIdStr);
|
|
178
|
+
if (event.operation === 'DELETE') {
|
|
179
|
+
return inResult;
|
|
180
|
+
}
|
|
181
|
+
if ((event.operation === 'INSERT' || event.operation === 'UPDATE') &&
|
|
182
|
+
!inResult &&
|
|
183
|
+
event.payload &&
|
|
184
|
+
typeof event.payload === 'object') {
|
|
185
|
+
const whereObj = options.where;
|
|
186
|
+
if ('$or' in whereObj || '$and' in whereObj)
|
|
187
|
+
return true;
|
|
188
|
+
for (const [key, expected] of Object.entries(whereObj)) {
|
|
189
|
+
if (expected !== null && typeof expected === 'object')
|
|
190
|
+
continue;
|
|
191
|
+
if (Object.prototype.hasOwnProperty.call(event.payload, key)) {
|
|
192
|
+
if (!isPlainEqualityMatch(event.payload[key], expected)) {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
164
200
|
/**
|
|
165
201
|
* Hook to execute a live reactive table query that automatically re-runs
|
|
166
202
|
* whenever the table is mutated locally or via incoming remote delta.
|
|
@@ -224,11 +260,21 @@ function useLiveQuery(client, tableName, queryOrOptions) {
|
|
|
224
260
|
}
|
|
225
261
|
}, [client, tableName, cacheKey]);
|
|
226
262
|
const scheduleRefetch = (0, runner_js_1.useCoalescedRunner)(refetch);
|
|
263
|
+
const dataRef = (0, react_1.useRef)(data);
|
|
264
|
+
dataRef.current = data;
|
|
227
265
|
(0, react_1.useEffect)(() => {
|
|
228
266
|
if (!client || !client.db)
|
|
229
267
|
return;
|
|
230
268
|
refetch();
|
|
231
|
-
const
|
|
269
|
+
const pk = client.getPrimaryKey();
|
|
270
|
+
const unsubscribe = client.events.on(tableName, (event) => {
|
|
271
|
+
const opts = typeof queryParamRef.current === 'object' &&
|
|
272
|
+
queryParamRef.current !== null
|
|
273
|
+
? queryParamRef.current
|
|
274
|
+
: undefined;
|
|
275
|
+
if (!shouldInvalidateLiveQuery(event, opts, dataRef.current, pk)) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
232
278
|
advanceTableVersion(client, tableName);
|
|
233
279
|
scheduleRefetch();
|
|
234
280
|
});
|
|
@@ -545,6 +591,8 @@ function useLiveInfiniteQuery(client, tableName, options = {}) {
|
|
|
545
591
|
await loadData(pageCountRef.current, false);
|
|
546
592
|
}, [loadData]);
|
|
547
593
|
const scheduleRefetch = (0, runner_js_1.useCoalescedRunner)(refetch);
|
|
594
|
+
const dataRef = (0, react_1.useRef)(data);
|
|
595
|
+
dataRef.current = data;
|
|
548
596
|
const fetchNextPage = (0, react_1.useCallback)(async () => {
|
|
549
597
|
if (!hasNextPage || isFetchingNextPage)
|
|
550
598
|
return;
|
|
@@ -556,7 +604,11 @@ function useLiveInfiniteQuery(client, tableName, options = {}) {
|
|
|
556
604
|
if (!client || !client.db)
|
|
557
605
|
return;
|
|
558
606
|
loadData(1, false);
|
|
559
|
-
const
|
|
607
|
+
const pk = client.getPrimaryKey();
|
|
608
|
+
const unsubscribe = client.events.on(tableName, (event) => {
|
|
609
|
+
if (!shouldInvalidateLiveQuery(event, optionsRef.current, dataRef.current, pk)) {
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
560
612
|
advanceTableVersion(client, tableName);
|
|
561
613
|
scheduleRefetch();
|
|
562
614
|
});
|
package/dist/server/sync.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { DeltaChanges, SyncOperation, SyncOp } from '../core/types.js';
|
|
2
|
+
import type { LocalDBAdapter } from '../adapters/local/index.js';
|
|
2
3
|
import type { ApiEndpoint } from './index.js';
|
|
3
4
|
export interface ServerSyncRecord {
|
|
4
5
|
rowId: string;
|
|
@@ -36,6 +37,10 @@ export declare class MemoryServerSyncStorage implements ServerSyncStorage {
|
|
|
36
37
|
private tables;
|
|
37
38
|
private changeLog;
|
|
38
39
|
private monotonicCursor;
|
|
40
|
+
private maxChangeLogEntries;
|
|
41
|
+
constructor(options?: {
|
|
42
|
+
maxChangeLogEntries?: number;
|
|
43
|
+
});
|
|
39
44
|
applyOperations(operations: SyncOperation[], overrideTime?: number): Promise<{
|
|
40
45
|
applied: number;
|
|
41
46
|
successfulIds: string[];
|
|
@@ -53,6 +58,39 @@ export declare class MemoryServerSyncStorage implements ServerSyncStorage {
|
|
|
53
58
|
*/
|
|
54
59
|
clear(): void;
|
|
55
60
|
}
|
|
61
|
+
export interface SQLiteServerSyncStorageOptions {
|
|
62
|
+
/**
|
|
63
|
+
* Optional maximum changelog entries to retain. Older entries are compacted after writes.
|
|
64
|
+
*/
|
|
65
|
+
maxChangeLogEntries?: number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Persistent SQLite-backed ServerSyncStorage for production self-hosted sync servers.
|
|
69
|
+
* Works with any LocalDBAdapter (e.g. BetterSQLite3Adapter, NodeSQLiteAdapter, LibSQLAdapter).
|
|
70
|
+
*/
|
|
71
|
+
export declare class SQLiteServerSyncStorage implements ServerSyncStorage {
|
|
72
|
+
private db;
|
|
73
|
+
private initPromise?;
|
|
74
|
+
private maxChangeLogEntries?;
|
|
75
|
+
constructor(db: LocalDBAdapter, options?: SQLiteServerSyncStorageOptions);
|
|
76
|
+
private ensureSchema;
|
|
77
|
+
applyOperations(operations: SyncOperation[], overrideTime?: number): Promise<{
|
|
78
|
+
applied: number;
|
|
79
|
+
successfulIds: string[];
|
|
80
|
+
serverTime: number;
|
|
81
|
+
cursor: number;
|
|
82
|
+
}>;
|
|
83
|
+
getDeltas(tables: string[], since: number, limit?: number): Promise<{
|
|
84
|
+
changes: DeltaChanges;
|
|
85
|
+
cursor: number;
|
|
86
|
+
hasMore: boolean;
|
|
87
|
+
serverTime: number;
|
|
88
|
+
}>;
|
|
89
|
+
/**
|
|
90
|
+
* Compacts older changelog entries, retaining the most recent `retentionCount` rows.
|
|
91
|
+
*/
|
|
92
|
+
compactChangeLog(retentionCount?: number): Promise<void>;
|
|
93
|
+
}
|
|
56
94
|
export interface ServerSyncEndpointOptions {
|
|
57
95
|
/**
|
|
58
96
|
* Optional secret token expected in `Authorization: Bearer <token>` or `x-sync-secret`.
|
package/dist/server/sync.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MemoryServerSyncStorage = void 0;
|
|
3
|
+
exports.SQLiteServerSyncStorage = exports.MemoryServerSyncStorage = void 0;
|
|
4
4
|
exports.createSyncEndpoint = createSyncEndpoint;
|
|
5
5
|
/**
|
|
6
6
|
* In-memory server sync storage.
|
|
@@ -10,6 +10,10 @@ class MemoryServerSyncStorage {
|
|
|
10
10
|
tables = new Map();
|
|
11
11
|
changeLog = [];
|
|
12
12
|
monotonicCursor = 0;
|
|
13
|
+
maxChangeLogEntries;
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
this.maxChangeLogEntries = options.maxChangeLogEntries ?? 10000;
|
|
16
|
+
}
|
|
13
17
|
async applyOperations(operations, overrideTime) {
|
|
14
18
|
const serverTime = overrideTime ?? Date.now();
|
|
15
19
|
let applied = 0;
|
|
@@ -46,6 +50,11 @@ class MemoryServerSyncStorage {
|
|
|
46
50
|
applied += 1;
|
|
47
51
|
successfulIds.push(op.id);
|
|
48
52
|
}
|
|
53
|
+
if (Number.isFinite(this.maxChangeLogEntries) &&
|
|
54
|
+
this.maxChangeLogEntries > 0 &&
|
|
55
|
+
this.changeLog.length > this.maxChangeLogEntries) {
|
|
56
|
+
this.changeLog.splice(0, this.changeLog.length - this.maxChangeLogEntries);
|
|
57
|
+
}
|
|
49
58
|
return { applied, successfulIds, serverTime, cursor: this.monotonicCursor };
|
|
50
59
|
}
|
|
51
60
|
async getDeltas(tables, since, limit = 1000) {
|
|
@@ -63,8 +72,11 @@ class MemoryServerSyncStorage {
|
|
|
63
72
|
latestMap.set(key, entry);
|
|
64
73
|
if (entry.cursor > maxCursor)
|
|
65
74
|
maxCursor = entry.cursor;
|
|
66
|
-
|
|
75
|
+
// Only fall back to wall-clock timestamp if the client's cursor is ahead of
|
|
76
|
+
// this in-memory instance (e.g. after a server restart).
|
|
77
|
+
if (since > this.monotonicCursor && entry.timestamp > maxCursor) {
|
|
67
78
|
maxCursor = entry.timestamp;
|
|
79
|
+
}
|
|
68
80
|
}
|
|
69
81
|
const deltaList = Array.from(latestMap.values()).map((entry) => ({
|
|
70
82
|
tableName: entry.tableName,
|
|
@@ -93,6 +105,206 @@ class MemoryServerSyncStorage {
|
|
|
93
105
|
}
|
|
94
106
|
}
|
|
95
107
|
exports.MemoryServerSyncStorage = MemoryServerSyncStorage;
|
|
108
|
+
/**
|
|
109
|
+
* Persistent SQLite-backed ServerSyncStorage for production self-hosted sync servers.
|
|
110
|
+
* Works with any LocalDBAdapter (e.g. BetterSQLite3Adapter, NodeSQLiteAdapter, LibSQLAdapter).
|
|
111
|
+
*/
|
|
112
|
+
class SQLiteServerSyncStorage {
|
|
113
|
+
db;
|
|
114
|
+
initPromise;
|
|
115
|
+
maxChangeLogEntries;
|
|
116
|
+
constructor(db, options = {}) {
|
|
117
|
+
this.db = db;
|
|
118
|
+
this.maxChangeLogEntries = options.maxChangeLogEntries ?? 10000;
|
|
119
|
+
}
|
|
120
|
+
async ensureSchema() {
|
|
121
|
+
if (!this.initPromise) {
|
|
122
|
+
this.initPromise = (async () => {
|
|
123
|
+
await this.db.execute(`CREATE TABLE IF NOT EXISTS _sync_server_records (
|
|
124
|
+
table_name TEXT NOT NULL,
|
|
125
|
+
row_id TEXT NOT NULL,
|
|
126
|
+
payload TEXT NOT NULL,
|
|
127
|
+
timestamp INTEGER NOT NULL,
|
|
128
|
+
operation TEXT NOT NULL,
|
|
129
|
+
cursor INTEGER NOT NULL,
|
|
130
|
+
PRIMARY KEY (table_name, row_id)
|
|
131
|
+
);`);
|
|
132
|
+
await this.db.execute(`CREATE TABLE IF NOT EXISTS _sync_server_changelog (
|
|
133
|
+
cursor INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
134
|
+
table_name TEXT NOT NULL,
|
|
135
|
+
row_id TEXT NOT NULL,
|
|
136
|
+
payload TEXT NOT NULL,
|
|
137
|
+
timestamp INTEGER NOT NULL,
|
|
138
|
+
operation TEXT NOT NULL
|
|
139
|
+
);`);
|
|
140
|
+
await this.db.execute(`CREATE INDEX IF NOT EXISTS idx_sync_srv_log_table_cursor ON _sync_server_changelog (table_name, cursor);`);
|
|
141
|
+
})().catch((err) => {
|
|
142
|
+
this.initPromise = undefined;
|
|
143
|
+
throw err;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
await this.initPromise;
|
|
147
|
+
}
|
|
148
|
+
async applyOperations(operations, overrideTime) {
|
|
149
|
+
await this.ensureSchema();
|
|
150
|
+
const serverTime = overrideTime ?? Date.now();
|
|
151
|
+
let applied = 0;
|
|
152
|
+
const successfulIds = [];
|
|
153
|
+
let latestCursor = 0;
|
|
154
|
+
await this.db.transaction(async (tx) => {
|
|
155
|
+
for (const op of operations) {
|
|
156
|
+
if (!op.tableName || !op.rowId)
|
|
157
|
+
continue;
|
|
158
|
+
const rowId = String(op.rowId);
|
|
159
|
+
const existingRows = await tx.execute(`SELECT payload, timestamp FROM _sync_server_records WHERE table_name = ? AND row_id = ? LIMIT 1`, [op.tableName, rowId]);
|
|
160
|
+
const existing = existingRows?.[0];
|
|
161
|
+
const opTime = op.timestamp ?? serverTime;
|
|
162
|
+
if (existing && Number(existing.timestamp) > opTime) {
|
|
163
|
+
successfulIds.push(op.id);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
let existingPayload = {};
|
|
167
|
+
if (existing?.payload) {
|
|
168
|
+
try {
|
|
169
|
+
existingPayload =
|
|
170
|
+
typeof existing.payload === 'string'
|
|
171
|
+
? JSON.parse(existing.payload)
|
|
172
|
+
: existing.payload;
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
existingPayload = {};
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const isDelete = op.operation === 'DELETE';
|
|
179
|
+
const recordPayload = isDelete
|
|
180
|
+
? { id: op.rowId, deleted: true }
|
|
181
|
+
: { ...existingPayload, ...(op.payload || {}), id: op.rowId };
|
|
182
|
+
const serializedPayload = JSON.stringify(recordPayload);
|
|
183
|
+
const recordTimestamp = Math.max(opTime, serverTime);
|
|
184
|
+
const insertRes = await tx.execute(`INSERT INTO _sync_server_changelog (table_name, row_id, payload, timestamp, operation) VALUES (?, ?, ?, ?, ?)`, [
|
|
185
|
+
op.tableName,
|
|
186
|
+
rowId,
|
|
187
|
+
serializedPayload,
|
|
188
|
+
recordTimestamp,
|
|
189
|
+
op.operation,
|
|
190
|
+
]);
|
|
191
|
+
let cursor = Number(insertRes?.[0]?.lastInsertRowid ?? 0);
|
|
192
|
+
if (!cursor) {
|
|
193
|
+
const maxRows = await tx.execute(`SELECT MAX(cursor) AS cursor FROM _sync_server_changelog`);
|
|
194
|
+
cursor = Number(maxRows?.[0]?.cursor ?? 0);
|
|
195
|
+
}
|
|
196
|
+
await tx.execute(`INSERT INTO _sync_server_records (table_name, row_id, payload, timestamp, operation, cursor)
|
|
197
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
198
|
+
ON CONFLICT (table_name, row_id) DO UPDATE SET
|
|
199
|
+
payload = excluded.payload,
|
|
200
|
+
timestamp = excluded.timestamp,
|
|
201
|
+
operation = excluded.operation,
|
|
202
|
+
cursor = excluded.cursor`, [
|
|
203
|
+
op.tableName,
|
|
204
|
+
rowId,
|
|
205
|
+
serializedPayload,
|
|
206
|
+
recordTimestamp,
|
|
207
|
+
op.operation,
|
|
208
|
+
cursor,
|
|
209
|
+
]);
|
|
210
|
+
latestCursor = Math.max(latestCursor, cursor);
|
|
211
|
+
applied += 1;
|
|
212
|
+
successfulIds.push(op.id);
|
|
213
|
+
}
|
|
214
|
+
if (this.maxChangeLogEntries !== undefined &&
|
|
215
|
+
Number.isFinite(this.maxChangeLogEntries) &&
|
|
216
|
+
this.maxChangeLogEntries > 0 &&
|
|
217
|
+
latestCursor > this.maxChangeLogEntries) {
|
|
218
|
+
await tx.execute(`DELETE FROM _sync_server_changelog WHERE cursor <= ?`, [latestCursor - this.maxChangeLogEntries]);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
if (latestCursor === 0) {
|
|
222
|
+
const maxRows = await this.db.execute(`SELECT MAX(cursor) AS cursor FROM _sync_server_changelog`);
|
|
223
|
+
latestCursor = Number(maxRows?.[0]?.cursor ?? 0);
|
|
224
|
+
}
|
|
225
|
+
return { applied, successfulIds, serverTime, cursor: latestCursor };
|
|
226
|
+
}
|
|
227
|
+
async getDeltas(tables, since, limit = 1000) {
|
|
228
|
+
await this.ensureSchema();
|
|
229
|
+
const serverTime = Date.now();
|
|
230
|
+
const safeLimit = Math.max(1, Math.floor(limit));
|
|
231
|
+
const maxRows = await this.db.execute(`SELECT MAX(cursor) AS maxCursor FROM _sync_server_changelog`);
|
|
232
|
+
const currentMaxCursor = Number(maxRows?.[0]?.maxCursor ?? 0);
|
|
233
|
+
const useCursorFilter = since <= currentMaxCursor;
|
|
234
|
+
const params = [since];
|
|
235
|
+
let whereClause = useCursorFilter ? `cursor > ?` : `timestamp > ?`;
|
|
236
|
+
if (tables.length > 0) {
|
|
237
|
+
const placeholders = tables.map(() => '?').join(', ');
|
|
238
|
+
whereClause += ` AND table_name IN (${placeholders})`;
|
|
239
|
+
params.push(...tables);
|
|
240
|
+
}
|
|
241
|
+
params.push(safeLimit + 1);
|
|
242
|
+
const rows = await this.db.execute(`SELECT cursor, table_name, row_id, payload, timestamp, operation
|
|
243
|
+
FROM _sync_server_changelog
|
|
244
|
+
WHERE ${whereClause}
|
|
245
|
+
ORDER BY cursor ASC
|
|
246
|
+
LIMIT ?`, params);
|
|
247
|
+
const hasMore = rows.length > safeLimit;
|
|
248
|
+
const paged = hasMore ? rows.slice(0, safeLimit) : rows;
|
|
249
|
+
const latestMap = new Map();
|
|
250
|
+
let maxCursor = since;
|
|
251
|
+
for (const row of paged) {
|
|
252
|
+
let parsedPayload = {};
|
|
253
|
+
try {
|
|
254
|
+
parsedPayload =
|
|
255
|
+
typeof row.payload === 'string'
|
|
256
|
+
? JSON.parse(row.payload)
|
|
257
|
+
: (row.payload ?? {});
|
|
258
|
+
}
|
|
259
|
+
catch {
|
|
260
|
+
parsedPayload = {};
|
|
261
|
+
}
|
|
262
|
+
const key = `${row.table_name}:${row.row_id}`;
|
|
263
|
+
const ts = Number(row.timestamp);
|
|
264
|
+
const cur = Number(row.cursor);
|
|
265
|
+
latestMap.set(key, {
|
|
266
|
+
tableName: row.table_name,
|
|
267
|
+
operation: row.operation,
|
|
268
|
+
rowId: row.row_id,
|
|
269
|
+
payload: parsedPayload,
|
|
270
|
+
timestamp: ts,
|
|
271
|
+
});
|
|
272
|
+
if (useCursorFilter) {
|
|
273
|
+
if (cur > maxCursor)
|
|
274
|
+
maxCursor = cur;
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
if (ts > maxCursor)
|
|
278
|
+
maxCursor = ts;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
const deltaList = Array.from(latestMap.values());
|
|
282
|
+
return {
|
|
283
|
+
changes: {
|
|
284
|
+
timestamp: serverTime,
|
|
285
|
+
changes: deltaList,
|
|
286
|
+
hasMore,
|
|
287
|
+
},
|
|
288
|
+
cursor: maxCursor,
|
|
289
|
+
hasMore,
|
|
290
|
+
serverTime,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Compacts older changelog entries, retaining the most recent `retentionCount` rows.
|
|
295
|
+
*/
|
|
296
|
+
async compactChangeLog(retentionCount = 10000) {
|
|
297
|
+
await this.ensureSchema();
|
|
298
|
+
if (!Number.isFinite(retentionCount) || retentionCount < 0)
|
|
299
|
+
return;
|
|
300
|
+
const maxRows = await this.db.execute(`SELECT MAX(cursor) AS cursor FROM _sync_server_changelog`);
|
|
301
|
+
const maxCursor = Number(maxRows?.[0]?.cursor ?? 0);
|
|
302
|
+
if (maxCursor > retentionCount) {
|
|
303
|
+
await this.db.execute(`DELETE FROM _sync_server_changelog WHERE cursor <= ?`, [maxCursor - retentionCount]);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
exports.SQLiteServerSyncStorage = SQLiteServerSyncStorage;
|
|
96
308
|
/**
|
|
97
309
|
* Creates turnkey push and pull API endpoints for self-hosted multi-device data synchronization.
|
|
98
310
|
*/
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import type { SupabaseClient } from '@supabase/supabase-js';
|
|
2
1
|
import { BlobRecord, RemoteStorageUploader } from './types.js';
|
|
2
|
+
export interface SupabaseStorageClientLike {
|
|
3
|
+
storage?: {
|
|
4
|
+
from: (bucket: string) => any;
|
|
5
|
+
};
|
|
6
|
+
from?: (bucket: string) => any;
|
|
7
|
+
}
|
|
3
8
|
export interface SupabaseStorageUploaderOptions {
|
|
4
9
|
/**
|
|
5
10
|
* Supabase Storage bucket name (e.g. 'avatars', 'property-photos', 'documents').
|
|
@@ -41,4 +46,4 @@ export interface SupabaseStorageUploaderInstance extends RemoteStorageUploader {
|
|
|
41
46
|
/**
|
|
42
47
|
* Creates a RemoteStorageUploader that syncs binary files to Supabase Storage buckets.
|
|
43
48
|
*/
|
|
44
|
-
export declare function createSupabaseStorageUploader(supabase:
|
|
49
|
+
export declare function createSupabaseStorageUploader(supabase: SupabaseStorageClientLike | any, options: SupabaseStorageUploaderOptions): SupabaseStorageUploaderInstance;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "local-lite",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Never let a lost connection break a promise. Local-first sync that faithfully keeps data safe until you're back online.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -213,16 +213,15 @@
|
|
|
213
213
|
}
|
|
214
214
|
},
|
|
215
215
|
"devDependencies": {
|
|
216
|
-
"@
|
|
217
|
-
"@types/node": "^26.6.5",
|
|
216
|
+
"@types/node": "^26.6.6",
|
|
218
217
|
"@types/nodemailer": "^8.0.2",
|
|
219
218
|
"@types/react": "^19.3.0",
|
|
220
219
|
"@types/uuid": "^11.0.0",
|
|
221
|
-
"expo-sqlite": "^58.0.
|
|
220
|
+
"expo-sqlite": "^58.0.17",
|
|
222
221
|
"nodemailer": "^10.5.0",
|
|
223
222
|
"react": "^19.3.1",
|
|
224
223
|
"typescript": "^7.0.2",
|
|
225
|
-
"vitest": "^5.0
|
|
224
|
+
"vitest": "^5.1.0"
|
|
226
225
|
},
|
|
227
226
|
"dependencies": {
|
|
228
227
|
"@noble/hashes": "^2.4.0",
|