From c359c2dbec009859c2343e5b6cf3a32e23ddd340 Mon Sep 17 00:00:00 2001 From: John Simon Date: Thu, 12 Dec 2019 13:41:11 -0500 Subject: [PATCH] Fix the crash when hashing an `async` function --- index.js | 2 +- test/index.js | 12 ++++++++++++ test/types.js | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index a4f7222..90c2285 100644 --- a/index.js +++ b/index.js @@ -214,7 +214,7 @@ function typeHasher(options, writeTo, context){ return write(object); } - if(objType !== 'object' && objType !== 'function') { + if(objType !== 'object' && objType !== 'function' && objType !== 'asyncfunction') { if(this['_' + objType]) { this['_' + objType](object); } else if (options.ignoreUnknown) { diff --git a/test/index.js b/test/index.js index 2c9cec0..ecff50a 100644 --- a/test/index.js +++ b/test/index.js @@ -219,6 +219,18 @@ describe('hash', function() { assert.notEqual(c,d, 'changing a property in the prototype changes the hash'); }); + it('distinguishes async functions based on their properties', function() { + var a, b; + + async function Foo() {} + a = hash(Foo); + + Foo.foo = 22; + b = hash(Foo); + + assert.notEqual(a,b, 'adding a property changes the hash'); + }); + it('Distinguish objects based on their type', function() { function Foo() {} diff --git a/test/types.js b/test/types.js index d59e2e9..eb2274b 100644 --- a/test/types.js +++ b/test/types.js @@ -8,11 +8,13 @@ var validSha1 = /^[0-9a-f]{40}$/i; describe('hash()ing different types', function() { it('hashes non-object types', function() { var func = function(a){ return a + 1; }; + var asyncFunc = async function(a){ return a + 1; }; assert.ok(validSha1.test(hash('Shazbot!')), 'hash string'); assert.ok(validSha1.test(hash(42)), 'hash number'); assert.ok(validSha1.test(hash(NaN)), 'hash bool'); assert.ok(validSha1.test(hash(true)), 'hash bool'); assert.ok(validSha1.test(hash(func)), 'hash function'); + assert.ok(validSha1.test(hash(asyncFunc)), 'hash async function'); }); it('hashes special object types', function() {