Skip to content

Commit efaf142

Browse files
committed
fix: io.js 3.0 - Node.js 5.3 typed array support
1 parent f03cebf commit efaf142

File tree

1 file changed

+52
-30
lines changed

1 file changed

+52
-30
lines changed

index.js

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,64 @@ var useArrayBuffer = typeof ArrayBuffer !== 'undefined'
2828
&& ArrayBuffer.isView
2929
&& (Buffer.prototype instanceof Uint8Array || Buffer.TYPED_ARRAY_SUPPORT);
3030

31-
CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
32-
var bufferData;
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+
*/
3336
if (data instanceof Buffer) {
34-
// No need to do anything
35-
bufferData = data;
36-
} else if (typeof data === 'string') {
37-
// Convert strings to Buffer
38-
bufferData = Buffer.from(data, inputEnc);
39-
} else if (useArrayBuffer && ArrayBuffer.isView(data)) {
40-
/*
41-
* Wrap any TypedArray instances and DataViews
42-
* Makes sense only on engines with full TypedArray support -- let Buffer detect that
43-
*/
44-
bufferData = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
45-
} else if (useUint8Array && data instanceof Uint8Array) {
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 v5.12, 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);
4656
/*
47-
* Uint8Array in engines where Buffer.from might not work with ArrayBuffer, just copy over
48-
* Doesn't make sense with other TypedArray instances
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
4959
*/
50-
bufferData = Buffer.from(data);
51-
} else if (
52-
Buffer.isBuffer(data)
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 (Buffer.isBuffer(data)
5379
&& data.constructor
54-
&& data.constructor.isBuffer
80+
&& typeof data.constructor.isBuffer === 'function'
5581
&& data.constructor.isBuffer(data)
56-
) {
57-
/*
58-
* Old Buffer polyfill on an engine that doesn't have TypedArray support
59-
* Also, this is from a different Buffer polyfill implementation then we have, as instanceof check failed
60-
* Convert to our current Buffer implementation
61-
*/
62-
bufferData = Buffer.from(data);
63-
} else {
64-
throw new Error('The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView.');
65-
}
82+
) { return Buffer.from(data); }
6683

84+
throw new TypeError('The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView.');
85+
}
86+
87+
CipherBase.prototype.update = function (data, inputEnc, outputEnc) {
88+
var bufferData = toBuffer(data, inputEnc); // asserts correct input type
6789
var outData = this._update(bufferData);
6890
if (this.hashMode) {
6991
return this;

0 commit comments

Comments
 (0)