local-lite 0.0.11 → 0.0.14
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 +49 -3
- package/dist/auth/apple.d.ts +91 -0
- package/dist/auth/apple.js +264 -0
- package/dist/auth/github.d.ts +72 -0
- package/dist/auth/github.js +284 -0
- package/dist/auth/google.d.ts +75 -0
- package/dist/auth/google.js +323 -0
- package/dist/auth/index.d.ts +4 -0
- package/dist/auth/index.js +20 -0
- package/dist/auth/microsoft.d.ts +76 -0
- package/dist/auth/microsoft.js +310 -0
- package/dist/backups/encryption.d.ts +18 -0
- package/dist/backups/encryption.js +122 -0
- package/dist/backups/index.d.ts +2 -0
- package/dist/backups/index.js +11 -1
- package/dist/backups/onedrive.d.ts +22 -0
- package/dist/backups/onedrive.js +114 -0
- package/dist/core/client.d.ts +2 -0
- package/dist/core/client.js +3 -0
- package/dist/core/conflict.d.ts +8 -1
- package/dist/core/conflict.js +75 -0
- package/dist/core/maintenance.d.ts +66 -0
- package/dist/core/maintenance.js +157 -0
- package/dist/core/sync-worker.js +1 -1
- package/dist/core/types.d.ts +34 -4
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/react/auth.d.ts +46 -0
- package/dist/react/auth.js +365 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +1 -0
- package/dist/utils/portability.d.ts +73 -0
- package/dist/utils/portability.js +276 -0
- package/package.json +6 -1
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OneDriveBackupClient = exports.OneDriveBackupStorage = exports.MICROSOFT_GRAPH_DRIVE = void 0;
|
|
4
|
+
const microsoft_js_1 = require("../auth/microsoft.js");
|
|
5
|
+
exports.MICROSOFT_GRAPH_DRIVE = 'https://graph.microsoft.com/v1.0/me/drive';
|
|
6
|
+
const BACKUP_FOLDER = 'backups';
|
|
7
|
+
class OneDriveBackupStorage {
|
|
8
|
+
getAccessToken;
|
|
9
|
+
fetcher;
|
|
10
|
+
constructor(getAccessToken, fetcher = fetch) {
|
|
11
|
+
this.getAccessToken = getAccessToken;
|
|
12
|
+
this.fetcher = fetcher;
|
|
13
|
+
}
|
|
14
|
+
async save(fileName, data) {
|
|
15
|
+
const token = await this.getAccessToken();
|
|
16
|
+
const encodedName = encodeURIComponent(fileName);
|
|
17
|
+
const url = `${exports.MICROSOFT_GRAPH_DRIVE}/special/approot:/${BACKUP_FOLDER}/${encodedName}:/content`;
|
|
18
|
+
const response = await this.fetcher(url, {
|
|
19
|
+
method: 'PUT',
|
|
20
|
+
headers: {
|
|
21
|
+
Authorization: `Bearer ${token}`,
|
|
22
|
+
'Content-Type': 'application/octet-stream',
|
|
23
|
+
},
|
|
24
|
+
body: data.slice().buffer,
|
|
25
|
+
});
|
|
26
|
+
if (!response.ok) {
|
|
27
|
+
let detail = '';
|
|
28
|
+
try {
|
|
29
|
+
const body = (await response.json());
|
|
30
|
+
detail = body?.error?.message || response.statusText;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
detail = response.statusText;
|
|
34
|
+
}
|
|
35
|
+
throw new Error(`OneDrive upload failed (${response.status}): ${detail}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async read(fileIdOrName) {
|
|
39
|
+
const token = await this.getAccessToken();
|
|
40
|
+
let url = `${exports.MICROSOFT_GRAPH_DRIVE}/items/${encodeURIComponent(fileIdOrName)}/content`;
|
|
41
|
+
// Support reading directly by filename inside approot:/backups/
|
|
42
|
+
if (fileIdOrName.endsWith('.db') && !fileIdOrName.startsWith('item-')) {
|
|
43
|
+
url = `${exports.MICROSOFT_GRAPH_DRIVE}/special/approot:/${BACKUP_FOLDER}/${encodeURIComponent(fileIdOrName)}:/content`;
|
|
44
|
+
}
|
|
45
|
+
const response = await this.fetcher(url, {
|
|
46
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
47
|
+
});
|
|
48
|
+
if (!response.ok) {
|
|
49
|
+
throw new Error(`OneDrive download failed (${response.status}): ${response.statusText}`);
|
|
50
|
+
}
|
|
51
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
52
|
+
}
|
|
53
|
+
async list() {
|
|
54
|
+
const token = await this.getAccessToken();
|
|
55
|
+
const url = `${exports.MICROSOFT_GRAPH_DRIVE}/special/approot:/${BACKUP_FOLDER}:/children?$select=id,name,size,lastModifiedDateTime`;
|
|
56
|
+
const response = await this.fetcher(url, {
|
|
57
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
58
|
+
});
|
|
59
|
+
if (response.status === 404) {
|
|
60
|
+
return []; // Folder does not exist yet
|
|
61
|
+
}
|
|
62
|
+
if (!response.ok) {
|
|
63
|
+
throw new Error(`OneDrive list failed (${response.status}): ${response.statusText}`);
|
|
64
|
+
}
|
|
65
|
+
const result = (await response.json());
|
|
66
|
+
return (result.value ?? [])
|
|
67
|
+
.filter(item => item.name.endsWith('.db'))
|
|
68
|
+
.map(item => ({
|
|
69
|
+
id: item.id,
|
|
70
|
+
name: item.name,
|
|
71
|
+
size: item.size,
|
|
72
|
+
modifiedAt: item.lastModifiedDateTime,
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.OneDriveBackupStorage = OneDriveBackupStorage;
|
|
77
|
+
class OneDriveBackupClient {
|
|
78
|
+
storage;
|
|
79
|
+
authClient;
|
|
80
|
+
constructor(options) {
|
|
81
|
+
const scopes = options.scopes ?? [
|
|
82
|
+
'openid',
|
|
83
|
+
'profile',
|
|
84
|
+
'email',
|
|
85
|
+
'User.Read',
|
|
86
|
+
'Files.ReadWrite.AppFolder',
|
|
87
|
+
];
|
|
88
|
+
this.authClient = new microsoft_js_1.MicrosoftAuthClient({
|
|
89
|
+
...options,
|
|
90
|
+
scopes,
|
|
91
|
+
});
|
|
92
|
+
this.storage = new OneDriveBackupStorage(() => this.authClient.getValidAccessToken(), options.fetch ?? fetch);
|
|
93
|
+
}
|
|
94
|
+
async connect() {
|
|
95
|
+
const user = await this.authClient.signIn();
|
|
96
|
+
return user !== null;
|
|
97
|
+
}
|
|
98
|
+
async disconnect() {
|
|
99
|
+
await this.authClient.signOut();
|
|
100
|
+
}
|
|
101
|
+
async isConnected() {
|
|
102
|
+
return this.authClient.isSignedIn();
|
|
103
|
+
}
|
|
104
|
+
save(fileName, data) {
|
|
105
|
+
return this.storage.save(fileName, data);
|
|
106
|
+
}
|
|
107
|
+
read(fileId) {
|
|
108
|
+
return this.storage.read(fileId);
|
|
109
|
+
}
|
|
110
|
+
list() {
|
|
111
|
+
return this.storage.list();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.OneDriveBackupClient = OneDriveBackupClient;
|
package/dist/core/client.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { LocalDBAdapter, LocalTransaction } from '../adapters/local/index.js';
|
|
|
2
2
|
import { RemoteDBAdapter } from '../adapters/remote/index.js';
|
|
3
3
|
import { EventEmitter } from '../reactive/event-emitter.js';
|
|
4
4
|
import { SyncWorker } from './sync-worker.js';
|
|
5
|
+
import { DatabaseMaintenance } from './maintenance.js';
|
|
5
6
|
import { SchemaManager } from './schema-manager.js';
|
|
6
7
|
import { BatchMutation, ConflictResolver, ConflictStrategy, MutateOptions, NetworkStatusProvider, OutboxRecord, OutboxStatus, QueryOptions, RetryConfig, SyncMetrics, SyncOp, TableChangeEvent, WhereFilter } from './types.js';
|
|
7
8
|
import { SyncLogger, CustomLogger, LogLevel } from '../utils/logger.js';
|
|
@@ -78,6 +79,7 @@ export declare class LocalFirstClient {
|
|
|
78
79
|
autoHydrateJSON: boolean;
|
|
79
80
|
/** Local column stamped on INSERT/UPDATE writes, or null when disabled. */
|
|
80
81
|
touchColumn: string | null;
|
|
82
|
+
readonly maintenance: DatabaseMaintenance;
|
|
81
83
|
private primaryKey;
|
|
82
84
|
private migrations;
|
|
83
85
|
private autoStart;
|
package/dist/core/client.js
CHANGED
|
@@ -6,6 +6,7 @@ const sync_worker_js_1 = require("./sync-worker.js");
|
|
|
6
6
|
const table_manager_js_1 = require("./table-manager.js");
|
|
7
7
|
const outbox_store_js_1 = require("./outbox-store.js");
|
|
8
8
|
const meta_store_js_1 = require("./meta-store.js");
|
|
9
|
+
const maintenance_js_1 = require("./maintenance.js");
|
|
9
10
|
const schema_manager_js_1 = require("./schema-manager.js");
|
|
10
11
|
const uuid_js_1 = require("../utils/uuid.js");
|
|
11
12
|
const sql_js_1 = require("../utils/sql.js");
|
|
@@ -156,6 +157,7 @@ class LocalFirstClient {
|
|
|
156
157
|
autoHydrateJSON;
|
|
157
158
|
/** Local column stamped on INSERT/UPDATE writes, or null when disabled. */
|
|
158
159
|
touchColumn;
|
|
160
|
+
maintenance;
|
|
159
161
|
primaryKey;
|
|
160
162
|
migrations;
|
|
161
163
|
autoStart;
|
|
@@ -180,6 +182,7 @@ class LocalFirstClient {
|
|
|
180
182
|
? touch
|
|
181
183
|
: this.remote?.updatedAtColumn || 'updated_at';
|
|
182
184
|
this.schemaManager = new schema_manager_js_1.SchemaManager();
|
|
185
|
+
this.maintenance = new maintenance_js_1.DatabaseMaintenance(this.db, () => this.tables);
|
|
183
186
|
const workerOptions = {
|
|
184
187
|
network: config.network,
|
|
185
188
|
conflictResolver: config.conflictResolver,
|
package/dist/core/conflict.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import { ConflictResolver, ConflictStrategy } from './types.js';
|
|
1
|
+
import { ConflictContext, ConflictResolution, ConflictResolver, ConflictStrategy, FieldMergeResolverOptions } from './types.js';
|
|
2
|
+
export type { ConflictContext, ConflictResolution, ConflictResolver, ConflictStrategy, FieldMergeResolverOptions, };
|
|
2
3
|
export declare const lastWriteWinsResolver: ConflictResolver;
|
|
3
4
|
export declare const serverWinsResolver: ConflictResolver;
|
|
4
5
|
export declare const clientWinsResolver: ConflictResolver;
|
|
6
|
+
/**
|
|
7
|
+
* Creates a smart 3-way field-level conflict resolver.
|
|
8
|
+
* Merges disjoint fields from both local and remote offline mutations,
|
|
9
|
+
* and resolves overlapping field changes using custom rules, timestamps, or winner strategies.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createFieldMergeResolver(options?: FieldMergeResolverOptions): ConflictResolver;
|
|
5
12
|
export declare function getConflictResolver(strategyOrResolver?: ConflictStrategy | ConflictResolver): ConflictResolver;
|
package/dist/core/conflict.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.clientWinsResolver = exports.serverWinsResolver = exports.lastWriteWinsResolver = void 0;
|
|
4
|
+
exports.createFieldMergeResolver = createFieldMergeResolver;
|
|
4
5
|
exports.getConflictResolver = getConflictResolver;
|
|
5
6
|
const lastWriteWinsResolver = (context) => {
|
|
6
7
|
const remoteTime = context.remoteChange.timestamp ?? 0;
|
|
@@ -22,6 +23,78 @@ const clientWinsResolver = (context) => {
|
|
|
22
23
|
return 'apply-remote';
|
|
23
24
|
};
|
|
24
25
|
exports.clientWinsResolver = clientWinsResolver;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a smart 3-way field-level conflict resolver.
|
|
28
|
+
* Merges disjoint fields from both local and remote offline mutations,
|
|
29
|
+
* and resolves overlapping field changes using custom rules, timestamps, or winner strategies.
|
|
30
|
+
*/
|
|
31
|
+
function createFieldMergeResolver(options = {}) {
|
|
32
|
+
const defaultWinner = options.defaultWinner ?? 'newer';
|
|
33
|
+
const deleteHandling = options.deleteHandling ?? 'delete-wins';
|
|
34
|
+
const fieldRules = options.fieldRules ?? {};
|
|
35
|
+
return (context) => {
|
|
36
|
+
const isRemoteDelete = context.remoteChange.operation === 'DELETE';
|
|
37
|
+
const isLocalDelete = context.localOutboxRecord?.operation === 'DELETE';
|
|
38
|
+
if (isRemoteDelete && isLocalDelete) {
|
|
39
|
+
return 'apply-remote';
|
|
40
|
+
}
|
|
41
|
+
if (isRemoteDelete && !isLocalDelete) {
|
|
42
|
+
return deleteHandling === 'update-wins' ? 'keep-local' : 'apply-remote';
|
|
43
|
+
}
|
|
44
|
+
if (isLocalDelete && !isRemoteDelete) {
|
|
45
|
+
return deleteHandling === 'update-wins' ? 'apply-remote' : 'keep-local';
|
|
46
|
+
}
|
|
47
|
+
// Both are non-delete operations (INSERT / UPDATE)
|
|
48
|
+
const baseRow = context.localRow ?? {};
|
|
49
|
+
const remotePayload = context.remoteChange.payload ?? {};
|
|
50
|
+
const localPayload = context.localOutboxRecord?.payload ?? {};
|
|
51
|
+
const merged = { ...baseRow, ...remotePayload };
|
|
52
|
+
const tableRules = fieldRules[context.tableName] ?? {};
|
|
53
|
+
const wildcardRules = fieldRules['*'] ?? {};
|
|
54
|
+
const localTime = context.localOutboxRecord?.timestamp ?? 0;
|
|
55
|
+
const remoteTime = context.remoteChange.timestamp ?? 0;
|
|
56
|
+
for (const key of Object.keys(localPayload)) {
|
|
57
|
+
if (!(key in remotePayload)) {
|
|
58
|
+
// Field only changed locally: preserve local modification
|
|
59
|
+
merged[key] = localPayload[key];
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
// Field was modified by BOTH local and remote: resolve conflict
|
|
63
|
+
const rule = tableRules[key] ?? wildcardRules[key];
|
|
64
|
+
const localVal = localPayload[key];
|
|
65
|
+
const remoteVal = remotePayload[key];
|
|
66
|
+
if (typeof rule === 'function') {
|
|
67
|
+
merged[key] = rule(localVal, remoteVal, context);
|
|
68
|
+
}
|
|
69
|
+
else if (rule === 'max') {
|
|
70
|
+
merged[key] = localVal >= remoteVal ? localVal : remoteVal;
|
|
71
|
+
}
|
|
72
|
+
else if (rule === 'min') {
|
|
73
|
+
merged[key] = localVal <= remoteVal ? localVal : remoteVal;
|
|
74
|
+
}
|
|
75
|
+
else if (rule === 'local') {
|
|
76
|
+
merged[key] = localVal;
|
|
77
|
+
}
|
|
78
|
+
else if (rule === 'remote') {
|
|
79
|
+
merged[key] = remoteVal;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
const winner = rule === 'newer' ? 'newer' : defaultWinner;
|
|
83
|
+
if (winner === 'local') {
|
|
84
|
+
merged[key] = localVal;
|
|
85
|
+
}
|
|
86
|
+
else if (winner === 'remote') {
|
|
87
|
+
merged[key] = remoteVal;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
// 'newer': compare timestamp
|
|
91
|
+
merged[key] = localTime >= remoteTime ? localVal : remoteVal;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return { mergePayload: merged };
|
|
96
|
+
};
|
|
97
|
+
}
|
|
25
98
|
function getConflictResolver(strategyOrResolver) {
|
|
26
99
|
if (typeof strategyOrResolver === 'function') {
|
|
27
100
|
return strategyOrResolver;
|
|
@@ -31,6 +104,8 @@ function getConflictResolver(strategyOrResolver) {
|
|
|
31
104
|
return exports.serverWinsResolver;
|
|
32
105
|
case 'client-wins':
|
|
33
106
|
return exports.clientWinsResolver;
|
|
107
|
+
case 'field-merge':
|
|
108
|
+
return createFieldMergeResolver();
|
|
34
109
|
case 'last-write-wins':
|
|
35
110
|
default:
|
|
36
111
|
return exports.lastWriteWinsResolver;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { LocalDBAdapter } from '../adapters/local/index.js';
|
|
2
|
+
import { OutboxStatus } from './types.js';
|
|
3
|
+
export interface PruneOutboxOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Only delete outbox records older than this many days.
|
|
6
|
+
* e.g. retentionDays: 7 deletes records older than 7 days.
|
|
7
|
+
*/
|
|
8
|
+
retentionDays?: number;
|
|
9
|
+
/**
|
|
10
|
+
* Explicit cutoff timestamp in milliseconds. Records older than this timestamp are eligible.
|
|
11
|
+
*/
|
|
12
|
+
beforeTimestamp?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Maximum number of outbox entries to retain. If the outbox exceeds this count,
|
|
15
|
+
* the oldest matching records are pruned.
|
|
16
|
+
*/
|
|
17
|
+
maxEntries?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Target statuses to prune. Defaults to ['COMPLETED', 'FAILED'].
|
|
20
|
+
* PENDING records are protected from pruning by default unless explicitly included.
|
|
21
|
+
*/
|
|
22
|
+
statuses?: OutboxStatus[];
|
|
23
|
+
}
|
|
24
|
+
export interface OutboxStorageStats {
|
|
25
|
+
total: number;
|
|
26
|
+
pending: number;
|
|
27
|
+
processing: number;
|
|
28
|
+
completed: number;
|
|
29
|
+
failed: number;
|
|
30
|
+
}
|
|
31
|
+
export interface StorageStats {
|
|
32
|
+
/** Row count per table */
|
|
33
|
+
tables: Record<string, number>;
|
|
34
|
+
/** Breakdown of sync outbox items */
|
|
35
|
+
outbox: OutboxStorageStats;
|
|
36
|
+
/** SQLite allocation page size in bytes */
|
|
37
|
+
pageSize: number;
|
|
38
|
+
/** Total allocated SQLite database pages */
|
|
39
|
+
pageCount: number;
|
|
40
|
+
/** Total calculated database size on disk in bytes */
|
|
41
|
+
databaseSizeBytes: number;
|
|
42
|
+
}
|
|
43
|
+
export type CheckpointMode = 'PASSIVE' | 'FULL' | 'RESTART' | 'TRUNCATE';
|
|
44
|
+
export declare class DatabaseMaintenance {
|
|
45
|
+
private readonly db;
|
|
46
|
+
private readonly getKnownTables;
|
|
47
|
+
constructor(db: LocalDBAdapter, getKnownTables?: () => string[]);
|
|
48
|
+
/**
|
|
49
|
+
* Reclaims unused space, defragments the database file, and optimizes internal B-trees.
|
|
50
|
+
*/
|
|
51
|
+
vacuum(): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Checkpoints WAL (Write-Ahead Log) pages into the main database file.
|
|
54
|
+
* 'TRUNCATE' (default) copies all WAL frames to the database file and resets WAL size to zero.
|
|
55
|
+
*/
|
|
56
|
+
checkpoint(mode?: CheckpointMode): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Prunes old or completed mutations from the sync_outbox table to keep database footprint small.
|
|
59
|
+
* Returns the count of deleted outbox records.
|
|
60
|
+
*/
|
|
61
|
+
pruneOutbox(options?: PruneOutboxOptions): Promise<number>;
|
|
62
|
+
/**
|
|
63
|
+
* Gathers storage metrics: table row counts, outbox status distribution, and database size.
|
|
64
|
+
*/
|
|
65
|
+
getStorageStats(): Promise<StorageStats>;
|
|
66
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DatabaseMaintenance = void 0;
|
|
4
|
+
const sql_js_1 = require("../utils/sql.js");
|
|
5
|
+
class DatabaseMaintenance {
|
|
6
|
+
db;
|
|
7
|
+
getKnownTables;
|
|
8
|
+
constructor(db, getKnownTables = () => []) {
|
|
9
|
+
this.db = db;
|
|
10
|
+
this.getKnownTables = getKnownTables;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Reclaims unused space, defragments the database file, and optimizes internal B-trees.
|
|
14
|
+
*/
|
|
15
|
+
async vacuum() {
|
|
16
|
+
await this.db.execute('VACUUM');
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Checkpoints WAL (Write-Ahead Log) pages into the main database file.
|
|
20
|
+
* 'TRUNCATE' (default) copies all WAL frames to the database file and resets WAL size to zero.
|
|
21
|
+
*/
|
|
22
|
+
async checkpoint(mode = 'TRUNCATE') {
|
|
23
|
+
await this.db.execute(`PRAGMA wal_checkpoint(${mode})`);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Prunes old or completed mutations from the sync_outbox table to keep database footprint small.
|
|
27
|
+
* Returns the count of deleted outbox records.
|
|
28
|
+
*/
|
|
29
|
+
async pruneOutbox(options = {}) {
|
|
30
|
+
const statuses = options.statuses ?? ['COMPLETED', 'FAILED'];
|
|
31
|
+
if (statuses.length === 0)
|
|
32
|
+
return 0;
|
|
33
|
+
let cutoffTimestamp = options.beforeTimestamp;
|
|
34
|
+
if (options.retentionDays !== undefined && options.retentionDays >= 0) {
|
|
35
|
+
const daysMs = options.retentionDays * 24 * 60 * 60 * 1000;
|
|
36
|
+
const calculatedCutoff = Date.now() - daysMs;
|
|
37
|
+
cutoffTimestamp = cutoffTimestamp !== undefined
|
|
38
|
+
? Math.min(cutoffTimestamp, calculatedCutoff)
|
|
39
|
+
: calculatedCutoff;
|
|
40
|
+
}
|
|
41
|
+
const conditions = [];
|
|
42
|
+
const params = [];
|
|
43
|
+
const statusPlaceholders = statuses.map(() => '?').join(', ');
|
|
44
|
+
conditions.push(`status IN (${statusPlaceholders})`);
|
|
45
|
+
params.push(...statuses);
|
|
46
|
+
if (cutoffTimestamp !== undefined) {
|
|
47
|
+
conditions.push('timestamp < ?');
|
|
48
|
+
params.push(cutoffTimestamp);
|
|
49
|
+
}
|
|
50
|
+
const whereClause = `WHERE ${conditions.join(' AND ')}`;
|
|
51
|
+
// If maxEntries is set, only prune if total matching exceeds maxEntries
|
|
52
|
+
if (options.maxEntries !== undefined && options.maxEntries >= 0) {
|
|
53
|
+
const countRes = await this.db.execute(`SELECT COUNT(*) as total FROM sync_outbox ${whereClause}`, params);
|
|
54
|
+
const totalMatching = Number(countRes[0]?.total ?? 0);
|
|
55
|
+
const excess = totalMatching - options.maxEntries;
|
|
56
|
+
if (excess <= 0) {
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
// Delete the oldest `excess` records
|
|
60
|
+
const selectOldest = await this.db.execute(`SELECT id FROM sync_outbox ${whereClause} ORDER BY timestamp ASC LIMIT ?`, [...params, excess]);
|
|
61
|
+
if (selectOldest.length === 0)
|
|
62
|
+
return 0;
|
|
63
|
+
const ids = selectOldest.map((r) => r.id);
|
|
64
|
+
const idPlaceholders = ids.map(() => '?').join(', ');
|
|
65
|
+
await this.db.execute(`DELETE FROM sync_outbox WHERE id IN (${idPlaceholders})`, ids);
|
|
66
|
+
return ids.length;
|
|
67
|
+
}
|
|
68
|
+
// Standard cutoff delete
|
|
69
|
+
const beforeCountRes = await this.db.execute(`SELECT COUNT(*) as total FROM sync_outbox ${whereClause}`, params);
|
|
70
|
+
const toDeleteCount = Number(beforeCountRes[0]?.total ?? 0);
|
|
71
|
+
if (toDeleteCount === 0)
|
|
72
|
+
return 0;
|
|
73
|
+
await this.db.execute(`DELETE FROM sync_outbox ${whereClause}`, params);
|
|
74
|
+
return toDeleteCount;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Gathers storage metrics: table row counts, outbox status distribution, and database size.
|
|
78
|
+
*/
|
|
79
|
+
async getStorageStats() {
|
|
80
|
+
// 1. Table row counts
|
|
81
|
+
const tableCounts = {};
|
|
82
|
+
let tableNames = [];
|
|
83
|
+
try {
|
|
84
|
+
const masterRows = await this.db.execute(`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'`);
|
|
85
|
+
tableNames = masterRows.map((r) => r.name);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
tableNames = this.getKnownTables();
|
|
89
|
+
}
|
|
90
|
+
for (const tableName of tableNames) {
|
|
91
|
+
try {
|
|
92
|
+
const safeTable = (0, sql_js_1.escapeIdentifier)(tableName);
|
|
93
|
+
const rows = await this.db.execute(`SELECT COUNT(*) as count FROM ${safeTable}`);
|
|
94
|
+
tableCounts[tableName] = Number(rows[0]?.count ?? 0);
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
// Table might not exist or be accessible
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// 2. Outbox breakdown
|
|
101
|
+
const outboxStats = {
|
|
102
|
+
total: 0,
|
|
103
|
+
pending: 0,
|
|
104
|
+
processing: 0,
|
|
105
|
+
completed: 0,
|
|
106
|
+
failed: 0,
|
|
107
|
+
};
|
|
108
|
+
try {
|
|
109
|
+
const outboxRows = await this.db.execute(`SELECT status, COUNT(*) as count FROM sync_outbox GROUP BY status`);
|
|
110
|
+
for (const row of outboxRows) {
|
|
111
|
+
const st = String(row.status).toUpperCase();
|
|
112
|
+
const cnt = Number(row.count ?? 0);
|
|
113
|
+
outboxStats.total += cnt;
|
|
114
|
+
if (st === 'PENDING')
|
|
115
|
+
outboxStats.pending = cnt;
|
|
116
|
+
else if (st === 'PROCESSING')
|
|
117
|
+
outboxStats.processing = cnt;
|
|
118
|
+
else if (st === 'COMPLETED')
|
|
119
|
+
outboxStats.completed = cnt;
|
|
120
|
+
else if (st === 'FAILED')
|
|
121
|
+
outboxStats.failed = cnt;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
// sync_outbox table may not exist yet
|
|
126
|
+
}
|
|
127
|
+
// 3. SQLite database sizing via PRAGMAs
|
|
128
|
+
let pageSize = 4096;
|
|
129
|
+
let pageCount = 0;
|
|
130
|
+
try {
|
|
131
|
+
const pageSizeRes = await this.db.execute('PRAGMA page_size');
|
|
132
|
+
if (pageSizeRes[0]) {
|
|
133
|
+
pageSize = Number(Object.values(pageSizeRes[0])[0] ?? 4096);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
// fallback
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
const pageCountRes = await this.db.execute('PRAGMA page_count');
|
|
141
|
+
if (pageCountRes[0]) {
|
|
142
|
+
pageCount = Number(Object.values(pageCountRes[0])[0] ?? 0);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
// fallback
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
tables: tableCounts,
|
|
150
|
+
outbox: outboxStats,
|
|
151
|
+
pageSize,
|
|
152
|
+
pageCount,
|
|
153
|
+
databaseSizeBytes: pageSize * pageCount,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
exports.DatabaseMaintenance = DatabaseMaintenance;
|
package/dist/core/sync-worker.js
CHANGED
|
@@ -654,7 +654,7 @@ class SyncWorker {
|
|
|
654
654
|
}
|
|
655
655
|
}
|
|
656
656
|
// 3. Apply remote change to local database
|
|
657
|
-
await table_manager_js_1.TableManager.applyMutation(tx, change.tableName, change.rowId, change.operation, change.payload, this.primaryKey, { autoSchema: this.autoSchema, schemaManager: this.schemaManager });
|
|
657
|
+
await table_manager_js_1.TableManager.applyMutation(tx, change.tableName, change.rowId, change.operation, change.payload ?? null, this.primaryKey, { autoSchema: this.autoSchema, schemaManager: this.schemaManager });
|
|
658
658
|
// 4. Queue table change event to notify reactive listeners after commit
|
|
659
659
|
queuedEvents.push({
|
|
660
660
|
event: change.tableName,
|
package/dist/core/types.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export interface SyncOperation {
|
|
|
5
5
|
tableName: string;
|
|
6
6
|
rowId: string;
|
|
7
7
|
operation: SyncOp;
|
|
8
|
-
payload
|
|
8
|
+
payload?: Record<string, any> | null;
|
|
9
9
|
timestamp: number;
|
|
10
10
|
}
|
|
11
11
|
export interface PushResult {
|
|
@@ -16,14 +16,14 @@ export interface PushResult {
|
|
|
16
16
|
}
|
|
17
17
|
export interface OutboxRecord extends SyncOperation {
|
|
18
18
|
status: OutboxStatus;
|
|
19
|
-
retryCount
|
|
19
|
+
retryCount?: number;
|
|
20
20
|
lastError?: string | null;
|
|
21
21
|
}
|
|
22
22
|
export interface DeltaChange {
|
|
23
23
|
tableName: string;
|
|
24
24
|
operation: SyncOp;
|
|
25
25
|
rowId: string;
|
|
26
|
-
payload
|
|
26
|
+
payload?: Record<string, any> | null;
|
|
27
27
|
timestamp?: number;
|
|
28
28
|
}
|
|
29
29
|
export interface DeltaChanges {
|
|
@@ -41,7 +41,37 @@ export interface ConflictContext {
|
|
|
41
41
|
localRow?: Record<string, any> | null;
|
|
42
42
|
}
|
|
43
43
|
export type ConflictResolver = (context: ConflictContext) => ConflictResolution | Promise<ConflictResolution>;
|
|
44
|
-
export type ConflictStrategy = 'last-write-wins' | 'server-wins' | 'client-wins';
|
|
44
|
+
export type ConflictStrategy = 'last-write-wins' | 'server-wins' | 'client-wins' | 'field-merge';
|
|
45
|
+
export type FieldMergeWinner = 'remote' | 'local' | 'newer' | 'max' | 'min';
|
|
46
|
+
export type FieldMergeCustomResolver = (localVal: any, remoteVal: any, context: ConflictContext) => any;
|
|
47
|
+
export type FieldMergeRule = FieldMergeWinner | FieldMergeCustomResolver;
|
|
48
|
+
export interface FieldMergeResolverOptions {
|
|
49
|
+
/**
|
|
50
|
+
* Default resolution strategy when both local and remote updated the exact same field.
|
|
51
|
+
* 'newer' (default) uses timestamp to pick the newer field; 'remote' or 'local' picks fixed side.
|
|
52
|
+
*/
|
|
53
|
+
defaultWinner?: 'remote' | 'local' | 'newer';
|
|
54
|
+
/**
|
|
55
|
+
* Field-level merge rules by table and column.
|
|
56
|
+
* e.g.
|
|
57
|
+
* {
|
|
58
|
+
* notes: {
|
|
59
|
+
* tags: (local, remote) => Array.from(new Set([...(local ?? []), ...(remote ?? [])])),
|
|
60
|
+
* view_count: 'max',
|
|
61
|
+
* },
|
|
62
|
+
* '*': {
|
|
63
|
+
* updated_at: 'newer'
|
|
64
|
+
* }
|
|
65
|
+
* }
|
|
66
|
+
*/
|
|
67
|
+
fieldRules?: Record<string, Record<string, FieldMergeRule>>;
|
|
68
|
+
/**
|
|
69
|
+
* Strategy when one side deleted the row while the other side updated fields.
|
|
70
|
+
* 'delete-wins' (default) preserves the deletion.
|
|
71
|
+
* 'update-wins' resurrects the row with the updated fields.
|
|
72
|
+
*/
|
|
73
|
+
deleteHandling?: 'delete-wins' | 'update-wins';
|
|
74
|
+
}
|
|
45
75
|
export interface NetworkStatusProvider {
|
|
46
76
|
isOnline(): boolean | Promise<boolean>;
|
|
47
77
|
subscribe(callback: (isOnline: boolean) => void): () => void;
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export * from './core/meta-store.js';
|
|
|
8
8
|
export * from './core/table-manager.js';
|
|
9
9
|
export * from './core/schema-manager.js';
|
|
10
10
|
export * from './core/query-builder.js';
|
|
11
|
+
export * from './core/maintenance.js';
|
|
11
12
|
export * from './reactive/event-emitter.js';
|
|
12
13
|
export * from './adapters/local/index.js';
|
|
13
14
|
export * from './adapters/remote/index.js';
|
|
@@ -17,4 +18,7 @@ export type * from './react/index.js';
|
|
|
17
18
|
export * from './utils/uuid.js';
|
|
18
19
|
export * from './utils/sql.js';
|
|
19
20
|
export * from './utils/logger.js';
|
|
21
|
+
export * from './utils/portability.js';
|
|
20
22
|
export * from './schema/index.js';
|
|
23
|
+
export * from './auth/index.js';
|
|
24
|
+
export * from './backups/index.js';
|
package/dist/index.js
CHANGED
|
@@ -24,6 +24,7 @@ __exportStar(require("./core/meta-store.js"), exports);
|
|
|
24
24
|
__exportStar(require("./core/table-manager.js"), exports);
|
|
25
25
|
__exportStar(require("./core/schema-manager.js"), exports);
|
|
26
26
|
__exportStar(require("./core/query-builder.js"), exports);
|
|
27
|
+
__exportStar(require("./core/maintenance.js"), exports);
|
|
27
28
|
__exportStar(require("./reactive/event-emitter.js"), exports);
|
|
28
29
|
__exportStar(require("./adapters/local/index.js"), exports);
|
|
29
30
|
__exportStar(require("./adapters/remote/index.js"), exports);
|
|
@@ -32,4 +33,7 @@ __exportStar(require("./config/index.js"), exports);
|
|
|
32
33
|
__exportStar(require("./utils/uuid.js"), exports);
|
|
33
34
|
__exportStar(require("./utils/sql.js"), exports);
|
|
34
35
|
__exportStar(require("./utils/logger.js"), exports);
|
|
36
|
+
__exportStar(require("./utils/portability.js"), exports);
|
|
35
37
|
__exportStar(require("./schema/index.js"), exports);
|
|
38
|
+
__exportStar(require("./auth/index.js"), exports);
|
|
39
|
+
__exportStar(require("./backups/index.js"), exports);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { GoogleAuthClient, GoogleUser } from '../auth/google.js';
|
|
2
|
+
import type { MicrosoftAuthClient, MicrosoftUser } from '../auth/microsoft.js';
|
|
3
|
+
import type { GitHubAuthClient, GitHubUser } from '../auth/github.js';
|
|
4
|
+
import type { AppleAuthClient, AppleUser, AppleNativeCredential } from '../auth/apple.js';
|
|
5
|
+
export interface UseGoogleAuthResult {
|
|
6
|
+
user: GoogleUser | null;
|
|
7
|
+
isSignedIn: boolean;
|
|
8
|
+
loading: boolean;
|
|
9
|
+
error: Error | null;
|
|
10
|
+
signIn: () => Promise<GoogleUser | null>;
|
|
11
|
+
signOut: () => Promise<void>;
|
|
12
|
+
refetchUser: () => Promise<GoogleUser | null>;
|
|
13
|
+
}
|
|
14
|
+
export declare function useGoogleAuth(client: GoogleAuthClient | null | undefined): UseGoogleAuthResult;
|
|
15
|
+
export interface UseMicrosoftAuthResult {
|
|
16
|
+
user: MicrosoftUser | null;
|
|
17
|
+
isSignedIn: boolean;
|
|
18
|
+
loading: boolean;
|
|
19
|
+
error: Error | null;
|
|
20
|
+
signIn: () => Promise<MicrosoftUser | null>;
|
|
21
|
+
signOut: () => Promise<void>;
|
|
22
|
+
refetchUser: () => Promise<MicrosoftUser | null>;
|
|
23
|
+
}
|
|
24
|
+
export type UseOneDriveAuthResult = UseMicrosoftAuthResult;
|
|
25
|
+
export declare function useMicrosoftAuth(client: MicrosoftAuthClient | null | undefined): UseMicrosoftAuthResult;
|
|
26
|
+
export declare const useOneDriveAuth: typeof useMicrosoftAuth;
|
|
27
|
+
export interface UseGitHubAuthResult {
|
|
28
|
+
user: GitHubUser | null;
|
|
29
|
+
isSignedIn: boolean;
|
|
30
|
+
loading: boolean;
|
|
31
|
+
error: Error | null;
|
|
32
|
+
signIn: () => Promise<GitHubUser | null>;
|
|
33
|
+
signOut: () => Promise<void>;
|
|
34
|
+
refetchUser: () => Promise<GitHubUser | null>;
|
|
35
|
+
}
|
|
36
|
+
export declare function useGitHubAuth(client: GitHubAuthClient | null | undefined): UseGitHubAuthResult;
|
|
37
|
+
export interface UseAppleAuthResult {
|
|
38
|
+
user: AppleUser | null;
|
|
39
|
+
isSignedIn: boolean;
|
|
40
|
+
loading: boolean;
|
|
41
|
+
error: Error | null;
|
|
42
|
+
signIn: (nativeCredential?: AppleNativeCredential) => Promise<AppleUser | null>;
|
|
43
|
+
signOut: () => Promise<void>;
|
|
44
|
+
refetchUser: () => Promise<AppleUser | null>;
|
|
45
|
+
}
|
|
46
|
+
export declare function useAppleAuth(client: AppleAuthClient | null | undefined): UseAppleAuthResult;
|