@isdk/proxy 0.1.1 → 0.1.3

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 (36) hide show
  1. package/README.cn.md +249 -9
  2. package/README.md +249 -7
  3. package/dist/index.d.mts +374 -41
  4. package/dist/index.d.ts +374 -41
  5. package/dist/index.js +1 -1
  6. package/dist/index.mjs +1 -1
  7. package/docs/README.md +249 -7
  8. package/docs/classes/OfflineCacheMissError.md +426 -0
  9. package/docs/classes/SmartCache.md +81 -13
  10. package/docs/functions/createCachedFetch.md +1 -1
  11. package/docs/functions/createFetchWithCache.md +1 -1
  12. package/docs/functions/extractData.md +34 -5
  13. package/docs/functions/fetchWithCache.md +18 -9
  14. package/docs/functions/generateCacheKey.md +34 -4
  15. package/docs/functions/getSiteConfig.md +39 -0
  16. package/docs/functions/isAllowed.md +35 -8
  17. package/docs/functions/isCacheable.md +27 -0
  18. package/docs/functions/isGlob.md +23 -0
  19. package/docs/functions/isMatch.md +44 -0
  20. package/docs/functions/prefetch.md +33 -0
  21. package/docs/globals.md +15 -0
  22. package/docs/interfaces/BodyFilterConfig.md +77 -0
  23. package/docs/interfaces/CacheEntry.md +9 -9
  24. package/docs/interfaces/CacheMetadata.md +8 -8
  25. package/docs/interfaces/CacheRule.md +80 -0
  26. package/docs/interfaces/FetchWithCacheContext.md +44 -16
  27. package/docs/interfaces/FetchWithCacheOptions.md +40 -12
  28. package/docs/interfaces/KeyFilterConfig.md +11 -7
  29. package/docs/interfaces/PrefetchOptions.md +107 -0
  30. package/docs/interfaces/PrefetchRequest.md +31 -0
  31. package/docs/interfaces/PrefetchResult.md +47 -0
  32. package/docs/interfaces/ProxyConfig.md +4 -4
  33. package/docs/interfaces/SiteCacheConfig.md +56 -11
  34. package/docs/interfaces/SmartCacheOptions.md +32 -6
  35. package/docs/variables/OfflineCacheMissErrorCode.md +18 -0
  36. package/package.json +5 -3
package/dist/index.d.mts CHANGED
@@ -1,4 +1,30 @@
1
- import { KeyvCacheableMemoryOptions } from '@cacheable/memory';
1
+ import { CommonError, ErrorCode } from '@isdk/common-error';
2
+
3
+ /**
4
+ * 错误类型定义
5
+ */
6
+
7
+ /**
8
+ * Offline 缓存未命中错误代码
9
+ *
10
+ * 当处于 offline 模式且请求的 URL 没有对应缓存时抛出。
11
+ * 这帮助调用者区分:
12
+ * - 网络请求失败(其他错误类型)
13
+ * - offline 模式下缓存不存在(本错误)
14
+ */
15
+ declare const OfflineCacheMissErrorCode = ErrorCode.OfflineCacheMiss;
16
+ /**
17
+ * Offline 缓存未命中错误
18
+ *
19
+ * @example
20
+ * throw new OfflineCacheMissError('http://example.com/data')
21
+ *
22
+ * @extends CommonError
23
+ */
24
+ declare class OfflineCacheMissError extends CommonError {
25
+ static code: ErrorCode;
26
+ constructor(url: string | number, name?: string | Record<string, any>);
27
+ }
2
28
 
3
29
  /**
4
30
  * 缓存键过滤配置
@@ -6,25 +32,107 @@ import { KeyvCacheableMemoryOptions } from '@cacheable/memory';
6
32
  * 用于定义在生成缓存指纹时,哪些字段应该被包含或排除。
7
33
  */
