Skip to content

Commit

Permalink
Handle streams attached to an error by replacing them with `'[object …
Browse files Browse the repository at this point in the history
…Stream]'` (#57)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
wodka and sindresorhus committed Feb 14, 2022
1 parent 627c510 commit b589f8e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Expand Up @@ -77,6 +77,12 @@ const destroyCircular = ({
continue;
}

// TODO: Use `stream.isReadable()` when targeting Node.js 18.
if (typeof value === 'object' && typeof value.pipe === 'function') {
to[key] = '[object Stream]';
continue;
}

if (typeof value === 'function') {
continue;
}
Expand Down
10 changes: 10 additions & 0 deletions test.js
@@ -1,4 +1,5 @@
import {Buffer} from 'node:buffer';
import Stream from 'node:stream';
import test from 'ava';
import {serializeError, deserializeError} from './index.js';

Expand Down Expand Up @@ -82,6 +83,15 @@ test('should discard buffers', t => {
t.deepEqual(serialized, {a: '[object Buffer]'});
});

test('should discard streams', t => {
t.deepEqual(serializeError({s: new Stream.Stream()}), {s: '[object Stream]'}, 'Stream.Stream');
t.deepEqual(serializeError({s: new Stream.Readable()}), {s: '[object Stream]'}, 'Stream.Readable');
t.deepEqual(serializeError({s: new Stream.Writable()}), {s: '[object Stream]'}, 'Stream.Writable');
t.deepEqual(serializeError({s: new Stream.Duplex()}), {s: '[object Stream]'}, 'Stream.Duplex');
t.deepEqual(serializeError({s: new Stream.Transform()}), {s: '[object Stream]'}, 'Stream.Transform');
t.deepEqual(serializeError({s: new Stream.PassThrough()}), {s: '[object Stream]'}, 'Stream.PassThrough');
});

test('should replace top-level functions with a helpful string', t => {
function a() {}
function b() {}
Expand Down

0 comments on commit b589f8e

Please sign in to comment.