@@ -133,6 +133,15 @@ FastBuffer.prototype.constructor = Buffer;
133
133
Buffer . prototype = FastBuffer . prototype ;
134
134
addBufferPrototypeMethods ( Buffer . prototype ) ;
135
135
136
+ const BUFFER_NAMES = [
137
+ 'ArrayBuffer' ,
138
+ 'Buffer' ,
139
+ 'TypedArray' ,
140
+ 'DataView' ,
141
+ 'ArrayBuffer' ,
142
+ 'SharedArrayBuffer' ,
143
+ ]
144
+
136
145
const constants = ObjectDefineProperties ( { } , {
137
146
MAX_LENGTH : {
138
147
__proto__ : null ,
@@ -203,10 +212,13 @@ function toInteger(n, defaultVal) {
203
212
}
204
213
205
214
function copyImpl ( source , target , targetStart , sourceStart , sourceEnd ) {
206
- if ( ! ArrayBufferIsView ( source ) )
207
- throw new ERR_INVALID_ARG_TYPE ( 'source' , [ 'Buffer' , 'Uint8Array' ] , source ) ;
208
- if ( ! ArrayBufferIsView ( target ) )
209
- throw new ERR_INVALID_ARG_TYPE ( 'target' , [ 'Buffer' , 'Uint8Array' ] , target ) ;
215
+ if ( isArrayBufferView ( target ) ) {
216
+ // Do nothing..
217
+ } else if ( isAnyArrayBuffer ( target ) ) {
218
+ target = new Uint8Array ( target ) ;
219
+ } else {
220
+ throw new ERR_INVALID_ARG_TYPE ( 'target' , BUFFER_NAMES , target ) ;
221
+ }
210
222
211
223
if ( targetStart === undefined ) {
212
224
targetStart = 0 ;
@@ -321,25 +333,29 @@ Buffer.from = function from(value, encodingOrOffset, length) {
321
333
322
334
throw new ERR_INVALID_ARG_TYPE (
323
335
'first argument' ,
324
- [ 'string' , 'Buffer' , 'ArrayBuffer' , ' Array', 'Array-like Object' ] ,
336
+ [ 'string' , 'Array' , 'Array-like Object' , ... BUFFER_NAMES ] ,
325
337
value ,
326
338
) ;
327
339
} ;
328
340
329
341
/**
330
342
* Creates the Buffer as a copy of the underlying ArrayBuffer of the view
331
343
* rather than the contents of the view.
332
- * @param {TypedArray } view
344
+ * @param {Buffer| TypedArray|DataView } view
333
345
* @param {number } [offset]
334
346
* @param {number } [length]
335
347
* @returns {Buffer }
336
348
*/
337
349
Buffer . copyBytesFrom = function copyBytesFrom ( view , offset , length ) {
338
- if ( ! isTypedArray ( view ) ) {
339
- throw new ERR_INVALID_ARG_TYPE ( 'view' , [ 'TypedArray' ] , view ) ;
350
+ if ( isArrayBufferView ( view ) ) {
351
+ // Do nothing..
352
+ } else if ( isAnyArrayBuffer ( view ) ) {
353
+ view = new Uint8Array ( view ) ;
354
+ } else {
355
+ throw new ERR_INVALID_ARG_TYPE ( 'view' , BUFFER_NAMES , view ) ;
340
356
}
341
357
342
- const viewLength = TypedArrayPrototypeGetLength ( view ) ;
358
+ const viewLength = view . byteLength ;
343
359
if ( viewLength === 0 ) {
344
360
return Buffer . alloc ( 0 ) ;
345
361
}
@@ -351,21 +367,17 @@ Buffer.copyBytesFrom = function copyBytesFrom(view, offset, length) {
351
367
} else {
352
368
offset = 0 ;
353
369
}
354
- let end ;
355
370
if ( length !== undefined ) {
356
371
validateInteger ( length , 'length' , 0 ) ;
357
- end = offset + length ;
358
372
} else {
359
- end = viewLength ;
373
+ length = viewLength ;
360
374
}
361
-
362
- view = TypedArrayPrototypeSlice ( view , offset , end ) ;
363
375
}
364
376
365
377
return fromArrayLike ( new Uint8Array (
366
- TypedArrayPrototypeGetBuffer ( view ) ,
367
- TypedArrayPrototypeGetByteOffset ( view ) ,
368
- TypedArrayPrototypeGetByteLength ( view ) ) ) ;
378
+ view . buffer ,
379
+ view . byteOffset ,
380
+ length ) ) ;
369
381
} ;
370
382
371
383
// Identical to the built-in %TypedArray%.of(), but avoids using the deprecated
@@ -583,8 +595,8 @@ Buffer.concat = function concat(list, length) {
583
595
if ( length === undefined ) {
584
596
length = 0 ;
585
597
for ( let i = 0 ; i < list . length ; i ++ ) {
586
- if ( list [ i ] . length ) {
587
- length += list [ i ] . length ;
598
+ if ( list [ i ] . byteLength ) {
599
+ length += list [ i ] . byteLength ;
588
600
}
589
601
}
590
602
} else {
@@ -595,13 +607,13 @@ Buffer.concat = function concat(list, length) {
595
607
let pos = 0 ;
596
608
for ( let i = 0 ; i < list . length ; i ++ ) {
597
609
const buf = list [ i ] ;
598
- if ( ! isUint8Array ( buf ) ) {
610
+ if ( ! isArrayBufferView ( buf ) && ! isAnyArrayBuffer ( buf ) ) {
599
611
// TODO(BridgeAR): This should not be of type ERR_INVALID_ARG_TYPE.
600
612
// Instead, find the proper error code for this.
601
613
throw new ERR_INVALID_ARG_TYPE (
602
- `list[${ i } ]` , [ 'Buffer' , 'Uint8Array' ] , list [ i ] ) ;
614
+ `list[${ i } ]` , BUFFER_NAMES , list [ i ] ) ;
603
615
}
604
- pos += _copyActual ( buf , buffer , pos , 0 , buf . length ) ;
616
+ pos += _copyActual ( buf , buffer , pos , 0 , buf . byteLength ) ;
605
617
}
606
618
607
619
// Note: `length` is always equal to `buffer.length` at this point
@@ -773,9 +785,7 @@ function byteLength(string, encoding) {
773
785
return string . byteLength ;
774
786
}
775
787
776
- throw new ERR_INVALID_ARG_TYPE (
777
- 'string' , [ 'string' , 'Buffer' , 'ArrayBuffer' ] , string ,
778
- ) ;
788
+ throw new ERR_INVALID_ARG_TYPE ( 'string' , [ 'string' , ...BUFFER_NAMES ] , string ) ;
779
789
}
780
790
781
791
const len = string . length ;
@@ -864,15 +874,18 @@ Buffer.prototype.toString = function toString(encoding, start, end) {
864
874
} ;
865
875
866
876
Buffer . prototype . equals = function equals ( otherBuffer ) {
867
- if ( ! isUint8Array ( otherBuffer ) ) {
868
- throw new ERR_INVALID_ARG_TYPE (
869
- 'otherBuffer' , [ 'Buffer' , 'Uint8Array' ] , otherBuffer ) ;
877
+ if ( isArrayBufferView ( otherBuffer ) ) {
878
+ // Do nothing..
879
+ } else if ( isAnyArrayBuffer ( otherBuffer ) ) {
880
+ otherBuffer = new Uint8Array ( otherBuffer ) ;
881
+ } else {
882
+ throw new ERR_INVALID_ARG_TYPE ( 'otherBuffer' , BUFFER_NAMES , otherBuffer ) ;
870
883
}
871
884
872
885
if ( this === otherBuffer )
873
886
return true ;
874
- const len = TypedArrayPrototypeGetByteLength ( this ) ;
875
- if ( len !== TypedArrayPrototypeGetByteLength ( otherBuffer ) )
887
+ const len = this . byteLength ;
888
+ if ( len !== otherBuffer . byteLength )
876
889
return false ;
877
890
878
891
return len === 0 || _compare ( this , otherBuffer ) === 0 ;
@@ -919,9 +932,14 @@ Buffer.prototype.compare = function compare(target,
919
932
targetEnd ,
920
933
sourceStart ,
921
934
sourceEnd ) {
922
- if ( ! isUint8Array ( target ) ) {
923
- throw new ERR_INVALID_ARG_TYPE ( 'target' , [ 'Buffer' , 'Uint8Array' ] , target ) ;
935
+ if ( isArrayBufferView ( target ) ) {
936
+ // Do nothing..
937
+ } else if ( isAnyArrayBuffer ( target ) ) {
938
+ target = new Uint8Array ( target ) ;
939
+ } else {
940
+ throw new ERR_INVALID_ARG_TYPE ( 'target' , BUFFER_NAMES , target ) ;
924
941
}
942
+
925
943
if ( arguments . length === 1 )
926
944
return _compare ( this , target ) ;
927
945
@@ -997,14 +1015,14 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
997
1015
return ops . indexOf ( buffer , val , byteOffset , dir ) ;
998
1016
}
999
1017
1000
- if ( isUint8Array ( val ) ) {
1018
+ if ( isArrayBufferView ( val ) || isAnyArrayBuffer ( val ) ) {
1001
1019
const encodingVal =
1002
1020
( ops === undefined ? encodingsMap . utf8 : ops . encodingVal ) ;
1003
1021
return indexOfBuffer ( buffer , val , byteOffset , encodingVal , dir ) ;
1004
1022
}
1005
1023
1006
1024
throw new ERR_INVALID_ARG_TYPE (
1007
- 'value' , [ 'number' , 'string' , 'Buffer' , 'Uint8Array' ] , val ,
1025
+ 'value' , [ 'number' , 'string' , ... BUFFER_NAMES ] , val ,
1008
1026
) ;
1009
1027
}
1010
1028
@@ -1081,7 +1099,7 @@ function _fill(buf, value, offset, end, encoding) {
1081
1099
1082
1100
if ( typeof value === 'number' ) {
1083
1101
// OOB check
1084
- const byteLen = TypedArrayPrototypeGetByteLength ( buf ) ;
1102
+ const byteLen = buf . byteLength ;
1085
1103
const fillLength = end - offset ;
1086
1104
if ( offset > end || fillLength + offset > byteLen )
1087
1105
throw new ERR_BUFFER_OUT_OF_BOUNDS ( ) ;
@@ -1247,9 +1265,8 @@ if (internalBinding('config').hasIntl) {
1247
1265
// Transcodes the Buffer from one encoding to another, returning a new
1248
1266
// Buffer instance.
1249
1267
transcode = function transcode ( source , fromEncoding , toEncoding ) {
1250
- if ( ! isUint8Array ( source ) ) {
1251
- throw new ERR_INVALID_ARG_TYPE ( 'source' ,
1252
- [ 'Buffer' , 'Uint8Array' ] , source ) ;
1268
+ if ( ! ArrayBufferIsView ( source ) && ! isAnyArrayBuffer ( source ) ) {
1269
+ throw new ERR_INVALID_ARG_TYPE ( 'source' , BUFFER_NAMES , source ) ;
1253
1270
}
1254
1271
if ( source . length === 0 ) return Buffer . alloc ( 0 ) ;
1255
1272
@@ -1305,19 +1322,19 @@ function atob(input) {
1305
1322
}
1306
1323
1307
1324
function isUtf8 ( input ) {
1308
- if ( isTypedArray ( input ) || isAnyArrayBuffer ( input ) ) {
1325
+ if ( isArrayBufferView ( input ) || isAnyArrayBuffer ( input ) ) {
1309
1326
return bindingIsUtf8 ( input ) ;
1310
1327
}
1311
1328
1312
- throw new ERR_INVALID_ARG_TYPE ( 'input' , [ 'ArrayBuffer' , 'Buffer' , 'TypedArray' ] , input ) ;
1329
+ throw new ERR_INVALID_ARG_TYPE ( 'input' , BUFFER_NAMES , input ) ;
1313
1330
}
1314
1331
1315
1332
function isAscii ( input ) {
1316
- if ( isTypedArray ( input ) || isAnyArrayBuffer ( input ) ) {
1333
+ if ( isArrayBufferView ( input ) || isAnyArrayBuffer ( input ) ) {
1317
1334
return bindingIsAscii ( input ) ;
1318
1335
}
1319
1336
1320
- throw new ERR_INVALID_ARG_TYPE ( 'input' , [ 'ArrayBuffer' , 'Buffer' , 'TypedArray' ] , input ) ;
1337
+ throw new ERR_INVALID_ARG_TYPE ( 'input' , BUFFER_NAMES , input ) ;
1321
1338
}
1322
1339
1323
1340
module . exports = {
0 commit comments