8
34
  interface KeyFilterConfig {
9
- /** 仅包含(白名单):如果设置,只有这些字段会参与 Key 的计算 */
10
- include?: string[];
11
- /** 排除(黑名单):用于排除像 `timestamp`、`nonce` 等干扰缓存命中的动态字段 */
12
- exclude?: string[];
35
+ /** 仅包含(白名单):如果设置,只有这些字段会参与 Key 的计算。支持字符串、Glob 模式或正则表达式。 */
36
+ include?: (string | RegExp)[];
37
+ /** 排除(黑名单):用于排除像 `timestamp`、`nonce` 等干扰缓存命中的动态字段。支持字符串、Glob 模式或正则表达式。 */
38
+ exclude?: (string | RegExp)[];
39
+ }
40
+ interface BodyFilterConfig extends KeyFilterConfig {
41
+ /**
42
+ * 用于非 JSON (文本) Body 的提取正则表达式。
43
+ * 如果包含捕获组,则提取捕获组内容作为指纹;否则提取整个匹配部分。
44
+ */
45
+ extract?: string | RegExp;
46
+ /**
47
+ * 是否对提取出的捕获组进行排序。
48
+ * 开启后可解决 Body 中参数顺序不一致导致的指纹失效问题。
49
+ */
50
+ sort?: boolean;
51
+ /** 用于正则匹配/提取 Body 时的最大长度限制,默认 1024 (1KB) */
52
+ maxLength?: number;
53
+ }
54
+ /**
55
+ * 精细化缓存匹配规则
56
+ *
57
+ * 用于在 `methods` 过滤的基础上,进一步限定哪些具体的请求路径或参数需要被缓存。
58
+ * 多个规则之间是 **OR (逻辑或)** 关系,即请求只需匹配其中一条规则即可。
59
+ * 在单个规则对象内部,各字段之间是 **AND (逻辑与)** 关系。
60
+ */
61
+ interface CacheRule {
62
+ /**
63
+ * 匹配的方法 (如 "POST")。
64
+ * 如果指定,则必须方法完全一致;如果不指定,则匹配所有 `methods` 中允许的方法。
65
+ */
66
+ method?: string;
67
+ /**
68
+ * 路径匹配。
69
+ * - 字符串: 默认进行 Glob 模式匹配(支持 `!` 否定),若非 Glob 且非正则字符串则退化为前缀匹配。
70
+ * - 正则表达式: 检查 `url.pathname` 是否匹配。
71
+ */
72
+ /**
73
+ * 路径匹配。
74
+ * - 字符串: 默认进行 Glob 模式匹配(支持 `!` 否定),若非 Glob 且非正则字符串则退化为前缀匹配。
75
+ * - 正则表达式: 检查 `url.pathname` 是否匹配。
76
+ * - 数组: 支持传入多个模式(含否定模式),只要其中一个匹配即可。
77
+ */
78
+ path?: string | RegExp | (string | RegExp)[];
79
+ /**
80
+ * Query 参数匹配规则。
81
+ * - 键名: 支持字符串、Glob 或正则。
82
+ * - 值:
83
+ * - 字符串: 支持 Glob 模式匹配。
84
+ * - 正则表达式: 检查参数值是否匹配。
85
+ * - `true`: 要求该参数必须存在于 URL 中。
86
+ * - `false`: 要求该参数必须 **不** 存在于 URL 中。
87
+ */
88
+ query?: Record<string, string | boolean | RegExp>;
89
+ /**
90
+ * 强制指定 Body 类型。
91
+ * 如果不指定,则根据 `Content-Type` 自动判断。
92
+ */
93
+ bodyType?: 'json' | 'text' | 'binary';
94
+ /**
95
+ * Body 内容匹配。
96
+ * 仅当 Body 为文本或 JSON 时有效。
97
+ * - 字符串: 支持 Glob 模式匹配。
98
+ * - 正则表达式: 检查 Body 内容是否匹配。
99
+ * - 数组: 支持传入多个模式。
100
+ */
101
+ body?: string | RegExp | (string | RegExp)[];
13
102
  }
14
103
  /**
15
104
  * 站点级缓存配置
16
105
  */
