Remove cache invalidation endpoint from middleware.
Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
committed by
Eric Peterson
parent
a3909d2c2f
commit
8cf4684a4e
@@ -67,39 +67,6 @@ describe('createCacheMiddleware', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('invalidate', () => {
|
||||
it('responds with 400 when no objects are provided', async () => {
|
||||
const response = await request(app).post('/cache/invalidate');
|
||||
expect(response.status).toBe(400);
|
||||
});
|
||||
|
||||
it('responds with 500 if invalidation throws', async () => {
|
||||
cache.invalidateMultiple.mockRejectedValueOnce(new Error());
|
||||
const response = await request(app)
|
||||
.post('/cache/invalidate')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send({
|
||||
objects: ['one/index.html', 'two/index.html'],
|
||||
});
|
||||
|
||||
expect(response.status).toBe(500);
|
||||
});
|
||||
|
||||
it('responds with 204 if invalidation succeeds', async () => {
|
||||
const expectedObjects = ['one/index.html', 'two/index.html'];
|
||||
cache.invalidateMultiple.mockResolvedValue([]);
|
||||
await request(app)
|
||||
.post('/cache/invalidate')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send({
|
||||
objects: expectedObjects,
|
||||
})
|
||||
.expect(204);
|
||||
|
||||
expect(cache.invalidateMultiple).toHaveBeenCalledWith(expectedObjects);
|
||||
});
|
||||
});
|
||||
|
||||
describe('middleware', () => {
|
||||
it('does not apply to non-static/docs paths', async () => {
|
||||
await request(app)
|
||||
|
||||
+1
-48
@@ -13,22 +13,10 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { Router, Request, json } from 'express';
|
||||
import { Router } from 'express';
|
||||
import router from 'express-promise-router';
|
||||
import { Logger } from 'winston';
|
||||
import { TechDocsCache } from '.';
|
||||
import { CacheInvalidationError } from './TechDocsCache';
|
||||
|
||||
type CacheClearRequestParams = {
|
||||
objects: string[];
|
||||
};
|
||||
|
||||
type CacheClearRequest = Request<
|
||||
any,
|
||||
unknown,
|
||||
CacheClearRequestParams,
|
||||
unknown
|
||||
>;
|
||||
|
||||
type CacheMiddlewareOptions = {
|
||||
cache: TechDocsCache;
|
||||
@@ -39,44 +27,9 @@ type ErrorCallback = (err?: Error) => void;
|
||||
|
||||
export const createCacheMiddleware = ({
|
||||
cache,
|
||||
logger,
|
||||
}: CacheMiddlewareOptions): Router => {
|
||||
const cacheMiddleware = router();
|
||||
|
||||
// And endpoint for handling cache invalidation external to the Backstage
|
||||
// Backend (e.g. from the TechDocs CLI).
|
||||
cacheMiddleware.use(json());
|
||||
cacheMiddleware.post(
|
||||
'/cache/invalidate',
|
||||
async (req: CacheClearRequest, res) => {
|
||||
if (req.body?.objects?.length) {
|
||||
logger.debug(
|
||||
`Clearing ${req.body.objects.length} cache entries: (eg: ${req.body.objects[0]})`,
|
||||
);
|
||||
|
||||
try {
|
||||
const invalidated = await cache.invalidateMultiple(req.body.objects);
|
||||
logger.debug(
|
||||
`Successfully invalidated ${invalidated.length} cache entries`,
|
||||
);
|
||||
res.status(204).send();
|
||||
} catch (e) {
|
||||
if (e instanceof CacheInvalidationError) {
|
||||
const uniqueReasons = [
|
||||
...new Set(e.rejections.map(r => r.reason.message)),
|
||||
].join(', ');
|
||||
logger.warn(
|
||||
`Problem invalidating ${e.rejections.length} entries: ${uniqueReasons}`,
|
||||
);
|
||||
}
|
||||
res.status(500).send();
|
||||
}
|
||||
} else {
|
||||
res.status(400).send();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// Middleware that, through socket monkey patching, captures responses as
|
||||
// they're sent over /static/docs/* and caches them. Subsequent requests are
|
||||
// loaded from cache. Cache key is the object's path (after `/static/docs/`).
|
||||
|
||||
Reference in New Issue
Block a user