Skip to content

Commit

Permalink
add a fix for a NodeJS 20.0.0 bug with cloning File via `structured…
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Apr 18, 2023
1 parent 1a399bc commit 6cc816e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Changelog
##### Unreleased
- Added a fix for a NodeJS 20.0.0 [bug](https://github.com/nodejs/node/issues/47612) with cloning `File` via `structuredClone`
- Compat data improvements:
- Added NodeJS 20.0 compat data mapping

Expand Down
30 changes: 15 additions & 15 deletions packages/core-js/modules/web.structured-clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ var throwUnpolyfillable = function (type, action) {
throw new DOMException((action || 'Cloning') + ' of ' + type + ' cannot be properly polyfilled in this engine', DATA_CLONE_ERROR);
};

var tryNativeRestrictedStructuredClone = function (value, type) {
if (!nativeRestrictedStructuredClone) throwUnpolyfillable(type);
return nativeRestrictedStructuredClone(value);
};

var createDataTransfer = function () {
var dataTransfer;
try {
Expand Down Expand Up @@ -245,21 +250,24 @@ var structuredCloneInternal = function (value, map) {
structuredCloneInternal(value.p4, map)
);
} catch (error) {
if (nativeRestrictedStructuredClone) {
cloned = nativeRestrictedStructuredClone(value);
} else throwUnpolyfillable(type);
cloned = tryNativeRestrictedStructuredClone(value, type);
}
break;
// NodeJS 20.0.0 bug, https://github.com/nodejs/node/issues/47612
case 'File':
try {
cloned = new File([value], value.name, value);
} catch (error) {
cloned = tryNativeRestrictedStructuredClone(value, type);
} break;
case 'FileList':
dataTransfer = createDataTransfer();
if (dataTransfer) {
for (i = 0, length = lengthOfArrayLike(value); i < length; i++) {
dataTransfer.items.add(structuredCloneInternal(value[i], map));
}
cloned = dataTransfer.files;
} else if (nativeRestrictedStructuredClone) {
cloned = nativeRestrictedStructuredClone(value);
} else throwUnpolyfillable(type);
} else cloned = tryNativeRestrictedStructuredClone(value, type);
break;
case 'ImageData':
// Safari 9 ImageData is a constructor, but typeof ImageData is 'object'
Expand All @@ -271,9 +279,7 @@ var structuredCloneInternal = function (value, map) {
{ colorSpace: value.colorSpace }
);
} catch (error) {
if (nativeRestrictedStructuredClone) {
cloned = nativeRestrictedStructuredClone(value);
} else throwUnpolyfillable(type);
cloned = tryNativeRestrictedStructuredClone(value, type);
} break;
default:
if (nativeRestrictedStructuredClone) {
Expand Down Expand Up @@ -365,12 +371,6 @@ var structuredCloneInternal = function (value, map) {
} catch (error) {
throwUncloneable(type);
} break;
case 'File':
try {
cloned = new File([value], value.name, value);
} catch (error) {
throwUnpolyfillable(type);
} break;
case 'CropTarget':
case 'CryptoKey':
case 'FileSystemDirectoryHandle':
Expand Down

0 comments on commit 6cc816e

Please sign in to comment.