[Pkg-javascript-commits] [node-serve-favicon] 01/09: Imported Upstream version 2.1.5

Leo Iannacone l3on-guest at moszumanska.debian.org
Wed Oct 15 16:54:23 UTC 2014


This is an automated email from the git hooks/post-receive script.

l3on-guest pushed a commit to branch master
in repository node-serve-favicon.

commit d853165442af47fa8ab92b9d5f1c9ffd8d61fac4
Author: Leo Iannacone <l3on at ubuntu.com>
Date:   Wed Oct 15 18:23:44 2014 +0200

    Imported Upstream version 2.1.5
---
 .npmignore               |  3 ---
 History.md => HISTORY.md | 34 +++++++++++++++++++++++++++++
 README.md                | 51 +++++++++++++++++++++++++++++++------------
 index.js                 | 56 +++++++++++++++++++++++++++++++-----------------
 package.json             | 22 +++++++++++++------
 test/test.js             | 20 +++++++++++------
 6 files changed, 136 insertions(+), 50 deletions(-)

diff --git a/.npmignore b/.npmignore
deleted file mode 100644
index cd39b77..0000000
--- a/.npmignore
+++ /dev/null
@@ -1,3 +0,0 @@
-coverage/
-test/
-.travis.yml
diff --git a/History.md b/HISTORY.md
similarity index 51%
rename from History.md
rename to HISTORY.md
index a1bdd39..08c137c 100644
--- a/History.md
+++ b/HISTORY.md
@@ -1,3 +1,37 @@
+2.1.5 / 2014-09-24
+==================
+
+  * deps: etag@~1.4.0
+
+2.1.4 / 2014-09-15
+==================
+
+  * Fix content headers being sent in 304 response
+  * deps: etag@~1.3.1
+    - Improve ETag generation speed
+
+2.1.3 / 2014-09-07
+==================
+
+  * deps: fresh at 0.2.4
+
+2.1.2 / 2014-09-05
+==================
+
+  * deps: etag@~1.3.0
+    - Improve ETag generation speed
+
+2.1.1 / 2014-08-25
+==================
+
+  * Fix `ms` to be listed as a dependency
+
+2.1.0 / 2014-08-24
+==================
+
+  * Accept string for `maxAge` (converted by `ms`)
+  * Use `etag` to generate `ETag` header
+
 2.0.1 / 2014-06-05
 ==================
 
diff --git a/README.md b/README.md
index b7c6541..74fbf2d 100644
--- a/README.md
+++ b/README.md
@@ -1,14 +1,18 @@
 # serve-favicon
 
