local-lite 1.0.4 → 2.0.1
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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/payments/adapters.d.ts +49 -0
- package/dist/payments/adapters.js +459 -0
- package/dist/payments/index.d.ts +3 -0
- package/dist/payments/index.js +19 -0
- package/dist/payments/registry.d.ts +18 -0
- package/dist/payments/registry.js +114 -0
- package/dist/payments/server.d.ts +19 -0
- package/dist/payments/server.js +235 -0
- package/dist/payments/types.d.ts +106 -0
- package/dist/payments/types.js +2 -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 +14 -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/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -44,3 +44,4 @@ __exportStar(require("./kv/index.js"), exports);
|
|
|
44
44
|
__exportStar(require("./search/index.js"), exports);
|
|
45
45
|
__exportStar(require("./storage/index.js"), exports);
|
|
46
46
|
__exportStar(require("./scheduler/index.js"), exports);
|
|
47
|
+
__exportStar(require("./payments/index.js"), exports);
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { PaymentProviderAdapter } from './types.js';
|
|
2
|
+
export declare function formatKenyanPhone(phone: string | number): string;
|
|
3
|
+
export interface ManualPaymentAdapterOptions {
|
|
4
|
+
id?: string;
|
|
5
|
+
label?: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
supportedCurrencies?: string[] | '*';
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Offline-capable manual payment adapter (cash, bank transfer, cheque, etc.).
|
|
11
|
+
*/
|
|
12
|
+
export declare function createManualPaymentAdapter(options?: ManualPaymentAdapterOptions): PaymentProviderAdapter;
|
|
13
|
+
export interface DarajaAdapterOptions {
|
|
14
|
+
consumerKey: string;
|
|
15
|
+
consumerSecret: string;
|
|
16
|
+
shortcode: string;
|
|
17
|
+
passkey: string;
|
|
18
|
+
callbackUrl: string;
|
|
19
|
+
env?: 'sandbox' | 'live' | 'production';
|
|
20
|
+
apiUrl?: string;
|
|
21
|
+
partyB?: string;
|
|
22
|
+
transactionType?: string;
|
|
23
|
+
fetch?: typeof fetch;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Safaricom M-Pesa Daraja STK Push adapter (Kenya / KES).
|
|
27
|
+
*/
|
|
28
|
+
export declare function createDarajaAdapter(options: DarajaAdapterOptions): PaymentProviderAdapter;
|
|
29
|
+
export interface PaystackAdapterOptions {
|
|
30
|
+
secretKey: string;
|
|
31
|
+
supportedCurrencies?: string[];
|
|
32
|
+
supportedCountries?: string[] | '*';
|
|
33
|
+
fetch?: typeof fetch;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Paystack Checkout adapter (NGN, GHS, ZAR, KES, USD, etc.).
|
|
37
|
+
*/
|
|
38
|
+
export declare function createPaystackAdapter(options: PaystackAdapterOptions): PaymentProviderAdapter;
|
|
39
|
+
export interface StripeCheckoutAdapterOptions {
|
|
40
|
+
secretKey: string;
|
|
41
|
+
webhookSecret?: string;
|
|
42
|
+
supportedCurrencies?: string[] | '*';
|
|
43
|
+
supportedCountries?: string[] | '*';
|
|
44
|
+
fetch?: typeof fetch;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Stripe Checkout Session adapter for global card/wallet/bank payments.
|
|
48
|
+
*/
|
|
49
|
+
export declare function createStripeCheckoutAdapter(options: StripeCheckoutAdapterOptions): PaymentProviderAdapter;
|