From: Feross Aboukhadijeh Date: Wed, 16 Sep 2015 05:29:21 +0000 (-0700) Subject: Add `instanceof Buffer` support (fix #40) X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=1d20f50f8b4c62779c0dbfb1fa401168ab52dce0;p=buffer.git Add `instanceof Buffer` support (fix #40) --- diff --git a/index.js b/index.js index 1ff1731..5a68f2c 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ * @author Feross Aboukhadijeh * @license MIT */ +/* eslint-disable no-proto */ var base64 = require('base64-js') var ieee754 = require('ieee754') @@ -214,10 +215,16 @@ function fromJsonObject (that, object) { return that } +if (Buffer.TYPED_ARRAY_SUPPORT) { + Buffer.prototype.__proto__ = Uint8Array.prototype + Buffer.__proto__ = Uint8Array +} + function allocate (that, length) { if (Buffer.TYPED_ARRAY_SUPPORT) { // Return an augmented `Uint8Array` instance, for best performance that = Buffer._augment(new Uint8Array(length)) + that.__proto__ = Buffer.prototype } else { // Fallback: Return an object instance of the Buffer class that.length = length diff --git a/test/basic.js b/test/basic.js index de47970..f093f8e 100644 --- a/test/basic.js +++ b/test/basic.js @@ -8,6 +8,12 @@ test('buf.constructor is Buffer', function (t) { t.end() }) +test('instanceof Buffer', function (t) { + var buf = new B([1, 2]) + t.ok(buf instanceof B) + t.end() +}) + test('convert to Uint8Array in modern browsers', function (t) { if (B.TYPED_ARRAY_SUPPORT) { var buf = new B([1, 2])