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 Apr 30, 2017
1 parent 08135aa commit 11108e1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Use `safe-buffer` for improved Buffer API

2.4.2 / 2017-03-24
==================

Expand Down
16 changes: 2 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @private
*/

var Buffer = require('safe-buffer').Buffer
var etag = require('etag')
var fresh = require('fresh')
var fs = require('fs')
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
*
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 7 additions & 6 deletions test/test.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 favicon = require('..')
var http = require('http')
var path = require('path')
Expand All @@ -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 () {
Expand Down Expand Up @@ -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(), '####################')
Expand All @@ -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)
})
})
})
Expand Down

0 comments on commit 11108e1

Please sign in to comment.