Skip to content
Myr

Use safe-buffer for improved Buffer API

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 fa272ed · Browse files · 8 files+19 added−17 removed
This commit

State

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

Diff stats

8 files+19 added−17 removed

  • tests:+11 added−11 removed
  • other:+8 added−6 removed

8 files+19 added−17 removed

modified:benchmarks/middleware.js+1 added-3 removed
modified:lib/response.js+3 added-2 removed
10 unmodified lines
11 * Module dependencies.12 * @private13 */1415var Buffer = require('safe-buffer').Buffer16var contentDisposition = require('content-disposition');17var deprecate = require('depd')('express');18var encodeUrl = require('encodeurl');19var escapeHtml = require('escape-html');
75 unmodified lines
95 * Send a response.96 *97 * Examples:98 *98 * res.send(new Buffer('wahoo'));99 * res.send(Buffer.from('wahoo'));100 * res.send({ some: 'json' });101 * res.send('<p>some html</p>');102 *103 * @param {string|number|boolean|object|Buffer} body
78 unmodified lines
182 // just calculate length when no ETag + small chunk183 len = Buffer.byteLength(chunk, encoding)184 } else if (!Buffer.isBuffer(chunk)) {185 // convert chunk to Buffer and calculate185 chunk = new Buffer(chunk, encoding);186 chunk = Buffer.from(chunk, encoding)187 encoding = undefined;188 len = chunk.length189 } else {190 // get length of Buffer
896 unmodified lines
modified:lib/utils.js+2 added-1 removed
10 unmodified lines
11 * Module dependencies.12 * @api private13 */1415var Buffer = require('safe-buffer').Buffer16var contentDisposition = require('content-disposition');17var contentType = require('content-type');18var deprecate = require('depd')('express');19var flatten = require('array-flatten');
253 unmodified lines
273274function createETagGenerator (options) {275 return function generateETag (body, encoding) {276 var buf = !Buffer.isBuffer(body)276 ? new Buffer(body, encoding)277 ? Buffer.from(body, encoding)278 : body279280 return etag(buf, options)281 }
25 unmodified lines
modified:test/res.attachment.js+2 added-1 removed
modified:test/res.send.js+6 added-6 removed
12var Buffer = require('safe-buffer').Buffer3var express = require('..');4var methods = require('methods');5var request = require('supertest');6var utils = require('./support/utils');
159 unmodified lines
166 it('should keep charset in Content-Type for Buffers', function(done){167 var app = express();168169 app.use(function(req, res){169 res.set('Content-Type', 'text/plain; charset=iso-8859-1').send(new Buffer('hi'));170 res.set('Content-Type', 'text/plain; charset=iso-8859-1').send(Buffer.from('hi'))171 });172173 request(app)174 .get('/')
6 unmodified lines
181 it('should send as octet-stream', function(done){182 var app = express();183184 app.use(function(req, res){184 res.send(new Buffer('hello'));185 res.send(Buffer.from('hello'))186 });187188 request(app)189 .get('/')
4 unmodified lines
194 it('should set ETag', function (done) {195 var app = express();196197 app.use(function (req, res) {197 var str = Array(1000).join('-');198 res.send(new Buffer(str));198 res.send(Buffer.alloc(999, '-'))199 });200201 request(app)202 .get('/')
4 unmodified lines
207 it('should not override Content-Type', function(done){208 var app = express();209210 app.use(function(req, res){211 res.set('Content-Type', 'text/plain').send(new Buffer('hey'));211 res.set('Content-Type', 'text/plain').send(Buffer.from('hey'))212 });213214 request(app)215 .get('/')
295 unmodified lines
511 var app = express();512513 app.set('etag', function (body, encoding) {514 var chunk = !Buffer.isBuffer(body)515 ? new Buffer(body, encoding)515 ? Buffer.from(body, encoding)516 : body;517 chunk.toString().should.equal('hello, world!');518 return '"custom"';519 });
30 unmodified lines
modified:test/utils.js+3 added-4 removed
modified:History.md+1 added
1 unmodified line
2==========34 * Improve error message when autoloading invalid view engine5 * Skip `Buffer` encoding when not generating ETag for small response6 * Use `safe-buffer` for improved Buffer API7 * deps: accepts@~1.3.48 - deps: mime-types@~2.1.169 * deps: content-type@~1.0.410 - perf: remove argument reassignment
3330 unmodified lines
modified:package.json+1 added
46 unmodified lines
47 "path-to-regexp": "0.1.7",48 "proxy-addr": "~2.0.2",49 "qs": "6.5.1",50 "range-parser": "~1.2.0",51 "safe-buffer": "5.1.1",52 "send": "0.15.6",53 "serve-static": "1.12.6",54 "setprototypeof": "1.1.0",55 "statuses": "~1.3.1",
43 unmodified lines