Use etag module to generate ETag headers
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 0e56133 · Browse files · 5 files·+23 added−33 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+23 added−33 removed
- tests:+9 added−9 removed
- other:+14 added−24 removed
Provenance
- Commit
4d1ee23- Tree
792de3f- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
0e56133
5 files changed+23 added−33 removed
modified:lib/utils.js+12 added-23 removed
1 unmodified line2 /**3 * Module dependencies.4 */5 6-var crc = require('crc').crc32;7-var mime = require('connect').mime8- , proxyaddr = require('proxy-addr')9- , crypto = require('crypto');6+var etag = require('etag');7+var mime = require('connect').mime;8+var proxyaddr = require('proxy-addr');9 var typer = require('media-typer');10 11 /**12 * toString ref.9 unmodified lines22 * @return {String}23 * @api private24 */25 27-exports.etag = function etag(body, encoding){28- if (body.length === 0) {29- // fast-path empty body30- return '"1B2M2Y8AsgTpgAmY7PhCfg=="'31- }26+exports.etag = function (body, encoding) {27+ var buf = !Buffer.isBuffer(body)28+ ? new Buffer(body, encoding)29+ : body;30 33- var hash = crypto34- .createHash('md5')35- .update(body, encoding)36- .digest('base64')37- return '"' + hash + '"'31+ return etag(buf, {weak: false});32 };33 34 /**35 * Return weak ETag for `body`.4 unmodified lines40 * @api private41 */42 43 exports.wetag = function wetag(body, encoding){50- if (body.length === 0) {51- // fast-path empty body52- return 'W/"0-0"'53- }44+ var buf = !Buffer.isBuffer(body)45+ ? new Buffer(body, encoding)46+ : body;47 55- var buf = Buffer.isBuffer(body)56- ? body57- : new Buffer(body, encoding)58- var len = buf.length59- return 'W/"' + len.toString(16) + '-' + crc(buf) + '"'48+ return etag(buf, {weak: true});49 };50 51 /**52 * Make `locals()` bound to the given `obj`.355 unmodified linesmodified:test/res.send.js+6 added-6 removed
118 unmodified lines119 });120 121 request(app)122 .get('/')123- .expect('ETag', 'W/"7ff-2796319984"')123+ .expect('ETag', 'W/"fz/jGo0ONwzb+aKy/rWipg=="')124 .end(done);125 })126 127 it('should not set ETag for non-GET/HEAD', function(done){80 unmodified lines208 });209 210 request(app)211 .get('/')212- .expect('ETag', 'W/"7ff-2796319984"')212+ .expect('ETag', 'W/"fz/jGo0ONwzb+aKy/rWipg=="')213 .end(done);214 })215 216 it('should not override Content-Type', function(done){107 unmodified lines324 });325 326 request(app)327 .get('/')328- .set('If-None-Match', 'W/"7ff-2796319984"')328+ .set('If-None-Match', 'W/"fz/jGo0ONwzb+aKy/rWipg=="')329 .expect(304, done);330 })331 332 it('should not perform freshness check unless 2xx or 304', function(done){37 unmodified lines370 app.enable('etag');371 372 request(app)373 .get('/')374- .expect('etag', 'W/"c-1525560792"', done)374+ .expect('etag', 'W/"c-5aee35d8"', done)375 })376 377 it('should send ETag for empty string response', function(done){378 var app = express()20 unmodified lines399 app.enable('etag');400 401 request(app)402 .get('/')403- .expect('etag', 'W/"7ff-2796319984"', done)403+ .expect('etag', 'W/"fz/jGo0ONwzb+aKy/rWipg=="', done)404 });405 406 it('should not override ETag when manually set', function(done){407 var app = express();90 unmodified lines498 });499 500 request(app)501 .get('/')502- .expect('etag', 'W/"d-1486392595"', done)502+ .expect('etag', 'W/"d-58988d13"', done)503 })504 })505 506 describe('when a function', function(){39 unmodified linesmodified:test/utils.js+3 added-3 removed
26 unmodified lines27 28 describe('utils.wetag(body, encoding)', function(){29 it('should support strings', function(){30 utils.wetag('express!')31- .should.eql('W/"8-3098196679"')31+ .should.eql('W/"8-b8aabac7"')32 })33 34 it('should support utf8 strings', function(){35 utils.wetag('express❤', 'utf8')36- .should.eql('W/"a-1751845617"')36+ .should.eql('W/"a-686b0af1"')37 })38 39 it('should support buffer', function(){40 var buf = new Buffer('express!')41 utils.wetag(buf)42- .should.eql('W/"8-3098196679"');42+ .should.eql('W/"8-b8aabac7"');43 })44 45 it('should support empty string', function(){46 utils.wetag('')204 unmodified linesmodified:History.md+1 added
2 unmodified lines3 4 * Use `content-disposition` module for `res.attachment`/`res.download`5 - Sends standards-compliant `Content-Disposition` header6 - Full Unicode support7+ * Use `etag` module to generate `ETag` headers8 * deps: connect@2.27.09 - Use `http-errors` module for creating errors10 - Use `utils-merge` module for merging objects11 - deps: body-parser@~1.9.01839 unmodified linesmodified:package.json+1 added-1 removed
29 unmodified lines3031323334343536373839404140 unmodified lines29 unmodified lines "connect": "2.27.0", "content-disposition": "0.5.0", "commander": "1.3.2", "cookie-signature": "1.0.5", "crc": "3.0.0", "debug": "~2.1.0", "depd": "~1.0.0", "escape-html": "1.0.1", "etag": "~1.5.0", "fresh": "0.2.4", "media-typer": "0.3.0", "methods": "1.1.0", "mkdirp": "0.5.0",40 unmodified lines