Skip to content

Commit

Permalink
chore: rename local header from cache-hit to x-proxy-cache and leave …
Browse files Browse the repository at this point in the history
…it in the reply
  • Loading branch information
alfetopito committed Apr 29, 2024
1 parent aeec38c commit 71ecd72
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/routes/coingeckoProxy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ const coingeckoProxy: FastifyPluginAsync = async (
if (isCachedResponse(result)) {
fastify.log.info(`onRequest: cache hit for ${req.url}`);
reply
.headers({ "cache-hit": true, ...result.item.headers })
.headers({
...result.item.headers,
"x-proxy-cache": "HIT",
})
.send(result.item.contents);
}
});
Expand All @@ -59,12 +62,16 @@ const coingeckoProxy: FastifyPluginAsync = async (
);
}

if (reply.getHeader("cache-hit")) {
// Header set when there's a cache hit, remove it
reply.removeHeader("cache-hit");
} else if (reply.statusCode >= 200 && reply.statusCode < 300) {
if (
reply.getHeader("x-proxy-cache") !== "HIT" &&
reply.statusCode >= 200 &&
reply.statusCode < 300
) {
fastify.log.info(`onSend: caching response for ${req.url}`);

// Set header indicating the internal cache miss
reply.header("x-proxy-cache", "MISS");

// No cache hit, consume the payload stream to be able to cache it
let contents = "";
for await (const chunk of payload as ReadableStream) {
Expand Down

0 comments on commit 71ecd72

Please sign in to comment.