Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle streams attached to an error by replacing them with '[object Stream]' #57

Merged
merged 3 commits into from Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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