@keq-request/cache 5.0.0-alpha.7 → 5.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,353 @@
1
+ import { queueAsPromised } from "fastq";
2
+ import { Keq, KeqContext, KeqExecutionContext, KeqMiddleware, KeqNext } from "keq";
3
+ import { Promisable } from "type-fest";
4
+ //#region src/cache-entry/types/cache-entry-options.d.ts
5
+ interface CacheEntryOptions {
6
+ key: string;
7
+ response: Response;
8
+ size: number;
9
+ expiredAt?: Date;
10
+ }
11
+ //#endregion
12
+ //#region src/cache-entry/types/cache-entry-build-options.d.ts
13
+ interface CacheEntryBuildOptions {
14
+ key: string;
15
+ response: Response;
16
+ size?: number;
17
+ ttl?: number;
18
+ expiredAt?: Date;
19
+ }
20
+ //#endregion
21
+ //#region src/cache-entry/cache-entry.d.ts
22
+ declare class CacheEntry {
23
+ key: string;
24
+ response: Response;
25
+ /**
26
+ * @en bytes
27
+ * @zh 字节数
28
+ */
29
+ size: number;
30
+ expiredAt: Date;
31
+ constructor(options: CacheEntryOptions);
32
+ static build(options: CacheEntryBuildOptions): Promise<CacheEntry>;
33
+ clone(): CacheEntry;
34
+ assignResponseHeaders(headers: Headers): void;
35
+ }
36
+ //#endregion
37
+ //#region src/storage/keq-cache-storage.d.ts
38
+ declare abstract class KeqCacheStorage {
39
+ /**
40
+ * @en Get the length of the storage
41
+ * @zh 获取 Storage 缓存的条目数量
42
+ */
43
+ abstract get(key: string): Promisable<CacheEntry | undefined>;
44
+ /**
45
+ * @en Set a new entry to the storage
46
+ * @zh 将被缓存的数据添加到Storage中
47
+ */
48
+ abstract set(entry: CacheEntry): Promisable<void>;
49
+ /**
50
+ * @en Remove an entry by key
51
+ * @zh 根据key删除Storage中的数据
52
+ */
53
+ abstract remove(key: string): Promisable<void>;
54
+ }
55
+ //#endregion
56
+ //#region src/types/keq-cache-key.d.ts
57
+ type KeqCacheKeyFactory = ((context: KeqContext) => string);
58
+ type KeqCacheKey = string | KeqCacheKeyFactory;
59
+ //#endregion
60
+ //#region src/constants/eviction.enum.d.ts
61
+ declare enum Eviction {
62
+ LRU = "lru",
63
+ LFU = "lfu",
64
+ RANDOM = "random",
65
+ TTL = "ttl"
66
+ }
67
+ //#endregion
68
+ //#region src/constants/size.enum.d.ts
69
+ declare enum Size {
70
+ B = 1,
71
+ KB = 1024,
72
+ MB = 1048576,
73
+ GB = 1073741824
74
+ }
75
+ //#endregion
76
+ //#region src/constants/strategy.enum.d.ts
77
+ declare const Strategy: {
78
+ STALE_WHILE_REVALIDATE: KeqCacheStrategy;
79
+ NETWORK_FIRST: KeqCacheStrategy;
80
+ NETWORK_ONLY: KeqCacheStrategy;
81
+ CACHE_FIRST: KeqCacheStrategy;
82
+ };
83
+ //#endregion
84
+ //#region src/storage/internal-storage/types/events.d.ts
85
+ interface OnCacheGetEvent {
86
+ readonly key: string;
87
+ }
88
+ interface OnCacheSetEvent {
89
+ readonly key: string;
90
+ }
91
+ interface OnCacheRemoveEvent {
92
+ readonly key: string;
93
+ }
94
+ interface OnCacheEvictEvent {
95
+ readonly keys: string[];
96
+ }
97
+ interface OnCacheExpiredEvent {
98
+ readonly keys: string[];
99
+ }
100
+ //#endregion
101
+ //#region src/storage/internal-storage/types/storage-options.d.ts
102
+ interface InternalStorageOptions {
103
+ /**
104
+ * @default false
105
+ */
106
+ debug?: boolean;
107
+ /**
108
+ * @en bytes
109
+ * @zh 字节数
110
+ *
111
+ * @default Infinity
112
+ */
113
+ size?: number;
114
+ onCacheGet?: (event: OnCacheGetEvent) => void;
115
+ onCacheSet?: (event: OnCacheSetEvent) => void;
116
+ onCacheRemove?: (event: OnCacheRemoveEvent) => void;
117
+ onCacheEvict?: (event: OnCacheEvictEvent) => void;
118
+ onCacheExpired?: (event: OnCacheExpiredEvent) => void;
119
+ }
120
+ //#endregion
121
+ //#region src/storage/memory-storage/types/memory-storage-options.d.ts
122
+ interface MemoryStorageOptions extends InternalStorageOptions {
123
+ /**
124
+ * @default Eviction.LRU
125
+ */
126
+ eviction?: Eviction;
127
+ }
128
+ //#endregion
129
+ //#region src/storage/memory-storage/memory-storage.d.ts
130
+ declare class MemoryStorage extends KeqCacheStorage {
131
+ private storage;
132
+ constructor(options?: MemoryStorageOptions);
133
+ set(entry: CacheEntry): Promisable<void>;
134
+ get(key: string): Promisable<CacheEntry | undefined>;
135
+ remove(key: string): Promisable<void>;
136
+ print(): Promise<void>;
137
+ }
138
+ //#endregion
139
+ //#region src/storage/indexed-db-storage/types/indexed-db-storage-options.d.ts
140
+ interface IndexedDbStorageOptions extends InternalStorageOptions {
141
+ /**
142
+ * @en The table name for the IndexedDB storage, multiple instances using the same table name will share the cached data.
143
+ * @zh IndexedDB 存储的表名, 多个实例使用同一个表名会共享缓存数据。
144
+ */
145
+ tableName?: string;
146
+ /**
147
+ * @default Eviction.LRU
148
+ */
149
+ eviction?: Eviction;
150
+ }
151
+ //#endregion
152
+ //#region src/storage/indexed-db-storage/indexed-db-storage.d.ts
153
+ declare class IndexedDBStorage extends KeqCacheStorage {
154
+ private storage;
155
+ constructor(options?: IndexedDbStorageOptions);
156
+ set(entry: CacheEntry): Promisable<void>;
157
+ get(key: string): Promisable<CacheEntry | undefined>;
158
+ remove(key: string): Promisable<void>;
159
+ }
160
+ //#endregion
161
+ //#region src/storage/multi-tier-storage/types/multi-tier-storage-options.d.ts
162
+ interface MultiTierStorageOptions {
163
+ tiers: KeqCacheStorage[];
164
+ }
165
+ //#endregion
166
+ //#region src/storage/multi-tier-storage/multi-tier-storage.d.ts
167
+ /**
168
+ * @en Multi-tier cache storage that manages multiple KeqCacheStorage instances in tiers
169
+ * @zh 多层缓存存储,管理多个分层的 KeqCacheStorage 实例
170
+ */
171
+ declare class MultiTierStorage extends KeqCacheStorage {
172
+ private readonly storages;
173
+ /**
174
+ * @param storages Array of storage instances ordered by performance (fastest first)
175
+ * @zh 按性价比排序的存储实例数组,排在前面的成本低,排在后面的成本高
176
+ */
177
+ constructor(options: MultiTierStorageOptions);
178
+ /**
179
+ * @en Get cache entry, searching from lowest to highest tier
180
+ * @zh 获取缓存条目,从最底层到高层依次搜索
181
+ */
182
+ get(key: string): Promise<CacheEntry | undefined>;
183
+ /**
184
+ * @en Set cache entry to all tiers concurrently
185
+ * @zh 并发写入所有层的缓存
186
+ */
187
+ set(entry: CacheEntry): Promise<void>;
188
+ /**
189
+ * @en Remove cache entry from all tiers
190
+ * @zh 从所有层删除缓存条目
191
+ */
192
+ remove(key: string): Promise<void>;
193
+ /**
194
+ * @en Sync cache entry to all lower tiers (tiers with index < currentTierIndex)
195
+ * @zh 将缓存条目同步到所有低层存储(索引小于当前层的存储)
196
+ */
197
+ private syncToLowerTiers;
198
+ }
199
+ //#endregion
200
+ //#region src/storage/tier-storage/types/tier-storage-options.d.ts
201
+ /**
202
+ * @en Configuration options for TierStorage
203
+ * @zh TierStorage 的配置选项
204
+ */
205
+ interface TierStorageOptions {
206
+ /**
207
+ * @en Memory storage instance or options for the memory storage (L1 cache)
208
+ * @zh 内存存储实例或配置选项(一级缓存)
209
+ */
210
+ memory?: MemoryStorage | MemoryStorageOptions;
211
+ /**
212
+ * @en IndexedDB storage instance or options for the IndexedDB storage (L2 cache)
213
+ * @zh IndexedDB 存储实例或配置选项(二级缓存)
214
+ */
215
+ indexedDB?: IndexedDBStorage | IndexedDbStorageOptions;
216
+ }
217
+ //#endregion
218
+ //#region src/storage/tier-storage/tier-storage.d.ts
219
+ /**
220
+ * @en Two-tier cache storage that combines MemoryStorage (L1) and IndexedDBStorage (L2)
221
+ * @zh 二级缓存存储,结合了MemoryStorage(一级)和IndexedDBStorage(二级)
222
+ *
223
+ * This is a convenience wrapper around MultiTierStorage that provides:
224
+ * - Fast in-memory cache (L1)
225
+ * - Persistent IndexedDB cache (L2)
226
+ * - Simplified configuration options
227
+ */
228
+ declare class TierStorage extends MultiTierStorage {
229
+ constructor(options?: TierStorageOptions);
230
+ }
231
+ //#endregion
232
+ //#region src/request-cache-handler/request-cache-handler.d.ts
233
+ type RequestCacheHandlerOptions = Omit<RequestCacheOptions, 'strategy'>;
234
+ declare class RequestCacheHandler {
235
+ readonly cacheKey: string;
236
+ readonly storage: KeqCacheStorage;
237
+ readonly options: Readonly<RequestCacheHandlerOptions>;
238
+ constructor(cacheKey: string, storage: KeqCacheStorage, options: Readonly<RequestCacheHandlerOptions>);
239
+ /**
240
+ * Resolve cache key for request context
241
+ */
242
+ static resolveRequestCacheKey(context: KeqContext, options: RequestCacheHandlerOptions): string;
243
+ /**
244
+ * Get cache from storage
245
+ */
246
+ getCache(): Promise<[string, CacheEntry | undefined]>;
247
+ /**
248
+ * Store response that in context to storage
249
+ */
250
+ setCache(context: KeqContext): Promise<[string, CacheEntry | undefined]>;
251
+ }
252
+ //#endregion
253
+ //#region src/types/keq-cache-strategy.d.ts
254
+ interface KeqCacheStrategy {
255
+ (handler: RequestCacheHandler, context: KeqExecutionContext, next: KeqNext): Promise<void>;
256
+ }
257
+ //#endregion
258
+ //#region src/types/request-cache-options.d.ts
259
+ /**
260
+ * The options for a cacheable request.
261
+ */
262
+ interface RequestCacheOptions {
263
+ /**
264
+ * Enable debug logs
265
+ */
266
+ debug?: boolean;
267
+ /**
268
+ * Enable Server-Timing header
269
+ */
270
+ serverTiming?: boolean;
271
+ /**
272
+ * This configuration item controls whether requests are allowed to be concurrent.
273
+ * By default, concurrency is disabled
274
+ *
275
+ * When multiple requests with the same key occur,
276
+ * the subsequent ones are blocked until the previous request completes.
277
+ * This ensures that the cache is utilized effectively and prevents redundant processing for identical keys
278
+ *
279
+ * @default false
280
+ */
281
+ concurrent?: boolean;
282
+ /**
283
+ * Cache Key
284
+ */
285
+ key?: KeqCacheKey;
286
+ /**
287
+ * @default Strategy.NETWORK_FIRST
288
+ */
289
+ strategy: KeqCacheStrategy;
290
+ /**
291
+ * @en seconds
292
+ * @zh 秒
293
+ *
294
+ * @default Infinity
295
+ */
296
+ ttl?: number;
297
+ /**
298
+ * If exclude is true, the request will not be cached.
299
+ */
300
+ exclude?: (res: Response) => (Promise<boolean> | boolean);
301
+ }
302
+ //#endregion
303
+ //#region src/types/keq-cache-pattern.d.ts
304
+ type KeqCachePattern = RegExp | ((ctx: KeqContext) => boolean) | true;
305
+ //#endregion
306
+ //#region src/types/keq-cache-rule.d.ts
307
+ interface KeqCacheRule extends RequestCacheOptions {
308
+ pattern?: KeqCachePattern;
309
+ }
310
+ //#endregion
311
+ //#region src/cache.d.ts
312
+ interface KeqCacheOptions extends Pick<RequestCacheOptions, 'serverTiming'> {
313
+ storage: KeqCacheStorage;
314
+ /**
315
+ * Cache Key Factory
316
+ */
317
+ keyFactory?: KeqCacheKeyFactory;
318
+ rules?: KeqCacheRule[];
319
+ }
320
+ declare module 'keq' {
321
+ interface KeqMiddlewareOptions<OP> {
322
+ /**
323
+ * [keq-cache](https://github.com/keq-request/keq-cache)
324
+ */
325
+ cache(option: RequestCacheOptions | false): Keq<OP>;
326
+ }
327
+ interface KeqEvents {
328
+ 'cache:hit': {
329
+ key: string;
330
+ response: Response;
331
+ context: KeqContext;
332
+ };
333
+ 'cache:miss': {
334
+ key: string;
335
+ context: KeqContext;
336
+ };
337
+ 'cache:update': {
338
+ key: string;
339
+ oldResponse?: Response;
340
+ newResponse: Response;
341
+ context: KeqContext;
342
+ };
343
+ }
344
+ interface KeqGlobalCore {
345
+ cache?: Record<string, queueAsPromised<{
346
+ next: KeqNext;
347
+ }, void>>;
348
+ }
349
+ }
350
+ declare function cache(options: KeqCacheOptions): KeqMiddleware;
351
+ //#endregion
352
+ export { Eviction, IndexedDBStorage, type IndexedDbStorageOptions, type KeqCacheKey, type KeqCacheKeyFactory, KeqCacheOptions, type KeqCachePattern, type KeqCacheRule, KeqCacheStorage, type KeqCacheStrategy, MemoryStorage, type MemoryStorageOptions, MultiTierStorage, type MultiTierStorageOptions, Size, Strategy, TierStorage, type TierStorageOptions, cache };
353
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/cache-entry/types/cache-entry-options.ts","../src/cache-entry/types/cache-entry-build-options.ts","../src/cache-entry/cache-entry.ts","../src/storage/keq-cache-storage.ts","../src/types/keq-cache-key.ts","../src/constants/eviction.enum.ts","../src/constants/size.enum.ts","../src/constants/strategy.enum.ts","../src/storage/internal-storage/types/events.ts","../src/storage/internal-storage/types/storage-options.ts","../src/storage/memory-storage/types/memory-storage-options.ts","../src/storage/memory-storage/memory-storage.ts","../src/storage/indexed-db-storage/types/indexed-db-storage-options.ts","../src/storage/indexed-db-storage/indexed-db-storage.ts","../src/storage/multi-tier-storage/types/multi-tier-storage-options.ts","../src/storage/multi-tier-storage/multi-tier-storage.ts","../src/storage/tier-storage/types/tier-storage-options.ts","../src/storage/tier-storage/tier-storage.ts","../src/request-cache-handler/request-cache-handler.ts","../src/types/keq-cache-strategy.ts","../src/types/request-cache-options.ts","../src/types/keq-cache-pattern.ts","../src/types/keq-cache-rule.ts","../src/cache.ts"],"mappings":";;;;UAAiB,iBAAA;EACf,GAAA;EACA,QAAA,EAAU,QAAA;EACV,IAAA;EACA,SAAA,GAAY,IAAA;AAAA;;;UCJG,sBAAA;EACf,GAAA;EACA,QAAA,EAAU,QAAA;EACV,IAAA;EACA,GAAA;EACA,SAAA,GAAY,IAAA;AAAA;;;cCED,UAAA;EACX,GAAA;EACA,QAAA,EAAU,QAAA;;AFTZ;;;EEeE,IAAA;EAEA,SAAA,EAAW,IAAA;cAEC,OAAA,EAAS,iBAAA;EAAA,OAOR,KAAA,CAAM,OAAA,EAAS,sBAAA,GAAyB,OAAA,CAAQ,UAAA;EAgB7D,KAAA,CAAA,GAAS,UAAA;EAST,qBAAA,CAAsB,OAAA,EAAS,OAAA;AAAA;;;uBC/CX,eAAA;;;;AHJtB;WGSW,GAAA,CAAI,GAAA,WAAc,UAAA,CAAW,UAAA;;;;;WAM7B,GAAA,CAAI,KAAA,EAAO,UAAA,GAAa,UAAA;EHZjC;;;;EAAA,SGkBS,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