@isdk/proxy 0.1.2 → 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.
- package/README.cn.md +59 -1
- package/README.md +59 -1
- package/dist/index.d.mts +113 -22
- package/dist/index.d.ts +113 -22
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/docs/README.md +59 -1
- package/docs/classes/OfflineCacheMissError.md +426 -0
- package/docs/classes/SmartCache.md +30 -10
- package/docs/functions/createCachedFetch.md +1 -1
- package/docs/functions/createFetchWithCache.md +1 -1
- package/docs/functions/extractData.md +1 -1
- package/docs/functions/fetchWithCache.md +14 -15
- package/docs/functions/generateCacheKey.md +1 -1
- package/docs/functions/getSiteConfig.md +1 -1
- package/docs/functions/isAllowed.md +1 -1
- package/docs/functions/isCacheable.md +27 -0
- package/docs/functions/isGlob.md +1 -1
- package/docs/functions/isMatch.md +1 -1
- package/docs/functions/prefetch.md +33 -0
- package/docs/globals.md +10 -0
- package/docs/interfaces/BodyFilterConfig.md +6 -6
- package/docs/interfaces/CacheEntry.md +9 -9
- package/docs/interfaces/CacheMetadata.md +8 -8
- package/docs/interfaces/CacheRule.md +6 -6
- package/docs/interfaces/FetchWithCacheContext.md +11 -13
- package/docs/interfaces/FetchWithCacheOptions.md +7 -9
- package/docs/interfaces/KeyFilterConfig.md +3 -3
- package/docs/interfaces/PrefetchOptions.md +107 -0
- package/docs/interfaces/PrefetchRequest.md +31 -0
- package/docs/interfaces/PrefetchResult.md +47 -0
- package/docs/interfaces/ProxyConfig.md +4 -4
- package/docs/interfaces/SiteCacheConfig.md +19 -9
- package/docs/interfaces/SmartCacheOptions.md +5 -5
- package/docs/variables/OfflineCacheMissErrorCode.md +18 -0
- package/package.json +2 -2
package/docs/README.md
CHANGED
|
@@ -93,17 +93,31 @@ const myPostFetch = createCachedFetch({
|
|
|
93
93
|
| `query` | `KeyFilterConfig` | Filters for URL search parameters (`include`/`exclude`). |
|
|
94
94
|
| `headers` | `KeyFilterConfig` | Filters for request headers. |
|
|
95
95
|
| `cookies` | `KeyFilterConfig` | Filters for cookies. |
|
|
96
|
-
| `body` | `KeyFilterConfig` | Filters for
|
|
96
|
+
| `body` | `KeyFilterConfig` | Filters for request body fields. For JSON, supports field-level filtering; also supports extracting key data via `extract` regex. |
|
|
97
97
|
| `staleIfError`| `boolean` | Serve stale cache on network failure. |
|
|
98
98
|
| `forceCache` | `boolean` | Ignore `no-store` and force caching (useful for offline support). |
|
|
99
|
+
| `offline` | `boolean` | Offline mode. Only reads from cache; throws `OfflineCacheMissError` if no cache exists. |
|
|
99
100
|
|
|
100
101
|
### `CacheRule` Object
|
|
101
102
|
|
|
102
103
|
- `method`: HTTP method to match.
|
|
103
104
|
- `path`: URL pathname matching (supports **RegExp**, **Glob**, **Array**, or **prefix match**).
|
|
104
105
|
- `query`: Key-value pairs. Values can be `string` (exact/Glob match), `true` (must exist), `false` (must not exist), or `RegExp`.
|
|
106
|
+
- `bodyType`: Match body type. Supports `'json'`, `'text'`, `'binary'`.
|
|
105
107
|
- `body`: Body content matching (supports **RegExp**, **Glob**, or **Array**).
|
|
106
108
|
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
### `fetchWithCache` Advanced Options
|
|
112
|
+
|
|
113
|
+
In addition to `SiteCacheConfig`, `fetchWithCache` supports the following control options:
|
|
114
|
+
|
|
115
|
+
| Option | Type | Description |
|
|
116
|
+
| :--- | :--- | :--- |
|
|
117
|
+
| `backgroundUpdate` | `boolean` | Whether to enable background async update (SWR). Default is `true`. |
|
|
118
|
+
| `onBackgroundUpdate`| `function` | Callback that receives the update Promise when a background update is triggered. Useful for task tracking. |
|
|
119
|
+
| `generateKey` | `function` | Custom cache key generation function. |
|
|
120
|
+
|
|
107
121
|
### Pattern Matching
|
|
108
122
|
|
|
109
123
|
`@isdk/proxy` provides powerful pattern matching for all configurable fields:
|
|
@@ -303,6 +317,50 @@ extractData(headers, { include: ['content-type'] }); // { 'content-type': ['appl
|
|
|
303
317
|
extractData(headers, { include: ['*'], exclude: ['x-request-id'] }, true); // { 'content-type': ['application/json'] }
|
|
304
318
|
```
|
|
305
319
|
|
|
320
|
+
### `prefetch(options)`
|
|
321
|
+
|
|
322
|
+
Pre-cache function that fetches and stores a list of URLs into cache ahead of time.
|
|
323
|
+
|
|
324
|
+
- **`urls`**: `PrefetchRequest[]`. Each object contains `url` and optional `request` config.
|
|
325
|
+
- **`config`**: `ProxyConfig` full configuration.
|
|
326
|
+
- **`cache`**: `SmartCache` instance.
|
|
327
|
+
- **`concurrency`**: Concurrency limit (default `3`).
|
|
328
|
+
- **`onProgress`**: Progress callback `(completed, total, url) => void`.
|
|
329
|
+
|
|
330
|
+
```typescript
|
|
331
|
+
import { prefetch } from '@isdk/proxy';
|
|
332
|
+
|
|
333
|
+
const result = await prefetch({
|
|
334
|
+
urls: [
|
|
335
|
+
{ url: 'https://api.example.com/page1' },
|
|
336
|
+
{ url: 'https://api.example.com/api2', request: { method: 'POST', body: '...' } }
|
|
337
|
+
],
|
|
338
|
+
config,
|
|
339
|
+
cache,
|
|
340
|
+
onProgress: (c, t, url) => console.log(`Progress: ${c}/${t} - ${url}`)
|
|
341
|
+
});
|
|
342
|
+
console.log(`Succeeded: ${result.succeeded}, Failed: ${result.failed}`);
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
### Error Handling: `OfflineCacheMissError`
|
|
346
|
+
|
|
347
|
+
When `offline: true` mode is enabled and a request does not hit the cache, this error is thrown.
|
|
348
|
+
|
|
349
|
+
- **`name`**: `OfflineCacheMissError`
|
|
350
|
+
- **`code`**: `ERR_OFFLINE_CACHE_MISS` (can be imported as `OfflineCacheMissErrorCode`)
|
|
351
|
+
|
|
352
|
+
```typescript
|
|
353
|
+
import { OfflineCacheMissError } from '@isdk/proxy';
|
|
354
|
+
|
|
355
|
+
try {
|
|
356
|
+
await myFetch(request);
|
|
357
|
+
} catch (e) {
|
|
358
|
+
if (e instanceof OfflineCacheMissError) {
|
|
359
|
+
// Handle offline cache miss
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
```
|
|
363
|
+
|
|
306
364
|
### Cache Status Headers
|
|
307
365
|
|
|
308
366
|
Every response processed by `@isdk/proxy` will include an `x-proxy-cache` header indicating its lifecycle:
|
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
[**@isdk/proxy**](../README.md)
|
|
2
|
+
|
|
3
|
+
***
|
|
4
|
+
|
|
5
|
+
[@isdk/proxy](../globals.md) / OfflineCacheMissError
|
|
6
|
+
|
|
7
|
+
# Class: OfflineCacheMissError
|
|
8
|
+
|
|
9
|
+
Defined in: [packages/proxy/src/errors.ts:24](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/errors.ts#L24)
|
|
10
|
+
|
|
11
|
+
Offline 缓存未命中错误
|
|
12
|
+
|
|
13
|
+
## Example
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
throw new OfflineCacheMissError('http://example.com/data')
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Extends
|
|
20
|
+
|
|
21
|
+
- `CommonError`
|
|
22
|
+
|
|
23
|
+
## Constructors
|
|
24
|
+
|
|
25
|
+
### Constructor
|
|
26
|
+
|
|
27
|
+
> **new OfflineCacheMissError**(`url`, `name?`): `OfflineCacheMissError`
|
|
28
|
+
|
|
29
|
+
Defined in: [packages/proxy/src/errors.ts:26](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/errors.ts#L26)
|
|
30
|
+
|
|
31
|
+
#### Parameters
|
|
32
|
+
|
|
33
|
+
##### url
|
|
34
|
+
|
|
35
|
+
`string` | `number`
|
|
36
|
+
|
|
37
|
+
##### name?
|
|
38
|
+
|
|
39
|
+
`string` | `Record`\<`string`, `any`\>
|
|
40
|
+
|
|
41
|
+
#### Returns
|
|
42
|
+
|
|
43
|
+
`OfflineCacheMissError`
|
|
44
|
+
|
|
45
|
+
#### Overrides
|
|
46
|
+
|
|
47
|
+
`CommonError.constructor`
|
|
48
|
+
|
|
49
|
+
## Properties
|
|
50
|
+
|
|
51
|
+
### caller
|
|
52
|
+
|
|
53
|
+
> **caller**: `string`
|
|
54
|
+
|
|
55
|
+
Defined in: node\_modules/.pnpm/@isdk+common-error@0.2.2/node\_modules/@isdk/common-error/dist/index.d.ts:141
|
|
56
|
+
|
|
57
|
+
#### Inherited from
|
|
58
|
+
|
|
59
|
+
`CommonError.caller`
|
|
60
|
+
|
|
61
|
+
***
|
|
62
|
+
|
|
63
|
+
### cause?
|
|
64
|
+
|
|
65
|
+
> `optional` **cause**: `unknown`
|
|
66
|
+
|
|
67
|
+
Defined in: node\_modules/.pnpm/typescript@5.7.3/node\_modules/typescript/lib/lib.es2022.error.d.ts:26
|
|
68
|
+
|
|
69
|
+
#### Inherited from
|
|
70
|
+
|
|
71
|
+
`CommonError.cause`
|
|
72
|
+
|
|
73
|
+
***
|
|
74
|
+
|
|
75
|
+
### code
|
|
76
|
+
|
|
77
|
+
> **code**: `ErrorCodeType`
|
|
78
|
+
|
|
79
|
+
Defined in: node\_modules/.pnpm/@isdk+common-error@0.2.2/node\_modules/@isdk/common-error/dist/index.d.ts:142
|
|
80
|
+
|
|
81
|
+
the error code
|
|
82
|
+
|
|
83
|
+
#### Inherited from
|
|
84
|
+
|
|
85
|
+
`CommonError.code`
|
|
86
|
+
|
|
87
|
+
***
|
|
88
|
+
|
|
89
|
+
### data?
|
|
90
|
+
|
|
91
|
+
> `optional` **data**: `any`
|
|
92
|
+
|
|
93
|
+
Defined in: node\_modules/.pnpm/@isdk+common-error@0.2.2/node\_modules/@isdk/common-error/dist/index.d.ts:143
|
|
94
|
+
|
|
95
|
+
#### Inherited from
|
|
96
|
+
|
|
97
|
+
`CommonError.data`
|
|
98
|
+
|
|
99
|
+
***
|
|
100
|
+
|
|
101
|
+
### message
|
|
102
|
+
|
|
103
|
+
> **message**: `string`
|
|
104
|
+
|
|
105
|
+
Defined in: node\_modules/.pnpm/typescript@5.7.3/node\_modules/typescript/lib/lib.es5.d.ts:1077
|
|
106
|
+
|
|
107
|
+
#### Inherited from
|
|
108
|
+
|
|
109
|
+
`CommonError.message`
|
|
110
|
+
|
|
111
|
+
***
|
|
112
|
+
|
|
113
|
+
### name
|
|
114
|
+
|
|
115
|
+
> **name**: `string`
|
|
116
|
+
|
|
117
|
+
Defined in: node\_modules/.pnpm/typescript@5.7.3/node\_modules/typescript/lib/lib.es5.d.ts:1076
|
|
118
|
+
|
|
119
|
+
#### Inherited from
|
|
120
|
+
|
|
121
|
+
`CommonError.name`
|
|
122
|
+
|
|
123
|
+
***
|
|
124
|
+
|
|
125
|
+
### stack?
|
|
126
|
+
|
|
127
|
+
> `optional` **stack**: `string`
|
|
128
|
+
|
|
129
|
+
Defined in: node\_modules/.pnpm/typescript@5.7.3/node\_modules/typescript/lib/lib.es5.d.ts:1078
|
|
130
|
+
|
|
131
|
+
#### Inherited from
|
|
132
|
+
|
|
133
|
+
`CommonError.stack`
|
|
134
|
+
|
|
135
|
+
***
|
|
136
|
+
|
|
137
|
+
### code
|
|
138
|
+
|
|
139
|
+
> `static` **code**: `ErrorCode` = `OfflineCacheMissErrorCode`
|
|
140
|
+
|
|
141
|
+
Defined in: [packages/proxy/src/errors.ts:25](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/errors.ts#L25)
|
|
142
|
+
|
|
143
|
+
The error code associated with the error.
|
|
144
|
+
|
|
145
|
+
#### Overrides
|
|
146
|
+
|
|
147
|
+
`CommonError.code`
|
|
148
|
+
|
|
149
|
+
***
|
|
150
|
+
|
|
151
|
+
### stackTraceLimit
|
|
152
|
+
|
|
153
|
+
> `static` **stackTraceLimit**: `number`
|
|
154
|
+
|
|
155
|
+
Defined in: node\_modules/.pnpm/@types+node@20.19.26/node\_modules/@types/node/globals.d.ts:68
|
|
156
|
+
|
|
157
|
+
The `Error.stackTraceLimit` property specifies the number of stack frames
|
|
158
|
+
collected by a stack trace (whether generated by `new Error().stack` or
|
|
159
|
+
`Error.captureStackTrace(obj)`).
|
|
160
|
+
|
|
161
|
+
The default value is `10` but may be set to any valid JavaScript number. Changes
|
|
162
|
+
will affect any stack trace captured _after_ the value has been changed.
|
|
163
|
+
|
|
164
|
+
If set to a non-number value, or set to a negative number, stack traces will
|
|
165
|
+
not capture any frames.
|
|
166
|
+
|
|
167
|
+
#### Inherited from
|
|
168
|
+
|
|
169
|
+
`CommonError.stackTraceLimit`
|
|
170
|
+
|
|
171
|
+
## Methods
|
|
172
|
+
|
|
173
|
+
### fromJSON()
|
|
174
|
+
|
|
175
|
+
> **fromJSON**(`json`): `BaseError`
|
|
176
|
+
|
|
177
|
+
Defined in: node\_modules/.pnpm/@isdk+common-error@0.2.2/node\_modules/@isdk/common-error/dist/index.d.ts:183
|
|
178
|
+
|
|
179
|
+
#### Parameters
|
|
180
|
+
|
|
181
|
+
##### json
|
|
182
|
+
|
|
183
|
+
`any`
|
|
184
|
+
|
|
185
|
+
#### Returns
|
|
186
|
+
|
|
187
|
+
`BaseError`
|
|
188
|
+
|
|
189
|
+
#### Inherited from
|
|
190
|
+
|
|
191
|
+
`CommonError.fromJSON`
|
|
192
|
+
|
|
193
|
+
***
|
|
194
|
+
|
|
195
|
+
### toJSON()
|
|
196
|
+
|
|
197
|
+
> **toJSON**(): `any`
|
|
198
|
+
|
|
199
|
+
Defined in: node\_modules/.pnpm/@isdk+common-error@0.2.2/node\_modules/@isdk/common-error/dist/index.d.ts:165
|
|
200
|
+
|
|
201
|
+
Returns a JSON representation of the error.
|
|
202
|
+
|
|
203
|
+
#### Returns
|
|
204
|
+
|
|
205
|
+
`any`
|
|
206
|
+
|
|
207
|
+
A JSON representation of the error.
|
|
208
|
+
|
|
209
|
+
#### Inherited from
|
|
210
|
+
|
|
211
|
+
`CommonError.toJSON`
|
|
212
|
+
|
|
213
|
+
***
|
|
214
|
+
|
|
215
|
+
### captureStackTrace()
|
|
216
|
+
|
|
217
|
+
> `static` **captureStackTrace**(`targetObject`, `constructorOpt?`): `void`
|
|
218
|
+
|
|
219
|
+
Defined in: node\_modules/.pnpm/@types+node@20.19.26/node\_modules/@types/node/globals.d.ts:52
|
|
220
|
+
|
|
221
|
+
Creates a `.stack` property on `targetObject`, which when accessed returns
|
|
222
|
+
a string representing the location in the code at which
|
|
223
|
+
`Error.captureStackTrace()` was called.
|
|
224
|
+
|
|
225
|
+
```js
|
|
226
|
+
const myObject = {};
|
|
227
|
+
Error.captureStackTrace(myObject);
|
|
228
|
+
myObject.stack; // Similar to `new Error().stack`
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
The first line of the trace will be prefixed with
|
|
232
|
+
`${myObject.name}: ${myObject.message}`.
|
|
233
|
+
|
|
234
|
+
The optional `constructorOpt` argument accepts a function. If given, all frames
|
|
235
|
+
above `constructorOpt`, including `constructorOpt`, will be omitted from the
|
|
236
|
+
generated stack trace.
|
|
237
|
+
|
|
238
|
+
The `constructorOpt` argument is useful for hiding implementation
|
|
239
|
+
details of error generation from the user. For instance:
|
|
240
|
+
|
|
241
|
+
```js
|
|
242
|
+
function a() {
|
|
243
|
+
b();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function b() {
|
|
247
|
+
c();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function c() {
|
|
251
|
+
// Create an error without stack trace to avoid calculating the stack trace twice.
|
|
252
|
+
const { stackTraceLimit } = Error;
|
|
253
|
+
Error.stackTraceLimit = 0;
|
|
254
|
+
const error = new Error();
|
|
255
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
256
|
+
|
|
257
|
+
// Capture the stack trace above function b
|
|
258
|
+
Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
|
|
259
|
+
throw error;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
a();
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
#### Parameters
|
|
266
|
+
|
|
267
|
+
##### targetObject
|
|
268
|
+
|
|
269
|
+
`object`
|
|
270
|
+
|
|
271
|
+
##### constructorOpt?
|
|
272
|
+
|
|
273
|
+
`Function`
|
|
274
|
+
|
|
275
|
+
#### Returns
|
|
276
|
+
|
|
277
|
+
`void`
|
|
278
|
+
|
|
279
|
+
#### Inherited from
|
|
280
|
+
|
|
281
|
+
`CommonError.captureStackTrace`
|
|
282
|
+
|
|
283
|
+
***
|
|
284
|
+
|
|
285
|
+
### create()
|
|
286
|
+
|
|
287
|
+
> `static` **create**(`__namedParameters`): `CommonError`
|
|
288
|
+
|
|
289
|
+
Defined in: node\_modules/.pnpm/@isdk+common-error@0.2.2/node\_modules/@isdk/common-error/dist/index.d.ts:186
|
|
290
|
+
|
|
291
|
+
#### Parameters
|
|
292
|
+
|
|
293
|
+
##### \_\_namedParameters
|
|
294
|
+
|
|
295
|
+
###### code?
|
|
296
|
+
|
|
297
|
+
`string` \| `number`
|
|
298
|
+
|
|
299
|
+
###### data?
|
|
300
|
+
|
|
301
|
+
`any`
|
|
302
|
+
|
|
303
|
+
###### error
|
|
304
|
+
|
|
305
|
+
`string`
|
|
306
|
+
|
|
307
|
+
###### name?
|
|
308
|
+
|
|
309
|
+
`string`
|
|
310
|
+
|
|
311
|
+
#### Returns
|
|
312
|
+
|
|
313
|
+
`CommonError`
|
|
314
|
+
|
|
315
|
+
#### Inherited from
|
|
316
|
+
|
|
317
|
+
`CommonError.create`
|
|
318
|
+
|
|
319
|
+
***
|
|
320
|
+
|
|
321
|
+
### createErrorClass()
|
|
322
|
+
|
|
323
|
+
> `static` **createErrorClass**(`aType`, `aErrorCode?`, `ParentErrorClass?`): *typeof* `BaseError`
|
|
324
|
+
|
|
325
|
+
Defined in: node\_modules/.pnpm/@isdk+common-error@0.2.2/node\_modules/@isdk/common-error/dist/index.d.ts:145
|
|
326
|
+
|
|
327
|
+
Create an Error Class
|
|
328
|
+
|
|
329
|
+
#### Parameters
|
|
330
|
+
|
|
331
|
+
##### aType
|
|
332
|
+
|
|
333
|
+
`string`
|
|
334
|
+
|
|
335
|
+
the error type(class) name
|
|
336
|
+
|
|
337
|
+
##### aErrorCode?
|
|
338
|
+
|
|
339
|
+
`string` | `number` | *typeof* `AbstractError`
|
|
340
|
+
|
|
341
|
+
##### ParentErrorClass?
|
|
342
|
+
|
|
343
|
+
*typeof* `BaseError`
|
|
344
|
+
|
|
345
|
+
the parent error class. defaults to AbstractError
|
|
346
|
+
|
|
347
|
+
#### Returns
|
|
348
|
+
|
|
349
|
+
*typeof* `BaseError`
|
|
350
|
+
|
|
351
|
+
the new Error Class
|
|
352
|
+
|
|
353
|
+
#### Inherited from
|
|
354
|
+
|
|
355
|
+
`CommonError.createErrorClass`
|
|
356
|
+
|
|
357
|
+
***
|
|
358
|
+
|
|
359
|
+
### fromJSON()
|
|
360
|
+
|
|
361
|
+
> `static` **fromJSON**(`json`): `BaseError`
|
|
362
|
+
|
|
363
|
+
Defined in: node\_modules/.pnpm/@isdk+common-error@0.2.2/node\_modules/@isdk/common-error/dist/index.d.ts:182
|
|
364
|
+
|
|
365
|
+
Creates a new error instance from a JSON representation.
|
|
366
|
+
This method is useful for deserializing an error that was serialized with `toJSON`.
|
|
367
|
+
|
|
368
|
+
#### Parameters
|
|
369
|
+
|
|
370
|
+
##### json
|
|
371
|
+
|
|
372
|
+
`any`
|
|
373
|
+
|
|
374
|
+
A JSON object representing the error.
|
|
375
|
+
|
|
376
|
+
#### Returns
|
|
377
|
+
|
|
378
|
+
`BaseError`
|
|
379
|
+
|
|
380
|
+
A new instance of the error class (or a subclass).
|
|
381
|
+
|
|
382
|
+
#### Example
|
|
383
|
+
|
|
384
|
+
```ts
|
|
385
|
+
const originalError = new NotFoundError('thing');
|
|
386
|
+
const json = originalError.toJSON();
|
|
387
|
+
|
|
388
|
+
// Deserialize
|
|
389
|
+
const newError = NotFoundError.fromJSON(json);
|
|
390
|
+
console.log(newError instanceof NotFoundError); // true
|
|
391
|
+
console.log(newError.message); // 'Could not find thing.'
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
#### Inherited from
|
|
395
|
+
|
|
396
|
+
`CommonError.fromJSON`
|
|
397
|
+
|
|
398
|
+
***
|
|
399
|
+
|
|
400
|
+
### prepareStackTrace()
|
|
401
|
+
|
|
402
|
+
> `static` **prepareStackTrace**(`err`, `stackTraces`): `any`
|
|
403
|
+
|
|
404
|
+
Defined in: node\_modules/.pnpm/@types+node@20.19.26/node\_modules/@types/node/globals.d.ts:56
|
|
405
|
+
|
|
406
|
+
#### Parameters
|
|
407
|
+
|
|
408
|
+
##### err
|
|
409
|
+
|
|
410
|
+
`Error`
|
|
411
|
+
|
|
412
|
+
##### stackTraces
|
|
413
|
+
|
|
414
|
+
`CallSite`[]
|
|
415
|
+
|
|
416
|
+
#### Returns
|
|
417
|
+
|
|
418
|
+
`any`
|
|
419
|
+
|
|
420
|
+
#### See
|
|
421
|
+
|
|
422
|
+
https://v8.dev/docs/stack-trace-api#customizing-stack-traces
|
|
423
|
+
|
|
424
|
+
#### Inherited from
|
|
425
|
+
|
|
426
|
+
`CommonError.prepareStackTrace`
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
# Class: SmartCache
|
|
8
8
|
|
|
9
|
-
Defined in: [core/SmartCache.ts:39](https://github.com/isdk/proxy.js/blob/
|
|
9
|
+
Defined in: [packages/proxy/src/core/SmartCache.ts:39](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/core/SmartCache.ts#L39)
|
|
10
10
|
|
|
11
11
|
智能混合缓存类 (Hybrid Multi-tier Cache)
|
|
12
12
|
|
|
@@ -26,7 +26,7 @@ Defined in: [core/SmartCache.ts:39](https://github.com/isdk/proxy.js/blob/76fee3
|
|
|
26
26
|
|
|
27
27
|
> **new SmartCache**(`options`): `SmartCache`
|
|
28
28
|
|
|
29
|
-
Defined in: [core/SmartCache.ts:44](https://github.com/isdk/proxy.js/blob/
|
|
29
|
+
Defined in: [packages/proxy/src/core/SmartCache.ts:44](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/core/SmartCache.ts#L44)
|
|
30
30
|
|
|
31
31
|
#### Parameters
|
|
32
32
|
|
|
@@ -42,9 +42,19 @@ Defined in: [core/SmartCache.ts:44](https://github.com/isdk/proxy.js/blob/76fee3
|
|
|
42
42
|
|
|
43
43
|
### clear()
|
|
44
44
|
|
|
45
|
-
> **clear**(): `Promise`\<`void`\>
|
|
45
|
+
> **clear**(`clearPersistent?`): `Promise`\<`void`\>
|
|
46
46
|
|
|
47
|
-
Defined in: [core/SmartCache.ts:
|
|
47
|
+
Defined in: [packages/proxy/src/core/SmartCache.ts:202](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/core/SmartCache.ts#L202)
|
|
48
|
+
|
|
49
|
+
Clears the cache. By default, both the in-memory cache and the persistent disk cache are cleared.
|
|
50
|
+
|
|
51
|
+
#### Parameters
|
|
52
|
+
|
|
53
|
+
##### clearPersistent?
|
|
54
|
+
|
|
55
|
+
`boolean` = `true`
|
|
56
|
+
|
|
57
|
+
Whether to clear the persistent (disk) cache. Defaults to `true` for backward compatibility.
|
|
48
58
|
|
|
49
59
|
#### Returns
|
|
50
60
|
|
|
@@ -54,9 +64,11 @@ Defined in: [core/SmartCache.ts:193](https://github.com/isdk/proxy.js/blob/76fee
|
|
|
54
64
|
|
|
55
65
|
### delete()
|
|
56
66
|
|
|
57
|
-
> **delete**(`key`): `Promise`\<`void`\>
|
|
67
|
+
> **delete**(`key`, `clearPersistent?`): `Promise`\<`void`\>
|
|
58
68
|
|
|
59
|
-
Defined in: [core/SmartCache.ts:
|
|
69
|
+
Defined in: [packages/proxy/src/core/SmartCache.ts:193](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/core/SmartCache.ts#L193)
|
|
70
|
+
|
|
71
|
+
Deletes the cache entry for the specified key.
|
|
60
72
|
|
|
61
73
|
#### Parameters
|
|
62
74
|
|
|
@@ -64,6 +76,14 @@ Defined in: [core/SmartCache.ts:188](https://github.com/isdk/proxy.js/blob/76fee
|
|
|
64
76
|
|
|
65
77
|
`string`
|
|
66
78
|
|
|
79
|
+
The cache key to delete
|
|
80
|
+
|
|
81
|
+
##### clearPersistent?
|
|
82
|
+
|
|
83
|
+
`boolean` = `true`
|
|
84
|
+
|
|
85
|
+
Whether to also delete the entry from persistent (disk) storage. Defaults to `true`.
|
|
86
|
+
|
|
67
87
|
#### Returns
|
|
68
88
|
|
|
69
89
|
`Promise`\<`void`\>
|
|
@@ -74,7 +94,7 @@ Defined in: [core/SmartCache.ts:188](https://github.com/isdk/proxy.js/blob/76fee
|
|
|
74
94
|
|
|
75
95
|
> **get**(`key`): `Promise`\<[`CacheEntry`](../interfaces/CacheEntry.md) \| `null`\>
|
|
76
96
|
|
|
77
|
-
Defined in: [core/SmartCache.ts:79](https://github.com/isdk/proxy.js/blob/
|
|
97
|
+
Defined in: [packages/proxy/src/core/SmartCache.ts:79](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/core/SmartCache.ts#L79)
|
|
78
98
|
|
|
79
99
|
获取缓存条目
|
|
80
100
|
|
|
@@ -104,7 +124,7 @@ Defined in: [core/SmartCache.ts:79](https://github.com/isdk/proxy.js/blob/76fee3
|
|
|
104
124
|
|
|
105
125
|
> **getStream**(`key`): `ReadableStream`
|
|
106
126
|
|
|
107
|
-
Defined in: [core/SmartCache.ts:159](https://github.com/isdk/proxy.js/blob/
|
|
127
|
+
Defined in: [packages/proxy/src/core/SmartCache.ts:159](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/core/SmartCache.ts#L159)
|
|
108
128
|
|
|
109
129
|
获取磁盘读取流
|
|
110
130
|
|
|
@@ -130,7 +150,7 @@ Node.js 可读流
|
|
|
130
150
|
|
|
131
151
|
> **set**(`key`, `body`, `metadata`): `Promise`\<`void`\>
|
|
132
152
|
|
|
133
|
-
Defined in: [core/SmartCache.ts:129](https://github.com/isdk/proxy.js/blob/
|
|
153
|
+
Defined in: [packages/proxy/src/core/SmartCache.ts:129](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/core/SmartCache.ts#L129)
|
|
134
154
|
|
|
135
155
|
写入缓存条目 (原子写入)
|
|
136
156
|
|
|
@@ -166,7 +186,7 @@ Defined in: [core/SmartCache.ts:129](https://github.com/isdk/proxy.js/blob/76fee
|
|
|
166
186
|
|
|
167
187
|
> **setStream**(`key`, `metadata`): `WritableStream`
|
|
168
188
|
|
|
169
|
-
Defined in: [core/SmartCache.ts:175](https://github.com/isdk/proxy.js/blob/
|
|
189
|
+
Defined in: [packages/proxy/src/core/SmartCache.ts:175](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/core/SmartCache.ts#L175)
|
|
170
190
|
|
|
171
191
|
获取磁盘写入流 (流式缓存)
|
|
172
192
|
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
> **createCachedFetch**(`defaultOptions`): (`request`, `fetcher`, `overrideOptions?`) => `Promise`\<`Response`\>
|
|
10
10
|
|
|
11
|
-
Defined in: [core/createCachedFetch.ts:17](https://github.com/isdk/proxy.js/blob/
|
|
11
|
+
Defined in: [packages/proxy/src/core/createCachedFetch.ts:17](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/core/createCachedFetch.ts#L17)
|
|
12
12
|
|
|
13
13
|
缓存请求工厂函数 (针对终端用户的顶层高阶 API)
|
|
14
14
|
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
> **createFetchWithCache**(`activeCacheWrites?`): (`request`, `fetcher`, `options`) => `Promise`\<`Response`\>
|
|
10
10
|
|
|
11
|
-
Defined in: [core/createFetchWithCache.ts:16](https://github.com/isdk/proxy.js/blob/
|
|
11
|
+
Defined in: [packages/proxy/src/core/createFetchWithCache.ts:16](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/core/createFetchWithCache.ts#L16)
|
|
12
12
|
|
|
13
13
|
单一职责高阶函数:专门用于封装和隔离 activeCacheWrites 并发追踪器。
|
|
14
14
|
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
> **extractData**(`source`, `config?`, `defaultAllowed?`): `Record`\<`string`, `string`[]\>
|
|
10
10
|
|
|
11
|
-
Defined in: [utils/extractData.ts:40](https://github.com/isdk/proxy.js/blob/
|
|
11
|
+
Defined in: [packages/proxy/src/utils/extractData.ts:40](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/utils/extractData.ts#L40)
|
|
12
12
|
|
|
13
13
|
从源对象中根据过滤配置提取数据并标准化。
|
|
14
14
|
|
|
@@ -8,19 +8,18 @@
|
|
|
8
8
|
|
|
9
9
|
> **fetchWithCache**(`request`, `fetcher`, `options`): `Promise`\<`Response`\>
|
|
10
10
|
|
|
11
|
-
Defined in: [core/fetchWithCache.ts:
|
|
11
|
+
Defined in: [packages/proxy/src/core/fetchWithCache.ts:234](https://github.com/isdk/proxy.js/blob/a1563efa4c3081261eb3af8a6404f1b704b33bf1/src/core/fetchWithCache.ts#L234)
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
核心协调函数:协调请求、缓存命中、并发控制和 SWR
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
并且会在响应头中自动注入 `x-proxy-cache` 标明缓存命中状态 (`HIT`, `STALE`, `MISS`, `STALE_IF_ERROR`)。
|
|
15
|
+
流程如下:
|
|
16
|
+
1. 初始化上下文并生成缓存键。
|
|
17
|
+
2. 检查离线模式:若开启则强读取,未命中直接抛错。
|
|
18
|
+
3. 检查请求是否符合缓存规则 (isCacheable)。
|
|
19
|
+
4. 尝试读取缓存并判定状态 (HIT / STALE)。
|
|
20
|
+
5. 处理 SWR (后台更新)。
|
|
21
|
+
6. 处理请求合并 (Request Coalescing),防止缓存击穿。
|
|
22
|
+
7. 若缓存缺失,发起网络请求并流式写入。
|
|
24
23
|
|
|
25
24
|
## Parameters
|
|
26
25
|
|
|
@@ -28,22 +27,22 @@ Defined in: [core/fetchWithCache.ts:370](https://github.com/isdk/proxy.js/blob/7
|
|
|
28
27
|
|
|
29
28
|
`Request`
|
|
30
29
|
|
|
31
|
-
|
|
30
|
+
标准 Web Request 对象
|
|
32
31
|
|
|
33
32
|
### fetcher
|
|
34
33
|
|
|
35
34
|
(`req`) => `Promise`\<`Response`\>
|
|
36
35
|
|
|
37
|
-
|
|
36
|
+
底层发起真实请求的函数
|
|
38
37
|
|
|
39
38
|
### options
|
|
40
39
|
|
|
41
40
|
[`FetchWithCacheOptions`](../interfaces/FetchWithCacheOptions.md)
|
|
42
41
|
|
|
43
|
-
|
|
42
|
+
缓存协调配置项
|
|
44
43
|
|
|
45
44
|
## Returns
|
|
46
45
|
|
|
47
46
|
`Promise`\<`Response`\>
|
|
48
47
|
|
|
49
|
-
|
|
48
|
+
标准 Web Response 对象 (带 x-proxy-cache 标头)
|