17
106
  interface SiteCacheConfig {
18
- /** Query 参数过滤配置 */
107
+ /**
108
+ * 允许缓存的 HTTP 方法列表。
109
+ * 默认值: ['GET', 'HEAD']。
110
+ * 若要缓存 POST/PUT,必须在此显式添加,并确保后端响应满足缓存条件(或开启 `forceCache`)。
111
+ */
112
+ methods?: string[];
113
+ /**
114
+ * 精细化缓存规则列表。
115
+ * 如果配置了此项,请求必须匹配其中至少一条规则才会被允许进入缓存流程。
116
+ * 适用于只希望缓存特定 API 接口的场景。
117
+ */
118
+ cacheRules?: CacheRule[];
119
+ /** Query 参数过滤配置:决定哪些查询参数参与缓存指纹 (Cache Key) 的计算 */
19
120
  query?: KeyFilterConfig;
20
- /** 请求头过滤配置 */
121
+ /** 请求头过滤配置:决定哪些 Header 参与缓存指纹计算 */
21
122
  headers?: KeyFilterConfig;
22
- /** Cookie 过滤配置 */
123
+ /** Cookie 过滤配置:决定哪些 Cookie 字段参与缓存指纹计算 */
23
124
  cookies?: KeyFilterConfig;
24
- /** 当后端请求失败且存在旧缓存时,是否强制返回旧缓存(容错机制) */
125
+ /**
126
+ * 请求体过滤配置 (仅限 JSON 类型)。
127
+ * 当方法为 POST/PUT/PATCH 且为 JSON 格式时,用于从 Body 中提取特定字段参与指纹计算。
128
+ */
129
+ body?: BodyFilterConfig;
130
+ /** 容错机制:当后端请求失败(网络错误或 5xx)且存在旧缓存时,是否强制返回旧缓存 */
25
131
  staleIfError?: boolean;
26
- /** 是否强制缓存一切响应(无视 no-store 等不缓存指令),用于极端的离线可用容错场景 */
132
+ /** 强制缓存:是否忽略 `Cache-Control: no-store` 等指令强制入库。 */
27
133
  forceCache?: boolean;
134
+ /** 严格离线模式:不发起任何网络请求,只读缓存。缓存未命中时抛出 OfflineCacheMissError */
135
+ offline?: boolean;
28
136
  }
29
137
  /**
30
138
  * 缓存元数据
@@ -75,11 +183,28 @@ interface SmartCacheOptions {
75
183
  storagePath?: string;
76
184
  /** 内存缓存阈值(字节)。响应体大小超过此值时,Body 将只存入磁盘,而 Meta 仍保留在内存。默认 1MB。 */
77
185
  maxMemorySize?: number;
78
- /** 透传给 L1 (Memory) 的高级配置 */
79
- memoryOptions?: Partial<KeyvCacheableMemoryOptions>;
186
+ /** 内存缓存总大小阈值(字节)。默认 100MB。超过此值将清空内存缓存。 */
187
+ maxTotalMemorySize?: number;
188
+ /** 透传给 L1 (Memory) 的高级配置 (secondary-cache LRUCache options) */
189
+ memoryOptions?: {
190
+ capacity?: number;
191
+ expires?: number;
192
+ cleanInterval?: number;
193
+ [key: string]: any;
194
+ };
80
195
  }
81
196
  /**
82
- * 智能混合缓存类 (Hybrid Cache)
197
+ * 智能混合缓存类 (Hybrid Multi-tier Cache)
198
+ *
199
+ * 该类实现了 L1 (内存) 和 L2 (磁盘) 的双层混合存储架构,旨在提供高性能且大容量的缓存能力。
200
+ *
201
+ * ### 核心特性:
202
+ * - **双层架构**: L1 使用 LRU 内存缓存(基于 `secondary-cache` 的 LRUCache),L2 使用持久化磁盘缓存(基于 `cacache`)。
203
+ * - **大小感知存储**: 自动识别响应体大小。小于阈值的文件同时存于内存和磁盘;超过阈值的文件仅存于磁盘,但其元数据仍保留在内存中。
204
+ * - **元数据驻留 (Meta-Residency)**: 无论 Body 多大,Headers、Status、Policy 等信息始终优先从内存读取,确保缓存判定性能。
205
+ * - **流式支持**: 支持通过 `setStream` 和 `getStream` 直接操作大数据流,防止 OOM。
206
+ * - **一致性保障**: 在并发写入时自动清理内存,确保后续读取不会拿到被污染的旧数据。
207
+ * - **内存限制**: 通过 `maxTotalMemorySize` 控制 L1 缓存的总内存占用。
83
208
  */