-[![NPM version](https://badge.fury.io/js/serve-favicon.svg)](http://badge.fury.io/js/serve-favicon)
-[![Build Status](https://travis-ci.org/expressjs/serve-favicon.svg?branch=master)](https://travis-ci.org/expressjs/serve-favicon)
-[![Coverage Status](https://img.shields.io/coveralls/expressjs/serve-favicon.svg?branch=master)](https://coveralls.io/r/expressjs/serve-favicon)
+[![NPM Version][npm-image]][npm-url]
+[![NPM Downloads][downloads-image]][downloads-url]
+[![Build Status][travis-image]][travis-url]
+[![Test Coverage][coveralls-image]][coveralls-url]
+[![Gittip][gittip-image]][gittip-url]
 
 Node.js middleware for serving a favicon.
 
 ## Install
 
-    npm install serve-favicon
+```bash
+npm install serve-favicon
+```
 
 ## API
 
@@ -17,15 +21,23 @@ Node.js middleware for serving a favicon.
 Create new middleware to serve a favicon from the given `path` to a favicon file.
 `path` may also be a `Buffer` of the icon to serve.
 
-#### options
+#### Options
+
+Serve favicon accepts these properties in the options object.
+
+##### maxAge
 
-  - `maxAge` - 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
 
-Typically this middleware will come very early in your stack (maybe even first) to avoid processing any other middleware if we already know the request is for `/favicon.ico`.
+Typically this middleware will come very early in your stack (maybe even first)
+to avoid processing any other middleware if we already know the request is for
+`/favicon.ico`.
 
-### express 3.x/4.x
+### express
 
 ```javascript
 var express = require('express');
@@ -55,21 +67,21 @@ app.listen(3000);
 
 ### vanilla http server
 
-This middleware can be used anywhere, even outside express/connect. It takes `req`, `res`, and `callback`.
+This middleware can be used anywhere, even outside express/connect. It takes
+`req`, `res`, and `callback`.
 
 ```javascript
 var http = require('http');
 var favicon = require('serve-favicon');
+var finalhandler = require('finalhandler');
 
 var _favicon = favicon(__dirname + '/public/favicon.ico');
 
 var server = http.createServer(function onRequest(req, res) {
+  var done = finalhandler(req, res);
+
   _favicon(req, res, function onNext(err) {
-    if (err) {
-      res.statusCode = 500;
-      res.end();
-      return;
-    }
+    if (err) return done(err);
 
     // continue to process the request here, etc.
 
@@ -84,3 +96,14 @@ server.listen(3000);
 ## License
 
 [MIT](LICENSE)
+
+[npm-image]: https://img.shields.io/npm/v/serve-favicon.svg?style=flat
+[npm-url]: https://npmjs.org/package/serve-favicon
+[travis-image]: https://img.shields.io/travis/expressjs/serve-favicon.svg?style=flat
+[travis-url]: https://travis-ci.org/expressjs/serve-favicon
+[coveralls-image]: https://img.shields.io/coveralls/expressjs/serve-favicon.svg?style=flat
+[coveralls-url]: https://coveralls.io/r/expressjs/serve-favicon?branch=master
+[downloads-image]: https://img.shields.io/npm/dm/serve-favicon.svg?style=flat
+[downloads-url]: https://npmjs.org/package/serve-favicon
+[gittip-image]: https://img.shields.io/gittip/dougwilson.svg?style=flat
+[gittip-url]: https://www.gittip.com/dougwilson/
diff --git a/index.js b/index.js
index 4c44f0c..f453761 100644
--- a/index.js
+++ b/index.js
@@ -10,13 +10,20 @@
  * Module dependencies.
  */
 
-var crypto = require('crypto');
+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`.
  *
  * @param {String|Buffer} path
@@ -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');
@@ -70,14 +75,22 @@ 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,
     headers: {
-      'Content-Type': 'image/x-icon',
-      'Content-Length': buf.length,
       'Cache-Control': 'public, max-age=' + ~~(maxAge / 1000),
-      'etag': etag(buf)
+      'ETag': etag(buf)
     }
   };
 }
@@ -91,19 +104,22 @@ function createIsDirError(path) {
   return error;
 }
 
-function etag(buf){
-  var hash = crypto
-    .createHash('md5')
-    .update(buf)
-    .digest('base64');
-  return '"' + hash + '"';
-}
+function send(req, res, icon) {
+  var headers = icon.headers;
 
-function send(req, res, icon){
-  var _fresh = fresh(req.headers, icon.headers);
-  var buf = _fresh ? '' : icon.body;
-  var status = _fresh ? 304 : 200;
+  // Set headers
+  for (var header in headers) {
+    res.setHeader(header, headers[header]);
+  }
+
+  if (fresh(req.headers, res._headers)) {
+    res.statusCode = 304;
+    res.end();
+    return;
+  }
 
-  res.writeHead(status, icon.headers);
-  res.end(buf);
+  res.statusCode = 200;
+  res.setHeader('Content-Length', icon.body.length);
+  res.setHeader('Content-Type', 'image/x-icon');
+  res.end(icon.body);
 }
diff --git a/package.json b/package.json
index 10119b2..1e2122b 100644
--- a/package.json
+++ b/package.json
@@ -1,30 +1,38 @@
 {
   "name": "serve-favicon",
   "description": "favicon serving middleware with caching",
-  "version": "2.0.1",
+  "version": "2.1.5",
   "author": "Douglas Christopher Wilson <doug at somethingdoug.com>",
   "license": "MIT",
   "keywords": [
+    "express",
     "favicon",
     "middleware"
   ],
   "repository": "expressjs/serve-favicon",
   "dependencies": {
-    "fresh": "0.2.2"
+    "etag": "~1.4.0",
+    "fresh": "0.2.4",
+    "ms": "0.6.2"
   },
   "devDependencies": {
-    "istanbul": "0.2.10",
-    "mocha": "~1.20.1",
+    "istanbul": "0.3.2",
+    "mocha": "~1.21.4",
     "proxyquire": "~1.0.1",
     "should": "~4.0.1",
     "supertest": "~0.13.0"
   },
+  "files": [
+    "LICENSE",
+    "HISTORY.md",
+    "index.js"
+  ],
   "engines": {
     "node": ">= 0.8.0"
   },
   "scripts": {
-    "test": "mocha --reporter dot test/",
-    "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot test/",
-    "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec test/"
+    "test": "mocha --reporter spec --bail --check-leaks test/",
+    "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
+    "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
   }
 }
diff --git a/test/test.js b/test/test.js
index bd3bb79..82c4292 100644
--- a/test/test.js
+++ b/test/test.js
@@ -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)
@@ -82,11 +90,11 @@ 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);
       })
 
@@ -94,7 +102,7 @@ describe('favicon()', function(){
         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);
       })
     })
@@ -142,7 +150,7 @@ describe('favicon()', function(){
     });
 
     it('should understand If-None-Match', function(done){
-      request(server.listen())
+      request(server)
       .get('/favicon.ico')
       .expect(200, function(err, res){
         if (err) return done(err);
@@ -180,7 +188,7 @@ describe('favicon()', function(){
       });
 
       it('should cache for second request', function(done){
-        request(server.listen())
+        request(server)
         .get('/favicon.ico')
         .expect(200, function(err){
           if (err) return done(err);
@@ -216,7 +224,7 @@ describe('favicon()', function(){
 
       it('should retry reading file after error', function(done){
         readFile.setNextError(new Error('oh no'));
-        request(server.listen())
+        request(server)
         .get('/favicon.ico')
         .expect(500, 'oh no', function(err){
           if (err) return done(err);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-serve-favicon.git



More information about the Pkg-javascript-commits mailing list