Skip to content

Commit

Permalink
feat(cache): properly handle http caching headers (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
JacopoDaeli committed Nov 29, 2021
1 parent 647b49d commit d90e8f5
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 10,053 deletions.
1 change: 1 addition & 0 deletions lib/routes/cdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ module.exports =
});
}

res.header('Cache-Control', 'no-storage');
res.status(201).send({
fingerprints: files.map((file) => `${file.id}.gz`),
recommended: files.map((file) => `${file.id}/${file.name}`),
Expand Down
17 changes: 17 additions & 0 deletions lib/routes/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ module.exports =
);
}
version = obj.latestVersion;
// If no version is specified, we want a client
// to cache the result for only 60 seconds
res.header('Cache-Control', 'public, max-age: 60');
} else {
const objVersions = await fastify.getAllObjectVersions({
name,
Expand All @@ -92,6 +95,10 @@ module.exports =
`Version ${version} not found in '${env}'`
);
}

// If a version is specified, the response can be
// cached for longer time since it's unlikely to change
res.header('Cache-Control', 'public, max-age: 86400');
}

/** @type {string[]|null} */
Expand Down Expand Up @@ -172,6 +179,7 @@ module.exports =
}

const { headVersion, latestVersion } = obj;
res.header('Cache-Control', 'public, max-age: 60');
res.send({
headVersion,
latestVersion
Expand Down Expand Up @@ -234,7 +242,9 @@ module.exports =
fastify.log.security({
success: true,
message: `Object "${name}" variant "${variant}" sucessfully created in "${env}"`,
// eslint-disable-next-line max-len
category: 'database', // see https://www.elastic.co/guide/en/ecs/current/ecs-allowed-values-event-category.html#ecs-event-category-database
// eslint-disable-next-line max-len
type: ['creation', 'allowed'], // see https://www.elastic.co/guide/en/ecs/current/ecs-allowed-values-event-type.html#ecs-event-type-creation
method: req.method,
url: req.url,
Expand All @@ -244,6 +254,7 @@ module.exports =
});
}

res.header('Cache-Control', 'no-storage');
res.status(201).send({ created: true });
}
});
Expand Down Expand Up @@ -294,6 +305,7 @@ module.exports =
});
}

res.header('Cache-Control', 'no-storage');
res.status(204);
}
});
Expand Down Expand Up @@ -325,6 +337,7 @@ module.exports =
/**
* @type {FastifyHandler}
*/
// eslint-disable-next-line max-statements
async (req, res) => {
const {
params: { name, env, version },
Expand Down Expand Up @@ -402,6 +415,7 @@ module.exports =
// this operation so that can be run in isolation
await fastify.checkAndFixCorruptedHead({ name, env });

res.header('Cache-Control', 'no-storage');
res.status(204);
}
});
Expand Down Expand Up @@ -488,6 +502,7 @@ module.exports =
});
}

res.header('Cache-Control', 'no-storage');
res.status(204);
}
});
Expand Down Expand Up @@ -592,6 +607,8 @@ module.exports =
host: req.hostname
});
}

res.header('Cache-Control', 'no-storage');
res.status(204);
}
});
Expand Down
1 change: 1 addition & 0 deletions lib/routes/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports =
* @type {FastifyHandler}
*/
async (req, res) => {
res.header('Cache-Control', 'must-revalidate, max-age: 10');
res.send('Warehouse is running');
}
});
Expand Down
5 changes: 5 additions & 0 deletions lib/warehouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fastifyAuth = require('fastify-auth');
const AutoLoad = require('fastify-autoload');
const fp = require('fastify-plugin');
const fastifySensible = require('fastify-sensible');
const etag = require('fastify-etag');
const path = require('path');

const config = require('./config');
Expand Down Expand Up @@ -63,6 +64,10 @@ module.exports = fp(
fastify.register(fastifyAuth, opts);
fastify.register(fastifySensible, opts);

// Automatically calculate and return response etag
// and return 304 status if-none-match condition fulfil
fastify.register(etag, { algorithm: 'fnv1a' });

await fastify.after();

return Promise.all([
Expand Down
Loading

0 comments on commit d90e8f5

Please sign in to comment.