Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,21 @@ const destroyCircular = ({

for (const [key, value] of Object.entries(from)) {
if (value && value instanceof Uint8Array && value.constructor.name === 'Buffer') {
to[key] = '[object Buffer]';
to[key] = serialize ? '[object Buffer]' : value;
continue;
}

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

if (typeof value === 'function') {
if (!serialize) {
to[key] = value;
}

continue;
}

Expand Down
23 changes: 23 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,29 @@ test('should deserialize plain object', t => {
t.is(deserialized.code, 'code');
});

test('should preserve buffers when deserializing', t => {
const buffer = Buffer.from([1, 2, 3]);
const deserialized = deserializeError({
message: 'buffer',
stack: '',
data: buffer,
});

t.true(Buffer.isBuffer(deserialized.data));
t.is(deserialized.data, buffer);
});

test('should preserve functions when deserializing', t => {
const sideEffect = () => 'no-op';
const deserialized = deserializeError({
message: 'function',
stack: '',
callback: sideEffect,
});

t.is(deserialized.callback, sideEffect);
});

for (const property of ['cause', 'any']) {
// `cause` is treated differently from other properties in the code
test(`should deserialize errors on ${property} property`, t => {
Expand Down