cache-headers
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -27,4 +27,3 @@ /** | ||
var utcTime = utils.getUtcTime(testDate); | ||
var newTime = utcTime.add(maxAge); | ||
var newTime = utils.addTime({ date: testDate, timeToAdd: maxAge }); | ||
var value = utils.formatDate(newTime.toISOString(), formatType); | ||
@@ -64,4 +63,4 @@ | ||
module.exports = { | ||
generateExpiresHeader: generateExpiresHeader, | ||
generateLastModifiedHeader: generateLastModifiedHeader | ||
expires: generateExpiresHeader, | ||
lastModified: generateLastModifiedHeader | ||
}; |
@@ -21,7 +21,3 @@ /** | ||
var _require2 = require('./additionalHeaders'); | ||
var generateExpiresHeader = _require2.generateExpiresHeader; | ||
var generateLastModifiedHeader = _require2.generateLastModifiedHeader; | ||
var additionalHeaders = require('./additionalHeaders'); | ||
var utils = require('./utils'); | ||
@@ -31,11 +27,32 @@ var timeValues = require('./timeValues'); | ||
/** | ||
* This will either set a specific header or defer to using express' res.set() functionality | ||
* {{@link http://expressjs.com/en/api.html#res.set}} | ||
* @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 | ||
* @param {string} [headerData.name] The response header to use | ||
* @param {string} [headerData.value] The corresponding response header value | ||
*/ | ||
function setHeader(res, headerData) { | ||
res.setHeader(headerData.name, headerData.value); | ||
if (headerData.name && headerData.value) { | ||
res.setHeader(headerData.name, headerData.value); | ||
} else if (utils.isTrueObject(headerData)) { | ||
res.set(headerData); | ||
} | ||
} | ||
function setAdditionalHeaders() { | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
return function (req, res, next) { | ||
Object.keys(options).forEach(function (key) { | ||
if (typeof additionalHeaders[key] === 'function') { | ||
var option = options[key]; | ||
var headerData = additionalHeaders[key](option); | ||
setHeader(res, headerData); | ||
} | ||
}); | ||
next(); | ||
}; | ||
} | ||
/** | ||
@@ -88,6 +105,4 @@ * {{@link module:cacheControl#generate}} for acceptable values | ||
headerTypes: headerTypes, | ||
setHeader: setHeader, | ||
middleware: middleware, | ||
generateExpiresHeader: generateExpiresHeader, | ||
generateLastModifiedHeader: generateLastModifiedHeader | ||
setAdditionalHeaders: setAdditionalHeaders, | ||
middleware: middleware | ||
}, timeValues); |
@@ -19,2 +19,3 @@ /** | ||
var isEmpty = require('lodash.isempty'); | ||
var timeValues = require('./timeValues'); | ||
@@ -91,4 +92,5 @@ /** | ||
*/ | ||
function getUtcTime(time) { | ||
time = time || new Date(); | ||
function getUtcTime() { | ||
var time = arguments.length <= 0 || arguments[0] === undefined ? new Date() : arguments[0]; | ||
return moment.utc(time); | ||
@@ -98,2 +100,23 @@ } | ||
/** | ||
* | ||
* @param {object} options | ||
* @param {object} [options.time=new Date()] Date object | ||
* @param {number} [options.timeToAdd=timeValues.TEN_MINUTES] A number of time to add, defaults in seconds | ||
* @param {string} [options.timeFormat='s'] The time format based on momentjs {{@link http://momentjs.com/docs/#/manipulating/add/}} | ||
* @return {object} moment object in UTC format with additional time added | ||
*/ | ||
function addTime() { | ||
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; | ||
var _options$time = options.time; | ||
var time = _options$time === undefined ? new Date() : _options$time; | ||
var _options$timeToAdd = options.timeToAdd; | ||
var timeToAdd = _options$timeToAdd === undefined ? timeValues.TEN_MINUTES : _options$timeToAdd; | ||
var _options$timeFormat = options.timeFormat; | ||
var timeFormat = _options$timeFormat === undefined ? 's' : _options$timeFormat; | ||
var utcTime = getUtcTime(time); | ||
return utcTime.add(timeToAdd, timeFormat); | ||
} | ||
/** | ||
* Format a UTC Date value | ||
@@ -283,3 +306,3 @@ * @param {number} time UTC time format | ||
* @module utils | ||
* @type {Object} | ||
* @type {{dateFormats: dateFormats, isTrueObject: isTrueObject, isNumberLike: isNumberLike, formatDate: formatDate, getUtcTime: getUtcTime, getTimestamp: getTimestamp, createUnixTime: createUnixTime, getLastModified: getLastModified, addTime: addTime}} | ||
*/ | ||
@@ -294,3 +317,4 @@ module.exports = { | ||
createUnixTime: createUnixTime, | ||
getLastModified: getLastModified | ||
getLastModified: getLastModified, | ||
addTime: addTime | ||
}; |
{ | ||
"name": "cache-headers", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Generate browser and cdn cache header values", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -23,4 +23,3 @@ /** | ||
const { maxAge, testDate, formatType } = options; | ||
const utcTime = utils.getUtcTime(testDate); | ||
const newTime = utcTime.add(maxAge); | ||
const newTime = utils.addTime({ date: testDate, timeToAdd: maxAge }); | ||
const value = utils.formatDate(newTime.toISOString(), formatType); | ||
@@ -57,4 +56,4 @@ | ||
module.exports = { | ||
generateExpiresHeader, | ||
generateLastModifiedHeader | ||
expires: generateExpiresHeader, | ||
lastModified: generateLastModifiedHeader | ||
}; |
@@ -16,3 +16,3 @@ /** | ||
const { headerTypes, generate } = require('./cacheControl'); | ||
const { generateExpiresHeader, generateLastModifiedHeader } = require('./additionalHeaders'); | ||
const additionalHeaders = require('./additionalHeaders'); | ||
const utils = require('./utils'); | ||
@@ -22,11 +22,30 @@ const timeValues = require('./timeValues'); | ||
/** | ||
* This will either set a specific header or defer to using express' res.set() functionality | ||
* {{@link http://expressjs.com/en/api.html#res.set}} | ||
* @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 | ||
* @param {string} [headerData.name] The response header to use | ||
* @param {string} [headerData.value] The corresponding response header value | ||
*/ | ||
function setHeader(res, headerData) { | ||
res.setHeader(headerData.name, headerData.value); | ||
if (headerData.name && headerData.value) { | ||
res.setHeader(headerData.name, headerData.value); | ||
} else if (utils.isTrueObject(headerData)) { | ||
res.set(headerData); | ||
} | ||
} | ||
function setAdditionalHeaders(options = {}) { | ||
return (req, res, next) => { | ||
Object.keys(options).forEach(key => { | ||
if (typeof additionalHeaders[key] === 'function') { | ||
const option = options[key]; | ||
const headerData = additionalHeaders[key](option); | ||
setHeader(res, headerData); | ||
} | ||
}); | ||
next(); | ||
}; | ||
} | ||
/** | ||
@@ -77,6 +96,4 @@ * {{@link module:cacheControl#generate}} for acceptable values | ||
headerTypes, | ||
setHeader, | ||
middleware, | ||
generateExpiresHeader, | ||
generateLastModifiedHeader | ||
setAdditionalHeaders, | ||
middleware | ||
}, timeValues); |
@@ -17,2 +17,3 @@ /** | ||
const isEmpty = require('lodash.isempty'); | ||
const timeValues = require('./timeValues'); | ||
@@ -87,4 +88,3 @@ /** | ||
*/ | ||
function getUtcTime(time) { | ||
time = time || new Date(); | ||
function getUtcTime(time = new Date()) { | ||
return moment.utc(time); | ||
@@ -94,2 +94,19 @@ } | ||
/** | ||
* | ||
* @param {object} options | ||
* @param {object} [options.time=new Date()] Date object | ||
* @param {number} [options.timeToAdd=timeValues.TEN_MINUTES] A number of time to add, defaults in seconds | ||
* @param {string} [options.timeFormat='s'] The time format based on momentjs {{@link http://momentjs.com/docs/#/manipulating/add/}} | ||
* @return {object} moment object in UTC format with additional time added | ||
*/ | ||
function addTime(options = {}) { | ||
const { | ||
time = new Date(), | ||
timeToAdd = timeValues.TEN_MINUTES, | ||
timeFormat = 's' } = options; | ||
const utcTime = getUtcTime(time); | ||
return utcTime.add(timeToAdd, timeFormat); | ||
} | ||
/** | ||
* Format a UTC Date value | ||
@@ -284,3 +301,3 @@ * @param {number} time UTC time format | ||
* @module utils | ||
* @type {Object} | ||
* @type {{dateFormats: dateFormats, isTrueObject: isTrueObject, isNumberLike: isNumberLike, formatDate: formatDate, getUtcTime: getUtcTime, getTimestamp: getTimestamp, createUnixTime: createUnixTime, getLastModified: getLastModified, addTime: addTime}} | ||
*/ | ||
@@ -295,3 +312,4 @@ module.exports = { | ||
createUnixTime, | ||
getLastModified | ||
getLastModified, | ||
addTime | ||
}; |
@@ -12,3 +12,2 @@ /** | ||
const assert = require('assert'); | ||
const moment = require('moment'); | ||
const additionalHeaders = require('../src/additionalHeaders'); | ||
@@ -23,4 +22,3 @@ const utils = require('../src/utils'); | ||
const testDate = new Date(); | ||
const utcTime = moment.utc(testDate); | ||
const newTime = utcTime.add(maxAge); | ||
const newTime = utils.addTime({ date: testDate, timeToAdd: maxAge }); | ||
const value = utils.formatDate(newTime, formatType); | ||
@@ -31,3 +29,3 @@ const expect = { | ||
}; | ||
const actual = additionalHeaders.generateExpiresHeader({ | ||
const actual = additionalHeaders.expires({ | ||
maxAge, | ||
@@ -49,3 +47,3 @@ testDate, | ||
}; | ||
const actual = additionalHeaders.generateLastModifiedHeader({ | ||
const actual = additionalHeaders.lastModified({ | ||
date: testDate, | ||
@@ -66,3 +64,3 @@ formatType | ||
}; | ||
const actual = additionalHeaders.generateLastModifiedHeader({ | ||
const actual = additionalHeaders.lastModified({ | ||
date: testDate, | ||
@@ -69,0 +67,0 @@ formatType |
@@ -13,2 +13,3 @@ /** | ||
const timeValues = require('../src/timeValues'); | ||
const utils = require('../src/utils'); | ||
const assert = require('assert'); | ||
@@ -213,2 +214,28 @@ const express = require('express'); | ||
}); | ||
it('should setAdditionalHeaders (Expires, Last-Modified)', (done) => { | ||
const testDate = new Date(); | ||
const lastModFormatted = utils.formatDate(testDate, 'test'); | ||
const timeToAdd = cacheControl.ONE_DAY; | ||
const newDate = utils.addTime({ date: testDate, timeToAdd }); | ||
const expiresDate = utils.formatDate(newDate.toISOString(), 'test'); | ||
const options = { | ||
expires: { | ||
maxAge: timeToAdd, | ||
testDate: testDate, | ||
formatType: 'test' | ||
}, | ||
lastModified: { | ||
date: testDate, | ||
formatType: 'test' | ||
} | ||
}; | ||
app.use(cacheControl.setAdditionalHeaders(options)); | ||
agent | ||
.get('/') | ||
.expect('Expires', expiresDate) | ||
.expect('Last-Modified', lastModFormatted) | ||
.end(done); | ||
}); | ||
}); |
@@ -15,2 +15,3 @@ /** | ||
const utils = require('../src/utils'); | ||
const timeValues = require('../src/timeValues'); | ||
@@ -56,2 +57,26 @@ const EXPECT_FALSE = false; | ||
describe('addTime', () => { | ||
it('should add ten minutes to the current date (default settings)', () => { | ||
const date = new Date(); | ||
const utcTime = utils.getUtcTime(date); | ||
const expect = utcTime.add(timeValues.TEN_MINUTES, 's'); | ||
const actual = utils.addTime(); | ||
assert.strictEqual(actual.toString(), expect.toString()); | ||
}); | ||
it('should add the passed in amount of time to the date', () => { | ||
const date = new Date(); | ||
const utcTime = utils.getUtcTime(date); | ||
const expect = utcTime.add(timeValues.ONE_YEAR, 's'); | ||
const actual = utils.addTime({ date , timeToAdd: timeValues.ONE_YEAR }); | ||
assert.strictEqual(actual.toString(), expect.toString()); | ||
}); | ||
it('should add time based on days', () => { | ||
const date = new Date(); | ||
const utcTime = utils.getUtcTime(date); | ||
const expect = utcTime.add(7, 'd'); | ||
const actual = utils.addTime({ date , timeToAdd: 7, timeFormat: 'd' }); | ||
assert.strictEqual(actual.toString(), expect.toString()); | ||
}); | ||
}); | ||
describe('getTimestamp', () => { | ||
@@ -172,4 +197,4 @@ | ||
const testDate = new Date(); | ||
const utcTime = moment.utc(testDate); | ||
const newTime = utcTime.add(maxAge); | ||
const newTime = utils.addTime({ date: testDate, timeToAdd: maxAge }); | ||
const expiresDate = moment.utc(newTime.toISOString()); | ||
@@ -176,0 +201,0 @@ const format = utils.dateFormats.test; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
498160
2395