Skip to content

Commit 5338e60

Browse files
committed
feat: use structuredClone when available
- Fixes #14
1 parent adbf88a commit 5338e60

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

lib/log-capture.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class LogCapture {
1818

1919
this._levels.forEach(level => {
2020
console[level] = (...args) => {
21-
this._logs.push({ level, args: clone(args) });
21+
this._logs.push({ level, args: this._clone(args) });
2222
};
2323
});
2424
}
@@ -48,6 +48,18 @@ class LogCapture {
4848
reset() {
4949
this._logs = [];
5050
}
51+
52+
_clone(args) {
53+
if (typeof structuredClone === 'function') {
54+
try {
55+
return structuredClone(args);
56+
} catch (e) {
57+
// Fallback to `clone` for objects that structuredClone cannot handle (e.g. functions).
58+
}
59+
}
60+
61+
return clone(args);
62+
}
5163
}
5264

5365
module.exports = new LogCapture();

0 commit comments

Comments
 (0)