@nxtedition/cache 1.0.1 → 1.0.2

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/lib/index.d.ts CHANGED
@@ -12,7 +12,7 @@ export interface AsyncCacheOptions<V> {
12
12
  ttl?: number | ((value: V, key: string) => number);
13
13
  stale?: number | ((value: V, key: string) => number);
14
14
  lru?: LRUCache.Options<string, CacheEntry<V>, unknown> | false | null;
15
- db?: AsyncCacheDbOptions;
15
+ db?: AsyncCacheDbOptions | false | null;
16
16
  }
17
17
  export type CacheResult<V> = {
18
18
  value: V;
package/lib/index.js CHANGED
@@ -52,7 +52,7 @@ const dbs = new Set ()
52
52
 
53
53
 
54
54
 
55
-
55
+
56
56
 
57
57
 
58
58
 
@@ -121,13 +121,12 @@ export class AsyncCache {
121
121
  }
122
122
 
123
123
  this.#lru =
124
- opts?.lru === false || opts?.lru === undefined
125
- ? null
126
- : new LRUCache({ max: 4096, ...opts?.lru })
124
+ opts?.lru === false || opts?.lru === null ? null : new LRUCache({ max: 4096, ...opts?.lru })
127
125
 
128
- for (let n = 0; true; n++) {
126
+ for (let n = 0; opts?.db !== null && opts?.db !== false; n++) {
129
127
  try {
130
- this.#db ??= new DatabaseSync(location, { timeout: 20, ...opts?.db })
128
+ const { maxSize = 256 * 1024 * 1024, timeout = 20 } = opts?.db ?? {}
129
+ this.#db ??= new DatabaseSync(location, { timeout })
131
130
 
132
131
  this.#db.exec(`
133
132
  PRAGMA journal_mode = WAL;
@@ -144,7 +143,6 @@ export class AsyncCache {
144
143
  `)
145
144
 
146
145
  {
147
- const maxSize = opts?.db?.maxSize ?? 256 * 1024 * 1024
148
146
  const { page_size } = this.#db.prepare('PRAGMA page_size').get()
149
147
  this.#db.exec(`PRAGMA max_page_count = ${Math.ceil(maxSize / page_size)}`)
150
148
  }
@@ -278,12 +276,12 @@ export class AsyncCache {
278
276
  : { value: promise, async: true }
279
277
  }
280
278
 
281
- // eslint-disable-next-line: no-unsafe-argument
282
279
  #refresh(args , key = this.#keySelector(...args)) {
283
280
  if (typeof key !== 'string' || key.length === 0) {
284
281
  throw new TypeError('keySelector must return a non-empty string')
285
282
  }
286
283
 
284
+ // TODO (fix): cross process/thread dedupe...
287
285
  let promise = this.#dedupe.get(key)
288
286
  if (promise === undefined && this.#valueSelector) {
289
287
  // eslint-disable-next-line: no-unsafe-argument
@@ -329,7 +327,11 @@ export class AsyncCache {
329
327
  return
330
328
  }
331
329
 
332
- this.#lru?.set(key, { ttl, stale, value })
330
+ const storedValue = ArrayBuffer.isView(value)
331
+ ? (Buffer.from(value.buffer, value.byteOffset, value.byteLength) )
332
+ : value
333
+
334
+ this.#lru?.set(key, { ttl, stale, value: storedValue })
333
335
 
334
336
  const data = ArrayBuffer.isView(value)
335
337
  ? value
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/cache",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -25,5 +25,5 @@
25
25
  "dependencies": {
26
26
  "lru-cache": "^11.2.6"
27
27
  },
28
- "gitHead": "1833b15a4085a33f85063e3fabf522dfa19c64b7"
28
+ "gitHead": "1bcd9e44bf750ac6fb478967c46e784e14ea63c5"
29
29
  }