Repository: sindresorhus/serialize-error
Pull request #57 (2022-01-30 13:59:58)

Handle streams attached to an error by replacing them with `'[object Stream]'`
fixes https://github.com/sindresorhus/serialize-error/issues/56

![image](https://user-images.githubusercontent.com/385731/151702830-23fde5ae-87bc-47d7-a521-57e53427297e.png)


Richienb: If the module is going to handle readables, then what about writables and transform streams?
Richienb: Also, this can be used: https://www.npmjs.com/package/is-stream
wodka: > Also, this can be used: https://github.com/sindresorhus/is-stream

would make sense - what do you think about adding this as a dependency?
sindresorhus: This needs a test.
sindresorhus: > would make sense - what do you think about adding this as a dependency?

Nah. I think what you have is fine for now. https://github.com/sindresorhus/serialize-error/pull/57/files#diff-e727e4bdf3657fd1d798edcd6b099d6e092f8573cba266154583a746bba0f346R80
sindresorhus: This also needs to support https://nodejs.org/api/webstreams.html#class-readablestream
fregante: Same applies here: https://github.com/sindresorhus/serialize-error/issues/55#issuecomment-1037943930

I didn't realize that `serialize-error` already includes `Buffer` accommodations. Even that one seems unrelated.
sindresorhus: >I didn't realize that serialize-error already includes Buffer accommodations. Even that one seems unrelated.

It's just to make the common use-case nicer. It's not meant for two-way transfer. It's mostly just to make it nicer for logging and storing errors. 
wodka: @sindresorhus would you think it makes more sense to split it up to show a text depending on the stream type? Something along the lines of this:

```
		if (typeof value === 'object' && typeof value.pipe === 'function') {
			const writeable = typeof value._write === 'function' && typeof value._writableState === 'object';
			const readable = typeof value._read === 'function' && typeof value._readableState === 'object';

			if (writeable && readable && typeof value._transform === 'function') {
				to[key] = '[object TransformStream]';
				continue;
			}

			if (writeable && readable) {
				to[key] = '[object DuplexStream]';
				continue;
			}

			if (readable) {
				to[key] = '[object ReadableStream]';
				continue;
			}

			if (writeable) {
				to[key] = '[object WriteableStream]';
				continue;
			}

			to[key] = '[object Stream]';
			continue;
		}
```
sindresorhus: > would you think it makes more sense to split it up to show a text depending on the stream type? Something along the lines of this:

No, let's keep it simple.
wodka: ok, then I think it's done

Patch:
diff --git a/index.js b/index.js
index d794d7d..4b27091 100644
--- a/index.js
+++ b/index.js
@@ -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;
 		}


Test patch:
diff --git a/test.js b/test.js
index 0e45c35..d53d574 100644
--- a/test.js
+++ b/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';
 
@@ -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() {}