flash-store 2.0.1 → 2.1.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 +92 -19
- package/dist/package.json +90 -61
- package/dist/src/browser.d.ts +20 -0
- package/dist/src/browser.d.ts.map +1 -0
- package/dist/src/browser.js +24 -0
- package/dist/src/browser.js.map +1 -0
- package/dist/src/config.d.ts +4 -0
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +4 -11
- package/dist/src/config.js.map +1 -1
- package/dist/src/flash-store-core.d.ts +65 -0
- package/dist/src/flash-store-core.d.ts.map +1 -0
- package/dist/src/flash-store-core.js +201 -0
- package/dist/src/flash-store-core.js.map +1 -0
- package/dist/src/flash-store-sync-core.d.ts +72 -0
- package/dist/src/flash-store-sync-core.d.ts.map +1 -0
- package/dist/src/flash-store-sync-core.js +177 -0
- package/dist/src/flash-store-sync-core.js.map +1 -0
- package/dist/src/flash-store-sync.d.ts +5 -66
- package/dist/src/flash-store-sync.d.ts.map +1 -1
- package/dist/src/flash-store-sync.js +7 -171
- package/dist/src/flash-store-sync.js.map +1 -1
- package/dist/src/flash-store.d.ts +5 -63
- package/dist/src/flash-store.d.ts.map +1 -1
- package/dist/src/flash-store.js +70 -208
- package/dist/src/flash-store.js.map +1 -1
- package/dist/src/indexeddb-backend.d.ts +40 -0
- package/dist/src/indexeddb-backend.d.ts.map +1 -0
- package/dist/src/indexeddb-backend.js +200 -0
- package/dist/src/indexeddb-backend.js.map +1 -0
- package/dist/src/key-encoding.d.ts +9 -0
- package/dist/src/key-encoding.d.ts.map +1 -0
- package/dist/src/key-encoding.js +22 -0
- package/dist/src/key-encoding.js.map +1 -0
- package/dist/src/store-backend.d.ts +35 -0
- package/dist/src/store-backend.d.ts.map +1 -0
- package/dist/src/store-backend.js +2 -0
- package/dist/src/store-backend.js.map +1 -0
- package/package.json +38 -9
|
@@ -1,177 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* FlashStore owns persistence; ready() is the boundary for observing write failures.
|
|
4
|
-
* Keys and values are validated and serialized when a mutation is accepted, so
|
|
5
|
-
* invalid input throws synchronously and later object changes are not persisted.
|
|
2
|
+
* Binds the shared synchronous Map cache to the native LMDB-backed store.
|
|
6
3
|
*/
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* Restored entries follow LMDB key order rather than their original insertion order.
|
|
13
|
-
*/
|
|
14
|
-
export class FlashStoreSync {
|
|
15
|
-
static VERSION = FlashStore.VERSION;
|
|
16
|
-
workdir;
|
|
17
|
-
cacheMap = new Map();
|
|
18
|
-
flashStore;
|
|
19
|
-
touched = new Set();
|
|
20
|
-
loading = true;
|
|
21
|
-
clearedDuringLoad = false;
|
|
22
|
-
pending;
|
|
23
|
-
closing = false;
|
|
24
|
-
failure;
|
|
25
|
-
/** Opens the backing store and starts restoring its cache. */
|
|
4
|
+
import { FlashStore } from './flash-store.js';
|
|
5
|
+
import { FlashStoreSyncCore } from './flash-store-sync-core.js';
|
|
6
|
+
/** In-memory Map with ordered write-back to LMDB. */
|
|
7
|
+
export class FlashStoreSync extends FlashStoreSyncCore {
|
|
8
|
+
/** Opens the backing path and starts restoring its cache. Await ready() before reading restored data. */
|
|
26
9
|
constructor(workdir = '.flash-store-sync') {
|
|
27
|
-
|
|
28
|
-
this.workdir = workdir;
|
|
29
|
-
this.flashStore = new FlashStore(workdir);
|
|
30
|
-
this.pending = this.load();
|
|
31
|
-
this.observeFailure();
|
|
32
|
-
}
|
|
33
|
-
/** Restore untouched keys while immediate cache mutations remain authoritative. */
|
|
34
|
-
async load() {
|
|
35
|
-
try {
|
|
36
|
-
for await (const [key, value] of this.flashStore) {
|
|
37
|
-
if (!this.clearedDuringLoad && !this.touched.has(key))
|
|
38
|
-
this.cacheMap.set(key, value);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
finally {
|
|
42
|
-
this.loading = false;
|
|
43
|
-
this.touched.clear();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
/** Serialize writes behind initialization; reject mutations after a detected failure. */
|
|
47
|
-
enqueue(action) {
|
|
48
|
-
if (this.closing)
|
|
49
|
-
throw new Error('Store is closing or closed');
|
|
50
|
-
if (this.failure)
|
|
51
|
-
throw this.failure.error;
|
|
52
|
-
this.pending = this.pending.then(async () => {
|
|
53
|
-
await action();
|
|
54
|
-
});
|
|
55
|
-
this.observeFailure();
|
|
56
|
-
}
|
|
57
|
-
/** Report background errors once while retaining the rejection for lifecycle calls. */
|
|
58
|
-
observeFailure() {
|
|
59
|
-
void this.pending.catch((error) => {
|
|
60
|
-
if (!this.failure) {
|
|
61
|
-
this.failure = { error };
|
|
62
|
-
log.error('CacheStore', 'Background operation failed: %s', error);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
/** Returns the package version. */
|
|
67
|
-
version() {
|
|
68
|
-
return this.flashStore.version();
|
|
69
|
-
}
|
|
70
|
-
/** Waits for initialization and writes queued before this call, rejecting on failure. */
|
|
71
|
-
async ready() {
|
|
72
|
-
await this.pending;
|
|
73
|
-
}
|
|
74
|
-
/** Waits for writes and closes the backing store, preserving write and close failures. */
|
|
75
|
-
async close() {
|
|
76
|
-
this.closing = true;
|
|
77
|
-
try {
|
|
78
|
-
await this.ready();
|
|
79
|
-
}
|
|
80
|
-
catch (error) {
|
|
81
|
-
try {
|
|
82
|
-
await this.flashStore.close();
|
|
83
|
-
}
|
|
84
|
-
catch (closeError) {
|
|
85
|
-
throw new AggregateError([error, closeError], 'Write and close failed');
|
|
86
|
-
}
|
|
87
|
-
throw error;
|
|
88
|
-
}
|
|
89
|
-
await this.flashStore.close();
|
|
90
|
-
}
|
|
91
|
-
/** Closes even on write failure; removes the directory only after successful writes. */
|
|
92
|
-
async destroy() {
|
|
93
|
-
await this.close();
|
|
94
|
-
await this.flashStore.destroy();
|
|
95
|
-
}
|
|
96
|
-
/** Returns the current number of cached entries. */
|
|
97
|
-
get size() {
|
|
98
|
-
return this.cacheMap.size;
|
|
99
|
-
}
|
|
100
|
-
/** Identifies this object as a Map. */
|
|
101
|
-
get [Symbol.toStringTag]() {
|
|
102
|
-
return 'Map';
|
|
103
|
-
}
|
|
104
|
-
/** Iterates cached entries in insertion order. */
|
|
105
|
-
[Symbol.iterator]() {
|
|
106
|
-
return this.cacheMap[Symbol.iterator]();
|
|
107
|
-
}
|
|
108
|
-
/** Iterates cached entries in insertion order. */
|
|
109
|
-
entries() {
|
|
110
|
-
return this.cacheMap.entries();
|
|
111
|
-
}
|
|
112
|
-
/** Iterates cached keys in insertion order. */
|
|
113
|
-
keys() {
|
|
114
|
-
return this.cacheMap.keys();
|
|
115
|
-
}
|
|
116
|
-
/** Iterates cached values in insertion order. */
|
|
117
|
-
values() {
|
|
118
|
-
return this.cacheMap.values();
|
|
119
|
-
}
|
|
120
|
-
/** Clears the cache immediately and queues persistent removal. */
|
|
121
|
-
clear() {
|
|
122
|
-
this.enqueue(() => this.flashStore.clear());
|
|
123
|
-
if (this.loading)
|
|
124
|
-
this.clearedDuringLoad = true;
|
|
125
|
-
this.cacheMap.clear();
|
|
126
|
-
}
|
|
127
|
-
/** Removes a cached entry immediately and queues persistent removal. */
|
|
128
|
-
delete(key) {
|
|
129
|
-
validateKey(key);
|
|
130
|
-
this.enqueue(() => this.flashStore.delete(key));
|
|
131
|
-
if (this.loading)
|
|
132
|
-
this.touched.add(key);
|
|
133
|
-
return this.cacheMap.delete(key);
|
|
134
|
-
}
|
|
135
|
-
/** Invokes the callback for each cached entry. */
|
|
136
|
-
forEach(callbackfn, thisArg) {
|
|
137
|
-
for (const [key, value] of this.cacheMap)
|
|
138
|
-
callbackfn.call(thisArg, value, key, this);
|
|
139
|
-
}
|
|
140
|
-
/** Reads a cached value; await ready() first to include previously persisted data. */
|
|
141
|
-
get(key) {
|
|
142
|
-
return this.cacheMap.get(key);
|
|
143
|
-
}
|
|
144
|
-
/** Checks the cache; await ready() first to include previously persisted data. */
|
|
145
|
-
has(key) {
|
|
146
|
-
return this.cacheMap.has(key);
|
|
147
|
-
}
|
|
148
|
-
/** Returns a cached value or inserts and queues the supplied default. */
|
|
149
|
-
getOrInsert(key, defaultValue) {
|
|
150
|
-
if (this.cacheMap.has(key))
|
|
151
|
-
return this.cacheMap.get(key);
|
|
152
|
-
this.set(key, defaultValue);
|
|
153
|
-
return defaultValue;
|
|
154
|
-
}
|
|
155
|
-
/** Returns a cached value or computes, inserts, and queues a new value. */
|
|
156
|
-
getOrInsertComputed(key, callback) {
|
|
157
|
-
if (this.cacheMap.has(key))
|
|
158
|
-
return this.cacheMap.get(key);
|
|
159
|
-
const value = callback(key);
|
|
160
|
-
this.set(key, value);
|
|
161
|
-
return value;
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Updates the cache immediately and queues an ordered persistent write of the
|
|
165
|
-
* value's JSON at call time. Throws without changes for invalid keys or values.
|
|
166
|
-
*/
|
|
167
|
-
set(key, value) {
|
|
168
|
-
validateKey(key);
|
|
169
|
-
const serialized = serialize(value);
|
|
170
|
-
this.enqueue(() => writeSerialized(this.flashStore, key, serialized));
|
|
171
|
-
if (this.loading)
|
|
172
|
-
this.touched.add(key);
|
|
173
|
-
this.cacheMap.set(key, value);
|
|
174
|
-
return this;
|
|
10
|
+
super(workdir, new FlashStore(workdir));
|
|
175
11
|
}
|
|
176
12
|
}
|
|
177
13
|
export default FlashStoreSync;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flash-store-sync.js","sourceRoot":"","sources":["../../src/flash-store-sync.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"flash-store-sync.js","sourceRoot":"","sources":["../../src/flash-store-sync.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,UAAU,EAAiB,MAAM,kBAAkB,CAAA;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAA;AAE/D,qDAAqD;AACrD,MAAM,OAAO,cAGX,SAAQ,kBAAwB;IAChC,yGAAyG;IACzG,YAAY,OAAO,GAAG,mBAAmB;QACvC,KAAK,CAAC,OAAO,EAAE,IAAI,UAAU,CAAO,OAAO,CAAC,CAAC,CAAA;IAC/C,CAAC;CACF;AAED,eAAe,cAAc,CAAA"}
|
|
@@ -1,68 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
export type StoreKey
|
|
3
|
-
/**
|
|
4
|
-
export
|
|
5
|
-
gt?: K;
|
|
6
|
-
gte?: K;
|
|
7
|
-
lt?: K;
|
|
8
|
-
lte?: K;
|
|
9
|
-
reverse?: boolean;
|
|
10
|
-
limit?: number;
|
|
11
|
-
prefix?: string;
|
|
12
|
-
}
|
|
13
|
-
/** Reject keys that the configured ordered-binary encoding cannot represent safely. */
|
|
14
|
-
export declare function validateKey(key: StoreKey): void;
|
|
15
|
-
/** Serialize a value once, rejecting values that JSON cannot represent. */
|
|
16
|
-
export declare function serialize(value: unknown): string;
|
|
17
|
-
/**
|
|
18
|
-
* Writes pre-serialized JSON for FlashStoreSync, which serializes at call time.
|
|
19
|
-
* Internal: mod.ts does not export it, and package exports expose only mod.ts.
|
|
20
|
-
*/
|
|
21
|
-
export declare let writeSerialized: <K extends StoreKey>(store: FlashStore<K, unknown>, key: K, serialized: string) => Promise<void>;
|
|
22
|
-
/**
|
|
23
|
-
* Persistent key-value store. Values use JSON round-tripping, which may change their
|
|
24
|
-
* runtime type; the generic value type does not validate or prevent this.
|
|
25
|
-
*/
|
|
26
|
-
export declare class FlashStore<K extends StoreKey = StoreKey, V = unknown> {
|
|
27
|
-
static VERSION: string;
|
|
28
|
-
readonly workdir: string;
|
|
29
|
-
private readonly db;
|
|
30
|
-
private closePromise?;
|
|
1
|
+
import { FlashStoreCore, type StoreKey } from './flash-store-core.ts';
|
|
2
|
+
export type { IteratorOptions, StoreKey } from './flash-store-core.ts';
|
|
3
|
+
/** Persistent Map backed by LMDB with JSON-serialized values. */
|
|
4
|
+
export declare class FlashStore<K extends StoreKey = StoreKey, V = unknown> extends FlashStoreCore<K, V> {
|
|
31
5
|
/** Opens an LMDB database in the specified directory. */
|
|
32
6
|
constructor(workdir: string);
|
|
33
|
-
/**
|
|
34
|
-
version(): string;
|
|
35
|
-
/** Stores a JSON-serializable value and resolves after its write completes. */
|
|
36
|
-
set(key: K, value: V): Promise<this>;
|
|
37
|
-
/** Writes validated, serialized JSON and publishes the set event on success. */
|
|
38
|
-
private write;
|
|
39
|
-
/** Returns the stored value, or undefined when the key is absent. */
|
|
40
|
-
get(key: K): Promise<V | undefined>;
|
|
41
|
-
/** Returns whether the key exists, including when its value is falsey. */
|
|
42
|
-
has(key: K): Promise<boolean>;
|
|
43
|
-
/** Deletes a key and reports whether an entry existed. */
|
|
44
|
-
delete(key: K): Promise<boolean>;
|
|
45
|
-
/** Removes every entry after queued writes. */
|
|
46
|
-
clear(): Promise<void>;
|
|
47
|
-
/** Resolves to the number of entries in the current snapshot. */
|
|
48
|
-
get size(): Promise<number>;
|
|
49
|
-
/**
|
|
50
|
-
* Iterates a snapshot in LMDB key order, releasing the cursor on early exit.
|
|
51
|
-
* A prefix seeks to its contiguous key interval and stops on leaving it, so
|
|
52
|
-
* the cost depends on matching entries rather than database size.
|
|
53
|
-
*/
|
|
54
|
-
entries(options?: IteratorOptions<K>): AsyncIterableIterator<[K, V]>;
|
|
55
|
-
/** Iterates keys with the same range semantics as entries(). */
|
|
56
|
-
keys(options?: IteratorOptions<K>): AsyncIterableIterator<K>;
|
|
57
|
-
/** Iterates values with the same range semantics as entries(). */
|
|
58
|
-
values(options?: IteratorOptions<K>): AsyncIterableIterator<V>;
|
|
59
|
-
/** Returns the asynchronous entries iterator. */
|
|
60
|
-
[Symbol.asyncIterator](): AsyncIterableIterator<[K, V]>;
|
|
61
|
-
/** Awaits each callback in snapshot key order; stops and rejects on the first failure. */
|
|
62
|
-
forEach(callback: (value: V, key: K, store: this) => unknown, thisArg?: unknown): Promise<void>;
|
|
63
|
-
/** Closes the database after outstanding writes; repeated calls are safe. */
|
|
64
|
-
close(): Promise<void>;
|
|
65
|
-
/** Closes and removes the entire database directory. Never pass a shared directory. */
|
|
7
|
+
/** Refuses to close or remove protected locations. Use a dedicated directory. */
|
|
66
8
|
destroy(): Promise<void>;
|
|
67
9
|
}
|
|
68
10
|
export default FlashStore;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flash-store.d.ts","sourceRoot":"","sources":["../../src/flash-store.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"flash-store.d.ts","sourceRoot":"","sources":["../../src/flash-store.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,cAAc,EAAE,KAAK,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAIrE,YAAY,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAA;AAoGtE,iEAAiE;AACjE,qBAAa,UAAU,CACrB,CAAC,SAAS,QAAQ,GAAG,QAAQ,EAC7B,CAAC,GAAG,OAAO,CACX,SAAQ,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC;IAC5B,yDAAyD;IACzD,YAAY,OAAO,EAAE,MAAM,EAG1B;IAED,iFAAiF;IAClE,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAUtC;CACF;eAEc,UAAU"}
|
package/dist/src/flash-store.js
CHANGED
|
@@ -1,235 +1,98 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* synchronous facade builds on this API and its internal serialized-write hook.
|
|
2
|
+
* Binds the public native FlashStore constructor to LMDB. Filesystem paths,
|
|
3
|
+
* destruction safeguards, and Node diagnostics stay behind this entry point.
|
|
5
4
|
*/
|
|
6
5
|
import { channel } from 'node:diagnostics_channel';
|
|
7
6
|
import { rm } from 'node:fs/promises';
|
|
8
7
|
import { homedir, tmpdir } from 'node:os';
|
|
9
8
|
import { parse, resolve } from 'node:path';
|
|
10
9
|
import { IF_EXISTS, open } from 'lmdb';
|
|
11
|
-
import {
|
|
10
|
+
import { FlashStoreCore } from './flash-store-core.js';
|
|
11
|
+
import { encodePrefixEnd } from './key-encoding.js';
|
|
12
12
|
const operations = channel('flash-store.operation');
|
|
13
|
-
/**
|
|
14
|
-
|
|
15
|
-
if ((typeof key === 'number' && !Number.isFinite(key)) ||
|
|
16
|
-
(typeof key === 'string' && key.includes('\0')) ||
|
|
17
|
-
(typeof key !== 'number' && typeof key !== 'string')) {
|
|
18
|
-
throw new TypeError('Keys must be finite numbers or strings without null characters');
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
/** Serialize a value once, rejecting values that JSON cannot represent. */
|
|
22
|
-
export function serialize(value) {
|
|
23
|
-
const serialized = JSON.stringify(value);
|
|
24
|
-
if (serialized === undefined)
|
|
25
|
-
throw new TypeError('Value must be JSON serializable');
|
|
26
|
-
return serialized;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Return the smallest string after every string starting with `prefix`, or
|
|
30
|
-
* undefined when none exists. LMDB orders strings by UTF-8 bytes, which matches
|
|
31
|
-
* code point order, so matching keys form one contiguous interval.
|
|
32
|
-
*/
|
|
33
|
-
function prefixEnd(prefix) {
|
|
34
|
-
const codePoints = Array.from(prefix);
|
|
35
|
-
while (codePoints.length > 0) {
|
|
36
|
-
const last = codePoints.pop()?.codePointAt(0);
|
|
37
|
-
if (last !== undefined && last < 0x10ffff) {
|
|
38
|
-
// Surrogate code points are not encodable scalar values; skip them.
|
|
39
|
-
const next = last === 0xd7ff ? 0xe000 : last + 1;
|
|
40
|
-
return codePoints.join('') + String.fromCodePoint(next);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return undefined;
|
|
44
|
-
}
|
|
45
|
-
/** Start reverse prefix scans just below the interval end, or at the database end. */
|
|
46
|
-
function prefixRange(prefix) {
|
|
47
|
-
const end = prefixEnd(prefix);
|
|
48
|
-
return { start: end, reverse: true, exclusiveStart: end !== undefined };
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Writes pre-serialized JSON for FlashStoreSync, which serializes at call time.
|
|
52
|
-
* Internal: mod.ts does not export it, and package exports expose only mod.ts.
|
|
53
|
-
*/
|
|
54
|
-
export let writeSerialized;
|
|
55
|
-
/** Use Map's zero equality; TypeScript represents both signs as the same numeric type. */
|
|
56
|
-
function normalizeKey(key) {
|
|
57
|
-
return Object.is(key, -0) ? 0 : key;
|
|
58
|
-
}
|
|
59
|
-
/** Reject ambiguous bounds before creating an LMDB cursor. */
|
|
60
|
-
function validateOptions(options) {
|
|
61
|
-
if (options.prefix !== undefined && typeof options.prefix !== 'string') {
|
|
62
|
-
throw new TypeError('prefix must be a string');
|
|
63
|
-
}
|
|
64
|
-
if ((options.gt !== undefined && options.gte !== undefined) ||
|
|
65
|
-
(options.lt !== undefined && options.lte !== undefined) ||
|
|
66
|
-
(options.prefix !== undefined &&
|
|
67
|
-
(options.gt !== undefined ||
|
|
68
|
-
options.gte !== undefined ||
|
|
69
|
-
options.lt !== undefined ||
|
|
70
|
-
options.lte !== undefined))) {
|
|
71
|
-
throw new TypeError('Conflicting range bounds');
|
|
72
|
-
}
|
|
73
|
-
if (options.limit !== undefined &&
|
|
74
|
-
(!Number.isSafeInteger(options.limit) || options.limit < 0)) {
|
|
75
|
-
throw new RangeError('limit must be a non-negative safe integer');
|
|
76
|
-
}
|
|
77
|
-
for (const bound of [options.gt, options.gte, options.lt, options.lte]) {
|
|
78
|
-
if (bound !== undefined)
|
|
79
|
-
validateKey(bound);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Persistent key-value store. Values use JSON round-tripping, which may change their
|
|
84
|
-
* runtime type; the generic value type does not validate or prevent this.
|
|
85
|
-
*/
|
|
86
|
-
export class FlashStore {
|
|
87
|
-
static VERSION = VERSION;
|
|
88
|
-
workdir;
|
|
13
|
+
/** Adapts the existing LMDB layout without changing its key encoding or JSON bytes. */
|
|
14
|
+
class LmdbBackend {
|
|
89
15
|
db;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return this;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
async
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
/** Returns the stored value, or undefined when the key is absent. */
|
|
125
|
-
async get(key) {
|
|
126
|
-
validateKey(key);
|
|
127
|
-
log.verbose('FlashStore', 'get(%s)', key);
|
|
128
|
-
const value = this.db.get(normalizeKey(key));
|
|
129
|
-
return value === undefined ? undefined : JSON.parse(value);
|
|
130
|
-
}
|
|
131
|
-
/** Returns whether the key exists, including when its value is falsey. */
|
|
132
|
-
async has(key) {
|
|
133
|
-
validateKey(key);
|
|
134
|
-
return this.db.doesExist(normalizeKey(key));
|
|
135
|
-
}
|
|
136
|
-
/** Deletes a key and reports whether an entry existed. */
|
|
137
|
-
async delete(key) {
|
|
138
|
-
validateKey(key);
|
|
139
|
-
log.verbose('FlashStore', 'delete(%s)', key);
|
|
140
|
-
// A plain remove() resolves true even for absent keys; IF_EXISTS makes LMDB
|
|
141
|
-
// report this operation's own result, in order with other queued writes.
|
|
142
|
-
const deleted = await this.db.remove(normalizeKey(key), IF_EXISTS);
|
|
143
|
-
if (operations.hasSubscribers)
|
|
144
|
-
operations.publish({ operation: 'delete' });
|
|
145
|
-
return deleted;
|
|
146
|
-
}
|
|
147
|
-
/** Removes every entry after queued writes. */
|
|
148
|
-
async clear() {
|
|
149
|
-
log.verbose('FlashStore', 'clear()');
|
|
150
|
-
await this.db.clearAsync();
|
|
151
|
-
if (operations.hasSubscribers)
|
|
152
|
-
operations.publish({ operation: 'clear' });
|
|
153
|
-
}
|
|
154
|
-
/** Resolves to the number of entries in the current snapshot. */
|
|
155
|
-
get size() {
|
|
156
|
-
log.verbose('FlashStore', 'size()');
|
|
157
|
-
return Promise.resolve().then(() => this.db.getCount());
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Iterates a snapshot in LMDB key order, releasing the cursor on early exit.
|
|
161
|
-
* A prefix seeks to its contiguous key interval and stops on leaving it, so
|
|
162
|
-
* the cost depends on matching entries rather than database size.
|
|
163
|
-
*/
|
|
164
|
-
async *entries(options = {}) {
|
|
165
|
-
log.verbose('FlashStore', '*entries(%s)', JSON.stringify(options));
|
|
166
|
-
validateOptions(options);
|
|
167
|
-
if (options.limit === 0)
|
|
168
|
-
return;
|
|
169
|
-
const reverse = options.reverse ?? false;
|
|
170
|
-
const lowerBound = options.gte ?? options.gt;
|
|
171
|
-
const upperBound = options.lte ?? options.lt;
|
|
172
|
-
const lower = lowerBound === undefined ? undefined : normalizeKey(lowerBound);
|
|
173
|
-
const upper = upperBound === undefined ? undefined : normalizeKey(upperBound);
|
|
174
|
-
const { prefix } = options;
|
|
16
|
+
path;
|
|
17
|
+
/** Opens the existing on-disk database or creates a fresh environment. */
|
|
18
|
+
constructor(path) {
|
|
19
|
+
this.path = path;
|
|
20
|
+
this.db = open({ path, noSubdir: false, encoding: 'string' });
|
|
21
|
+
}
|
|
22
|
+
/** Enqueues a write of serialized JSON. */
|
|
23
|
+
put(key, json) {
|
|
24
|
+
return this.db.put(key, json);
|
|
25
|
+
}
|
|
26
|
+
/** Reads from the current transaction snapshot. */
|
|
27
|
+
get(key) {
|
|
28
|
+
return this.db.get(key);
|
|
29
|
+
}
|
|
30
|
+
/** Checks existence without decoding the value. */
|
|
31
|
+
has(key) {
|
|
32
|
+
return this.db.doesExist(key);
|
|
33
|
+
}
|
|
34
|
+
/** Reports existence in the transaction that performs the deletion. */
|
|
35
|
+
delete(key) {
|
|
36
|
+
// A plain remove() reports success even when the key was absent.
|
|
37
|
+
return this.db.remove(key, IF_EXISTS);
|
|
38
|
+
}
|
|
39
|
+
/** Queues an atomic clear. */
|
|
40
|
+
clear() {
|
|
41
|
+
return this.db.clearAsync();
|
|
42
|
+
}
|
|
43
|
+
/** Counts the entries in the current snapshot. */
|
|
44
|
+
async count() {
|
|
45
|
+
return this.db.getCount();
|
|
46
|
+
}
|
|
47
|
+
/** Seeks directly to the range and releases the LMDB cursor on early exit. */
|
|
48
|
+
*scan({ lower, upper, lowerOpen, upperOpen, reverse, prefix, limit, }) {
|
|
49
|
+
const endOfPrefix = reverse && prefix !== undefined ? encodePrefixEnd(prefix) : undefined;
|
|
175
50
|
const range = prefix === undefined
|
|
176
51
|
? {
|
|
177
52
|
start: reverse ? upper : lower,
|
|
178
53
|
end: reverse ? lower : upper,
|
|
179
54
|
reverse,
|
|
180
|
-
exclusiveStart: reverse
|
|
181
|
-
|
|
182
|
-
: options.gt !== undefined,
|
|
183
|
-
inclusiveEnd: reverse
|
|
184
|
-
? options.gte !== undefined
|
|
185
|
-
: options.lte !== undefined,
|
|
55
|
+
exclusiveStart: reverse ? upperOpen : lowerOpen,
|
|
56
|
+
inclusiveEnd: reverse ? !lowerOpen : !upperOpen,
|
|
186
57
|
}
|
|
187
|
-
:
|
|
188
|
-
?
|
|
189
|
-
|
|
58
|
+
: {
|
|
59
|
+
start: reverse ? endOfPrefix : prefix,
|
|
60
|
+
reverse,
|
|
61
|
+
exclusiveStart: reverse && endOfPrefix !== undefined,
|
|
62
|
+
};
|
|
190
63
|
let count = 0;
|
|
191
|
-
for (const
|
|
192
|
-
// Matching keys are contiguous, so the first mismatch ends the prefix.
|
|
64
|
+
for (const entry of this.db.getRange(range)) {
|
|
193
65
|
if (prefix !== undefined &&
|
|
194
|
-
(typeof key !== 'string' || !key.startsWith(prefix)))
|
|
66
|
+
(typeof entry.key !== 'string' || !entry.key.startsWith(prefix)))
|
|
195
67
|
break;
|
|
196
|
-
yield
|
|
197
|
-
if (++count ===
|
|
68
|
+
yield entry;
|
|
69
|
+
if (++count === limit)
|
|
198
70
|
break;
|
|
199
71
|
}
|
|
200
72
|
}
|
|
201
|
-
/**
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
for await (const [key] of this.entries(options))
|
|
205
|
-
yield key;
|
|
73
|
+
/** Flushes pending writes and closes LMDB. */
|
|
74
|
+
close() {
|
|
75
|
+
return this.db.close();
|
|
206
76
|
}
|
|
207
|
-
/**
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
for await (const [, value] of this.entries(options))
|
|
211
|
-
yield value;
|
|
77
|
+
/** Removes the dedicated directory after the facade closes the environment. */
|
|
78
|
+
destroy() {
|
|
79
|
+
return rm(this.path, { recursive: true, force: true });
|
|
212
80
|
}
|
|
213
|
-
/**
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
}
|
|
218
|
-
/** Awaits each callback in snapshot key order; stops and rejects on the first failure. */
|
|
219
|
-
async forEach(callback, thisArg) {
|
|
220
|
-
log.verbose('FlashStore', 'forEach()');
|
|
221
|
-
for await (const [key, value] of this)
|
|
222
|
-
await callback.call(thisArg, value, key, this);
|
|
81
|
+
/** Publishes the operation name after a successful commit without storing caller data. */
|
|
82
|
+
publish(operation) {
|
|
83
|
+
if (operations.hasSubscribers)
|
|
84
|
+
operations.publish({ operation });
|
|
223
85
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
86
|
+
}
|
|
87
|
+
/** Persistent Map backed by LMDB with JSON-serialized values. */
|
|
88
|
+
export class FlashStore extends FlashStoreCore {
|
|
89
|
+
/** Opens an LMDB database in the specified directory. */
|
|
90
|
+
constructor(workdir) {
|
|
91
|
+
const path = resolve(workdir);
|
|
92
|
+
super(path, new LmdbBackend(path), workdir);
|
|
229
93
|
}
|
|
230
|
-
/**
|
|
94
|
+
/** Refuses to close or remove protected locations. Use a dedicated directory. */
|
|
231
95
|
async destroy() {
|
|
232
|
-
log.verbose('FlashStore', 'destroy()');
|
|
233
96
|
const protectedPaths = [
|
|
234
97
|
parse(this.workdir).root,
|
|
235
98
|
resolve(process.cwd()),
|
|
@@ -238,8 +101,7 @@ export class FlashStore {
|
|
|
238
101
|
];
|
|
239
102
|
if (protectedPaths.includes(this.workdir))
|
|
240
103
|
throw new Error('Refusing to remove a protected directory');
|
|
241
|
-
await
|
|
242
|
-
await rm(this.workdir, { recursive: true, force: true });
|
|
104
|
+
await super.destroy();
|
|
243
105
|
}
|
|
244
106
|
}
|
|
245
107
|
export default FlashStore;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flash-store.js","sourceRoot":"","sources":["../../src/flash-store.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"flash-store.js","sourceRoot":"","sources":["../../src/flash-store.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAClD,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAA;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AACzC,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAC1C,OAAO,EAAE,SAAS,EAAE,IAAI,EAAqB,MAAM,MAAM,CAAA;AACzD,OAAO,EAAE,cAAc,EAAiB,MAAM,uBAAuB,CAAA;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAKnD,MAAM,UAAU,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAA;AAEnD,uFAAuF;AACvF,MAAM,WAAW;IACE,EAAE,CAAgC;IAClC,IAAI,CAAQ;IAE7B,0EAA0E;IAC1E,YAAY,IAAY;QACtB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,EAAE,IA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