From 11108e1c75686a9aafce0fe01487cdce4ef0038d Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Sat, 29 Apr 2017 21:09:46 -0400 Subject: [PATCH] Use safe-buffer for improved Buffer API --- HISTORY.md | 5 +++++ index.js | 16 ++-------------- package.json | 3 ++- test/test.js | 13 +++++++------ 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 58f92ee..df7fe32 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +unreleased +========== + + * Use `safe-buffer` for improved Buffer API + 2.4.2 / 2017-03-24 ================== diff --git a/index.js b/index.js index 2162abd..727013d 100644 --- a/index.js +++ b/index.js @@ -13,6 +13,7 @@ * @private */ +var Buffer = require('safe-buffer').Buffer var etag = require('etag') var fresh = require('fresh') var fs = require('fs') @@ -55,7 +56,7 @@ function favicon (path, options) { } if (Buffer.isBuffer(path)) { - icon = createIcon(copyBuffer(path), maxAge) + icon = createIcon(Buffer.from(path), maxAge) } else if (typeof path === 'string') { path = resolveSync(path) } else { @@ -107,19 +108,6 @@ function calcMaxAge (val) { : ONE_YEAR_MS } -/** - * Copy a given Buffer. - * - * @param {Buffer} buf - * @private - */ - -function copyBuffer (buf) { - var copy = new Buffer(buf.length) - buf.copy(copy) - return copy -} - /** * Create icon data from Buffer and max-age. * diff --git a/package.json b/package.json index 8a052f2..c8f7985 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "etag": "~1.8.0", "fresh": "0.5.0", "ms": "1.0.0", - "parseurl": "~1.3.1" + "parseurl": "~1.3.1", + "safe-buffer": "5.0.1" }, "devDependencies": { "eslint": "3.19.0", diff --git a/test/test.js b/test/test.js index d493476..a30a7c6 100644 --- a/test/test.js +++ b/test/test.js @@ -1,5 +1,6 @@ var assert = require('assert') +var Buffer = require('safe-buffer').Buffer var favicon = require('..') var http = require('http') var path = require('path') @@ -21,7 +22,7 @@ describe('favicon()', function () { }) it('should accept buffer', function () { - assert.doesNotThrow(favicon.bind(null, new Buffer(20))) + assert.doesNotThrow(favicon.bind(null, Buffer.alloc(20))) }) it('should exist', function () { @@ -260,17 +261,17 @@ describe('favicon()', function () { describe('buffer', function () { it('should be served from buffer', function (done) { - var buffer = new Buffer('####################') + var buffer = Buffer.alloc(20, '#') var server = createServer(buffer) request(server) .get('/favicon.ico') - .expect('Content-Length', buffer.length) + .expect('Content-Length', '20') .expect(200, buffer, done) }) it('should be copied', function (done) { - var buffer = new Buffer('####################') + var buffer = Buffer.alloc(20, '#') var server = createServer(buffer) assert.equal(buffer.toString(), '####################') @@ -279,8 +280,8 @@ describe('favicon()', function () { request(server) .get('/favicon.ico') - .expect('Content-Length', buffer.length) - .expect(200, new Buffer('####################'), done) + .expect('Content-Length', '20') + .expect(200, Buffer.from('####################'), done) }) }) })