@@ -22,8 +22,42 @@ function CipherBase(hashMode) {
2222}
2323inherits ( CipherBase , Transform ) ;
2424
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+
2531CipherBase . prototype . update = function ( data , inputEnc , outputEnc ) {
26- var bufferData = typeof data === 'string' ? Buffer . from ( data , inputEnc ) : data ;
32+ var bufferData ;
33+ 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 ) {
46+ /*
47+ * Uint8Array in engines where Buffer.from might not work with ArrayBuffer, just copy over
48+ * Doesn't make sense with other TypedArray instances
49+ */
50+ bufferData = Buffer . from ( data ) ;
51+ } else if ( Buffer . isBuffer ( data ) && data . consructor && data . consructor . isBuffer && data . consructor . isBuffer ( data ) ) {
52+ /*
53+ * Old Buffer polyfill on an engine that doesn't have TypedArray support
54+ * Also, this is from a different Buffer polyfill implementation then we have, as instanceof check failed
55+ * Convert to our current Buffer implementation
56+ */
57+ bufferData = Buffer . from ( data ) ;
58+ } else {
59+ throw new Error ( 'The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView.' ) ;
60+ }
2761
2862 var outData = this . _update ( bufferData ) ;
2963 if ( this . hashMode ) {
0 commit comments