@samitouri / QOS-React / commits / 42b3c89c57

DevTooks: Don't dehydrate hook source fileNames (#21814)

Brian Vaughn committed Jul 7, 2021 at 14:24 UTC 42b3c89c57a6b7027a6599785d63f47f809fda26
3 files changed +18 -20
packages/react-devtools-extensions/src/parseHookNames.js
+1 -19
@@ -27,7 +27,6 @@ import type {Thenable} from 'shared/ReactTypes';
27 import type {SourceConsumer} from './astUtils';
28
29 const SOURCE_MAP_REGEX = / ?sourceMappingURL=([^\s'"]+)/gm;
30 -const ABSOLUTE_URL_REGEX = /^https?:\/\//i;
30 const MAX_SOURCE_LENGTH = 100_000_000;
31
32 type AST = mixed;
@@ -282,14 +281,7 @@ function extractAndLoadSourceMaps(
281 }
282
283 let url = sourceMappingURLs[i].split('=')[1];
285 - if (ABSOLUTE_URL_REGEX.test(url)) {
286 - const baseURL = url.slice(0, url.lastIndexOf('/'));
287 - url = `${baseURL}/${url}`;
288 -
289 - if (!isValidUrl(url)) {
290 - throw new Error(`Invalid source map URL "${url}"`);
291 - }
292 - } else if (!url.startsWith('/')) {
284 + if (!url.startsWith('http') && !url.startsWith('/')) {
285 // Resolve paths relative to the location of the file name
286 const lastSlashIdx = runtimeSourceURL.lastIndexOf('/');
287 if (lastSlashIdx !== -1) {
@@ -440,16 +432,6 @@ function findHookNames(
432 return map;
433 }
434
443 -function isValidUrl(possibleURL: string): boolean {
444 - try {
445 - // eslint-disable-next-line no-new
446 - new URL(possibleURL);
447 - } catch (_) {
448 - return false;
449 - }
450 - return true;
451 -}
452 -
435 function loadSourceFiles(
436 locationKeyToHookSourceData: Map<string, HookSourceData>,
437 ): Promise<*> {
packages/react-devtools-shared/src/backend/renderer.js
+11
@@ -3240,6 +3240,17 @@ export function attach(
3240 // Never dehydrate the "hooks" object at the top levels.
3241 return true;
3242 }
3243 +
3244 + if (
3245 + path[path.length - 2] === 'hookSource' &&
3246 + path[path.length - 1] === 'fileName'
3247 + ) {
3248 + // It's important to preserve the full file name (URL) for hook sources
3249 + // in case the user has enabled the named hooks feature.
3250 + // Otherwise the frontend may end up with a partial URL which it can't load.
3251 + return true;
3252 + }
3253 +
3254 if (
3255 path[path.length - 1] === 'subHooks' ||
3256 path[path.length - 2] === 'subHooks'
packages/react-devtools-shared/src/hydration.js
+6 -1
@@ -160,7 +160,12 @@ export function dehydrate(
160 };
161
162 case 'string':
163 - return data.length <= 500 ? data : data.slice(0, 500) + '...';
163 + isPathAllowedCheck = isPathAllowed(path);
164 + if (isPathAllowedCheck) {
165 + return data;
166 + } else {
167 + return data.length <= 500 ? data : data.slice(0, 500) + '...';
168 + }
169
170 case 'bigint':
171 cleaned.push(path);