Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
);
}
const { policy: policyRaw, ttlOverride, body } = JSON.parse(entry);
const policy = CachePolicy.fromObject(policyRaw);
// Remove url from the policy, because otherwise it would never match a request with a custom cache key
policy._url = undefined;
if (
(ttlOverride && policy.age() < ttlOverride) ||
(!ttlOverride &&
policy.satisfiesWithoutRevalidation(policyRequestFrom(request)))
) {
const headers = policy.responseHeaders();
return new Response(body, {
url: policy._url,
status: policy._status,
headers,
});
} else {
const revalidationHeaders = policy.revalidationHeaders(
policyRequestFrom(request),
);
const revalidationRequest = new Request(request, {
headers: revalidationHeaders,
});
const revalidationResponse = await this.httpFetch(revalidationRequest);
const { policy: revalidatedPolicy, modified } = policy.revalidatedPolicy(
policyRequestFrom(revalidationRequest),
policyResponseFrom(revalidationResponse),
const body = await response.text();
const entry = JSON.stringify({
policy: policy.toObject(),
ttlOverride,
body,
});
await this.keyValueCache.set(cacheKey, entry, {
ttl,
});
// We have to clone the response before returning it because the
// body can only be used once.
// To avoid https://github.com/bitinn/node-fetch/issues/151, we don't use
// response.clone() but create a new response from the consumed body
return new Response(body, {
url: response.url,
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
}
}
({ graphqlResponse, responseInit }) =>
new Response(graphqlResponse, responseInit),
(error: HttpQueryError) => {
(error: HttpQueryError) => {
if ('HttpQueryError' !== error.name) throw error;
const res = new Response(error.message, {
status: error.statusCode,
headers: error.headers,
});
return res;
},
);
} else {
const revalidationHeaders = policy.revalidationHeaders(
policyRequestFrom(request),
);
const revalidationRequest = new Request(request, {
headers: revalidationHeaders,
});
const revalidationResponse = await this.httpFetch(revalidationRequest);
const { policy: revalidatedPolicy, modified } = policy.revalidatedPolicy(
policyRequestFrom(revalidationRequest),
policyResponseFrom(revalidationResponse),
);
return this.storeResponseAndReturnClone(
new Response(modified ? await revalidationResponse.text() : body, {
url: revalidatedPolicy._url,
status: revalidatedPolicy._status,
headers: revalidatedPolicy.responseHeaders(),
}),
request,
revalidatedPolicy,
cacheKey,
options.cacheOptions,
);
}
}