@keq-request/cache 5.0.0-alpha.34 → 5.0.0-alpha.36

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.
Files changed (151) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/index.d.mts +354 -0
  3. package/dist/index.d.mts.map +1 -0
  4. package/dist/index.d.ts +352 -5
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +1211 -1276
  7. package/dist/index.js.map +1 -1
  8. package/dist/index.mjs +1183 -1251
  9. package/dist/index.mjs.map +1 -1
  10. package/package.json +9 -9
  11. package/tsdown.config.ts +12 -0
  12. package/dist/cache-entry/cache-entry.d.ts +0 -17
  13. package/dist/cache-entry/cache-entry.d.ts.map +0 -1
  14. package/dist/cache-entry/index.d.ts +0 -4
  15. package/dist/cache-entry/index.d.ts.map +0 -1
  16. package/dist/cache-entry/types/cache-entry-build-options.d.ts +0 -8
  17. package/dist/cache-entry/types/cache-entry-build-options.d.ts.map +0 -1
  18. package/dist/cache-entry/types/cache-entry-options.d.ts +0 -7
  19. package/dist/cache-entry/types/cache-entry-options.d.ts.map +0 -1
  20. package/dist/cache.d.ts +0 -44
  21. package/dist/cache.d.ts.map +0 -1
  22. package/dist/constants/eviction.enum.d.ts +0 -7
  23. package/dist/constants/eviction.enum.d.ts.map +0 -1
  24. package/dist/constants/index.d.ts +0 -5
  25. package/dist/constants/index.d.ts.map +0 -1
  26. package/dist/constants/max-expired-at.d.ts +0 -2
  27. package/dist/constants/max-expired-at.d.ts.map +0 -1
  28. package/dist/constants/size.enum.d.ts +0 -7
  29. package/dist/constants/size.enum.d.ts.map +0 -1
  30. package/dist/constants/strategy.enum.d.ts +0 -7
  31. package/dist/constants/strategy.enum.d.ts.map +0 -1
  32. package/dist/exceptions/cache-exception.d.ts +0 -5
  33. package/dist/exceptions/cache-exception.d.ts.map +0 -1
  34. package/dist/exceptions/index.d.ts +0 -2
  35. package/dist/exceptions/index.d.ts.map +0 -1
  36. package/dist/request-cache-handler/index.d.ts +0 -2
  37. package/dist/request-cache-handler/index.d.ts.map +0 -1
  38. package/dist/request-cache-handler/request-cache-handler.d.ts +0 -24
  39. package/dist/request-cache-handler/request-cache-handler.d.ts.map +0 -1
  40. package/dist/storage/index.d.ts +0 -6
  41. package/dist/storage/index.d.ts.map +0 -1
  42. package/dist/storage/indexed-db-storage/base-indexed-db-storage.d.ts +0 -22
  43. package/dist/storage/indexed-db-storage/base-indexed-db-storage.d.ts.map +0 -1
  44. package/dist/storage/indexed-db-storage/constants/default-table-name.d.ts +0 -2
  45. package/dist/storage/indexed-db-storage/constants/default-table-name.d.ts.map +0 -1
  46. package/dist/storage/indexed-db-storage/index.d.ts +0 -3
  47. package/dist/storage/indexed-db-storage/index.d.ts.map +0 -1
  48. package/dist/storage/indexed-db-storage/indexed-db-storage.d.ts +0 -12
  49. package/dist/storage/indexed-db-storage/indexed-db-storage.d.ts.map +0 -1
  50. package/dist/storage/indexed-db-storage/lfu-indexed-db-storage.d.ts +0 -11
  51. package/dist/storage/indexed-db-storage/lfu-indexed-db-storage.d.ts.map +0 -1
  52. package/dist/storage/indexed-db-storage/lru-indexed-db-storage.d.ts +0 -11
  53. package/dist/storage/indexed-db-storage/lru-indexed-db-storage.d.ts.map +0 -1
  54. package/dist/storage/indexed-db-storage/random-indexed-db-storage.d.ts +0 -11
  55. package/dist/storage/indexed-db-storage/random-indexed-db-storage.d.ts.map +0 -1
  56. package/dist/storage/indexed-db-storage/ttl-indexed-db-storage.d.ts +0 -11
  57. package/dist/storage/indexed-db-storage/ttl-indexed-db-storage.d.ts.map +0 -1
  58. package/dist/storage/indexed-db-storage/types/index.d.ts +0 -7
  59. package/dist/storage/indexed-db-storage/types/index.d.ts.map +0 -1
  60. package/dist/storage/indexed-db-storage/types/indexed-db-entry-metadata.d.ts +0 -12
  61. package/dist/storage/indexed-db-storage/types/indexed-db-entry-metadata.d.ts.map +0 -1
  62. package/dist/storage/indexed-db-storage/types/indexed-db-entry-response.d.ts +0 -8
  63. package/dist/storage/indexed-db-storage/types/indexed-db-entry-response.d.ts.map +0 -1
  64. package/dist/storage/indexed-db-storage/types/indexed-db-entry-visits.d.ts +0 -14
  65. package/dist/storage/indexed-db-storage/types/indexed-db-entry-visits.d.ts.map +0 -1
  66. package/dist/storage/indexed-db-storage/types/indexed-db-schema.d.ts +0 -29
  67. package/dist/storage/indexed-db-storage/types/indexed-db-schema.d.ts.map +0 -1
  68. package/dist/storage/indexed-db-storage/types/indexed-db-storage-options.d.ts +0 -14
  69. package/dist/storage/indexed-db-storage/types/indexed-db-storage-options.d.ts.map +0 -1
  70. package/dist/storage/indexed-db-storage/types/indexed-db-storage-size.d.ts +0 -13
  71. package/dist/storage/indexed-db-storage/types/indexed-db-storage-size.d.ts.map +0 -1
  72. package/dist/storage/internal-storage/internal-storage.d.ts +0 -16
  73. package/dist/storage/internal-storage/internal-storage.d.ts.map +0 -1
  74. package/dist/storage/internal-storage/types/events.d.ts +0 -16
  75. package/dist/storage/internal-storage/types/events.d.ts.map +0 -1
  76. package/dist/storage/internal-storage/types/storage-options.d.ts +0 -20
  77. package/dist/storage/internal-storage/types/storage-options.d.ts.map +0 -1
  78. package/dist/storage/keq-cache-storage.d.ts +0 -20
  79. package/dist/storage/keq-cache-storage.d.ts.map +0 -1
  80. package/dist/storage/memory-storage/base-memory-storage.d.ts +0 -31
  81. package/dist/storage/memory-storage/base-memory-storage.d.ts.map +0 -1
  82. package/dist/storage/memory-storage/index.d.ts +0 -3
  83. package/dist/storage/memory-storage/index.d.ts.map +0 -1
  84. package/dist/storage/memory-storage/lfu-memory-storage.d.ts +0 -11
  85. package/dist/storage/memory-storage/lfu-memory-storage.d.ts.map +0 -1
  86. package/dist/storage/memory-storage/lru-memory-storage.d.ts +0 -11
  87. package/dist/storage/memory-storage/lru-memory-storage.d.ts.map +0 -1
  88. package/dist/storage/memory-storage/memory-storage.d.ts +0 -13
  89. package/dist/storage/memory-storage/memory-storage.d.ts.map +0 -1
  90. package/dist/storage/memory-storage/random-memory-storage.d.ts +0 -11
  91. package/dist/storage/memory-storage/random-memory-storage.d.ts.map +0 -1
  92. package/dist/storage/memory-storage/ttl-memory-storage.d.ts +0 -11
  93. package/dist/storage/memory-storage/ttl-memory-storage.d.ts.map +0 -1
  94. package/dist/storage/memory-storage/types/index.d.ts +0 -3
  95. package/dist/storage/memory-storage/types/index.d.ts.map +0 -1
  96. package/dist/storage/memory-storage/types/memory-storage-options.d.ts +0 -9
  97. package/dist/storage/memory-storage/types/memory-storage-options.d.ts.map +0 -1
  98. package/dist/storage/memory-storage/types/memory-storage-size.d.ts +0 -13
  99. package/dist/storage/memory-storage/types/memory-storage-size.d.ts.map +0 -1
  100. package/dist/storage/memory-storage/utils/humanize-size.d.ts +0 -6
  101. package/dist/storage/memory-storage/utils/humanize-size.d.ts.map +0 -1
  102. package/dist/storage/memory-storage/utils/index.d.ts +0 -3
  103. package/dist/storage/memory-storage/utils/index.d.ts.map +0 -1
  104. package/dist/storage/memory-storage/utils/serialize-response-body.d.ts +0 -6
  105. package/dist/storage/memory-storage/utils/serialize-response-body.d.ts.map +0 -1
  106. package/dist/storage/multi-tier-storage/index.d.ts +0 -3
  107. package/dist/storage/multi-tier-storage/index.d.ts.map +0 -1
  108. package/dist/storage/multi-tier-storage/multi-tier-storage.d.ts +0 -36
  109. package/dist/storage/multi-tier-storage/multi-tier-storage.d.ts.map +0 -1
  110. package/dist/storage/multi-tier-storage/types/index.d.ts +0 -2
  111. package/dist/storage/multi-tier-storage/types/index.d.ts.map +0 -1
  112. package/dist/storage/multi-tier-storage/types/multi-tier-storage-options.d.ts +0 -5
  113. package/dist/storage/multi-tier-storage/types/multi-tier-storage-options.d.ts.map +0 -1
  114. package/dist/storage/tier-storage/index.d.ts +0 -3
  115. package/dist/storage/tier-storage/index.d.ts.map +0 -1
  116. package/dist/storage/tier-storage/tier-storage.d.ts +0 -15
  117. package/dist/storage/tier-storage/tier-storage.d.ts.map +0 -1
  118. package/dist/storage/tier-storage/types/index.d.ts +0 -2
  119. package/dist/storage/tier-storage/types/index.d.ts.map +0 -1
  120. package/dist/storage/tier-storage/types/tier-storage-options.d.ts +0 -21
  121. package/dist/storage/tier-storage/types/tier-storage-options.d.ts.map +0 -1
  122. package/dist/strategies/cache-first.d.ts +0 -3
  123. package/dist/strategies/cache-first.d.ts.map +0 -1
  124. package/dist/strategies/index.d.ts +0 -5
  125. package/dist/strategies/index.d.ts.map +0 -1
  126. package/dist/strategies/network-first.d.ts +0 -3
  127. package/dist/strategies/network-first.d.ts.map +0 -1
  128. package/dist/strategies/network-only.d.ts +0 -3
  129. package/dist/strategies/network-only.d.ts.map +0 -1
  130. package/dist/strategies/stale-while-revalidate.d.ts +0 -3
  131. package/dist/strategies/stale-while-revalidate.d.ts.map +0 -1
  132. package/dist/types/index.d.ts +0 -6
  133. package/dist/types/index.d.ts.map +0 -1
  134. package/dist/types/keq-cache-key.d.ts +0 -4
  135. package/dist/types/keq-cache-key.d.ts.map +0 -1
  136. package/dist/types/keq-cache-pattern.d.ts +0 -3
  137. package/dist/types/keq-cache-pattern.d.ts.map +0 -1
  138. package/dist/types/keq-cache-rule.d.ts +0 -6
  139. package/dist/types/keq-cache-rule.d.ts.map +0 -1
  140. package/dist/types/keq-cache-strategy.d.ts +0 -6
  141. package/dist/types/keq-cache-strategy.d.ts.map +0 -1
  142. package/dist/types/request-cache-options.d.ts +0 -45
  143. package/dist/types/request-cache-options.d.ts.map +0 -1
  144. package/dist/utils/get-response-bytes.d.ts +0 -2
  145. package/dist/utils/get-response-bytes.d.ts.map +0 -1
  146. package/dist/utils/index.d.ts +0 -4
  147. package/dist/utils/index.d.ts.map +0 -1
  148. package/dist/utils/logger.d.ts +0 -5
  149. package/dist/utils/logger.d.ts.map +0 -1
  150. package/dist/utils/random.d.ts +0 -2
  151. package/dist/utils/random.d.ts.map +0 -1
package/dist/index.mjs CHANGED
@@ -1,1305 +1,1237 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
-
5
- // src/cache.ts
6
- import * as R2 from "ramda";
7
- import * as fastq from "fastq";
8
-
9
- // src/request-cache-handler/request-cache-handler.ts
10
1
  import * as R from "ramda";
11
-
12
- // src/exceptions/cache-exception.ts
13
- import { Exception } from "keq";
2
+ import * as fastq from "fastq";
3
+ import { Exception, createProxyResponse } from "keq";
4
+ import dayjs from "dayjs";
5
+ import { openDB } from "idb";
6
+ //#region src/exceptions/cache-exception.ts
14
7
  var CacheException = class extends Exception {
15
- constructor(message) {
16
- super("[@keq-request/cache] ".concat(message));
17
- }
8
+ constructor(message) {
9
+ super(`[@keq-request/cache] ${message}`);
10
+ }
18
11
  };
19
-
20
- // src/cache-entry/cache-entry.ts
21
- import { createProxyResponse } from "keq";
22
-
23
- // src/utils/get-response-bytes.ts
12
+ //#endregion
13
+ //#region src/utils/get-response-bytes.ts
24
14
  async function getResponseBytes(response) {
25
- const contentLength = response.headers.get("content-length");
26
- if (contentLength) {
27
- return parseInt(contentLength);
28
- }
29
- const arrayBuffer = await response.clone().arrayBuffer();
30
- return arrayBuffer.byteLength;
15
+ const contentLength = response.headers.get("content-length");
16
+ if (contentLength) return parseInt(contentLength);
17
+ return (await response.clone().arrayBuffer()).byteLength;
31
18
  }
