local-lite 2.0.4 → 2.0.6
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 +2 -2
- package/bin/local-lite.mjs +215 -2
- package/dist/auth/apple.js +1 -1
- package/dist/auth/github.js +1 -1
- package/dist/auth/google.js +1 -12
- package/dist/auth/local.js +2 -25
- package/dist/auth/microsoft.js +1 -12
- package/dist/backups/cloud-storage.js +1 -12
- package/dist/core/client.d.ts +3 -3
- package/dist/core/client.js +6 -6
- package/dist/devtools/BenchmarkModal.js +16 -37
- package/dist/devtools/SyncInspector.js +11 -43
- package/dist/devtools/shared.d.ts +25 -0
- package/dist/devtools/shared.js +39 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/kv-store/index.d.ts +3 -0
- package/dist/kv-store/index.js +19 -0
- package/dist/kv-store/manager.d.ts +52 -0
- package/dist/kv-store/manager.js +167 -0
- package/dist/kv-store/store.d.ts +13 -0
- package/dist/kv-store/store.js +85 -0
- package/dist/kv-store/types.d.ts +27 -0
- package/dist/kv-store/types.js +2 -0
- package/dist/react/index.d.ts +1 -1
- package/dist/react/index.js +1 -1
- package/dist/react/kv-store.d.ts +9 -0
- package/dist/react/kv-store.js +53 -0
- package/dist/schema/index.d.ts +0 -1
- package/dist/schema/index.js +0 -1
- package/dist/server/dashboard.d.ts +16 -0
- package/dist/server/dashboard.js +1364 -0
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +1 -0
- package/dist/utils/oauth.d.ts +9 -0
- package/dist/utils/oauth.js +18 -0
- package/package.json +10 -5
- package/skills/local-lite-offline-actions/SKILL.md +11 -11
- package/skills/local-lite-react/SKILL.md +7 -7
|
@@ -6,20 +6,7 @@ const react_1 = require("react");
|
|
|
6
6
|
const status_js_1 = require("../react/status.js");
|
|
7
7
|
const BenchmarkModal_js_1 = require("./BenchmarkModal.js");
|
|
8
8
|
const theme_js_1 = require("../react/theme.js");
|
|
9
|
-
|
|
10
|
-
try {
|
|
11
|
-
RN = require('react-native');
|
|
12
|
-
}
|
|
13
|
-
catch {
|
|
14
|
-
// Web / DOM environment
|
|
15
|
-
}
|
|
16
|
-
let RNSafeArea;
|
|
17
|
-
try {
|
|
18
|
-
RNSafeArea = require('react-native-safe-area-context');
|
|
19
|
-
}
|
|
20
|
-
catch {
|
|
21
|
-
// Web / DOM environment or not installed
|
|
22
|
-
}
|
|
9
|
+
const shared_js_1 = require("./shared.js");
|
|
23
10
|
const BENCHMARK_DESCRIPTION = 'Runs automated stress tests directly validating local-lite client APIs: atomic batch ingestion, indexing, ACID rollback isolation, outbox queues, and non-blocking WAL concurrency.';
|
|
24
11
|
/**
|
|
25
12
|
* Universal Developer Inspector Component for local-lite.
|
|
@@ -32,7 +19,7 @@ function SyncInspector({ client, position = 'bottom-right', variant = 'pill', in
|
|
|
32
19
|
const status = (0, status_js_1.useSyncStatus)(client);
|
|
33
20
|
const [metrics, setMetrics] = (0, react_1.useState)(null);
|
|
34
21
|
const [actionMessage, setActionMessage] = (0, react_1.useState)(null);
|
|
35
|
-
const systemColorScheme = RN?.useColorScheme ? RN.useColorScheme() : null;
|
|
22
|
+
const systemColorScheme = shared_js_1.RN?.useColorScheme ? shared_js_1.RN.useColorScheme() : null;
|
|
36
23
|
const currentTheme = (0, react_1.useMemo)(() => (0, theme_js_1.resolveSyncTheme)(theme, systemColorScheme), [theme, systemColorScheme]);
|
|
37
24
|
(0, react_1.useEffect)(() => {
|
|
38
25
|
if (!client || !isOpen)
|
|
@@ -83,8 +70,8 @@ function SyncInspector({ client, position = 'bottom-right', variant = 'pill', in
|
|
|
83
70
|
}
|
|
84
71
|
});
|
|
85
72
|
const message = 'This permanently removes queued pending, processing, and failed mutations. Local database records are not deleted.';
|
|
86
|
-
if (RN?.Alert?.alert) {
|
|
87
|
-
RN.Alert.alert('Clear stuck outbox?', message, [
|
|
73
|
+
if (shared_js_1.RN?.Alert?.alert) {
|
|
74
|
+
shared_js_1.RN.Alert.alert('Clear stuck outbox?', message, [
|
|
88
75
|
{ text: 'Cancel', style: 'cancel' },
|
|
89
76
|
{
|
|
90
77
|
text: 'Clear outbox',
|
|
@@ -206,9 +193,9 @@ function SyncInspector({ client, position = 'bottom-right', variant = 'pill', in
|
|
|
206
193
|
if (!client)
|
|
207
194
|
return null;
|
|
208
195
|
// React Native Renderer
|
|
209
|
-
if (RN && RN.View) {
|
|
210
|
-
const { View, Text, TouchableOpacity, ScrollView, Modal, Platform, StatusBar, } = RN;
|
|
211
|
-
const SafeComp = RNSafeArea?.SafeAreaView || RN.SafeAreaView || View;
|
|
196
|
+
if (shared_js_1.RN && shared_js_1.RN.View) {
|
|
197
|
+
const { View, Text, TouchableOpacity, ScrollView, Modal, Platform, StatusBar, } = shared_js_1.RN;
|
|
198
|
+
const SafeComp = shared_js_1.RNSafeArea?.SafeAreaView || shared_js_1.RN.SafeAreaView || View;
|
|
212
199
|
const isAndroid = Platform?.OS === 'android';
|
|
213
200
|
const statusBarHeight = isAndroid ? StatusBar?.currentHeight || 24 : 0;
|
|
214
201
|
const pillPosStyle = isInline
|
|
@@ -257,7 +244,7 @@ function SyncInspector({ client, position = 'bottom-right', variant = 'pill', in
|
|
|
257
244
|
backgroundColor: currentTheme.surface,
|
|
258
245
|
borderBottomColor: currentTheme.border,
|
|
259
246
|
},
|
|
260
|
-
!RNSafeArea?.SafeAreaView &&
|
|
247
|
+
!shared_js_1.RNSafeArea?.SafeAreaView &&
|
|
261
248
|
isAndroid && { paddingTop: statusBarHeight + 10 },
|
|
262
249
|
], children: [(0, jsx_runtime_1.jsxs)(View, { style: { flex: 1 }, children: [(0, jsx_runtime_1.jsx)(Text, { style: [rnStyles.topBarTitle, { color: currentTheme.text }], children: title }), (0, jsx_runtime_1.jsx)(Text, { style: [
|
|
263
250
|
rnStyles.topBarSub,
|
|
@@ -616,28 +603,9 @@ function SyncInspector({ client, position = 'bottom-right', variant = 'pill', in
|
|
|
616
603
|
fontWeight: 700,
|
|
617
604
|
}, children: status.pendingCount }))] })) })), enableBenchmark && !onOpenBenchmark && ((0, jsx_runtime_1.jsx)(BenchmarkModal_js_1.BenchmarkModal, { client: client, visible: isBenchmarkModalOpen, onDismiss: () => setIsBenchmarkModalOpen(false), theme: theme }))] }));
|
|
618
605
|
}
|
|
619
|
-
const rnStyles = RN && RN.StyleSheet
|
|
620
|
-
? RN.StyleSheet.create({
|
|
621
|
-
|
|
622
|
-
flex: 1,
|
|
623
|
-
height: '100%',
|
|
624
|
-
},
|
|
625
|
-
topBar: {
|
|
626
|
-
flexDirection: 'row',
|
|
627
|
-
alignItems: 'center',
|
|
628
|
-
justifyContent: 'space-between',
|
|
629
|
-
paddingHorizontal: 18,
|
|
630
|
-
paddingVertical: 14,
|
|
631
|
-
borderBottomWidth: 1,
|
|
632
|
-
},
|
|
633
|
-
topBarTitle: {
|
|
634
|
-
fontSize: 17,
|
|
635
|
-
fontWeight: '700',
|
|
636
|
-
},
|
|
637
|
-
topBarSub: {
|
|
638
|
-
fontSize: 11,
|
|
639
|
-
marginTop: 2,
|
|
640
|
-
},
|
|
606
|
+
const rnStyles = shared_js_1.RN && shared_js_1.RN.StyleSheet
|
|
607
|
+
? shared_js_1.RN.StyleSheet.create({
|
|
608
|
+
...shared_js_1.devtoolsBaseRNStyles,
|
|
641
609
|
statusBadge: {
|
|
642
610
|
paddingHorizontal: 8,
|
|
643
611
|
paddingVertical: 3,
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
declare let RN: any;
|
|
2
|
+
declare let RNSafeArea: any;
|
|
3
|
+
export { RN, RNSafeArea };
|
|
4
|
+
export declare const devtoolsBaseRNStyles: {
|
|
5
|
+
fullScreenSafe: {
|
|
6
|
+
flex: number;
|
|
7
|
+
height: string;
|
|
8
|
+
};
|
|
9
|
+
topBar: {
|
|
10
|
+
flexDirection: 'row';
|
|
11
|
+
alignItems: 'center';
|
|
12
|
+
justifyContent: 'space-between';
|
|
13
|
+
paddingHorizontal: number;
|
|
14
|
+
paddingVertical: number;
|
|
15
|
+
borderBottomWidth: number;
|
|
16
|
+
};
|
|
17
|
+
topBarTitle: {
|
|
18
|
+
fontSize: number;
|
|
19
|
+
fontWeight: '700';
|
|
20
|
+
};
|
|
21
|
+
topBarSub: {
|
|
22
|
+
fontSize: number;
|
|
23
|
+
marginTop: number;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.devtoolsBaseRNStyles = exports.RNSafeArea = exports.RN = void 0;
|
|
4
|
+
let RN;
|
|
5
|
+
try {
|
|
6
|
+
exports.RN = RN = require('react-native');
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
// Web / DOM environment
|
|
10
|
+
}
|
|
11
|
+
let RNSafeArea;
|
|
12
|
+
try {
|
|
13
|
+
exports.RNSafeArea = RNSafeArea = require('react-native-safe-area-context');
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
// Web / DOM environment or not installed
|
|
17
|
+
}
|
|
18
|
+
exports.devtoolsBaseRNStyles = {
|
|
19
|
+
fullScreenSafe: {
|
|
20
|
+
flex: 1,
|
|
21
|
+
height: '100%',
|
|
22
|
+
},
|
|
23
|
+
topBar: {
|
|
24
|
+
flexDirection: 'row',
|
|
25
|
+
alignItems: 'center',
|
|
26
|
+
justifyContent: 'space-between',
|
|
27
|
+
paddingHorizontal: 18,
|
|
28
|
+
paddingVertical: 14,
|
|
29
|
+
borderBottomWidth: 1,
|
|
30
|
+
},
|
|
31
|
+
topBarTitle: {
|
|
32
|
+
fontSize: 17,
|
|
33
|
+
fontWeight: '700',
|
|
34
|
+
},
|
|
35
|
+
topBarSub: {
|
|
36
|
+
fontSize: 11,
|
|
37
|
+
marginTop: 2,
|
|
38
|
+
},
|
|
39
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ export * from './auth/index.js';
|
|
|
24
24
|
export * from './backups/index.js';
|
|
25
25
|
export * from './actions/index.js';
|
|
26
26
|
export * from './keys/index.js';
|
|
27
|
-
export * from './kv/index.js';
|
|
27
|
+
export * from './kv-store/index.js';
|
|
28
28
|
export * from './search/index.js';
|
|
29
29
|
export * from './storage/index.js';
|
|
30
30
|
export * from './scheduler/index.js';
|
package/dist/index.js
CHANGED
|
@@ -40,7 +40,7 @@ __exportStar(require("./auth/index.js"), exports);
|
|
|
40
40
|
__exportStar(require("./backups/index.js"), exports);
|
|
41
41
|
__exportStar(require("./actions/index.js"), exports);
|
|
42
42
|
__exportStar(require("./keys/index.js"), exports);
|
|
43
|
-
__exportStar(require("./kv/index.js"), exports);
|
|
43
|
+
__exportStar(require("./kv-store/index.js"), exports);
|
|
44
44
|
__exportStar(require("./search/index.js"), exports);
|
|
45
45
|
__exportStar(require("./storage/index.js"), exports);
|
|
46
46
|
__exportStar(require("./scheduler/index.js"), exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
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);
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { DBExecutable } from '../core/schema-manager.js';
|
|
2
|
+
import { EventEmitter } from '../reactive/event-emitter.js';
|
|
3
|
+
import { KVStoreRecord, KVStoreSetOptions, KVStoreSetSecretOptions, KVStoreOptions } from './types.js';
|
|
4
|
+
export declare class KVStoreManager {
|
|
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?: KVStoreSetOptions): 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 KVStoreRecord metadata for a key.
|
|
21
|
+
*/
|
|
22
|
+
getRecord(key: string): Promise<KVStoreRecord | 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: KVStoreSetSecretOptions): 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
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KVStoreManager = 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 KVStoreManager {
|
|
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_STORE_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 KVStoreRecord 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.KVStoreManager = KVStoreManager;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { DBExecutable } from '../core/schema-manager.js';
|
|
2
|
+
import { KVStoreRecord } from './types.js';
|
|
3
|
+
export declare const DEFAULT_KV_STORE_TABLE = "_local_kv_store";
|
|
4
|
+
export declare function mapKVStoreRow(r: any): KVStoreRecord;
|
|
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<KVStoreRecord | 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
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KVStore = exports.DEFAULT_KV_STORE_TABLE = void 0;
|
|
4
|
+
exports.mapKVStoreRow = mapKVStoreRow;
|
|
5
|
+
exports.DEFAULT_KV_STORE_TABLE = '_local_kv_store';
|
|
6
|
+
function mapKVStoreRow(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_STORE_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_STORE_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_STORE_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 mapKVStoreRow(rows[0]);
|
|
58
|
+
}
|
|
59
|
+
static async delete(db, tableName = exports.DEFAULT_KV_STORE_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_STORE_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_STORE_TABLE) {
|
|
77
|
+
await db.execute(`DELETE FROM ${tableName};`);
|
|
78
|
+
}
|
|
79
|
+
static async pruneExpired(db, tableName = exports.DEFAULT_KV_STORE_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;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface KVStoreSetOptions {
|
|
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 KVStoreSetSecretOptions extends KVStoreSetOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Passphrase used to encrypt the value using AES-256-GCM.
|
|
14
|
+
*/
|
|
15
|
+
passphrase: string;
|
|
16
|
+
}
|
|
17
|
+
export interface KVStoreRecord<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/react/index.d.ts
CHANGED
|
@@ -128,6 +128,6 @@ export * from './status.js';
|
|
|
128
128
|
export * from './theme.js';
|
|
129
129
|
export * from './auth.js';
|
|
130
130
|
export * from './backups.js';
|
|
131
|
-
export * from './kv.js';
|
|
131
|
+
export * from './kv-store.js';
|
|
132
132
|
export * from './search.js';
|
|
133
133
|
export * from './session.js';
|
package/dist/react/index.js
CHANGED
|
@@ -699,6 +699,6 @@ __exportStar(require("./status.js"), exports);
|
|
|
699
699
|
__exportStar(require("./theme.js"), exports);
|
|
700
700
|
__exportStar(require("./auth.js"), exports);
|
|
701
701
|
__exportStar(require("./backups.js"), exports);
|
|
702
|
-
__exportStar(require("./kv.js"), exports);
|
|
702
|
+
__exportStar(require("./kv-store.js"), exports);
|
|
703
703
|
__exportStar(require("./search.js"), exports);
|
|
704
704
|
__exportStar(require("./session.js"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { LocalFirstClient } from '../core/client.js';
|
|
2
|
+
/**
|
|
3
|
+
* Reactive React hook for client.kvStore key-value preferences.
|
|
4
|
+
* Automatically re-renders whenever the key is updated or deleted.
|
|
5
|
+
*
|
|
6
|
+
* Example:
|
|
7
|
+
* const [theme, setTheme, loading] = useKVStore(client, 'theme', 'light');
|
|
8
|
+
*/
|
|
9
|
+
export declare function useKVStore<T = any>(client: LocalFirstClient | null | undefined, key: string, defaultValue?: T): [T | undefined, (value: T) => Promise<void>, boolean];
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useKVStore = useKVStore;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
/**
|
|
6
|
+
* Reactive React hook for client.kvStore key-value preferences.
|
|
7
|
+
* Automatically re-renders whenever the key is updated or deleted.
|
|
8
|
+
*
|
|
9
|
+
* Example:
|
|
10
|
+
* const [theme, setTheme, loading] = useKVStore(client, 'theme', 'light');
|
|
11
|
+
*/
|
|
12
|
+
function useKVStore(client, key, defaultValue) {
|
|
13
|
+
const [value, setValueState] = (0, react_1.useState)(defaultValue);
|
|
14
|
+
const [loading, setLoading] = (0, react_1.useState)(true);
|
|
15
|
+
(0, react_1.useEffect)(() => {
|
|
16
|
+
if (!client) {
|
|
17
|
+
setValueState(defaultValue);
|
|
18
|
+
setLoading(false);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
let isMounted = true;
|
|
22
|
+
const load = async () => {
|
|
23
|
+
try {
|
|
24
|
+
const val = await client.kvStore.get(key, defaultValue);
|
|
25
|
+
if (isMounted) {
|
|
26
|
+
setValueState(val);
|
|
27
|
+
setLoading(false);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
if (isMounted)
|
|
32
|
+
setLoading(false);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
load();
|
|
36
|
+
const unsubscribe = client.events.on('_local_kv_store', (e) => {
|
|
37
|
+
if (!e || e.key === key || !e.key) {
|
|
38
|
+
load();
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return () => {
|
|
42
|
+
isMounted = false;
|
|
43
|
+
unsubscribe();
|
|
44
|
+
};
|
|
45
|
+
}, [client, key, defaultValue]);
|
|
46
|
+
const setKVStoreValue = (0, react_1.useCallback)(async (newValue) => {
|
|
47
|
+
if (!client)
|
|
48
|
+
return;
|
|
49
|
+
await client.kvStore.set(key, newValue);
|
|
50
|
+
setValueState(newValue);
|
|
51
|
+
}, [client, key]);
|
|
52
|
+
return [value, setKVStoreValue, loading];
|
|
53
|
+
}
|
package/dist/schema/index.d.ts
CHANGED
package/dist/schema/index.js
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { LocalDBAdapter } from '../adapters/local/index.js';
|
|
2
|
+
export interface DashboardHandlerOptions {
|
|
3
|
+
/** Display title in the dashboard header. */
|
|
4
|
+
title?: string;
|
|
5
|
+
/** Disable mutations and mutating SQL queries. Defaults to false. */
|
|
6
|
+
readOnly?: boolean;
|
|
7
|
+
/** Optional bearer/query token required to access the dashboard and its APIs. */
|
|
8
|
+
authToken?: string;
|
|
9
|
+
/** Base path if mounted under a sub-path (e.g. '/_dashboard'). Defaults to ''. */
|
|
10
|
+
basePath?: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Creates a Web Standard Request/Response handler for the local-lite dashboard.
|
|
14
|
+
* Can be mounted on any Node, Bun, Deno, or Edge server, or launched via `local-lite dashboard`.
|
|
15
|
+
*/
|
|
16
|
+
export declare function createDashboardHandler(db: LocalDBAdapter, options?: DashboardHandlerOptions): (request: Request) => Promise<Response>;
|