cache-headers
Advanced tools
Comparing version 0.0.2 to 0.0.3
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -13,7 +14,8 @@ * Date: 12/19/15 | ||
/** | ||
* @memberof additionalHeaders | ||
* @param {object} options | ||
* @param {number} [options.maxAge] Additional time to add | ||
* @param {object} [options.testDate] A test date object | ||
* @param {string} [options.formatType] @see module:utils#format | ||
* @return string | ||
* @param {string} [options.formatType] {@link module:utils#formatDate} | ||
* @return {{ name: string, value: string }} | ||
*/ | ||
@@ -37,4 +39,8 @@ function generateExpiresHeader() { | ||
/** | ||
* @param {string} lastModified | ||
* @return string | ||
* @memberof additionalHeaders | ||
* @alias module:additionalHeaders.generateLastModifiedHeader | ||
* @param {string} options | ||
* @param {object} options.date | ||
* @param {string} [options.formatType] | ||
* @return {{ name: string, value: string }} | ||
*/ | ||
@@ -54,2 +60,6 @@ function generateLastModifiedHeader() { | ||
/** | ||
* @module additionalHeaders | ||
* @type {{generateExpiresHeader: generateExpiresHeader, generateLastModifiedHeader: generateLastModifiedHeader}} | ||
*/ | ||
module.exports = { | ||
@@ -56,0 +66,0 @@ generateExpiresHeader: generateExpiresHeader, |
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -12,2 +13,6 @@ * Date: 12/19/15 | ||
var timeValues = require('./timeValues'); | ||
/** | ||
* @memberof module:cacheControl | ||
* @type {Object} | ||
*/ | ||
var headerTypes = Object.freeze({ | ||
@@ -35,4 +40,5 @@ browser: { | ||
* If a string, and it is in a the `timeValues` map, return that time value | ||
* @private | ||
* @param {number|string} value | ||
* @returns {*} | ||
* @return {number|string} | ||
*/ | ||
@@ -70,11 +76,11 @@ function getTimeValue(value) { | ||
/** | ||
* All options can use a string value | ||
* All options can use a string value. See {@link module:timeValues} for all available values | ||
* @memberof module:cacheControl | ||
* @alias generate | ||
* @param {object} [options] Caching options | ||
* @param {number|string} [options.maxAge] The browser cache length | ||
* @param {number|string} [options.sMaxAge] The cdn cache length | ||
* @param {number|string} [options.staleRevalidate] | ||
* @param {number|string} [options.staleError] | ||
* @see module:timeValue | ||
* @returns {{name: string, value: string}} | ||
* @param {number|string} [options.maxAge=timeValues.TEN_MINUTES] The browser cache length | ||
* @param {number|string} [options.sMaxAge=false] The cdn cache length | ||
* @param {number|string} [options.staleRevalidate=false] Time when to refresh the content in the background | ||
* @param {number|string} [options.staleError=false] Time to allow for serving cache when there is an error from a back-end service | ||
* @return {{name: string, value: string}} | ||
*/ | ||
@@ -81,0 +87,0 @@ function generateCacheControl(options) { |
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -14,12 +15,22 @@ * Date: 12/19/15 | ||
var isEmpty = require('lodash.isempty'); | ||
var cacheControl = require('./cacheControl'); | ||
var _require = require('./additionalHeaders'); | ||
var _require = require('./cacheControl'); | ||
var generateExpiresHeader = _require.generateExpiresHeader; | ||
var generateLastModifiedHeader = _require.generateLastModifiedHeader; | ||
var headerTypes = _require.headerTypes; | ||
var generate = _require.generate; | ||
var _require2 = require('./additionalHeaders'); | ||
var generateExpiresHeader = _require2.generateExpiresHeader; | ||
var generateLastModifiedHeader = _require2.generateLastModifiedHeader; | ||
var utils = require('./utils'); | ||
var timeValues = require('./timeValues'); | ||
/** | ||
* @param {object} res The current response object | ||
* @param {object} headerData | ||
* @param {string} headerData.name The response header to use | ||
* @param {string} headerData.value The corresponding response header value | ||
*/ | ||
function setHeader(res, headerData) { | ||
@@ -30,7 +41,8 @@ res.setHeader(headerData.name, headerData.value); | ||
/** | ||
* {{@link module:cacheControl#generate}} for acceptable values | ||
* @memberof index | ||
* @param {object} [config] | ||
* @param {object} [config.cacheSettings=undefined] Cache settings to override the default `paths` settings | ||
* @see module:cacheControl#generate for acceptable values | ||
* @param {object} [config.paths] Cache settings with glob path patterns | ||
* @returns {Function} | ||
* @return {Function} | ||
*/ | ||
@@ -51,7 +63,7 @@ function middleware(config) { | ||
// override default cacheValue settings | ||
cacheValue = cacheControl.generate(cacheSettings).value; | ||
cacheValue = generate(cacheSettings).value; | ||
} else if (utils.isTrueObject(cacheValue)) { | ||
cacheValue = cacheControl.generate(cacheValue).value; | ||
cacheValue = generate(cacheValue).value; | ||
} else if (cacheValue === false) { | ||
cacheValue = cacheControl.generate({ maxAge: 0, sMaxAge: 0, setNoCache: true }).value; | ||
cacheValue = generate({ maxAge: 0, sMaxAge: 0, setNoCache: true }).value; | ||
} else if (utils.isNumberLike(cacheValue)) { | ||
@@ -61,5 +73,5 @@ // catch `0` before !cacheValue check | ||
cacheValue = Number(cacheValue); | ||
cacheValue = cacheControl.generate({ maxAge: cacheValue, sMaxAge: cacheValue }).value; | ||
cacheValue = generate({ maxAge: cacheValue, sMaxAge: cacheValue }).value; | ||
} else if (!cacheValue || isEmpty(cacheValue)) { | ||
cacheValue = cacheControl.generate().value; | ||
cacheValue = generate().value; | ||
} | ||
@@ -72,9 +84,12 @@ setHeader(res, { name: 'Cache-Control', value: cacheValue }); | ||
module.exports = { | ||
headerTypes: cacheControl.headerTypes, | ||
/** | ||
* @module index | ||
* @type {object} | ||
*/ | ||
module.exports = Object.assign({ | ||
headerTypes: headerTypes, | ||
setHeader: setHeader, | ||
middleware: middleware, | ||
timeValues: timeValues, | ||
generateExpiresHeader: generateExpiresHeader, | ||
generateLastModifiedHeader: generateLastModifiedHeader | ||
}; | ||
}, timeValues); |
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -3,0 +4,0 @@ * Date: 12/24/15 |
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -19,4 +20,6 @@ * Date: 12/19/15 | ||
// Mon, 21 Dec 2015 19:45:29 GMT | ||
// php: D, d M Y H:i:s | ||
/** | ||
* Possible date format output | ||
* @type {Object} | ||
*/ | ||
var dateFormats = Object.freeze({ | ||
@@ -29,6 +32,15 @@ // all numbers have leading zero | ||
function isTrueObject(obj) { | ||
return !Array.isArray(obj) && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object' && !isEmpty(obj); | ||
/** | ||
* @param {*} val The value to check if it is an actual object. Arrays are not considered objects in this case | ||
* @return {boolean} | ||
*/ | ||
function isTrueObject(val) { | ||
return !Array.isArray(val) && (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object' && !isEmpty(val); | ||
} | ||
/** | ||
* | ||
* @param {*} val The value to check if it is like a number ie. 100 and "100" would return true | ||
* @return {boolean} | ||
*/ | ||
function isNumberLike(val) { | ||
@@ -40,3 +52,4 @@ return isNumber(val) || regular.number.test(val); | ||
* @param {*} val Any JS object | ||
* @returns {string} | ||
* @private | ||
* @return {string} | ||
*/ | ||
@@ -49,3 +62,3 @@ function getType(val) { | ||
* @param {object} [time] A Date object | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
@@ -63,3 +76,4 @@ function createUnixTime(time) { | ||
* @param {object[]} [timestamps] An array of Dates | ||
* @returns {object} A Date object | ||
* @private | ||
* @return {object} A Date object | ||
*/ | ||
@@ -79,3 +93,3 @@ function getLatestTimestamp() { | ||
* @param {object} [time] Date object | ||
* @returns {object} moment object in UTC format | ||
* @return {object} moment object in UTC format | ||
*/ | ||
@@ -91,3 +105,3 @@ function getUtcTime(time) { | ||
* @param {string} [formatType='normal'] Primarily used for testing | ||
* @returns {string} header date string in GMT format | ||
* @return {string} header date string in GMT format | ||
*/ | ||
@@ -107,3 +121,2 @@ function formatDate(time) { | ||
* @param {number|string|object} time if an object, a Date object | ||
* @returns {*} | ||
* @return {Promise} | ||
@@ -130,5 +143,7 @@ */ | ||
/** | ||
* | ||
* Creates a wrapping promise of promises and only resolves | ||
* when all promises have been resolved | ||
* @param {object[]} values | ||
* @returns {Promise} | ||
* @private | ||
* @return {Promise} | ||
*/ | ||
@@ -145,4 +160,7 @@ function arrayOfTimestamps(values) { | ||
* Gets the last modified time of a list of files | ||
* Creates a wrapping promise of promises and only resolves | ||
* when all promises have been resolved | ||
* @param {object[]} files An array of file path strings | ||
* @returns {Promise} | ||
* @private | ||
* @return {Promise} | ||
*/ | ||
@@ -172,3 +190,4 @@ function arrayOfTimestampsFiles(files) { | ||
* @param {string} dirPath The directory to look into | ||
* @returns {Promise} | ||
* @private | ||
* @return {Promise} | ||
*/ | ||
@@ -199,5 +218,7 @@ function getTimestampFromDirectory(dirPath) { | ||
/** | ||
* | ||
* Gets the stats of the file. This checks whether it is an actual | ||
* file or a directory and delegates to other methods accordingly | ||
* @param {string} filePath | ||
* @returns {Promise} | ||
* @private | ||
* @return {Promise} | ||
*/ | ||
@@ -220,5 +241,10 @@ function checkTimestampFileType(filePath) { | ||
function getFileTimestamp(time) { | ||
/** | ||
* @param {string} filePath Path to the file | ||
* @private | ||
* @return {Promise} | ||
*/ | ||
function getFileTimestamp(filePath) { | ||
return new Promise(function (resolve) { | ||
return checkTimestampFileType(time).then(function (resolvedTime) { | ||
return checkTimestampFileType(filePath).then(function (resolvedTime) { | ||
resolve(resolvedTime); | ||
@@ -232,12 +258,38 @@ }).catch(function () { | ||
/** | ||
* All promises return a formatted date string to be used for response headers | ||
* in the format of `Mon, 21 Dec 2015 19:45:29 GMT` | ||
* @param {object[]|string|null|boolean} compare Array of timestamps or a single path to check the last modified time | ||
* @param {string} [formatType=normal] Typically used for testing. Values of `test` and `normal` are accepted | ||
* @return {Promise} | ||
*/ | ||
function getLastModified() { | ||
var compare = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0]; | ||
var formatType = arguments.length <= 1 || arguments[1] === undefined ? 'normal' : arguments[1]; | ||
return new Promise(function (resolve, reject) { | ||
if (Array.isArray(compare) && compare.length > 0) { | ||
return arrayOfTimestamps(compare).then(function (timestamps) { | ||
var latestTimestamp = getLatestTimestamp(timestamps); | ||
return resolve(formatDate(latestTimestamp, formatType)); | ||
}).catch(function (err) { | ||
return reject(err); | ||
}); | ||
} else if (getType(compare) === 'string' && compare !== '') { | ||
return getTimestamp(compare).then(function (timestamp) { | ||
resolve(formatDate(timestamp, formatType)); | ||
}).catch(function () { | ||
getFileTimestamp(compare).then(function (timestamp) { | ||
resolve(formatDate(timestamp, formatType)); | ||
}).catch(function (err) { | ||
return reject(err); | ||
}); | ||
}); | ||
} | ||
return resolve(formatDate(createUnixTime(), formatType)); | ||
}); | ||
} | ||
/** | ||
* @module utils | ||
* @type {{ | ||
* dateFormats: Object, | ||
* format: format, | ||
* getUtcTime: getUtcTime, | ||
* getTimestamp: getTimestamp, | ||
* createUnixTime: createUnixTime, | ||
* checkModTimes, | ||
* getLastModified | ||
* }} | ||
* @type {Object} | ||
*/ | ||
@@ -252,52 +304,3 @@ module.exports = { | ||
createUnixTime: createUnixTime, | ||
/** | ||
* @description If NULLs are found in modTimes array, returns FALSE | ||
* @param array modTimes | ||
* @return bool | ||
*/ | ||
checkModTimes: function checkModTimes() { | ||
var modTimes = arguments.length <= 0 || arguments[0] === undefined ? [null] : arguments[0]; | ||
var nulls = modTimes.filter(function (val) { | ||
return typeof val !== null; | ||
}); | ||
if (nulls.length === 0) { | ||
return true; | ||
} | ||
return false; | ||
}, | ||
/** | ||
* All promises return a formatted date string to be used for response headers | ||
* in the format of `Mon, 21 Dec 2015 19:45:29 GMT` | ||
* @param {object[]|string|null|boolean} compare Array of timestamps or a single path to check the last modified time | ||
* @param {string} [formatType=normal] Typically used for testing. Values of `test` and `normal` are accepted | ||
* @returns {Promise} | ||
*/ | ||
getLastModified: function getLastModified() { | ||
var compare = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0]; | ||
var formatType = arguments.length <= 1 || arguments[1] === undefined ? 'normal' : arguments[1]; | ||
return new Promise(function (resolve, reject) { | ||
if (Array.isArray(compare) && compare.length > 0) { | ||
return arrayOfTimestamps(compare).then(function (timestamps) { | ||
var latestTimestamp = getLatestTimestamp(timestamps); | ||
return resolve(formatDate(latestTimestamp, formatType)); | ||
}).catch(function (err) { | ||
return reject(err); | ||
}); | ||
} else if (getType(compare) === 'string' && compare !== '') { | ||
return getTimestamp(compare).then(function (timestamp) { | ||
resolve(formatDate(timestamp, formatType)); | ||
}).catch(function () { | ||
getFileTimestamp(compare).then(function (timestamp) { | ||
resolve(formatDate(timestamp, formatType)); | ||
}).catch(function (err) { | ||
return reject(err); | ||
}); | ||
}); | ||
} | ||
return resolve(formatDate(createUnixTime(), formatType)); | ||
}); | ||
} | ||
getLastModified: getLastModified | ||
}; |
{ | ||
"name": "cache-headers", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Generate browser and cdn cache header values", | ||
@@ -10,3 +10,7 @@ "main": "dist/index.js", | ||
"gitlint": "node_modules/.bin/dibslint --git --warnings -e", | ||
"prepublish": "npm run gitlint && npm test && npm run compile" | ||
"docs": "./node_modules/.bin/mr-doc -s src/ -o docs -n 'Cache Headers'", | ||
"updatedocs": "rm -rf ./docs && npm run docs && git add ./docs && git commit -n -m 'updated docs v${npm show . version}'", | ||
"generatedist": "./generate.sh compile dist", | ||
"generatedocs": "./generate.sh docs docs", | ||
"preversion": "npm run gitlint && npm test && npm run generatedocs && npm run generatedist" | ||
}, | ||
@@ -44,2 +48,3 @@ "pre-commit": { | ||
"mocha": "^2.3.3", | ||
"mr-doc": "^3.0.7", | ||
"pre-commit": "^1.1.2", | ||
@@ -46,0 +51,0 @@ "supertest": "^1.1.0" |
@@ -92,3 +92,3 @@ # Cache Headers | ||
Rather than set the original headers defined in the `paths` config in the app-level setup (for the ``/**/generic` path), this will output the following: `Cache-Control: max-age=2000` | ||
Rather than set the original headers defined in the `paths` config in the app-level setup (for the `/**/generic` path), this will output the following: `Cache-Control: max-age=2000` | ||
@@ -95,0 +95,0 @@ ## API |
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -13,7 +14,8 @@ * Date: 12/19/15 | ||
/** | ||
* @memberof additionalHeaders | ||
* @param {object} options | ||
* @param {number} [options.maxAge] Additional time to add | ||
* @param {object} [options.testDate] A test date object | ||
* @param {string} [options.formatType] @see module:utils#format | ||
* @return string | ||
* @param {string} [options.formatType] {@link module:utils#formatDate} | ||
* @return {{ name: string, value: string }} | ||
*/ | ||
@@ -33,4 +35,8 @@ function generateExpiresHeader(options = {}) { | ||
/** | ||
* @param {string} lastModified | ||
* @return string | ||
* @memberof additionalHeaders | ||
* @alias module:additionalHeaders.generateLastModifiedHeader | ||
* @param {string} options | ||
* @param {object} options.date | ||
* @param {string} [options.formatType] | ||
* @return {{ name: string, value: string }} | ||
*/ | ||
@@ -47,2 +53,6 @@ function generateLastModifiedHeader(options = {}) { | ||
/** | ||
* @module additionalHeaders | ||
* @type {{generateExpiresHeader: generateExpiresHeader, generateLastModifiedHeader: generateLastModifiedHeader}} | ||
*/ | ||
module.exports = { | ||
@@ -49,0 +59,0 @@ generateExpiresHeader, |
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -12,2 +13,6 @@ * Date: 12/19/15 | ||
const timeValues = require('./timeValues'); | ||
/** | ||
* @memberof module:cacheControl | ||
* @type {Object} | ||
*/ | ||
const headerTypes = Object.freeze({ | ||
@@ -35,4 +40,5 @@ browser: { | ||
* If a string, and it is in a the `timeValues` map, return that time value | ||
* @private | ||
* @param {number|string} value | ||
* @returns {*} | ||
* @return {number|string} | ||
*/ | ||
@@ -70,11 +76,11 @@ function getTimeValue(value) { | ||
/** | ||
* All options can use a string value | ||
* All options can use a string value. See {@link module:timeValues} for all available values | ||
* @memberof module:cacheControl | ||
* @alias generate | ||
* @param {object} [options] Caching options | ||
* @param {number|string} [options.maxAge] The browser cache length | ||
* @param {number|string} [options.sMaxAge] The cdn cache length | ||
* @param {number|string} [options.staleRevalidate] | ||
* @param {number|string} [options.staleError] | ||
* @see module:timeValue | ||
* @returns {{name: string, value: string}} | ||
* @param {number|string} [options.maxAge=timeValues.TEN_MINUTES] The browser cache length | ||
* @param {number|string} [options.sMaxAge=false] The cdn cache length | ||
* @param {number|string} [options.staleRevalidate=false] Time when to refresh the content in the background | ||
* @param {number|string} [options.staleError=false] Time to allow for serving cache when there is an error from a back-end service | ||
* @return {{name: string, value: string}} | ||
*/ | ||
@@ -81,0 +87,0 @@ function generateCacheControl(options) { |
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -14,3 +15,3 @@ * Date: 12/19/15 | ||
const isEmpty = require('lodash.isempty'); | ||
const cacheControl = require('./cacheControl'); | ||
const { headerTypes, generate } = require('./cacheControl'); | ||
const { generateExpiresHeader, generateLastModifiedHeader } = require('./additionalHeaders'); | ||
@@ -20,2 +21,8 @@ const utils = require('./utils'); | ||
/** | ||
* @param {object} res The current response object | ||
* @param {object} headerData | ||
* @param {string} headerData.name The response header to use | ||
* @param {string} headerData.value The corresponding response header value | ||
*/ | ||
function setHeader(res, headerData) { | ||
@@ -26,7 +33,8 @@ res.setHeader(headerData.name, headerData.value); | ||
/** | ||
* {{@link module:cacheControl#generate}} for acceptable values | ||
* @memberof index | ||
* @param {object} [config] | ||
* @param {object} [config.cacheSettings=undefined] Cache settings to override the default `paths` settings | ||
* @see module:cacheControl#generate for acceptable values | ||
* @param {object} [config.paths] Cache settings with glob path patterns | ||
* @returns {Function} | ||
* @return {Function} | ||
*/ | ||
@@ -45,7 +53,7 @@ function middleware(config) { | ||
// override default cacheValue settings | ||
cacheValue = cacheControl.generate(cacheSettings).value; | ||
cacheValue = generate(cacheSettings).value; | ||
} else if (utils.isTrueObject(cacheValue)) { | ||
cacheValue = cacheControl.generate(cacheValue).value; | ||
cacheValue = generate(cacheValue).value; | ||
} else if (cacheValue === false) { | ||
cacheValue = cacheControl.generate({ maxAge: 0, sMaxAge: 0, setNoCache: true }).value; | ||
cacheValue = generate({ maxAge: 0, sMaxAge: 0, setNoCache: true }).value; | ||
} else if (utils.isNumberLike(cacheValue)) { | ||
@@ -55,5 +63,5 @@ // catch `0` before !cacheValue check | ||
cacheValue = Number(cacheValue); | ||
cacheValue = cacheControl.generate({ maxAge: cacheValue, sMaxAge: cacheValue }).value; | ||
cacheValue = generate({ maxAge: cacheValue, sMaxAge: cacheValue }).value; | ||
} else if (!cacheValue || isEmpty(cacheValue)) { | ||
cacheValue = cacheControl.generate().value; | ||
cacheValue = generate().value; | ||
} | ||
@@ -66,9 +74,12 @@ setHeader(res, { name: 'Cache-Control', value: cacheValue }); | ||
module.exports = { | ||
headerTypes: cacheControl.headerTypes, | ||
/** | ||
* @module index | ||
* @type {object} | ||
*/ | ||
module.exports = Object.assign({ | ||
headerTypes, | ||
setHeader, | ||
middleware, | ||
timeValues, | ||
generateExpiresHeader, | ||
generateLastModifiedHeader | ||
}; | ||
}, timeValues); |
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -3,0 +4,0 @@ * Date: 12/24/15 |
152
src/utils.js
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -17,4 +18,6 @@ * Date: 12/19/15 | ||
// Mon, 21 Dec 2015 19:45:29 GMT | ||
// php: D, d M Y H:i:s | ||
/** | ||
* Possible date format output | ||
* @type {Object} | ||
*/ | ||
const dateFormats = Object.freeze({ | ||
@@ -27,6 +30,15 @@ // all numbers have leading zero | ||
function isTrueObject(obj) { | ||
return !Array.isArray(obj) && typeof obj === 'object' && !isEmpty(obj) ; | ||
/** | ||
* @param {*} val The value to check if it is an actual object. Arrays are not considered objects in this case | ||
* @return {boolean} | ||
*/ | ||
function isTrueObject(val) { | ||
return !Array.isArray(val) && typeof val === 'object' && !isEmpty(val) ; | ||
} | ||
/** | ||
* | ||
* @param {*} val The value to check if it is like a number ie. 100 and "100" would return true | ||
* @return {boolean} | ||
*/ | ||
function isNumberLike(val) { | ||
@@ -38,3 +50,4 @@ return isNumber(val) || regular.number.test(val); | ||
* @param {*} val Any JS object | ||
* @returns {string} | ||
* @private | ||
* @return {string} | ||
*/ | ||
@@ -47,3 +60,3 @@ function getType(val) { | ||
* @param {object} [time] A Date object | ||
* @returns {number} | ||
* @return {number} | ||
*/ | ||
@@ -61,3 +74,4 @@ function createUnixTime(time) { | ||
* @param {object[]} [timestamps] An array of Dates | ||
* @returns {object} A Date object | ||
* @private | ||
* @return {object} A Date object | ||
*/ | ||
@@ -75,3 +89,3 @@ function getLatestTimestamp(timestamps = []) { | ||
* @param {object} [time] Date object | ||
* @returns {object} moment object in UTC format | ||
* @return {object} moment object in UTC format | ||
*/ | ||
@@ -87,3 +101,3 @@ function getUtcTime(time) { | ||
* @param {string} [formatType='normal'] Primarily used for testing | ||
* @returns {string} header date string in GMT format | ||
* @return {string} header date string in GMT format | ||
*/ | ||
@@ -101,3 +115,2 @@ function formatDate(time, formatType = 'normal') { | ||
* @param {number|string|object} time if an object, a Date object | ||
* @returns {*} | ||
* @return {Promise} | ||
@@ -124,5 +137,7 @@ */ | ||
/** | ||
* | ||
* Creates a wrapping promise of promises and only resolves | ||
* when all promises have been resolved | ||
* @param {object[]} values | ||
* @returns {Promise} | ||
* @private | ||
* @return {Promise} | ||
*/ | ||
@@ -141,4 +156,7 @@ function arrayOfTimestamps(values) { | ||
* Gets the last modified time of a list of files | ||
* Creates a wrapping promise of promises and only resolves | ||
* when all promises have been resolved | ||
* @param {object[]} files An array of file path strings | ||
* @returns {Promise} | ||
* @private | ||
* @return {Promise} | ||
*/ | ||
@@ -170,3 +188,4 @@ function arrayOfTimestampsFiles(files) { | ||
* @param {string} dirPath The directory to look into | ||
* @returns {Promise} | ||
* @private | ||
* @return {Promise} | ||
*/ | ||
@@ -199,5 +218,7 @@ function getTimestampFromDirectory(dirPath) { | ||
/** | ||
* | ||
* Gets the stats of the file. This checks whether it is an actual | ||
* file or a directory and delegates to other methods accordingly | ||
* @param {string} filePath | ||
* @returns {Promise} | ||
* @private | ||
* @return {Promise} | ||
*/ | ||
@@ -220,5 +241,10 @@ function checkTimestampFileType(filePath) { | ||
function getFileTimestamp(time) { | ||
/** | ||
* @param {string} filePath Path to the file | ||
* @private | ||
* @return {Promise} | ||
*/ | ||
function getFileTimestamp(filePath) { | ||
return new Promise((resolve) => { | ||
return checkTimestampFileType(time) | ||
return checkTimestampFileType(filePath) | ||
.then(resolvedTime => { | ||
@@ -234,12 +260,37 @@ resolve(resolvedTime); | ||
/** | ||
* All promises return a formatted date string to be used for response headers | ||
* in the format of `Mon, 21 Dec 2015 19:45:29 GMT` | ||
* @param {object[]|string|null|boolean} compare Array of timestamps or a single path to check the last modified time | ||
* @param {string} [formatType=normal] Typically used for testing. Values of `test` and `normal` are accepted | ||
* @return {Promise} | ||
*/ | ||
function getLastModified(compare = null, formatType = 'normal') { | ||
return new Promise((resolve, reject) => { | ||
if (Array.isArray(compare) && compare.length > 0) { | ||
return arrayOfTimestamps(compare) | ||
.then(timestamps => { | ||
const latestTimestamp = getLatestTimestamp(timestamps); | ||
return resolve(formatDate(latestTimestamp, formatType)); | ||
}) | ||
.catch(err => reject(err)); | ||
} else if (getType(compare) === 'string' && compare !== '') { | ||
return getTimestamp(compare) | ||
.then(timestamp => { | ||
resolve(formatDate(timestamp, formatType)); | ||
}) | ||
.catch(() => { | ||
getFileTimestamp(compare) | ||
.then(timestamp => { | ||
resolve(formatDate(timestamp, formatType)); | ||
}) | ||
.catch(err => reject(err)); | ||
}); | ||
} | ||
return resolve(formatDate(createUnixTime(), formatType)); | ||
}); | ||
} | ||
/** | ||
* @module utils | ||
* @type {{ | ||
* dateFormats: Object, | ||
* format: format, | ||
* getUtcTime: getUtcTime, | ||
* getTimestamp: getTimestamp, | ||
* createUnixTime: createUnixTime, | ||
* checkModTimes, | ||
* getLastModified | ||
* }} | ||
* @type {Object} | ||
*/ | ||
@@ -254,48 +305,3 @@ module.exports = { | ||
createUnixTime, | ||
/** | ||
* @description If NULLs are found in modTimes array, returns FALSE | ||
* @param array modTimes | ||
* @return bool | ||
*/ | ||
checkModTimes(modTimes = [null]) { | ||
const nulls = modTimes.filter(val => { | ||
return typeof val !== null; | ||
}); | ||
if (nulls.length === 0) { | ||
return true; | ||
} | ||
return false; | ||
}, | ||
/** | ||
* All promises return a formatted date string to be used for response headers | ||
* in the format of `Mon, 21 Dec 2015 19:45:29 GMT` | ||
* @param {object[]|string|null|boolean} compare Array of timestamps or a single path to check the last modified time | ||
* @param {string} [formatType=normal] Typically used for testing. Values of `test` and `normal` are accepted | ||
* @returns {Promise} | ||
*/ | ||
getLastModified(compare = null, formatType = 'normal') { | ||
return new Promise((resolve, reject) => { | ||
if (Array.isArray(compare) && compare.length > 0) { | ||
return arrayOfTimestamps(compare) | ||
.then(timestamps => { | ||
const latestTimestamp = getLatestTimestamp(timestamps); | ||
return resolve(formatDate(latestTimestamp, formatType)); | ||
}) | ||
.catch(err => reject(err)); | ||
} else if (getType(compare) === 'string' && compare !== '') { | ||
return getTimestamp(compare) | ||
.then(timestamp => { | ||
resolve(formatDate(timestamp, formatType)); | ||
}) | ||
.catch(() => { | ||
getFileTimestamp(compare) | ||
.then(timestamp => { | ||
resolve(formatDate(timestamp, formatType)); | ||
}) | ||
.catch(err => reject(err)); | ||
}); | ||
} | ||
return resolve(formatDate(createUnixTime(), formatType)); | ||
}); | ||
} | ||
getLastModified | ||
}; |
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -3,0 +4,0 @@ * Date: 12/22/15 |
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -3,0 +4,0 @@ * Date: 12/22/15 |
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -11,2 +12,4 @@ * Date: 12/26/15 | ||
const cacheControl = require('../src'); | ||
const timeValues = require('../src/timeValues'); | ||
const assert = require('assert'); | ||
const express = require('express'); | ||
@@ -26,3 +29,3 @@ const supertest = require('supertest'); | ||
describe('cache control middleware', function () { | ||
describe('cache control', function () { | ||
@@ -37,2 +40,10 @@ let app; | ||
it('should have default cache time values', () => { | ||
// make sure default cache values are attached to main module export | ||
Object.keys(timeValues).forEach(val => { | ||
const expect = timeValues[val]; | ||
assert.deepEqual(cacheControl[val], expect); | ||
}); | ||
}); | ||
describe('application-level middleware', () => { | ||
@@ -39,0 +50,0 @@ |
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -3,0 +4,0 @@ * Date: 12/31/15 |
/** | ||
* @ignore | ||
* User: daletan | ||
@@ -3,0 +4,0 @@ * Date: 12/21/15 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
489899
45
2282
13
3
5
1