sindresorhus / hash-object

Get the hash of an object
MIT License
134 stars 10 forks source link

Handling Circular References in `hashObject` Function #5

Closed palashmon closed 1 year ago

palashmon commented 1 year ago

When the hashObject function encounters circular references in the input object, it currently leads to a "maximum call stack size exceeded" error due to an infinite loop during the normalization process. This issue can result in unexpected behavior and a lack of graceful error handling for users.

Steps to Reproduce:

const obj = { a: { b: {} } };
obj.a.b = obj; // Creating a circular reference
const hash = hashObject(obj, { algorithm: 'sha1' });
console.log(hash);
//=> Error: Maximum call stack size exceeded

Proposed Solution:

Implement a mechanism within the hashObject function to detect circular references and handle them gracefully. Upon encountering a circular reference, the function should throw a specific error message informing users about the issue.