From 7f281e89655bfe6d9c45113a4af02d0bc3f39d13 Mon Sep 17 00:00:00 2001 From: jderuere Date: Fri, 30 Oct 2015 10:48:20 -0400 Subject: [PATCH 1/2] Should check that obj.toJSON is a function before calling it --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 7684d23..efa8fee 100644 --- a/index.js +++ b/index.js @@ -40,7 +40,7 @@ function hasBinary(data) { } } } else if (obj && 'object' == typeof obj) { - if (obj.toJSON) { + if (obj.toJSON && 'function' == typeof obj.toJSON) { obj = obj.toJSON(); } From 4b65767f7ec6b3a65ca9a8d45b22e26e94168d73 Mon Sep 17 00:00:00 2001 From: jderuere Date: Fri, 6 Nov 2015 10:12:04 -0500 Subject: [PATCH 2/2] Modify unit test to verify that a JSON object can contains a toJSON attribute and avoid a regression --- test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index 5320d4f..8d9247c 100644 --- a/test.js +++ b/test.js @@ -22,12 +22,12 @@ describe('has-binarydata', function(){ }); it('should work with an object that does not contain binary', function() { - var ob = {a: 'a', b: [], c: 1234}; + var ob = {a: 'a', b: [], c: 1234, toJSON: '{\"a\": \"a\"}'}; assert(!hasBinary(ob)); }); it('should work with an object that contains a buffer', function() { - var ob = {a: 'a', b: new Buffer('abc'), c: 1234}; + var ob = {a: 'a', b: new Buffer('abc'), c: 1234, toJSON: '{\"a\": \"a\"}'}; assert(hasBinary(ob)); });