Skip to content

Commit

Permalink
Fix handling of circular references in event data when using debug mode
Browse files Browse the repository at this point in the history
Fixes #95
  • Loading branch information
sindresorhus committed Apr 1, 2022
1 parent f3da6ab commit 8a2371c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion index.js
Expand Up @@ -216,7 +216,12 @@ class Emittery {

if (!this.debug.logger) {
this.debug.logger = (type, debugName, eventName, eventData) => {
eventData = JSON.stringify(eventData);
try {
// TODO: Use https://github.com/sindresorhus/safe-stringify when the package is more mature. Just copy-paste the code.
eventData = JSON.stringify(eventData);
} catch {
eventData = `Object with the following keys failed to stringify: ${Object.keys(eventData).join(',')}`;
}

if (typeof eventName === 'symbol') {
eventName = eventName.toString();
Expand Down
14 changes: 14 additions & 0 deletions test/index.js
Expand Up @@ -1210,3 +1210,17 @@ test('isDebug can be turned on for and instance without using the constructor',
t.is(eventStore[2].debugName, 'testEmitter');
t.is(eventStore[2].eventData, 'test data');
});

test('debug mode - handles circular references in event data', async t => {
const emitter = new Emittery({
debug: {
name: 'testEmitter',
enabled: true
}
});

const data = {};
data.circular = data;

await t.notThrowsAsync(emitter.emit('test', data));
});

0 comments on commit 8a2371c

Please sign in to comment.