32
-
33
- // src/constants/max-expired-at.ts
34
- var MAX_EXPIRED_AT = /* @__PURE__ */ new Date(864e13);
35
-
36
- // src/constants/eviction.enum.ts
37
- var Eviction = /* @__PURE__ */ ((Eviction2) => {
38
- Eviction2["LRU"] = "lru";
39
- Eviction2["LFU"] = "lfu";
40
- Eviction2["RANDOM"] = "random";
41
- Eviction2["TTL"] = "ttl";
42
- return Eviction2;
43
- })(Eviction || {});
44
-
45
- // src/constants/size.enum.ts
46
- var Size = /* @__PURE__ */ ((Size2) => {
47
- Size2[Size2["B"] = 1] = "B";
48
- Size2[Size2["KB"] = 1024] = "KB";
49
- Size2[Size2["MB"] = 1048576] = "MB";
50
- Size2[Size2["GB"] = 1073741824] = "GB";
51
- return Size2;
52
- })(Size || {});
53
-
54
- // src/utils/logger.ts
19
+ //#endregion
20
+ //#region src/constants/max-expired-at.ts
21
+ const MAX_EXPIRED_AT = /* @__PURE__ */ new Date(864e13);
22
+ //#endregion
23
+ //#region src/constants/eviction.enum.ts
24
+ let Eviction = /* @__PURE__ */ function(Eviction) {
25
+ Eviction["LRU"] = "lru";
26
+ Eviction["LFU"] = "lfu";
27
+ Eviction["RANDOM"] = "random";
28
+ Eviction["TTL"] = "ttl";
29
+ return Eviction;
30
+ }({});
31
+ //#endregion
32
+ //#region src/constants/size.enum.ts
33
+ let Size = /* @__PURE__ */ function(Size) {
34
+ Size[Size["B"] = 1] = "B";
35
+ Size[Size["KB"] = 1024] = "KB";
36
+ Size[Size["MB"] = 1048576] = "MB";
37
+ Size[Size["GB"] = 1073741824] = "GB";
38
+ return Size;
39
+ }({});
40
+ //#endregion
41
+ //#region src/utils/logger.ts
55
42
  var Logger = class {
56
- static debug(...args) {
57
- console.debug("[@keq-request/cache] [DEBUG] ", ...args);
58
- }
59
- static error(...args) {
60
- console.error("[@keq-request/cache] [ERROR] ", ...args);
61
- }
43
+ static debug(...args) {
44
+ console.debug("[@keq-request/cache] [DEBUG] ", ...args);
45
+ }
46
+ static error(...args) {
47
+ console.error("[@keq-request/cache] [ERROR] ", ...args);
48
+ }
62
49
  };
63
-
64
- // src/utils/random.ts
50
+ //#endregion
51
+ //#region src/utils/random.ts
65
52
  function random(min, max) {
66
- return Math.floor(Math.random() * (max - min)) + min;
53
+ return Math.floor(Math.random() * (max - min)) + min;
67
54
  }
