Skip to content

Commit

Permalink
Use safe-buffer for improved Buffer API
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 28, 2017
1 parent fa272ed commit 12c3712
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 17 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions benchmarks/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
5 changes: 3 additions & 2 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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>');
*
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion test/res.attachment.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

var Buffer = require('safe-buffer').Buffer
var express = require('../')
, request = require('supertest');

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions test/res.send.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

var Buffer = require('safe-buffer').Buffer
var express = require('..');
var methods = require('methods');
var request = require('supertest');
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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"';
Expand Down
7 changes: 3 additions & 4 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -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(){
Expand All @@ -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"')
})

Expand Down Expand Up @@ -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"')
})

Expand Down

0 comments on commit 12c3712

Please sign in to comment.