local-lite 1.0.3 → 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/README.md +4 -3
- 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 +6 -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/email/index.d.ts +17 -18
- package/dist/email/index.js +2 -56
- package/dist/react/index.js +54 -2
- package/dist/server/email.d.ts +8 -0
- package/dist/server/email.js +45 -1
- 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/email/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { Transporter, SendMailOptions, TransportOptions } from 'nodemailer';
|
|
2
1
|
import type { ActionHandlerContext, ActionRecord, EmailAttachment, EmailPayload } from '../actions/types.js';
|
|
3
2
|
export type EmailProviderType = 'nodemailer' | 'resend' | 'sendgrid' | 'postmark' | 'http' | 'dev' | 'preview' | 'custom';
|
|
4
3
|
export interface EmailHandlerOptions {
|
|
@@ -10,7 +9,7 @@ export interface EmailHandlerOptions {
|
|
|
10
9
|
/**
|
|
11
10
|
* Additional default options merged into every outgoing email.
|
|
12
11
|
*/
|
|
13
|
-
defaults?: Partial<
|
|
12
|
+
defaults?: Partial<EmailMailOptions> & Record<string, any>;
|
|
14
13
|
/**
|
|
15
14
|
* Custom template renderer invoked when `template` or `templateData` is provided in EmailPayload.
|
|
16
15
|
*/
|
|
@@ -53,7 +52,19 @@ export interface EmailDeliveryConfig extends EmailHandlerOptions {
|
|
|
53
52
|
silentDev?: boolean;
|
|
54
53
|
}
|
|
55
54
|
export type NodemailerHandlerOptions = EmailHandlerOptions;
|
|
56
|
-
export
|
|
55
|
+
export interface EmailMailOptions {
|
|
56
|
+
to?: any;
|
|
57
|
+
from?: any;
|
|
58
|
+
subject?: string;
|
|
59
|
+
text?: string;
|
|
60
|
+
html?: string;
|
|
61
|
+
cc?: any;
|
|
62
|
+
bcc?: any;
|
|
63
|
+
replyTo?: any;
|
|
64
|
+
headers?: Record<string, any>;
|
|
65
|
+
attachments?: any[];
|
|
66
|
+
[key: string]: any;
|
|
67
|
+
}
|
|
57
68
|
export interface EmailSendResult {
|
|
58
69
|
messageId: string;
|
|
59
70
|
response?: any;
|
|
@@ -66,7 +77,7 @@ export type ConcreteEmailHandler = (payload: EmailPayload, record: ActionRecord<
|
|
|
66
77
|
export interface DevSentEmail {
|
|
67
78
|
id: string;
|
|
68
79
|
sentAt: number;
|
|
69
|
-
mailOptions:
|
|
80
|
+
mailOptions: EmailMailOptions;
|
|
70
81
|
payload: EmailPayload;
|
|
71
82
|
record: ActionRecord<EmailPayload>;
|
|
72
83
|
}
|
|
@@ -80,14 +91,6 @@ export declare class DevMailbox {
|
|
|
80
91
|
static clear(): void;
|
|
81
92
|
static record(email: DevSentEmail): void;
|
|
82
93
|
}
|
|
83
|
-
/**
|
|
84
|
-
* Dynamically loads the nodemailer module if not already available.
|
|
85
|
-
*/
|
|
86
|
-
export declare function loadNodemailer(): Promise<any>;
|
|
87
|
-
/**
|
|
88
|
-
* Resolves or instantiates a Nodemailer Transporter instance.
|
|
89
|
-
*/
|
|
90
|
-
export declare function resolveTransporter(input: NodemailerTransportInput, defaults?: Partial<SendMailOptions>): Promise<Transporter>;
|
|
91
94
|
/**
|
|
92
95
|
* Renders a React element or JSX component into clean HTML and plaintext fallback.
|
|
93
96
|
*/
|
|
@@ -102,12 +105,8 @@ export declare function hydrateAttachment(att: EmailAttachment): Promise<any>;
|
|
|
102
105
|
/**
|
|
103
106
|
* Maps an offline Local-Lite EmailPayload to standard email options.
|
|
104
107
|
*/
|
|
105
|
-
export declare function buildMailOptions(payload: EmailPayload, options?: EmailHandlerOptions):
|
|
106
|
-
|
|
107
|
-
* Creates an EmailHandler for local-lite ActionManager that sends emails
|
|
108
|
-
* directly via Nodemailer (SMTP, Sendmail, SES, etc.).
|
|
109
|
-
*/
|
|
110
|
-
export declare function createNodemailerHandler(transporterOrOptions: NodemailerTransportInput, handlerOptions?: NodemailerHandlerOptions): ConcreteEmailHandler;
|
|
108
|
+
export declare function buildMailOptions(payload: EmailPayload, options?: EmailHandlerOptions): EmailMailOptions;
|
|
109
|
+
export declare function prepareMailOptions(payload: EmailPayload, options: EmailHandlerOptions, record: ActionRecord<EmailPayload>): Promise<EmailMailOptions>;
|
|
111
110
|
/**
|
|
112
111
|
* Creates an EmailHandler for local-lite ActionManager that dispatches emails
|
|
113
112
|
* via HTTP APIs (e.g. Resend, SendGrid, Postmark, or custom webhooks/endpoints).
|
package/dist/email/index.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DevMailbox = void 0;
|
|
4
|
-
exports.loadNodemailer = loadNodemailer;
|
|
5
|
-
exports.resolveTransporter = resolveTransporter;
|
|
6
4
|
exports.renderReactEmail = renderReactEmail;
|
|
7
5
|
exports.hydrateAttachment = hydrateAttachment;
|
|
8
6
|
exports.buildMailOptions = buildMailOptions;
|
|
9
|
-
exports.
|
|
7
|
+
exports.prepareMailOptions = prepareMailOptions;
|
|
10
8
|
exports.createHttpEmailHandler = createHttpEmailHandler;
|
|
11
9
|
exports.createDevEmailHandler = createDevEmailHandler;
|
|
12
10
|
exports.createEmailHandler = createEmailHandler;
|
|
@@ -30,28 +28,6 @@ class DevMailbox {
|
|
|
30
28
|
}
|
|
31
29
|
}
|
|
32
30
|
exports.DevMailbox = DevMailbox;
|
|
33
|
-
/**
|
|
34
|
-
* Dynamically loads the nodemailer module if not already available.
|
|
35
|
-
*/
|
|
36
|
-
async function loadNodemailer() {
|
|
37
|
-
try {
|
|
38
|
-
const mod = await import('nodemailer');
|
|
39
|
-
return mod.default || mod;
|
|
40
|
-
}
|
|
41
|
-
catch (err) {
|
|
42
|
-
throw new Error("The 'nodemailer' package is required to send emails via Nodemailer transport. Please install it via 'npm install nodemailer'.");
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Resolves or instantiates a Nodemailer Transporter instance.
|
|
47
|
-
*/
|
|
48
|
-
async function resolveTransporter(input, defaults) {
|
|
49
|
-
if (input && typeof input.sendMail === 'function') {
|
|
50
|
-
return input;
|
|
51
|
-
}
|
|
52
|
-
const nm = await loadNodemailer();
|
|
53
|
-
return nm.createTransport(input, defaults);
|
|
54
|
-
}
|
|
55
31
|
function interpolate(templateStr, data) {
|
|
56
32
|
return templateStr.replace(/\{\{\s*([\w.]+)\s*\}\}/g, (_, key) => {
|
|
57
33
|
const val = data[key];
|
|
@@ -216,36 +192,6 @@ async function prepareMailOptions(payload, options, record) {
|
|
|
216
192
|
}
|
|
217
193
|
return mailOptions;
|
|
218
194
|
}
|
|
219
|
-
/**
|
|
220
|
-
* Creates an EmailHandler for local-lite ActionManager that sends emails
|
|
221
|
-
* directly via Nodemailer (SMTP, Sendmail, SES, etc.).
|
|
222
|
-
*/
|
|
223
|
-
function createNodemailerHandler(transporterOrOptions, handlerOptions = {}) {
|
|
224
|
-
let cachedTransporter = transporterOrOptions &&
|
|
225
|
-
typeof transporterOrOptions.sendMail === 'function'
|
|
226
|
-
? transporterOrOptions
|
|
227
|
-
: null;
|
|
228
|
-
const getTransporter = async () => {
|
|
229
|
-
if (!cachedTransporter) {
|
|
230
|
-
cachedTransporter = await resolveTransporter(transporterOrOptions, handlerOptions.defaults);
|
|
231
|
-
}
|
|
232
|
-
return cachedTransporter;
|
|
233
|
-
};
|
|
234
|
-
return async (payload, record, _context) => {
|
|
235
|
-
const transporter = await getTransporter();
|
|
236
|
-
const mailOptions = await prepareMailOptions(payload, handlerOptions, record);
|
|
237
|
-
const info = await transporter.sendMail(mailOptions);
|
|
238
|
-
if (handlerOptions.afterSend) {
|
|
239
|
-
await handlerOptions.afterSend(info, payload, record);
|
|
240
|
-
}
|
|
241
|
-
return {
|
|
242
|
-
messageId: info.messageId,
|
|
243
|
-
response: info.response,
|
|
244
|
-
accepted: info.accepted,
|
|
245
|
-
rejected: info.rejected,
|
|
246
|
-
};
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
195
|
/**
|
|
250
196
|
* Creates an EmailHandler for local-lite ActionManager that dispatches emails
|
|
251
197
|
* via HTTP APIs (e.g. Resend, SendGrid, Postmark, or custom webhooks/endpoints).
|
|
@@ -440,7 +386,7 @@ function createEmailHandler(config) {
|
|
|
440
386
|
return createDevEmailHandler({ ...config, silent: config.silentDev });
|
|
441
387
|
}
|
|
442
388
|
if (config.transport || config.provider === 'nodemailer') {
|
|
443
|
-
|
|
389
|
+
throw new Error("Nodemailer email delivery is server-only. Import 'local-lite/server' for SMTP delivery.");
|
|
444
390
|
}
|
|
445
391
|
if (config.provider === 'resend' ||
|
|
446
392
|
config.provider === 'sendgrid' ||
|
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/email.d.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
import type { ApiEndpoint } from './index.js';
|
|
2
|
+
import { type ConcreteEmailHandler, type EmailHandlerOptions, type EmailMailOptions } from '../email/index.js';
|
|
3
|
+
export type NodemailerTransportInput = {
|
|
4
|
+
sendMail?: (options: EmailMailOptions) => Promise<any>;
|
|
5
|
+
[key: string]: any;
|
|
6
|
+
} | string;
|
|
7
|
+
export declare function loadNodemailer(): Promise<any>;
|
|
8
|
+
export declare function resolveTransporter(input: NodemailerTransportInput, defaults?: Partial<EmailMailOptions>): Promise<any>;
|
|
9
|
+
export declare function createNodemailerHandler(transporterOrOptions: NodemailerTransportInput, handlerOptions?: EmailHandlerOptions): ConcreteEmailHandler;
|
|
2
10
|
export interface EmailEndpointOptions {
|
|
3
11
|
/**
|
|
4
12
|
* Secret token required in `Authorization: Bearer <secret>` or `x-app-secret` header.
|
package/dist/server/email.js
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadNodemailer = loadNodemailer;
|
|
4
|
+
exports.resolveTransporter = resolveTransporter;
|
|
5
|
+
exports.createNodemailerHandler = createNodemailerHandler;
|
|
3
6
|
exports.createEmailEndpoint = createEmailEndpoint;
|
|
7
|
+
const index_js_1 = require("../email/index.js");
|
|
8
|
+
async function loadNodemailer() {
|
|
9
|
+
try {
|
|
10
|
+
const mod = await import('nodemailer');
|
|
11
|
+
return mod.default || mod;
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
throw new Error("The 'nodemailer' package is required to send emails via Nodemailer transport. Please install it via 'npm install nodemailer'.");
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
async function resolveTransporter(input, defaults) {
|
|
18
|
+
if (input && typeof input !== 'string' && typeof input.sendMail === 'function') {
|
|
19
|
+
return input;
|
|
20
|
+
}
|
|
21
|
+
const nodemailer = await loadNodemailer();
|
|
22
|
+
return nodemailer.createTransport(input, defaults);
|
|
23
|
+
}
|
|
24
|
+
function createNodemailerHandler(transporterOrOptions, handlerOptions = {}) {
|
|
25
|
+
let cachedTransporter = transporterOrOptions &&
|
|
26
|
+
typeof transporterOrOptions !== 'string' &&
|
|
27
|
+
typeof transporterOrOptions.sendMail === 'function'
|
|
28
|
+
? transporterOrOptions
|
|
29
|
+
: null;
|
|
30
|
+
const getTransporter = async () => {
|
|
31
|
+
if (!cachedTransporter) {
|
|
32
|
+
cachedTransporter = await resolveTransporter(transporterOrOptions, handlerOptions.defaults);
|
|
33
|
+
}
|
|
34
|
+
return cachedTransporter;
|
|
35
|
+
};
|
|
36
|
+
return async (payload, record, _context) => {
|
|
37
|
+
const transporter = await getTransporter();
|
|
38
|
+
const mailOptions = await (0, index_js_1.prepareMailOptions)(payload, handlerOptions, record);
|
|
39
|
+
const info = await transporter.sendMail(mailOptions);
|
|
40
|
+
await handlerOptions.afterSend?.(info, payload, record);
|
|
41
|
+
return {
|
|
42
|
+
messageId: info.messageId,
|
|
43
|
+
response: info.response,
|
|
44
|
+
accepted: info.accepted,
|
|
45
|
+
rejected: info.rejected,
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
}
|
|
4
49
|
/**
|
|
5
50
|
* Creates a turnkey Request/Response API endpoint for receiving offline-enqueued emails
|
|
6
51
|
* from local-lite clients and dispatching them via Nodemailer or cloud providers.
|
|
@@ -47,7 +92,6 @@ function createEmailEndpoint(options = {}) {
|
|
|
47
92
|
result = await options.dispatch(payload, request);
|
|
48
93
|
}
|
|
49
94
|
else if (options.transport || (!options.provider && !options.apiKey)) {
|
|
50
|
-
const { createNodemailerHandler } = await import('../email/index.js');
|
|
51
95
|
const handler = createNodemailerHandler(options.transport || {}, {
|
|
52
96
|
defaultFrom: options.defaultFrom,
|
|
53
97
|
});
|
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`.
|