Skip to content

Commit

Permalink
[Flight] Try/Catch Eval (#29671)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sebmarkbage authored May 30, 2024
1 parent 9d4fba0 commit 9710853
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ const taskCache: null | WeakMap<
ConsoleTask,
> = supportsCreateTask ? new WeakMap() : null;

type FakeFunction<T> = (FakeFunction<T>) => T;
type FakeFunction<T> = (() => T) => T;
const fakeFunctionCache: Map<string, FakeFunction<any>> = __DEV__
? new Map()
: (null: any);
Expand Down Expand Up @@ -1684,8 +1684,18 @@ function createFakeFunction<T>(
code += '//# sourceURL=' + filename;
}

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

0 comments on commit 9710853

Please sign in to comment.