@@ -239,7 +239,7 @@ function fromArrayBuffer (array, byteOffset, length) {
239
239
}
240
240
241
241
function fromObject ( obj ) {
242
- if ( obj instanceof Buffer ) {
242
+ if ( Buffer . isBuffer ( obj ) ) {
243
243
var len = checked ( obj . length ) | 0
244
244
var buf = createBuffer ( len )
245
245
@@ -285,11 +285,11 @@ function SlowBuffer (length) {
285
285
}
286
286
287
287
Buffer . isBuffer = function isBuffer ( b ) {
288
- return b instanceof Buffer
288
+ return b != null && b . _isBuffer === true
289
289
}
290
290
291
291
Buffer . compare = function compare ( a , b ) {
292
- if ( ! ( a instanceof Buffer ) || ! ( b instanceof Buffer ) ) {
292
+ if ( ! Buffer . isBuffer ( a ) || ! Buffer . isBuffer ( b ) ) {
293
293
throw new TypeError ( 'Arguments must be Buffers' )
294
294
}
295
295
@@ -351,7 +351,7 @@ Buffer.concat = function concat (list, length) {
351
351
var pos = 0
352
352
for ( i = 0 ; i < list . length ; ++ i ) {
353
353
var buf = list [ i ]
354
- if ( ! ( buf instanceof Buffer ) ) {
354
+ if ( ! Buffer . isBuffer ( buf ) ) {
355
355
throw new TypeError ( '"list" argument must be an Array of Buffers' )
356
356
}
357
357
buf . copy ( buffer , pos )
@@ -361,7 +361,7 @@ Buffer.concat = function concat (list, length) {
361
361
}
362
362
363
363
function byteLength ( string , encoding ) {
364
- if ( string instanceof Buffer ) {
364
+ if ( Buffer . isBuffer ( string ) ) {
365
365
return string . length
366
366
}
367
367
if ( ArrayBuffer . isView ( string ) || string instanceof ArrayBuffer ) {
@@ -474,8 +474,12 @@ function slowToString (encoding, start, end) {
474
474
}
475
475
}
476
476
477
- // The property is used by the `is-buffer` npm package to detect Buffer instances
478
- // in Safari 5-7. Remove this eventually.
477
+ // This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)
478
+ // to detect a Buffer instance. It's not possible to use `instanceof Buffer`
479
+ // reliably in a browserify context because there could be multiple different
480
+ // copies of the 'buffer' package in use. This method works even for Buffer
481
+ // instances that were created from another copy of the `buffer` package.
482
+ // See: https://github.com/feross/buffer/issues/154
479
483
Buffer . prototype . _isBuffer = true
480
484
481
485
function swap ( b , n , m ) {
@@ -529,7 +533,7 @@ Buffer.prototype.toString = function toString () {
529
533
}
530
534
531
535
Buffer . prototype . equals = function equals ( b ) {
532
- if ( ! ( b instanceof Buffer ) ) throw new TypeError ( 'Argument must be a Buffer' )
536
+ if ( ! Buffer . isBuffer ( b ) ) throw new TypeError ( 'Argument must be a Buffer' )
533
537
if ( this === b ) return true
534
538
return Buffer . compare ( this , b ) === 0
535
539
}
@@ -545,7 +549,7 @@ Buffer.prototype.inspect = function inspect () {
545
549
}
546
550
547
551
Buffer . prototype . compare = function compare ( target , start , end , thisStart , thisEnd ) {
548
- if ( ! ( target instanceof Buffer ) ) {
552
+ if ( ! Buffer . isBuffer ( target ) ) {
549
553
throw new TypeError ( 'Argument must be a Buffer' )
550
554
}
551
555
@@ -647,7 +651,7 @@ function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
647
651
}
648
652
649
653
// Finally, search either indexOf (if dir is true) or lastIndexOf
650
- if ( val instanceof Buffer ) {
654
+ if ( Buffer . isBuffer ( val ) ) {
651
655
// Special case: looking for empty string/buffer always fails
652
656
if ( val . length === 0 ) {
653
657
return - 1
@@ -1213,7 +1217,7 @@ Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
1213
1217
}
1214
1218
1215
1219
function checkInt ( buf , value , offset , ext , max , min ) {
1216
- if ( ! ( buf instanceof Buffer ) ) throw new TypeError ( '"buffer" argument must be a Buffer instance' )
1220
+ if ( ! Buffer . isBuffer ( buf ) ) throw new TypeError ( '"buffer" argument must be a Buffer instance' )
1217
1221
if ( value > max || value < min ) throw new RangeError ( '"value" argument is out of bounds' )
1218
1222
if ( offset + ext > buf . length ) throw new RangeError ( 'Index out of range' )
1219
1223
}
@@ -1541,7 +1545,7 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) {
1541
1545
this [ i ] = val
1542
1546
}
1543
1547
} else {
1544
- var bytes = val instanceof Buffer
1548
+ var bytes = Buffer . isBuffer ( val )
1545
1549
? val
1546
1550
: new Buffer ( val , encoding )
1547
1551
var len = bytes . length
0 commit comments