Skip to content

Commit

Permalink
Fix the crash when hashing an async function
Browse files Browse the repository at this point in the history
  • Loading branch information
whatisaphone committed Dec 12, 2019
1 parent 9b84910 commit c359c2d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down
2 changes: 2 additions & 0 deletions test/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit c359c2d

Please sign in to comment.