Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
})
c.pipe(body)
}
: () => {
removeOnResume()
cacache.get.byDigest(cachePath, info.integrity, {
memoize: opts.memoize
})
.then(data => body.end(data))
.catch(/* istanbul ignore next */ err => {
body.emit('error', err)
})
}
body.once('resume', onResume)
body.once('end', () => removeOnResume)
return this.Promise.resolve(new fetch.Response(body, {
url: req.url,
headers: resHeaders,
status: 200,
size: info.size
}))
}
})
}
}).then(info => {
if (info && info.metadata && matchDetails(req, {
url: info.metadata.url,
reqHeaders: new fetch.Headers(info.metadata.reqHeaders),
resHeaders: new fetch.Headers(info.metadata.resHeaders),
cacheIntegrity: info.integrity,
integrity: opts && opts.integrity
})) {
const resHeaders = new fetch.Headers(info.metadata.resHeaders)
addCacheHeaders(resHeaders, this._path, key, info.integrity, info.time)
if (req.method === 'HEAD') {
return new fetch.Response(null, {
url: req.url,
headers: resHeaders,
status: 200
})
}
const cachePath = this._path
// avoid opening cache file handles until a user actually tries to
// read from it.
const body = new Minipass()
const fitInMemory = info.size < MAX_MEM_SIZE
const removeOnResume = () => body.removeListener('resume', onResume)
const onResume =
opts.memoize !== false && fitInMemory
? () => {
const c = cacache.get.stream.byDigest(cachePath, info.integrity, {
memoize: opts.memoize
headers: iterableToObject(condRes.headers)
})
if (condRes.status >= 500 && !mustRevalidate(cachedRes)) {
// 111 Revalidation failed
// MUST be included if a cache returns a stale response because an
// attempt to revalidate the response failed, due to an inability to
// reach the server.
// (https://tools.ietf.org/html/rfc2616#section-14.46)
setWarning(cachedRes, 111, 'Revalidation failed')
return cachedRes
}
if (condRes.status === 304) { // 304 Not Modified
// Create a synthetic response from the cached body and original req
const synthRes = new fetch.Response(cachedRes.body, condRes)
return opts.cacheManager.put(req, synthRes, opts)
.then(newRes => {
// Get the list first, because if we delete while iterating,
// it'll throw off the count and not make it through all
// of them.
const newHeaders = revalidatedPolicy.policy.responseHeaders()
const toDelete = [...newRes.headers.keys()]
.filter(k => !newHeaders[k])
for (const key of toDelete) {
newRes.headers.delete(key)
}
for (const [key, val] of Object.entries(newHeaders)) {
newRes.headers.set(key, val)
}
return newRes
})
t.test('does not work if response body is a buffer', (t) => {
const Cache = require('../cache')
const dir = t.testdir()
const cache = new Cache(dir, {})
const req = new Request(`${HOST}/put-test`)
const res = new Response(CONTENT, {
headers: { 'content-size': CONTENT.length }
})
t.throws(
() => cache.put(req, res),
'oldBody.on is not a function'
)
t.end()
})
put: () => Promise.resolve(new Response())
}
return Promise.resolve()
}
MockCacache.prototype.put.stream = function putStream () {}
const Cache = mockRequire({ cacache: new MockCacache() })
const cache = new Cache(dir, {})
const req = new Request(`${HOST}/put-test`)
const body = new Minipass()
body.end(CONTENT)
const resOpts = {
url: req.url,
status: 200,
headers: {
'content-length': CONTENT.length
}
}
const initialResponse = new Response(body, resOpts)
return cache.put(req, initialResponse)
.then((actualResponse) => {
t.ok(actualResponse instanceof Response, 'type of response is Response')
return t.resolveMatch(
actualResponse.text(),
CONTENT.toString(),
'should have same body'
)
})
})
mp.end(CONTENT)
return mp
}
const Cache = mockRequire({ cacache: new MockCacache() })
const cache = new Cache(dir, {})
const req = new Request(`${HOST}/put-test`, { method: 'HEAD' })
const body = new Minipass()
body.end(CONTENT)
const resOpts = {
url: req.url,
status: 200,
headers: {
'content-length': CONTENT.length
}
}
const initialResponse = new Response(body, resOpts)
return cache.put(req, initialResponse)
.then((actualResponse) => {
t.ok(actualResponse instanceof Response, 'type of response is Response')
t.equal(actualResponse, initialResponse, 'same response object')
return t.resolveMatch(
actualResponse.text(),
CONTENT.toString(),
'should have same body'
)
})
})
function remoteFetchHandleIntegrity (res, integrity) {
const oldBod = res.body
const newBod = ssri.integrityStream({
integrity
})
return new fetch.Response(new MinipassPipeline(oldBod, newBod), res)
}
function checkResponse (method, res, registry, startTime, opts) {
opts = config(opts)
if (res.headers.has('npm-notice') && !res.headers.has('x-local-cache')) {
opts.log.notice('', res.headers.get('npm-notice'))
}
checkWarnings(res, registry, opts)
if (res.status >= 400) {
logRequest(method, res, startTime, opts)
return checkErrors(method, res, startTime, opts)
} else {
res.body.on('end', () => logRequest(method, res, startTime, opts))
if (opts.ignoreBody) {
res.body.resume()
return new Response(null, res)
}
return res
}
}