@@ -28,42 +28,67 @@ 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 <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 ) ;
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 (
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 (
5279 Buffer . isBuffer ( data )
53- && data . constructor
54- && data . constructor . isBuffer
55- && data . constructor . isBuffer ( data )
80+ && data . constructor
81+ && typeof data . constructor . isBuffer === 'function'
82+ && data . constructor . isBuffer ( data )
5683 ) {
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.' ) ;
84+ return Buffer . from ( data ) ;
6585 }
6686
87+ throw new TypeError ( 'The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView.' ) ;
88+ }
89+
90+ CipherBase . prototype . update = function ( data , inputEnc , outputEnc ) {
91+ var bufferData = toBuffer ( data , inputEnc ) ; // asserts correct input type
6792 var outData = this . _update ( bufferData ) ;
6893 if ( this . hashMode ) {
6994 return this ;
0 commit comments