Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async match(req) {
const hashed = hashData(req)
const key = "httpcache:policy:" + hashed // first try with no vary variant
for (let i = 0; i < 5; i++) {
const policyRaw = await fly.cache.getString(key)
logger.debug("Got policy:", key, policyRaw)
if (!policyRaw) {
return undefined
}
const policy = CachePolicy.fromObject(JSON.parse(policyRaw))
// if it fits i sits
if (policy.satisfiesWithoutRevalidation(req)) {
const headers = policy.responseHeaders()
const bodyKey = "httpcache:body:" + hashed
const body = await fly.cache.get(bodyKey)
logger.debug("Got body", body.constructor.name, body.byteLength)
return new Response(body, { status: policy._status, headers })
// }else if(policy._headers){
// TODO: try a new vary based key
// policy._headers has the varies / vary values
// key = hashData(req, policy._headers)
// return undefined
} else {
return undefined
.then(cacheEntry => {
if (typeof cacheEntry === 'undefined') {
return makeRequest(opts);
}
const policy = CachePolicy.fromObject(cacheEntry.cachePolicy);
if (policy.satisfiesWithoutRevalidation(opts)) {
const headers = policy.responseHeaders();
const response = new Response(cacheEntry.statusCode, headers, cacheEntry.body, cacheEntry.url);
response.cachePolicy = policy;
response.fromCache = true;
ee.emit('response', response);
if (typeof cb === 'function') {
cb(response);
}
} else {
revalidate = cacheEntry;
opts.headers = policy.revalidationHeaders(opts);
makeRequest(opts);
}
});
const get = async opts => {
await Promise.resolve();
const cacheEntry = opts.cache ? await this.cache.get(key) : undefined;
if (typeof cacheEntry === 'undefined') {
return makeRequest(opts);
}
const policy = CachePolicy.fromObject(cacheEntry.cachePolicy);
if (policy.satisfiesWithoutRevalidation(opts) && !opts.forceRefresh) {
const headers = policy.responseHeaders();
const response = new Response(cacheEntry.statusCode, headers, cacheEntry.body, cacheEntry.url);
response.cachePolicy = policy;
response.fromCache = true;
ee.emit('response', response);
if (typeof cb === 'function') {
cb(response);
}
} else {
revalidate = cacheEntry;
opts.headers = policy.revalidationHeaders(opts);
makeRequest(opts);
}
};
const get = async opts => {
await Promise.resolve();
const cacheEntry = opts.cache ? await this.cache.get(key) : undefined;
if (typeof cacheEntry === 'undefined') {
return makeRequest(opts);
}
const policy = CachePolicy.fromObject(cacheEntry.cachePolicy);
if (policy.satisfiesWithoutRevalidation(opts) && !opts.forceRefresh) {
const headers = policy.responseHeaders();
const response = new Response(cacheEntry.statusCode, headers, cacheEntry.body, cacheEntry.url);
response.cachePolicy = policy;
response.fromCache = true;
ee.emit('response', response);
if (typeof cb === 'function') {
cb(response);
}
} else {
revalidate = cacheEntry;
opts.headers = policy.revalidationHeaders(opts);
makeRequest(opts);
}
};
return function (request: any, callback: ivm.Reference) {
const key = `${ctx.meta.get('app').id}:${request.url}`
log.debug("cache match called! key:", key)
const found = cache[key]
log.debug("found:", found)
if (!found)
return callback.apply(null, [null, null])
const { rawPolicy, response } = found
let policy = CachePolicy.fromObject(rawPolicy)
log.debug("satisfactory?", policy.satisfiesWithoutRevalidation(request))
if (policy && policy.satisfiesWithoutRevalidation(request)) {
response.headers = policy.responseHeaders();
const body = response.body
delete response.body
return callback.apply(null, [null, new ivm.ExternalCopy(response).copyInto(), transferInto(body)]);
}
return callback.apply(null, [null, null])
}
})
const handler = response => {
if (revalidate && !opts.forceRefresh) {
response.status = response.statusCode;
const revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(opts, response);
if (!revalidatedPolicy.modified) {
const headers = revalidatedPolicy.policy.responseHeaders();
response = new Response(revalidate.statusCode, headers, revalidate.body, revalidate.url);
response.cachePolicy = revalidatedPolicy.policy;
response.fromCache = true;
}
}
if (!response.fromCache) {
response.cachePolicy = new CachePolicy(opts, response, opts);
response.fromCache = false;
}
let clonedResponse;
if (opts.cache && response.cachePolicy.storable()) {
clonedResponse = cloneResponse(response);
async function getItem(key: string): Promise {
let [[, response], [, policy]] = await AsyncStorage.multiGet([
`${ROOT}:${key}:response`,
`${ROOT}:${key}:policy`,
])
if (!response) {
return {response, policy: null}
}
let {body, ...init} = JSON.parse(response)
return {
response: new Response(body, init),
policy: CachePolicy.fromObject(JSON.parse(policy)),
}
}
const handler = response => {
if (revalidate && !opts.forceRefresh) {
response.status = response.statusCode;
const revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(opts, response);
if (!revalidatedPolicy.modified) {
const headers = revalidatedPolicy.policy.responseHeaders();
response = new Response(revalidate.statusCode, headers, revalidate.body, revalidate.url);
response.cachePolicy = revalidatedPolicy.policy;
response.fromCache = true;
}
}
if (!response.fromCache) {
response.cachePolicy = new CachePolicy(opts, response, opts);
response.fromCache = false;
}
let clonedResponse;
if (opts.cache && response.cachePolicy.storable()) {
clonedResponse = cloneResponse(response);
const handler = response => {
if (revalidate && !opts.forceRefresh) {
response.status = response.statusCode;
const revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(opts, response);
if (!revalidatedPolicy.modified) {
const headers = revalidatedPolicy.policy.responseHeaders();
response = new Response(revalidate.statusCode, headers, revalidate.body, revalidate.url);
response.cachePolicy = revalidatedPolicy.policy;
response.fromCache = true;
}
}
if (!response.fromCache) {
response.cachePolicy = new CachePolicy(opts, response, opts);
response.fromCache = false;
}
let clonedResponse;
if (opts.cache && response.cachePolicy.storable()) {
clonedResponse = cloneResponse(response);