Skip to content

Commit

Permalink
Don't serialize buffers (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
OfryL committed Jan 1, 2021
1 parent a125743 commit d58ce16
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Serialize an `Error` object into a plain object.
Non-error values are passed through.
Custom properties are preserved.
Buffer properties are replaced with `[object Buffer]`.
Circular references are handled.
@example
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ const destroyCircular = ({from, seen, to_, forceEnumerable}) => {
seen.push(from);

for (const [key, value] of Object.entries(from)) {
if (Buffer.isBuffer(value)) {
to[key] = '[object Buffer]';
continue;
}

if (typeof value === 'function') {
continue;
}
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Non-error values are passed through.
Custom properties are preserved.
Non-enumerable properties are kept non-enumerable (name, message, stack).
Enumerable properties are kept enumerable (all properties besides the non-enumerable ones).
Buffer properties are replaced with `[object Buffer]`.
Circular references are handled.

### deserializeError(value)
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ test('should discard nested functions', t => {
t.deepEqual(serialized, {});
});

test('should discard buffers', t => {
const object = {a: Buffer.alloc(1)};
const serialized = serializeError(object);
t.deepEqual(serialized, {a: '[object Buffer]'});
});

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

0 comments on commit d58ce16

Please sign in to comment.