@keq-request/cache 5.0.0-alpha.10 → 5.0.0-alpha.12

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/dist/index.js CHANGED
@@ -5,6 +5,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
9
  var __export = (target, all) => {
9
10
  for (var name in all)
10
11
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -26,6 +27,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
26
27
  mod
27
28
  ));
28
29
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
29
31
 
30
32
  // src/index.ts
31
33
  var index_exports = {};
@@ -34,6 +36,7 @@ __export(index_exports, {
34
36
  Eviction: () => Eviction,
35
37
  IndexedDBStorage: () => IndexedDBStorage,
36
38
  MemoryStorage: () => MemoryStorage,
39
+ Size: () => Size,
37
40
  Strategy: () => Strategy,
38
41
  TierStorage: () => TierStorage,
39
42
  cache: () => cache
@@ -45,8 +48,12 @@ var R = __toESM(require("ramda"));
45
48
  var import_keq = require("keq");
46
49
  function cache(opts) {
47
50
  const storage = opts.storage;
48
- const rules = opts?.rules || [];
51
+ const rules = (opts == null ? void 0 : opts.rules) || [];
49
52
  return async function cache2(ctx, next) {
53
+ if (ctx.options.cache === false) {
54
+ await next();
55
+ return;
56
+ }
50
57
  let cOpt = ctx.options.cache;
51
58
  const rule = rules.find((rule2) => {
52
59
  if (rule2.pattern === void 0 || rule2.pattern === true) return true;
@@ -62,7 +69,7 @@ function cache(opts) {
62
69
  if (cOpt.key) {
63
70
  if (typeof cOpt.key === "function") key = cOpt.key(ctx);
64
71
  else key = cOpt.key;
65
- } else if (opts?.keyFactory) {
72
+ } else if (opts == null ? void 0 : opts.keyFactory) {
66
73
  key = opts.keyFactory(ctx);
67
74
  }
68
75
  if (!key) throw new import_keq.Exception("Cache key is required");
@@ -89,27 +96,29 @@ async function getResponseBytes(response) {
89
96
 
90
97
  // src/cache-entry/cache-entry.ts
91
98
  var CacheEntry = class _CacheEntry {
92
- key;
93
- response;
94
- /**
95
- * @en bytes
96
- * @zh 字节数
97
- */
98
- size;
99
- expiredAt;
100
99
  constructor(options) {
100
+ __publicField(this, "key");
101
+ __publicField(this, "response");
102
+ /**
103
+ * @en bytes
104
+ * @zh 字节数
105
+ */
106
+ __publicField(this, "size");
107
+ __publicField(this, "expiredAt");
108
+ var _a;
101
109
  this.key = options.key;
102
110
  this.response = options.response;
103
111
  this.size = options.size;
104
- this.expiredAt = options.expiredAt ?? /* @__PURE__ */ new Date(864e13);
112
+ this.expiredAt = (_a = options.expiredAt) != null ? _a : /* @__PURE__ */ new Date(864e13);
105
113
  }
106
114
  static async build(options) {
115
+ var _a;
107
116
  const expiredAt = "expiredAt" in options ? options.expiredAt : "ttl" in options && typeof options.ttl === "number" && options.ttl > 0 ? new Date(Date.now() + options.ttl * 1e3) : /* @__PURE__ */ new Date(864e13);
108
117
  const response = options.response.clone();
109
118
  return new _CacheEntry({
110
119
  key: options.key,
111
120
  response,
112
- size: options.size ?? await getResponseBytes(response),
121
+ size: (_a = options.size) != null ? _a : await getResponseBytes(response),
113
122
  expiredAt
114
123
  });
115
124
  }
@@ -180,7 +189,7 @@ var networkFirst = function(opts) {
180
189
  if (entry) {
181
190
  context.emitter.emit("cache:update", {
182
191
  key,
183
- oldResponse: cache2?.response,
192
+ oldResponse: cache2 == null ? void 0 : cache2.response,
184
193
  newResponse: entry.response,
185
194
  context
186
195
  });
@@ -262,6 +271,15 @@ var Eviction = /* @__PURE__ */ ((Eviction2) => {
262
271
  return Eviction2;
263
272
  })(Eviction || {});
264
273
 
274
+ // src/constants/size.enum.ts
275
+ var Size = /* @__PURE__ */ ((Size2) => {
276
+ Size2[Size2["B"] = 1] = "B";
277
+ Size2[Size2["KB"] = 1024] = "KB";
278
+ Size2[Size2["MB"] = 1048576] = "MB";
279
+ Size2[Size2["GB"] = 1073741824] = "GB";
280
+ return Size2;
281
+ })(Size || {});
282
+
265
283
  // src/storage/memory-storage/ttl-memory-storage.ts
266
284
  var import_dayjs2 = __toESM(require("dayjs"));
267
285
  var R3 = __toESM(require("ramda"));
@@ -281,31 +299,32 @@ var KeqCacheStorage = class {
281
299
 
282
300
  // src/storage/internal-storage/internal-storage.ts
283
301
  var InternalStorage = class extends KeqCacheStorage {
284
- __id__ = Math.random().toString(36).slice(2);
285
- __size__;
286
- __debug__;
287
- __onCacheGet__;
288
- __onCacheSet__;
289
- __onCacheRemove__;
290
- __onCacheEvict__;
291
- __onCacheExpired__;
292
302
  constructor(options) {
303
+ var _a;
293
304
  super();
294
- if (options?.size && (typeof options?.size !== "number" || options.size <= 0)) {
295
- throw TypeError(`Invalid size: ${String(options?.size)}`);
305
+ __publicField(this, "__id__", Math.random().toString(36).slice(2));
306
+ __publicField(this, "__size__");
307
+ __publicField(this, "__debug__");
308
+ __publicField(this, "__onCacheGet__");
309
+ __publicField(this, "__onCacheSet__");
310
+ __publicField(this, "__onCacheRemove__");
311
+ __publicField(this, "__onCacheEvict__");
312
+ __publicField(this, "__onCacheExpired__");
313
+ if ((options == null ? void 0 : options.size) && (typeof (options == null ? void 0 : options.size) !== "number" || options.size <= 0)) {
314
+ throw TypeError("Invalid size: ".concat(String(options == null ? void 0 : options.size)));
296
315
  }
297
- this.__size__ = options?.size ?? Infinity;
298
- this.__debug__ = !!options?.debug;
299
- this.__onCacheGet__ = options?.onCacheGet;
300
- this.__onCacheSet__ = options?.onCacheSet;
301
- this.__onCacheRemove__ = options?.onCacheRemove;
302
- this.__onCacheEvict__ = options?.onCacheEvict;
316
+ this.__size__ = (_a = options == null ? void 0 : options.size) != null ? _a : Infinity;
317
+ this.__debug__ = !!(options == null ? void 0 : options.debug);
318
+ this.__onCacheGet__ = options == null ? void 0 : options.onCacheGet;
319
+ this.__onCacheSet__ = options == null ? void 0 : options.onCacheSet;
320
+ this.__onCacheRemove__ = options == null ? void 0 : options.onCacheRemove;
321
+ this.__onCacheEvict__ = options == null ? void 0 : options.onCacheEvict;
303
322
  this.debug((log) => log("Storage Created: ", this));
304
323
  }
305
324
  debug(fn) {
306
325
  if (this.__debug__) {
307
326
  fn((...args) => {
308
- debug(`[Storage(${this.__id__})]`, ...args);
327
+ debug("[Storage(".concat(this.__id__, ")]"), ...args);
309
328
  });
310
329
  }
311
330
  }
@@ -313,9 +332,13 @@ var InternalStorage = class extends KeqCacheStorage {
313
332
 
314
333
  // src/storage/memory-storage/base-memory-storage.ts
315
334
  var BaseMemoryStorage = class extends InternalStorage {
316
- storage = /* @__PURE__ */ new Map();
317
- visitTimeRecords = /* @__PURE__ */ new Map();
318
- visitCountRecords = /* @__PURE__ */ new Map();
335
+ constructor() {
336
+ super(...arguments);
337
+ __publicField(this, "storage", /* @__PURE__ */ new Map());
338
+ __publicField(this, "visitTimeRecords", /* @__PURE__ */ new Map());
339
+ __publicField(this, "visitCountRecords", /* @__PURE__ */ new Map());
340
+ __publicField(this, "lastEvictExpiredTime", (0, import_dayjs.default)());
341
+ }
319
342
  get size() {
320
343
  const used = R2.sum(R2.pluck("size", [...this.storage.values()]));
321
344
  const free = this.__size__ > used ? this.__size__ - used : 0;
@@ -325,22 +348,24 @@ var BaseMemoryStorage = class extends InternalStorage {
325
348
  };
326
349
  }
327
350
  get(key) {
351
+ var _a;
328
352
  this.evictExpired();
329
353
  const entry = this.storage.get(key);
330
- this.visitCountRecords.set(key, (this.visitCountRecords.get(key) ?? 0) + 1);
354
+ this.visitCountRecords.set(key, ((_a = this.visitCountRecords.get(key)) != null ? _a : 0) + 1);
331
355
  this.visitTimeRecords.set(key, /* @__PURE__ */ new Date());
332
- if (!entry) this.debug((log) => log(`Entry(${key}) Not Found`));
333
- else this.debug((log) => log(`Entry(${key}) Found: `, entry));
334
- return entry?.clone();
356
+ if (!entry) this.debug((log) => log("Entry(".concat(key, ") Not Found")));
357
+ else this.debug((log) => log("Entry(".concat(key, ") Found: "), entry));
358
+ return entry == null ? void 0 : entry.clone();
335
359
  }
336
360
  set(value) {
361
+ var _a;
337
362
  if (!this.evict(value.size)) {
338
363
  this.debug((log) => log("Storage Size Not Enough: ", this.size.free, " < ", value.size));
339
364
  return;
340
365
  }
341
366
  this.storage.set(value.key, value);
342
367
  this.visitTimeRecords.set(value.key, /* @__PURE__ */ new Date());
343
- this.visitCountRecords.set(value.key, this.visitCountRecords.get(value.key) ?? 0);
368
+ this.visitCountRecords.set(value.key, (_a = this.visitCountRecords.get(value.key)) != null ? _a : 0);
344
369
  this.debug((log) => log("Entry Added: ", value));
345
370
  this.debug((log) => log("Storage Size: ", this.size));
346
371
  }
@@ -358,11 +383,11 @@ var BaseMemoryStorage = class extends InternalStorage {
358
383
  remove(key) {
359
384
  this.__remove__([key]);
360
385
  }
361
- lastEvictExpiredTime = (0, import_dayjs.default)();
362
386
  /**
363
387
  * @zh 清除过期的缓存
364
388
  */
365
389
  evictExpired() {
390
+ var _a;
366
391
  const now = (0, import_dayjs.default)();
367
392
  if (now.diff(this.lastEvictExpiredTime, "second") < 1) return;
368
393
  const keys = [];
@@ -372,7 +397,7 @@ var BaseMemoryStorage = class extends InternalStorage {
372
397
  }
373
398
  }
374
399
  this.__remove__(keys);
375
- this.__onCacheExpired__?.({ keys });
400
+ (_a = this.__onCacheExpired__) == null ? void 0 : _a.call(this, { keys });
376
401
  }
377
402
  /**
378
403
  * @en Evict the storage to make sure the size is enough
@@ -393,19 +418,23 @@ var TTLMemoryStorage = class extends BaseMemoryStorage {
393
418
  super(options);
394
419
  }
395
420
  get(key) {
421
+ var _a;
396
422
  const entry = super.get(key);
397
- this.__onCacheGet__?.({ key });
423
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
398
424
  return entry;
399
425
  }
400
426
  set(value) {
427
+ var _a;
401
428
  super.set(value);
402
- this.__onCacheSet__?.({ key: value.key });
429
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
403
430
  }
404
431
  remove(key) {
432
+ var _a;
405
433
  super.remove(key);
406
- this.__onCacheRemove__?.({ key });
434
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
407
435
  }
408
436
  evict(expectSize) {
437
+ var _a;
409
438
  if (expectSize > this.__size__) {
410
439
  this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
411
440
  return false;
@@ -429,7 +458,7 @@ var TTLMemoryStorage = class extends BaseMemoryStorage {
429
458
  keys.push(entry.key);
430
459
  }
431
460
  this.__remove__(keys);
432
- this.__onCacheEvict__?.({ keys });
461
+ (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
433
462
  return true;
434
463
  }
435
464
  };
@@ -445,19 +474,23 @@ var RandomMemoryStorage = class extends BaseMemoryStorage {
445
474
  super(options);
446
475
  }
447
476
  get(key) {
477
+ var _a;
448
478
  const entry = super.get(key);
449
- this.__onCacheGet__?.({ key });
479
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
450
480
  return entry;
451
481
  }
452
482
  set(value) {
483
+ var _a;
453
484
  super.set(value);
454
- this.__onCacheSet__?.({ key: value.key });
485
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
455
486
  }
456
487
  remove(key) {
488
+ var _a;
457
489
  super.remove(key);
458
- this.__onCacheRemove__?.({ key });
490
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
459
491
  }
460
492
  evict(expectSize) {
493
+ var _a;
461
494
  if (expectSize > this.__size__) {
462
495
  this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
463
496
  return false;
@@ -475,7 +508,7 @@ var RandomMemoryStorage = class extends BaseMemoryStorage {
475
508
  keys.push(entry.key);
476
509
  }
477
510
  this.__remove__(keys);
478
- this.__onCacheEvict__?.({ keys });
511
+ (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
479
512
  return true;
480
513
  }
481
514
  };
@@ -487,19 +520,23 @@ var LRUMemoryStorage = class extends BaseMemoryStorage {
487
520
  super(options);
488
521
  }
489
522
  get(key) {
523
+ var _a;
490
524
  const entry = super.get(key);
491
- this.__onCacheGet__?.({ key });
525
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
492
526
  return entry;
493
527
  }
494
528
  set(value) {
529
+ var _a;
495
530
  super.set(value);
496
- this.__onCacheSet__?.({ key: value.key });
531
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
497
532
  }
498
533
  remove(key) {
534
+ var _a;
499
535
  super.remove(key);
500
- this.__onCacheRemove__?.({ key });
536
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
501
537
  }
502
538
  evict(expectSize) {
539
+ var _a;
503
540
  if (expectSize > this.__size__) {
504
541
  this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
505
542
  return false;
@@ -522,7 +559,7 @@ var LRUMemoryStorage = class extends BaseMemoryStorage {
522
559
  keys.push(entry.key);
523
560
  }
524
561
  this.__remove__(keys);
525
- this.__onCacheEvict__?.({ keys });
562
+ (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
526
563
  return true;
527
564
  }
528
565
  };
@@ -533,19 +570,23 @@ var LFUMemoryStorage = class extends BaseMemoryStorage {
533
570
  super(options);
534
571
  }
535
572
  get(key) {
573
+ var _a;
536
574
  const entry = super.get(key);
537
- this.__onCacheGet__?.({ key });
575
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
538
576
  return entry;
539
577
  }
540
578
  set(value) {
579
+ var _a;
541
580
  super.set(value);
542
- this.__onCacheSet__?.({ key: value.key });
581
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
543
582
  }
544
583
  remove(key) {
584
+ var _a;
545
585
  super.remove(key);
546
- this.__onCacheRemove__?.({ key });
586
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
547
587
  }
548
588
  evict(expectSize) {
589
+ var _a;
549
590
  if (expectSize > this.__size__) {
550
591
  this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
551
592
  return false;
@@ -565,17 +606,17 @@ var LFUMemoryStorage = class extends BaseMemoryStorage {
565
606
  keys.push(entry.key);
566
607
  }
567
608
  this.__remove__(keys);
568
- this.__onCacheEvict__?.({ keys });
609
+ (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
569
610
  return true;
570
611
  }
571
612
  };
572
613
 
573
614
  // src/storage/memory-storage/memory-storage.ts
574
615
  var MemoryStorage = class extends KeqCacheStorage {
575
- storage;
576
616
  constructor(options) {
577
617
  super();
578
- const eviction = options?.eviction || "lru" /* LRU */;
618
+ __publicField(this, "storage");
619
+ const eviction = (options == null ? void 0 : options.eviction) || "lru" /* LRU */;
579
620
  if (eviction === "ttl" /* TTL */) {
580
621
  this.storage = new TTLMemoryStorage(options);
581
622
  } else if (eviction === "random" /* RANDOM */) {
@@ -585,7 +626,7 @@ var MemoryStorage = class extends KeqCacheStorage {
585
626
  } else if (eviction === "lfu" /* LFU */) {
586
627
  this.storage = new LFUMemoryStorage(options);
587
628
  } else {
588
- throw new TypeError(`Invalid eviction: ${String(eviction)}`);
629
+ throw new TypeError("Invalid eviction: ".concat(String(eviction)));
589
630
  }
590
631
  }
591
632
  set(entry) {
@@ -612,14 +653,15 @@ var DEFAULT_TABLE_NAME = "keq_cache_indexed_db_storage";
612
653
 
613
654
  // src/storage/indexed-db-storage/base-indexed-db-storage.ts
614
655
  var BaseIndexedDBStorage = class extends InternalStorage {
615
- tableName = DEFAULT_TABLE_NAME;
616
- db;
617
656
  constructor(options) {
618
657
  super(options);
619
- if (options?.tableName === DEFAULT_TABLE_NAME) {
620
- throw new TypeError(`[keq-cache] IndexedDBStorage name cannot be "${DEFAULT_TABLE_NAME}"`);
658
+ __publicField(this, "tableName", DEFAULT_TABLE_NAME);
659
+ __publicField(this, "db");
660
+ __publicField(this, "lastEvictExpiredTime", (0, import_dayjs4.default)());
661
+ if ((options == null ? void 0 : options.tableName) === DEFAULT_TABLE_NAME) {
662
+ throw new TypeError('[keq-cache] IndexedDBStorage name cannot be "'.concat(DEFAULT_TABLE_NAME, '"'));
621
663
  }
622
- this.tableName = options?.tableName || DEFAULT_TABLE_NAME;
664
+ this.tableName = (options == null ? void 0 : options.tableName) || DEFAULT_TABLE_NAME;
623
665
  }
624
666
  async openDB() {
625
667
  if (this.db) return this.db;
@@ -641,13 +683,13 @@ var BaseIndexedDBStorage = class extends InternalStorage {
641
683
  }
642
684
  },
643
685
  blocked() {
644
- console.error(`IndexedDB Table ${tableName} is blocked`);
686
+ console.error("IndexedDB Table ".concat(tableName, " is blocked"));
645
687
  },
646
688
  blocking() {
647
- console.error(`IndexedDB Table ${tableName} is blocking`);
689
+ console.error("IndexedDB Table ".concat(tableName, " is blocking"));
648
690
  },
649
691
  terminated() {
650
- console.error(`IndexedDB Table ${tableName} is terminated`);
692
+ console.error("IndexedDB Table ".concat(tableName, " is terminated"));
651
693
  }
652
694
  });
653
695
  this.db = db;
@@ -692,7 +734,7 @@ var BaseIndexedDBStorage = class extends InternalStorage {
692
734
  try {
693
735
  if (!await this.evict(entry.size)) {
694
736
  const size = await this.getSize();
695
- this.debug((log) => log(`Storage Size Not Enough: ${size.free} < ${entry.size}`));
737
+ this.debug((log) => log("Storage Size Not Enough: ".concat(size.free, " < ").concat(entry.size)));
696
738
  return;
697
739
  }
698
740
  const dbMetadata = {
@@ -751,11 +793,11 @@ var BaseIndexedDBStorage = class extends InternalStorage {
751
793
  return;
752
794
  }
753
795
  }
754
- lastEvictExpiredTime = (0, import_dayjs4.default)();
755
796
  /**
756
797
  * @zh 清除过期的缓存
757
798
  */
758
799
  async evictExpired() {
800
+ var _a;
759
801
  const now = (0, import_dayjs4.default)();
760
802
  if (now.diff(this.lastEvictExpiredTime, "second") < 1) return;
761
803
  try {
@@ -775,7 +817,7 @@ var BaseIndexedDBStorage = class extends InternalStorage {
775
817
  }
776
818
  await this.__remove__(tx, expiredKeys);
777
819
  await tx.done;
778
- this.__onCacheExpired__?.({ keys: expiredKeys });
820
+ (_a = this.__onCacheExpired__) == null ? void 0 : _a.call(this, { keys: expiredKeys });
779
821
  } catch (error) {
780
822
  return;
781
823
  }
@@ -788,19 +830,23 @@ var RandomIndexedDBStorage = class extends BaseIndexedDBStorage {
788
830
  super(options);
789
831
  }
790
832
  async get(key) {
833
+ var _a;
791
834
  const entry = await super.get(key);
792
- this.__onCacheGet__?.({ key });
835
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
793
836
  return entry;
794
837
  }
795
838
  async set(value) {
839
+ var _a;
796
840
  await super.set(value);
797
- this.__onCacheSet__?.({ key: value.key });
841
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
798
842
  }
799
843
  async remove(key) {
844
+ var _a;
800
845
  await super.remove(key);
801
- this.__onCacheRemove__?.({ key });
846
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
802
847
  }
803
848
  async evict(expectSize) {
849
+ var _a;
804
850
  await this.evictExpired();
805
851
  const size = await this.getSize();
806
852
  let deficitSize = expectSize - size.free;
@@ -811,7 +857,7 @@ var RandomIndexedDBStorage = class extends BaseIndexedDBStorage {
811
857
  const metadatas = await metadataStore.getAll();
812
858
  const totalSize = R5.sum(metadatas.map((m) => m.size));
813
859
  if (totalSize < deficitSize) {
814
- this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize - totalSize}`));
860
+ this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize - totalSize)));
815
861
  await tx.abort();
816
862
  return false;
817
863
  }
@@ -825,7 +871,7 @@ var RandomIndexedDBStorage = class extends BaseIndexedDBStorage {
825
871
  }
826
872
  await this.__remove__(tx, keys);
827
873
  await tx.done;
828
- await this.__onCacheEvict__?.({ keys });
874
+ await ((_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys }));
829
875
  return true;
830
876
  }
831
877
  };
@@ -836,19 +882,23 @@ var LFUIndexedDBStorage = class extends BaseIndexedDBStorage {
836
882
  super(options);
837
883
  }
838
884
  async get(key) {
885
+ var _a;
839
886
  const entry = await super.get(key);
840
- this.__onCacheGet__?.({ key });
887
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
841
888
  return entry;
842
889
  }
843
890
  async set(value) {
891
+ var _a;
844
892
  await super.set(value);
845
- this.__onCacheSet__?.({ key: value.key });
893
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
846
894
  }
847
895
  async remove(key) {
896
+ var _a;
848
897
  await super.remove(key);
849
- this.__onCacheRemove__?.({ key });
898
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
850
899
  }
851
900
  async evict(expectSize) {
901
+ var _a;
852
902
  await this.evictExpired();
853
903
  const size = await this.getSize();
854
904
  let deficitSize = expectSize - size.free;
@@ -871,13 +921,13 @@ var LFUIndexedDBStorage = class extends BaseIndexedDBStorage {
871
921
  cursor = await cursor.continue();
872
922
  }
873
923
  if (deficitSize > 0) {
874
- this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize}`));
924
+ this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
875
925
  await tx.abort;
876
926
  return false;
877
927
  }
878
928
  await this.__remove__(tx, keys);
879
929
  await tx.done;
880
- this.__onCacheEvict__?.({ keys });
930
+ (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
881
931
  return true;
882
932
  }
883
933
  };
@@ -888,19 +938,23 @@ var LRUIndexedDBStorage = class extends BaseIndexedDBStorage {
888
938
  super(options);
889
939
  }
890
940
  async get(key) {
941
+ var _a;
891
942
  const entry = await super.get(key);
892
- this.__onCacheGet__?.({ key });
943
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
893
944
  return entry;
894
945
  }
895
946
  async set(value) {
947
+ var _a;
896
948
  await super.set(value);
897
- this.__onCacheSet__?.({ key: value.key });
949
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
898
950
  }
899
951
  async remove(key) {
952
+ var _a;
900
953
  await super.remove(key);
901
- this.__onCacheRemove__?.({ key });
954
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
902
955
  }
903
956
  async evict(expectSize) {
957
+ var _a;
904
958
  await this.evictExpired();
905
959
  const size = await this.getSize();
906
960
  let deficitSize = expectSize - size.free;
@@ -923,13 +977,13 @@ var LRUIndexedDBStorage = class extends BaseIndexedDBStorage {
923
977
  cursor = await cursor.continue();
924
978
  }
925
979
  if (deficitSize > 0) {
926
- this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize}`));
980
+ this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
927
981
  await tx.abort();
928
982
  return false;
929
983
  }
930
984
  await this.__remove__(tx, keys);
931
985
  await tx.done;
932
- await this.__onCacheEvict__?.({ keys });
986
+ await ((_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys }));
933
987
  return true;
934
988
  }
935
989
  };
@@ -940,19 +994,23 @@ var TTLIndexedDBStorage = class extends BaseIndexedDBStorage {
940
994
  super(options);
941
995
  }
942
996
  async get(key) {
997
+ var _a;
943
998
  const entry = await super.get(key);
944
- this.__onCacheGet__?.({ key });
999
+ (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
945
1000
  return entry;
946
1001
  }
947
1002
  async set(value) {
1003
+ var _a;
948
1004
  await super.set(value);
949
- this.__onCacheSet__?.({ key: value.key });
1005
+ (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
950
1006
  }
951
1007
  async remove(key) {
1008
+ var _a;
952
1009
  await super.remove(key);
953
- this.__onCacheRemove__?.({ key });
1010
+ (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
954
1011
  }
955
1012
  async evict(expectSize) {
1013
+ var _a;
956
1014
  await this.evictExpired();
957
1015
  const size = await this.getSize();
958
1016
  let deficitSize = expectSize - size.free;
@@ -974,23 +1032,23 @@ var TTLIndexedDBStorage = class extends BaseIndexedDBStorage {
974
1032
  cursor = await cursor.continue();
975
1033
  }
976
1034
  if (deficitSize > 0) {
977
- this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize}`));
1035
+ this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
978
1036
  await tx.abort();
979
1037
  return false;
980
1038
  }
981
1039
  await this.__remove__(tx, keys);
982
1040
  await tx.done;
983
- await this.__onCacheEvict__?.({ keys });
1041
+ await ((_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys }));
984
1042
  return true;
985
1043
  }
986
1044
  };
987
1045
 
988
1046
  // src/storage/indexed-db-storage/indexed-db-storage.ts
989
1047
  var IndexedDBStorage = class extends KeqCacheStorage {
990
- storage;
991
1048
  constructor(options) {
992
1049
  super();
993
- const eviction = options?.eviction || "lru" /* LRU */;
1050
+ __publicField(this, "storage");
1051
+ const eviction = (options == null ? void 0 : options.eviction) || "lru" /* LRU */;
994
1052
  if (eviction === "random" /* RANDOM */) {
995
1053
  this.storage = new RandomIndexedDBStorage(options);
996
1054
  } else if (eviction === "lfu" /* LFU */) {
@@ -1000,7 +1058,7 @@ var IndexedDBStorage = class extends KeqCacheStorage {
1000
1058
  } else if (eviction === "ttl" /* TTL */) {
1001
1059
  this.storage = new TTLIndexedDBStorage(options);
1002
1060
  } else {
1003
- throw TypeError(`Not Supported Eviction: ${String(options?.eviction)}`);
1061
+ throw TypeError("Not Supported Eviction: ".concat(String(options == null ? void 0 : options.eviction)));
1004
1062
  }
1005
1063
  }
1006
1064
  set(entry) {
@@ -1016,13 +1074,13 @@ var IndexedDBStorage = class extends KeqCacheStorage {
1016
1074
 
1017
1075
  // src/storage/multi-tier-storage/multi-tier-storage.ts
1018
1076
  var MultiTierStorage = class extends KeqCacheStorage {
1019
- storages;
1020
1077
  /**
1021
1078
  * @param storages Array of storage instances ordered by performance (fastest first)
1022
1079
  * @zh 按性价比排序的存储实例数组,排在前面的成本低,排在后面的成本高
1023
1080
  */
1024
1081
  constructor(options) {
1025
1082
  super();
1083
+ __publicField(this, "storages");
1026
1084
  if (!options.tiers || options.tiers.length === 0) {
1027
1085
  throw new Error("At least one storage instance is required");
1028
1086
  }
@@ -1082,21 +1140,11 @@ var MultiTierStorage = class extends KeqCacheStorage {
1082
1140
  // src/storage/tier-storage/tier-storage.ts
1083
1141
  var TierStorage = class extends MultiTierStorage {
1084
1142
  constructor(options) {
1085
- const memoryStorage = options?.memory instanceof MemoryStorage ? options.memory : new MemoryStorage(options?.memory);
1086
- const indexedDBStorage = options?.indexedDB instanceof IndexedDBStorage ? options.indexedDB : new IndexedDBStorage(options?.indexedDB);
1143
+ const memoryStorage = (options == null ? void 0 : options.memory) instanceof MemoryStorage ? options.memory : new MemoryStorage(options == null ? void 0 : options.memory);
1144
+ const indexedDBStorage = (options == null ? void 0 : options.indexedDB) instanceof IndexedDBStorage ? options.indexedDB : new IndexedDBStorage(options == null ? void 0 : options.indexedDB);
1087
1145
  super({
1088
1146
  tiers: [memoryStorage, indexedDBStorage]
1089
1147
  });
1090
1148
  }
1091
1149
  };
1092
- // Annotate the CommonJS export names for ESM import in node:
1093
- 0 && (module.exports = {
1094
- CacheStorage,
1095
- Eviction,
1096
- IndexedDBStorage,
1097
- MemoryStorage,
1098
- Strategy,
1099
- TierStorage,
1100
- cache
1101
- });
1102
1150
  //# sourceMappingURL=index.js.map