Skip to content

Commit 0750d61

Browse files
committed
Replace useless structuredClone with rfdc
1 parent d5ea1cd commit 0750d61

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66
and this project adheres to Semantic Versioning](https://semver.org/spec/v2.0.0.html)
77

8+
## 5.0.1
9+
10+
- structuredClone throws on unclonable properties (like functions). Replaced with rfdc.
11+
812
## 5.0.0
913

1014
- Enable the include processor to target a sub document of the log record

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ logger.info("How blissful it is, for one who has nothing", {
323323
### include
324324

325325
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!).
326-
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).
326+
Please note, this processor uses [rfdc](https://www.npmjs.com/package/rfdc) with 'circles' mode enabled so may be a little slow.
327327

328328
It has the following options:
329329

lib/processors/include.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const has = require('has-value');
22
const get = require('get-value');
33
const set = require('set-value');
44
const toPath = require('to-path');
5+
const clone = require('rfdc')({ circles: true });
56

67
const ALWAYS_PASS = () => true;
78

@@ -46,5 +47,5 @@ function getPatch(paths, record) {
4647
}
4748

4849
function applyPatch(record, basePath, patch) {
49-
return set(structuredClone(record), basePath, patch, { split: objectsOnly });
50+
return set(clone(record), basePath, patch, { split: objectsOnly });
5051
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"get-value": "^3.0.1",
99
"has-value": "^2.0.2",
1010
"json-stringify-safe": "^5.0.1",
11+
"rfdc": "^1.3.1",
1112
"set-value": "^4.1.0",
1213
"to-path": "^1.0.1"
1314
},

test/processors/include.test.js

+22-7
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,28 @@ describe('include', () => {
7676
});
7777

7878
// See https://github.com/jonschlinkert/get-value/issues/29
79-
// it("should work with recursive documents", () => {
80-
// const fn = include({ basePath: 'r' paths: ["a.b.c"] });
81-
// const a = { c: 2 };
82-
// a.b = a;
83-
// const result = fn({ record: { r: { a }, keep: 1 } });
84-
// eq(result, { r: { a: { b: { c: 2 } }, keep: 1 });
85-
// });
79+
it('should work with recursive documents', () => {
80+
const fn = include({ basePath: 'r', paths: ['a.b.c'] });
81+
const a = { c: 2 };
82+
a.b = a;
83+
const result = fn({ record: { r: { a }, keep: 1 } });
84+
eq(result, { r: { a: { b: { c: 2 } } }, keep: 1 });
85+
});
86+
87+
it('should clone with recursive documents', () => {
88+
const fn = include({ basePath: 'r', paths: ['a'] });
89+
const keep = { a: 1 };
90+
keep.z = keep;
91+
const result = fn({ record: { r: { a: 1 }, keep } });
92+
eq(result, { r: { a: 1 }, keep });
93+
});
94+
95+
it('should tolerate unclonable documents', () => {
96+
const fn = include({ basePath: 'r', paths: ['a'] });
97+
const keep = () => true;
98+
const result = fn({ record: { r: { a: 1 }, keep } });
99+
eq(result, { r: { a: 1 }, keep });
100+
});
86101

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

0 commit comments

Comments
 (0)