From 1d20f50f8b4c62779c0dbfb1fa401168ab52dce0 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 15 Sep 2015 22:29:21 -0700 Subject: [PATCH] Add `instanceof Buffer` support (fix #40) --- index.js | 7 +++++++ test/basic.js | 6 ++++++ 2 files changed, 13 insertions(+) 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]) -- 2.34.1