@@ -317,6 +317,7 @@ function fromArrayBuffer (array, byteOffset, length) {
317317
318318function fromObject ( obj ) {
319319 if ( Buffer . isBuffer ( obj ) ) {
320+ // Note: Probably not necessary anymore.
320321 const len = checked ( obj . length ) | 0
321322 const buf = createBuffer ( len )
322323
@@ -363,9 +364,7 @@ Buffer.isBuffer = function isBuffer (b) {
363364}
364365
365366Buffer . 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 ) ) {
369368 throw new TypeError (
370369 'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array'
371370 )
@@ -428,37 +427,28 @@ Buffer.concat = function concat (list, length) {
428427 const buffer = Buffer . allocUnsafe ( length )
429428 let pos = 0
430429 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 ) ) {
446432 throw new TypeError ( '"list" argument must be an Array of Buffers' )
447- } else {
448- buf . copy ( buffer , pos )
449433 }
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 )
450439 pos += buf . length
451440 }
452441 return buffer
453442}
454443
455444function byteLength ( string , encoding ) {
456- if ( Buffer . isBuffer ( string ) ) {
457- return string . length
458- }
459445 if ( ArrayBuffer . isView ( string ) || isInstance ( string , ArrayBuffer ) ) {
460446 return string . byteLength
461447 }
448+ if ( typeof SharedArrayBuffer !== 'undefined' &&
449+ isInstance ( string , SharedArrayBuffer ) ) {
450+ return string . byteLength
451+ }
462452 if ( typeof string !== 'string' ) {
463453 throw new TypeError (
464454 'The "string" argument must be one of type string, Buffer, or ArrayBuffer. ' +
@@ -632,7 +622,6 @@ Buffer.prototype.toString = function toString () {
632622Buffer . prototype . toLocaleString = Buffer . prototype . toString
633623
634624Buffer . prototype . equals = function equals ( b ) {
635- if ( ! Buffer . isBuffer ( b ) ) throw new TypeError ( 'Argument must be a Buffer' )
636625 if ( this === b ) return true
637626 return Buffer . compare ( this , b ) === 0
638627}
@@ -649,10 +638,7 @@ if (customInspectSymbol) {
649638}
650639
651640Buffer . 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 ) ) {
656642 throw new TypeError (
657643 'The "target" argument must be one of type Buffer or Uint8Array. ' +
658644 'Received type ' + ( typeof target )
@@ -697,13 +683,10 @@ Buffer.prototype.compare = function compare (target, start, end, thisStart, this
697683 let y = end - start
698684 const len = Math . min ( x , y )
699685
700- const thisCopy = this . slice ( thisStart , thisEnd )
701- const targetCopy = target . slice ( start , end )
702-
703686 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 ]
707690 break
708691 }
709692 }
@@ -1719,7 +1702,7 @@ Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert
17191702
17201703// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
17211704Buffer . 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' )
17231706 if ( ! start ) start = 0
17241707 if ( ! end && end !== 0 ) end = this . length
17251708 if ( targetStart >= target . length ) targetStart = target . length
@@ -1814,7 +1797,7 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) {
18141797 this [ i ] = val
18151798 }
18161799 } else {
1817- const bytes = Buffer . isBuffer ( val )
1800+ const bytes = isInstance ( val , Uint8Array )
18181801 ? val
18191802 : Buffer . from ( val , encoding )
18201803 const len = bytes . length
@@ -2100,7 +2083,8 @@ function blitBuffer (src, dst, offset, length) {
21002083function isInstance ( obj , type ) {
21012084 return obj instanceof type ||
21022085 ( 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 ) )
21042088}
21052089function numberIsNaN ( obj ) {
21062090 // For IE11 support
0 commit comments