From 3b3567b7912eee93fc8b11e9687b2cc772fc2942 Mon Sep 17 00:00:00 2001 From: Scott Gress Date: Fri, 24 Feb 2017 09:13:57 -0600 Subject: [PATCH] Ensure globals are functions before running `instanceof` checks against them. `File` and `Blob` don't exist in Node, and `Buffer` and `ArrayBuffer` don't exist in HTML5. So if an app uses those globals for some other purpose and also uses `has-binary`, it can crash with a "TypeError: Expecting a function in instanceof check, but got " error. This commit fixes that issue. --- index.js | 8 ++++---- test.js | 8 ++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 434ccfa..5a10f5b 100644 --- a/index.js +++ b/index.js @@ -25,10 +25,10 @@ function hasBinary(data) { function _hasBinary(obj) { if (!obj) return false; - if ( (global.Buffer && global.Buffer.isBuffer && global.Buffer.isBuffer(obj)) || - (global.ArrayBuffer && obj instanceof ArrayBuffer) || - (global.Blob && obj instanceof Blob) || - (global.File && obj instanceof File) + if ( (typeof global.Buffer === 'function' && global.Buffer.isBuffer && global.Buffer.isBuffer(obj)) || + (typeof global.ArrayBuffer === 'function' && obj instanceof ArrayBuffer) || + (typeof global.Blob === 'function' && obj instanceof Blob) || + (typeof global.File === 'function' && obj instanceof File) ) { return true; } diff --git a/test.js b/test.js index c6bd5aa..2afd655 100644 --- a/test.js +++ b/test.js @@ -70,4 +70,12 @@ describe('has-binarydata', function(){ }); } + else { + it('should not crash if global Blob is not a function', function() { + global.Blob = [ 1, 2, 3 ]; + hasBinary(global.Blob); + return; + }); + } + });