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 = {}; const cache = {};
@@ -30,13 +31,14 @@ function saveResponse (res) {
if (res?.headersSent) { if (res?.headersSent) {
const etag = res.get("ETag"); const etag = res.get("ETag");
if (etag && res.locals.saveEtag !== false) { if (etag && res.locals.saveEtag !== false) {
const req = res.req;
if (res.get("set-cookie")) { if (res.get("set-cookie")) {
// Do not save any responses containing cookies // Do not save any responses containing cookies
DEBUG(`Not caching due to cookies ${etag}: ${req.method} ${req.url}`);
return; return;
} }
const cache = getCache(res); const cache = getCache(res);
const req = res.req; DEBUG(`Caching ETag ${etag}: ${req.method} ${req.url}`);
console.log(`Saving ETag: ${req.method} ${req.url}${etag}`);
const headers = structuredClone(res.getHeaders()); const headers = structuredClone(res.getHeaders());
cache[req.url] = {etag, headers}; 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'); const { isCached } = require('./cache');
function isIdempotentMethod (method) { function isIdempotentMethod (method) {
@@ -20,7 +21,8 @@ function ifNoneMatch (req, res, next) {
const cached = isCached(req); const cached = isCached(req);
if (cached) { 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); setHeaders(res, cached.headers);
if (req.method == "GET" || req.method == "HEAD") { if (req.method == "GET" || req.method == "HEAD") {
res.status(304).send(); res.status(304).send();

View File

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