84
209
  declare class SmartCache {
85
210
  private memory;
@@ -88,27 +213,97 @@ declare class SmartCache {
88
213
  constructor(options?: SmartCacheOptions);
89
214
  /**
90
215
  * 获取缓存条目
91
- * 如果是小文件,返回带 Buffer 的 Entry;如果是大文件,返回带 ReadStream 的 Entry。
216
+ *
217
+ * 逻辑:
218
+ * 1. 首先尝试从 L1 内存获取。
219
+ * 2. 如果内存中有 Body,直接返回(Buffer 类型)。
220
+ * 3. 如果内存中只有 Meta(大文件),则从 L2 磁盘创建并返回 ReadStream。
221
+ * 4. 如果内存完全未命中,从磁盘 L2 检索,并根据大小决定是否回填 L1。
222
+ *
223
+ * @param key - 缓存指纹键
224
+ * @returns 完整的缓存条目(带 Buffer 或 Stream 的 Body),未命中返回 null
92
225
  */
93
226
  get(key: string): Promise<CacheEntry | null>;
94
227
  /**
95
- * 写入缓存
228
+ * 写入缓存条目 (原子写入)
229
+ *
230
+ * 适用于已知长度的小型数据块。该操作会同时写入磁盘并回填内存(如果大小未超标)。
231
+ *
232
+ * @param key - 缓存指纹键
233
+ * @param body - 响应体数据 Buffer
234
+ * @param metadata - 响应元数据(不含 size,由本方法自动计算)
96
235
  */
97
236
  set(key: string, body: Buffer, metadata: Omit<CacheMetadata, 'size'>): Promise<void>;
98
237
  /**
99
238
  * 内部方法:处理内存回填
100
239
  */
101
240
  private saveToMemory;
241
+ /**
242
+ * 获取磁盘读取流
243
+ *
244
+ * 允许直接从 L2 磁盘层以流的形式读取数据,适用于大文件代理。
245
+ *
246
+ * @param key - 缓存指纹键
247
+ * @returns Node.js 可读流
248
+ */
102
249
  getStream(key: string): NodeJS.ReadableStream;
250
+ /**
251
+ * 获取磁盘写入流 (流式缓存)
252
+ *
253
+ * 该方法用于支持真正的流式代理。它会执行以下一致性操作:
254
+ * 1. 立即清除 L1 内存中的对应键,防止读到旧数据。
255
+ * 2. 返回一个可写流,数据将直接流入磁盘。
256
+ * 3. **一致性修复**: 在流写入完成(finish)时再次清理内存,防止写入期间的并发读取将旧数据再次回填进内存。
257
+ *
258
+ * @param key - 缓存指纹键
259
+ * @param metadata - 响应元数据
260
+ * @returns Node.js 可写流
261
+ */
103
262
  setStream(key: string, metadata: Omit<CacheMetadata, 'size'>): NodeJS.WritableStream;
104
- delete(key: string): Promise<void>;
105
- clear(): Promise<void>;
263
+ /**
264
+ * Deletes the cache entry for the specified key.
265
+ * @param key - The cache key to delete
266
+ * @param [clearPersistent=true] - Whether to also delete the entry from persistent (disk) storage. Defaults to `true`.
267
+ */
268
+ delete(key: string, clearPersistent?: boolean): Promise<void>;
269
+ /**
270
+ * Clears the cache. By default, both the in-memory cache and the persistent disk cache are cleared.
271
+ * @param [clearPersistent=true] - Whether to clear the persistent (disk) cache. Defaults to `true` for backward compatibility.
272
+ */
273
+ clear(clearPersistent?: boolean): Promise<void>;
106
274
  }
107
275
 
108
276
  /**
109
- * 根据 Request 和配置生成唯一的缓存键
277
+ * 根据 Request 对象和站点配置生成唯一的缓存指纹 (异步)
278
+ *
279
+ * 该函数是缓存系统的核心组件,用于将复杂的 HTTP 请求对象转换为唯一的 SHA-256 字符串。
280
+ * 它实现了高度可定制的提取逻辑,允许通过配置排除掉请求中不稳定的因素(如时间戳、Nonce 等)。
281
+ *
282
+ * ### 生成指纹包含的要素:
283
+ * 1. **Method**: 请求方法(统一转为大写)。
284
+ * 2. **Host & Path**: 请求的域名和路径。
285
+ * 3. **Query Params**: URL 查询参数,受 `config.query` 过滤影响。
286
+ * 4. **Headers**: 请求头信息,受 `config.headers` 过滤影响。默认排除 `cookie` 头。
287
+ * 5. **Cookies**: 特别提取的 Cookie 字段,受 `config.cookies` 过滤影响。
288
+ * 6. **Request Body**:
289
+ * - 对于 `POST`, `PUT`, `PATCH` 请求,会自动尝试读取 Body。
290
+ * - **JSON 类型**: 如果 `Content-Type` 包含 `application/json`,则解析为对象并应用 `config.body` 过滤。
291
+ * - **非 JSON/流类型**: 回退到对原始 Body 字节流进行 SHA-256 哈希计算。
292
+ * - **安全性**: 使用 `req.clone()` 读取 Body,确保不影响后续真实的 Fetch 请求流消费。
293
+ *
294
+ * @param req - 原始 Web 标准 Request 对象。
295
+ * @param config - 站点级缓存配置,决定了哪些字段参与指纹计算。
296
+ * @returns 返回一个 64 位十六进制的 SHA-256 哈希字符串作为缓存键。
297
+ *
298
+ * @example
299
+ * ```typescript
300
+ * const cacheKey = await generateCacheKey(request, {
301
+ * query: { exclude: ['timestamp'] },
302
+ * body: { include: ['id', 'action'] }
303
+ * });
304
+ * ```
110
305
  */
111
- declare const generateCacheKey: (req: Request, config: SiteCacheConfig) => string;
306
+ declare function generateCacheKey(req: Request, config: SiteCacheConfig): Promise<string>;
112
307
 
113
308
  /**
114
309
  * fetchWithCache 选项
@@ -126,12 +321,10 @@ interface FetchWithCacheOptions {
126
321
  generateKey?: typeof generateCacheKey;
127
322
  /**
128
323
  * 并发写入任务追踪器
129
- * 传入一个外部维护的 Map,用于在跨请求、跨实例时防止针对同一文件的并发重复下载。
130
- * Map 的 Key 是缓存 Key,Value 是一个代表写入完成的 Promise。
131
324
  */
132
325
  activeCacheWrites?: Map<string, Promise<void>>;
133
326
  }
