@isdk/proxy 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,7 @@
6
6
 
7
7
  # Interface: FetchWithCacheOptions
8
8
 
9
- Defined in: [core/fetchWithCache.ts:11](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/core/fetchWithCache.ts#L11)
9
+ Defined in: [core/fetchWithCache.ts:12](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/core/fetchWithCache.ts#L12)
10
10
 
11
11
  fetchWithCache 选项
12
12
 
@@ -20,7 +20,7 @@ fetchWithCache 选项
20
20
 
21
21
  > `optional` **activeCacheWrites**: `Map`\<`string`, `Promise`\<`void`\>\>
22
22
 
23
- Defined in: [core/fetchWithCache.ts:27](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/core/fetchWithCache.ts#L27)
23
+ Defined in: [core/fetchWithCache.ts:28](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/core/fetchWithCache.ts#L28)
24
24
 
25
25
  并发写入任务追踪器
26
26
  传入一个外部维护的 Map,用于在跨请求、跨实例时防止针对同一文件的并发重复下载。
@@ -32,7 +32,7 @@ Map 的 Key 是缓存 Key,Value 是一个代表写入完成的 Promise。
32
32
 
33
33
  > `optional` **backgroundUpdate**: `boolean`
34
34
 
35
- Defined in: [core/fetchWithCache.ts:17](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/core/fetchWithCache.ts#L17)
35
+ Defined in: [core/fetchWithCache.ts:18](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/core/fetchWithCache.ts#L18)
36
36
 
37
37
  是否启用后台异步更新 (SWR)
38
38
 
@@ -42,7 +42,7 @@ Defined in: [core/fetchWithCache.ts:17](https://github.com/isdk/proxy.js/blob/be
42
42
 
43
43
  > **cache**: [`SmartCache`](../classes/SmartCache.md)
44
44
 
45
- Defined in: [core/fetchWithCache.ts:13](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/core/fetchWithCache.ts#L13)
45
+ Defined in: [core/fetchWithCache.ts:14](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/core/fetchWithCache.ts#L14)
46
46
 
47
47
  混合缓存实例
48
48
 
@@ -52,7 +52,7 @@ Defined in: [core/fetchWithCache.ts:13](https://github.com/isdk/proxy.js/blob/be
52
52
 
53
53
  > **config**: [`SiteCacheConfig`](SiteCacheConfig.md)
54
54
 
55
- Defined in: [core/fetchWithCache.ts:15](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/core/fetchWithCache.ts#L15)
55
+ Defined in: [core/fetchWithCache.ts:16](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/core/fetchWithCache.ts#L16)
56
56
 
57
57
  站点级缓存配置
58
58
 
@@ -60,13 +60,28 @@ Defined in: [core/fetchWithCache.ts:15](https://github.com/isdk/proxy.js/blob/be
60
60
 
61
61
  ### generateKey()?
62
62
 
63
- > `optional` **generateKey**: (`req`, `config`) => `string`
63
+ > `optional` **generateKey**: (`req`, `config`) => `Promise`\<`string`\>
64
64
 
65
- Defined in: [core/fetchWithCache.ts:21](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/core/fetchWithCache.ts#L21)
65
+ Defined in: [core/fetchWithCache.ts:22](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/core/fetchWithCache.ts#L22)
66
66
 
67
67
  自定义缓存键生成函数
68
68
 
69
- 根据 Request 和配置生成唯一的缓存键
69
+ 根据 Request 对象和站点配置生成唯一的缓存指纹 (异步)
70
+
71
+ 该函数是缓存系统的核心组件,用于将复杂的 HTTP 请求对象转换为唯一的 SHA-256 字符串。
72
+ 它实现了高度可定制的提取逻辑,允许通过配置排除掉请求中不稳定的因素(如时间戳、Nonce 等)。
73
+
74
+ ### 生成指纹包含的要素:
75
+ 1. **Method**: 请求方法(统一转为大写)。
76
+ 2. **Host & Path**: 请求的域名和路径。
77
+ 3. **Query Params**: URL 查询参数,受 `config.query` 过滤影响。
78
+ 4. **Headers**: 请求头信息,受 `config.headers` 过滤影响。默认排除 `cookie` 头。
79
+ 5. **Cookies**: 特别提取的 Cookie 字段,受 `config.cookies` 过滤影响。
80
+ 6. **Request Body**:
81
+ - 对于 `POST`, `PUT`, `PATCH` 请求,会自动尝试读取 Body。
82
+ - **JSON 类型**: 如果 `Content-Type` 包含 `application/json`,则解析为对象并应用 `config.body` 过滤。
83
+ - **非 JSON/流类型**: 回退到对原始 Body 字节流进行 SHA-256 哈希计算。
84
+ - **安全性**: 使用 `req.clone()` 读取 Body,确保不影响后续真实的 Fetch 请求流消费。
70
85
 
71
86
  #### Parameters
72
87
 
@@ -74,13 +89,28 @@ Defined in: [core/fetchWithCache.ts:21](https://github.com/isdk/proxy.js/blob/be
74
89
 
75
90
  `Request`
76
91
 
92
+ 原始 Web 标准 Request 对象。
93
+
77
94
  ##### config
78
95
 
79
96
  [`SiteCacheConfig`](SiteCacheConfig.md)
80
97
 
98
+ 站点级缓存配置,决定了哪些字段参与指纹计算。
99
+
81
100
  #### Returns
82
101
 
83
- `string`
102
+ `Promise`\<`string`\>
103
+
104
+ 返回一个 64 位十六进制的 SHA-256 哈希字符串作为缓存键。
105
+
106
+ #### Example
107
+
108
+ ```typescript
109
+ const cacheKey = await generateCacheKey(request, {
110
+ query: { exclude: ['timestamp'] },
111
+ body: { include: ['id', 'action'] }
112
+ });
113
+ ```
84
114
 
85
115
  ***
86
116
 
@@ -88,7 +118,7 @@ Defined in: [core/fetchWithCache.ts:21](https://github.com/isdk/proxy.js/blob/be
88
118
 
89
119
  > `optional` **onBackgroundUpdate**: (`promise`) => `void`
90
120
 
91
- Defined in: [core/fetchWithCache.ts:19](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/core/fetchWithCache.ts#L19)
121
+ Defined in: [core/fetchWithCache.ts:20](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/core/fetchWithCache.ts#L20)
92
122
 
93
123
  后台更新 Promise 触发时的回调
94
124
 
@@ -6,28 +6,32 @@
6
6
 
7
7
  # Interface: KeyFilterConfig
8
8
 
9
- Defined in: [types.ts:6](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/types.ts#L6)
9
+ Defined in: [types.ts:6](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L6)
10
10
 
11
11
  缓存键过滤配置
12
12
 
13
13
  用于定义在生成缓存指纹时,哪些字段应该被包含或排除。
14
14
 
15
+ ## Extended by
16
+
17
+ - [`BodyFilterConfig`](BodyFilterConfig.md)
18
+
15
19
  ## Properties
16
20
 
17
21
  ### exclude?
18
22
 
19
- > `optional` **exclude**: `string`[]
23
+ > `optional` **exclude**: (`string` \| `RegExp`)[]
20
24
 
21
- Defined in: [types.ts:10](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/types.ts#L10)
25
+ Defined in: [types.ts:10](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L10)
22
26
 
23
- 排除(黑名单):用于排除像 `timestamp`、`nonce` 等干扰缓存命中的动态字段
27
+ 排除(黑名单):用于排除像 `timestamp`、`nonce` 等干扰缓存命中的动态字段。支持字符串、Glob 模式或正则表达式。
24
28
 
25
29
  ***
26
30
 
27
31
  ### include?
28
32
 
29
- > `optional` **include**: `string`[]
33
+ > `optional` **include**: (`string` \| `RegExp`)[]
30
34
 
31
- Defined in: [types.ts:8](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/types.ts#L8)
35
+ Defined in: [types.ts:8](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L8)
32
36
 
33
- 仅包含(白名单):如果设置,只有这些字段会参与 Key 的计算
37
+ 仅包含(白名单):如果设置,只有这些字段会参与 Key 的计算。支持字符串、Glob 模式或正则表达式。
@@ -6,7 +6,7 @@
6
6
 
7
7
  # Interface: ProxyConfig
8
8
 
9
- Defined in: [types.ts:63](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/types.ts#L63)
9
+ Defined in: [types.ts:145](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L145)
10
10
 
11
11
  代理拦截器全局配置
12
12
 
@@ -16,7 +16,7 @@ Defined in: [types.ts:63](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe
16
16
 
17
17
  > **default**: [`SiteCacheConfig`](SiteCacheConfig.md)
18
18
 
19
- Defined in: [types.ts:65](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/types.ts#L65)
19
+ Defined in: [types.ts:147](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L147)
20
20
 
21
21
  默认缓存配置,当请求的域名未在 sites 中匹配时使用
22
22
 
@@ -26,7 +26,7 @@ Defined in: [types.ts:65](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe
26
26
 
27
27
  > **sites**: `Record`\<`string`, [`SiteCacheConfig`](SiteCacheConfig.md)\>
28
28
 
29
- Defined in: [types.ts:67](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/types.ts#L67)
29
+ Defined in: [types.ts:149](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L149)
30
30
 
31
31
  针对特定域名的精细化缓存配置
32
32
 
@@ -36,6 +36,6 @@ Defined in: [types.ts:67](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe
36
36
 
37
37
  > `optional` **storagePath**: `string`
38
38
 
39
- Defined in: [types.ts:69](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/types.ts#L69)
39
+ Defined in: [types.ts:151](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L151)
40
40
 
41
41
  磁盘缓存(cacache)的物理存储路径,可选,默认为系统临时目录
@@ -6,19 +6,42 @@
6
6
 
7
7
  # Interface: SiteCacheConfig
8
8
 
9
- Defined in: [types.ts:16](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/types.ts#L16)
9
+ Defined in: [types.ts:81](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L81)
10
10
 
11
11
  站点级缓存配置
12
12
 
13
13
  ## Properties
14
14
 
15
+ ### body?
16
+
17
+ > `optional` **body**: [`BodyFilterConfig`](BodyFilterConfig.md)
18
+
19
+ Defined in: [types.ts:104](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L104)
20
+
21
+ 请求体过滤配置 (仅限 JSON 类型)。
22
+ 当方法为 POST/PUT/PATCH 且为 JSON 格式时,用于从 Body 中提取特定字段参与指纹计算。
23
+
24
+ ***
25
+
26
+ ### cacheRules?
27
+
28
+ > `optional` **cacheRules**: [`CacheRule`](CacheRule.md)[]
29
+
30
+ Defined in: [types.ts:93](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L93)
31
+
32
+ 精细化缓存规则列表。
33
+ 如果配置了此项,请求必须匹配其中至少一条规则才会被允许进入缓存流程。
34
+ 适用于只希望缓存特定 API 接口的场景。
35
+
36
+ ***
37
+
15
38
  ### cookies?
16
39
 
17
40
  > `optional` **cookies**: [`KeyFilterConfig`](KeyFilterConfig.md)
18
41
 
19
- Defined in: [types.ts:22](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/types.ts#L22)
42
+ Defined in: [types.ts:99](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L99)
20
43
 
21
- Cookie 过滤配置
44
+ Cookie 过滤配置:决定哪些 Cookie 字段参与缓存指纹计算
22
45
 
23
46
  ***
24
47
 
@@ -26,9 +49,9 @@ Cookie 过滤配置
26
49
 
27
50
  > `optional` **forceCache**: `boolean`
28
51
 
29
- Defined in: [types.ts:26](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/types.ts#L26)
52
+ Defined in: [types.ts:108](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L108)
30
53
 
31
- 是否强制缓存一切响应(无视 no-store 等不缓存指令),用于极端的离线可用容错场景
54
+ 强制缓存:是否忽略 `Cache-Control: no-store` 等指令强制入库。
32
55
 
33
56
  ***
34
57
 
@@ -36,9 +59,21 @@ Defined in: [types.ts:26](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe
36
59
 
37
60
  > `optional` **headers**: [`KeyFilterConfig`](KeyFilterConfig.md)
38
61
 
39
- Defined in: [types.ts:20](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/types.ts#L20)
62
+ Defined in: [types.ts:97](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L97)
63
+
64
+ 请求头过滤配置:决定哪些 Header 参与缓存指纹计算
65
+
66
+ ***
67
+
68
+ ### methods?
69
+
70
+ > `optional` **methods**: `string`[]
71
+
72
+ Defined in: [types.ts:87](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L87)
40
73
 
41
- 请求头过滤配置
74
+ 允许缓存的 HTTP 方法列表。
75
+ 默认值: ['GET', 'HEAD']。
76
+ 若要缓存 POST/PUT,必须在此显式添加,并确保后端响应满足缓存条件(或开启 `forceCache`)。
42
77
 
43
78
  ***
44
79
 
@@ -46,9 +81,9 @@ Defined in: [types.ts:20](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe
46
81
 
47
82
  > `optional` **query**: [`KeyFilterConfig`](KeyFilterConfig.md)
48
83
 
49
- Defined in: [types.ts:18](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/types.ts#L18)
84
+ Defined in: [types.ts:95](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L95)
50
85
 
51
- Query 参数过滤配置
86
+ Query 参数过滤配置:决定哪些查询参数参与缓存指纹 (Cache Key) 的计算
52
87
 
53
88
  ***
54
89
 
@@ -56,6 +91,6 @@ Query 参数过滤配置
56
91
 
57
92
  > `optional` **staleIfError**: `boolean`
58
93
 
59
- Defined in: [types.ts:24](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/types.ts#L24)
94
+ Defined in: [types.ts:106](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/types.ts#L106)
60
95
 
61
- 当后端请求失败且存在旧缓存时,是否强制返回旧缓存(容错机制)
96
+ 容错机制:当后端请求失败(网络错误或 5xx)且存在旧缓存时,是否强制返回旧缓存
@@ -6,7 +6,7 @@
6
6
 
7
7
  # Interface: SmartCacheOptions
8
8
 
9
- Defined in: [core/SmartCache.ts:10](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/core/SmartCache.ts#L10)
9
+ Defined in: [core/SmartCache.ts:10](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/core/SmartCache.ts#L10)
10
10
 
11
11
  SmartCache 选项
12
12
 
@@ -16,19 +16,45 @@ SmartCache 选项
16
16
 
17
17
  > `optional` **maxMemorySize**: `number`
18
18
 
19
- Defined in: [core/SmartCache.ts:14](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/core/SmartCache.ts#L14)
19
+ Defined in: [core/SmartCache.ts:14](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/core/SmartCache.ts#L14)
20
20
 
21
21
  内存缓存阈值(字节)。响应体大小超过此值时,Body 将只存入磁盘,而 Meta 仍保留在内存。默认 1MB。
22
22
 
23
23
  ***
24
24
 
25
+ ### maxTotalMemorySize?
26
+
27
+ > `optional` **maxTotalMemorySize**: `number`
28
+
29
+ Defined in: [core/SmartCache.ts:16](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/core/SmartCache.ts#L16)
30
+
31
+ 内存缓存总大小阈值(字节)。默认 100MB。超过此值将清空内存缓存。
32
+
33
+ ***
34
+
25
35
  ### memoryOptions?
26
36
 
27
- > `optional` **memoryOptions**: `Partial`\<`KeyvCacheableMemoryOptions`\>
37
+ > `optional` **memoryOptions**: `object`
38
+
39
+ Defined in: [core/SmartCache.ts:18](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/core/SmartCache.ts#L18)
40
+
41
+ 透传给 L1 (Memory) 的高级配置 (secondary-cache LRUCache options)
42
+
43
+ #### Index Signature
44
+
45
+ \[`key`: `string`\]: `any`
46
+
47
+ #### capacity?
48
+
49
+ > `optional` **capacity**: `number`
50
+
51
+ #### cleanInterval?
52
+
53
+ > `optional` **cleanInterval**: `number`
28
54
 
29
- Defined in: [core/SmartCache.ts:16](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/core/SmartCache.ts#L16)
55
+ #### expires?
30
56
 
31
- 透传给 L1 (Memory) 的高级配置
57
+ > `optional` **expires**: `number`
32
58
 
33
59
  ***
34
60
 
@@ -36,6 +62,6 @@ Defined in: [core/SmartCache.ts:16](https://github.com/isdk/proxy.js/blob/bed37f
36
62
 
37
63
  > `optional` **storagePath**: `string`
38
64
 
39
- Defined in: [core/SmartCache.ts:12](https://github.com/isdk/proxy.js/blob/bed37fa43507dcbe5cdfa453876163571399d761/src/core/SmartCache.ts#L12)
65
+ Defined in: [core/SmartCache.ts:12](https://github.com/isdk/proxy.js/blob/76fee3a101f98e5bf29599fe7ea02ab06479cf70/src/core/SmartCache.ts#L12)
40
66
 
41
67
  磁盘缓存的物理路径。如果不提供,将默认使用系统临时目录。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isdk/proxy",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "A framework-agnostic, high-performance hybrid caching middleware with SWR, request collapsing, and stale-if-error support.",
5
5
  "license": "MIT",
6
6
  "author": "Riceball LEE <snowyu.lee@gmail.com>",
@@ -19,13 +19,14 @@
19
19
  }
20
20
  },
21
21
  "dependencies": {
22
- "@cacheable/memory": "^2.0.8",
23
22
  "@isdk/common-error": "^0.2.0",
24
23
  "cacache": "^20.0.4",
25
24
  "events-ex": "^2.3.1",
26
25
  "http-cache-semantics": "^4.2.0",
27
26
  "lodash-es": "^4.18.1",
28
27
  "nanoid": "^5.1.11",
28
+ "picomatch": "^4.0.4",
29
+ "secondary-cache": "^2.1.0",
29
30
  "util-ex": "^2.5.2"
30
31
  },
31
32
  "devDependencies": {
@@ -40,6 +41,7 @@
40
41
  "@types/jsdom": "^27.0.0",
41
42
  "@types/lodash-es": "^4.17.12",
42
43
  "@types/node": "^20.0.0",
44
+ "@types/picomatch": "^4.0.3",
43
45
  "@typescript-eslint/eslint-plugin": "~8.41.0",
44
46
  "eslint": "^9.34.0",
45
47
  "eslint-config-prettier": "^10.1.8",