Skip to content

Only Node v4 or greater [Buffer only] #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
sudo: false
language: node_js
node_js:
- "0.10"
- "0.11"
- "0.12"
- "4"
- "5"
- "6"
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function throwIfNotStringOrBuffer (val, prefix) {
function HashBase (blockSize) {
Transform.call(this)

this._block = new Buffer(blockSize)
this._block = Buffer.allocUnsafe(blockSize)
this._blockSize = blockSize
this._blockOffset = 0
this._length = [0, 0, 0, 0]
Expand Down Expand Up @@ -46,7 +46,7 @@ HashBase.prototype._flush = function (callback) {
HashBase.prototype.update = function (data, encoding) {
throwIfNotStringOrBuffer(data, 'Data')
if (this._finalized) throw new Error('Digest already called')
if (!Buffer.isBuffer(data)) data = new Buffer(data, encoding)
if (!Buffer.isBuffer(data)) data = Buffer.from(data, encoding)

// consume data
var block = this._block
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@
"nyc": "^8.3.2",
"standard": "*",
"tape": "^4.2.0"
},
"engines": {
"node": ">=4"
}
}
17 changes: 8 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var test = require('tape')
var HashBase = require('../')

var utf8text = 'УТФ-8 text'
var utf8buf = new Buffer(utf8text, 'utf8')
var utf8buf = Buffer.from(utf8text, 'utf8')
function noop () {}

function createHashBase (t) { t.base = new HashBase(64) }
Expand Down Expand Up @@ -38,7 +38,7 @@ test('HashBase#_transform', function (t) {
t.plan(1)
var err = new Error('hey')
t.base.update = function () { throw err }
t.base._transform(new Buffer(0), 'buffer', function (_err) {
t.base._transform(Buffer.allocUnsafe(0), 'buffer', function (_err) {
t.true(_err === err)
})
t.end()
Expand All @@ -52,7 +52,7 @@ test('HashBase#_flush', function (t) {

t.test('should use HashBase#digest', function (t) {
t.plan(2)
var buffer = new Buffer(0)
var buffer = Buffer.allocUnsafe(0)
t.base.push = function (data) { t.true(data === buffer) }
t.base.digest = function () { return buffer }
t.base._flush(function (err) { t.same(err, null) })
Expand Down Expand Up @@ -92,13 +92,13 @@ test('HashBase#update', function (t) {
t.test('should use HashBase#_update', function (t) {
t.plan(1)
t.base._update = t.pass
t.base.update(new Buffer(64))
t.base.update(Buffer.allocUnsafe(64))
t.end()
})

t.test('default encoding is utf8', function (t) {
t.plan(1)
var buffer = new Buffer(64)
var buffer = Buffer.allocUnsafe(64)
buffer.fill(0)
utf8buf.copy(buffer)
t.base._update = function () { t.same(this._block, buffer) }
Expand All @@ -108,22 +108,21 @@ test('HashBase#update', function (t) {

t.test('decode string with custom encoding', function (t) {
t.plan(1)
var buffer = new Buffer(64)
buffer.fill(0x42)
var buffer = Buffer.allocUnsafe(64).fill(0x42)
t.base._update = function () { t.same(this._block, buffer) }
t.base.update(buffer.toString('hex'), 'hex')
t.end()
})

t.test('data length is more than 2^32 bits', function (t) {
t.base._length = [ Math.pow(2, 32) - 1, 0, 0, 0 ]
t.base.update(new Buffer(1))
t.base.update(Buffer.allocUnsafe(1))
t.same(t.base._length, [ 7, 1, 0, 0 ])
t.end()
})

t.test('should return `this`', function (t) {
t.same(t.base.update(new Buffer(0)), t.base)
t.same(t.base.update(Buffer.allocUnsafe(0)), t.base)
t.end()
})

Expand Down