@@ -317,6 +317,7 @@ function fromArrayBuffer (array, byteOffset, length) {
317
317
318
318
function fromObject ( obj ) {
319
319
if ( Buffer . isBuffer ( obj ) ) {
320
+ // Note: Probably not necessary anymore.
320
321
const len = checked ( obj . length ) | 0
321
322
const buf = createBuffer ( len )
322
323
@@ -363,9 +364,7 @@ Buffer.isBuffer = function isBuffer (b) {
363
364
}
364
365
365
366
Buffer . compare = function compare ( a , b ) {
366
- if ( isInstance ( a , Uint8Array ) ) a = Buffer . from ( a , a . offset , a . byteLength )
367
- if ( isInstance ( b , Uint8Array ) ) b = Buffer . from ( b , b . offset , b . byteLength )
368
- if ( ! Buffer . isBuffer ( a ) || ! Buffer . isBuffer ( b ) ) {
367
+ if ( ! isInstance ( a , Uint8Array ) || ! isInstance ( b , Uint8Array ) ) {
369
368
throw new TypeError (
370
369
'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
371
370
)
@@ -428,37 +427,28 @@ Buffer.concat = function concat (list, length) {
428
427
const buffer = Buffer . allocUnsafe ( length )
429
428
let pos = 0
430
429
for ( i = 0 ; i < list . length ; ++ i ) {
431
- let buf = list [ i ]
432
- if ( isInstance ( buf , Uint8Array ) ) {
433
- if ( pos + buf . length > buffer . length ) {
434
- if ( ! Buffer . isBuffer ( buf ) ) {
435
- buf = Buffer . from ( buf . buffer , buf . byteOffset , buf . byteLength )
436
- }
437
- buf . copy ( buffer , pos )
438
- } else {
439
- Uint8Array . prototype . set . call (
440
- buffer ,
441
- buf ,
442
- pos
443
- )
444
- }
445
- } else if ( ! Buffer . isBuffer ( buf ) ) {
430
+ const buf = list [ i ]
431
+ if ( ! isInstance ( buf , Uint8Array ) ) {
446
432
throw new TypeError ( '"list" argument must be an Array of Buffers' )
447
- } else {
448
- buf . copy ( buffer , pos )
449
433
}
434
+ if ( pos + buf . length > buffer . length ) {
435
+ buffer . set ( buf . subarray ( 0 , buffer . length - pos ) , pos )
436
+ break
437
+ }
438
+ buffer . set ( buf , pos )
450
439
pos += buf . length
451
440
}
452
441
return buffer
453
442
}
454
443
455
444
function byteLength ( string , encoding ) {
456
- if ( Buffer . isBuffer ( string ) ) {
457
- return string . length
458
- }
459
445
if ( ArrayBuffer . isView ( string ) || isInstance ( string , ArrayBuffer ) ) {
460
446
return string . byteLength
461
447
}
448
+ if ( typeof SharedArrayBuffer !== 'undefined' &&
449
+ isInstance ( string , SharedArrayBuffer ) ) {
450
+ return string . byteLength
451
+ }
462
452
if ( typeof string !== 'string' ) {
463
453
throw new TypeError (
464
454
'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' +
@@ -632,7 +622,6 @@ Buffer.prototype.toString = function toString () {
632
622
Buffer . prototype . toLocaleString = Buffer . prototype . toString
633
623
634
624
Buffer . prototype . equals = function equals ( b ) {
635
- if ( ! Buffer . isBuffer ( b ) ) throw new TypeError ( 'Argument must be a Buffer' )
636
625
if ( this === b ) return true
637
626
return Buffer . compare ( this , b ) === 0
638
627
}
@@ -649,10 +638,7 @@ if (customInspectSymbol) {
649
638
}
650
639
651
640
Buffer . prototype . compare = function compare ( target , start , end , thisStart , thisEnd ) {
652
- if ( isInstance ( target , Uint8Array ) ) {
653
- target = Buffer . from ( target , target . offset , target . byteLength )
654
- }
655
- if ( ! Buffer . isBuffer ( target ) ) {
641
+ if ( ! isInstance ( target , Uint8Array ) ) {
656
642
throw new TypeError (
657
643
'The "target" argument must be one of type Buffer or Uint8Array. ' +
658
644
'Received type ' + ( typeof target )
@@ -697,13 +683,10 @@ Buffer.prototype.compare = function compare (target, start, end, thisStart, this
697
683
let y = end - start
698
684
const len = Math . min ( x , y )
699
685
700
- const thisCopy = this . slice ( thisStart , thisEnd )
701
- const targetCopy = target . slice ( start , end )
702
-
703
686
for ( let i = 0 ; i < len ; ++ i ) {
704
- if ( thisCopy [ i ] !== targetCopy [ i ] ) {
705
- x = thisCopy [ i ]
706
- y = targetCopy [ i ]
687
+ if ( this [ thisStart + i ] !== target [ start + i ] ) {
688
+ x = this [ thisStart + i ]
689
+ y = target [ start + i ]
707
690
break
708
691
}
709
692
}
@@ -1719,7 +1702,7 @@ Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert
1719
1702
1720
1703
// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
1721
1704
Buffer . prototype . copy = function copy ( target , targetStart , start , end ) {
1722
- if ( ! Buffer . isBuffer ( target ) ) throw new TypeError ( 'argument should be a Buffer' )
1705
+ if ( ! isInstance ( target , Uint8Array ) ) throw new TypeError ( 'argument should be a Buffer' )
1723
1706
if ( ! start ) start = 0
1724
1707
if ( ! end && end !== 0 ) end = this . length
1725
1708
if ( targetStart >= target . length ) targetStart = target . length
@@ -1814,7 +1797,7 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) {
1814
1797
this [ i ] = val
1815
1798
}
1816
1799
} else {
1817
- const bytes = Buffer . isBuffer ( val )
1800
+ const bytes = isInstance ( val , Uint8Array )
1818
1801
? val
1819
1802
: Buffer . from ( val , encoding )
1820
1803
const len = bytes . length
@@ -2100,7 +2083,8 @@ function blitBuffer (src, dst, offset, length) {
2100
2083
function isInstance ( obj , type ) {
2101
2084
return obj instanceof type ||
2102
2085
( obj != null && obj . constructor != null && obj . constructor . name != null &&
2103
- obj . constructor . name === type . name )
2086
+ obj . constructor . name === type . name ) ||
2087
+ ( type === Uint8Array && Buffer . isBuffer ( obj ) )
2104
2088
}
2105
2089
function numberIsNaN ( obj ) {
2106
2090
// For IE11 support
0 commit comments