als-send-file
Version:
file serving with advanced options for caching, headers, and error handling, compatible with Express middleware.
23 lines (21 loc) • 913 B
JavaScript
/**
* Sets the Cache-Control header based on provided options.
* @param {http.ServerResponse} res - The response object.
* @param {Object} options - Options for setting the header.
* @param {number} [options.maxAge] - The maximum age in seconds.
* @param {boolean} [options.noCache] - Flag to set no-cache.
* @param {boolean} [options.noStore] - Flag to set no-store.
* @param {string} [options.public] - The public or private directive.
*/
function cacheControl(res, options) {
const { maxAge, noCache, noStore, public } = options
let ccArr = [];
if (public === 'public' || public === 'private') ccArr.push(public);
if (!isNaN(maxAge)) ccArr.push(`max-age=${maxAge}`);
if (noCache) ccArr.push('no-cache');
if (noStore) ccArr.push('no-store');
if (ccArr.length > 0) {
res.setHeader('Cache-Control', ccArr.join(', '));
}
}
module.exports = cacheControl