Skip to content

Commit 2de1802

Browse files
committed
fix: refactor prefix processing at one level higher.
1 parent 09aea9f commit 2de1802

File tree

5 files changed

+85
-36
lines changed

5 files changed

+85
-36
lines changed

src/deploy/functions/build.spec.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -224,27 +224,6 @@ describe("toBackend", () => {
224224
expect(endpointDef.func.serviceAccount).to.equal("service-account-1@");
225225
}
226226
});
227-
228-
it("should apply the prefix to the function name", () => {
229-
const desiredBuild: build.Build = build.of({
230-
func: {
231-
platform: "gcfv1",
232-
region: ["us-central1"],
233-
project: "project",
234-
runtime: "nodejs16",
235-
entryPoint: "func",
236-
httpsTrigger: {},
237-
},
238-
});
239-
const backend = build.toBackend(desiredBuild, {}, "my-prefix");
240-
expect(Object.keys(backend.endpoints).length).to.equal(1);
241-
const regionalEndpoints = Object.values(backend.endpoints)[0];
242-
const endpoint = Object.values(regionalEndpoints)[0];
243-
expect(endpoint).to.not.equal(undefined);
244-
if (endpoint) {
245-
expect(endpoint.id).to.equal("my-prefix-func");
246-
}
247-
});
248227
});
249228

