make-fetch-happen
Advanced tools
Comparing version
19
cache.js
@@ -42,3 +42,4 @@ 'use strict' | ||
match (req, opts) { | ||
return cacache.get.info(this._path, cacheKey(req)).then(info => { | ||
const key = cacheKey(req) | ||
return cacache.get.info(this._path, key).then(info => { | ||
if (info && info.metadata && matchDetails(req, { | ||
@@ -52,4 +53,3 @@ url: info.metadata.url, | ||
const resHeaders = new fetch.Headers(info.metadata.resHeaders) | ||
resHeaders.set('X-LOCAL-CACHE', this._path) | ||
resHeaders.set('X-LOCAL-CACHE-TIME', new Date(info.time).toUTCString()) | ||
addCacheHeaders(resHeaders, this._path, key, info.integrity, info.time) | ||
if (req.method === 'HEAD') { | ||
@@ -121,2 +121,3 @@ return new fetch.Response(null, { | ||
const fitInMemory = !!size && size < MAX_MEM_SIZE | ||
const ckey = cacheKey(req) | ||
const cacheOpts = { | ||
@@ -136,5 +137,8 @@ algorithms: opts.algorithms, | ||
// Update metadata without writing | ||
return cacache.get.info(this._path, cacheKey(req)).then(info => { | ||
return cacache.get.info(this._path, ckey).then(info => { | ||
// Providing these will bypass content write | ||
cacheOpts.integrity = info.integrity | ||
addCacheHeaders( | ||
response.headers, this._path, ckey, info.integrity, info.time | ||
) | ||
return new this.Promise((resolve, reject) => { | ||
@@ -245,1 +249,8 @@ pipe( | ||
} | ||
function addCacheHeaders (resHeaders, path, key, hash, time) { | ||
resHeaders.set('X-Local-Cache', path) | ||
resHeaders.set('X-Local-Cache-Key', key) | ||
resHeaders.set('X-Local-Cache-Hash', hash) | ||
resHeaders.set('X-Local-Cache-Time', new Date(time).toUTCString()) | ||
} |
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="1.7.0"></a> | ||
# [1.7.0](https://github.com/zkat/make-fetch-happen/compare/v1.6.0...v1.7.0) (2017-04-08) | ||
### Features | ||
* **cache:** add useful headers to inform users about cached data ([9bd7b00](https://github.com/zkat/make-fetch-happen/commit/9bd7b00)) | ||
<a name="1.6.0"></a> | ||
@@ -7,0 +17,0 @@ # [1.6.0](https://github.com/zkat/make-fetch-happen/compare/v1.5.1...v1.6.0) (2017-04-06) |
{ | ||
"name": "make-fetch-happen", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "Opinionated, caching, retrying fetch client", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -145,2 +145,20 @@ # make-fetch-happen [](https://npm.im/make-fetch-happen) [](https://npm.im/make-fetch-happen) [](https://travis-ci.org/zkat/make-fetch-happen) [](https://ci.appveyor.com/project/zkat/make-fetch-happen) [](https://coveralls.io/github/zkat/make-fetch-happen?branch=latest) | ||
The default cache manager also adds the following headers to cached responses: | ||
* `X-Local-Cache`: Path to the cache the content was found in | ||
* `X-Local-Cache-Key`: Unique cache entry key for this response | ||
* `X-Local-Cache-Hash`: Specific integrity hash for the cached entry | ||
* `X-Local-Cache-Time`: UTCString of the cache insertion time for the entry | ||
Using [`cacache`](https://npm.im/cacache), a call like this may be used to | ||
manually fetch the cached entry: | ||
```javascript | ||
const h = response.headers | ||
cacache.get(h.get('x-local-cache'), h.get('x-local-cache-key')) | ||
// grab content only, directly: | ||
cacache.get.byDigest(h.get('x-local-cache'), h.get('x-local-cache-hash')) | ||
``` | ||
##### Example | ||
@@ -147,0 +165,0 @@ |
40569
3.32%579
1.76%324
5.88%