|
|
@ -68,6 +68,15 @@ function ApiCache() {
|
|
|
|
instances.push(this);
|
|
|
|
instances.push(this);
|
|
|
|
this.id = instances.length;
|
|
|
|
this.id = instances.length;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Logs a message to the console if the `DEBUG` environment variable is set.
|
|
|
|
|
|
|
|
* @param {string} a - The first argument to log.
|
|
|
|
|
|
|
|
* @param {string} b - The second argument to log.
|
|
|
|
|
|
|
|
* @param {string} c - The third argument to log.
|
|
|
|
|
|
|
|
* @param {string} d - The fourth argument to log, and so on... (optional)
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Generated by Trelent
|
|
|
|
|
|
|
|
*/
|
|
|
|
function debug(a, b, c, d) {
|
|
|
|
function debug(a, b, c, d) {
|
|
|
|
let arr = ["\x1b[36m[apicache]\x1b[0m", a, b, c, d].filter(function (arg) {
|
|
|
|
let arr = ["\x1b[36m[apicache]\x1b[0m", a, b, c, d].filter(function (arg) {
|
|
|
|
return arg !== undefined;
|
|
|
|
return arg !== undefined;
|
|
|
@ -77,6 +86,13 @@ function ApiCache() {
|
|
|
|
return (globalOptions.debug || debugEnv) && console.log.apply(null, arr);
|
|
|
|
return (globalOptions.debug || debugEnv) && console.log.apply(null, arr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Returns true if the given request and response should be logged.
|
|
|
|
|
|
|
|
* @param {Object} request The HTTP request object.
|
|
|
|
|
|
|
|
* @param {Object} response The HTTP response object.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Generated by Trelent
|
|
|
|
|
|
|
|
*/
|
|
|
|
function shouldCacheResponse(request, response, toggle) {
|
|
|
|
function shouldCacheResponse(request, response, toggle) {
|
|
|
|
let opt = globalOptions;
|
|
|
|
let opt = globalOptions;
|
|
|
|
let codes = opt.statusCodes;
|
|
|
|
let codes = opt.statusCodes;
|
|
|
@ -99,6 +115,12 @@ function ApiCache() {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Adds a key to the index.
|
|
|
|
|
|
|
|
* @param {string} key The key to add.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Generated by Trelent
|
|
|
|
|
|
|
|
*/
|
|
|
|
function addIndexEntries(key, req) {
|
|
|
|
function addIndexEntries(key, req) {
|
|
|
|
let groupName = req.apicacheGroup;
|
|
|
|
let groupName = req.apicacheGroup;
|
|
|
|
|
|
|
|
|
|
|
@ -111,6 +133,13 @@ function ApiCache() {
|
|
|
|
index.all.unshift(key);
|
|
|
|
index.all.unshift(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Returns a new object containing only the whitelisted headers.
|
|
|
|
|
|
|
|
* @param {Object} headers The original object of header names and values.
|
|
|
|
|
|
|
|
* @param {Array.<string>} globalOptions.headerWhitelist An array of strings representing the whitelisted header names to keep in the output object.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Generated by Trelent
|
|
|
|
|
|
|
|
*/
|
|
|
|
function filterBlacklistedHeaders(headers) {
|
|
|
|
function filterBlacklistedHeaders(headers) {
|
|
|
|
return Object.keys(headers)
|
|
|
|
return Object.keys(headers)
|
|
|
|
.filter(function (key) {
|
|
|
|
.filter(function (key) {
|
|
|
@ -122,6 +151,12 @@ function ApiCache() {
|
|
|
|
}, {});
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @param {Object} headers The response headers to filter.
|
|
|
|
|
|
|
|
* @returns {Object} A new object containing only the whitelisted response headers.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Generated by Trelent
|
|
|
|
|
|
|
|
*/
|
|
|
|
function createCacheObject(status, headers, data, encoding) {
|
|
|
|
function createCacheObject(status, headers, data, encoding) {
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
status: status,
|
|
|
|
status: status,
|
|
|
@ -132,6 +167,14 @@ function ApiCache() {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Sets a cache value for the given key.
|
|
|
|
|
|
|
|
* @param {string} key The cache key to set.
|
|
|
|
|
|
|
|
* @param {*} value The cache value to set.
|
|
|
|
|
|
|
|
* @param {number} duration How long in milliseconds the cached response should be valid for (defaults to 1 hour).
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Generated by Trelent
|
|
|
|
|
|
|
|
*/
|
|
|
|
function cacheResponse(key, value, duration) {
|
|
|
|
function cacheResponse(key, value, duration) {
|
|
|
|
let redis = globalOptions.redisClient;
|
|
|
|
let redis = globalOptions.redisClient;
|
|
|
|
let expireCallback = globalOptions.events.expire;
|
|
|
|
let expireCallback = globalOptions.events.expire;
|
|
|
@ -154,6 +197,12 @@ function ApiCache() {
|
|
|
|
}, Math.min(duration, 2147483647));
|
|
|
|
}, Math.min(duration, 2147483647));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Appends content to the response.
|
|
|
|
|
|
|
|
* @param {string|Buffer} content The content to append.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Generated by Trelent
|
|
|
|
|
|
|
|
*/
|
|
|
|
function accumulateContent(res, content) {
|
|
|
|
function accumulateContent(res, content) {
|
|
|
|
if (content) {
|
|
|
|
if (content) {
|
|
|
|
if (typeof content == "string") {
|
|
|
|
if (typeof content == "string") {
|
|
|
@ -179,6 +228,13 @@ function ApiCache() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Monkeypatches the response object to add cache control headers and create a cache object.
|
|
|
|
|
|
|
|
* @param {Object} req - The request object.
|
|
|
|
|
|
|
|
* @param {Object} res - The response object.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Generated by Trelent
|
|
|
|
|
|
|
|
*/
|
|
|
|
function makeResponseCacheable(req, res, next, key, duration, strDuration, toggle) {
|
|
|
|
function makeResponseCacheable(req, res, next, key, duration, strDuration, toggle) {
|
|
|
|
// monkeypatch res.end to create cache object
|
|
|
|
// monkeypatch res.end to create cache object
|
|
|
|
res._apicache = {
|
|
|
|
res._apicache = {
|
|
|
@ -245,6 +301,13 @@ function ApiCache() {
|
|
|
|
next();
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @param {Request} request
|
|
|
|
|
|
|
|
* @param {Response} response
|
|
|
|
|
|
|
|
* @returns {boolean|undefined} true if the request should be cached, false otherwise. If undefined, defaults to true.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Generated by Trelent
|
|
|
|
|
|
|
|
*/
|
|
|
|
function sendCachedResponse(request, response, cacheObject, toggle, next, duration) {
|
|
|
|
function sendCachedResponse(request, response, cacheObject, toggle, next, duration) {
|
|
|
|
if (toggle && !toggle(request, response)) {
|
|
|
|
if (toggle && !toggle(request, response)) {
|
|
|
|
return next();
|
|
|
|
return next();
|
|
|
@ -365,6 +428,13 @@ function ApiCache() {
|
|
|
|
return this.getIndex();
|
|
|
|
return this.getIndex();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Converts a duration string to an integer number of milliseconds.
|
|
|
|
|
|
|
|
* @param {string} duration - The string to convert.
|
|
|
|
|
|
|
|
* @returns {number} The converted value in milliseconds, or the defaultDuration if it can't be parsed.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Generated by Trelent
|
|
|
|
|
|
|
|
*/
|
|
|
|
function parseDuration(duration, defaultDuration) {
|
|
|
|
function parseDuration(duration, defaultDuration) {
|
|
|
|
if (typeof duration === "number") {
|
|
|
|
if (typeof duration === "number") {
|
|
|
|
return duration;
|
|
|
|
return duration;
|
|
|
|