@@ -22,8 +22,47 @@ 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 (
52+ Buffer . isBuffer ( data )
53+ && data . constructor
54+ && data . constructor . isBuffer
55+ && 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+ }
2766
2867 var outData = this . _update ( bufferData ) ;
2968 if ( this . hashMode ) {
0 commit comments