@blaze-cache/core 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +60 -2
- package/dist/cache.d.ts +126 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +356 -0
- package/dist/cache.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/persistence/indexeddb.d.ts +19 -0
- package/dist/persistence/indexeddb.d.ts.map +1 -0
- package/dist/persistence/indexeddb.js +141 -0
- package/dist/persistence/indexeddb.js.map +1 -0
- package/dist/persistence/types.d.ts +46 -0
- package/dist/persistence/types.d.ts.map +1 -0
- package/dist/persistence/types.js +2 -0
- package/dist/persistence/types.js.map +1 -0
- package/dist/types.d.ts +81 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +26 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +49 -0
- package/dist/utils.js.map +1 -0
- package/dist/wasm.d.ts +21 -0
- package/dist/wasm.d.ts.map +1 -0
- package/dist/wasm.js +36 -0
- package/dist/wasm.js.map +1 -0
- package/package.json +52 -3
- package/pkg/LICENSE +21 -0
- package/pkg/blaze_cache_wasm.d.ts +156 -0
- package/pkg/blaze_cache_wasm.js +641 -0
- package/pkg/blaze_cache_wasm_bg.wasm +0 -0
- package/pkg/blaze_cache_wasm_bg.wasm.d.ts +28 -0
- package/pkg/package.json +21 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Blaze Cache
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,61 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/blaze-cache/blaze-cache/main/docs/blaze-cache-logo.png" alt="Blaze Cache logo" width="150">
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<h1 align="center">@blaze-cache/core</h1>
|
|
6
|
+
|
|
7
|
+
WebAssembly client-side cache for the browser: SIEVE eviction, per-entry TTLs, tag
|
|
8
|
+
invalidation and optional IndexedDB persistence.
|
|
9
|
+
|
|
10
|
+
The engine behind
|
|
11
|
+
[`@blaze-cache/axios`](https://www.npmjs.com/package/@blaze-cache/axios) and
|
|
12
|
+
[`@blaze-cache/react-query`](https://www.npmjs.com/package/@blaze-cache/react-query) —
|
|
13
|
+
also usable on its own.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @blaze-cache/core
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { createCache } from '@blaze-cache/core';
|
|
25
|
+
|
|
26
|
+
const cache = await createCache({ capacity: 1000, persistence: { namespace: 'app' } });
|
|
27
|
+
|
|
28
|
+
cache.set('user:42', { name: 'Ada' }, { ttlMs: 30_000, tags: ['user:42'] });
|
|
29
|
+
|
|
30
|
+
cache.get('user:42'); // { name: 'Ada' } — counted as a hit
|
|
31
|
+
cache.invalidateTags('user:42'); // removes it, synchronously, and syncs IndexedDB
|
|
32
|
+
cache.stats(); // { hits, misses, evictions, expirations, … }
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`create` instantiates the wasm module once, so everything after it is **synchronous** —
|
|
36
|
+
`get`/`set` never allocate a `Promise`.
|
|
37
|
+
|
|
38
|
+
## Good to know
|
|
39
|
+
|
|
40
|
+
* **The `.wasm` ships inside this package** — no CDN setup. If your bundler
|
|
41
|
+
pre-bundles dependencies, point at it explicitly:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import wasmUrl from '@blaze-cache/core/pkg/blaze_cache_wasm_bg.wasm?url';
|
|
45
|
+
|
|
46
|
+
const cache = await createCache({ wasm: wasmUrl });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
* **Types are bundled** — no `@types/*` packages to install.
|
|
50
|
+
* **ESM only, browser only.** Node/SSR targets are out of scope by design.
|
|
51
|
+
* **Persistence is optional** and off by default; a blocked IndexedDB (private mode)
|
|
52
|
+
degrades to memory-only instead of breaking the app.
|
|
53
|
+
|
|
54
|
+
## Documentation
|
|
55
|
+
|
|
56
|
+
Setup, persistence, eviction, tags and the full API are documented in the
|
|
57
|
+
[repository README](https://github.com/blaze-cache/blaze-cache#readme).
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
[MIT](https://github.com/blaze-cache/blaze-cache/blob/main/LICENSE) © Blaze Cache
|
package/dist/cache.d.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { BlazeCacheLike, CacheEntry, CacheListener, CacheStats, SetOptions } from './types.js';
|
|
2
|
+
import type { WasmInitInput } from './wasm.js';
|
|
3
|
+
import type { CachePersistence, IndexedDbOptions } from './persistence/types.js';
|
|
4
|
+
/** Entries held when `capacity` is not given. */
|
|
5
|
+
export declare const DEFAULT_CAPACITY = 500;
|
|
6
|
+
/** Milliseconds a write waits for more writes before hitting IndexedDB. */
|
|
7
|
+
export declare const DEFAULT_FLUSH_DELAY_MS = 400;
|
|
8
|
+
/** Upper bound on records written by a single flush. */
|
|
9
|
+
export declare const DEFAULT_MAX_RECORDS = 2000;
|
|
10
|
+
/** Options for a custom {@link CachePersistence} adapter. */
|
|
11
|
+
export interface CachePersistenceOptions {
|
|
12
|
+
adapter: CachePersistence;
|
|
13
|
+
/** Debounce applied to write-through flushes. Default {@link DEFAULT_FLUSH_DELAY_MS}. */
|
|
14
|
+
flushDelayMs?: number;
|
|
15
|
+
/** Maximum records persisted per flush (newest win). Default {@link DEFAULT_MAX_RECORDS}. */
|
|
16
|
+
maxRecords?: number;
|
|
17
|
+
}
|
|
18
|
+
/** Options for the built-in IndexedDB adapter — settings plus flush tuning. */
|
|
19
|
+
export interface IndexedDbPersistenceOptions extends IndexedDbOptions {
|
|
20
|
+
flushDelayMs?: number;
|
|
21
|
+
maxRecords?: number;
|
|
22
|
+
}
|
|
23
|
+
/** Options accepted by {@link BlazeCache.create}. */
|
|
24
|
+
export interface BlazeCacheOptions {
|
|
25
|
+
/** Maximum number of entries kept in memory (clamped to `>= 1`). Default {@link DEFAULT_CAPACITY}. */
|
|
26
|
+
capacity?: number;
|
|
27
|
+
/**
|
|
28
|
+
* Durable storage. `false`/omitted means memory only.
|
|
29
|
+
*
|
|
30
|
+
* * `{ namespace, database, store, ... }` — the built-in IndexedDB adapter.
|
|
31
|
+
* * `{ adapter, ... }` — any {@link CachePersistence} implementation.
|
|
32
|
+
*/
|
|
33
|
+
persistence?: false | CachePersistenceOptions | IndexedDbPersistenceOptions;
|
|
34
|
+
/**
|
|
35
|
+
* How the `.wasm` binary is fetched and instantiated.
|
|
36
|
+
*
|
|
37
|
+
* Defaults to the binary shipped next to this package. Override it to serve the module
|
|
38
|
+
* from a CDN, or to pass pre-read bytes (as the Node smoke tests do).
|
|
39
|
+
*/
|
|
40
|
+
wasm?: WasmInitInput;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The client-side cache: WebAssembly storage and eviction, optional write-through persistence.
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { createCache } from '@blaze-cache/core';
|
|
47
|
+
*
|
|
48
|
+
* const cache = await createCache({ capacity: 1000, persistence: { namespace: 'app' } });
|
|
49
|
+
* cache.set('user:42', { name: 'Ada' }, { ttlMs: 30_000, tags: ['user:42'] });
|
|
50
|
+
* cache.get('user:42'); // { name: 'Ada' } — counted as a hit
|
|
51
|
+
* cache.invalidateTags('user:42'); // removes it and syncs IndexedDB
|
|
52
|
+
* await cache.dispose();
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* All reads and writes are synchronous — the wasm module is instantiated during `create`, so
|
|
56
|
+
* the hot path never touches a `Promise`.
|
|
57
|
+
*/
|
|
58
|
+
export declare class BlazeCache<V = any> implements BlazeCacheLike<V> {
|
|
59
|
+
#private;
|
|
60
|
+
private constructor();
|
|
61
|
+
/**
|
|
62
|
+
* Loads the wasm module, builds the cache and — if requested — hydrates it from storage.
|
|
63
|
+
*
|
|
64
|
+
* Storage failures (blocked IndexedDB, quota, private mode) degrade to memory-only with a
|
|
65
|
+
* console warning instead of rejecting.
|
|
66
|
+
*/
|
|
67
|
+
static create<V = any>(options?: BlazeCacheOptions): Promise<BlazeCache<V>>;
|
|
68
|
+
/** Value stored under `key`, or `undefined` when missing or expired. Counts a hit/miss. */
|
|
69
|
+
get(key: string): V | undefined;
|
|
70
|
+
/** `true` when `key` is present and not expired. Never changes eviction state. */
|
|
71
|
+
has(key: string): boolean;
|
|
72
|
+
/** Live entry — value, remaining TTL and tags — without touching statistics. */
|
|
73
|
+
entry(key: string): CacheEntry<V> | undefined;
|
|
74
|
+
/** Snapshot of every live entry, oldest first. */
|
|
75
|
+
entries(): CacheEntry<V>[];
|
|
76
|
+
/** Live keys, oldest first. */
|
|
77
|
+
keys(): string[];
|
|
78
|
+
/** Live keys carrying `tag`. */
|
|
79
|
+
keysWithTag(tag: string): string[];
|
|
80
|
+
/** Tags attached to `key` (empty when the key is unknown). */
|
|
81
|
+
tagsOf(key: string): string[];
|
|
82
|
+
/** `true` when `key` carries `tag`. */
|
|
83
|
+
hasTag(key: string, tag: string): boolean;
|
|
84
|
+
/** Number of stored entries (may include not-yet-pruned expired ones). */
|
|
85
|
+
get size(): number;
|
|
86
|
+
/** Maximum number of entries. */
|
|
87
|
+
get capacity(): number;
|
|
88
|
+
/** Cumulative counters plus a computed `hitRate`. */
|
|
89
|
+
stats(): CacheStats;
|
|
90
|
+
/** Stores (or replaces) `key`. Replacing refreshes the TTL and replaces the tags. */
|
|
91
|
+
set(key: string, value: V, options?: SetOptions): void;
|
|
92
|
+
/** Removes `key`, returning whether it was there. */
|
|
93
|
+
delete(key: string): boolean;
|
|
94
|
+
/** Removes every entry carrying any of `tags`, returning the removed keys. */
|
|
95
|
+
invalidateTags(...tags: string[]): string[];
|
|
96
|
+
/** Removes every expired entry, returning the removed keys. */
|
|
97
|
+
prune(): string[];
|
|
98
|
+
/** Empties the cache. */
|
|
99
|
+
clear(): void;
|
|
100
|
+
/** Zeroes the cumulative counters without touching stored entries. */
|
|
101
|
+
resetStats(): void;
|
|
102
|
+
/**
|
|
103
|
+
* Subscribes to mutations. Returns the unsubscribe function.
|
|
104
|
+
*
|
|
105
|
+
* Persistence uses this internally, so listeners fire for every change regardless of
|
|
106
|
+
* whether storage is enabled.
|
|
107
|
+
*/
|
|
108
|
+
on(listener: CacheListener): () => void;
|
|
109
|
+
/**
|
|
110
|
+
* Writes pending changes to storage immediately.
|
|
111
|
+
*
|
|
112
|
+
* Resolves once the underlying transaction commits. Concurrent calls are serialised, so
|
|
113
|
+
* this is safe to call from a `beforeunload` handler while a debounced flush is pending.
|
|
114
|
+
*/
|
|
115
|
+
flush(): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Flushes, unbinds page-lifecycle listeners and frees the wasm memory.
|
|
118
|
+
*
|
|
119
|
+
* The cache is unusable afterwards. A failed flush still rejects — the caller may retry or
|
|
120
|
+
* report it.
|
|
121
|
+
*/
|
|
122
|
+
dispose(): Promise<void>;
|
|
123
|
+
}
|
|
124
|
+
/** Shorthand for {@link BlazeCache.create}. */
|
|
125
|
+
export declare function createCache<V = any>(options?: BlazeCacheOptions): Promise<BlazeCache<V>>;
|
|
126
|
+
//# sourceMappingURL=cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EAEd,UAAU,EACV,aAAa,EACb,UAAU,EACV,UAAU,EACX,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,gBAAgB,EAAmB,MAAM,wBAAwB,CAAC;AAElG,iDAAiD;AACjD,eAAO,MAAM,gBAAgB,MAAM,CAAC;AACpC,2EAA2E;AAC3E,eAAO,MAAM,sBAAsB,MAAM,CAAC;AAC1C,wDAAwD;AACxD,eAAO,MAAM,mBAAmB,OAAO,CAAC;AAUxC,6DAA6D;AAC7D,MAAM,WAAW,uBAAuB;IACtC,OAAO,EAAE,gBAAgB,CAAC;IAC1B,yFAAyF;IACzF,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,6FAA6F;IAC7F,UAAU,CAAC,EAAE,MA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AI,IAAI,MAAM,CAEjB;IAED,iCAAiC;IACjC,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED,qDAAqD;IACrD,KAAK,IAAI,UAAU;IAQnB,qFAAqF;IACrF,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,IAAI;IAQtD,qDAAqD;IACrD,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAM5B,8EAA8E;IAC9E,cAAc,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE;IAS3C,+DAA+D;IAC/D,KAAK,IAAI,MAAM,EAAE;IAQjB,yBAAyB;IACzB,KAAK,IAAI,IAAI;IAKb,sEAAsE;IACtE,UAAU,IAAI,IAAI;IAMlB;;;;;OAKG;IACH,EAAE,CAAC,QAAQ,EAAE,aAAa,GAAG,MAAM,IAAI;IAOvC;;;;;OAKG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IActB;;;;;OAKG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAkK/B;AAED,+CAA+C;AAC/C,wBAAgB,WAAW,CAAC,CAAC,GAAG,GAAG,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAExF"}
|
package/dist/cache.js
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import { initBlazeWasm, WasmCache } from './wasm.js';
|
|
2
|
+
import { indexedDbPersistence } from './persistence/indexeddb.js';
|
|
3
|
+
/** Entries held when `capacity` is not given. */
|
|
4
|
+
export const DEFAULT_CAPACITY = 500;
|
|
5
|
+
/** Milliseconds a write waits for more writes before hitting IndexedDB. */
|
|
6
|
+
export const DEFAULT_FLUSH_DELAY_MS = 400;
|
|
7
|
+
/** Upper bound on records written by a single flush. */
|
|
8
|
+
export const DEFAULT_MAX_RECORDS = 2000;
|
|
9
|
+
/**
|
|
10
|
+
* Shared stand-in for "no tags" on the write path.
|
|
11
|
+
*
|
|
12
|
+
* The binding copies the array into wasm memory synchronously during the call and never
|
|
13
|
+
* retains it, so one module-level array can serve every untagged write instead of allocating
|
|
14
|
+
* a fresh `[]` per `set`. Never mutated.
|
|
15
|
+
*/
|
|
16
|
+
const NO_TAGS = [];
|
|
17
|
+
/**
|
|
18
|
+
* The client-side cache: WebAssembly storage and eviction, optional write-through persistence.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* import { createCache } from '@blaze-cache/core';
|
|
22
|
+
*
|
|
23
|
+
* const cache = await createCache({ capacity: 1000, persistence: { namespace: 'app' } });
|
|
24
|
+
* cache.set('user:42', { name: 'Ada' }, { ttlMs: 30_000, tags: ['user:42'] });
|
|
25
|
+
* cache.get('user:42'); // { name: 'Ada' } — counted as a hit
|
|
26
|
+
* cache.invalidateTags('user:42'); // removes it and syncs IndexedDB
|
|
27
|
+
* await cache.dispose();
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* All reads and writes are synchronous — the wasm module is instantiated during `create`, so
|
|
31
|
+
* the hot path never touches a `Promise`.
|
|
32
|
+
*/
|
|
33
|
+
export class BlazeCache {
|
|
34
|
+
#wasm;
|
|
35
|
+
#listeners = new Set();
|
|
36
|
+
#unbind = [];
|
|
37
|
+
#persist = null;
|
|
38
|
+
#flushDelayMs = DEFAULT_FLUSH_DELAY_MS;
|
|
39
|
+
#maxRecords = DEFAULT_MAX_RECORDS;
|
|
40
|
+
#dirty = new Set();
|
|
41
|
+
#deleted = new Set();
|
|
42
|
+
#clearAll = false;
|
|
43
|
+
#timer = null;
|
|
44
|
+
#chain = Promise.resolve();
|
|
45
|
+
#disposed = false;
|
|
46
|
+
constructor(wasm) {
|
|
47
|
+
this.#wasm = wasm;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Loads the wasm module, builds the cache and — if requested — hydrates it from storage.
|
|
51
|
+
*
|
|
52
|
+
* Storage failures (blocked IndexedDB, quota, private mode) degrade to memory-only with a
|
|
53
|
+
* console warning instead of rejecting.
|
|
54
|
+
*/
|
|
55
|
+
static async create(options = {}) {
|
|
56
|
+
await initBlazeWasm(options.wasm);
|
|
57
|
+
const cache = new BlazeCache(new WasmCache(options.capacity ?? DEFAULT_CAPACITY));
|
|
58
|
+
await cache.#setupPersistence(options.persistence);
|
|
59
|
+
return cache;
|
|
60
|
+
}
|
|
61
|
+
// ------------------------------------------------------------------ reads
|
|
62
|
+
/** Value stored under `key`, or `undefined` when missing or expired. Counts a hit/miss. */
|
|
63
|
+
get(key) {
|
|
64
|
+
return this.#wasm.get(key);
|
|
65
|
+
}
|
|
66
|
+
/** `true` when `key` is present and not expired. Never changes eviction state. */
|
|
67
|
+
has(key) {
|
|
68
|
+
return this.#wasm.has(key);
|
|
69
|
+
}
|
|
70
|
+
/** Live entry — value, remaining TTL and tags — without touching statistics. */
|
|
71
|
+
entry(key) {
|
|
72
|
+
return this.#wasm.entry(key);
|
|
73
|
+
}
|
|
74
|
+
/** Snapshot of every live entry, oldest first. */
|
|
75
|
+
entries() {
|
|
76
|
+
return this.#wasm.entries();
|
|
77
|
+
}
|
|
78
|
+
/** Live keys, oldest first. */
|
|
79
|
+
keys() {
|
|
80
|
+
return this.#wasm.keys();
|
|
81
|
+
}
|
|
82
|
+
/** Live keys carrying `tag`. */
|
|
83
|
+
keysWithTag(tag) {
|
|
84
|
+
return this.#wasm.keysWithTag(tag);
|
|
85
|
+
}
|
|
86
|
+
/** Tags attached to `key` (empty when the key is unknown). */
|
|
87
|
+
tagsOf(key) {
|
|
88
|
+
return this.#wasm.tagsOf(key);
|
|
89
|
+
}
|
|
90
|
+
/** `true` when `key` carries `tag`. */
|
|
91
|
+
hasTag(key, tag) {
|
|
92
|
+
return this.#wasm.hasTag(key, tag);
|
|
93
|
+
}
|
|
94
|
+
/** Number of stored entries (may include not-yet-pruned expired ones). */
|
|
95
|
+
get size() {
|
|
96
|
+
return this.#wasm.size;
|
|
97
|
+
}
|
|
98
|
+
/** Maximum number of entries. */
|
|
99
|
+
get capacity() {
|
|
100
|
+
return this.#wasm.capacity;
|
|
101
|
+
}
|
|
102
|
+
/** Cumulative counters plus a computed `hitRate`. */
|
|
103
|
+
stats() {
|
|
104
|
+
const base = this.#wasm.stats();
|
|
105
|
+
const total = base.hits + base.misses;
|
|
106
|
+
return { ...base, hitRate: total === 0 ? 0 : base.hits / total };
|
|
107
|
+
}
|
|
108
|
+
// ----------------------------------------------------------------- writes
|
|
109
|
+
/** Stores (or replaces) `key`. Replacing refreshes the TTL and replaces the tags. */
|
|
110
|
+
set(key, value, options) {
|
|
111
|
+
// No defensive copy of `tags`: the binding reads the array synchronously while building
|
|
112
|
+
// the wasm call and never retains it, so the `readonly`→`string[]` cast below is safe.
|
|
113
|
+
// Untagged writes share `NO_TAGS` (see its doc).
|
|
114
|
+
this.#wasm.set(key, value, options?.ttlMs ?? null, (options?.tags ?? NO_TAGS));
|
|
115
|
+
if (this.#hasChangeConsumers)
|
|
116
|
+
this.#notify({ type: 'set', key });
|
|
117
|
+
}
|
|
118
|
+
/** Removes `key`, returning whether it was there. */
|
|
119
|
+
delete(key) {
|
|
120
|
+
const removed = this.#wasm.delete(key);
|
|
121
|
+
if (removed && this.#hasChangeConsumers)
|
|
122
|
+
this.#notify({ type: 'delete', key });
|
|
123
|
+
return removed;
|
|
124
|
+
}
|
|
125
|
+
/** Removes every entry carrying any of `tags`, returning the removed keys. */
|
|
126
|
+
invalidateTags(...tags) {
|
|
127
|
+
if (tags.length === 0)
|
|
128
|
+
return [];
|
|
129
|
+
const removed = this.#wasm.invalidateTags(tags);
|
|
130
|
+
if (removed.length > 0 && this.#hasChangeConsumers) {
|
|
131
|
+
this.#notify({ type: 'invalidate', keys: removed });
|
|
132
|
+
}
|
|
133
|
+
return removed;
|
|
134
|
+
}
|
|
135
|
+
/** Removes every expired entry, returning the removed keys. */
|
|
136
|
+
prune() {
|
|
137
|
+
const removed = this.#wasm.prune();
|
|
138
|
+
if (removed.length > 0 && this.#hasChangeConsumers) {
|
|
139
|
+
this.#notify({ type: 'prune', keys: removed });
|
|
140
|
+
}
|
|
141
|
+
return removed;
|
|
142
|
+
}
|
|
143
|
+
/** Empties the cache. */
|
|
144
|
+
clear() {
|
|
145
|
+
this.#wasm.clear();
|
|
146
|
+
if (this.#hasChangeConsumers)
|
|
147
|
+
this.#notify({ type: 'clear' });
|
|
148
|
+
}
|
|
149
|
+
/** Zeroes the cumulative counters without touching stored entries. */
|
|
150
|
+
resetStats() {
|
|
151
|
+
this.#wasm.resetStats();
|
|
152
|
+
}
|
|
153
|
+
// ------------------------------------------------------------- observers
|
|
154
|
+
/**
|
|
155
|
+
* Subscribes to mutations. Returns the unsubscribe function.
|
|
156
|
+
*
|
|
157
|
+
* Persistence uses this internally, so listeners fire for every change regardless of
|
|
158
|
+
* whether storage is enabled.
|
|
159
|
+
*/
|
|
160
|
+
on(listener) {
|
|
161
|
+
this.#listeners.add(listener);
|
|
162
|
+
return () => {
|
|
163
|
+
this.#listeners.delete(listener);
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Writes pending changes to storage immediately.
|
|
168
|
+
*
|
|
169
|
+
* Resolves once the underlying transaction commits. Concurrent calls are serialised, so
|
|
170
|
+
* this is safe to call from a `beforeunload` handler while a debounced flush is pending.
|
|
171
|
+
*/
|
|
172
|
+
flush() {
|
|
173
|
+
if (this.#disposed)
|
|
174
|
+
return Promise.resolve();
|
|
175
|
+
if (this.#timer !== null) {
|
|
176
|
+
clearTimeout(this.#timer);
|
|
177
|
+
this.#timer = null;
|
|
178
|
+
}
|
|
179
|
+
// Chain instead of guarding: a change landing *during* a flush starts a follow-up pass.
|
|
180
|
+
this.#chain = this.#chain.then(() => this.#runFlush(), () => this.#runFlush());
|
|
181
|
+
return this.#chain;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Flushes, unbinds page-lifecycle listeners and frees the wasm memory.
|
|
185
|
+
*
|
|
186
|
+
* The cache is unusable afterwards. A failed flush still rejects — the caller may retry or
|
|
187
|
+
* report it.
|
|
188
|
+
*/
|
|
189
|
+
async dispose() {
|
|
190
|
+
if (this.#disposed)
|
|
191
|
+
return;
|
|
192
|
+
await this.flush();
|
|
193
|
+
this.#disposed = true;
|
|
194
|
+
if (this.#timer !== null) {
|
|
195
|
+
clearTimeout(this.#timer);
|
|
196
|
+
this.#timer = null;
|
|
197
|
+
}
|
|
198
|
+
for (const unbind of this.#unbind.splice(0))
|
|
199
|
+
unbind();
|
|
200
|
+
this.#listeners.clear();
|
|
201
|
+
await this.#persist?.close?.();
|
|
202
|
+
this.#persist = null;
|
|
203
|
+
this.#wasm.free();
|
|
204
|
+
}
|
|
205
|
+
// ----------------------------------------------------------- persistence
|
|
206
|
+
async #setupPersistence(spec) {
|
|
207
|
+
if (!spec)
|
|
208
|
+
return;
|
|
209
|
+
let adapter;
|
|
210
|
+
let flushDelayMs;
|
|
211
|
+
let maxRecords;
|
|
212
|
+
if ('adapter' in spec) {
|
|
213
|
+
adapter = spec.adapter;
|
|
214
|
+
flushDelayMs = spec.flushDelayMs;
|
|
215
|
+
maxRecords = spec.maxRecords;
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
adapter = indexedDbPersistence(spec);
|
|
219
|
+
flushDelayMs = spec.flushDelayMs;
|
|
220
|
+
maxRecords = spec.maxRecords;
|
|
221
|
+
}
|
|
222
|
+
this.#persist = adapter;
|
|
223
|
+
this.#flushDelayMs = flushDelayMs ?? DEFAULT_FLUSH_DELAY_MS;
|
|
224
|
+
this.#maxRecords = maxRecords ?? DEFAULT_MAX_RECORDS;
|
|
225
|
+
try {
|
|
226
|
+
await this.#hydrate(adapter);
|
|
227
|
+
}
|
|
228
|
+
catch (error) {
|
|
229
|
+
this.#persist = null;
|
|
230
|
+
console.warn('[blaze-cache] persistence unavailable — running memory-only.', error);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
this.#bindPageLifecycle();
|
|
234
|
+
}
|
|
235
|
+
/** Fills the cache from storage, dropping records whose TTL elapsed while we were away. */
|
|
236
|
+
async #hydrate(adapter) {
|
|
237
|
+
const records = await adapter.load();
|
|
238
|
+
const now = Date.now();
|
|
239
|
+
for (const record of records) {
|
|
240
|
+
const remaining = record.ttlMs === null ? null : record.ttlMs - (now - record.storedAt);
|
|
241
|
+
if (remaining !== null && remaining <= 0)
|
|
242
|
+
continue;
|
|
243
|
+
// Direct wasm call: hydration is not a user write, so it emits no change event.
|
|
244
|
+
this.#wasm.set(record.key, record.value, remaining, record.tags);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
#bindPageLifecycle() {
|
|
248
|
+
if (typeof window === 'undefined')
|
|
249
|
+
return;
|
|
250
|
+
const flush = () => {
|
|
251
|
+
void this.flush().catch((error) => {
|
|
252
|
+
console.error('[blaze-cache] flush on page hide failed.', error);
|
|
253
|
+
});
|
|
254
|
+
};
|
|
255
|
+
const onVisibility = () => {
|
|
256
|
+
if (document.visibilityState === 'hidden')
|
|
257
|
+
flush();
|
|
258
|
+
};
|
|
259
|
+
window.addEventListener('pagehide', flush);
|
|
260
|
+
document.addEventListener('visibilitychange', onVisibility);
|
|
261
|
+
this.#unbind.push(() => {
|
|
262
|
+
window.removeEventListener('pagehide', flush);
|
|
263
|
+
document.removeEventListener('visibilitychange', onVisibility);
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
#scheduleFlush() {
|
|
267
|
+
if (this.#timer !== null)
|
|
268
|
+
return;
|
|
269
|
+
this.#timer = setTimeout(() => {
|
|
270
|
+
this.#timer = null;
|
|
271
|
+
void this.flush().catch((error) => {
|
|
272
|
+
console.error('[blaze-cache] background flush failed.', error);
|
|
273
|
+
});
|
|
274
|
+
}, this.#flushDelayMs);
|
|
275
|
+
}
|
|
276
|
+
async #runFlush() {
|
|
277
|
+
const persist = this.#persist;
|
|
278
|
+
if (!persist || this.#disposed)
|
|
279
|
+
return;
|
|
280
|
+
// Take ownership of the pending work up front, so a `set` racing with the awaits below
|
|
281
|
+
// lands in the *next* pass instead of being written twice.
|
|
282
|
+
const clearAll = this.#clearAll;
|
|
283
|
+
const deleted = [...this.#deleted];
|
|
284
|
+
const dirty = [...this.#dirty].filter((key) => !this.#deleted.has(key));
|
|
285
|
+
this.#clearAll = false;
|
|
286
|
+
this.#dirty.clear();
|
|
287
|
+
this.#deleted.clear();
|
|
288
|
+
if (clearAll)
|
|
289
|
+
await persist.clear();
|
|
290
|
+
if (dirty.length > 0) {
|
|
291
|
+
const records = [];
|
|
292
|
+
for (const key of dirty.slice(-this.#maxRecords)) {
|
|
293
|
+
const entry = this.#wasm.entry(key);
|
|
294
|
+
if (!entry)
|
|
295
|
+
continue;
|
|
296
|
+
records.push({
|
|
297
|
+
key,
|
|
298
|
+
value: entry.value,
|
|
299
|
+
tags: entry.tags,
|
|
300
|
+
ttlMs: entry.ttlMs,
|
|
301
|
+
storedAt: Date.now(),
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
if (records.length > 0)
|
|
305
|
+
await persist.save(records);
|
|
306
|
+
}
|
|
307
|
+
if (deleted.length > 0)
|
|
308
|
+
await persist.remove(deleted);
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* `true` when a change event would actually reach someone — persistence bookkeeping or at
|
|
312
|
+
* least one listener.
|
|
313
|
+
*
|
|
314
|
+
* Lets the mutation paths skip building a {@link CacheChange} object altogether when
|
|
315
|
+
* nothing consumes it (memory-only cache, no subscribers). Equivalent to the conditions
|
|
316
|
+
* inside {@link BlazeCache.#notify}: after `dispose` the persistence adapter is `null`, so
|
|
317
|
+
* the `!#disposed` check inside `#notify` cannot change the outcome here.
|
|
318
|
+
*/
|
|
319
|
+
get #hasChangeConsumers() {
|
|
320
|
+
return this.#listeners.size > 0 || this.#persist !== null;
|
|
321
|
+
}
|
|
322
|
+
#notify(change) {
|
|
323
|
+
if (this.#persist && !this.#disposed) {
|
|
324
|
+
switch (change.type) {
|
|
325
|
+
case 'set':
|
|
326
|
+
this.#deleted.delete(change.key);
|
|
327
|
+
this.#dirty.add(change.key);
|
|
328
|
+
break;
|
|
329
|
+
case 'delete':
|
|
330
|
+
this.#dirty.delete(change.key);
|
|
331
|
+
this.#deleted.add(change.key);
|
|
332
|
+
break;
|
|
333
|
+
case 'clear':
|
|
334
|
+
this.#dirty.clear();
|
|
335
|
+
this.#deleted.clear();
|
|
336
|
+
this.#clearAll = true;
|
|
337
|
+
break;
|
|
338
|
+
case 'invalidate':
|
|
339
|
+
case 'prune':
|
|
340
|
+
for (const key of change.keys) {
|
|
341
|
+
this.#dirty.delete(key);
|
|
342
|
+
this.#deleted.add(key);
|
|
343
|
+
}
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
this.#scheduleFlush();
|
|
347
|
+
}
|
|
348
|
+
for (const listener of this.#listeners)
|
|
349
|
+
listener(change);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
/** Shorthand for {@link BlazeCache.create}. */
|
|
353
|
+
export function createCache(options) {
|
|
354
|
+
return BlazeCache.create(options);
|
|
355
|
+
}
|
|
356
|
+
//# sourceMappingURL=cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAErD,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAGlE,iDAAiD;AACjD,MAAM,CAAC,MAAM,gBAAgB,GAAG,GAAG,CAAC;AACpC,2EAA2E;AAC3E,MAAM,CAAC,MAAM,sBAAsB,GAAG,GAAG,CAAC;AAC1C,wDAAwD;AACxD,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,CAAC;AACxC;;;;;;GAMG;AACH,MAAM,OAAO,GAAa,EAAE,CAAC;AAqC7B;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,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AAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;oBAC5B,MAAM;gBACR,KAAK,QAAQ;oBACX,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;oBAC/B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;oBAC9B,MAAM;gBACR,KAAK,OAAO;oBACV,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;oBACpB,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;oBACtB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;oBACtB,MAAM;gBACR,KAAK,YAAY,CAAC;gBAClB,KAAK,OAAO;oBACV,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC;wBAC9B,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;wBACxB,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;oBACzB,CAAC;oBACD,MAAM;YACV,CAAC;YACD,IAAI,CAAC,cAAc,EAAE,CAAC;QACxB,CAAC;QACD,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,UAAU;YAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IAC3D,CAAC;CACF;AAED,+CAA+C;AAC/C,MAAM,UAAU,WAAW,CAAU,OAA2B;IAC9D,OAAO,UAAU,CAAC,MAAM,CAAI,OAAO,CAAC,CAAC;AACvC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Blaze Cache — a WebAssembly client-side cache.
|
|
3
|
+
*
|
|
4
|
+
* The Rust core provides SIEVE eviction, per-entry TTLs and tag invalidation; this package
|
|
5
|
+
* adds the browser glue: one-time wasm initialisation and optional IndexedDB persistence.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { createCache } from '@blaze-cache/core';
|
|
10
|
+
*
|
|
11
|
+
* const cache = await createCache({ capacity: 1000 });
|
|
12
|
+
* cache.set('greeting', 'hello', { ttlMs: 30_000, tags: ['greeting'] });
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @packageDocumentation
|
|
16
|
+
*/
|
|
17
|
+
export { BlazeCache, createCache, DEFAULT_CAPACITY, DEFAULT_FLUSH_DELAY_MS, DEFAULT_MAX_RECORDS } from './cache.js';
|
|
18
|
+
export type { BlazeCacheOptions, CachePersistenceOptions, IndexedDbPersistenceOptions } from './cache.js';
|
|
19
|
+
export { initBlazeWasm, wasmUrl } from './wasm.js';
|
|
20
|
+
export type { WasmInitInput } from './wasm.js';
|
|
21
|
+
export { indexedDbPersistence } from './persistence/indexeddb.js';
|
|
22
|
+
export type { CachePersistence, IndexedDbOptions, PersistedRecord } from './persistence/types.js';
|
|
23
|
+
export { safeStableStringify, stableStringify } from './utils.js';
|
|
24
|
+
export type { BlazeCacheLike, CacheChange, CacheEntry, CacheListener, CacheStats, SetOptions, } from './types.js';
|
|
25
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AACpH,YAAY,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAC;AAE1G,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACnD,YAAY,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE/C,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,YAAY,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAElG,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElE,YAAY,EACV,cAAc,EACd,WAAW,EACX,UAAU,EACV,aAAa,EACb,UAAU,EACV,UAAU,GACX,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Blaze Cache — a WebAssembly client-side cache.
|
|
3
|
+
*
|
|
4
|
+
* The Rust core provides SIEVE eviction, per-entry TTLs and tag invalidation; this package
|
|
5
|
+
* adds the browser glue: one-time wasm initialisation and optional IndexedDB persistence.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { createCache } from '@blaze-cache/core';
|
|
10
|
+
*
|
|
11
|
+
* const cache = await createCache({ capacity: 1000 });
|
|
12
|
+
* cache.set('greeting', 'hello', { ttlMs: 30_000, tags: ['greeting'] });
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @packageDocumentation
|
|
16
|
+
*/
|
|
17
|
+
export { BlazeCache, createCache, DEFAULT_CAPACITY, DEFAULT_FLUSH_DELAY_MS, DEFAULT_MAX_RECORDS } from './cache.js';
|
|
18
|
+
export { initBlazeWasm, wasmUrl } from './wasm.js';
|
|
19
|
+
export { indexedDbPersistence } from './persistence/indexeddb.js';
|
|
20
|
+
export { safeStableStringify, stableStringify } from './utils.js';
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAGpH,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGnD,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAGlE,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { CachePersistence, IndexedDbOptions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* IndexedDB-backed persistence.
|
|
4
|
+
*
|
|
5
|
+
* One database holds one object store; records from different caches are separated by a
|
|
6
|
+
* `namespace` field with its own index, so several `BlazeCache` instances can share a
|
|
7
|
+
* database without stepping on each other.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const cache = await createCache({
|
|
11
|
+
* persistence: { namespace: 'session-42', flushDelayMs: 250 },
|
|
12
|
+
* });
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* Failures reject with a normal `Error` — `BlazeCache.create` catches them, warns, and keeps
|
|
16
|
+
* running memory-only (private browsing modes routinely block IndexedDB).
|
|
17
|
+
*/
|
|
18
|
+
export declare function indexedDbPersistence(options?: IndexedDbOptions): CachePersistence;
|
|
19
|
+
//# sourceMappingURL=indexeddb.d.ts.map
|