Skip to content

Commit fd1e5ee

Browse files
committed
[Refactor] use to-buffer
1 parent 08ba803 commit fd1e5ee

File tree

2 files changed

+3
-66
lines changed

2 files changed

+3
-66
lines changed

index.js

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var Buffer = require('safe-buffer').Buffer;
44
var Transform = require('stream').Transform;
55
var StringDecoder = require('string_decoder').StringDecoder;
66
var inherits = require('inherits');
7+
var toBuffer = require('to-buffer');
78

89
function CipherBase(hashMode) {
910
Transform.call(this);
@@ -22,71 +23,6 @@ function CipherBase(hashMode) {
2223
}
2324
inherits(CipherBase, Transform);
2425

25-
var useUint8Array = typeof Uint8Array !== 'undefined';
26-
var useArrayBuffer = typeof ArrayBuffer !== 'undefined'
27-
&& typeof Uint8Array !== 'undefined'
28-
&& ArrayBuffer.isView
29-
&& (Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT);
30-
31-
function toBuffer(data, encoding) {
32-
/*
33-
* No need to do anything for exact instance
34-
* This is only valid when safe-buffer.Buffer === buffer.Buffer, i.e. when Buffer.from/Buffer.alloc existed
35-
*/
36-
if (data instanceof Buffer) {
37-
return data;
38-
}
39-
40-
// Convert strings to Buffer
41-
if (typeof data === 'string') {
42-
return Buffer.from(data, encoding);
43-
}
44-
45-
/*
46-
* Wrap any TypedArray instances and DataViews
47-
* Makes sense only on engines with full TypedArray support -- let Buffer detect that
48-
*/
49-
if (useArrayBuffer && ArrayBuffer.isView(data)) {
50-
// Bug in Node.js <6.3.1, which treats this as out-of-bounds
51-
if (data.byteLength === 0) {
52-
return Buffer.alloc(0);
53-
}
54-
55-
var res = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
56-
/*
57-
* Recheck result size, as offset/length doesn't work on Node.js <5.10
58-
* We just go to Uint8Array case if this fails
59-
*/
60-
if (res.byteLength === data.byteLength) {
61-
return res;
62-
}
63-
}
64-
65-
/*
66-
* Uint8Array in engines where Buffer.from might not work with ArrayBuffer, just copy over
67-
* Doesn't make sense with other TypedArray instances
68-
*/
69-
if (useUint8Array && data instanceof Uint8Array) {
70-
return Buffer.from(data);
71-
}
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-
9026
CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
9127
var bufferData = toBuffer(data, inputEnc); // asserts correct input type
9228
var outData = this._update(bufferData);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"homepage": "https://github.com/crypto-browserify/cipher-base#readme",
3232
"dependencies": {
3333
"inherits": "^2.0.4",
34-
"safe-buffer": "^5.2.1"
34+
"safe-buffer": "^5.2.1",
35+
"to-buffer": "^1.2.2"
3536
},
3637
"devDependencies": {
3738
"@ljharb/eslint-config": "^21.2.0",

0 commit comments

Comments
 (0)