@samitouri / QOS-React / commits / 9710853baf

[Flight] Try/Catch Eval (#29671)

Follow up to https://github.com/facebook/react/pull/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.

Sebastian Markbåge committed May 30, 2024 at 15:00 UTC 9710853baf9649fed556dc0e9d39765649675b7b
1 file changed +13 -3
packages/react-client/src/ReactFlightClient.js
+13 -3
@@ -1648,7 +1648,7 @@ const taskCache: null | WeakMap<
1648 ConsoleTask,
1649 > = supportsCreateTask ? new WeakMap() : null;
1650
1651 -type FakeFunction<T> = (FakeFunction<T>) => T;
1651 +type FakeFunction<T> = (() => T) => T;
1652 const fakeFunctionCache: Map<string, FakeFunction<any>> = __DEV__
1653 ? new Map()
1654 : (null: any);
@@ -1684,8 +1684,18 @@ function createFakeFunction<T>(
1684 code += '//# sourceURL=' + filename;
1685 }
1686
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 + }
1699 // $FlowFixMe[cannot-write]
1700 Object.defineProperty(fn, 'name', {value: name || '(anonymous)'});
1701 // $FlowFixMe[prop-missing]