Skip to content

Commit 41d738b

Browse files
committed
[Refactor] use to-buffer
1 parent d59c766 commit 41d738b

File tree

4 files changed

+24
-51
lines changed

4 files changed

+24
-51
lines changed

index.js

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22
var Buffer = require('safe-buffer').Buffer
3+
var toBuffer = require('./to-buffer')
34
var Transform = require('readable-stream').Transform
45
var inherits = require('inherits')
56

@@ -38,55 +39,6 @@ HashBase.prototype._flush = function (callback) {
3839
callback(error)
3940
}
4041

41-
var useUint8Array = typeof Uint8Array !== 'undefined'
42-
var useArrayBuffer = typeof ArrayBuffer !== 'undefined' &&
43-
typeof Uint8Array !== 'undefined' &&
44-
ArrayBuffer.isView &&
45-
(Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT)
46-
47-
function toBuffer (data, encoding) {
48-
// No need to do anything for exact instance
49-
// This is only valid when safe-buffer.Buffer === buffer.Buffer, i.e. when Buffer.from/Buffer.alloc existed
50-
if (data instanceof Buffer) return data
51-
52-
// Convert strings to Buffer
53-
if (typeof data === 'string') return Buffer.from(data, encoding)
54-
55-
/*
56-
* Wrap any TypedArray instances and DataViews
57-
* Makes sense only on engines with full TypedArray support -- let Buffer detect that
58-
*/
59-
if (useArrayBuffer && ArrayBuffer.isView(data)) {
60-
if (data.byteLength === 0) return Buffer.alloc(0) // Bug in Node.js <6.3.1, which treats this as out-of-bounds
61-
var res = Buffer.from(data.buffer, data.byteOffset, data.byteLength)
62-
// Recheck result size, as offset/length doesn't work on Node.js <5.10
63-
// We just go to Uint8Array case if this fails
64-
if (res.byteLength === data.byteLength) return res
65-
}
66-
67-
/*
68-
* Uint8Array in engines where Buffer.from might not work with ArrayBuffer, just copy over
69-
* Doesn't make sense with other TypedArray instances
70-
*/
71-
if (useUint8Array && data instanceof Uint8Array) return Buffer.from(data)
72-
73-
/*
74-
* Old Buffer polyfill on an engine that doesn't have TypedArray support
75-
* Also, this is from a different Buffer polyfill implementation then we have, as instanceof check failed
76-
* Convert to our current Buffer implementation
77-
*/
78-
if (
79-
Buffer.isBuffer(data) &&
80-
data.constructor &&
81-
typeof data.constructor.isBuffer === 'function' &&
82-
data.constructor.isBuffer(data)
83-
) {
84-
return Buffer.from(data)
85-
}
86-
87-
throw new TypeError('The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView.')
88-
}
89-
9042
HashBase.prototype.update = function (data, encoding) {
9143
if (this._finalized) throw new Error('Digest already called')
9244

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"dependencies": {
3131
"inherits": "^2.0.4",
3232
"readable-stream": "^2.3.8",
33-
"safe-buffer": "^5.2.1"
33+
"safe-buffer": "^5.2.1",
34+
"to-buffer": "^1.2.1"
3435
},
3536
"devDependencies": {
3637
"nyc": "^10.3.2",

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ test('HashBase#update', function (t) {
6868
var base = new HashBase(64)
6969
t.throws(function () {
7070
base.update(null)
71-
}, /^TypeError: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView.$/)
71+
}, /^TypeError: The "data" argument must be a string, a Buffer, a Uint8Array, or a DataView$/)
7272
t.end()
7373
})
7474

to-buffer.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
var Buffer = require('safe-buffer').Buffer
4+
var toBuffer = require('to-buffer')
5+
6+
var useUint8Array = typeof Uint8Array !== 'undefined'
7+
var useArrayBuffer = useUint8Array && typeof ArrayBuffer !== 'undefined'
8+
var isView = useArrayBuffer && ArrayBuffer.isView
9+
10+
module.exports = function (thing, encoding) {
11+
if (
12+
typeof thing === 'string' ||
13+
Buffer.isBuffer(thing) ||
14+
(useUint8Array && thing instanceof Uint8Array) ||
15+
(isView && isView(thing))
16+
) {
17+
return toBuffer(thing, encoding)
18+
}
19+
throw new TypeError('The "data" argument must be a string, a Buffer, a Uint8Array, or a DataView')
20+
}

0 commit comments

Comments
 (0)