Skip to content

Commit 3fb1452

Browse files
committed
fix: validate PerformanceObserver observe options
1 parent 7f396d1 commit 3fb1452

2 files changed

Lines changed: 50 additions & 4 deletions

File tree

packages/react-native/src/private/webapis/performance/PerformanceObserver.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,36 @@ export class PerformanceObserver {
203203
return observerHandle;
204204
}
205205

206-
#validateObserveOptions(options: PerformanceObserverInit): void {
207-
const {type, entryTypes, durationThreshold} = options;
206+
#validateObserveOptions(options: ?PerformanceObserverInit): void {
207+
if (options == null) {
208+
throw new TypeError(
209+
"Failed to execute 'observe' on 'PerformanceObserver': 1 argument required, but only 0 present.",
210+
);
211+
}
212+
213+
const {type, entryTypes, durationThreshold, buffered} = options;
208214

209215
if (!type && !entryTypes) {
210216
throw new TypeError(
211-
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must not include both entryTypes and type arguments.",
217+
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must include either entryTypes or type arguments.",
212218
);
213219
}
214220

215221
if (entryTypes && type) {
216222
throw new TypeError(
217-
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must include either entryTypes or type arguments.",
223+
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must not include both entryTypes and type arguments.",
224+
);
225+
}
226+
227+
if (entryTypes && entryTypes.length === 0) {
228+
throw new TypeError(
229+
"Failed to execute 'observe' on 'PerformanceObserver': The entryTypes sequence must not be empty.",
230+
);
231+
}
232+
233+
if (entryTypes && buffered != null) {
234+
throw new TypeError(
235+
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must not include both entryTypes and buffered arguments.",
218236
);
219237
}
220238

packages/react-native/src/private/webapis/performance/__tests__/PerformanceObserver-itest.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,34 @@ describe('PerformanceObserver', () => {
9696
expect(entries2.getEntries()[1]).toBe(measure);
9797
});
9898

99+
describe('observe()', () => {
100+
it('rejects invalid entry type option combinations before registering with native', () => {
101+
const callback = jest.fn();
102+
const observer = new PerformanceObserver(callback);
103+
104+
// $FlowExpectedError[incompatible-call]
105+
expect(() => observer.observe()).toThrow(
106+
"Failed to execute 'observe' on 'PerformanceObserver': 1 argument required, but only 0 present.",
107+
);
108+
expect(() => observer.observe({})).toThrow(
109+
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must include either entryTypes or type arguments.",
110+
);
111+
expect(() =>
112+
observer.observe({entryTypes: ['mark'], type: 'measure'}),
113+
).toThrow(
114+
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must not include both entryTypes and type arguments.",
115+
);
116+
expect(() => observer.observe({entryTypes: []})).toThrow(
117+
"Failed to execute 'observe' on 'PerformanceObserver': The entryTypes sequence must not be empty.",
118+
);
119+
expect(() =>
120+
observer.observe({entryTypes: ['mark'], buffered: true}),
121+
).toThrow(
122+
"Failed to execute 'observe' on 'PerformanceObserver': An observe() call must not include both entryTypes and buffered arguments.",
123+
);
124+
});
125+
});
126+
99127
describe('takeRecords()', () => {
100128
it('provides all buffered events and clears the buffer', () => {
101129
const callback = jest.fn();

0 commit comments

Comments
 (0)