+2
-2
@@ -43,8 +43,8 @@ export class TechDocsCache {
|
||||
config: Config,
|
||||
{ cache, logger }: { cache: CacheClient; logger: Logger },
|
||||
) {
|
||||
const readTimeout =
|
||||
config.getOptionalNumber('techdocs.cache.readTimeout') || 1000;
|
||||
const timeout = config.getOptionalNumber('techdocs.cache.readTimeout');
|
||||
const readTimeout = timeout === undefined ? 1000 : timeout;
|
||||
return new TechDocsCache({ cache, logger, readTimeout });
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ describe('createCacheMiddleware', () => {
|
||||
app = express().use(router);
|
||||
app.use((req, res, next) => {
|
||||
// By default, send cacheable content.
|
||||
if (req.path !== '/api/static/docs/error.png') {
|
||||
if (req.path !== '/static/docs/error.png') {
|
||||
res.send('default-response');
|
||||
} else {
|
||||
next(new Error());
|
||||
@@ -70,7 +70,7 @@ describe('createCacheMiddleware', () => {
|
||||
describe('middleware', () => {
|
||||
it('does not apply to non-static/docs paths', async () => {
|
||||
await request(app)
|
||||
.get('/api/static/not-docs')
|
||||
.get('/static/not-docs')
|
||||
.expect(200, 'default-response');
|
||||
|
||||
expect(cache.set).not.toHaveBeenCalled();
|
||||
@@ -79,7 +79,7 @@ describe('createCacheMiddleware', () => {
|
||||
it('responds with cached response', async () => {
|
||||
cache.get.mockResolvedValueOnce(getMockHttpResponseFor('xyz'));
|
||||
|
||||
await request(app).get('/api/static/docs/foo.html').expect(200, 'xyz');
|
||||
await request(app).get('/static/docs/foo.html').expect(200, 'xyz');
|
||||
|
||||
await waitForSocketClose();
|
||||
expect(cache.set).not.toHaveBeenCalled();
|
||||
@@ -88,7 +88,7 @@ describe('createCacheMiddleware', () => {
|
||||
it('sets cache when content is cacheable', async () => {
|
||||
const expectedPath = 'default/api/xyz/index.html';
|
||||
await request(app)
|
||||
.get(`/api/static/docs/${expectedPath}`)
|
||||
.get(`/static/docs/${expectedPath}`)
|
||||
.expect(200, 'default-response');
|
||||
|
||||
await waitForSocketClose();
|
||||
@@ -100,7 +100,7 @@ describe('createCacheMiddleware', () => {
|
||||
});
|
||||
|
||||
it('does not set cache on error', async () => {
|
||||
await request(app).get('/api/static/docs/error.png').expect(500);
|
||||
await request(app).get('/static/docs/error.png').expect(500);
|
||||
|
||||
await waitForSocketClose();
|
||||
expect(cache.set).not.toHaveBeenCalled();
|
||||
|
||||
+3
-3
@@ -35,7 +35,7 @@ export const createCacheMiddleware = ({
|
||||
// loaded from cache. Cache key is the object's path (after `/static/docs/`).
|
||||
cacheMiddleware.use(async (req, res, next) => {
|
||||
const socket = res.socket;
|
||||
const isCacheable = req.path.includes('/static/docs/');
|
||||
const isCacheable = req.path.startsWith('/static/docs/');
|
||||
|
||||
// Continue early if this is non-cacheable, or there's no socket.
|
||||
if (!isCacheable || !socket) {
|
||||
@@ -66,11 +66,11 @@ export const createCacheMiddleware = ({
|
||||
|
||||
// When a socket is closed, if there were no errors and the data written
|
||||
// over the socket should be cached, cache it!
|
||||
socket.on('close', hadError => {
|
||||
socket.on('close', async hadError => {
|
||||
const content = Buffer.concat(chunks);
|
||||
const head = content.toString('utf8', 0, 12);
|
||||
if (writeToCache && !hadError && head.match(/HTTP\/\d\.\d 200/)) {
|
||||
cache.set(reqPath, content);
|
||||
await cache.set(reqPath, content);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user