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
Provenance
- Commit
12c3712- Tree
6722894- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
fa272ed
8 files changed+19 added−17 removed
modified:benchmarks/middleware.js+1 added-3 removed
11 unmodified lines12131415161716191718192011 unmodified lines next(); });}
var body = new Buffer('Hello World');
app.use(function(req, res, next){ res.send(body); res.send('Hello World')});
app.listen(3333);modified:lib/response.js+3 added-2 removed
10 unmodified lines11 * Module dependencies.12 * @private13 */14 15+var Buffer = require('safe-buffer').Buffer16 var contentDisposition = require('content-disposition');17 var deprecate = require('depd')('express');18 var encodeUrl = require('encodeurl');19 var escapeHtml = require('escape-html');75 unmodified lines95 * 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} body78 unmodified lines182 // 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 Buffer896 unmodified linesmodified:lib/utils.js+2 added-1 removed
10 unmodified lines11 * Module dependencies.12 * @api private13 */14 15+var Buffer = require('safe-buffer').Buffer16 var contentDisposition = require('content-disposition');17 var contentType = require('content-type');18 var deprecate = require('depd')('express');19 var flatten = require('array-flatten');253 unmodified lines273 274 function 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 : body279 280 return etag(buf, options)281 }25 unmodified linesmodified:test/res.attachment.js+2 added-1 removed
12345629 unmodified lines3637383939404142434433 unmodified lines
var Buffer = require('safe-buffer').Buffervar express = require('../') , request = require('supertest');
describe('res', function(){29 unmodified lines var app = express();
app.use(function(req, res){ res.attachment('/path/to/image.png'); res.send(new Buffer(4)); res.send(Buffer.alloc(4, '.')) });
request(app) .get('/')33 unmodified linesmodified:test/res.send.js+6 added-6 removed
1 2+var Buffer = require('safe-buffer').Buffer3 var express = require('..');4 var methods = require('methods');5 var request = require('supertest');6 var utils = require('./support/utils');159 unmodified lines166 it('should keep charset in Content-Type for Buffers', function(done){167 var app = express();168 169 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 });172 173 request(app)174 .get('/')6 unmodified lines181 it('should send as octet-stream', function(done){182 var app = express();183 184 app.use(function(req, res){184- res.send(new Buffer('hello'));185+ res.send(Buffer.from('hello'))186 });187 188 request(app)189 .get('/')4 unmodified lines194 it('should set ETag', function (done) {195 var app = express();196 197 app.use(function (req, res) {197- var str = Array(1000).join('-');198- res.send(new Buffer(str));198+ res.send(Buffer.alloc(999, '-'))199 });200 201 request(app)202 .get('/')4 unmodified lines207 it('should not override Content-Type', function(done){208 var app = express();209 210 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 });213 214 request(app)215 .get('/')295 unmodified lines511 var app = express();512 513 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 linesmodified:test/utils.js+3 added-4 removed
12345676 unmodified lines141516171718181920212235 unmodified lines585960616263626364656629 unmodified lines
var assert = require('assert');var Buffer = require('safe-buffer').Buffervar utils = require('../lib/utils');
describe('utils.etag(body, encoding)', function(){ it('should support strings', function(){6 unmodified lines .should.eql('"a-JBiXf7GyzxwcrxY4hVXUwa7tmks"') })
it('should support buffer', function(){ var buf = new Buffer('express!') utils.etag(buf) utils.etag(Buffer.from('express!')) .should.eql('"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"') })
it('should support empty string', function(){35 unmodified lines .should.eql('W/"a-JBiXf7GyzxwcrxY4hVXUwa7tmks"') })
it('should support buffer', function(){ var buf = new Buffer('express!') utils.wetag(buf) utils.wetag(Buffer.from('express!')) .should.eql('W/"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"') })
it('should support empty string', function(){29 unmodified linesmodified:History.md+1 added
1 unmodified line2 ==========3 4 * 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 reassignment3330 unmodified linesmodified:package.json+1 added
46 unmodified lines47 "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