local-lite 2.0.7 → 3.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 +0 -30
- package/dist/adapters/local/index.d.ts +1 -0
- package/dist/adapters/local/index.js +1 -0
- package/dist/adapters/local/sqlite-wasm.d.ts +25 -0
- package/dist/adapters/local/sqlite-wasm.js +55 -0
- package/dist/adapters/remote/convex.d.ts +1 -1
- package/dist/adapters/remote/convex.js +25 -3
- package/dist/adapters/remote/demo.d.ts +3 -3
- package/dist/adapters/remote/demo.js +11 -3
- package/dist/adapters/remote/firestore.d.ts +1 -1
- package/dist/adapters/remote/firestore.js +57 -91
- package/dist/adapters/remote/index.d.ts +2 -1
- package/dist/adapters/remote/index.js +1 -0
- package/dist/adapters/remote/postgres.d.ts +37 -0
- package/dist/adapters/remote/postgres.js +119 -0
- package/dist/adapters/remote/self-hosted.d.ts +12 -0
- package/dist/adapters/remote/self-hosted.js +55 -4
- package/dist/adapters/remote/supabase.d.ts +1 -1
- package/dist/adapters/remote/supabase.js +22 -5
- package/dist/auth/apple.js +0 -6
- package/dist/auth/github.js +0 -3
- package/dist/auth/google.js +0 -3
- package/dist/auth/identity.d.ts +0 -5
- package/dist/auth/identity.js +0 -3
- package/dist/auth/microsoft.js +0 -3
- package/dist/auth/unified.d.ts +5 -5
- package/dist/auth/unified.js +1 -1
- package/dist/config/index.d.ts +0 -12
- package/dist/config/index.js +3 -26
- package/dist/config/types.d.ts +3 -2
- package/dist/core/audit.d.ts +52 -0
- package/dist/core/audit.js +153 -0
- package/dist/core/client.d.ts +4 -0
- package/dist/core/client.js +7 -0
- package/dist/core/conflict.d.ts +24 -0
- package/dist/core/conflict.js +77 -0
- package/dist/core/field-encryption.d.ts +25 -0
- package/dist/core/field-encryption.js +128 -0
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.js +2 -0
- package/dist/core/query-builder.d.ts +51 -0
- package/dist/core/query-builder.js +278 -35
- package/dist/core/query.js +6 -1
- package/dist/core/types.d.ts +2 -1
- package/dist/devtools/BenchmarkModal.js +285 -1
- package/dist/email/index.d.ts +1 -1
- package/dist/email/index.js +2 -2
- package/dist/index.d.ts +2 -27
- package/dist/index.js +2 -27
- package/dist/presence/index.d.ts +45 -0
- package/dist/presence/index.js +142 -0
- package/dist/reactive/event-emitter.d.ts +10 -1
- package/dist/reactive/event-emitter.js +44 -1
- package/dist/reactive/live-query.d.ts +18 -0
- package/dist/reactive/live-query.js +71 -0
- package/dist/schema/index.d.ts +1 -0
- package/dist/schema/index.js +1 -0
- package/dist/schema/typed.d.ts +38 -0
- package/dist/schema/typed.js +33 -0
- package/dist/server/dashboard.d.ts +2 -0
- package/dist/server/dashboard.js +182 -2
- package/dist/solid/index.d.ts +24 -0
- package/dist/solid/index.js +35 -0
- package/dist/storage/firebase.d.ts +3 -3
- package/dist/storage/firebase.js +40 -94
- package/dist/svelte/index.d.ts +15 -0
- package/dist/svelte/index.js +27 -0
- package/dist/testing/index.d.ts +22 -0
- package/dist/testing/index.js +74 -0
- package/dist/vue/index.d.ts +27 -0
- package/dist/vue/index.js +37 -0
- package/package.json +46 -9
- package/dist/kv/index.d.ts +0 -3
- package/dist/kv/index.js +0 -19
- package/dist/kv/manager.d.ts +0 -52
- package/dist/kv/manager.js +0 -167
- package/dist/kv/store.d.ts +0 -13
- package/dist/kv/store.js +0 -85
- package/dist/kv/types.d.ts +0 -27
- package/dist/kv/types.js +0 -2
- package/dist/react/kv.d.ts +0 -9
- package/dist/react/kv.js +0 -53
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.liveQuery = liveQuery;
|
|
4
|
+
exports.syncStatusStore = syncStatusStore;
|
|
5
|
+
const live_query_js_1 = require("../reactive/live-query.js");
|
|
6
|
+
/**
|
|
7
|
+
* Creates a Svelte-compatible Readable store (`$store` reactive) backed by a live local-lite query.
|
|
8
|
+
*/
|
|
9
|
+
function liveQuery(client, tableName, options) {
|
|
10
|
+
return {
|
|
11
|
+
subscribe(run) {
|
|
12
|
+
run({ data: [], loading: true, error: null });
|
|
13
|
+
const { dispose } = (0, live_query_js_1.subscribeLiveQuery)(client, tableName, options, run);
|
|
14
|
+
return dispose;
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates a Svelte-compatible Readable store tracking `client.syncWorker` status.
|
|
20
|
+
*/
|
|
21
|
+
function syncStatusStore(client) {
|
|
22
|
+
return {
|
|
23
|
+
subscribe(run) {
|
|
24
|
+
return (0, live_query_js_1.subscribeSyncStatus)(client, run);
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { LocalFirstClient, LocalFirstClientConfig } from '../core/client.js';
|
|
2
|
+
import { MemoryDemoRemoteAdapter } from '../adapters/remote/demo.js';
|
|
3
|
+
import { DefaultNetworkStatusProvider } from '../adapters/network/index.js';
|
|
4
|
+
export interface CreateTestClientOptions extends Partial<Omit<LocalFirstClientConfig, 'local'>> {
|
|
5
|
+
tables?: string[];
|
|
6
|
+
online?: boolean;
|
|
7
|
+
remoteLatencyMs?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface TestClientHandle {
|
|
10
|
+
client: LocalFirstClient;
|
|
11
|
+
remote: MemoryDemoRemoteAdapter;
|
|
12
|
+
network: DefaultNetworkStatusProvider;
|
|
13
|
+
simulateOffline: () => void;
|
|
14
|
+
simulateOnline: () => void;
|
|
15
|
+
flushOutbox: () => Promise<void>;
|
|
16
|
+
injectRemoteChange: (tableName: string, rowId: string, payload: Record<string, any>, operation?: 'INSERT' | 'UPDATE' | 'DELETE') => Promise<void>;
|
|
17
|
+
dispose: () => Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Creates an isolated in-memory `LocalFirstClient` with controllable network and remote state for deterministic tests.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createTestClient(options?: CreateTestClientOptions): Promise<TestClientHandle>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createTestClient = createTestClient;
|
|
4
|
+
const node_sqlite_1 = require("node:sqlite");
|
|
5
|
+
const client_js_1 = require("../core/client.js");
|
|
6
|
+
const node_sqlite_js_1 = require("../adapters/local/node-sqlite.js");
|
|
7
|
+
const demo_js_1 = require("../adapters/remote/demo.js");
|
|
8
|
+
const index_js_1 = require("../adapters/network/index.js");
|
|
9
|
+
/**
|
|
10
|
+
* Creates an isolated in-memory `LocalFirstClient` with controllable network and remote state for deterministic tests.
|
|
11
|
+
*/
|
|
12
|
+
async function createTestClient(options = {}) {
|
|
13
|
+
const raw = new node_sqlite_1.DatabaseSync(':memory:');
|
|
14
|
+
const local = new node_sqlite_js_1.NodeSQLiteAdapter(raw);
|
|
15
|
+
const remote = options.remote ??
|
|
16
|
+
new demo_js_1.MemoryDemoRemoteAdapter(options.remoteLatencyMs ?? 0);
|
|
17
|
+
const injectedChanges = [];
|
|
18
|
+
if (!options.remote) {
|
|
19
|
+
const origFetch = remote.fetchChanges.bind(remote);
|
|
20
|
+
remote.fetchChanges = async (sinceTimestamp) => {
|
|
21
|
+
const base = await origFetch(sinceTimestamp);
|
|
22
|
+
const pending = injectedChanges.filter((c) => c.timestamp > sinceTimestamp);
|
|
23
|
+
return {
|
|
24
|
+
...base,
|
|
25
|
+
timestamp: Date.now(),
|
|
26
|
+
changes: [...(base.changes ?? []), ...pending],
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const network = options.network ??
|
|
31
|
+
new index_js_1.DefaultNetworkStatusProvider(options.online ?? true);
|
|
32
|
+
const client = new client_js_1.LocalFirstClient({
|
|
33
|
+
...options,
|
|
34
|
+
local,
|
|
35
|
+
remote,
|
|
36
|
+
network,
|
|
37
|
+
tables: options.tables ?? ['todos'],
|
|
38
|
+
autoSchema: options.autoSchema ?? true,
|
|
39
|
+
autoStart: options.autoStart ?? false,
|
|
40
|
+
logger: options.logger ?? false,
|
|
41
|
+
});
|
|
42
|
+
await client.initialize();
|
|
43
|
+
return {
|
|
44
|
+
client,
|
|
45
|
+
remote,
|
|
46
|
+
network,
|
|
47
|
+
simulateOffline() {
|
|
48
|
+
network.setOnline(false);
|
|
49
|
+
},
|
|
50
|
+
simulateOnline() {
|
|
51
|
+
network.setOnline(true);
|
|
52
|
+
},
|
|
53
|
+
async flushOutbox() {
|
|
54
|
+
await client.syncWorker.pushLocalChanges();
|
|
55
|
+
await client.syncWorker.pullRemoteChanges();
|
|
56
|
+
},
|
|
57
|
+
async injectRemoteChange(tableName, rowId, payload, operation = 'UPDATE') {
|
|
58
|
+
const ts = Date.now() + 1000;
|
|
59
|
+
const change = {
|
|
60
|
+
tableName,
|
|
61
|
+
rowId,
|
|
62
|
+
operation,
|
|
63
|
+
timestamp: ts,
|
|
64
|
+
payload: operation === 'DELETE' ? null : payload,
|
|
65
|
+
};
|
|
66
|
+
injectedChanges.push(change);
|
|
67
|
+
remote.emitLiveChanges({ timestamp: ts, changes: [change] });
|
|
68
|
+
},
|
|
69
|
+
async dispose() {
|
|
70
|
+
client.stop();
|
|
71
|
+
raw.close();
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { LocalFirstClient } from '../core/client.js';
|
|
2
|
+
import { QueryOptions, SyncStatusEvent } from '../core/types.js';
|
|
3
|
+
export interface VueRef<T> {
|
|
4
|
+
value: T;
|
|
5
|
+
}
|
|
6
|
+
export interface VueCompositionApi {
|
|
7
|
+
ref<T>(initial: T): VueRef<T>;
|
|
8
|
+
onUnmounted?: (fn: () => void) => void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Vue 3 composable for live local-lite queries.
|
|
12
|
+
* Pass `{ ref, onUnmounted }` from `'vue'` to avoid bundling Vue inside `local-lite`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function useLiveQuery<T = any>(client: LocalFirstClient, tableName: string, options: QueryOptions | undefined, vue: VueCompositionApi): {
|
|
15
|
+
data: VueRef<T[]>;
|
|
16
|
+
loading: VueRef<boolean>;
|
|
17
|
+
error: VueRef<Error | null>;
|
|
18
|
+
refresh: () => Promise<void>;
|
|
19
|
+
dispose: () => void;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Vue 3 composable for tracking `client.syncWorker` status.
|
|
23
|
+
*/
|
|
24
|
+
export declare function useSyncStatus(client: LocalFirstClient, vue: VueCompositionApi): {
|
|
25
|
+
status: VueRef<SyncStatusEvent>;
|
|
26
|
+
dispose: () => void;
|
|
27
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useLiveQuery = useLiveQuery;
|
|
4
|
+
exports.useSyncStatus = useSyncStatus;
|
|
5
|
+
const live_query_js_1 = require("../reactive/live-query.js");
|
|
6
|
+
/**
|
|
7
|
+
* Vue 3 composable for live local-lite queries.
|
|
8
|
+
* Pass `{ ref, onUnmounted }` from `'vue'` to avoid bundling Vue inside `local-lite`.
|
|
9
|
+
*/
|
|
10
|
+
function useLiveQuery(client, tableName, options, vue) {
|
|
11
|
+
const data = vue.ref([]);
|
|
12
|
+
const loading = vue.ref(true);
|
|
13
|
+
const error = vue.ref(null);
|
|
14
|
+
const { refresh, dispose } = (0, live_query_js_1.subscribeLiveQuery)(client, tableName, options, (state) => {
|
|
15
|
+
data.value = state.data;
|
|
16
|
+
loading.value = state.loading;
|
|
17
|
+
error.value = state.error;
|
|
18
|
+
});
|
|
19
|
+
vue.onUnmounted?.(dispose);
|
|
20
|
+
return { data, loading, error, refresh, dispose };
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Vue 3 composable for tracking `client.syncWorker` status.
|
|
24
|
+
*/
|
|
25
|
+
function useSyncStatus(client, vue) {
|
|
26
|
+
const status = vue.ref({
|
|
27
|
+
isOnline: true,
|
|
28
|
+
isPushing: false,
|
|
29
|
+
isPulling: false,
|
|
30
|
+
pendingCount: 0,
|
|
31
|
+
});
|
|
32
|
+
const dispose = (0, live_query_js_1.subscribeSyncStatus)(client, (next) => {
|
|
33
|
+
status.value = next;
|
|
34
|
+
});
|
|
35
|
+
vue.onUnmounted?.(dispose);
|
|
36
|
+
return { status, dispose };
|
|
37
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "local-lite",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.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",
|
|
@@ -121,6 +121,16 @@
|
|
|
121
121
|
"import": "./dist/adapters/local/node-sqlite.js",
|
|
122
122
|
"default": "./dist/adapters/local/node-sqlite.js"
|
|
123
123
|
},
|
|
124
|
+
"./sqlite-wasm": {
|
|
125
|
+
"types": "./dist/adapters/local/sqlite-wasm.d.ts",
|
|
126
|
+
"import": "./dist/adapters/local/sqlite-wasm.js",
|
|
127
|
+
"default": "./dist/adapters/local/sqlite-wasm.js"
|
|
128
|
+
},
|
|
129
|
+
"./postgres": {
|
|
130
|
+
"types": "./dist/adapters/remote/postgres.d.ts",
|
|
131
|
+
"import": "./dist/adapters/remote/postgres.js",
|
|
132
|
+
"default": "./dist/adapters/remote/postgres.js"
|
|
133
|
+
},
|
|
124
134
|
"./core": {
|
|
125
135
|
"types": "./dist/core/index.d.ts",
|
|
126
136
|
"import": "./dist/core/index.js",
|
|
@@ -170,27 +180,54 @@
|
|
|
170
180
|
"types": "./dist/payments/server.d.ts",
|
|
171
181
|
"import": "./dist/payments/server.js",
|
|
172
182
|
"default": "./dist/payments/server.js"
|
|
183
|
+
},
|
|
184
|
+
"./svelte": {
|
|
185
|
+
"types": "./dist/svelte/index.d.ts",
|
|
186
|
+
"import": "./dist/svelte/index.js",
|
|
187
|
+
"default": "./dist/svelte/index.js"
|
|
188
|
+
},
|
|
189
|
+
"./vue": {
|
|
190
|
+
"types": "./dist/vue/index.d.ts",
|
|
191
|
+
"import": "./dist/vue/index.js",
|
|
192
|
+
"default": "./dist/vue/index.js"
|
|
193
|
+
},
|
|
194
|
+
"./solid": {
|
|
195
|
+
"types": "./dist/solid/index.d.ts",
|
|
196
|
+
"import": "./dist/solid/index.js",
|
|
197
|
+
"default": "./dist/solid/index.js"
|
|
198
|
+
},
|
|
199
|
+
"./testing": {
|
|
200
|
+
"types": "./dist/testing/index.d.ts",
|
|
201
|
+
"import": "./dist/testing/index.js",
|
|
202
|
+
"default": "./dist/testing/index.js"
|
|
203
|
+
},
|
|
204
|
+
"./presence": {
|
|
205
|
+
"types": "./dist/presence/index.d.ts",
|
|
206
|
+
"import": "./dist/presence/index.js",
|
|
207
|
+
"default": "./dist/presence/index.js"
|
|
173
208
|
}
|
|
174
209
|
},
|
|
175
210
|
"scripts": {
|
|
176
211
|
"build": "tsc",
|
|
212
|
+
"docs": "node scripts/serve-docs.mjs",
|
|
213
|
+
"example": "npm run --prefix example dev",
|
|
177
214
|
"test": "vitest run",
|
|
178
215
|
"test:watch": "vitest",
|
|
179
216
|
"test:package": "node scripts/package-smoke.mjs",
|
|
180
217
|
"prepublishOnly": "npm run test && npm run build && npm run test:package"
|
|
181
218
|
},
|
|
182
219
|
"peerDependencies": {
|
|
183
|
-
"@libsql/client": "
|
|
184
|
-
"@op-engineering/op-sqlite": "
|
|
185
|
-
"@supabase/supabase-js": "
|
|
186
|
-
"better-sqlite3": "
|
|
187
|
-
"convex": ">=1.
|
|
220
|
+
"@libsql/client": ">=0.14.0",
|
|
221
|
+
"@op-engineering/op-sqlite": ">=10.0.0",
|
|
222
|
+
"@supabase/supabase-js": ">=2.45.0",
|
|
223
|
+
"better-sqlite3": ">=11.0.0",
|
|
224
|
+
"convex": ">=1.15.0",
|
|
188
225
|
"expo-file-system": ">=58.0.0",
|
|
189
226
|
"expo-sqlite": ">=58.0.0",
|
|
190
|
-
"firebase": ">=
|
|
227
|
+
"firebase": ">=11.0.0",
|
|
191
228
|
"nodemailer": ">=6.9.0",
|
|
192
229
|
"react": ">=18.0.0",
|
|
193
|
-
"sql.js": "
|
|
230
|
+
"sql.js": ">=1.10.0"
|
|
194
231
|
},
|
|
195
232
|
"peerDependenciesMeta": {
|
|
196
233
|
"nodemailer": {
|
|
@@ -233,7 +270,7 @@
|
|
|
233
270
|
"@types/react": "^19.3.0",
|
|
234
271
|
"@types/uuid": "^11.0.0",
|
|
235
272
|
"expo-sqlite": "^58.0.17",
|
|
236
|
-
"nodemailer": "^10.
|
|
273
|
+
"nodemailer": "^10.6.0",
|
|
237
274
|
"react": "^19.3.1",
|
|
238
275
|
"typescript": "^7.0.2",
|
|
239
276
|
"vitest": "^5.1.0"
|
package/dist/kv/index.d.ts
DELETED
package/dist/kv/index.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./types.js"), exports);
|
|
18
|
-
__exportStar(require("./store.js"), exports);
|
|
19
|
-
__exportStar(require("./manager.js"), exports);
|
package/dist/kv/manager.d.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { DBExecutable } from '../core/schema-manager.js';
|
|
2
|
-
import { EventEmitter } from '../reactive/event-emitter.js';
|
|
3
|
-
import { KVRecord, KVSetOptions, KVSetSecretOptions, KVStoreOptions } from './types.js';
|
|
4
|
-
export declare class KVManager {
|
|
5
|
-
private db;
|
|
6
|
-
private tableName;
|
|
7
|
-
private events?;
|
|
8
|
-
constructor(db: DBExecutable, eventsOrOptions?: EventEmitter | KVStoreOptions, options?: KVStoreOptions);
|
|
9
|
-
initialize(): Promise<void>;
|
|
10
|
-
/**
|
|
11
|
-
* Sets a value in the key-value store with optional TTL.
|
|
12
|
-
* Emits an event on the event emitter if configured.
|
|
13
|
-
*/
|
|
14
|
-
set(key: string, value: any, options?: KVSetOptions): Promise<void>;
|
|
15
|
-
/**
|
|
16
|
-
* Retrieves the value for a key, returning defaultValue if not found or expired.
|
|
17
|
-
*/
|
|
18
|
-
get<T = any>(key: string, defaultValue?: T): Promise<T | undefined>;
|
|
19
|
-
/**
|
|
20
|
-
* Retrieves the full KVRecord metadata for a key.
|
|
21
|
-
*/
|
|
22
|
-
getRecord(key: string): Promise<KVRecord | null>;
|
|
23
|
-
/**
|
|
24
|
-
* Checks whether a non-expired key exists in the store.
|
|
25
|
-
*/
|
|
26
|
-
has(key: string): Promise<boolean>;
|
|
27
|
-
/**
|
|
28
|
-
* Removes a key from the store.
|
|
29
|
-
* Emits an event on the event emitter if configured.
|
|
30
|
-
*/
|
|
31
|
-
delete(key: string): Promise<void>;
|
|
32
|
-
/**
|
|
33
|
-
* Lists all non-expired keys, optionally filtered by a prefix.
|
|
34
|
-
*/
|
|
35
|
-
keys(prefix?: string): Promise<string[]>;
|
|
36
|
-
/**
|
|
37
|
-
* Clears all stored key-value pairs.
|
|
38
|
-
*/
|
|
39
|
-
clear(): Promise<void>;
|
|
40
|
-
/**
|
|
41
|
-
* Stores an encrypted secret using AES-256-GCM + PBKDF2 passphrase encryption.
|
|
42
|
-
*/
|
|
43
|
-
setSecret(key: string, secretValue: any, options: KVSetSecretOptions): Promise<void>;
|
|
44
|
-
/**
|
|
45
|
-
* Decrypts and retrieves a stored secret.
|
|
46
|
-
*/
|
|
47
|
-
getSecret<T = any>(key: string, passphrase: string, defaultValue?: T): Promise<T | undefined>;
|
|
48
|
-
/**
|
|
49
|
-
* Purges all expired keys immediately.
|
|
50
|
-
*/
|
|
51
|
-
pruneExpired(): Promise<number>;
|
|
52
|
-
}
|
package/dist/kv/manager.js
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.KVManager = void 0;
|
|
4
|
-
const encryption_js_1 = require("../backups/encryption.js");
|
|
5
|
-
const signed_token_js_1 = require("../keys/signed-token.js");
|
|
6
|
-
const store_js_1 = require("./store.js");
|
|
7
|
-
class KVManager {
|
|
8
|
-
db;
|
|
9
|
-
tableName;
|
|
10
|
-
events;
|
|
11
|
-
constructor(db, eventsOrOptions, options) {
|
|
12
|
-
this.db = db;
|
|
13
|
-
let opts = {};
|
|
14
|
-
if (eventsOrOptions &&
|
|
15
|
-
typeof eventsOrOptions.emit === 'function') {
|
|
16
|
-
this.events = eventsOrOptions;
|
|
17
|
-
opts = options || {};
|
|
18
|
-
}
|
|
19
|
-
else if (eventsOrOptions) {
|
|
20
|
-
opts = eventsOrOptions;
|
|
21
|
-
}
|
|
22
|
-
this.tableName = opts.tableName ?? store_js_1.DEFAULT_KV_TABLE;
|
|
23
|
-
}
|
|
24
|
-
async initialize() {
|
|
25
|
-
await store_js_1.KVStore.ensureSchema(this.db, this.tableName);
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Sets a value in the key-value store with optional TTL.
|
|
29
|
-
* Emits an event on the event emitter if configured.
|
|
30
|
-
*/
|
|
31
|
-
async set(key, value, options = {}) {
|
|
32
|
-
const now = Date.now();
|
|
33
|
-
const expiresAt = options.expiresAt !== undefined
|
|
34
|
-
? options.expiresAt
|
|
35
|
-
: options.ttlMs !== undefined
|
|
36
|
-
? now + options.ttlMs
|
|
37
|
-
: null;
|
|
38
|
-
let type;
|
|
39
|
-
let valueStr;
|
|
40
|
-
if (typeof value === 'string') {
|
|
41
|
-
type = 'string';
|
|
42
|
-
valueStr = value;
|
|
43
|
-
}
|
|
44
|
-
else if (typeof value === 'number') {
|
|
45
|
-
type = 'number';
|
|
46
|
-
valueStr = String(value);
|
|
47
|
-
}
|
|
48
|
-
else if (typeof value === 'boolean') {
|
|
49
|
-
type = 'boolean';
|
|
50
|
-
valueStr = value ? '1' : '0';
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
type = 'json';
|
|
54
|
-
valueStr = JSON.stringify(value ?? null);
|
|
55
|
-
}
|
|
56
|
-
await store_js_1.KVStore.set(this.db, this.tableName, key, valueStr, type, now, expiresAt);
|
|
57
|
-
this.events?.emit(this.tableName, { operation: 'SET', key, value });
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Retrieves the value for a key, returning defaultValue if not found or expired.
|
|
61
|
-
*/
|
|
62
|
-
async get(key, defaultValue) {
|
|
63
|
-
const record = await store_js_1.KVStore.get(this.db, this.tableName, key);
|
|
64
|
-
if (!record)
|
|
65
|
-
return defaultValue;
|
|
66
|
-
if (record.expiresAt && record.expiresAt <= Date.now()) {
|
|
67
|
-
// Lazy deletion of expired key
|
|
68
|
-
store_js_1.KVStore.delete(this.db, this.tableName, key).catch(() => { });
|
|
69
|
-
return defaultValue;
|
|
70
|
-
}
|
|
71
|
-
return record.value;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Retrieves the full KVRecord metadata for a key.
|
|
75
|
-
*/
|
|
76
|
-
async getRecord(key) {
|
|
77
|
-
const record = await store_js_1.KVStore.get(this.db, this.tableName, key);
|
|
78
|
-
if (!record)
|
|
79
|
-
return null;
|
|
80
|
-
if (record.expiresAt && record.expiresAt <= Date.now()) {
|
|
81
|
-
store_js_1.KVStore.delete(this.db, this.tableName, key).catch(() => { });
|
|
82
|
-
return null;
|
|
83
|
-
}
|
|
84
|
-
return record;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Checks whether a non-expired key exists in the store.
|
|
88
|
-
*/
|
|
89
|
-
async has(key) {
|
|
90
|
-
const val = await this.get(key);
|
|
91
|
-
return val !== undefined;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Removes a key from the store.
|
|
95
|
-
* Emits an event on the event emitter if configured.
|
|
96
|
-
*/
|
|
97
|
-
async delete(key) {
|
|
98
|
-
await store_js_1.KVStore.delete(this.db, this.tableName, key);
|
|
99
|
-
this.events?.emit(this.tableName, { operation: 'DELETE', key });
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Lists all non-expired keys, optionally filtered by a prefix.
|
|
103
|
-
*/
|
|
104
|
-
async keys(prefix) {
|
|
105
|
-
return store_js_1.KVStore.listKeys(this.db, this.tableName, prefix);
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Clears all stored key-value pairs.
|
|
109
|
-
*/
|
|
110
|
-
async clear() {
|
|
111
|
-
await store_js_1.KVStore.clear(this.db, this.tableName);
|
|
112
|
-
this.events?.emit(this.tableName, { operation: 'CLEAR' });
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Stores an encrypted secret using AES-256-GCM + PBKDF2 passphrase encryption.
|
|
116
|
-
*/
|
|
117
|
-
async setSecret(key, secretValue, options) {
|
|
118
|
-
if (!options.passphrase) {
|
|
119
|
-
throw new Error('Passphrase is required to store encrypted secret.');
|
|
120
|
-
}
|
|
121
|
-
const strValue = typeof secretValue === 'string'
|
|
122
|
-
? secretValue
|
|
123
|
-
: JSON.stringify(secretValue);
|
|
124
|
-
const plainBytes = new TextEncoder().encode(strValue);
|
|
125
|
-
const encryptedBytes = await (0, encryption_js_1.encryptBackupData)(plainBytes, options.passphrase);
|
|
126
|
-
const encryptedBase64 = (0, signed_token_js_1.base64UrlEncode)(encryptedBytes);
|
|
127
|
-
const now = Date.now();
|
|
128
|
-
const expiresAt = options.expiresAt !== undefined
|
|
129
|
-
? options.expiresAt
|
|
130
|
-
: options.ttlMs !== undefined
|
|
131
|
-
? now + options.ttlMs
|
|
132
|
-
: null;
|
|
133
|
-
await store_js_1.KVStore.set(this.db, this.tableName, key, encryptedBase64, 'encrypted', now, expiresAt);
|
|
134
|
-
this.events?.emit(this.tableName, { operation: 'SET_SECRET', key });
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Decrypts and retrieves a stored secret.
|
|
138
|
-
*/
|
|
139
|
-
async getSecret(key, passphrase, defaultValue) {
|
|
140
|
-
const record = await store_js_1.KVStore.get(this.db, this.tableName, key);
|
|
141
|
-
if (!record)
|
|
142
|
-
return defaultValue;
|
|
143
|
-
if (record.expiresAt && record.expiresAt <= Date.now()) {
|
|
144
|
-
store_js_1.KVStore.delete(this.db, this.tableName, key).catch(() => { });
|
|
145
|
-
return defaultValue;
|
|
146
|
-
}
|
|
147
|
-
if (record.type !== 'encrypted') {
|
|
148
|
-
return record.value;
|
|
149
|
-
}
|
|
150
|
-
const encryptedBytes = (0, signed_token_js_1.base64UrlDecode)(record.value);
|
|
151
|
-
const decryptedBytes = await (0, encryption_js_1.decryptBackupData)(encryptedBytes, passphrase);
|
|
152
|
-
const text = new TextDecoder().decode(decryptedBytes);
|
|
153
|
-
try {
|
|
154
|
-
return JSON.parse(text);
|
|
155
|
-
}
|
|
156
|
-
catch {
|
|
157
|
-
return text;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Purges all expired keys immediately.
|
|
162
|
-
*/
|
|
163
|
-
async pruneExpired() {
|
|
164
|
-
return store_js_1.KVStore.pruneExpired(this.db, this.tableName);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
exports.KVManager = KVManager;
|
package/dist/kv/store.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { DBExecutable } from '../core/schema-manager.js';
|
|
2
|
-
import { KVRecord } from './types.js';
|
|
3
|
-
export declare const DEFAULT_KV_TABLE = "_local_kv";
|
|
4
|
-
export declare function mapKVRow(r: any): KVRecord;
|
|
5
|
-
export declare class KVStore {
|
|
6
|
-
static ensureSchema(db: DBExecutable, tableName?: string): Promise<void>;
|
|
7
|
-
static set(db: DBExecutable, tableName: string | undefined, key: string, valueStr: string, type: string, now: number, expiresAt: number | null): Promise<void>;
|
|
8
|
-
static get(db: DBExecutable, tableName: string | undefined, key: string): Promise<KVRecord | null>;
|
|
9
|
-
static delete(db: DBExecutable, tableName: string | undefined, key: string): Promise<boolean>;
|
|
10
|
-
static listKeys(db: DBExecutable, tableName?: string, prefix?: string): Promise<string[]>;
|
|
11
|
-
static clear(db: DBExecutable, tableName?: string): Promise<void>;
|
|
12
|
-
static pruneExpired(db: DBExecutable, tableName?: string): Promise<number>;
|
|
13
|
-
}
|
package/dist/kv/store.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.KVStore = exports.DEFAULT_KV_TABLE = void 0;
|
|
4
|
-
exports.mapKVRow = mapKVRow;
|
|
5
|
-
exports.DEFAULT_KV_TABLE = '_local_kv';
|
|
6
|
-
function mapKVRow(r) {
|
|
7
|
-
let val = r.value;
|
|
8
|
-
const type = r.type;
|
|
9
|
-
if (type === 'number') {
|
|
10
|
-
val = Number(val);
|
|
11
|
-
}
|
|
12
|
-
else if (type === 'boolean') {
|
|
13
|
-
val = val === 'true' || val === '1' || val === 1;
|
|
14
|
-
}
|
|
15
|
-
else if (type === 'json') {
|
|
16
|
-
try {
|
|
17
|
-
val = JSON.parse(val);
|
|
18
|
-
}
|
|
19
|
-
catch {
|
|
20
|
-
// fallback
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return {
|
|
24
|
-
key: r.key,
|
|
25
|
-
value: val,
|
|
26
|
-
type: r.type,
|
|
27
|
-
createdAt: Number(r.created_at || 0),
|
|
28
|
-
updatedAt: Number(r.updated_at || 0),
|
|
29
|
-
expiresAt: r.expires_at != null ? Number(r.expires_at) : null,
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
class KVStore {
|
|
33
|
-
static async ensureSchema(db, tableName = exports.DEFAULT_KV_TABLE) {
|
|
34
|
-
await db.execute(`CREATE TABLE IF NOT EXISTS ${tableName} (
|
|
35
|
-
key TEXT PRIMARY KEY,
|
|
36
|
-
value TEXT NOT NULL,
|
|
37
|
-
type TEXT NOT NULL,
|
|
38
|
-
created_at INTEGER NOT NULL,
|
|
39
|
-
updated_at INTEGER NOT NULL,
|
|
40
|
-
expires_at INTEGER
|
|
41
|
-
);`);
|
|
42
|
-
await db.execute(`CREATE INDEX IF NOT EXISTS idx_${tableName}_expires ON ${tableName} (expires_at);`);
|
|
43
|
-
}
|
|
44
|
-
static async set(db, tableName = exports.DEFAULT_KV_TABLE, key, valueStr, type, now, expiresAt) {
|
|
45
|
-
await db.execute(`INSERT INTO ${tableName} (key, value, type, created_at, updated_at, expires_at)
|
|
46
|
-
VALUES (?, ?, ?, ?, ?, ?)
|
|
47
|
-
ON CONFLICT(key) DO UPDATE SET
|
|
48
|
-
value = excluded.value,
|
|
49
|
-
type = excluded.type,
|
|
50
|
-
updated_at = excluded.updated_at,
|
|
51
|
-
expires_at = excluded.expires_at;`, [key, valueStr, type, now, now, expiresAt]);
|
|
52
|
-
}
|
|
53
|
-
static async get(db, tableName = exports.DEFAULT_KV_TABLE, key) {
|
|
54
|
-
const rows = await db.execute(`SELECT * FROM ${tableName} WHERE key = ? LIMIT 1;`, [key]);
|
|
55
|
-
if (!rows || rows.length === 0)
|
|
56
|
-
return null;
|
|
57
|
-
return mapKVRow(rows[0]);
|
|
58
|
-
}
|
|
59
|
-
static async delete(db, tableName = exports.DEFAULT_KV_TABLE, key) {
|
|
60
|
-
await db.execute(`DELETE FROM ${tableName} WHERE key = ?;`, [key]);
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
|
-
static async listKeys(db, tableName = exports.DEFAULT_KV_TABLE, prefix) {
|
|
64
|
-
let rows;
|
|
65
|
-
if (prefix) {
|
|
66
|
-
rows = await db.execute(`SELECT key, expires_at FROM ${tableName} WHERE key LIKE ? ORDER BY key ASC;`, [`${prefix}%`]);
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
rows = await db.execute(`SELECT key, expires_at FROM ${tableName} ORDER BY key ASC;`);
|
|
70
|
-
}
|
|
71
|
-
const now = Date.now();
|
|
72
|
-
return (rows || [])
|
|
73
|
-
.filter((r) => r.expires_at == null || Number(r.expires_at) > now)
|
|
74
|
-
.map((r) => r.key);
|
|
75
|
-
}
|
|
76
|
-
static async clear(db, tableName = exports.DEFAULT_KV_TABLE) {
|
|
77
|
-
await db.execute(`DELETE FROM ${tableName};`);
|
|
78
|
-
}
|
|
79
|
-
static async pruneExpired(db, tableName = exports.DEFAULT_KV_TABLE) {
|
|
80
|
-
const now = Date.now();
|
|
81
|
-
await db.execute(`DELETE FROM ${tableName} WHERE expires_at IS NOT NULL AND expires_at <= ?;`, [now]);
|
|
82
|
-
return 0;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
exports.KVStore = KVStore;
|
package/dist/kv/types.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export interface KVSetOptions {
|
|
2
|
-
/**
|
|
3
|
-
* Time-to-live in milliseconds from current time.
|
|
4
|
-
*/
|
|
5
|
-
ttlMs?: number;
|
|
6
|
-
/**
|
|
7
|
-
* Explicit Unix timestamp in milliseconds when this key expires.
|
|
8
|
-
*/
|
|
9
|
-
expiresAt?: number;
|
|
10
|
-
}
|
|
11
|
-
export interface KVSetSecretOptions extends KVSetOptions {
|
|
12
|
-
/**
|
|
13
|
-
* Passphrase used to encrypt the value using AES-256-GCM.
|
|
14
|
-
*/
|
|
15
|
-
passphrase: string;
|
|
16
|
-
}
|
|
17
|
-
export interface KVRecord<T = any> {
|
|
18
|
-
key: string;
|
|
19
|
-
value: T;
|
|
20
|
-
type: 'string' | 'number' | 'boolean' | 'json' | 'encrypted';
|
|
21
|
-
createdAt: number;
|
|
22
|
-
updatedAt: number;
|
|
23
|
-
expiresAt?: number | null;
|
|
24
|
-
}
|
|
25
|
-
export interface KVStoreOptions {
|
|
26
|
-
tableName?: string;
|
|
27
|
-
}
|
package/dist/kv/types.js
DELETED
package/dist/react/kv.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { LocalFirstClient } from '../core/client.js';
|
|
2
|
-
/**
|
|
3
|
-
* Reactive React hook for client.kv key-value preferences.
|
|
4
|
-
* Automatically re-renders whenever the key is updated or deleted.
|
|
5
|
-
*
|
|
6
|
-
* Example:
|
|
7
|
-
* const [theme, setTheme, loading] = useKV(client, 'theme', 'light');
|
|
8
|
-
*/
|
|
9
|
-
export declare function useKV<T = any>(client: LocalFirstClient | null | undefined, key: string, defaultValue?: T): [T | undefined, (value: T) => Promise<void>, boolean];
|