Skip to content
Myr

Added Buffer ETag support to res.send()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent b79853e · Browse files · 2 files+41 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+41 added−7 removed

  • tests:+33 added−1 removed
  • other:+8 added−6 removed

2 files+41 added−7 removed

modified:lib/response.js+8 added-6 removed
16 unmodified lines
17 , normalizeType = require('./utils').normalizeType18 , normalizeTypes = require('./utils').normalizeTypes19 , statusCodes = http.STATUS_CODES20 , send = connect.static.send21 , crc32 = require('crc').crc3221 , crc = require('crc')22 , mime = require('mime')23 , basename = path.basename24 , extname = path.extname25 , join = path.join;
77 unmodified lines
103 }104105 // populate Content-Length106 if (undefined !== body && !this.get('Content-Length')) {107 len = Buffer.isBuffer(body) ? body.length : Buffer.byteLength(body);108 this.set('Content-Length', len);107 this.set('Content-Length', len = Buffer.isBuffer(body)108 ? body.length109 : Buffer.byteLength(body));110 }111112 // ETag support113 // TODO: W/ support113 // TODO: Buffer support for crc32114 if (len > 1024 && 'string' == typeof body) {115 this.set('ETag', crc32(body));114 if (len > 1024) {115 this.set('ETag', Buffer.isBuffer(body)116 ? crc.buffer.crc32(body)117 : crc.crc32(body));118 if (req.fresh) this.statusCode = 304;119 }120121 // strip irrelevant headers
531 unmodified lines
modified:test/res.send.js+33 added-1 removed
88 unmodified lines
89 res.statusCode.should.equal(200);90 done();91 })92 })9394 it('should set ETag', function(done){95 var app = express();9697 app.use(function(req, res){98 var str = Array(1024 * 2).join('-');99 res.send(str);100 });101102 request(app)103 .get('/')104 .end(function(res){105 res.headers.should.have.property('etag', '-1498647312');106 done();107 })108 })109 110 it('should not override Content-Type', function(done){111 var app = express();112
64 unmodified lines
177 res.statusCode.should.equal(200);178 done();179 })180 })165 181182 it('should set ETag', function(done){183 var app = express();184185 app.use(function(req, res){186 var str = Array(1024 * 2).join('-');187 res.send(new Buffer(str));188 });189190 request(app)191 .get('/')192 .end(function(res){193 res.headers.should.have.property('etag', '-1498647312');194 done();195 })196 })197198 it('should not override Content-Type', function(done){199 var app = express();200201 app.use(function(req, res){
87 unmodified lines