Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DevTooks: Don't dehydrate hook source fileNames #21814

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions packages/react-devtools-extensions/src/parseHookNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import type {Thenable} from 'shared/ReactTypes';
import type {SourceConsumer} from './astUtils';

const SOURCE_MAP_REGEX = / ?sourceMappingURL=([^\s'"]+)/gm;
const ABSOLUTE_URL_REGEX = /^https?:\/\//i;
const MAX_SOURCE_LENGTH = 100_000_000;

type AST = mixed;
Expand Down Expand Up @@ -282,14 +281,7 @@ function extractAndLoadSourceMaps(
}

let url = sourceMappingURLs[i].split('=')[1];
if (ABSOLUTE_URL_REGEX.test(url)) {
const baseURL = url.slice(0, url.lastIndexOf('/'));
url = `${baseURL}/${url}`;

if (!isValidUrl(url)) {
throw new Error(`Invalid source map URL "${url}"`);
}
} else if (!url.startsWith('/')) {
if (!url.startsWith('http') && !url.startsWith('/')) {
// Resolve paths relative to the location of the file name
const lastSlashIdx = runtimeSourceURL.lastIndexOf('/');
if (lastSlashIdx !== -1) {
Expand Down Expand Up @@ -440,16 +432,6 @@ function findHookNames(
return map;
}

function isValidUrl(possibleURL: string): boolean {
try {
// eslint-disable-next-line no-new
new URL(possibleURL);
} catch (_) {
return false;
}
return true;
}

function loadSourceFiles(
locationKeyToHookSourceData: Map<string, HookSourceData>,
): Promise<*> {
Expand Down
11 changes: 11 additions & 0 deletions packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3240,6 +3240,17 @@ export function attach(
// Never dehydrate the "hooks" object at the top levels.
return true;
}

if (
path[path.length - 2] === 'hookSource' &&
path[path.length - 1] === 'fileName'
) {
// It's important to preserve the full file name (URL) for hook sources
// in case the user has enabled the named hooks feature.
// Otherwise the frontend may end up with a partial URL which it can't load.
return true;
}

if (
path[path.length - 1] === 'subHooks' ||
path[path.length - 2] === 'subHooks'
Expand Down
7 changes: 6 additions & 1 deletion packages/react-devtools-shared/src/hydration.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ export function dehydrate(
};

case 'string':
return data.length <= 500 ? data : data.slice(0, 500) + '...';
isPathAllowedCheck = isPathAllowed(path);
if (isPathAllowedCheck) {
return data;
} else {
return data.length <= 500 ? data : data.slice(0, 500) + '...';
}

case 'bigint':
cleaned.push(path);
Expand Down