Skip to content

Commit

Permalink
chore: hotfix v0.6.4 (#67)
Browse files Browse the repository at this point in the history
* fix: consume the payload stream before caching it

* fix: fix start script
  • Loading branch information
alfetopito authored Jul 25, 2024
1 parent 0f3c80b commit 1d464a5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
39 changes: 23 additions & 16 deletions apps/api/src/app/plugins/bffCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CACHE_CONTROL_HEADER, getCache, getCacheControlHeaderValue, parseCacheC
import { FastifyPluginCallback, FastifyReply, FastifyRequest } from "fastify";

const HEADER_NAME = 'x-bff-cache'
import { ReadableStream } from 'stream/web';

interface BffCacheOptions {
ttl?: number
Expand All @@ -17,6 +18,8 @@ export const bffCache: FastifyPluginCallback<BffCacheOptions> = (fastify, opts,
}

let key = getKey(request)
// Remove it so we can cache it properly
request.headers['accept-encoding'] = undefined;

const cacheItem = await getCache(key, fastify).catch(e => {
fastify.log.error(`Error getting key ${key} from cache`, e)
Expand All @@ -39,32 +42,36 @@ export const bffCache: FastifyPluginCallback<BffCacheOptions> = (fastify, opts,
return
})

fastify.addHook('onSend', function (req, reply, payload, next) {
const isCacheHit = reply.getHeader(HEADER_NAME) === 'HIT'
const cacheTtl: number | undefined = getTtlFromResponse(reply, ttl)
const isStatus200 = reply.statusCode >= 200 && reply.statusCode < 300
fastify.addHook('onSend', async function (req, reply, payload) {
const isCacheHit = reply.getHeader(HEADER_NAME) === 'HIT';
const cacheTtl: number | undefined = getTtlFromResponse(reply, ttl);
const isStatus200 = reply.statusCode >= 200 && reply.statusCode < 300;

// If the cache is a hit, or is non-cacheable, we just proceed with the request
if (isCacheHit || cacheTtl === undefined || !isStatus200) {
next();
return;
}

// If there is no cached data, then its a cache-miss
reply.header(HEADER_NAME, 'MISS')

let key = getKey(req)
setCache(key, payload, cacheTtl, fastify).catch(e => {
fastify.log.error(`Error setting key ${key} from cache`, e)
return null
})
reply.header(CACHE_CONTROL_HEADER, getCacheControlHeaderValue(cacheTtl))
next()
})
let contents = '';
for await (const chunk of payload as ReadableStream) {
contents += chunk.toString(); // Process each chunk of data
}

next()
}

const key = getKey(req);
setCache(key, contents, cacheTtl, fastify).catch((e) => {
fastify.log.error(`Error setting key ${key} from cache`, e);
return null;
});
reply.header(CACHE_CONTROL_HEADER, getCacheControlHeaderValue(cacheTtl));

return contents;
});

next();
};

function getKey(req: FastifyRequest) {
return `GET:${req.routerPath}`;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"scripts": {
"start": "yarn start:api",
"start:api": "nx start",
"start:api": "nx start api",
"twap:run-migrations": "nx run twap:run-migrations",
"twap:generate-migrations": "nx run twap:generate-migrations",
"producer": "nx run notification-producer:start",
Expand Down Expand Up @@ -86,4 +86,4 @@
"vite-plugin-dts": "^3.0.3",
"vite-tsconfig-paths": "^4.2.0"
}
}
}

0 comments on commit 1d464a5

Please sign in to comment.