Skip to content
Myr

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

5 files+23 added−33 removed

modified:lib/utils.js+12 added-23 removed
1 unmodified line
2/**3 * Module dependencies.4 */56var crc = require('crc').crc32;7var mime = require('connect').mime8 , proxyaddr = require('proxy-addr')9 , crypto = require('crypto');6var etag = require('etag');7var mime = require('connect').mime;8var proxyaddr = require('proxy-addr');9var typer = require('media-typer');1011/**12 * toString ref.
9 unmodified lines
22 * @return {String}23 * @api private24 */2527exports.etag = function etag(body, encoding){28 if (body.length === 0) {29 // fast-path empty body30 return '"1B2M2Y8AsgTpgAmY7PhCfg=="'31 }26exports.etag = function (body, encoding) {27 var buf = !Buffer.isBuffer(body)28 ? new Buffer(body, encoding)29 : body;3033 var hash = crypto34 .createHash('md5')35 .update(body, encoding)36 .digest('base64')37 return '"' + hash + '"'31 return etag(buf, {weak: false});32};3334/**35 * Return weak ETag for `body`.
4 unmodified lines
40 * @api private41 */4243exports.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;4755 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};5051/**52 * Make `locals()` bound to the given `obj`.
355 unmodified lines
modified:test/res.send.js+6 added-6 removed
118 unmodified lines
119 });120121 request(app)122 .get('/')123 .expect('ETag', 'W/"7ff-2796319984"')123 .expect('ETag', 'W/"fz/jGo0ONwzb+aKy/rWipg=="')124 .end(done);125 })126127 it('should not set ETag for non-GET/HEAD', function(done){
80 unmodified lines
208 });209210 request(app)211 .get('/')212 .expect('ETag', 'W/"7ff-2796319984"')212 .expect('ETag', 'W/"fz/jGo0ONwzb+aKy/rWipg=="')213 .end(done);214 })215216 it('should not override Content-Type', function(done){
107 unmodified lines
324 });325326 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 })331332 it('should not perform freshness check unless 2xx or 304', function(done){
37 unmodified lines
370 app.enable('etag');371372 request(app)373 .get('/')374 .expect('etag', 'W/"c-1525560792"', done)374 .expect('etag', 'W/"c-5aee35d8"', done)375 })376377 it('should send ETag for empty string response', function(done){378 var app = express()
20 unmodified lines
399 app.enable('etag');400401 request(app)402 .get('/')403 .expect('etag', 'W/"7ff-2796319984"', done)403 .expect('etag', 'W/"fz/jGo0ONwzb+aKy/rWipg=="', done)404 });405406 it('should not override ETag when manually set', function(done){407 var app = express();
90 unmodified lines
498 });499500 request(app)501 .get('/')502 .expect('etag', 'W/"d-1486392595"', done)502 .expect('etag', 'W/"d-58988d13"', done)503 })504 })505506 describe('when a function', function(){
39 unmodified lines
modified:test/utils.js+3 added-3 removed
26 unmodified lines
2728describe('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 })3334 it('should support utf8 strings', function(){35 utils.wetag('express❤', 'utf8')36 .should.eql('W/"a-1751845617"')36 .should.eql('W/"a-686b0af1"')37 })3839 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 })4445 it('should support empty string', function(){46 utils.wetag('')
204 unmodified lines
modified:History.md+1 added
2 unmodified lines
34 * 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.0
1839 unmodified lines
modified:package.json+1 added-1 removed