Skip to content

Commit

Permalink
Accept string for maxAge
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Aug 25, 2014
1 parent 3df27fb commit 5cd483e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
@@ -1,6 +1,7 @@
unreleased
==========

* Accept string for `maxAge` (converted by `ms`)
* Use `etag` to generate `ETag` header

2.0.1 / 2014-06-05
Expand Down
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -27,7 +27,9 @@ Serve favicon accepts these properties in the options object.

##### maxAge

The `cache-control` `max-age` directive in `ms`, defaulting to 1 day.
The `cache-control` `max-age` directive in `ms`, defaulting to 1 day. This can
also be a string accepted by the [ms](https://www.npmjs.org/package/ms#readme)
module.

## Examples

Expand Down
21 changes: 18 additions & 3 deletions index.js
Expand Up @@ -13,9 +13,16 @@
var etag = require('etag');
var fresh = require('fresh');
var fs = require('fs');
var ms = require('ms');
var path = require('path');
var resolve = path.resolve;

/**
* Module variables.
*/

var maxMaxAge = 60 * 60 * 24 * 365 * 1000; // 1 year

/**
* Serves the favicon located by the given `path`.
*
Expand All @@ -30,9 +37,7 @@ module.exports = function favicon(path, options){

var buf;
var icon; // favicon cache
var maxAge = options.maxAge == null
? 86400000
: Math.min(Math.max(0, options.maxAge), 31556926000);
var maxAge = calcMaxAge(options.maxAge);
var stat;

if (!path) throw new TypeError('path to favicon.ico is required');
Expand Down Expand Up @@ -70,6 +75,16 @@ module.exports = function favicon(path, options){
};
};

function calcMaxAge(val) {
var num = typeof val === 'string'
? ms(val)
: val;

return num != null
? Math.min(Math.max(0, num), maxMaxAge)
: maxMaxAge
}

function createIcon(buf, maxAge) {
return {
body: buf,
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -17,6 +17,7 @@
"devDependencies": {
"istanbul": "0.3.0",
"mocha": "~1.21.4",
"ms": "0.6.2",

This comment has been minimized.

Copy link
@bajtos

bajtos Aug 25, 2014

ms should have been added to regular dependencies. This way the module cannot be loaded when installed without dev dependencies. See #13 for a fix.

This comment has been minimized.

Copy link
@dougwilson

dougwilson Aug 25, 2014

Author Contributor

Yep. This is one of those cases where the default Travis CI configuration does not help me notice it earlier :(

"proxyquire": "~1.0.1",
"should": "~4.0.1",
"supertest": "~0.13.0"
Expand Down
14 changes: 11 additions & 3 deletions test/test.js
Expand Up @@ -66,6 +66,14 @@ describe('favicon()', function(){
.expect(200, done);
})

it('should accept string', function(done){
var server = createServer(null, {maxAge: '30d'});
request(server)
.get('/favicon.ico')
.expect('Cache-Control', 'public, max-age=2592000')
.expect(200, done);
})

it('should be valid delta-seconds', function(done){
var server = createServer(null, {maxAge: 1234});
request(server)
Expand All @@ -82,19 +90,19 @@ describe('favicon()', function(){
.expect(200, done);
})

it('should ceil at 31556926', function(done){
it('should ceil at 1 year', function(done){
var server = createServer(null, {maxAge: 900000000000});
request(server)
.get('/favicon.ico')
.expect('Cache-Control', 'public, max-age=31556926')
.expect('Cache-Control', 'public, max-age=31536000')
.expect(200, done);
})

it('should accept Inifnity', function(done){
var server = createServer(null, {maxAge: Infinity});
request(server)
.get('/favicon.ico')
.expect('Cache-Control', 'public, max-age=31556926')
.expect('Cache-Control', 'public, max-age=31536000')
.expect(200, done);
})
})
Expand Down

0 comments on commit 5cd483e

Please sign in to comment.