Skip to content

Commit

Permalink
add detection of NodeJS bug in structuredClone that can not clone `…
Browse files Browse the repository at this point in the history
…DOMException`

nodejs/node#41038
  • Loading branch information
zloirock committed Jun 5, 2022
1 parent ac1debc commit 26e6570
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@
##### Unreleased
- Fixed a bug in the order of getting flags in `RegExp.prototype.flags` in the actual version of V8
- Fixed property descriptors of some `Math` and `Number` constants
- Added detection of NodeJS [bug](https://github.com/nodejs/node/issues/41038) in `structuredClone` that can not clone `DOMException` (just in case for future versions that will fix other issues)
- Added NodeJS 18.3 compat data mapping
- Updated Electron 20.0 compat data mapping

Expand Down
15 changes: 10 additions & 5 deletions packages/core-js/modules/web.structured-clone.js
Expand Up @@ -61,11 +61,11 @@ var checkBasicSemantic = function (structuredCloneImplementation) {
}) && structuredCloneImplementation;
};

var checkErrorsCloning = function (structuredCloneImplementation) {
var checkErrorsCloning = function (structuredCloneImplementation, $Error) {
return !fails(function () {
var error = new Error();
var error = new $Error();
var test = structuredCloneImplementation({ a: error, b: error });
return !(test && test.a === test.b && test.a instanceof Error);
return !(test && test.a === test.b && test.a instanceof $Error);
});
};

Expand All @@ -80,12 +80,17 @@ var checkNewErrorsCloningSemantic = function (structuredCloneImplementation) {
// FF94+, Safari 15.4+, Chrome 98+, NodeJS 17.0+, Deno 1.13+
// FF and Safari implementations can't clone errors
// https://bugzilla.mozilla.org/show_bug.cgi?id=1556604
// Chrome <103 returns `null` if cloned object contains multiple references to one error
// Chrome <102 returns `null` if cloned object contains multiple references to one error
// https://bugs.chromium.org/p/v8/issues/detail?id=12542
// NodeJS implementation can't clone DOMExceptions
// https://github.com/nodejs/node/issues/41038
// no one of current implementations supports new (html/5749) error cloning semantic
var nativeStructuredClone = global.structuredClone;

var FORCED_REPLACEMENT = IS_PURE || !checkErrorsCloning(nativeStructuredClone) || !checkNewErrorsCloningSemantic(nativeStructuredClone);
var FORCED_REPLACEMENT = IS_PURE
|| !checkErrorsCloning(nativeStructuredClone, Error)
|| !checkErrorsCloning(nativeStructuredClone, DOMException)
|| !checkNewErrorsCloningSemantic(nativeStructuredClone);

// Chrome 82+, Safari 14.1+, Deno 1.11+
// Chrome 78-81 implementation swaps `.name` and `.message` of cloned `DOMException`
Expand Down

0 comments on commit 26e6570

Please sign in to comment.