Permalink
Browse files

Use safe-buffer for improved Buffer API

  • Loading branch information...
dougwilson committed Sep 28, 2017
1 parent fa272ed commit 12c37124689380837b24a7ed962432596237b440
Showing with 19 additions and 17 deletions.
  1. +1 −0 History.md
  2. +1 −3 benchmarks/middleware.js
  3. +3 −2 lib/response.js
  4. +2 −1 lib/utils.js
  5. +1 −0 package.json
  6. +2 −1 test/res.attachment.js
  7. +6 −6 test/res.send.js
  8. +3 −4 test/utils.js
View
@@ -3,6 +3,7 @@ unreleased
* Improve error message when autoloading invalid view engine
* Skip `Buffer` encoding when not generating ETag for small response
* Use `safe-buffer` for improved Buffer API
* deps: accepts@~1.3.4
- deps: mime-types@~2.1.16
* deps: content-type@~1.0.4
View
@@ -13,10 +13,8 @@ while (n--) {
});
}
var body = new Buffer('Hello World');
app.use(function(req, res, next){
res.send(body);
res.send('Hello World')
});
app.listen(3333);
View
@@ -12,6 +12,7 @@
* @private
*/
var Buffer = require('safe-buffer').Buffer
var contentDisposition = require('content-disposition');
var deprecate = require('depd')('express');
var encodeUrl = require('encodeurl');
@@ -95,7 +96,7 @@ res.links = function(links){
*
* Examples:
*
* res.send(new Buffer('wahoo'));
* res.send(Buffer.from('wahoo'));
* res.send({ some: 'json' });
* res.send('<p>some html</p>');
*
@@ -182,7 +183,7 @@ res.send = function send(body) {
len = Buffer.byteLength(chunk, encoding)
} else if (!Buffer.isBuffer(chunk)) {
// convert chunk to Buffer and calculate
chunk = new Buffer(chunk, encoding);
chunk = Buffer.from(chunk, encoding)
encoding = undefined;
len = chunk.length
} else {
View
@@ -12,6 +12,7 @@
* @api private
*/
var Buffer = require('safe-buffer').Buffer
var contentDisposition = require('content-disposition');
var contentType = require('content-type');
var deprecate = require('depd')('express');
@@ -273,7 +274,7 @@ exports.setCharset = function setCharset(type, charset) {
function createETagGenerator (options) {
return function generateETag (body, encoding) {
var buf = !Buffer.isBuffer(body)
? new Buffer(body, encoding)
? Buffer.from(body, encoding)
: body
return etag(buf, options)
View
@@ -48,6 +48,7 @@
"proxy-addr": "~2.0.2",
"qs": "6.5.1",
"range-parser": "~1.2.0",
"safe-buffer": "5.1.1",
"send": "0.15.6",
"serve-static": "1.12.6",
"setprototypeof": "1.1.0",
View
@@ -1,4 +1,5 @@
var Buffer = require('safe-buffer').Buffer
var express = require('../')
, request = require('supertest');
@@ -36,7 +37,7 @@ describe('res', function(){
app.use(function(req, res){
res.attachment('/path/to/image.png');
res.send(new Buffer(4));
res.send(Buffer.alloc(4, '.'))
});
request(app)
View
@@ -1,4 +1,5 @@
var Buffer = require('safe-buffer').Buffer
var express = require('..');
var methods = require('methods');
var request = require('supertest');
@@ -166,7 +167,7 @@ describe('res', function(){
var app = express();
app.use(function(req, res){
res.set('Content-Type', 'text/plain; charset=iso-8859-1').send(new Buffer('hi'));
res.set('Content-Type', 'text/plain; charset=iso-8859-1').send(Buffer.from('hi'))
});
request(app)
@@ -181,7 +182,7 @@ describe('res', function(){
var app = express();
app.use(function(req, res){
res.send(new Buffer('hello'));
res.send(Buffer.from('hello'))
});
request(app)
@@ -194,8 +195,7 @@ describe('res', function(){
var app = express();
app.use(function (req, res) {
var str = Array(1000).join('-');
res.send(new Buffer(str));
res.send(Buffer.alloc(999, '-'))
});
request(app)
@@ -208,7 +208,7 @@ describe('res', function(){
var app = express();
app.use(function(req, res){
res.set('Content-Type', 'text/plain').send(new Buffer('hey'));
res.set('Content-Type', 'text/plain').send(Buffer.from('hey'))
});
request(app)
@@ -512,7 +512,7 @@ describe('res', function(){
app.set('etag', function (body, encoding) {
var chunk = !Buffer.isBuffer(body)
? new Buffer(body, encoding)
? Buffer.from(body, encoding)
: body;
chunk.toString().should.equal('hello, world!');
return '"custom"';
View
@@ -1,5 +1,6 @@
var assert = require('assert');
var Buffer = require('safe-buffer').Buffer
var utils = require('../lib/utils');
describe('utils.etag(body, encoding)', function(){
@@ -14,8 +15,7 @@ describe('utils.etag(body, encoding)', function(){
})
it('should support buffer', function(){
var buf = new Buffer('express!')
utils.etag(buf)
utils.etag(Buffer.from('express!'))
.should.eql('"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"')
})
@@ -59,8 +59,7 @@ describe('utils.wetag(body, encoding)', function(){
})
it('should support buffer', function(){
var buf = new Buffer('express!')
utils.wetag(buf)
utils.wetag(Buffer.from('express!'))
.should.eql('W/"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"')
})

0 comments on commit 12c3712

Please sign in to comment.