68
-
69
- // src/strategies/cache-first.ts
70
- var cacheFirst = async function cacheFirst2(handler, context, next) {
71
- const [cacheKey, cacheValue] = await handler.getCache();
72
- if (handler.options.debug) {
73
- Logger.debug([
74
- "",
75
- "Request: ".concat(context.request.method.toUpperCase(), " ").concat(context.request.__url__.href),
76
- "Strategy: Cache First",
77
- "Cache Key: ".concat(cacheKey),
78
- "Cache Status: ".concat(cacheValue ? "HIT" : "MISS")
79
- ].join("\n"));
80
- }
81
- if (cacheValue) {
82
- context.emitter.emit("cache:hit", { key: cacheKey, response: cacheValue.response, context });
83
- context.res = cacheValue.response;
84
- return;
85
- }
86
- context.emitter.emit("cache:miss", { key: cacheKey, context });
87
- await next();
88
- const [, entry] = await handler.setCache(context);
89
- if (handler.options.debug) {
90
- Logger.debug([
91
- "",
92
- "Request: ".concat(context.request.method.toUpperCase(), " ").concat(context.request.__url__.href),
93
- "Strategy: Cache First",
94
- "Cache Key: ".concat(cacheKey),
95
- "ACTIONS: ".concat(entry ? "UPDATED" : "EXCLUDED")
96
- ].join("\n"));
97
- }
98
- if (entry) {
99
- context.emitter.emit("cache:update", {
100
- key: entry.key,
101
- oldResponse: void 0,
102
- newResponse: entry.response,
103
- context
104
- });
105
- }
55
+ //#endregion
56
+ //#region src/strategies/cache-first.ts
57
+ const cacheFirst = async function cacheFirst(handler, context, next) {
58
+ const [cacheKey, cacheValue] = await handler.getCache();
59
+ if (handler.options.debug) Logger.debug([
60
+ "",
61
+ `Request: ${context.request.method.toUpperCase()} ${context.request.__url__.href}`,
62
+ "Strategy: Cache First",
63
+ `Cache Key: ${cacheKey}`,
64
+ `Cache Status: ${cacheValue ? "HIT" : "MISS"}`
65
+ ].join("\n"));
66
+ if (cacheValue) {
67
+ context.emitter.emit("cache:hit", {
68
+ key: cacheKey,
69
+ response: cacheValue.response,
70
+ context
71
+ });
72
+ context.res = cacheValue.response;
73
+ return;
74
+ }
75
+ context.emitter.emit("cache:miss", {
76
+ key: cacheKey,
77
+ context
78
+ });
79
+ await next();
80
+ const [, entry] = await handler.setCache(context);
81
+ if (handler.options.debug) Logger.debug([
82
+ "",
83
+ `Request: ${context.request.method.toUpperCase()} ${context.request.__url__.href}`,
84
+ "Strategy: Cache First",
85
+ `Cache Key: ${cacheKey}`,
86
+ `ACTIONS: ${entry ? "UPDATED" : "EXCLUDED"}`
87
+ ].join("\n"));
88
+ if (entry) context.emitter.emit("cache:update", {
89
+ key: entry.key,
90
+ oldResponse: void 0,
91
+ newResponse: entry.response,
92
+ context
93
+ });
106
94
  };
107
-
108
- // src/strategies/network-first.ts
109
- var networkFirst = async function networkFirst2(handler, context, next) {
110
- try {
111
- await next();
112
- const [cacheKey, cache2] = await handler.getCache();
113
- const [, entry] = await handler.setCache(context);
114
- if (handler.options.debug) {
115
- Logger.debug([
116
- "",
117
- "Request: ".concat(context.request.method.toUpperCase(), " ").concat(context.request.__url__.href),
118
- "Strategy: Network First",
119
- "Cache Key: ".concat(cacheKey),
120
- "ACTIONS: ".concat(entry ? "UPDATED" : "EXCLUDED")
121
- ].join("\n"));
122
- }
123
- if (entry) {
124
- context.emitter.emit("cache:update", {
125
- key: entry.key,
126
- oldResponse: cache2 == null ? void 0 : cache2.response,
127
- newResponse: entry.response,
128
- context
129
- });
130
- }
131
- } catch (err) {
132
- const [cacheKey, cache2] = await handler.getCache();
133
- if (handler.options.debug) {
134
- Logger.debug([
135
- "",
136
- "Request: ".concat(context.request.method.toUpperCase(), " ").concat(context.request.__url__.href),
137
- "Strategy: Network First",
138
- "Cache Key: ".concat(cacheKey),
139
- "Cache Status: ".concat(cache2 ? "HIT" : "MISS")
140
- ].join("\n"));
141
- }
142
- if (!cache2) {
143
- context.emitter.emit("cache:miss", { key: cacheKey, context });
144
- throw err;
145
- }
146
- context.emitter.emit("cache:hit", { key: cacheKey, response: cache2.response, context });
147
- context.res = cache2.response;
148
- }
95
+ //#endregion
96
+ //#region src/strategies/network-first.ts
97
+ const networkFirst = async function networkFirst(handler, context, next) {
98
+ try {
99
+ await next();
100
+ const [cacheKey, cache] = await handler.getCache();
101
+ const [, entry] = await handler.setCache(context);
102
+ if (handler.options.debug) Logger.debug([
103
+ "",
104
+ `Request: ${context.request.method.toUpperCase()} ${context.request.__url__.href}`,
105
+ "Strategy: Network First",
106
+ `Cache Key: ${cacheKey}`,
107
+ `ACTIONS: ${entry ? "UPDATED" : "EXCLUDED"}`
108
+ ].join("\n"));
109
+ if (entry) context.emitter.emit("cache:update", {
110
+ key: entry.key,
111
+ oldResponse: cache?.response,
112
+ newResponse: entry.response,
113
+ context
114
+ });
115
+ } catch (err) {
116
+ const [cacheKey, cache] = await handler.getCache();
117
+ if (handler.options.debug) Logger.debug([
118
+ "",
119
+ `Request: ${context.request.method.toUpperCase()} ${context.request.__url__.href}`,
120
+ "Strategy: Network First",
121
+ `Cache Key: ${cacheKey}`,
122
+ `Cache Status: ${cache ? "HIT" : "MISS"}`
123
+ ].join("\n"));
124
+ if (!cache) {
125
+ context.emitter.emit("cache:miss", {
126
+ key: cacheKey,
127
+ context
128
+ });
129
+ throw err;
130
+ }
131
+ context.emitter.emit("cache:hit", {
132
+ key: cacheKey,
133
+ response: cache.response,
134
+ context
135
+ });
136
+ context.res = cache.response;
137
+ }
149
138
  };
150
-
151
- // src/strategies/network-only.ts
152
- var networkOnly = async function(handler, context, next) {
153
- await next();
139
+ //#endregion
140
+ //#region src/strategies/network-only.ts
141
+ const networkOnly = async function(handler, context, next) {
142
+ await next();
154
143
  };
155
-
156
- // src/strategies/stale-while-revalidate.ts
157
- var staleWhileRevalidate = async function(handler, context, next) {
158
- const [cacheKey, cache2] = await handler.getCache();
159
- if (handler.options.debug) {
160
- Logger.debug([
161
- "",
162
- "Request: ".concat(context.request.method.toUpperCase(), " ").concat(context.request.__url__.href),
163
- "Strategy: Stale While Revalidate",
164
- "Cache Key: ".concat(cacheKey),
165
- "Cache Status: ".concat(cache2 ? "HIT" : "MISS")
166
- ].join("\n"));
167
- }
168
- if (cache2) {
169
- context.emitter.emit("cache:hit", { key: cacheKey, response: cache2.response, context });
170
- } else {
171
- context.emitter.emit("cache:miss", { key: cacheKey, context });
172
- }
173
- if (cache2) {
174
- const orchestrator = context.orchestration.fork();
175
- context.res = cache2.response;
176
- setTimeout(async () => {
177
- try {
178
- await orchestrator.execute();
179
- const context2 = orchestrator.context;
180
- const [cacheKey2, entry] = await handler.setCache(context2);
181
- if (handler.options.debug) {
182
- Logger.debug([
183
- "",
184
- "Request: ".concat(context2.request.method.toUpperCase(), " ").concat(context2.request.__url__.href),
185
- "Strategy: Stale While Revalidate",
186
- "Cache Key: ".concat(cacheKey2),
187
- "ACTIONS: ".concat(entry ? "UPDATED" : "EXCLUDED")
188
- ].join("\n"));
189
- }
190
- if (entry) {
191
- context2.emitter.emit("cache:update", {
192
- key: cacheKey2,
193
- oldResponse: cache2.response,
194
- newResponse: entry.response,
195
- context: context2
196
- });
197
- }
198
- } catch (err) {
199
- }
200
- }, 1);
201
- } else {
202
- await next();
203
- const [cacheKey2, entry] = await handler.setCache(context);
204
- if (handler.options.debug) {
205
- Logger.debug([
206
- "",
207
- "Request: ".concat(context.request.method.toUpperCase(), " ").concat(context.request.__url__.href),
208
- "Strategy: Stale While Revalidate",
209
- "Cache Key: ".concat(cacheKey2),
210
- "ACTIONS: ".concat(entry ? "UPDATED" : "EXCLUDED")
211
- ].join("\n"));
212
- }
213
- if (entry) {
214
- context.emitter.emit("cache:update", {
215
- key: cacheKey2,
216
- oldResponse: void 0,
217
- newResponse: entry.response,
218
- context
219
- });
220
- }
221
- }
144
+ //#endregion
145
+ //#region src/strategies/stale-while-revalidate.ts
146
+ const staleWhileRevalidate = async function(handler, context, next) {
147
+ const [cacheKey, cache] = await handler.getCache();
148
+ if (handler.options.debug) Logger.debug([
149
+ "",
150
+ `Request: ${context.request.method.toUpperCase()} ${context.request.__url__.href}`,
151
+ "Strategy: Stale While Revalidate",
152
+ `Cache Key: ${cacheKey}`,
153
+ `Cache Status: ${cache ? "HIT" : "MISS"}`
154
+ ].join("\n"));
155
+ if (cache) context.emitter.emit("cache:hit", {
156
+ key: cacheKey,
157
+ response: cache.response,
158
+ context
159
+ });
160
+ else context.emitter.emit("cache:miss", {
161
+ key: cacheKey,
162
+ context
163
+ });
164
+ if (cache) {
165
+ const orchestrator = context.orchestration.fork();
166
+ context.res = cache.response;
167
+ setTimeout(async () => {
168
+ try {
169
+ await orchestrator.execute();
170
+ const context = orchestrator.context;
171
+ const [cacheKey, entry] = await handler.setCache(context);
172
+ if (handler.options.debug) Logger.debug([
173
+ "",
174
+ `Request: ${context.request.method.toUpperCase()} ${context.request.__url__.href}`,
175
+ "Strategy: Stale While Revalidate",
176
+ `Cache Key: ${cacheKey}`,
177
+ `ACTIONS: ${entry ? "UPDATED" : "EXCLUDED"}`
178
+ ].join("\n"));
179
+ if (entry) context.emitter.emit("cache:update", {
180
+ key: cacheKey,
181
+ oldResponse: cache.response,
182
+ newResponse: entry.response,
183
+ context
184
+ });
185
+ } catch (err) {}
186
+ }, 1);
187
+ } else {
188
+ await next();
189
+ const [cacheKey, entry] = await handler.setCache(context);
190
+ if (handler.options.debug) Logger.debug([
191
+ "",
192
+ `Request: ${context.request.method.toUpperCase()} ${context.request.__url__.href}`,
193
+ "Strategy: Stale While Revalidate",
194
+ `Cache Key: ${cacheKey}`,
195
+ `ACTIONS: ${entry ? "UPDATED" : "EXCLUDED"}`
196
+ ].join("\n"));
197
+ if (entry) context.emitter.emit("cache:update", {
198
+ key: cacheKey,
199
+ oldResponse: void 0,
200
+ newResponse: entry.response,
201
+ context
202
+ });
203
+ }
222
204
  };
223
-
224
- // src/constants/strategy.enum.ts
225
- var Strategy = {
226
- STALE_WHILE_REVALIDATE: staleWhileRevalidate,
227
- NETWORK_FIRST: networkFirst,
228
- NETWORK_ONLY: networkOnly,
229
- CACHE_FIRST: cacheFirst
205
+ //#endregion
206
+ //#region src/constants/strategy.enum.ts
207
+ const Strategy = {
208
+ STALE_WHILE_REVALIDATE: staleWhileRevalidate,
209
+ NETWORK_FIRST: networkFirst,
210
+ NETWORK_ONLY: networkOnly,
211
+ CACHE_FIRST: cacheFirst
230
212
  };
231
-
232
- // src/cache-entry/cache-entry.ts
233
- var CacheEntry = class _CacheEntry {
234
- constructor(options) {
235
- __publicField(this, "key");
236
- __publicField(this, "response");
237
- /**
238
- * @en bytes
239
- * @zh 字节数
240
- */
241
- __publicField(this, "size");
242
- __publicField(this, "expiredAt");
243
- var _a;
244
- this.key = options.key;
245
- this.response = createProxyResponse(options.response);
246
- this.size = options.size;
247
- this.expiredAt = (_a = options.expiredAt) != null ? _a : MAX_EXPIRED_AT;
248
- }
249
- static async build(options) {
250
- var _a;
251
- const expiredAt = "expiredAt" in options ? options.expiredAt : "ttl" in options && typeof options.ttl === "number" && options.ttl > 0 ? new Date(Date.now() + options.ttl * 1e3) : MAX_EXPIRED_AT;
252
- const response = options.response.clone();
253
- return new _CacheEntry({
254
- key: options.key,
255
- response,
256
- size: (_a = options.size) != null ? _a : await getResponseBytes(response),
257
- expiredAt
258
- });
259
- }
260
- clone() {
261
- return new _CacheEntry({
262
- key: this.key,
263
- response: this.response.clone(),
264
- size: this.size,
265
- expiredAt: this.expiredAt
266
- });
267
- }
268
- assignResponseHeaders(headers) {
269
- this.response = new Response(this.response.body, {
270
- status: this.response.status,
271
- statusText: this.response.statusText,
272
- headers
273
- });
274
- }
213
+ //#endregion
214
+ //#region \0@oxc-project+runtime@0.127.0/helpers/typeof.js
215
+ function _typeof(o) {
216
+ "@babel/helpers - typeof";
217
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
218
+ return typeof o;
219
+ } : function(o) {
220
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
221
+ }, _typeof(o);
222
+ }
223
+ //#endregion
224
+ //#region \0@oxc-project+runtime@0.127.0/helpers/toPrimitive.js
225
+ function toPrimitive(t, r) {
226
+ if ("object" != _typeof(t) || !t) return t;
227
+ var e = t[Symbol.toPrimitive];
228
+ if (void 0 !== e) {
229
+ var i = e.call(t, r || "default");
230
+ if ("object" != _typeof(i)) return i;
231
+ throw new TypeError("@@toPrimitive must return a primitive value.");
232
+ }
233
+ return ("string" === r ? String : Number)(t);
234
+ }
235
+ //#endregion
236
+ //#region \0@oxc-project+runtime@0.127.0/helpers/toPropertyKey.js
237
+ function toPropertyKey(t) {
238
+ var i = toPrimitive(t, "string");
239
+ return "symbol" == _typeof(i) ? i : i + "";
240
+ }
241
+ //#endregion
242
+ //#region \0@oxc-project+runtime@0.127.0/helpers/defineProperty.js
243
+ function _defineProperty(e, r, t) {
244
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
245
+ value: t,
246
+ enumerable: !0,
247
+ configurable: !0,
248
+ writable: !0
249
+ }) : e[r] = t, e;
250
+ }
251
+ //#endregion
252
+ //#region src/cache-entry/cache-entry.ts
253
+ var CacheEntry = class CacheEntry {
254
+ constructor(options) {
255
+ _defineProperty(this, "key", void 0);
256
+ _defineProperty(this, "response", void 0);
257
+ _defineProperty(
258
+ this,
259
+ /**
260
+ * @en bytes
261
+ * @zh 字节数
262
+ */
263
+ "size",
264
+ void 0
265
+ );
266
+ _defineProperty(this, "expiredAt", void 0);
267
+ this.key = options.key;
268
+ this.response = createProxyResponse(options.response);
269
+ this.size = options.size;
270
+ this.expiredAt = options.expiredAt ?? MAX_EXPIRED_AT;
271
+ }
272
+ static async build(options) {
273
+ const expiredAt = "expiredAt" in options ? options.expiredAt : "ttl" in options && typeof options.ttl === "number" && options.ttl > 0 ? new Date(Date.now() + options.ttl * 1e3) : MAX_EXPIRED_AT;
274
+ const response = options.response.clone();
275
+ return new CacheEntry({
276
+ key: options.key,
277
+ response,
278
+ size: options.size ?? await getResponseBytes(response),
279
+ expiredAt
280
+ });
281
+ }
282
+ clone() {
283
+ return new CacheEntry({
284
+ key: this.key,
285
+ response: this.response.clone(),
286
+ size: this.size,
287
+ expiredAt: this.expiredAt
288
+ });
289
+ }
290
+ assignResponseHeaders(headers) {
291
+ this.response = new Response(this.response.body, {
292
+ status: this.response.status,
293
+ statusText: this.response.statusText,
294
+ headers
295
+ });
296
+ }
275
297
  };
276
-
277
- // src/request-cache-handler/request-cache-handler.ts
298
+ //#endregion
299
+ //#region src/request-cache-handler/request-cache-handler.ts
278
300
  var RequestCacheHandler = class {
279
- constructor(cacheKey, storage, options) {
280
- this.cacheKey = cacheKey;
281
- this.storage = storage;
282
- this.options = options;
283
- }
284
- /**
285
- * Resolve cache key for request context
286
- */
287
- static resolveRequestCacheKey(context, options) {
288
- if (typeof options.key === "string") return options.key;
289
- else if (typeof options.key === "function") return options.key(context);
290
- else if (R.isNil(options.key) && context.locationId) return context.locationId;
291
- else throw new CacheException("Cannot resolve cache key");
292
- }
293
- /**
294
- * Get cache from storage
295
- */
296
- async getCache() {
297
- const key = this.cacheKey;
298
- if (this.options.serverTiming) {
299
- const startAt = /* @__PURE__ */ new Date();
300
- const entry = await this.storage.get(key);
301
- if (entry) {
302
- const dur = (/* @__PURE__ */ new Date()).getTime() - startAt.getTime();
303
- const HeadersWithServerTiming = new Headers(entry.response.headers);
304
- HeadersWithServerTiming.set("Server-Timing", "keq-cache; dur=".concat(dur, '; desc="HIT"'));
305
- entry.assignResponseHeaders(HeadersWithServerTiming);
306
- }
307
- return [key, entry];
308
- }
309
- return [key, await this.storage.get(key)];
310
- }
311
- /**
312
- * Store response that in context to storage
313
- */
314
- async setCache(context) {
315
- const options = this.options;
316
- const key = this.cacheKey;
317
- if (!context.response) return [key, void 0];
318
- if (options.exclude && await options.exclude(context.response)) return [key, void 0];
319
- const entry = await CacheEntry.build({
320
- key,
321
- response: context.response,
322
- ttl: options.ttl
323
- });
324
- void this.storage.set(entry);
325
- return [key, entry];
326
- }
301
+ constructor(cacheKey, storage, options) {
302
+ this.cacheKey = cacheKey;
303
+ this.storage = storage;
304
+ this.options = options;
305
+ }
306
+ /**
307
+ * Resolve cache key for request context
308
+ */
309
+ static resolveRequestCacheKey(context, options) {
310
+ if (typeof options.key === "string") return options.key;
311
+ else if (typeof options.key === "function") return options.key(context);
312
+ else if (R.isNil(options.key) && context.locationId) return context.locationId;
313
+ else throw new CacheException("Cannot resolve cache key");
314
+ }
315
+ /**
316
+ * Get cache from storage
317
+ */
318
+ async getCache() {
319
+ const key = this.cacheKey;
320
+ if (this.options.serverTiming) {
321
+ const startAt = /* @__PURE__ */ new Date();
322
+ const entry = await this.storage.get(key);
323
+ if (entry) {
324
+ const dur = (/* @__PURE__ */ new Date()).getTime() - startAt.getTime();
325
+ const HeadersWithServerTiming = new Headers(entry.response.headers);
326
+ HeadersWithServerTiming.set("Server-Timing", `keq-cache; dur=${dur}; desc="HIT"`);
327
+ entry.assignResponseHeaders(HeadersWithServerTiming);
328
+ }
329
+ return [key, entry];
330
+ }
331
+ return [key, await this.storage.get(key)];
332
+ }
333
+ /**
334
+ * Store response that in context to storage
335
+ */
336
+ async setCache(context) {
337
+ const options = this.options;
338
+ const key = this.cacheKey;
339
+ if (!context.response) return [key, void 0];
340
+ if (options.exclude && await options.exclude(context.response)) return [key, void 0];
341
+ const entry = await CacheEntry.build({
342
+ key,
343
+ response: context.response,
344
+ ttl: options.ttl
345
+ });
346
+ this.storage.set(entry);
347
+ return [key, entry];
348
+ }
327
349
  };
328
-
329
- // src/cache.ts
350
+ //#endregion
351
+ //#region src/cache.ts
330
352
  function cache(options) {
331
- const storage = options.storage;
332
- const rules = (options == null ? void 0 : options.rules) || [];
333
- return async function cache2(ctx, next) {
334
- if (ctx.options.cache === false) {
335
- await next();
336
- return;
337
- }
338
- let requestCacheOptions = ctx.options.cache;
339
- const rule = rules.find((rule2) => {
340
- if (rule2.pattern === void 0 || rule2.pattern === true) return true;
341
- if (typeof rule2.pattern === "function") return rule2.pattern(ctx);
342
- return rule2.pattern.test(ctx.request.__url__.href);
343
- });
344
- if (rule) requestCacheOptions = R2.mergeRight(rule, requestCacheOptions || {});
345
- if (!requestCacheOptions || R2.isEmpty(requestCacheOptions)) {
346
- await next();
347
- return;
348
- }
349
- if (!requestCacheOptions.key) requestCacheOptions.key = options.keyFactory;
350
- if (!ctx.locationId && !requestCacheOptions.key) {
351
- console.warn("[@keq/cache] Warning: Cannot resolve Cache Key. Cache is skipped.");
352
- await next();
353
- return;
354
- }
355
- if (requestCacheOptions.serverTiming === void 0 && options.serverTiming !== void 0) {
356
- requestCacheOptions.serverTiming = options.serverTiming;
357
- }
358
- const cacheKey = RequestCacheHandler.resolveRequestCacheKey(ctx, requestCacheOptions);
359
- const handler = new RequestCacheHandler(cacheKey, storage, requestCacheOptions);
360
- const strategy = requestCacheOptions.strategy;
361
- if (requestCacheOptions.concurrent) {
362
- await strategy(handler, ctx, next);
363
- } else {
364
- if (!ctx.global.core) ctx.global.core = {};
365
- if (!ctx.global.core.cache) ctx.global.core.cache = {};
366
- if (!ctx.global.core.cache[cacheKey]) {
367
- ctx.global.core.cache[cacheKey] = fastq.promise(async ({ next: next2 }) => {
368
- await next2();
369
- }, 1);
370
- }
371
- const queue = ctx.global.core.cache[cacheKey];
372
- await queue.push({
373
- next: async () => {
374
- await strategy(handler, ctx, next);
375
- }
376
- });
377
- }
378
- };
353
+ const storage = options.storage;
354
+ const rules = options?.rules || [];
355
+ return async function cache(ctx, next) {
356
+ if (ctx.options.cache === false) {
357
+ await next();
358
+ return;
359
+ }
360
+ let requestCacheOptions = ctx.options.cache;
361
+ const rule = rules.find((rule) => {
362
+ if (rule.pattern === void 0 || rule.pattern === true) return true;
363
+ if (typeof rule.pattern === "function") return rule.pattern(ctx);
364
+ return rule.pattern.test(ctx.request.__url__.href);
365
+ });
366
+ if (rule) requestCacheOptions = R.mergeRight(rule, requestCacheOptions || {});
367
+ if (!requestCacheOptions || R.isEmpty(requestCacheOptions)) {
368
+ await next();
369
+ return;
370
+ }
371
+ if (!requestCacheOptions.key) requestCacheOptions.key = options.keyFactory;
372
+ if (!ctx.locationId && !requestCacheOptions.key) {
373
+ console.warn("[@keq/cache] Warning: Cannot resolve Cache Key. Cache is skipped.");
374
+ await next();
375
+ return;
376
+ }
377
+ if (requestCacheOptions.serverTiming === void 0 && options.serverTiming !== void 0) requestCacheOptions.serverTiming = options.serverTiming;
378
+ const cacheKey = RequestCacheHandler.resolveRequestCacheKey(ctx, requestCacheOptions);
379
+ const handler = new RequestCacheHandler(cacheKey, storage, requestCacheOptions);
380
+ const strategy = requestCacheOptions.strategy;
381
+ if (requestCacheOptions.concurrent) await strategy(handler, ctx, next);
382
+ else {
383
+ if (!ctx.global.core) ctx.global.core = {};
384
+ if (!ctx.global.core.cache) ctx.global.core.cache = {};
385
+ if (!ctx.global.core.cache[cacheKey]) ctx.global.core.cache[cacheKey] = fastq.promise(async ({ next }) => {
386
+ await next();
387
+ }, 1);
388
+ await ctx.global.core.cache[cacheKey].push({ next: async () => {
389
+ await strategy(handler, ctx, next);
390
+ } });
391
+ }
392
+ };
379
393
  }
380
-
381
- // src/storage/memory-storage/ttl-memory-storage.ts
382
- import dayjs2 from "dayjs";
383
- import * as R4 from "ramda";
384
-
385
- // src/storage/memory-storage/base-memory-storage.ts
386
- import dayjs from "dayjs";
387
- import * as R3 from "ramda";
388
-
389
- // src/storage/keq-cache-storage.ts
390
- var KeqCacheStorage = class {
391
- };
392
-
393
- // src/storage/internal-storage/internal-storage.ts
394
+ //#endregion
395
+ //#region src/storage/keq-cache-storage.ts
396
+ var KeqCacheStorage = class {};
397
+ //#endregion
398
+ //#region src/storage/internal-storage/internal-storage.ts
394
399
  var InternalStorage = class extends KeqCacheStorage {
395
- constructor(options) {
396
- var _a;
397
- super();
398
- __publicField(this, "__id__", Math.random().toString(36).slice(2));
399
- __publicField(this, "__size__");
400
- __publicField(this, "__debug__");
401
- __publicField(this, "__onCacheGet__");
402
- __publicField(this, "__onCacheSet__");
403
- __publicField(this, "__onCacheRemove__");
404
- __publicField(this, "__onCacheEvict__");
405
- __publicField(this, "__onCacheExpired__");
406
- if ((options == null ? void 0 : options.size) && (typeof (options == null ? void 0 : options.size) !== "number" || options.size <= 0)) {
407
- throw new CacheException("Invalid size: ".concat(String(options == null ? void 0 : options.size)));
408
- }
409
- this.__size__ = (_a = options == null ? void 0 : options.size) != null ? _a : Infinity;
410
- this.__debug__ = !!(options == null ? void 0 : options.debug);
411
- this.__onCacheGet__ = options == null ? void 0 : options.onCacheGet;
412
- this.__onCacheSet__ = options == null ? void 0 : options.onCacheSet;
413
- this.__onCacheRemove__ = options == null ? void 0 : options.onCacheRemove;
414
- this.__onCacheEvict__ = options == null ? void 0 : options.onCacheEvict;
415
- this.debug((log) => log("Storage Created: ", this));
416
- }
417
- debug(fn) {
418
- if (this.__debug__) {
419
- fn((...args) => {
420
- Logger.debug("[Storage(".concat(this.__id__, ")]"), ...args);
421
- });
422
- }
423
- }
400
+ constructor(options) {
401
+ super();
402
+ _defineProperty(this, "__id__", Math.random().toString(36).slice(2));
403
+ _defineProperty(this, "__size__", void 0);
404
+ _defineProperty(this, "__debug__", void 0);
405
+ _defineProperty(this, "__onCacheGet__", void 0);
406
+ _defineProperty(this, "__onCacheSet__", void 0);
407
+ _defineProperty(this, "__onCacheRemove__", void 0);
408
+ _defineProperty(this, "__onCacheEvict__", void 0);
409
+ _defineProperty(this, "__onCacheExpired__", void 0);
410
+ if (options?.size && (typeof options?.size !== "number" || options.size <= 0)) throw new CacheException(`Invalid size: ${String(options?.size)}`);
411
+ this.__size__ = options?.size ?? Infinity;
412
+ this.__debug__ = !!options?.debug;
413
+ this.__onCacheGet__ = options?.onCacheGet;
414
+ this.__onCacheSet__ = options?.onCacheSet;
415
+ this.__onCacheRemove__ = options?.onCacheRemove;
416
+ this.__onCacheEvict__ = options?.onCacheEvict;
417
+ this.debug((log) => log("Storage Created: ", this));
418
+ }
419
+ debug(fn) {
420
+ if (this.__debug__) fn((...args) => {
421
+ Logger.debug(`[Storage(${this.__id__})]`, ...args);
422
+ });
423
+ }
424
424
  };
425
-
426
- // src/storage/memory-storage/utils/humanize-size.ts
425
+ //#endregion
426
+ //#region src/storage/memory-storage/utils/humanize-size.ts
427
+ /**
428
+ * @en Humanize size in bytes to KB, MB, GB
429
+ * @zh 将字节数转换为 KB、MB、GB 等易读格式
430
+ */
427
431
  function humanizeSize(size) {
428
- if (size < 1024) return "".concat(size, " B");
429
- if (size < 1024 * 1024) return "".concat((size / 1024).toFixed(2), " KB");
430
- if (size < 1024 * 1024 * 1024) return "".concat((size / (1024 * 1024)).toFixed(2), " MB");
431
- return "".concat((size / (1024 * 1024 * 1024)).toFixed(2), " GB");
432
+ if (size < 1024) return `${size} B`;
433
+ if (size < 1024 * 1024) return `${(size / 1024).toFixed(2)} KB`;
434
+ if (size < 1024 * 1024 * 1024) return `${(size / (1024 * 1024)).toFixed(2)} MB`;
435
+ return `${(size / (1024 * 1024 * 1024)).toFixed(2)} GB`;
432
436
  }
433
-
434
- // src/storage/memory-storage/utils/serialize-response-body.ts
437
+ //#endregion
438
+ //#region src/storage/memory-storage/utils/serialize-response-body.ts
439
+ /**
440
+ * @en Serialize the response body based on content-type
441
+ * @zh 根据 content-type 序列化响应体
442
+ */
435
443
  async function serializeResponseBody(response) {
436
- var _a;
437
- const contentType = (_a = response.headers.get("content-type")) != null ? _a : "";
438
- try {
439
- if (contentType.includes("application/json")) {
440
- const json = await response.json();
441
- return JSON.stringify(json);
442
- }
443
- if (contentType.includes("text/") || contentType.includes("application/xml") || contentType.includes("application/javascript")) {
444
- return await response.text();
445
- }
446
- return "[Binary or unsupported content]";
447
- } catch {
448
- return "[Unable to serialize]";
449
- }
444
+ const contentType = response.headers.get("content-type") ?? "";
445
+ try {
446
+ if (contentType.includes("application/json")) {
447
+ const json = await response.json();
448
+ return JSON.stringify(json);
449
+ }
450
+ if (contentType.includes("text/") || contentType.includes("application/xml") || contentType.includes("application/javascript")) return await response.text();
451
+ return "[Binary or unsupported content]";
452
+ } catch {
453
+ return "[Unable to serialize]";
454
+ }
450
455
  }
451
-
452
- // src/storage/memory-storage/base-memory-storage.ts
456
+ //#endregion
457
+ //#region src/storage/memory-storage/base-memory-storage.ts
453
458
  var BaseMemoryStorage = class extends InternalStorage {
454
- constructor() {
455
- super(...arguments);
456
- __publicField(this, "storage", /* @__PURE__ */ new Map());
457
- __publicField(this, "visitTimeRecords", /* @__PURE__ */ new Map());
458
- __publicField(this, "visitCountRecords", /* @__PURE__ */ new Map());
459
- __publicField(this, "lastEvictExpiredTime", dayjs());
460
- }
461
- get size() {
462
- const used = R3.sum(R3.pluck("size", [...this.storage.values()]));
463
- const free = this.__size__ > used ? this.__size__ - used : 0;
464
- return {
465
- used,
466
- free
467
- };
468
- }
469
- get(key) {
470
- var _a;
471
- this.evictExpired();
472
- const entry = this.storage.get(key);
473
- this.visitCountRecords.set(key, ((_a = this.visitCountRecords.get(key)) != null ? _a : 0) + 1);
474
- this.visitTimeRecords.set(key, /* @__PURE__ */ new Date());
475
- if (!entry) this.debug((log) => log("Entry(".concat(key, ") Not Found")));
476
- else this.debug((log) => log("Entry(".concat(key, ") Found: "), entry));
477
- return entry == null ? void 0 : entry.clone();
478
- }
479
- set(value) {
480
- var _a;
481
- if (!this.evict(value.size)) {
482
- this.debug((log) => log("Storage Size Not Enough: ", this.size.free, " < ", value.size));
483
- return;
484
- }
485
- this.storage.set(value.key, value);
486
- this.visitTimeRecords.set(value.key, /* @__PURE__ */ new Date());
487
- this.visitCountRecords.set(value.key, (_a = this.visitCountRecords.get(value.key)) != null ? _a : 0);
488
- this.debug((log) => log("Entry Added: ", value));
489
- this.debug((log) => log("Storage Size: ", this.size));
490
- }
491
- __remove__(keys) {
492
- for (const key of keys) {
493
- const entry = this.storage.get(key);
494
- if (!entry) return;
495
- this.storage.delete(key);
496
- this.visitCountRecords.delete(key);
497
- this.visitTimeRecords.delete(key);
498
- this.debug((log) => log("Entry Removed: ", entry));
499
- this.debug((log) => log("Storage Size: ", this.size));
500
- }
501
- }
502
- remove(key) {
503
- this.__remove__([key]);
504
- }
505
- /**
506
- * @zh 清除过期的缓存
507
- */
508
- evictExpired() {
509
- var _a;
510
- const now = dayjs();
511
- if (now.diff(this.lastEvictExpiredTime, "second") < 1) return;
512
- const keys = [];
513
- for (const [key, entry] of this.storage.entries()) {
514
- if (entry.expiredAt && now.isAfter(entry.expiredAt)) {
515
- keys.push(key);
516
- }
517
- }
518
- this.__remove__(keys);
519
- (_a = this.__onCacheExpired__) == null ? void 0 : _a.call(this, { keys });
520
- }
521
- /**
522
- * @en Evict the storage to make sure the size is enough
523
- * @zh 清除缓存以确保有足够的空间
524
- *
525
- * @return {boolean} - is evicted successfully
526
- */
527
- evict(expectSize) {
528
- this.evictExpired();
529
- const size = this.size;
530
- return size.free >= expectSize;
531
- }
532
- /**
533
- * @en Print all cached data using console.table for debugging
534
- * @zh 使用 console.table 打印所有缓存数据,用于调试
535
- */
536
- async print() {
537
- if (this.storage.size === 0) {
538
- console.log("MemoryStorage is empty");
539
- return;
540
- }
541
- const entries = await Promise.all(
542
- [...this.storage.entries()].map(async ([key, entry]) => {
543
- var _a, _b, _c;
544
- const body = await serializeResponseBody(entry.response.clone());
545
- return {
546
- key,
547
- size: humanizeSize(entry.size),
548
- "Expired Time": entry.expiredAt.getTime() >= MAX_EXPIRED_AT.getTime() ? "-" : entry.expiredAt.toISOString(),
549
- "Visit Count": (_a = this.visitCountRecords.get(key)) != null ? _a : 0,
550
- "Last Visit Time": (_c = (_b = this.visitTimeRecords.get(key)) == null ? void 0 : _b.toISOString()) != null ? _c : "-",
551
- "Response Status": entry.response.status,
552
- "Response URL": entry.response.url,
553
- "Response Body": body
554
- };
555
- })
556
- );
557
- console.table(entries);
558
- }
459
+ constructor(..._args) {
460
+ super(..._args);
461
+ _defineProperty(this, "storage", /* @__PURE__ */ new Map());
462
+ _defineProperty(this, "visitTimeRecords", /* @__PURE__ */ new Map());
463
+ _defineProperty(this, "visitCountRecords", /* @__PURE__ */ new Map());
464
+ _defineProperty(this, "lastEvictExpiredTime", dayjs());
465
+ }
466
+ get size() {
467
+ const used = R.sum(R.pluck("size", [...this.storage.values()]));
468
+ return {
469
+ used,
470
+ free: this.__size__ > used ? this.__size__ - used : 0
471
+ };
472
+ }
473
+ get(key) {
474
+ this.evictExpired();
475
+ const entry = this.storage.get(key);
476
+ this.visitCountRecords.set(key, (this.visitCountRecords.get(key) ?? 0) + 1);
477
+ this.visitTimeRecords.set(key, /* @__PURE__ */ new Date());
478
+ if (!entry) this.debug((log) => log(`Entry(${key}) Not Found`));
479
+ else this.debug((log) => log(`Entry(${key}) Found: `, entry));
480
+ return entry?.clone();
481
+ }
482
+ set(value) {
483
+ if (!this.evict(value.size)) {
484
+ this.debug((log) => log("Storage Size Not Enough: ", this.size.free, " < ", value.size));
485
+ return;
486
+ }
487
+ this.storage.set(value.key, value);
488
+ this.visitTimeRecords.set(value.key, /* @__PURE__ */ new Date());
489
+ this.visitCountRecords.set(value.key, this.visitCountRecords.get(value.key) ?? 0);
490
+ this.debug((log) => log("Entry Added: ", value));
491
+ this.debug((log) => log("Storage Size: ", this.size));
492
+ }
493
+ __remove__(keys) {
494
+ for (const key of keys) {
495
+ const entry = this.storage.get(key);
496
+ if (!entry) return;
497
+ this.storage.delete(key);
498
+ this.visitCountRecords.delete(key);
499
+ this.visitTimeRecords.delete(key);
500
+ this.debug((log) => log("Entry Removed: ", entry));
501
+ this.debug((log) => log("Storage Size: ", this.size));
502
+ }
503
+ }
504
+ remove(key) {
505
+ this.__remove__([key]);
506
+ }
507
+ /**
508
+ * @zh 清除过期的缓存
509
+ */
510
+ evictExpired() {
511
+ const now = dayjs();
512
+ if (now.diff(this.lastEvictExpiredTime, "second") < 1) return;
513
+ const keys = [];
514
+ for (const [key, entry] of this.storage.entries()) if (entry.expiredAt && now.isAfter(entry.expiredAt)) keys.push(key);
515
+ this.__remove__(keys);
516
+ this.__onCacheExpired__?.({ keys });
517
+ }
518
+ /**
519
+ * @en Evict the storage to make sure the size is enough
520
+ * @zh 清除缓存以确保有足够的空间
521
+ *
522
+ * @return {boolean} - is evicted successfully
523
+ */
524
+ evict(expectSize) {
525
+ this.evictExpired();
526
+ return this.size.free >= expectSize;
527
+ }
528
+ /**
529
+ * @en Print all cached data using console.table for debugging
530
+ * @zh 使用 console.table 打印所有缓存数据,用于调试
531
+ */
532
+ async print() {
533
+ if (this.storage.size === 0) {
534
+ console.log("MemoryStorage is empty");
535
+ return;
536
+ }
537
+ const entries = await Promise.all([...this.storage.entries()].map(async ([key, entry]) => {
538
+ const body = await serializeResponseBody(entry.response.clone());
539
+ return {
540
+ key,
541
+ size: humanizeSize(entry.size),
542
+ "Expired Time": entry.expiredAt.getTime() >= MAX_EXPIRED_AT.getTime() ? "-" : entry.expiredAt.toISOString(),
543
+ "Visit Count": this.visitCountRecords.get(key) ?? 0,
544
+ "Last Visit Time": this.visitTimeRecords.get(key)?.toISOString() ?? "-",
545
+ "Response Status": entry.response.status,
546
+ "Response URL": entry.response.url,
547
+ "Response Body": body
548
+ };
549
+ }));
550
+ console.table(entries);
551
+ }
559
552
  };
560
-
561
- // src/storage/memory-storage/ttl-memory-storage.ts
553
+ //#endregion
554
+ //#region src/storage/memory-storage/ttl-memory-storage.ts
562
555
  var TTLMemoryStorage = class extends BaseMemoryStorage {
563
- constructor(options) {
564
- super(options);
565
- }
566
- get(key) {
567
- var _a;
568
- const entry = super.get(key);
569
- (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
570
- return entry;
571
- }
572
- set(value) {
573
- var _a;
574
- super.set(value);
575
- (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
576
- }
577
- remove(key) {
578
- var _a;
579
- super.remove(key);
580
- (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
581
- }
582
- evict(expectSize) {
583
- var _a;
584
- if (expectSize > this.__size__) {
585
- this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
586
- return false;
587
- }
588
- this.evictExpired();
589
- let deficitSize = expectSize - this.size.free;
590
- if (deficitSize <= 0) return true;
591
- const entries = [...this.storage.values()].sort((a, b) => {
592
- const aExpiredAt = dayjs2(a.expiredAt);
593
- const bExpiredAt = dayjs2(b.expiredAt);
594
- return aExpiredAt.isBefore(bExpiredAt) ? 1 : -1;
595
- });
596
- if (R4.sum(R4.pluck("size", entries)) < deficitSize) {
597
- this.debug((log) => log("Storage Size Not Enough: ", this.size.free, " < ", deficitSize));
598
- return false;
599
- }
600
- const keys = [];
601
- while (deficitSize > 0 && entries.length) {
602
- const entry = entries.pop();
603
- deficitSize -= entry.size;
604
- keys.push(entry.key);
605
- }
606
- this.__remove__(keys);
607
- (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
608
- return true;
609
- }
556
+ constructor(options) {
557
+ super(options);
558
+ }
559
+ get(key) {
560
+ const entry = super.get(key);
561
+ this.__onCacheGet__?.({ key });
562
+ return entry;
563
+ }
564
+ set(value) {
565
+ super.set(value);
566
+ this.__onCacheSet__?.({ key: value.key });
567
+ }
568
+ remove(key) {
569
+ super.remove(key);
570
+ this.__onCacheRemove__?.({ key });
571
+ }
572
+ evict(expectSize) {
573
+ if (expectSize > this.__size__) {
574
+ this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
575
+ return false;
576
+ }
577
+ this.evictExpired();
578
+ let deficitSize = expectSize - this.size.free;
579
+ if (deficitSize <= 0) return true;
580
+ const entries = [...this.storage.values()].sort((a, b) => {
581
+ const aExpiredAt = dayjs(a.expiredAt);
582
+ const bExpiredAt = dayjs(b.expiredAt);
583
+ return aExpiredAt.isBefore(bExpiredAt) ? 1 : -1;
584
+ });
585
+ if (R.sum(R.pluck("size", entries)) < deficitSize) {
586
+ this.debug((log) => log("Storage Size Not Enough: ", this.size.free, " < ", deficitSize));
587
+ return false;
588
+ }
589
+ const keys = [];
590
+ while (deficitSize > 0 && entries.length) {
591
+ const entry = entries.pop();
592
+ deficitSize -= entry.size;
593
+ keys.push(entry.key);
594
+ }
595
+ this.__remove__(keys);
596
+ this.__onCacheEvict__?.({ keys });
597
+ return true;
598
+ }
610
599
  };
611
-
612
- // src/storage/memory-storage/random-memory-storage.ts
600
+ //#endregion
601
+ //#region src/storage/memory-storage/random-memory-storage.ts
613
602
  var RandomMemoryStorage = class extends BaseMemoryStorage {
614
- constructor(options) {
615
- super(options);
616
- }
617
- get(key) {
618
- var _a;
619
- const entry = super.get(key);
620
- (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
621
- return entry;
622
- }
623
- set(value) {
624
- var _a;
625
- super.set(value);
626
- (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
627
- }
628
- remove(key) {
629
- var _a;
630
- super.remove(key);
631
- (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
632
- }
633
- evict(expectSize) {
634
- var _a;
635
- if (expectSize > this.__size__) {
636
- this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
637
- return false;
638
- }
639
- this.evictExpired();
640
- let deficitSize = expectSize - this.size.free;
641
- if (deficitSize <= 0) return true;
642
- const entries = [...this.storage.values()];
643
- const keys = [];
644
- while (deficitSize > 0 && entries.length) {
645
- const index = random(0, entries.length - 1);
646
- const entry = entries[index];
647
- deficitSize -= entry.size;
648
- entries.splice(index, 1);
649
- keys.push(entry.key);
650
- }
651
- this.__remove__(keys);
652
- (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
653
- return true;
654
- }
603
+ constructor(options) {
604
+ super(options);
605
+ }
606
+ get(key) {
607
+ const entry = super.get(key);
608
+ this.__onCacheGet__?.({ key });
609
+ return entry;
610
+ }
611
+ set(value) {
612
+ super.set(value);
613
+ this.__onCacheSet__?.({ key: value.key });
614
+ }
615
+ remove(key) {
616
+ super.remove(key);
617
+ this.__onCacheRemove__?.({ key });
618
+ }
619
+ evict(expectSize) {
620
+ if (expectSize > this.__size__) {
621
+ this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
622
+ return false;
623
+ }
624
+ this.evictExpired();
625
+ let deficitSize = expectSize - this.size.free;
626
+ if (deficitSize <= 0) return true;
627
+ const entries = [...this.storage.values()];
628
+ const keys = [];
629
+ while (deficitSize > 0 && entries.length) {
630
+ const index = random(0, entries.length - 1);
631
+ const entry = entries[index];
632
+ deficitSize -= entry.size;
633
+ entries.splice(index, 1);
634
+ keys.push(entry.key);
635
+ }
636
+ this.__remove__(keys);
637
+ this.__onCacheEvict__?.({ keys });
638
+ return true;
639
+ }
655
640
  };
656
-
657
- // src/storage/memory-storage/lru-memory-storage.ts
658
- import dayjs3 from "dayjs";
641
+ //#endregion
642
+ //#region src/storage/memory-storage/lru-memory-storage.ts
659
643
  var LRUMemoryStorage = class extends BaseMemoryStorage {
660
- constructor(options) {
661
- super(options);
662
- }
663
- get(key) {
664
- var _a;
665
- const entry = super.get(key);
666
- (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
667
- return entry;
668
- }
669
- set(value) {
670
- var _a;
671
- super.set(value);
672
- (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
673
- }
674
- remove(key) {
675
- var _a;
676
- super.remove(key);
677
- (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
678
- }
679
- evict(expectSize) {
680
- var _a;
681
- if (expectSize > this.__size__) {
682
- this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
683
- return false;
684
- }
685
- this.evictExpired();
686
- let deficitSize = expectSize - this.size.free;
687
- if (deficitSize <= 0) return true;
688
- const entries = [...this.storage.values()].sort((a, b) => {
689
- const aVisitAt = this.visitTimeRecords.get(a.key);
690
- const bVisitAt = this.visitTimeRecords.get(b.key);
691
- if (aVisitAt === bVisitAt) return 0;
692
- if (!aVisitAt) return 1;
693
- if (!bVisitAt) return -1;
694
- return dayjs3(aVisitAt).isBefore(dayjs3(bVisitAt)) ? 1 : -1;
695
- });
696
- const keys = [];
697
- while (deficitSize > 0 && entries.length) {
698
- const entry = entries.pop();
699
- deficitSize -= entry.size;
700
- keys.push(entry.key);
701
- }
702
- this.__remove__(keys);
703
- (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
704
- return true;
705
- }
644
+ constructor(options) {
645
+ super(options);
646
+ }
647
+ get(key) {
648
+ const entry = super.get(key);
649
+ this.__onCacheGet__?.({ key });
650
+ return entry;
651
+ }
652
+ set(value) {
653
+ super.set(value);
654
+ this.__onCacheSet__?.({ key: value.key });
655
+ }
656
+ remove(key) {
657
+ super.remove(key);
658
+ this.__onCacheRemove__?.({ key });
659
+ }
660
+ evict(expectSize) {
661
+ if (expectSize > this.__size__) {
662
+ this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
663
+ return false;
664
+ }
665
+ this.evictExpired();
666
+ let deficitSize = expectSize - this.size.free;
667
+ if (deficitSize <= 0) return true;
668
+ const entries = [...this.storage.values()].sort((a, b) => {
669
+ const aVisitAt = this.visitTimeRecords.get(a.key);
670
+ const bVisitAt = this.visitTimeRecords.get(b.key);
671
+ if (aVisitAt === bVisitAt) return 0;
672
+ if (!aVisitAt) return 1;
673
+ if (!bVisitAt) return -1;
674
+ return dayjs(aVisitAt).isBefore(dayjs(bVisitAt)) ? 1 : -1;
675
+ });
676
+ const keys = [];
677
+ while (deficitSize > 0 && entries.length) {
678
+ const entry = entries.pop();
679
+ deficitSize -= entry.size;
680
+ keys.push(entry.key);
681
+ }
682
+ this.__remove__(keys);
683
+ this.__onCacheEvict__?.({ keys });
684
+ return true;
685
+ }
706
686
  };
707
-
708
- // src/storage/memory-storage/lfu-memory-storage.ts
687
+ //#endregion
688
+ //#region src/storage/memory-storage/lfu-memory-storage.ts
709
689
  var LFUMemoryStorage = class extends BaseMemoryStorage {
710
- constructor(options) {
711
- super(options);
712
- }
713
- get(key) {
714
- var _a;
715
- const entry = super.get(key);
716
- (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
717
- return entry;
718
- }
719
- set(value) {
720
- var _a;
721
- super.set(value);
722
- (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
723
- }
724
- remove(key) {
725
- var _a;
726
- super.remove(key);
727
- (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
728
- }
729
- evict(expectSize) {
730
- var _a;
731
- if (expectSize > this.__size__) {
732
- this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
733
- return false;
734
- }
735
- this.evictExpired();
736
- let deficitSize = expectSize - this.size.free;
737
- if (deficitSize <= 0) return true;
738
- const entries = [...this.storage.values()].sort((a, b) => {
739
- const aVisitCount = this.visitCountRecords.get(a.key) || 0;
740
- const bVisitCount = this.visitCountRecords.get(b.key) || 0;
741
- return bVisitCount - aVisitCount;
742
- });
743
- const keys = [];
744
- while (deficitSize > 0 && entries.length) {
745
- const entry = entries.pop();
746
- deficitSize -= entry.size;
747
- keys.push(entry.key);
748
- }
749
- this.__remove__(keys);
750
- (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
751
- return true;
752
- }
690
+ constructor(options) {
691
+ super(options);
692
+ }
693
+ get(key) {
694
+ const entry = super.get(key);
695
+ this.__onCacheGet__?.({ key });
696
+ return entry;
697
+ }
698
+ set(value) {
699
+ super.set(value);
700
+ this.__onCacheSet__?.({ key: value.key });
701
+ }
702
+ remove(key) {
703
+ super.remove(key);
704
+ this.__onCacheRemove__?.({ key });
705
+ }
706
+ evict(expectSize) {
707
+ if (expectSize > this.__size__) {
708
+ this.debug((log) => log("Storage Size Not Enough: ", this.__size__, " < ", expectSize));
709
+ return false;
710
+ }
711
+ this.evictExpired();
712
+ let deficitSize = expectSize - this.size.free;
713
+ if (deficitSize <= 0) return true;
714
+ const entries = [...this.storage.values()].sort((a, b) => {
715
+ const aVisitCount = this.visitCountRecords.get(a.key) || 0;
716
+ return (this.visitCountRecords.get(b.key) || 0) - aVisitCount;
717
+ });
718
+ const keys = [];
719
+ while (deficitSize > 0 && entries.length) {
720
+ const entry = entries.pop();
721
+ deficitSize -= entry.size;
722
+ keys.push(entry.key);
723
+ }
724
+ this.__remove__(keys);
725
+ this.__onCacheEvict__?.({ keys });
726
+ return true;
727
+ }
753
728
  };
754
-
755
- // src/storage/memory-storage/memory-storage.ts
729
+ //#endregion
730
+ //#region src/storage/memory-storage/memory-storage.ts
756
731
  var MemoryStorage = class extends KeqCacheStorage {
757
- constructor(options) {
758
- super();
759
- __publicField(this, "storage");
760
- const eviction = (options == null ? void 0 : options.eviction) || "lru" /* LRU */;
761
- if (eviction === "ttl" /* TTL */) {
762
- this.storage = new TTLMemoryStorage(options);
763
- } else if (eviction === "random" /* RANDOM */) {
764
- this.storage = new RandomMemoryStorage(options);
765
- } else if (eviction === "lru" /* LRU */) {
766
- this.storage = new LRUMemoryStorage(options);
767
- } else if (eviction === "lfu" /* LFU */) {
768
- this.storage = new LFUMemoryStorage(options);
769
- } else {
770
- throw new TypeError("Invalid eviction: ".concat(String(eviction)));
771
- }
772
- }
773
- set(entry) {
774
- return this.storage.set(entry);
775
- }
776
- get(key) {
777
- return this.storage.get(key);
778
- }
779
- remove(key) {
780
- return this.storage.remove(key);
781
- }
782
- async print() {
783
- return this.storage.print();
784
- }
732
+ constructor(options) {
733
+ super();
734
+ _defineProperty(this, "storage", void 0);
735
+ const eviction = options?.eviction || "lru";
736
+ if (eviction === "ttl") this.storage = new TTLMemoryStorage(options);
737
+ else if (eviction === "random") this.storage = new RandomMemoryStorage(options);
738
+ else if (eviction === "lru") this.storage = new LRUMemoryStorage(options);
739
+ else if (eviction === "lfu") this.storage = new LFUMemoryStorage(options);
740
+ else throw new TypeError(`Invalid eviction: ${String(eviction)}`);
741
+ }
742
+ set(entry) {
743
+ return this.storage.set(entry);
744
+ }
745
+ get(key) {
746
+ return this.storage.get(key);
747
+ }
748
+ remove(key) {
749
+ return this.storage.remove(key);
750
+ }
751
+ async print() {
752
+ return this.storage.print();
753
+ }
785
754
  };
786
-
787
- // src/storage/indexed-db-storage/random-indexed-db-storage.ts
788
- import * as R6 from "ramda";
789
-
790
- // src/storage/indexed-db-storage/base-indexed-db-storage.ts
791
- import * as R5 from "ramda";
792
- import dayjs4 from "dayjs";
793
- import { openDB } from "idb";
794
-
795
- // src/storage/indexed-db-storage/constants/default-table-name.ts
796
- var DEFAULT_TABLE_NAME = "keq_cache_indexed_db_storage";
797
-
798
- // src/storage/indexed-db-storage/base-indexed-db-storage.ts
755
+ //#endregion
756
+ //#region src/storage/indexed-db-storage/constants/default-table-name.ts
757
+ const DEFAULT_TABLE_NAME = "keq_cache_indexed_db_storage";
758
+ //#endregion
759
+ //#region src/storage/indexed-db-storage/base-indexed-db-storage.ts
799
760
  var BaseIndexedDBStorage = class extends InternalStorage {
800
- constructor(options) {
801
- super(options);
802
- __publicField(this, "tableName", DEFAULT_TABLE_NAME);
803
- __publicField(this, "db");
804
- __publicField(this, "lastEvictExpiredTime", dayjs4(0));
805
- if ((options == null ? void 0 : options.tableName) === DEFAULT_TABLE_NAME) {
806
- throw new CacheException('IndexedDBStorage name cannot be "'.concat(DEFAULT_TABLE_NAME, '"'));
807
- }
808
- this.tableName = (options == null ? void 0 : options.tableName) || DEFAULT_TABLE_NAME;
809
- }
810
- async openDB() {
811
- if (this.db) return this.db;
812
- const tableName = this.tableName;
813
- const db = await openDB(tableName, 2, {
814
- upgrade(db2) {
815
- if (!db2.objectStoreNames.contains("metadata")) {
816
- const entriesStore = db2.createObjectStore("metadata", { keyPath: "key" });
817
- entriesStore.createIndex("expiredAt", "expiredAt");
818
- }
819
- if (!db2.objectStoreNames.contains("response")) {
820
- const responsesStore = db2.createObjectStore("response", { keyPath: "key" });
821
- responsesStore.createIndex("responseStatus", "responseStatus");
822
- }
823
- if (!db2.objectStoreNames.contains("visits")) {
824
- const visitsStore = db2.createObjectStore("visits", { keyPath: "key" });
825
- visitsStore.createIndex("visitCount", "visitCount");
826
- visitsStore.createIndex("lastVisitedAt", "lastVisitedAt");
827
- }
828
- },
829
- blocked() {
830
- Logger.error("[@keq-request/cache] IndexedDB Table ".concat(tableName, " is blocked"));
831
- },
832
- blocking() {
833
- Logger.error("[@keq-request/cache] IndexedDB Table ".concat(tableName, " is blocking"));
834
- },
835
- terminated() {
836
- Logger.error("[@keq-request/cache] IndexedDB Table ".concat(tableName, " is terminated"));
837
- }
838
- });
839
- this.db = db;
840
- return db;
841
- }
842
- async getSize() {
843
- const db = await this.openDB();
844
- const items = await db.getAll("metadata");
845
- const used = R5.sum(items.map((entry) => entry.size));
846
- const free = this.__size__ - used;
847
- return { used, free };
848
- }
849
- async get(key) {
850
- await this.evictExpired();
851
- try {
852
- const db = await this.openDB();
853
- const dbMetadata = await db.get("metadata", key);
854
- const dbResponse = await db.get("response", key);
855
- const dbVisits = await db.get("visits", key);
856
- if (!dbMetadata || !dbResponse) return;
857
- await db.put("visits", {
858
- key: dbMetadata.key,
859
- visitCount: dbVisits ? dbVisits.visitCount + 1 : 1,
860
- lastVisitedAt: /* @__PURE__ */ new Date()
861
- });
862
- const response = new Response(dbResponse.responseBody, {
863
- status: dbResponse.responseStatus,
864
- headers: new Headers(dbResponse.responseHeaders),
865
- statusText: dbResponse.responseStatusText
866
- });
867
- return await CacheEntry.build({
868
- key: dbMetadata.key,
869
- expiredAt: dbMetadata.expiredAt,
870
- response,
871
- size: dbMetadata.size
872
- });
873
- } catch (error) {
874
- return;
875
- }
876
- }
877
- async set(entry) {
878
- try {
879
- if (!await this.evict(entry.size)) {
880
- const size = await this.getSize();
881
- this.debug((log) => log("Storage Size Not Enough: ".concat(size.free, " < ").concat(entry.size)));
882
- return;
883
- }
884
- const dbMetadata = {
885
- key: entry.key,
886
- size: entry.size,
887
- expiredAt: entry.expiredAt,
888
- visitedAt: /* @__PURE__ */ new Date(),
889
- visitCount: 0
890
- };
891
- const response = entry.response.clone();
892
- const dbResponse = {
893
- key: entry.key,
894
- responseBody: await response.arrayBuffer(),
895
- responseHeaders: [...response.headers.entries()],
896
- responseStatus: response.status,
897
- responseStatusText: response.statusText
898
- };
899
- const db = await this.openDB();
900
- const tx = db.transaction(["metadata", "response", "visits"], "readwrite");
901
- const metadataStore = tx.objectStore("metadata");
902
- const responseStore = tx.objectStore("response");
903
- const visitsStore = tx.objectStore("visits");
904
- const dbVisits = await visitsStore.get(entry.key) || {
905
- key: entry.key,
906
- visitCount: 0,
907
- lastVisitedAt: /* @__PURE__ */ new Date()
908
- };
909
- await Promise.all([
910
- metadataStore.put(dbMetadata),
911
- responseStore.put(dbResponse),
912
- visitsStore.put(dbVisits)
913
- ]);
914
- await tx.done;
915
- } catch (error) {
916
- return;
917
- }
918
- }
919
- async __remove__(tx, keys) {
920
- await Promise.all(
921
- R5.unnest(
922
- keys.map((key) => [
923
- tx.objectStore("metadata").delete(key),
924
- tx.objectStore("response").delete(key),
925
- tx.objectStore("visits").delete(key)
926
- ])
927
- )
928
- );
929
- }
930
- async remove(key) {
931
- try {
932
- const db = await this.openDB();
933
- const tx = db.transaction(["metadata", "response", "visits"], "readwrite");
934
- await this.__remove__(tx, [key]);
935
- await tx.done;
936
- } catch (error) {
937
- return;
938
- }
939
- }
940
- /**
941
- * @zh 清除过期的缓存
942
- */
943
- async evictExpired() {
944
- var _a;
945
- const now = dayjs4();
946
- if (now.diff(this.lastEvictExpiredTime, "second") < 1) return;
947
- this.lastEvictExpiredTime = now;
948
- try {
949
- const db = await this.openDB();
950
- const tx = db.transaction(["metadata", "response", "visits"], "readwrite");
951
- const metadataStore = tx.objectStore("metadata");
952
- let cursor = await metadataStore.index("expiredAt").openCursor(IDBKeyRange.upperBound(now.toDate()));
953
- const expiredKeys = [];
954
- while (cursor) {
955
- if (dayjs4(cursor.value.expiredAt).isBefore(now)) {
956
- expiredKeys.push(cursor.value.key);
957
- cursor = await cursor.continue();
958
- } else {
959
- break;
960
- }
961
- }
962
- await this.__remove__(tx, expiredKeys);
963
- await tx.done;
964
- (_a = this.__onCacheExpired__) == null ? void 0 : _a.call(this, { keys: expiredKeys });
965
- } catch (error) {
966
- return;
967
- }
968
- }
761
+ constructor(options) {
762
+ super(options);
763
+ _defineProperty(this, "tableName", DEFAULT_TABLE_NAME);
764
+ _defineProperty(this, "db", void 0);
765
+ _defineProperty(this, "lastEvictExpiredTime", dayjs(0));
766
+ if (options?.tableName === "keq_cache_indexed_db_storage") throw new CacheException(`IndexedDBStorage name cannot be "${DEFAULT_TABLE_NAME}"`);
767
+ this.tableName = options?.tableName || "keq_cache_indexed_db_storage";
768
+ }
769
+ async openDB() {
770
+ if (this.db) return this.db;
771
+ const tableName = this.tableName;
772
+ const db = await openDB(tableName, 2, {
773
+ upgrade(db) {
774
+ if (!db.objectStoreNames.contains("metadata")) db.createObjectStore("metadata", { keyPath: "key" }).createIndex("expiredAt", "expiredAt");
775
+ if (!db.objectStoreNames.contains("response")) db.createObjectStore("response", { keyPath: "key" }).createIndex("responseStatus", "responseStatus");
776
+ if (!db.objectStoreNames.contains("visits")) {
777
+ const visitsStore = db.createObjectStore("visits", { keyPath: "key" });
778
+ visitsStore.createIndex("visitCount", "visitCount");
779
+ visitsStore.createIndex("lastVisitedAt", "lastVisitedAt");
780
+ }
781
+ },
782
+ blocked() {
783
+ Logger.error(`[@keq-request/cache] IndexedDB Table ${tableName} is blocked`);
784
+ },
785
+ blocking() {
786
+ Logger.error(`[@keq-request/cache] IndexedDB Table ${tableName} is blocking`);
787
+ },
788
+ terminated() {
789
+ Logger.error(`[@keq-request/cache] IndexedDB Table ${tableName} is terminated`);
790
+ }
791
+ });
792
+ this.db = db;
793
+ return db;
794
+ }
795
+ async getSize() {
796
+ const items = await (await this.openDB()).getAll("metadata");
797
+ const used = R.sum(items.map((entry) => entry.size));
798
+ return {
799
+ used,
800
+ free: this.__size__ - used
801
+ };
802
+ }
803
+ async get(key) {
804
+ await this.evictExpired();
805
+ try {
806
+ const db = await this.openDB();
807
+ const dbMetadata = await db.get("metadata", key);
808
+ const dbResponse = await db.get("response", key);
809
+ const dbVisits = await db.get("visits", key);
810
+ if (!dbMetadata || !dbResponse) return;
811
+ await db.put("visits", {
812
+ key: dbMetadata.key,
813
+ visitCount: dbVisits ? dbVisits.visitCount + 1 : 1,
814
+ lastVisitedAt: /* @__PURE__ */ new Date()
815
+ });
816
+ const response = new Response(dbResponse.responseBody, {
817
+ status: dbResponse.responseStatus,
818
+ headers: new Headers(dbResponse.responseHeaders),
819
+ statusText: dbResponse.responseStatusText
820
+ });
821
+ return await CacheEntry.build({
822
+ key: dbMetadata.key,
823
+ expiredAt: dbMetadata.expiredAt,
824
+ response,
825
+ size: dbMetadata.size
826
+ });
827
+ } catch (error) {
828
+ return;
829
+ }
830
+ }
831
+ async set(entry) {
832
+ try {
833
+ if (!await this.evict(entry.size)) {
834
+ const size = await this.getSize();
835
+ this.debug((log) => log(`Storage Size Not Enough: ${size.free} < ${entry.size}`));
836
+ return;
837
+ }
838
+ const dbMetadata = {
839
+ key: entry.key,
840
+ size: entry.size,
841
+ expiredAt: entry.expiredAt,
842
+ visitedAt: /* @__PURE__ */ new Date(),
843
+ visitCount: 0
844
+ };
845
+ const response = entry.response.clone();
846
+ const dbResponse = {
847
+ key: entry.key,
848
+ responseBody: await response.arrayBuffer(),
849
+ responseHeaders: [...response.headers.entries()],
850
+ responseStatus: response.status,
851
+ responseStatusText: response.statusText
852
+ };
853
+ const tx = (await this.openDB()).transaction([
854
+ "metadata",
855
+ "response",
856
+ "visits"
857
+ ], "readwrite");
858
+ const metadataStore = tx.objectStore("metadata");
859
+ const responseStore = tx.objectStore("response");
860
+ const visitsStore = tx.objectStore("visits");
861
+ const dbVisits = await visitsStore.get(entry.key) || {
862
+ key: entry.key,
863
+ visitCount: 0,
864
+ lastVisitedAt: /* @__PURE__ */ new Date()
865
+ };
866
+ await Promise.all([
867
+ metadataStore.put(dbMetadata),
868
+ responseStore.put(dbResponse),
869
+ visitsStore.put(dbVisits)
870
+ ]);
871
+ await tx.done;
872
+ } catch (error) {
873
+ return;
874
+ }
875
+ }
876
+ async __remove__(tx, keys) {
877
+ await Promise.all(R.unnest(keys.map((key) => [
878
+ tx.objectStore("metadata").delete(key),
879
+ tx.objectStore("response").delete(key),
880
+ tx.objectStore("visits").delete(key)
881
+ ])));
882
+ }
883
+ async remove(key) {
884
+ try {
885
+ const tx = (await this.openDB()).transaction([
886
+ "metadata",
887
+ "response",
888
+ "visits"
889
+ ], "readwrite");
890
+ await this.__remove__(tx, [key]);
891
+ await tx.done;
892
+ } catch (error) {
893
+ return;
894
+ }
895
+ }
896
+ /**
897
+ * @zh 清除过期的缓存
898
+ */
899
+ async evictExpired() {
900
+ const now = dayjs();
901
+ if (now.diff(this.lastEvictExpiredTime, "second") < 1) return;
902
+ this.lastEvictExpiredTime = now;
903
+ try {
904
+ const tx = (await this.openDB()).transaction([
905
+ "metadata",
906
+ "response",
907
+ "visits"
908
+ ], "readwrite");
909
+ let cursor = await tx.objectStore("metadata").index("expiredAt").openCursor(IDBKeyRange.upperBound(now.toDate()));
910
+ const expiredKeys = [];
911
+ while (cursor) if (dayjs(cursor.value.expiredAt).isBefore(now)) {
912
+ expiredKeys.push(cursor.value.key);
913
+ cursor = await cursor.continue();
914
+ } else break;
915
+ await this.__remove__(tx, expiredKeys);
916
+ await tx.done;
917
+ this.__onCacheExpired__?.({ keys: expiredKeys });
918
+ } catch (error) {
919
+ return;
920
+ }
921
+ }
969
922
  };
970
-
971
- // src/storage/indexed-db-storage/random-indexed-db-storage.ts
923
+ //#endregion
924
+ //#region src/storage/indexed-db-storage/random-indexed-db-storage.ts
972
925
  var RandomIndexedDBStorage = class extends BaseIndexedDBStorage {
973
- constructor(options) {
974
- super(options);
975
- }
976
- async get(key) {
977
- var _a;
978
- const entry = await super.get(key);
979
- (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
980
- return entry;
981
- }
982
- async set(value) {
983
- var _a;
984
- await super.set(value);
985
- (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
986
- }
987
- async remove(key) {
988
- var _a;
989
- await super.remove(key);
990
- (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
991
- }
992
- async evict(expectSize) {
993
- var _a;
994
- await this.evictExpired();
995
- const size = await this.getSize();
996
- let deficitSize = expectSize - size.free;
997
- if (deficitSize <= 0) return true;
998
- const db = await this.openDB();
999
- const tx = db.transaction(["metadata", "response", "visits"], "readwrite");
1000
- const metadataStore = tx.objectStore("metadata");
1001
- const metadatas = await metadataStore.getAll();
1002
- const totalSize = R6.sum(metadatas.map((m) => m.size));
1003
- if (totalSize < deficitSize) {
1004
- this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize - totalSize)));
1005
- tx.abort();
1006
- return false;
1007
- }
1008
- const keys = [];
1009
- while (deficitSize > 0 && metadatas.length) {
1010
- const index = random(0, metadatas.length - 1);
1011
- const metadata = metadatas[index];
1012
- deficitSize -= metadata.size;
1013
- keys.push(metadata.key);
1014
- metadatas.splice(index, 1);
1015
- }
1016
- await this.__remove__(tx, keys);
1017
- await tx.done;
1018
- (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
1019
- return true;
1020
- }
926
+ constructor(options) {
927
+ super(options);
928
+ }
929
+ async get(key) {
930
+ const entry = await super.get(key);
931
+ this.__onCacheGet__?.({ key });
932
+ return entry;
933
+ }
934
+ async set(value) {
935
+ await super.set(value);
936
+ this.__onCacheSet__?.({ key: value.key });
937
+ }
938
+ async remove(key) {
939
+ await super.remove(key);
940
+ this.__onCacheRemove__?.({ key });
941
+ }
942
+ async evict(expectSize) {
943
+ await this.evictExpired();
944
+ let deficitSize = expectSize - (await this.getSize()).free;
945
+ if (deficitSize <= 0) return true;
946
+ const tx = (await this.openDB()).transaction([
947
+ "metadata",
948
+ "response",
949
+ "visits"
950
+ ], "readwrite");
951
+ const metadatas = await tx.objectStore("metadata").getAll();
952
+ const totalSize = R.sum(metadatas.map((m) => m.size));
953
+ if (totalSize < deficitSize) {
954
+ this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize - totalSize}`));
955
+ tx.abort();
956
+ return false;
957
+ }
958
+ const keys = [];
959
+ while (deficitSize > 0 && metadatas.length) {
960
+ const index = random(0, metadatas.length - 1);
961
+ const metadata = metadatas[index];
962
+ deficitSize -= metadata.size;
963
+ keys.push(metadata.key);
964
+ metadatas.splice(index, 1);
965
+ }
966
+ await this.__remove__(tx, keys);
967
+ await tx.done;
968
+ this.__onCacheEvict__?.({ keys });
969
+ return true;
970
+ }
1021
971
  };
1022
-
1023
- // src/storage/indexed-db-storage/lfu-indexed-db-storage.ts
972
+ //#endregion
973
+ //#region src/storage/indexed-db-storage/lfu-indexed-db-storage.ts
1024
974
  var LFUIndexedDBStorage = class extends BaseIndexedDBStorage {
1025
- constructor(options) {
1026
- super(options);
1027
- }
1028
- async get(key) {
1029
- var _a;
1030
- const entry = await super.get(key);
1031
- (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
1032
- return entry;
1033
- }
1034
- async set(value) {
1035
- var _a;
1036
- await super.set(value);
1037
- (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
1038
- }
1039
- async remove(key) {
1040
- var _a;
1041
- await super.remove(key);
1042
- (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
1043
- }
1044
- async evict(expectSize) {
1045
- var _a;
1046
- await this.evictExpired();
1047
- const size = await this.getSize();
1048
- let deficitSize = expectSize - size.free;
1049
- if (deficitSize <= 0) return true;
1050
- const db = await this.openDB();
1051
- const tx = db.transaction(["metadata", "response", "visits"], "readwrite");
1052
- const metadataStore = tx.objectStore("metadata");
1053
- const visitsStore = tx.objectStore("visits");
1054
- let cursor = await visitsStore.index("visitCount").openCursor();
1055
- const keys = [];
1056
- while (deficitSize > 0 && cursor) {
1057
- const metadata = await metadataStore.get(cursor.value.key);
1058
- if (!metadata) {
1059
- await cursor.delete();
1060
- cursor = await cursor.continue();
1061
- continue;
1062
- }
1063
- deficitSize -= metadata.size;
1064
- keys.push(cursor.value.key);
1065
- cursor = await cursor.continue();
1066
- }
1067
- if (deficitSize > 0) {
1068
- this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
1069
- tx.abort();
1070
- return false;
1071
- }
1072
- await this.__remove__(tx, keys);
1073
- await tx.done;
1074
- (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
1075
- return true;
1076
- }
975
+ constructor(options) {
976
+ super(options);
977
+ }
978
+ async get(key) {
979
+ const entry = await super.get(key);
980
+ this.__onCacheGet__?.({ key });
981
+ return entry;
982
+ }
983
+ async set(value) {
984
+ await super.set(value);
985
+ this.__onCacheSet__?.({ key: value.key });
986
+ }
987
+ async remove(key) {
988
+ await super.remove(key);
989
+ this.__onCacheRemove__?.({ key });
990
+ }
991
+ async evict(expectSize) {
992
+ await this.evictExpired();
993
+ let deficitSize = expectSize - (await this.getSize()).free;
994
+ if (deficitSize <= 0) return true;
995
+ const tx = (await this.openDB()).transaction([
996
+ "metadata",
997
+ "response",
998
+ "visits"
999
+ ], "readwrite");
1000
+ const metadataStore = tx.objectStore("metadata");
1001
+ let cursor = await tx.objectStore("visits").index("visitCount").openCursor();
1002
+ const keys = [];
1003
+ while (deficitSize > 0 && cursor) {
1004
+ const metadata = await metadataStore.get(cursor.value.key);
1005
+ if (!metadata) {
1006
+ await cursor.delete();
1007
+ cursor = await cursor.continue();
1008
+ continue;
1009
+ }
1010
+ deficitSize -= metadata.size;
1011
+ keys.push(cursor.value.key);
1012
+ cursor = await cursor.continue();
1013
+ }
1014
+ if (deficitSize > 0) {
1015
+ this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize}`));
1016
+ tx.abort();
1017
+ return false;
1018
+ }
1019
+ await this.__remove__(tx, keys);
1020
+ await tx.done;
1021
+ this.__onCacheEvict__?.({ keys });
1022
+ return true;
1023
+ }
1077
1024
  };
1078
-
1079
- // src/storage/indexed-db-storage/lru-indexed-db-storage.ts
1025
+ //#endregion
1026
+ //#region src/storage/indexed-db-storage/lru-indexed-db-storage.ts
1080
1027
  var LRUIndexedDBStorage = class extends BaseIndexedDBStorage {
1081
- constructor(options) {
1082
- super(options);
1083
- }
1084
- async get(key) {
1085
- var _a;
1086
- const entry = await super.get(key);
1087
- (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
1088
- return entry;
1089
- }
1090
- async set(value) {
1091
- var _a;
1092
- await super.set(value);
1093
- (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
1094
- }
1095
- async remove(key) {
1096
- var _a;
1097
- await super.remove(key);
1098
- (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
1099
- }
1100
- async evict(expectSize) {
1101
- var _a;
1102
- await this.evictExpired();
1103
- const size = await this.getSize();
1104
- let deficitSize = expectSize - size.free;
1105
- if (deficitSize <= 0) return true;
1106
- const db = await this.openDB();
1107
- const tx = db.transaction(["metadata", "response", "visits"], "readwrite");
1108
- const metadataStore = tx.objectStore("metadata");
1109
- const visitsStore = tx.objectStore("visits");
1110
- const keys = [];
1111
- let cursor = await visitsStore.index("lastVisitedAt").openCursor();
1112
- while (deficitSize > 0 && cursor) {
1113
- const metadata = await metadataStore.get(cursor.value.key);
1114
- if (!metadata) {
1115
- await cursor.delete();
1116
- cursor = await cursor.continue();
1117
- continue;
1118
- }
1119
- deficitSize -= metadata.size;
1120
- keys.push(cursor.value.key);
1121
- cursor = await cursor.continue();
1122
- }
1123
- if (deficitSize > 0) {
1124
- this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
1125
- tx.abort();
1126
- return false;
1127
- }
1128
- await this.__remove__(tx, keys);
1129
- await tx.done;
1130
- (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
1131
- return true;
1132
- }
1028
+ constructor(options) {
1029
+ super(options);
1030
+ }
1031
+ async get(key) {
1032
+ const entry = await super.get(key);
1033
+ this.__onCacheGet__?.({ key });
1034
+ return entry;
1035
+ }
1036
+ async set(value) {
1037
+ await super.set(value);
1038
+ this.__onCacheSet__?.({ key: value.key });
1039
+ }
1040
+ async remove(key) {
1041
+ await super.remove(key);
1042
+ this.__onCacheRemove__?.({ key });
1043
+ }
1044
+ async evict(expectSize) {
1045
+ await this.evictExpired();
1046
+ let deficitSize = expectSize - (await this.getSize()).free;
1047
+ if (deficitSize <= 0) return true;
1048
+ const tx = (await this.openDB()).transaction([
1049
+ "metadata",
1050
+ "response",
1051
+ "visits"
1052
+ ], "readwrite");
1053
+ const metadataStore = tx.objectStore("metadata");
1054
+ const visitsStore = tx.objectStore("visits");
1055
+ const keys = [];
1056
+ let cursor = await visitsStore.index("lastVisitedAt").openCursor();
1057
+ while (deficitSize > 0 && cursor) {
1058
+ const metadata = await metadataStore.get(cursor.value.key);
1059
+ if (!metadata) {
1060
+ await cursor.delete();
1061
+ cursor = await cursor.continue();
1062
+ continue;
1063
+ }
1064
+ deficitSize -= metadata.size;
1065
+ keys.push(cursor.value.key);
1066
+ cursor = await cursor.continue();
1067
+ }
1068
+ if (deficitSize > 0) {
1069
+ this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize}`));
1070
+ tx.abort();
1071
+ return false;
1072
+ }
1073
+ await this.__remove__(tx, keys);
1074
+ await tx.done;
1075
+ this.__onCacheEvict__?.({ keys });
1076
+ return true;
1077
+ }
1133
1078
  };
1134
-
1135
- // src/storage/indexed-db-storage/ttl-indexed-db-storage.ts
1079
+ //#endregion
1080
+ //#region src/storage/indexed-db-storage/ttl-indexed-db-storage.ts
1136
1081
  var TTLIndexedDBStorage = class extends BaseIndexedDBStorage {
1137
- constructor(options) {
1138
- super(options);
1139
- }
1140
- async get(key) {
1141
- var _a;
1142
- const entry = await super.get(key);
1143
- (_a = this.__onCacheGet__) == null ? void 0 : _a.call(this, { key });
1144
- return entry;
1145
- }
1146
- async set(value) {
1147
- var _a;
1148
- await super.set(value);
1149
- (_a = this.__onCacheSet__) == null ? void 0 : _a.call(this, { key: value.key });
1150
- }
1151
- async remove(key) {
1152
- var _a;
1153
- await super.remove(key);
1154
- (_a = this.__onCacheRemove__) == null ? void 0 : _a.call(this, { key });
1155
- }
1156
- async evict(expectSize) {
1157
- var _a;
1158
- await this.evictExpired();
1159
- const size = await this.getSize();
1160
- let deficitSize = expectSize - size.free;
1161
- if (deficitSize <= 0) return true;
1162
- const db = await this.openDB();
1163
- const tx = db.transaction(["metadata", "response", "visits"], "readwrite");
1164
- const metadataStore = tx.objectStore("metadata");
1165
- const keys = [];
1166
- let cursor = await metadataStore.index("expiredAt").openCursor();
1167
- while (deficitSize > 0 && cursor) {
1168
- const metadata = await metadataStore.get(cursor.value.key);
1169
- if (!metadata) {
1170
- await cursor.delete();
1171
- cursor = await cursor.continue();
1172
- continue;
1173
- }
1174
- deficitSize -= metadata.size;
1175
- keys.push(cursor.value.key);
1176
- cursor = await cursor.continue();
1177
- }
1178
- if (deficitSize > 0) {
1179
- this.debug((log) => log("Storage Size Not Enough, deficit size: ".concat(deficitSize)));
1180
- tx.abort();
1181
- return false;
1182
- }
1183
- await this.__remove__(tx, keys);
1184
- await tx.done;
1185
- (_a = this.__onCacheEvict__) == null ? void 0 : _a.call(this, { keys });
1186
- return true;
1187
- }
1082
+ constructor(options) {
1083
+ super(options);
1084
+ }
1085
+ async get(key) {
1086
+ const entry = await super.get(key);
1087
+ this.__onCacheGet__?.({ key });
1088
+ return entry;
1089
+ }
1090
+ async set(value) {
1091
+ await super.set(value);
1092
+ this.__onCacheSet__?.({ key: value.key });
1093
+ }
1094
+ async remove(key) {
1095
+ await super.remove(key);
1096
+ this.__onCacheRemove__?.({ key });
1097
+ }
1098
+ async evict(expectSize) {
1099
+ await this.evictExpired();
1100
+ let deficitSize = expectSize - (await this.getSize()).free;
1101
+ if (deficitSize <= 0) return true;
1102
+ const tx = (await this.openDB()).transaction([
1103
+ "metadata",
1104
+ "response",
1105
+ "visits"
1106
+ ], "readwrite");
1107
+ const metadataStore = tx.objectStore("metadata");
1108
+ const keys = [];
1109
+ let cursor = await metadataStore.index("expiredAt").openCursor();
1110
+ while (deficitSize > 0 && cursor) {
1111
+ const metadata = await metadataStore.get(cursor.value.key);
1112
+ if (!metadata) {
1113
+ await cursor.delete();
1114
+ cursor = await cursor.continue();
1115
+ continue;
1116
+ }
1117
+ deficitSize -= metadata.size;
1118
+ keys.push(cursor.value.key);
1119
+ cursor = await cursor.continue();
1120
+ }
1121
+ if (deficitSize > 0) {
1122
+ this.debug((log) => log(`Storage Size Not Enough, deficit size: ${deficitSize}`));
1123
+ tx.abort();
1124
+ return false;
1125
+ }
1126
+ await this.__remove__(tx, keys);
1127
+ await tx.done;
1128
+ this.__onCacheEvict__?.({ keys });
1129
+ return true;
1130
+ }
1188
1131
  };
1189
-
1190
- // src/storage/indexed-db-storage/indexed-db-storage.ts
1132
+ //#endregion
1133
+ //#region src/storage/indexed-db-storage/indexed-db-storage.ts
1191
1134
  var IndexedDBStorage = class extends KeqCacheStorage {
1192
- constructor(options) {
1193
- super();
1194
- __publicField(this, "storage");
1195
- const eviction = (options == null ? void 0 : options.eviction) || "lru" /* LRU */;
1196
- if (eviction === "random" /* RANDOM */) {
1197
- this.storage = new RandomIndexedDBStorage(options);
1198
- } else if (eviction === "lfu" /* LFU */) {
1199
- this.storage = new LFUIndexedDBStorage(options);
1200
- } else if (eviction === "lru" /* LRU */) {
1201
- this.storage = new LRUIndexedDBStorage(options);
1202
- } else if (eviction === "ttl" /* TTL */) {
1203
- this.storage = new TTLIndexedDBStorage(options);
1204
- } else {
1205
- throw new CacheException("Not Supported Eviction: ".concat(String(options == null ? void 0 : options.eviction)));
1206
- }
1207
- }
1208
- set(entry) {
1209
- return this.storage.set(entry);
1210
- }
1211
- get(key) {
1212
- return this.storage.get(key);
1213
- }
1214
- remove(key) {
1215
- return this.storage.remove(key);
1216
- }
1135
+ constructor(options) {
1136
+ super();
1137
+ _defineProperty(this, "storage", void 0);
1138
+ const eviction = options?.eviction || "lru";
1139
+ if (eviction === "random") this.storage = new RandomIndexedDBStorage(options);
1140
+ else if (eviction === "lfu") this.storage = new LFUIndexedDBStorage(options);
1141
+ else if (eviction === "lru") this.storage = new LRUIndexedDBStorage(options);
1142
+ else if (eviction === "ttl") this.storage = new TTLIndexedDBStorage(options);
1143
+ else throw new CacheException(`Not Supported Eviction: ${String(options?.eviction)}`);
1144
+ }
1145
+ set(entry) {
1146
+ return this.storage.set(entry);
1147
+ }
1148
+ get(key) {
1149
+ return this.storage.get(key);
1150
+ }
1151
+ remove(key) {
1152
+ return this.storage.remove(key);
1153
+ }
1217
1154
  };
1218
-
1219
- // src/storage/multi-tier-storage/multi-tier-storage.ts
1155
+ //#endregion
1156
+ //#region src/storage/multi-tier-storage/multi-tier-storage.ts
1157
+ /**
1158
+ * @en Multi-tier cache storage that manages multiple KeqCacheStorage instances in tiers
1159
+ * @zh 多层缓存存储,管理多个分层的 KeqCacheStorage 实例
1160
+ */
1220
1161
  var MultiTierStorage = class extends KeqCacheStorage {
1221
- /**
1222
- * @param storages Array of storage instances ordered by performance (fastest first)
1223
- * @zh 按性价比排序的存储实例数组,排在前面的成本低,排在后面的成本高
1224
- */
1225
- constructor(options) {
1226
- super();
1227
- __publicField(this, "storages");
1228
- if (!options.tiers || options.tiers.length === 0) {
1229
- throw new Error("At least one storage instance is required");
1230
- }
1231
- this.storages = [...options.tiers];
1232
- }
1233
- /**
1234
- * @en Get cache entry, searching from lowest to highest tier
1235
- * @zh 获取缓存条目,从最底层到高层依次搜索
1236
- */
1237
- async get(key) {
1238
- for (let i = 0; i < this.storages.length; i++) {
1239
- const storage = this.storages[i];
1240
- const entry = await storage.get(key);
1241
- if (entry) {
1242
- if (i > 0) {
1243
- await this.syncToLowerTiers(entry, i);
1244
- }
1245
- return entry;
1246
- }
1247
- }
1248
- return void 0;
1249
- }
1250
- /**
1251
- * @en Set cache entry to all tiers concurrently
1252
- * @zh 并发写入所有层的缓存
1253
- */
1254
- async set(entry) {
1255
- const promises = this.storages.map(async (storage) => storage.set(entry));
1256
- await Promise.all(promises);
1257
- }
1258
- /**
1259
- * @en Remove cache entry from all tiers
1260
- * @zh 从所有层删除缓存条目
1261
- */
1262
- async remove(key) {
1263
- const promises = this.storages.map(async (storage) => storage.remove(key));
1264
- await Promise.all(promises);
1265
- }
1266
- /**
1267
- * @en Sync cache entry to all lower tiers (tiers with index < currentTierIndex)
1268
- * @zh 将缓存条目同步到所有低层存储(索引小于当前层的存储)
1269
- */
1270
- async syncToLowerTiers(entry, currentTierIndex) {
1271
- const lowerTierStorages = this.storages.slice(0, currentTierIndex);
1272
- if (lowerTierStorages.length === 0) {
1273
- return;
1274
- }
1275
- const promises = lowerTierStorages.map(async (storage) => storage.set(entry.clone()));
1276
- try {
1277
- await Promise.all(promises);
1278
- } catch (error) {
1279
- console.warn("Failed to sync cache entry to lower tiers:", error);
1280
- }
1281
- }
1162
+ /**
1163
+ * @param storages Array of storage instances ordered by performance (fastest first)
1164
+ * @zh 按性价比排序的存储实例数组,排在前面的成本低,排在后面的成本高
1165
+ */
1166
+ constructor(options) {
1167
+ super();
1168
+ _defineProperty(this, "storages", void 0);
1169
+ if (!options.tiers || options.tiers.length === 0) throw new Error("At least one storage instance is required");
1170
+ this.storages = [...options.tiers];
1171
+ }
1172
+ /**
1173
+ * @en Get cache entry, searching from lowest to highest tier
1174
+ * @zh 获取缓存条目,从最底层到高层依次搜索
1175
+ */
1176
+ async get(key) {
1177
+ for (let i = 0; i < this.storages.length; i++) {
1178
+ const entry = await this.storages[i].get(key);
1179
+ if (entry) {
1180
+ if (i > 0) await this.syncToLowerTiers(entry, i);
1181
+ return entry;
1182
+ }
1183
+ }
1184
+ }
1185
+ /**
1186
+ * @en Set cache entry to all tiers concurrently
1187
+ * @zh 并发写入所有层的缓存
1188
+ */
1189
+ async set(entry) {
1190
+ const promises = this.storages.map(async (storage) => storage.set(entry));
1191
+ await Promise.all(promises);
1192
+ }
1193
+ /**
1194
+ * @en Remove cache entry from all tiers
1195
+ * @zh 从所有层删除缓存条目
1196
+ */
1197
+ async remove(key) {
1198
+ const promises = this.storages.map(async (storage) => storage.remove(key));
1199
+ await Promise.all(promises);
1200
+ }
1201
+ /**
1202
+ * @en Sync cache entry to all lower tiers (tiers with index < currentTierIndex)
1203
+ * @zh 将缓存条目同步到所有低层存储(索引小于当前层的存储)
1204
+ */
1205
+ async syncToLowerTiers(entry, currentTierIndex) {
1206
+ const lowerTierStorages = this.storages.slice(0, currentTierIndex);
1207
+ if (lowerTierStorages.length === 0) return;
1208
+ const promises = lowerTierStorages.map(async (storage) => storage.set(entry.clone()));
1209
+ try {
1210
+ await Promise.all(promises);
1211
+ } catch (error) {
1212
+ console.warn("Failed to sync cache entry to lower tiers:", error);
1213
+ }
1214
+ }
1282
1215
  };
1283
-
1284
- // src/storage/tier-storage/tier-storage.ts
1216
+ //#endregion
1217
+ //#region src/storage/tier-storage/tier-storage.ts
1218
+ /**
1219
+ * @en Two-tier cache storage that combines MemoryStorage (L1) and IndexedDBStorage (L2)
1220
+ * @zh 二级缓存存储,结合了MemoryStorage(一级)和IndexedDBStorage(二级)
1221
+ *
1222
+ * This is a convenience wrapper around MultiTierStorage that provides:
1223
+ * - Fast in-memory cache (L1)
1224
+ * - Persistent IndexedDB cache (L2)
1225
+ * - Simplified configuration options
1226
+ */
1285
1227
  var TierStorage = class extends MultiTierStorage {
1286
- constructor(options) {
1287
- const memoryStorage = (options == null ? void 0 : options.memory) instanceof MemoryStorage ? options.memory : new MemoryStorage(options == null ? void 0 : options.memory);
1288
- const indexedDBStorage = (options == null ? void 0 : options.indexedDB) instanceof IndexedDBStorage ? options.indexedDB : new IndexedDBStorage(options == null ? void 0 : options.indexedDB);
1289
- super({
1290
- tiers: [memoryStorage, indexedDBStorage]
1291
- });
1292
- }
1293
- };
1294
- export {
1295
- Eviction,
1296
- IndexedDBStorage,
1297
- KeqCacheStorage,
1298
- MemoryStorage,
1299
- MultiTierStorage,
1300
- Size,
1301
- Strategy,
1302
- TierStorage,
1303
- cache
1228
+ constructor(options) {
1229
+ const memoryStorage = options?.memory instanceof MemoryStorage ? options.memory : new MemoryStorage(options?.memory);
1230
+ const indexedDBStorage = options?.indexedDB instanceof IndexedDBStorage ? options.indexedDB : new IndexedDBStorage(options?.indexedDB);
1231
+ super({ tiers: [memoryStorage, indexedDBStorage] });
1232
+ }
1304
1233
  };
1234
+ //#endregion
1235
+ export { Eviction, IndexedDBStorage, KeqCacheStorage, MemoryStorage, MultiTierStorage, Size, Strategy, TierStorage, cache };
1236
+
1305
1237
  //# sourceMappingURL=index.mjs.map