4545// This is the fastest option since our custom metadata file is much smaller than a full source map,
4646// and there is no need to convert runtime code to the original source.
4747
48- import LRU from 'lru-cache' ;
4948import { __DEBUG__ } from 'react-devtools-shared/src/constants' ;
5049import { getHookSourceLocationKey } from 'react-devtools-shared/src/hookNamesCache' ;
5150import { sourceMapIncludesSource } from '../SourceMapUtils' ;
@@ -55,7 +54,6 @@ import {
5554 withSyncPerformanceMark ,
5655} from 'react-devtools-shared/src/PerformanceMarks' ;
5756
58- import type { LRUCache } from 'react-devtools-shared/src/types' ;
5957import type {
6058 HooksNode ,
6159 HookSource ,
@@ -70,14 +68,6 @@ const FETCH_OPTIONS = {cache: 'force-cache'};
7068
7169const MAX_SOURCE_LENGTH = 100_000_000 ;
7270
73- // Fetch requests originated from an extension might not have origin headers
74- // which may prevent subsequent requests from using cached responses
75- // if the server returns a Vary: 'Origin' header
76- // so this cache will temporarily store pre-fetches sources in memory.
77- const prefetchedSources : LRUCache < string , string > = new LRU ( {
78- max : 15 ,
79- } ) ;
80-
8171export type HookSourceAndMetadata = { |
8272 // Generated by react-debug-tools.
8373 hookSource : HookSource ,
@@ -462,109 +452,47 @@ function loadSourceFiles(
462452 locationKeyToHookSourceAndMetadata . forEach ( hookSourceAndMetadata => {
463453 const { runtimeSourceURL} = hookSourceAndMetadata ;
464454
465- const prefetchedSourceCode = prefetchedSources . get ( runtimeSourceURL ) ;
466- if ( prefetchedSourceCode != null ) {
467- hookSourceAndMetadata . runtimeSourceCode = prefetchedSourceCode ;
468- } else {
469- let fetchFileFunction = fetchFile ;
470- if ( fetchFileWithCaching != null ) {
471- // If a helper function has been injected to fetch with caching,
472- // use it to fetch the (already loaded) source file.
473- fetchFileFunction = url => {
474- return withAsyncPerformanceMark (
475- `fetchFileWithCaching("${ url } ")` ,
476- ( ) => {
477- return ( ( fetchFileWithCaching : any ) : FetchFileWithCaching ) ( url ) ;
478- } ,
479- ) ;
480- } ;
481- }
455+ let fetchFileFunction = fetchFile ;
456+ if ( fetchFileWithCaching != null ) {
457+ // If a helper function has been injected to fetch with caching,
458+ // use it to fetch the (already loaded) source file.
459+ fetchFileFunction = url => {
460+ return withAsyncPerformanceMark (
461+ `fetchFileWithCaching("${ url } ")` ,
462+ ( ) => {
463+ return ( ( fetchFileWithCaching : any ) : FetchFileWithCaching ) ( url ) ;
464+ } ,
465+ ) ;
466+ } ;
467+ }
482468
483- const fetchPromise =
484- dedupedFetchPromises . get ( runtimeSourceURL ) ||
485- fetchFileFunction ( runtimeSourceURL ) . then ( runtimeSourceCode => {
486- // TODO (named hooks) Re-think this; the main case where it matters is when there's no source-maps,
487- // because then we need to parse the full source file as an AST.
488- if ( runtimeSourceCode . length > MAX_SOURCE_LENGTH ) {
489- throw Error ( 'Source code too large to parse' ) ;
490- }
469+ const fetchPromise =
470+ dedupedFetchPromises . get ( runtimeSourceURL ) ||
471+ fetchFileFunction ( runtimeSourceURL ) . then ( runtimeSourceCode => {
472+ // TODO (named hooks) Re-think this; the main case where it matters is when there's no source-maps,
473+ // because then we need to parse the full source file as an AST.
474+ if ( runtimeSourceCode . length > MAX_SOURCE_LENGTH ) {
475+ throw Error ( 'Source code too large to parse' ) ;
476+ }
491477
492- if ( __DEBUG__ ) {
493- console . groupCollapsed (
494- `loadSourceFiles() runtimeSourceURL "${ runtimeSourceURL } "` ,
495- ) ;
496- console . log ( runtimeSourceCode ) ;
497- console . groupEnd ( ) ;
498- }
478+ if ( __DEBUG__ ) {
479+ console . groupCollapsed (
480+ `loadSourceFiles() runtimeSourceURL "${ runtimeSourceURL } "` ,
481+ ) ;
482+ console . log ( runtimeSourceCode ) ;
483+ console . groupEnd ( ) ;
484+ }
499485
500- return runtimeSourceCode ;
501- } ) ;
502- dedupedFetchPromises . set ( runtimeSourceURL , fetchPromise ) ;
486+ return runtimeSourceCode ;
487+ } ) ;
488+ dedupedFetchPromises . set ( runtimeSourceURL , fetchPromise ) ;
503489
504- setterPromises . push (
505- fetchPromise . then ( runtimeSourceCode => {
506- hookSourceAndMetadata . runtimeSourceCode = runtimeSourceCode ;
507- } ) ,
508- ) ;
509- }
490+ setterPromises . push (
491+ fetchPromise . then ( runtimeSourceCode => {
492+ hookSourceAndMetadata . runtimeSourceCode = runtimeSourceCode ;
493+ } ) ,
494+ ) ;
510495 } ) ;
511496
512497 return Promise . all ( setterPromises ) ;
513498}
514-
515- export function prefetchSourceFiles (
516- hooksTree : HooksTree ,
517- fetchFileWithCaching : FetchFileWithCaching | null ,
518- ) : void {
519- // Deduplicate fetches, since there can be multiple location keys per source map.
520- const dedupedFetchPromises = new Set ( ) ;
521-
522- let fetchFileFunction = null ;
523- if ( fetchFileWithCaching != null ) {
524- // If a helper function has been injected to fetch with caching,
525- // use it to fetch the (already loaded) source file.
526- fetchFileFunction = url => {
527- return withAsyncPerformanceMark (
528- `[pre] fetchFileWithCaching("${ url } ")` ,
529- ( ) => {
530- return ( ( fetchFileWithCaching : any ) : FetchFileWithCaching ) ( url ) ;
531- } ,
532- ) ;
533- } ;
534- } else {
535- fetchFileFunction = url => fetchFile ( url , '[pre] fetchFile' ) ;
536- }
537-
538- const hooksQueue = Array . from ( hooksTree ) ;
539-
540- for ( let i = 0 ; i < hooksQueue . length ; i ++ ) {
541- const hook = hooksQueue . pop ( ) ;
542- if ( isUnnamedBuiltInHook ( hook ) ) {
543- continue ;
544- }
545-
546- const hookSource = hook . hookSource ;
547- if ( hookSource == null ) {
548- continue ;
549- }
550-
551- const runtimeSourceURL = ( ( hookSource . fileName : any ) : string ) ;
552-
553- if ( prefetchedSources . has ( runtimeSourceURL ) ) {
554- // If we've already fetched this source, skip it.
555- continue ;
556- }
557-
558- if ( ! dedupedFetchPromises . has ( runtimeSourceURL ) ) {
559- dedupedFetchPromises . add ( runtimeSourceURL ) ;
560-
561- fetchFileFunction ( runtimeSourceURL ) . then ( text => {
562- prefetchedSources . set ( runtimeSourceURL , text ) ;
563- } ) ;
564- }
565-
566- if ( hook . subHooks . length > 0 ) {
567- hooksQueue . push ( ...hook . subHooks ) ;
568- }
569- }
570- }
0 commit comments