From 2879144b988ea2d8f52b29bdce455f796cc90e06 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 10 Feb 2020 20:40:14 +0100 Subject: [PATCH] squash! Fix the crash when hashing an `async` function Co-authored-by: Anna Henningsen --- test/index.js | 14 ++++++++++++-- test/types.js | 18 ++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/test/index.js b/test/index.js index ecff50a..5f46a69 100644 --- a/test/index.js +++ b/test/index.js @@ -221,8 +221,18 @@ describe('hash', function() { it('distinguishes async functions based on their properties', function() { var a, b; - - async function Foo() {} + + var Foo; + + try { + Foo = eval('async function Foo() {}; Foo'); + } catch (err) { + if (err.name === 'SyntaxError') + return this.skip('Not available on Node 6'); + else + throw err; + } + a = hash(Foo); Foo.foo = 22; diff --git a/test/types.js b/test/types.js index eb2274b..c0de028 100644 --- a/test/types.js +++ b/test/types.js @@ -8,7 +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; }; + var asyncFunc; + try { + asyncFunc = eval('async function(a) { return a + 1; }'); + } catch (err) { + if (err.name === 'SyntaxError') asyncFunc = func; + else throw err; + } assert.ok(validSha1.test(hash('Shazbot!')), 'hash string'); assert.ok(validSha1.test(hash(42)), 'hash number'); assert.ok(validSha1.test(hash(NaN)), 'hash bool'); @@ -36,7 +42,7 @@ describe('hash()ing different types', function() { if (typeof process !== 'undefined') { assert.ok(validSha1.test(hash(process)), 'hash process'); } - + var timer = setTimeout(function() {}, 0); assert.ok(validSha1.test(hash(timer)), 'hash timer'); }); @@ -55,7 +61,7 @@ describe('hash()ing different types', function() { if (typeof Uint8Array !== 'undefined') { it("Typed arrays can be hashed", function() { - + assert.ok(validSha1.test(hash(new Uint8Array([1,2,3,4]))), 'hashes Uint8Array'); assert.ok(validSha1.test(hash(new Int8Array([1,2,3,4]))), 'hashes Int8Array'); assert.ok(validSha1.test(hash(new Uint16Array([1,2,3,4]))), 'hashes Uint16Array'); @@ -132,7 +138,7 @@ describe('hash()ing different types', function() { // Self check; did we really hash all the types? assert.equal(no, types.length); }); - + it("Builtin types might result in identical hashes with respectFunctionNames = false", function() { var hashcount = {}; var types = [Object, Date, Number, String, Function, RegExp, @@ -161,14 +167,14 @@ describe('hash()ing different types', function() { // Self check; did we really hash all the types? assert.equal(no, types.length); }); - + it("Functions with identical bodies and different names result in identical hashes with respectFunctionNames = false", function() { var fn1 = function a() {}; var fn2 = function b() {}; var toStringDummy = function() { return '...'; }; fn1.toString = toStringDummy; fn2.toString = toStringDummy; - + var h1 = hash(fn1, { respectFunctionNames: false }); var h2 = hash(fn2, { respectFunctionNames: false }); assert.strictEqual(h1, h2);