Skip to content

Commit 6958024

Browse files
rubennortefacebook-github-bot
authored andcommitted
Expose structuredClone and DOMException globally
Summary: Install `DOMException` and `structuredClone` lazily during React Native environment setup so they are available without imports, matching their Web API placement. Add Flow and TypeScript global declarations, including the `DOMException` constants exposed by the implementation. Changelog: [General][Added] - Expose `structuredClone` and `DOMException` as global Web APIs Differential Revision: D122331156
1 parent 0e0be99 commit 6958024

5 files changed

Lines changed: 109 additions & 0 deletions

File tree

‎packages/react-native/__typetests__/globals.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
const noop = () => { };
99

10+
const domException = new DOMException('message', 'DataCloneError');
11+
const domExceptionCode: number = domException.code;
12+
const dataCloneErrorCode: 25 = DOMException.DATA_CLONE_ERR;
13+
const clonedValue: {nested: {value: number}} = structuredClone({
14+
nested: {value: 1},
15+
});
16+
console.log(domExceptionCode, dataCloneErrorCode, clonedValue.nested.value);
17+
1018
function testInterval() {
1119
clearInterval(null);
1220
clearInterval(undefined);

‎packages/react-native/flow/global.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ declare var global: {
8080
readonly RN$Bridgeless?: boolean,
8181

8282
// setupDOM
83+
readonly DOMException: typeof DOMException,
8384
readonly DOMRect: typeof DOMRect,
8485
readonly DOMRectReadOnly: typeof DOMRectReadOnly,
86+
readonly structuredClone: typeof structuredClone,
8587

8688
// Undeclared properties are implicitly `any`.
8789
[string | symbol]: any,

‎packages/react-native/src/private/setup/__tests__/setUpDefaultReactNativeEnvironment-Globals-itest.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,32 @@ describe('setUpDefaultReactNativeEnvironment (globals)', () => {
109109
});
110110

111111
describe('Web APIs', () => {
112+
describe('Structured data', () => {
113+
it('should provide DOMException', () => {
114+
const exception = new DOMException('message', 'DataCloneError');
115+
expect(exception).toBeInstanceOf(Error);
116+
expect(exception.name).toBe('DataCloneError');
117+
expect(exception.code).toBe(DOMException.DATA_CLONE_ERR);
118+
});
119+
120+
it('should provide structuredClone', () => {
121+
const value = {nested: {value: 1}};
122+
const clone = structuredClone(value);
123+
124+
expect(clone).toEqual(value);
125+
expect(clone).not.toBe(value);
126+
expect(clone.nested).not.toBe(value.nested);
127+
128+
let error: unknown;
129+
try {
130+
structuredClone(Symbol());
131+
} catch (thrownError: unknown) {
132+
error = thrownError;
133+
}
134+
expect(error).toBeInstanceOf(DOMException);
135+
});
136+
});
137+
112138
describe('DOM', () => {
113139
it('should provide Node', () => {
114140
expect(typeof Node).toBe('function');

‎packages/react-native/src/private/setup/setUpDOM.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ export default function setUpDOM() {
1919

2020
initialized = true;
2121

22+
polyfillGlobal(
23+
'DOMException',
24+
() => require('../webapis/errors/DOMException').default,
25+
);
26+
27+
polyfillGlobal(
28+
'structuredClone',
29+
() => require('../webapis/structuredClone/structuredClone').default,
30+
);
31+
2232
polyfillGlobal(
2333
'DOMRect',
2434
() => require('../webapis/geometry/DOMRect').default,

‎packages/react-native/src/types/globals.d.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,69 @@ declare global {
5757

5858
const HermesInternal: null | {};
5959

60+
interface DOMException extends Error {
61+
readonly code: number;
62+
readonly message: string;
63+
readonly name: string;
64+
readonly INDEX_SIZE_ERR: 1;
65+
readonly DOMSTRING_SIZE_ERR: 2;
66+
readonly HIERARCHY_REQUEST_ERR: 3;
67+
readonly WRONG_DOCUMENT_ERR: 4;
68+
readonly INVALID_CHARACTER_ERR: 5;
69+
readonly NO_DATA_ALLOWED_ERR: 6;
70+
readonly NO_MODIFICATION_ALLOWED_ERR: 7;
71+
readonly NOT_FOUND_ERR: 8;
72+
readonly NOT_SUPPORTED_ERR: 9;
73+
readonly INUSE_ATTRIBUTE_ERR: 10;
74+
readonly INVALID_STATE_ERR: 11;
75+
readonly SYNTAX_ERR: 12;
76+
readonly INVALID_MODIFICATION_ERR: 13;
77+
readonly NAMESPACE_ERR: 14;
78+
readonly INVALID_ACCESS_ERR: 15;
79+
readonly VALIDATION_ERR: 16;
80+
readonly TYPE_MISMATCH_ERR: 17;
81+
readonly SECURITY_ERR: 18;
82+
readonly NETWORK_ERR: 19;
83+
readonly ABORT_ERR: 20;
84+
readonly URL_MISMATCH_ERR: 21;
85+
readonly QUOTA_EXCEEDED_ERR: 22;
86+
readonly TIMEOUT_ERR: 23;
87+
readonly INVALID_NODE_TYPE_ERR: 24;
88+
readonly DATA_CLONE_ERR: 25;
89+
}
90+
91+
var DOMException: {
92+
prototype: DOMException;
93+
new (message?: string, name?: string): DOMException;
94+
readonly INDEX_SIZE_ERR: 1;
95+
readonly DOMSTRING_SIZE_ERR: 2;
96+
readonly HIERARCHY_REQUEST_ERR: 3;
97+
readonly WRONG_DOCUMENT_ERR: 4;
98+
readonly INVALID_CHARACTER_ERR: 5;
99+
readonly NO_DATA_ALLOWED_ERR: 6;
100+
readonly NO_MODIFICATION_ALLOWED_ERR: 7;
101+
readonly NOT_FOUND_ERR: 8;
102+
readonly NOT_SUPPORTED_ERR: 9;
103+
readonly INUSE_ATTRIBUTE_ERR: 10;
104+
readonly INVALID_STATE_ERR: 11;
105+
readonly SYNTAX_ERR: 12;
106+
readonly INVALID_MODIFICATION_ERR: 13;
107+
readonly NAMESPACE_ERR: 14;
108+
readonly INVALID_ACCESS_ERR: 15;
109+
readonly VALIDATION_ERR: 16;
110+
readonly TYPE_MISMATCH_ERR: 17;
111+
readonly SECURITY_ERR: 18;
112+
readonly NETWORK_ERR: 19;
113+
readonly ABORT_ERR: 20;
114+
readonly URL_MISMATCH_ERR: 21;
115+
readonly QUOTA_EXCEEDED_ERR: 22;
116+
readonly TIMEOUT_ERR: 23;
117+
readonly INVALID_NODE_TYPE_ERR: 24;
118+
readonly DATA_CLONE_ERR: 25;
119+
};
120+
121+
function structuredClone<T>(value: T): T;
122+
60123
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly) */
61124
interface DOMRectReadOnly {
62125
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMRectReadOnly/bottom) */

0 commit comments

Comments
 (0)