Skip to content

Commit

Permalink
Replace useless structuredClone with rfdc
Browse files Browse the repository at this point in the history
  • Loading branch information
cressie176 committed Mar 13, 2024
1 parent d5ea1cd commit 0750d61
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to Semantic Versioning](https://semver.org/spec/v2.0.0.html)

## 5.0.1

- structuredClone throws on unclonable properties (like functions). Replaced with rfdc.

## 5.0.0

- Enable the include processor to target a sub document of the log record
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ logger.info("How blissful it is, for one who has nothing", {
### include

Copies the specified paths into a new log record. This is useful to avoid logging every property from a noisy object, including potentially senstive ones (I'm looking at you AxiosError!).
Please note, this processor uses Node's [structuredClone](https://nodejs.org/api/globals.html#structuredclonevalue-options) function and may therefore be [slow](https://github.com/nodejs/node/issues/50320).
Please note, this processor uses [rfdc](https://www.npmjs.com/package/rfdc) with 'circles' mode enabled so may be a little slow.

It has the following options:

Expand Down
3 changes: 2 additions & 1 deletion lib/processors/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const has = require('has-value');
const get = require('get-value');
const set = require('set-value');
const toPath = require('to-path');
const clone = require('rfdc')({ circles: true });

const ALWAYS_PASS = () => true;

Expand Down Expand Up @@ -46,5 +47,5 @@ function getPatch(paths, record) {
}

function applyPatch(record, basePath, patch) {
return set(structuredClone(record), basePath, patch, { split: objectsOnly });
return set(clone(record), basePath, patch, { split: objectsOnly });
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"get-value": "^3.0.1",
"has-value": "^2.0.2",
"json-stringify-safe": "^5.0.1",
"rfdc": "^1.3.1",
"set-value": "^4.1.0",
"to-path": "^1.0.1"
},
Expand Down
29 changes: 22 additions & 7 deletions test/processors/include.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,28 @@ describe('include', () => {
});

// See https://github.com/jonschlinkert/get-value/issues/29
// it("should work with recursive documents", () => {
// const fn = include({ basePath: 'r' paths: ["a.b.c"] });
// const a = { c: 2 };
// a.b = a;
// const result = fn({ record: { r: { a }, keep: 1 } });
// eq(result, { r: { a: { b: { c: 2 } }, keep: 1 });
// });
it('should work with recursive documents', () => {
const fn = include({ basePath: 'r', paths: ['a.b.c'] });
const a = { c: 2 };
a.b = a;
const result = fn({ record: { r: { a }, keep: 1 } });
eq(result, { r: { a: { b: { c: 2 } } }, keep: 1 });
});

it('should clone with recursive documents', () => {
const fn = include({ basePath: 'r', paths: ['a'] });
const keep = { a: 1 };
keep.z = keep;
const result = fn({ record: { r: { a: 1 }, keep } });
eq(result, { r: { a: 1 }, keep });
});

it('should tolerate unclonable documents', () => {
const fn = include({ basePath: 'r', paths: ['a'] });
const keep = () => true;
const result = fn({ record: { r: { a: 1 }, keep } });
eq(result, { r: { a: 1 }, keep });
});

it('should not mutate original record', () => {
const record = { r: { a: { b: 1, c: 2 }, m: 1, x: { y: 1, z: 2 } }, keep: 1 };
Expand Down

0 comments on commit 0750d61

Please sign in to comment.