Add debugging statements to ETag middleware

This commit is contained in:
D. Berge
2023-11-04 10:43:36 +01:00
parent 60932300c1
commit 9bd0aca18f
3 changed files with 19 additions and 7 deletions

View File

@@ -1,3 +1,4 @@
const { ALERT, ERROR, WARNING, NOTICE, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename);
const cache = {};
@@ -30,13 +31,14 @@ function saveResponse (res) {
if (res?.headersSent) {
const etag = res.get("ETag");
if (etag && res.locals.saveEtag !== false) {
const req = res.req;
if (res.get("set-cookie")) {
// Do not save any responses containing cookies
DEBUG(`Not caching due to cookies ${etag}: ${req.method} ${req.url}`);
return;
}
const cache = getCache(res);
const req = res.req;
console.log(`Saving ETag: ${req.method} ${req.url}${etag}`);
DEBUG(`Caching ETag ${etag}: ${req.method} ${req.url}`);
const headers = structuredClone(res.getHeaders());
cache[req.url] = {etag, headers};
}

View File

@@ -1,3 +1,4 @@
const { ALERT, ERROR, WARNING, NOTICE, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename);
const { isCached } = require('./cache');
function isIdempotentMethod (method) {
@@ -20,7 +21,8 @@ function ifNoneMatch (req, res, next) {
const cached = isCached(req);
if (cached) {
console.log("ETag match", req.url);
DEBUG("ETag match. Returning cached response (ETag: %s, If-None-Match: %s) for %s %s",
cached.etag, req.get("If-None-Match"), req.method, req.url);
setHeaders(res, cached.headers);
if (req.method == "GET" || req.method == "HEAD") {
res.status(304).send();

View File

@@ -2,6 +2,7 @@ const pathToRegExp = require("path-to-regexp");
const { getCache } = require('./cache');
const { listen } = require('../../../lib/db/notify');
const channels = require('../../../lib/db/channels');
const { ALERT, ERROR, WARNING, NOTICE, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename);
const rels = [
{
@@ -71,19 +72,26 @@ function invalidateCache (data, cache) {
const table = data.payload.table;
const fields = { channel, project, operation, table };
// DEBUG("Event received: %j", fields);
for (let rel of rels) {
// DEBUG("Checking if channel matches: %j", rel.channels);
if (rel.channels.includes(channel)) {
for (let url of rel.urls) {
// DEBUG("Checking if url matches: %s", url);
const matches = typeof url === "function"
? url
: pathToRegExp.match(url, {decode: decodeURIComponent, end: false});
for (let [key, data] of Object.entries(cache)) {
// DEBUG("Checking cache entry %s against fields %j", key, fields);
const { params } = matches(key);
// DEBUG("Check result: %j", params);
if (params && Object.entries(params).every(i => fields[i[0]] == i[1])) {
DEBUG("Delete entry %s due to match with parameters %j", key, params);
delete cache[key];
}
@@ -93,7 +101,7 @@ function invalidateCache (data, cache) {
if (rel.callback) {
for (let key of Object.keys(cache)) {
if (rel.callback(key, data)) {
console.log("DELETE ENTRY (CALLBACK)", key);
DEBUG("Delete entry %s due to callback result on channel %s", key, channel);
delete cache[key];
}
}
@@ -116,10 +124,10 @@ async function watch (app) {
}
try {
const client = await listen(channels, etagWatch);
console.log("ETag watch installed", client);
INFO("ETag watch installed");
} catch (err) {
console.error(err);
console.log("ETag watch not installed");
ERROR("ETag watch install failed");
ERROR(err);
}
}