@@ -516,51 +516,52 @@ export function scheduleIdForFunction(cloudFunction: TargetIds): string {
516
516
* @param forceRefresh If true, ignores and overwrites the cache. These cases should eventually go away.
517
517
* @return The backend
518
518
*/
519
- export async function existingBackend ( context : Context , forceRefresh ?: boolean ) : Promise < Backend > {
520
- if ( ! context . loadedExistingBackend || forceRefresh ) {
521
- await loadExistingBackend ( context ) ;
519
+ export function existingBackend ( context : Context , forceRefresh ?: boolean ) : Promise < Backend > {
520
+ if ( ! context . existingBackendPromise || forceRefresh ) {
521
+ context . existingBackendPromise = loadExistingBackend ( context ) ;
522
522
}
523
- // loadExisting guarantees the validity of existingBackend and unreachableRegions
524
- return context . existingBackend ! ;
523
+ return context . existingBackendPromise ;
525
524
}
526
525
527
- async function loadExistingBackend ( ctx : Context ) : Promise < void > {
528
- ctx . loadedExistingBackend = true ;
526
+ async function loadExistingBackend ( ctx : Context ) : Promise < Backend > {
529
527
// Note: is it worth deducing the APIs that must have been enabled for this backend to work?
530
528
// it could reduce redundant API calls for enabling the APIs.
531
- ctx . existingBackend = {
529
+ const existingBackend = {
532
530
...empty ( ) ,
533
531
} ;
534
- ctx . unreachableRegions = {
535
- gcfV1 : [ ] ,
536
- gcfV2 : [ ] ,
537
- run : [ ] ,
532
+ const unreachableRegions = {
533
+ gcfV1 : [ ] as string [ ] ,
534
+ gcfV2 : [ ] as string [ ] ,
535
+ run : [ ] as string [ ] ,
538
536
} ;
539
537
const gcfV1Results = await gcf . listAllFunctions ( ctx . projectId ) ;
540
538
for ( const apiFunction of gcfV1Results . functions ) {
541
539
const endpoint = gcf . endpointFromFunction ( apiFunction ) ;
542
- ctx . existingBackend . endpoints [ endpoint . region ] =
543
- ctx . existingBackend . endpoints [ endpoint . region ] || { } ;
544
- ctx . existingBackend . endpoints [ endpoint . region ] [ endpoint . id ] = endpoint ;
540
+ existingBackend . endpoints [ endpoint . region ] = existingBackend . endpoints [ endpoint . region ] || { } ;
541
+ existingBackend . endpoints [ endpoint . region ] [ endpoint . id ] = endpoint ;
545
542
}
546
- ctx . unreachableRegions . gcfV1 = gcfV1Results . unreachable ;
543
+ unreachableRegions . gcfV1 = gcfV1Results . unreachable ;
547
544
548
545
let gcfV2Results ;
549
546
try {
550
547
gcfV2Results = await gcfV2 . listAllFunctions ( ctx . projectId ) ;
551
548
for ( const apiFunction of gcfV2Results . functions ) {
552
549
const endpoint = gcfV2 . endpointFromFunction ( apiFunction ) ;
553
- ctx . existingBackend . endpoints [ endpoint . region ] =
554
- ctx . existingBackend . endpoints [ endpoint . region ] || { } ;
555
- ctx . existingBackend . endpoints [ endpoint . region ] [ endpoint . id ] = endpoint ;
550
+ existingBackend . endpoints [ endpoint . region ] = existingBackend . endpoints [ endpoint . region ] || { } ;
551
+ existingBackend . endpoints [ endpoint . region ] [ endpoint . id ] = endpoint ;
556
552
}
557
- ctx . unreachableRegions . gcfV2 = gcfV2Results . unreachable ;
553
+ unreachableRegions . gcfV2 = gcfV2Results . unreachable ;
558
554
} catch ( err : any ) {
559
555
if ( err . status === 404 && err . message ?. toLowerCase ( ) . includes ( "method not found" ) ) {
560
- return ; // customer has preview enabled without allowlist set
556
+ // customer has preview enabled without allowlist set
557
+ } else {
558
+ throw err ;
561
559
}
562
- throw err ;
563
560
}
561
+
562
+ ctx . existingBackend = existingBackend ;
563
+ ctx . unreachableRegions = unreachableRegions ;
564
+ return ctx . existingBackend ;
564
565
}
565
566
566
567
/**
@@ -572,9 +573,7 @@ async function loadExistingBackend(ctx: Context): Promise<void> {
572
573
* @param want The desired backend. Can be backend.empty() to only warn about unavailability.
573
574
*/
574
575
export async function checkAvailability ( context : Context , want : Backend ) : Promise < void > {
575
- if ( ! context . loadedExistingBackend ) {
576
- await loadExistingBackend ( context ) ;
577
- }
576
+ await existingBackend ( context ) ;
578
577
const gcfV1Regions = new Set ( ) ;
579
578
const gcfV2Regions = new Set ( ) ;
580
579
for ( const ep of allEndpoints ( want ) ) {
0 commit comments