shared-http-cache 1.0.4 → 1.0.5

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 (2) hide show
  1. package/package.json +1 -1
  2. package/readme.md +38 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shared-http-cache",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Node.Js Utility for fetching multiple HTTP resources with browser-like cache management.",
5
5
  "keywords": [
6
6
  "http-cache",
package/readme.md CHANGED
@@ -315,6 +315,16 @@ The underlying cache store (`cacache`) is exposed directly.
315
315
  sharedHttpCache.store -> cacache
316
316
  ```
317
317
 
318
+ **Listing (example with promise)**
319
+
320
+ ```js
321
+ sharedHttpCache
322
+ .fetch(requests)
323
+ .then((sharedHttpCache) => sharedHttpCache.store.ls(sharedHttpCache.cacheDir))
324
+ .then(console.log)
325
+ .catch((errors) => console.error('Errors:', errors));
326
+ ```
327
+
318
328
  **Compacting (example with await)**
319
329
 
320
330
  ```ts
@@ -322,20 +332,38 @@ sharedHttpCache.store.verify(cacheDir) -> Promise<Object>
322
332
  ```
323
333
 
324
334
  ```js
325
- await sharedHttpCache.store.verify(sharedHttpCache.cacheDir);
335
+ // deadbeef collected, because of invalid checksum.
336
+ sharedHttpCache.store.verify(sharedHttpCache.cacheDir).then((stats) => {
337
+ console.log('cache is much nicer now! stats:', stats);
338
+ });
326
339
  ```
327
340
 
328
- **Listing (example with promise)**
341
+ **Basic cleanup strategy**
329
342
 
330
343
  ```js
331
- sharedHttpCache
332
- .fetch(requests)
333
- .then((sharedHttpCache) => sharedHttpCache.store.ls(sharedHttpCache.cacheDir))
334
- .then(console.log)
335
- .catch((errors) => console.error('Errors:', errors));
344
+ const SharedHttpCache = require('shared-http-cache');
345
+ // only-if-cached also means ... and is not stale!
346
+ (async () => {
347
+ const cache = new SharedHttpCache({ cacheDir: '.cache', awaitStorage: true });
348
+ const entries = await cache.store.ls(cache.cacheDir);
349
+ const requests = Object.keys(entries).map((url) => ({ url, options: { headers: { 'cache-control': 'only-if-cached' } } }));
350
+ await cache.fetch(requests).catch(async (errors) => {
351
+ for (const { url } of errors) {
352
+ const file = url && await cache.store.get.info(cache.cacheDir, url);
353
+ if (file) {
354
+ await cache.store.rm.entry(cache.cacheDir, url, { removeFully: true });
355
+ await cache.store.rm.content(cache.cacheDir, file.integrity);
356
+ }
357
+ }
358
+ });
359
+ })();
336
360
  ```
337
361
 
338
- Other available operations
362
+ **Note:**
363
+
364
+ - This is a fully `RFC 9111` compliant strategy that cleans up all the resources that can be determined as expired based on the stored response headers. For a more flexible approach, `max-stale=$acceptedStaleness` directive can be used in conjunction with `only-if-cached`. Cleanup strategies that rely on empirical calculations, such as `least recently used`, are `NOT RECOMMENDED`.
365
+
366
+ **Other available operations**
339
367
 
340
368
  - sharedHttpCache.store.put(...)
341
369
  - sharedHttpCache.store.get(...)
@@ -349,4 +377,6 @@ See full list of [cacache options](https://github.com/npm/cacache?tab=readme-ov-
349
377
 
350
378
  - `max-stale` is intended to be used: many servers enforce `max-age=0`, but clients usually know how much staleness they can tolerate. Using `max-stale` (recommended up to 24 h) can significantly reduce network requests.
351
379
  - providing `integrity` on requests enables fast loads by allowing cached content to be read directly from store.
380
+ - `SharedHttpCache` instantiation with `awaitStorage: true` is important when `fetch` is continued with `store` actions.
381
+ - private or sensitive content is served, but not stored.
352
382
  - cache cleanup and eviction are deliberately left to the consumer; a well-chosen cleanup strategy is essential for maintaining good performance.