Skip to content

Commit 0d2666d

Browse files
authored
Merge pull request #155 from feross/instanceof
Undo `instanceof Buffer` changes
2 parents 051039c + 6bd6029 commit 0d2666d

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

index.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ function fromArrayBuffer (array, byteOffset, length) {
239239
}
240240

241241
function fromObject (obj) {
242-
if (obj instanceof Buffer) {
242+
if (Buffer.isBuffer(obj)) {
243243
var len = checked(obj.length) | 0
244244
var buf = createBuffer(len)
245245

@@ -285,11 +285,11 @@ function SlowBuffer (length) {
285285
}
286286

287287
Buffer.isBuffer = function isBuffer (b) {
288-
return b instanceof Buffer
288+
return b != null && b._isBuffer === true
289289
}
290290

291291
Buffer.compare = function compare (a, b) {
292-
if (!(a instanceof Buffer) || !(b instanceof Buffer)) {
292+
if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
293293
throw new TypeError('Arguments must be Buffers')
294294
}
295295

@@ -351,7 +351,7 @@ Buffer.concat = function concat (list, length) {
351351
var pos = 0
352352
for (i = 0; i < list.length; ++i) {
353353
var buf = list[i]
354-
if (!(buf instanceof Buffer)) {
354+
if (!Buffer.isBuffer(buf)) {
355355
throw new TypeError('"list" argument must be an Array of Buffers')
356356
}
357357
buf.copy(buffer, pos)
@@ -361,7 +361,7 @@ Buffer.concat = function concat (list, length) {
361361
}
362362

363363
function byteLength (string, encoding) {
364-
if (string instanceof Buffer) {
364+
if (Buffer.isBuffer(string)) {
365365
return string.length
366366
}
367367
if (ArrayBuffer.isView(string) || string instanceof ArrayBuffer) {
@@ -474,8 +474,12 @@ function slowToString (encoding, start, end) {
474474
}
475475
}
476476

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
479483
Buffer.prototype._isBuffer = true
480484

481485
function swap (b, n, m) {
@@ -529,7 +533,7 @@ Buffer.prototype.toString = function toString () {
529533
}
530534

531535
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')
533537
if (this === b) return true
534538
return Buffer.compare(this, b) === 0
535539
}
@@ -545,7 +549,7 @@ Buffer.prototype.inspect = function inspect () {
545549
}
546550

547551
Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
548-
if (!(target instanceof Buffer)) {
552+
if (!Buffer.isBuffer(target)) {
549553
throw new TypeError('Argument must be a Buffer')
550554
}
551555

@@ -647,7 +651,7 @@ function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {
647651
}
648652

649653
// Finally, search either indexOf (if dir is true) or lastIndexOf
650-
if (val instanceof Buffer) {
654+
if (Buffer.isBuffer(val)) {
651655
// Special case: looking for empty string/buffer always fails
652656
if (val.length === 0) {
653657
return -1
@@ -1213,7 +1217,7 @@ Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
12131217
}
12141218

12151219
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')
12171221
if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
12181222
if (offset + ext > buf.length) throw new RangeError('Index out of range')
12191223
}
@@ -1541,7 +1545,7 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) {
15411545
this[i] = val
15421546
}
15431547
} else {
1544-
var bytes = val instanceof Buffer
1548+
var bytes = Buffer.isBuffer(val)
15451549
? val
15461550
: new Buffer(val, encoding)
15471551
var len = bytes.length

0 commit comments

Comments
 (0)