shared-http-cache 1.0.5 → 1.0.6
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/index.d.ts +39 -0
- package/index.js +0 -6
- package/package.json +1 -1
- package/readme.md +3 -3
package/index.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
interface FetchRequest {
|
|
2
|
+
/** Request URL. */
|
|
3
|
+
url: string;
|
|
4
|
+
/** Resource integrity. */
|
|
5
|
+
integrity?: string;
|
|
6
|
+
/** Request init options [MDN reference](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit) */
|
|
7
|
+
options?: RequestInit;
|
|
8
|
+
/** Callback function. */
|
|
9
|
+
callback?: (response: {
|
|
10
|
+
/** Response buffer. */
|
|
11
|
+
buffer: Buffer;
|
|
12
|
+
/** Response headers [MDN reference](https://developer.mozilla.org/en-US/docs/Web/API/Headers) */
|
|
13
|
+
headers: Headers;
|
|
14
|
+
/** Is response from cache? */
|
|
15
|
+
fromCache: boolean;
|
|
16
|
+
/** Request index. */
|
|
17
|
+
index: number;
|
|
18
|
+
}) => void;
|
|
19
|
+
}
|
|
20
|
+
declare class SharedHttpCache {
|
|
21
|
+
constructor(options?: {
|
|
22
|
+
/** Cache directory (default: ".cache") */
|
|
23
|
+
cacheDir?: string;
|
|
24
|
+
/** Await storage (default: false) */
|
|
25
|
+
awaitStorage?: boolean;
|
|
26
|
+
/** Optional properties */
|
|
27
|
+
[key: string]: any;
|
|
28
|
+
});
|
|
29
|
+
/** Cache directory path (default: ".cache") */
|
|
30
|
+
readonly cacheDir: string;
|
|
31
|
+
/** Await storage (default: false) */
|
|
32
|
+
readonly awaitStorage: boolean;
|
|
33
|
+
/** Fetch multiple requests (async).*/
|
|
34
|
+
fetch(requests: readonly FetchRequest[]): Promise<this>;
|
|
35
|
+
/** Storage management */
|
|
36
|
+
readonly store: typeof import('cacache');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export = SharedHttpCache;
|
package/index.js
CHANGED
|
@@ -7,12 +7,6 @@ class SharedHttpCache {
|
|
|
7
7
|
Object.assign(this, { cacheDir: '.cache', awaitStorage: false }, options);
|
|
8
8
|
this.store = cacache;
|
|
9
9
|
}
|
|
10
|
-
/**
|
|
11
|
-
* Fetch multiple resources with HTTP cache support.
|
|
12
|
-
* @param {Array<{url:string,integrity?:string,options?:RequestInit,callback:(response:{buffer:Buffer,headers:Headers,fromCache:boolean,index:number})=>void}>} requests
|
|
13
|
-
* @returns {Promise<this|{url:string,headers?:Headers,error:Error,index:number}[]>}
|
|
14
|
-
* @see [RequestInit](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit), [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) MDN references
|
|
15
|
-
*/
|
|
16
10
|
async fetch(requests) {
|
|
17
11
|
if (!Array.isArray(requests)) return Promise.reject([{ error: new TypeError('requests must be an array.') }]);
|
|
18
12
|
const errors = [];
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -17,7 +17,7 @@ This implementation models a shared HTTP cache that follows the semantics define
|
|
|
17
17
|
The cache is shared (not private) and applies shared-cache rules.
|
|
18
18
|
|
|
19
19
|
- Request `methods` other than `GET` are not stored.
|
|
20
|
-
- Private responses (request `Authorization`, response `Cache-Control="private"` and `Set-Cookie` headers)
|
|
20
|
+
- Private responses (request `Authorization`, response `Cache-Control="private"` and `Set-Cookie` headers) not stored.
|
|
21
21
|
- Variant responses (response header `Vary` present) are not stored.
|
|
22
22
|
- Partial content (response `Content-Range` header) is not stored.
|
|
23
23
|
- Time calculations rely exclusively on locally recorded timestamps, not on server-provided `Date`.
|
|
@@ -375,8 +375,8 @@ See full list of [cacache options](https://github.com/npm/cacache?tab=readme-ov-
|
|
|
375
375
|
|
|
376
376
|
## Bottom line
|
|
377
377
|
|
|
378
|
-
- `max-stale` is intended to be used: many servers enforce `max-age=0`, but clients
|
|
378
|
+
- `max-stale` is intended to be used: many servers enforce `max-age=0`, but clients know how much staleness they can tolerate. Using `max-stale` (recommended up to 24 h) can significantly reduce network requests.
|
|
379
379
|
- providing `integrity` on requests enables fast loads by allowing cached content to be read directly from store.
|
|
380
|
-
- `SharedHttpCache`
|
|
380
|
+
- `SharedHttpCache` init with `awaitStorage: true` is important when `fetch` is continued with `store` actions.
|
|
381
381
|
- private or sensitive content is served, but not stored.
|
|
382
382
|
- cache cleanup and eviction are deliberately left to the consumer; a well-chosen cleanup strategy is essential for maintaining good performance.
|