250229
describe("envWithType", () => {

src/deploy/functions/build.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ interface ResolveBackendOpts {
287287
userEnvs: Record<string, string>;
288288
nonInteractive?: boolean;
289289
isEmulator?: boolean;
290-
prefix?: string;
291290
}
292291

293292
/**
@@ -317,7 +316,7 @@ export async function resolveBackend(
317316
}
318317
writeUserEnvs(toWrite, opts.userEnvOpt);
319318

320-
return { backend: toBackend(opts.build, paramValues, opts.prefix), envs: paramValues };
319+
return { backend: toBackend(opts.build, paramValues), envs: paramValues };
321320
}
322321

323322
// Exported for testing
@@ -447,7 +446,6 @@ class Resolver {
447446
export function toBackend(
448447
build: Build,
449448
paramValues: Record<string, params.ParamValue>,
450-
prefix?: string,
451449
): backend.Backend {
452450
const r = new Resolver(paramValues);
453451
const bkEndpoints: Array<backend.Endpoint> = [];
@@ -483,7 +481,7 @@ export function toBackend(
483481
throw new FirebaseError("platform can't be undefined");
484482
}
485483
const bkEndpoint: backend.Endpoint = {
486-
id: prefix ? `${prefix}-${endpointId}` : endpointId,
484+
id: endpointId,
487485
project: bdEndpoint.project,
488486
region: region,
489487
entryPoint: bdEndpoint.entryPoint,

src/deploy/functions/prepare.spec.ts

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { expect } from "chai";
2-
3-
import * as backend from "./backend";
2+
import * as sinon from "sinon";
3+
import * as build from "./build";
44
import * as prepare from "./prepare";
5+
import * as runtimes from "./runtimes";
6+
import { RuntimeDelegate } from "./runtimes";
7+
import { RUNTIMES } from "./runtimes/supported";
8+
import { FirebaseError } from "../../error";
9+
import { Options } from "../../options";
10+
import { ValidatedConfig } from "../../functions/projectConfig";
11+
import * as backend from "./backend";
512
import * as ensureApiEnabled from "../../ensureApiEnabled";
613
import * as serviceusage from "../../gcp/serviceusage";
714
import { BEFORE_CREATE_EVENT, BEFORE_SIGN_IN_EVENT } from "../../functions/events/v1";
8-
import * as sinon from "sinon";
915
import * as prompt from "../../prompt";
10-
import { FirebaseError } from "../../error";
1116

1217
describe("prepare", () => {
1318
const ENDPOINT_BASE: Omit<backend.Endpoint, "httpsTrigger"> = {
@@ -16,14 +21,68 @@ describe("prepare", () => {
1621
region: "region",
1722
project: "project",
1823
entryPoint: "entry",
19-
runtime: "nodejs16",
24+
runtime: "nodejs22",
2025
};
2126

2227
const ENDPOINT: backend.Endpoint = {
2328
...ENDPOINT_BASE,
2429
httpsTrigger: {},
2530
};
2631

32+
describe("loadCodebases", () => {
33+
let sandbox: sinon.SinonSandbox;
34+
let runtimeDelegateStub: RuntimeDelegate;
35+
let discoverBuildStub: sinon.SinonStub;
36+
37+
beforeEach(() => {
38+
sandbox = sinon.createSandbox();
39+
discoverBuildStub = sandbox.stub();
40+
runtimeDelegateStub = {
41+
language: "nodejs",
42+
runtime: "nodejs22",
43+
bin: "node",
44+
validate: sandbox.stub().resolves(),
45+
build: sandbox.stub().resolves(),
46+
watch: sandbox.stub().resolves(() => Promise.resolve()),
47+
discoverBuild: discoverBuildStub,
48+
};
49+
discoverBuildStub.resolves(
50+
build.of({
51+
test: {
52+
platform: "gcfv2",
53+
entryPoint: "test",
54+
project: "project",
55+
runtime: "nodejs22",
56+
httpsTrigger: {},
57+
},
58+
}),
59+
);
60+
sandbox.stub(runtimes, "getRuntimeDelegate").resolves(runtimeDelegateStub);
61+
});
62+
63+
afterEach(() => {
64+
sandbox.restore();
65+
});
66+
67+
it("should apply the prefix to the function name", async () => {
68+
const config: ValidatedConfig = [
69+
{ source: "source", codebase: "codebase", prefix: "my-prefix", runtime: "nodejs22" },
70+
];
71+
const options = {
72+
config: {
73+
path: (p: string) => p,
74+
},
75+
projectId: "project",
76+
} as unknown as Options;
77+
const firebaseConfig = { projectId: "project" };
78+
const runtimeConfig = {};
79+
80+
const builds = await prepare.loadCodebases(config, options, firebaseConfig, runtimeConfig);
81+
82+
expect(Object.keys(builds.codebase.endpoints)).to.deep.equal(["my-prefix-test"]);
83+
});
84+
});
85+
2786
describe("inferDetailsFromExisting", () => {
2887
it("merges env vars if .env is not used", () => {
2988
const oldE = {
@@ -304,7 +363,7 @@ describe("prepare", () => {
304363
region: "us-central1",
305364
project: "project",
306365
entryPoint: "entry",
307-
runtime: "nodejs16",
366+
runtime: "nodejs22",
308367
httpsTrigger: {},
309368
};
310369

@@ -314,7 +373,7 @@ describe("prepare", () => {
314373
region: "us-central1",
315374
project: "project",
316375
entryPoint: "entry",
317-
runtime: "nodejs16",
376+
runtime: "nodejs22",
318377
callableTrigger: {
319378
genkitAction: "action",
320379
},
@@ -333,7 +392,7 @@ describe("prepare", () => {
333392
region: "us-central1",
334393
project: "project",
335394
entryPoint: "entry",
336-
runtime: "nodejs16",
395+
runtime: "nodejs22",
337396
callableTrigger: {
338397
genkitAction: "action",
339398
},

src/deploy/functions/prepare.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ export async function prepare(
135135
userEnvs,
136136
nonInteractive: options.nonInteractive,
137137
isEmulator: false,
138-
prefix: config.prefix,
139138
});
140139

141140
let hasEnvsFromParams = false;
@@ -474,13 +473,21 @@ export async function loadCodebases(
474473
"functions",
475474
`Loading and analyzing source code for codebase ${codebase} to determine what to deploy`,
476475
);
477-
wantBuilds[codebase] = await runtimeDelegate.discoverBuild(runtimeConfig, {
476+
const build = await runtimeDelegate.discoverBuild(runtimeConfig, {
478477
...firebaseEnvs,
479478
// Quota project is required when using GCP's Client-based APIs
480479
// Some GCP client SDKs, like Vertex AI, requires appropriate quota project setup
481480
// in order for .init() calls to succeed.
482481
GOOGLE_CLOUD_QUOTA_PROJECT: projectId,
483482
});
483+
if (codebaseConfig.prefix) {
484+
const newEndpoints: Record<string, build.Endpoint> = {};
485+
for (const id of Object.keys(build.endpoints)) {
486+
newEndpoints[`${codebaseConfig.prefix}-${id}`] = build.endpoints[id];
487+
}
488+
build.endpoints = newEndpoints;
489+
}
490+
wantBuilds[codebase] = build;
484491
wantBuilds[codebase].runtime = codebaseConfig.runtime;
485492
}
486493
return wantBuilds;

src/emulator/functionsEmulator.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,14 +564,20 @@ export class FunctionsEmulator implements EmulatorInstance {
564564
);
565565
await this.loadDynamicExtensionBackends();
566566
}
567+
if (emulatableBackend.prefix) {
568+
const newEndpoints: Record<string, backend.Endpoint> = {};
569+
for (const id of Object.keys(discoveredBuild.endpoints)) {
570+
newEndpoints[`${emulatableBackend.prefix}-${id}`] = discoveredBuild.endpoints[id];
571+
}
572+
discoveredBuild.endpoints = newEndpoints;
573+
}
567574
const resolution = await resolveBackend({
568575
build: discoveredBuild,
569576
firebaseConfig: JSON.parse(firebaseConfig),
570577
userEnvOpt,
571578
userEnvs,
572579
nonInteractive: false,
573580
isEmulator: true,
574-
prefix: emulatableBackend.prefix,
575581
});
576582
const discoveredBackend = resolution.backend;
577583
const endpoints = backend.allEndpoints(discoveredBackend);

0 commit comments

Comments
 (0)