@@ -104,7 +104,6 @@ const {
104104 isAnyArrayBuffer,
105105 isArrayBufferView,
106106 isUint8Array,
107- isTypedArray,
108107} = require ( 'internal/util/types' ) ;
109108const {
110109 inspect : utilInspect ,
@@ -154,6 +153,14 @@ FastBuffer.prototype.constructor = Buffer;
154153Buffer . prototype = FastBuffer . prototype ;
155154addBufferPrototypeMethods ( Buffer . prototype ) ;
156155
156+ const BUFFER_NAMES = [
157+ 'ArrayBuffer' ,
158+ 'Buffer' ,
159+ 'TypedArray' ,
160+ 'DataView' ,
161+ 'SharedArrayBuffer' ,
162+ ] ;
163+
157164const constants = ObjectDefineProperties ( { } , {
158165 MAX_LENGTH : {
159166 __proto__ : null ,
@@ -232,10 +239,15 @@ function toInteger(n, defaultVal) {
232239}
233240
234241function copyImpl ( source , target , targetStart , sourceStart , sourceEnd ) {
235- if ( ! ArrayBufferIsView ( source ) )
236- throw new ERR_INVALID_ARG_TYPE ( 'source' , [ 'Buffer' , 'Uint8Array' ] , source ) ;
237- if ( ! ArrayBufferIsView ( target ) )
238- throw new ERR_INVALID_ARG_TYPE ( 'target' , [ 'Buffer' , 'Uint8Array' ] , target ) ;
242+ if ( ! isArrayBufferView ( source ) && ! isAnyArrayBuffer ( source ) )
243+ throw new ERR_INVALID_ARG_TYPE ( 'source' , BUFFER_NAMES , source ) ;
244+ if ( isArrayBufferView ( target ) ) {
245+ // Do nothing..
246+ } else if ( isAnyArrayBuffer ( target ) ) {
247+ target = new Uint8Array ( target ) ;
248+ } else {
249+ throw new ERR_INVALID_ARG_TYPE ( 'target' , BUFFER_NAMES , target ) ;
250+ }
239251
240252 if ( targetStart === undefined ) {
241253 targetStart = 0 ;
@@ -359,25 +371,31 @@ Buffer.from = function from(value, encodingOrOffset, length) {
359371
360372 throw new ERR_INVALID_ARG_TYPE (
361373 'first argument' ,
362- [ 'string' , 'Buffer' , 'ArrayBuffer' , ' Array', 'Array-like Object' ] ,
374+ [ 'string' , 'Array' , 'Array-like Object' , ... BUFFER_NAMES ] ,
363375 value ,
364376 ) ;
365377} ;
366378
367379/**
368380 * Creates the Buffer as a copy of the underlying ArrayBuffer of the view
369381 * rather than the contents of the view.
370- * @param {TypedArray } view
382+ * @param {Buffer| TypedArray|DataView } view
371383 * @param {number } [offset]
372384 * @param {number } [length]
373385 * @returns {Buffer }
374386 */
375387Buffer . copyBytesFrom = function copyBytesFrom ( view , offset , length ) {
376- if ( ! isTypedArray ( view ) ) {
377- throw new ERR_INVALID_ARG_TYPE ( 'view' , [ 'TypedArray' ] , view ) ;
388+ if ( isArrayBufferView ( view ) ) {
389+ // Do nothing..
390+ } else if ( isAnyArrayBuffer ( view ) ) {
391+ view = new Uint8Array ( view ) ;
392+ } else {
393+ throw new ERR_INVALID_ARG_TYPE ( 'view' , BUFFER_NAMES , view ) ;
378394 }
379395
380- const viewLength = TypedArrayPrototypeGetLength ( view ) ;
396+ // view.length gives element count for TypedArrays, undefined for DataView.
397+ // Fall back to byteLength for DataView (elementSize will be 1).
398+ const viewLength = view . length ?? view . byteLength ;
381399 if ( viewLength === 0 ) {
382400 return new FastBuffer ( ) ;
383401 }
@@ -399,14 +417,14 @@ Buffer.copyBytesFrom = function copyBytesFrom(view, offset, length) {
399417
400418 if ( end <= start ) return new FastBuffer ( ) ;
401419
402- const viewByteLength = TypedArrayPrototypeGetByteLength ( view ) ;
420+ const viewByteLength = view . byteLength ;
403421 const elementSize = viewByteLength / viewLength ;
404- const srcByteOffset = TypedArrayPrototypeGetByteOffset ( view ) +
422+ const srcByteOffset = view . byteOffset +
405423 start * elementSize ;
406424 const srcByteLength = ( end - start ) * elementSize ;
407425
408426 return fromArrayLike ( new Uint8Array (
409- TypedArrayPrototypeGetBuffer ( view ) ,
427+ view . buffer ,
410428 srcByteOffset ,
411429 srcByteLength ) ) ;
412430} ;
@@ -620,12 +638,17 @@ Buffer.concat = function concat(list, length) {
620638 if ( length === undefined ) {
621639 length = 0 ;
622640 for ( let i = 0 ; i < list . length ; i ++ ) {
623- const buf = list [ i ] ;
624- if ( ! isUint8Array ( buf ) ) {
641+ let buf = list [ i ] ;
642+ if ( isUint8Array ( buf ) ) {
643+ // Do nothing..
644+ } else if ( isArrayBufferView ( buf ) || isAnyArrayBuffer ( buf ) ) {
645+ buf = list [ i ] = new Uint8Array ( buf . buffer ?? buf , buf . byteOffset ?? 0 ,
646+ buf . byteLength ) ;
647+ } else {
625648 // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
626649 // Instead, find the proper error code for this.
627650 throw new ERR_INVALID_ARG_TYPE (
628- `list[${ i } ]` , [ 'Buffer' , 'Uint8Array' ] , buf ) ;
651+ `list[${ i } ]` , BUFFER_NAMES , buf ) ;
629652 }
630653 length += TypedArrayPrototypeGetByteLength ( buf ) ;
631654 }
@@ -647,11 +670,17 @@ Buffer.concat = function concat(list, length) {
647670
648671 validateOffset ( length , 'length' ) ;
649672 for ( let i = 0 ; i < list . length ; i ++ ) {
650- if ( ! isUint8Array ( list [ i ] ) ) {
673+ if ( isUint8Array ( list [ i ] ) ) {
674+ // Do nothing..
675+ } else if ( isArrayBufferView ( list [ i ] ) || isAnyArrayBuffer ( list [ i ] ) ) {
676+ const buf = list [ i ] ;
677+ list [ i ] = new Uint8Array ( buf . buffer ?? buf , buf . byteOffset ?? 0 ,
678+ buf . byteLength ) ;
679+ } else {
651680 // TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
652681 // Instead, find the proper error code for this.
653682 throw new ERR_INVALID_ARG_TYPE (
654- `list[${ i } ]` , [ 'Buffer' , 'Uint8Array' ] , list [ i ] ) ;
683+ `list[${ i } ]` , BUFFER_NAMES , list [ i ] ) ;
655684 }
656685 }
657686
@@ -834,10 +863,7 @@ function byteLength(string, encoding) {
834863 if ( isArrayBufferView ( string ) || isAnyArrayBuffer ( string ) ) {
835864 return string . byteLength ;
836865 }
837-
838- throw new ERR_INVALID_ARG_TYPE (
839- 'string' , [ 'string' , 'Buffer' , 'ArrayBuffer' ] , string ,
840- ) ;
866+ throw new ERR_INVALID_ARG_TYPE ( 'string' , [ 'string' , ...BUFFER_NAMES ] , string ) ;
841867 }
842868
843869 const len = string . length ;
@@ -926,15 +952,18 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
926952} ;
927953
928954Buffer . prototype . equals = function equals ( otherBuffer ) {
929- if ( ! isUint8Array ( otherBuffer ) ) {
930- throw new ERR_INVALID_ARG_TYPE (
931- 'otherBuffer' , [ 'Buffer' , 'Uint8Array' ] , otherBuffer ) ;
955+ if ( isArrayBufferView ( otherBuffer ) ) {
956+ // Do nothing..
957+ } else if ( isAnyArrayBuffer ( otherBuffer ) ) {
958+ otherBuffer = new Uint8Array ( otherBuffer ) ;
959+ } else {
960+ throw new ERR_INVALID_ARG_TYPE ( 'otherBuffer' , BUFFER_NAMES , otherBuffer ) ;
932961 }
933962
934963 if ( this === otherBuffer )
935964 return true ;
936965 const len = TypedArrayPrototypeGetByteLength ( this ) ;
937- if ( len !== TypedArrayPrototypeGetByteLength ( otherBuffer ) )
966+ if ( len !== otherBuffer . byteLength )
938967 return false ;
939968
940969 return len === 0 || _compare ( this , otherBuffer ) === 0 ;
@@ -988,9 +1017,14 @@ Buffer.prototype.compare = function compare(target,
9881017 targetEnd ,
9891018 sourceStart ,
9901019 sourceEnd ) {
991- if ( ! isUint8Array ( target ) ) {
992- throw new ERR_INVALID_ARG_TYPE ( 'target' , [ 'Buffer' , 'Uint8Array' ] , target ) ;
1020+ if ( isArrayBufferView ( target ) ) {
1021+ // Do nothing..
1022+ } else if ( isAnyArrayBuffer ( target ) ) {
1023+ target = new Uint8Array ( target ) ;
1024+ } else {
1025+ throw new ERR_INVALID_ARG_TYPE ( 'target' , BUFFER_NAMES , target ) ;
9931026 }
1027+
9941028 if ( arguments . length === 1 )
9951029 return _compare ( this , target ) ;
9961030
@@ -1066,14 +1100,14 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
10661100 return ops . indexOf ( buffer , val , byteOffset , dir ) ;
10671101 }
10681102
1069- if ( isUint8Array ( val ) ) {
1103+ if ( isArrayBufferView ( val ) || isAnyArrayBuffer ( val ) ) {
10701104 const encodingVal =
10711105 ( ops === undefined ? encodingsMap . utf8 : ops . encodingVal ) ;
10721106 return indexOfBuffer ( buffer , val , byteOffset , encodingVal , dir ) ;
10731107 }
10741108
10751109 throw new ERR_INVALID_ARG_TYPE (
1076- 'value' , [ 'number' , 'string' , 'Buffer' , 'Uint8Array' ] , val ,
1110+ 'value' , [ 'number' , 'string' , ... BUFFER_NAMES ] , val ,
10771111 ) ;
10781112}
10791113
@@ -1244,7 +1278,9 @@ Buffer.prototype.subarray = function subarray(start, end) {
12441278 start = adjustOffset ( start , srcLength ) ;
12451279 end = end !== undefined ? adjustOffset ( end , srcLength ) : srcLength ;
12461280 const newLength = end > start ? end - start : 0 ;
1247- return new FastBuffer ( this . buffer , this . byteOffset + start , newLength ) ;
1281+ return new FastBuffer ( TypedArrayPrototypeGetBuffer ( this ) ,
1282+ TypedArrayPrototypeGetByteOffset ( this ) + start ,
1283+ newLength ) ;
12481284} ;
12491285
12501286Buffer . prototype . slice = function slice ( start , end ) {
@@ -1328,9 +1364,8 @@ if (internalBinding('config').hasIntl) {
13281364 // Transcodes the Buffer from one encoding to another, returning a new
13291365 // Buffer instance.
13301366 transcode = function transcode ( source , fromEncoding , toEncoding ) {
1331- if ( ! isUint8Array ( source ) ) {
1332- throw new ERR_INVALID_ARG_TYPE ( 'source' ,
1333- [ 'Buffer' , 'Uint8Array' ] , source ) ;
1367+ if ( ! ArrayBufferIsView ( source ) && ! isAnyArrayBuffer ( source ) ) {
1368+ throw new ERR_INVALID_ARG_TYPE ( 'source' , BUFFER_NAMES , source ) ;
13341369 }
13351370 if ( source . length === 0 ) return new FastBuffer ( ) ;
13361371
@@ -1386,19 +1421,19 @@ function atob(input) {
13861421}
13871422
13881423function isUtf8 ( input ) {
1389- if ( isTypedArray ( input ) || isAnyArrayBuffer ( input ) ) {
1424+ if ( isArrayBufferView ( input ) || isAnyArrayBuffer ( input ) ) {
13901425 return bindingIsUtf8 ( input ) ;
13911426 }
13921427
1393- throw new ERR_INVALID_ARG_TYPE ( 'input' , [ 'ArrayBuffer' , 'Buffer' , 'TypedArray' ] , input ) ;
1428+ throw new ERR_INVALID_ARG_TYPE ( 'input' , BUFFER_NAMES , input ) ;
13941429}
13951430
13961431function isAscii ( input ) {
1397- if ( isTypedArray ( input ) || isAnyArrayBuffer ( input ) ) {
1432+ if ( isArrayBufferView ( input ) || isAnyArrayBuffer ( input ) ) {
13981433 return bindingIsAscii ( input ) ;
13991434 }
14001435
1401- throw new ERR_INVALID_ARG_TYPE ( 'input' , [ 'ArrayBuffer' , 'Buffer' , 'TypedArray' ] , input ) ;
1436+ throw new ERR_INVALID_ARG_TYPE ( 'input' , BUFFER_NAMES , input ) ;
14021437}
14031438
14041439module . exports = {
0 commit comments