make-fetch-happen
Advanced tools
Comparing version 2.4.1 to 2.4.2
85
cache.js
@@ -10,2 +10,3 @@ 'use strict' | ||
const url = require('url') | ||
const stream = require('stream') | ||
@@ -45,2 +46,6 @@ const MAX_MEM_SIZE = 5 * 1024 * 1024 // 5MB | ||
return cacache.get.info(this._path, key).then(info => { | ||
return info && cacache.get.hasContent( | ||
this._path, info.integrity, opts | ||
).then(exists => exists && info) | ||
}).then(info => { | ||
if (info && info.metadata && matchDetails(req, { | ||
@@ -62,47 +67,41 @@ url: info.metadata.url, | ||
} | ||
return Promise.resolve().then(() => { | ||
const cachePath = this._path | ||
let disturbed = false | ||
// avoid opening cache file handles until a user actually tries to | ||
// read from it. | ||
const body = through((chunk, enc, cb) => { | ||
if (disturbed) { | ||
cb(null, chunk, enc) | ||
} else { | ||
disturbed = true | ||
if (opts.memoize !== false && info.size > MAX_MEM_SIZE) { | ||
pipe( | ||
cacache.get.stream.byDigest(cachePath, info.integrity, { | ||
memoize: opts.memoize | ||
}), | ||
body, | ||
() => {} | ||
) | ||
} else { | ||
// cacache is much faster at bulk reads | ||
cacache.get.byDigest(cachePath, info.integrity, { | ||
memoize: opts.memoize | ||
}).then(data => { | ||
body.write(data, () => { | ||
body.end() | ||
}) | ||
}, err => body.emit('error', err)) | ||
} | ||
cb() // throw away dummy data | ||
let body | ||
const cachePath = this._path | ||
// avoid opening cache file handles until a user actually tries to | ||
// read from it. | ||
if (opts.memoize !== false && info.size > MAX_MEM_SIZE) { | ||
body = new stream.PassThrough() | ||
const realRead = body._read | ||
body._read = function (size) { | ||
body._read = realRead | ||
pipe( | ||
cacache.get.stream.byDigest(cachePath, info.integrity, { | ||
memoize: opts.memoize | ||
}), | ||
body, | ||
err => body.emit(err)) | ||
return realRead.call(this, size) | ||
} | ||
} else { | ||
let readOnce = false | ||
// cacache is much faster at bulk reads | ||
body = new stream.Readable({ | ||
read () { | ||
if (readOnce) return this.push(null) | ||
readOnce = true | ||
cacache.get.byDigest(cachePath, info.integrity, { | ||
memoize: opts.memoize | ||
}).then(data => { | ||
this.push(data) | ||
this.push(null) | ||
}, err => this.emit('error', err)) | ||
} | ||
}) | ||
body.write('dummy') | ||
return new fetch.Response(body, { | ||
url: req.url, | ||
headers: resHeaders, | ||
status: 200, | ||
size: info.size | ||
}) | ||
}).catch(err => { | ||
if (err.code === 'ENOENT') { | ||
return null | ||
} else { | ||
throw err | ||
} | ||
}) | ||
} | ||
return Promise.resolve(new fetch.Response(body, { | ||
url: req.url, | ||
headers: resHeaders, | ||
status: 200, | ||
size: info.size | ||
})) | ||
} | ||
@@ -109,0 +108,0 @@ }) |
@@ -5,2 +5,13 @@ # Change Log | ||
<a name="2.4.2"></a> | ||
## [2.4.2](https://github.com/zkat/make-fetch-happen/compare/v2.4.1...v2.4.2) (2017-05-04) | ||
### Bug Fixes | ||
* **cache:** reduce race condition window by checking for content ([24544b1](https://github.com/zkat/make-fetch-happen/commit/24544b1)) | ||
* **match:** Rewrite the conditional stream logic (#25) ([66bba4b](https://github.com/zkat/make-fetch-happen/commit/66bba4b)) | ||
<a name="2.4.1"></a> | ||
@@ -7,0 +18,0 @@ ## [2.4.1](https://github.com/zkat/make-fetch-happen/compare/v2.4.0...v2.4.1) (2017-04-28) |
{ | ||
"name": "make-fetch-happen", | ||
"version": "2.4.1", | ||
"version": "2.4.2", | ||
"description": "Opinionated, caching, retrying fetch client", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55068
736