From 9afed44b26ec8c10a02af3ee5a00cded0787da52 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Mon, 25 Aug 2014 00:13:55 -0400 Subject: [PATCH] Use etag to generate ETag header --- History.md | 5 +++++ lib/send.js | 7 ++++--- lib/utils.js | 24 ------------------------ package.json | 1 + 4 files changed, 10 insertions(+), 27 deletions(-) diff --git a/History.md b/History.md index 5ec9c60..886f51f 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +unreleased +========== + + * Use `etag` to generate `ETag` header + 0.8.5 / 2014-09-04 ================== diff --git a/lib/send.js b/lib/send.js index 4b4b9f1..3fdb00f 100644 --- a/lib/send.js +++ b/lib/send.js @@ -17,6 +17,7 @@ var escapeHtml = require('escape-html') , normalize = path.normalize , join = path.join , utils = require('./utils'); +var etag = require('etag') var EventEmitter = require('events').EventEmitter; var ms = require('ms'); var onFinished = require('on-finished') @@ -718,9 +719,9 @@ SendStream.prototype.setHeader = function setHeader(path, stat){ if (!res.getHeader('Last-Modified')) res.setHeader('Last-Modified', stat.mtime.toUTCString()); if (this._etag && !res.getHeader('ETag')) { - var etag = utils.etag(path, stat); - debug('etag %s', etag); - res.setHeader('ETag', etag); + var val = etag(stat) + debug('etag %s', val) + res.setHeader('ETag', val) } }; diff --git a/lib/utils.js b/lib/utils.js index 3a88bf9..ef034b0 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,28 +1,4 @@ -/** - * Module dependencies. - */ - -var crypto = require('crypto'); - -/** - * Return a weak ETag from the given `path` and `stat`. - * - * @param {String} path - * @param {Object} stat - * @return {String} - * @api private - */ - -exports.etag = function etag(path, stat) { - var tag = String(stat.mtime.getTime()) + ':' + String(stat.size) + ':' + path; - var str = crypto - .createHash('md5') - .update(tag, 'utf8') - .digest('base64'); - return 'W/"' + str + '"'; -}; - /** * decodeURIComponent. * diff --git a/package.json b/package.json index f15a736..9b9f335 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "depd": "0.4.4", "destroy": "1.0.3", "escape-html": "1.0.1", + "etag": "~1.3.0", "fresh": "0.2.2", "mime": "1.2.11", "ms": "0.6.2",