Skip to content

Commit 9710853

Browse files
authored
[Flight] Try/Catch Eval (#29671)
Follow up to #29632. It's possible for `eval` to throw such as if we're in a CSP environment. This is non-essential debug information. We can still proceed to create a fake stack entry. It'll still have the right name. It just won't have the right line/col number nor source url/source map. It might also be ignored listed since it's inside Flight.
1 parent 9d4fba0 commit 9710853

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ const taskCache: null | WeakMap<
16481648
ConsoleTask,
16491649
> = supportsCreateTask ? new WeakMap() : null;
16501650

1651-
type FakeFunction<T> = (FakeFunction<T>) => T;
1651+
type FakeFunction<T> = (() => T) => T;
16521652
const fakeFunctionCache: Map<string, FakeFunction<any>> = __DEV__
16531653
? new Map()
16541654
: (null: any);
@@ -1684,8 +1684,18 @@ function createFakeFunction<T>(
16841684
code += '//# sourceURL=' + filename;
16851685
}
16861686

1687-
// eslint-disable-next-line no-eval
1688-
const fn: FakeFunction<T> = (0, eval)(code);
1687+
let fn: FakeFunction<T>;
1688+
try {
1689+
// eslint-disable-next-line no-eval
1690+
fn = (0, eval)(code);
1691+
} catch (x) {
1692+
// If eval fails, such as if in an environment that doesn't support it,
1693+
// we fallback to creating a function here. It'll still have the right
1694+
// name but it'll lose line/column number and file name.
1695+
fn = function (_) {
1696+
return _();
1697+
};
1698+
}
16891699
// $FlowFixMe[cannot-write]
16901700
Object.defineProperty(fn, 'name', {value: name || '(anonymous)'});
16911701
// $FlowFixMe[prop-missing]

0 commit comments

Comments
 (0)