Skip to content

Commit 61a07fa

Browse files
authored
refactor: clean up type casting in region resolution (#10504)
* refactor: clean up type casting in region resolution * Consolidate interface and refactor AILogic checks * Replace interface for individual args * Cleanup ailogic file
1 parent 7c96938 commit 61a07fa

2 files changed

Lines changed: 22 additions & 18 deletions

File tree

src/deploy/functions/prepare.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import * as events from "../../functions/events/v1";
1616
import { getDatabase } from "./services/firestore";
1717
import { getBucket } from "./services/storage";
1818
import { getDatabaseInstanceDetails } from "./services/database";
19-
import { isGlobalAILogicEndpoint } from "./services/ailogic";
19+
import { isGlobalAILogicTrigger } from "./services/ailogic";
2020
import { parseServiceName, parseConnectorName } from "../../dataconnect/names";
2121
import {
2222
functionsOrigin,
@@ -381,11 +381,13 @@ export async function resolveDefaultRegionsForBuild(
381381
} else {
382382
// Match triggers.
383383
try {
384-
const fullEndpoint = { ...endpoint, id } as any;
385384
if (build.isBlockingTriggered(endpoint)) {
386-
resolvedRegion = resolveRegionForBlockingTrigger(fullEndpoint);
385+
resolvedRegion = resolveRegionForBlockingTrigger(endpoint.blockingTrigger);
387386
} else if (build.isEventTriggered(endpoint)) {
388-
resolvedRegion = await resolveRegionForEventTrigger(fullEndpoint);
387+
resolvedRegion = await resolveRegionForEventTrigger(
388+
endpoint.project,
389+
endpoint.eventTrigger,
390+
);
389391
}
390392
} catch (err: any) {
391393
logger.debug(
@@ -400,25 +402,23 @@ export async function resolveDefaultRegionsForBuild(
400402
}
401403
}
402404

403-
function resolveRegionForBlockingTrigger(
404-
endpoint: backend.Endpoint & backend.BlockingTriggered,
405-
): string {
406-
const eventType = endpoint.blockingTrigger.eventType;
405+
function resolveRegionForBlockingTrigger(blockingTrigger: build.BlockingTrigger): string {
406+
const eventType = blockingTrigger.eventType;
407407
if ((events.AUTH_BLOCKING_EVENTS as readonly string[]).includes(eventType)) {
408408
return "us-east1";
409409
}
410410

411-
if (isGlobalAILogicEndpoint(endpoint)) {
411+
if (isGlobalAILogicTrigger(blockingTrigger)) {
412412
return "us-east1";
413413
}
414414

415415
return DEFAULT_FUNCTION_REGION;
416416
}
417417

418418
async function resolveRegionForEventTrigger(
419-
endpoint: backend.Endpoint & backend.EventTriggered,
419+
project: string,
420+
eventTrigger: build.EventTrigger,
420421
): Promise<string> {
421-
const eventTrigger = endpoint.eventTrigger;
422422
const eventType = eventTrigger.eventType;
423423

424424
// Global functions should be deployed to us-east1.
@@ -441,7 +441,7 @@ async function resolveRegionForEventTrigger(
441441
if (eventType.startsWith("google.cloud.firestore.")) {
442442
try {
443443
const databaseId = eventTrigger.eventFilters?.database || "(default)";
444-
const db = await getDatabase(endpoint.project, databaseId);
444+
const db = await getDatabase(project, databaseId);
445445
const locationId = db.locationId.toLowerCase();
446446

447447
if (locationId === "nam5" || locationId === "nam7") return "us-central1";
@@ -481,7 +481,7 @@ async function resolveRegionForEventTrigger(
481481
try {
482482
const instanceName = eventTrigger.eventFilters?.instance;
483483
if (instanceName) {
484-
const details = await getDatabaseInstanceDetails(endpoint.project, instanceName);
484+
const details = await getDatabaseInstanceDetails(project, instanceName);
485485
if (details.location && details.location !== "-") {
486486
return details.location.toLowerCase();
487487
}

src/deploy/functions/services/ailogic.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as backend from "../backend";
2+
import * as build from "../build";
23
import { FirebaseError, getErrStatus } from "../../../error";
34
import { Name, Service } from "./index";
45
import * as ailogicApi from "../../../gcp/ailogic";
@@ -33,11 +34,14 @@ export function isAILogicEvent(endpoint: backend.Endpoint): endpoint is AILogicE
3334
);
3435
}
3536

36-
export function isGlobalAILogicEndpoint(endpoint: backend.Endpoint): boolean {
37-
if (!isAILogicEvent(endpoint)) {
38-
return false;
39-
}
40-
return !endpoint.blockingTrigger.options?.regionalWebhook;
37+
/**
38+
* Check if a blocking trigger is a global AI Logic trigger (not a regional webhook).
39+
*/
40+
export function isGlobalAILogicTrigger(blockingTrigger: build.BlockingTrigger): boolean {
41+
return (
42+
AI_LOGIC_EVENTS.includes(blockingTrigger.eventType as (typeof AI_LOGIC_EVENTS)[number]) &&
43+
!blockingTrigger.options?.regionalWebhook
44+
);
4145
}
4246

4347
export class AILogicService implements Service {

0 commit comments

Comments
 (0)