134
- /** 内部流水线上下文,合并了入参和计算出的关键状态 */
327
+ /** 内部流水线上下文 */
135
328
  interface FetchWithCacheContext extends FetchWithCacheOptions {
136
329
  request: Request;
137
330
  fetcher: (req: Request) => Promise<Response>;
@@ -139,15 +332,21 @@ interface FetchWithCacheContext extends FetchWithCacheOptions {
139
332
  activeCacheWrites: Map<string, Promise<void>>;
140
333
  }
141
334
  /**
142
- * 核心协调函数 (Fetcher Orchestrator)
335
+ * 核心协调函数:协调请求、缓存命中、并发控制和 SWR
143
336
  *
144
- * 实现了基于流的混合缓存代理核心逻辑,主要机制包括:
145
- * - **大文件流式处理**:底层完全通过 Streams 实现,代理大文件时自动写入磁盘且防 OOM。
146
- * - **SWR (Stale-While-Revalidate)**:后台静默更新机制。
147
- * - **并发防击穿 (Request Coalescing)**:利用 `activeCacheWrites` 将并发请求合并。
148
- * - **强制离线容灾**:支持 `staleIfError` `forceCache`(无视 Cache-Control 强制入库)。
337
+ * 流程如下:
338
+ * 1. 初始化上下文并生成缓存键。
339
+ * 2. 检查离线模式:若开启则强读取,未命中直接抛错。
340
+ * 3. 检查请求是否符合缓存规则 (isCacheable)
341
+ * 4. 尝试读取缓存并判定状态 (HIT / STALE)。
342
+ * 5. 处理 SWR (后台更新)。
343
+ * 6. 处理请求合并 (Request Coalescing),防止缓存击穿。
344
+ * 7. 若缓存缺失,发起网络请求并流式写入。
149
345
  *
150
- * 并且会在响应头中自动注入 `x-proxy-cache` 标明缓存命中状态 (`HIT`, `STALE`, `MISS`, `STALE_IF_ERROR`)。
346
+ * @param request - 标准 Web Request 对象
347
+ * @param fetcher - 底层发起真实请求的函数
348
+ * @param options - 缓存协调配置项
349
+ * @returns 标准 Web Response 对象 (带 x-proxy-cache 标头)
151
350
  */
152
351
  declare function fetchWithCache(request: Request, fetcher: (req: Request) => Promise<Response>, options: FetchWithCacheOptions): Promise<Response>;
153
352
 
@@ -181,33 +380,167 @@ declare function createFetchWithCache(activeCacheWrites?: Map<string, Promise<vo
181
380
  */
182
381
  declare function createCachedFetch(defaultOptions: FetchWithCacheOptions): (request: Request, fetcher: (req: Request) => Promise<Response>, overrideOptions?: Partial<FetchWithCacheOptions>) => Promise<Response>;
183
382
 
383
+ /**
384
+ * 预缓存请求选项
385
+ */
386
+ interface PrefetchRequest {
387
+ /** 请求 URL */
388
+ url: string;
389
+ /** 可选的请求配置(method, headers, body 等) */
390
+ request?: RequestInit;
391
+ }
392
+ interface PrefetchOptions {
393
+ /** 要预缓存的 URL 列表及其请求选项 */
394
+ urls: PrefetchRequest[];
395
+ /** 完整的代理配置 */
396
+ config: ProxyConfig;
397
+ /** SmartCache 实例 */
398
+ cache: SmartCache;
399
+ /** 自定义 fetcher,默认使用 globalThis.fetch */
400
+ fetcher?: (req: Request) => Promise<Response>;
401
+ /** 并发数,默认 3 */
402
+ concurrency?: number;
403
+ /** 进度回调 (completed, total, url) */
404
+ onProgress?: (completed: number, total: number, url: string) => void;
405
+ /** 取消信号 */
406
+ signal?: AbortSignal;
407
+ }
408
+ interface PrefetchResult {
409
+ /** 成功数量 */
410
+ succeeded: number;
411
+ /** 失败数量 */
412
+ failed: number;
413
+ /** 失败详情 */
414
+ errors?: Array<{
415
+ url: string;
416
+ error: Error;
417
+ }>;
418
+ }
419
+ /**
420
+ * 预缓存函数
421
+ *
422
+ * 提前将指定的 URL 列表内容存入缓存,支持并发控制和进度回调。
423
+ * 复用了 `createCachedFetch` 的完整逻辑,自动支持:
424
+ * - GET/POST/PUT/PATCH/DELETE 等所有方法
425
+ * - POST body 过滤和缓存键生成
426
+ * - 站点级配置
427
+ *
428
+ * @param options - 预缓存选项
429
+ * @returns 预缓存结果,包含成功/失败数量和错误详情
430
+ */
431
+ declare function prefetch(options: PrefetchOptions): Promise<PrefetchResult>;
432
+
433
+ /**
434
+ * 判断当前请求是否满足可缓存的基础条件
435
+ */
436
+ declare function isCacheable(request: Request, config: SiteCacheConfig): Promise<boolean>;
437
+
184
438
  /**
185
439
  * 从源对象中根据过滤配置提取数据并标准化。
186
440
  *
187
441
  * 此函数主要用于生成缓存指纹。它会:
188
- * 1. 根据 `config` (include/exclude) 过滤键。
442
+ * 1. 根据 `config` (include/exclude) 过滤键,调用 `isAllowed` 判断每个键是否允许。
189
443
  * 2. 对键进行排序以保证指纹的一致性。
190
444
  * 3. 将所有键转换为小写。
191
445
  * 4. 将值统一包装为数组并进行排序,消除数组项顺序差异。
192
446
  *
447
+ * **关于 `defaultAllowed` 参数**:
448
+ * - 只有当没有配置 `include` 和 `exclude` 时,`defaultAllowed` 才会生效。
449
+ * - 如果配置了 `include`(即使为空数组),`defaultAllowed` 也不会生效。
450
+ * - 详见 `isAllowed` 函数的优先级逻辑。
451
+ *
193
452
  * @param source 原始数据对象 (如 QueryParams, Headers, Cookies)
194
- * @param config 过滤配置 (白名单或黑名单)
195
- * @returns 标准化后的数据 Map,键为小写,值为字符串数组
453
+ * @param config 过滤配置,支持 `include`(白名单)和 `exclude`(黑名单)
454
+ * @param defaultAllowed 当没有配置时的默认值(默认 `false`,即不提取任何键)
455
+ * @returns 标准化后的数据 Map,键为小写,值为排序后的字符串数组
456
+ *
457
+ * @example
458
+ * ```typescript
459
+ * const headers = { 'Content-Type': 'application/json', 'X-Request-Id': '123' };
460
+ *
461
+ * // 默认不提取任何键
462
+ * extractData(headers); // {}
463
+ *
464
+ * // 提取所有键
465
+ * extractData(headers, undefined, true); // { 'content-type': ['application/json'], 'x-request-id': ['123'] }
466
+ *
467
+ * // 白名单
468
+ * extractData(headers, { include: ['content-type'] }); // { 'content-type': ['application/json'] }
469
+ *
470
+ * // 黑名单(需要 include 或 defaultAllowed)
471
+ * extractData(headers, { include: ['*'], exclude: ['x-request-id'] }, true); // { 'content-type': ['application/json'] }
472
+ * ```
196
473
  */
197
- declare const extractData: (source: Record<string, any>, config?: KeyFilterConfig) => Record<string, string[]>;
474
+ declare const extractData: (source: Record<string, any>, config?: KeyFilterConfig, defaultAllowed?: boolean) => Record<string, string[]>;
198
475
 
199
476
  /**
200
477
  * 判断给定的键是否允许参与缓存指纹计算。
201
478
  *
202
- * 优先级逻辑:
203
- * 1. 如果配置了 `include` (白名单),则只有存在于 `include` 中的键才会被允许。
204
- * 2. 否则,如果配置了 `exclude` (黑名单),则存在于 `exclude` 中的键将被拒绝。
205
- * 3. 如果都没有配置,默认允许所有键。
479
+ * **优先级逻辑**:
480
+ * 1. `exclude` 命中 → 返回 `false`(优先级最高,会覆盖前面的结果)
481
+ * 2. `include` 存在且命中 → 返回 `true`
482
+ * 3. `include` 存在但不命中 → 返回 `false`
483
+ * 4. 都没有配置 → 使用 `defaultAllowed`(未传则返回 `undefined`)
484
+ *
485
+ * **注意**:`include` 和 `exclude` 可以同时配置,此时 `exclude` 优先级更高。
206
486
  *
207
487
  * @param key 要检查的键名
208
- * @param config 过滤配置
209
- * @returns 是否允许
488
+ * @param config 过滤配置,支持 `include`(白名单)和 `exclude`(黑名单)
489
+ * @param defaultAllowed 当没有配置或配置未命中时的默认值(可选)
490
+ * @returns 是否允许。返回 `boolean` 或 `undefined`(当没有配置且未传 defaultAllowed 时)
491
+ *
492
+ * @example
493
+ * ```typescript
494
+ * // 无配置
495
+ * isAllowed('key'); // undefined
496
+ *
497
+ * // 白名单
498
+ * isAllowed('id', { include: ['id', 'name'] }); // true
499
+ * isAllowed('email', { include: ['id', 'name'] }); // false
500
+ *
501
+ * // 黑名单
502
+ * isAllowed('password', { exclude: ['password'] }); // false
503
+ * isAllowed('name', { exclude: ['password'] }); // undefined
504
+ *
505
+ * // 设置默认值
506
+ * isAllowed('name', { exclude: ['password'] }, true); // true
507
+ * ```
508
+ */
509
+ declare function isAllowed(key: string, config?: KeyFilterConfig, defaultAllowed?: boolean): boolean;
510
+
511
+ /**
512
+ * 判断一个模式是否为 Glob 模式
513
+ */
514
+ declare function isGlob(str: string): boolean;
515
+ /**
516
+ * 通用匹配函数
517
+ *
518
+ * 逻辑优先级:
519
+ * 1. 如果 pattern 是数组,遵循:(匹配任一正向模式) 且 (不匹配任一负向模式)。
520
+ * 2. 如果 pattern 是 RegExp 对象,直接使用 regex.test(value)。
521
+ * 3. 如果 pattern 是 "/regex/flags" 格式的字符串,转为 RegExp 后使用 test。
522
+ * 4. 如果 pattern 是 Glob 字符串,使用 picomatch 进行匹配。
523
+ * 5. 否则,根据 usePrefix 参数进行前缀匹配或精确匹配。
524
+ *
525
+ * @param pattern 匹配模式 (RegExp 或 字符串 或 数组)
526
+ * @param value 要匹配的值
527
+ * @param usePrefix 是否在普通字符串匹配时启用前缀匹配 (默认为 false,即精确匹配)
528
+ */
529
+ declare function isMatch(pattern: string | RegExp | (string | RegExp)[], value: string, usePrefix?: boolean): boolean;
530
+
531
+ /**
532
+ * 根据 URL 获取对应的站点缓存配置
533
+ *
534
+ * 匹配逻辑:
535
+ * 1. 遍历 sites 中的所有 key。
536
+ * 2. 如果 key 是正则或 Glob 格式字符串,则对完整 URL 进行匹配。
537
+ * 3. 如果 key 是普通字符串,则作为 URL 前缀进行匹配。
538
+ * 4. 返回第一个匹配到的配置;若均未匹配,则返回 defaultConfig。
539
+ *
540
+ * @param urlString 请求的完整 URL
541
+ * @param proxyConfig 全局代理配置
542
+ * @returns 匹配到的站点配置
210
543
  */
211
- declare function isAllowed(key: string, config?: KeyFilterConfig): boolean;
544
+ declare function getSiteConfig(urlString: string, proxyConfig: ProxyConfig): SiteCacheConfig;
212
545
 
213
- export { type CacheEntry, type CacheMetadata, type FetchWithCacheContext, type FetchWithCacheOptions, type KeyFilterConfig, type ProxyConfig, type SiteCacheConfig, SmartCache, type SmartCacheOptions, createCachedFetch, createFetchWithCache, extractData, fetchWithCache, generateCacheKey, isAllowed };
546
+ export { type BodyFilterConfig, type CacheEntry, type CacheMetadata, type CacheRule, type FetchWithCacheContext, type FetchWithCacheOptions, type KeyFilterConfig, OfflineCacheMissError, OfflineCacheMissErrorCode, type PrefetchOptions, type PrefetchRequest, type PrefetchResult, type ProxyConfig, type SiteCacheConfig, SmartCache, type SmartCacheOptions, createCachedFetch, createFetchWithCache, extractData, fetchWithCache, generateCacheKey, getSiteConfig, isAllowed, isCacheable, isGlob, isMatch, prefetch };