Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// if verifying integrity, fetch must not decompress
opts.compress = false
}
const isCachable = (
(
opts.method === 'GET' ||
opts.method === 'HEAD'
) &&
Boolean(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
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()
})
t.test('in memory: caches correctly', (t) => {
t.plan(5)
const dir = t.testdir()
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)
const MockCacache = function cacache () {}
MockCacache.prototype.put = function putData (
cachePath,
cacheKey,
data,
cacheOpts
t.test('validate set headers', (t) => {
const date = JSON.stringify(new Date().toUTCString())
const replace = true
const headers = new Fetch.Headers({ Warning: 'world' })
const req = new Fetch.Request(url, { headers })
const origHeaderSet = req.headers.set
req.headers.set = (...args) => {
const expectedValue = `999 google.com {"hello":"world"} ${date}`
t.equal('Warning', args[0])
t.same(expectedValue, args[1])
return origHeaderSet.call(headers, ...args)
}
setWarning(req, code, message, replace)
req.headers.set = origHeaderSet
t.end()
})
integrity,
cacheOpts
) => {
t.equal(integrity, INTEGRITY, 'should pass in integrity value')
t.deepEqual(
cacheOpts.integrity,
INTEGRITY,
'should have added integrity property to cache options'
)
const mp = new Minipass()
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(),
(retryHandler, attemptNum) => {
const req = new fetch.Request(uri, reqOpts)
return fetch(req)
.then((res) => {
if (opts.integrity) {
res = remoteFetchHandleIntegrity(res, opts.integrity)
}
res.headers.set('x-fetch-attempts', attemptNum)
const isStream = Minipass.isStream(req.body)
if (opts.cacheManager) {
const isMethodGetHead = (
req.method === 'GET' ||
req.method === 'HEAD'
)
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)
}
}