Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function cachingFetch (uri, _opts) {
const opts = configureOptions(_opts)
if (opts.integrity) {
initializeSsri()
// if verifying integrity, node-fetch must not decompress
opts.compress = false
}
const isCachable = (opts.method === 'GET' || opts.method === 'HEAD') &&
opts.cacheManager &&
opts.cache !== 'no-store' &&
opts.cache !== 'reload'
if (isCachable) {
const req = new fetch.Request(uri, {
method: opts.method,
headers: opts.headers
})
return opts.cacheManager.match(req, opts).then(res => {
if (res) {
const warningCode = (res.headers.get('Warning') || '').match(/^\d+/)
if (warningCode && +warningCode >= 100 && +warningCode < 200) {
// https://tools.ietf.org/html/rfc7234#section-4.3.4
//
// If a stored response is selected for update, the cache MUST:
//
// * delete any Warning header fields in the stored response with
// warn-code 1xx (see Section 5.5);
//
// * retain any Warning header fields in the stored response with
(retryHandler, attemptNum) => {
const req = new fetch.Request(uri, reqOpts)
return fetch(req)
.then(res => {
res.headers.set('x-fetch-attempts', attemptNum)
if (opts.integrity) {
remoteFetchHandleIntegrity(res, opts.integrity)
}
const isStream = req.body instanceof Stream
if (opts.cacheManager) {
const isMethodGetHead = req.method === 'GET' ||
req.method === 'HEAD'
const isCachable = opts.cache !== 'no-store' &&
isMethodGetHead &&
function cacheDelete (uri, opts) {
opts = configureOptions(opts)
if (opts.cacheManager) {
const req = new fetch.Request(uri, {
method: opts.method,
headers: opts.headers
})
return opts.cacheManager.delete(req, opts)
}
}