Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9145702
feat: add extension.yaml adapter for Functions deployment
taeold Aug 17, 2025
4238d98
refactor: improve extensionAdapter test file
taeold Aug 17, 2025
108ba75
refactor: simplify extensionAdapter code
taeold Aug 17, 2025
1b2aa27
refactor: unify endpoint creation and remove any types
taeold Aug 17, 2025
5bc2318
refactor: simplify parameter conversion logic
taeold Aug 17, 2025
a3ff006
refactor: remove runtime parameter from extensionAdapter
taeold Aug 17, 2025
d44834a
fix: address code review feedback for extensionAdapter
taeold Aug 17, 2025
1a5ff94
refactor: simplify IAM and lifecycle handling with TODOs
taeold Aug 17, 2025
3000da5
refactor: simplify extensionAdapter using existing utilities
taeold Aug 17, 2025
f6a5f91
style: clean up extensionAdapter per code review
taeold Aug 17, 2025
4f55af6
fix: address extensionAdapter code review feedback and add edge case …
taeold Aug 17, 2025
90b33fa
refactor: improve extensionAdapter based on code review feedback
taeold Aug 18, 2025
27486aa
fix: address additional extensionAdapter review feedback
taeold Aug 18, 2025
d62d0ff
fix: optimize extension detection and clean up code
taeold Aug 18, 2025
3a8f3d2
feat: add default Firestore v2 filters for parity with emulator
taeold Aug 18, 2025
9f154da
test: update expected output for v2-function test
taeold Aug 18, 2025
8a6cabc
refactor: remove redundant endpoints initialization
taeold Aug 18, 2025
12b3dee
Merge branch 'master' into extension-adapter-support
taeold Aug 18, 2025
786f483
fix: convert multiSelect extension params to list type in functions
taeold Aug 19, 2025
07bcfda
fix: auto-detect numeric select params and convert to IntParam
taeold Aug 19, 2025
319553b
docs: add comprehensive documentation about system params in extensio…
taeold Aug 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/deploy/functions/extensionAdapter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { expect } from "chai";
import * as fs from "fs";
import * as path from "path";
import { detectAndAdaptExtension } from "./extensionAdapter";

describe("extensionAdapter golden tests", () => {
const fixturesDir = path.resolve(__dirname, "../../../src/test/fixtures/extensionAdapter");

const getFixtures = (): string[] => {
return fs.readdirSync(fixturesDir).filter((dir) => {
const stats = fs.statSync(path.join(fixturesDir, dir));
return (
stats.isDirectory() &&
fs.existsSync(path.join(fixturesDir, dir, "extension.yaml")) &&
fs.existsSync(path.join(fixturesDir, dir, "expected.json"))
);
});
};

const fixtures = getFixtures();

fixtures.forEach((fixtureName) => {
it(`should correctly convert ${fixtureName}`, async () => {
const fixtureDir = path.join(fixturesDir, fixtureName);
const expectedPath = path.join(fixtureDir, "expected.json");
const expected = JSON.parse(fs.readFileSync(expectedPath, "utf8"));

Check warning on line 26 in src/deploy/functions/extensionAdapter.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

const result = await detectAndAdaptExtension(fixtureDir, "test-project");

expect(result).to.not.be.undefined;
expect(result).to.deep.equal(expected);
});
});

it("should return undefined for directory without extension.yaml", async () => {
const result = await detectAndAdaptExtension("/tmp/no-extension", "test-project");

expect(result).to.be.undefined;
});
});
332 changes: 332 additions & 0 deletions src/deploy/functions/extensionAdapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,332 @@
import * as path from "path";
import * as fs from "fs";
import { logger } from "../../logger";
import { FirebaseError } from "../../error";
import * as build from "./build";
import * as params from "./params";
import * as api from "../../api";
import * as proto from "../../gcp/proto";
import { readExtensionYaml, DEFAULT_RUNTIME } from "../../extensions/emulator/specHelper";
import { getResourceRuntime } from "../../extensions/utils";
import {
Resource,
Param,
FUNCTIONS_RESOURCE_TYPE,
FUNCTIONS_V2_RESOURCE_TYPE,
FunctionResourceProperties,
FunctionV2ResourceProperties,
} from "../../extensions/types";

// Reuse validFunctionTypes from specHelper
const validFunctionTypes = [
FUNCTIONS_RESOURCE_TYPE,
FUNCTIONS_V2_RESOURCE_TYPE,
"firebaseextensions.v1beta.scheduledFunction",
];

/**
* Convert extension parameter references to CEL format
* ${param:NAME} -> {{ params.NAME }}
* ${NAME} -> {{ params.NAME }} (some extensions use this shorthand)
*/
function convertParamReference(value: string): string {
return value
.replace(/\${param:([^}]+)}/g, "{{ params.$1 }}")
.replace(/\${([^:}]+)}/g, "{{ params.$1 }}");
}

/**
* Process any field that might contain parameter references
*/
function processField<T>(value: T): T {
if (value === null || value === undefined) {
return value;
}

if (typeof value === "string") {
if (value.includes("${param:") || (value.includes("${") && value.includes("}"))) {
return convertParamReference(value) as T;
}
return value;
}

if (Array.isArray(value)) {
return value.map(processField) as T;
}

if (typeof value === "object") {
const processed: Record<string, unknown> = {};
for (const [key, val] of Object.entries(value)) {
processed[key] = processField(val);
}
return processed as T;
}

return value;
}

/**
* Convert an extension resource to a functions deployment endpoint
*/
function createEndpoint(resource: Resource, projectId: string): build.Endpoint {
const runtime = getResourceRuntime(resource) || DEFAULT_RUNTIME;
const isV2 = resource.type === FUNCTIONS_V2_RESOURCE_TYPE;

const v1Resource = resource as Resource & {
properties?: FunctionResourceProperties["properties"];
};
const v2Resource = resource as Resource & {
properties?: FunctionV2ResourceProperties["properties"];
};

const location = isV2 ? v2Resource.properties?.location : v1Resource.properties?.location;
const baseEndpoint = {
entryPoint: resource.entryPoint || resource.name,
platform: (isV2 ? "gcfv2" : "gcfv1") as "gcfv1" | "gcfv2",

Check warning on line 85 in src/deploy/functions/extensionAdapter.ts

View workflow job for this annotation

GitHub Actions / lint (20)

This assertion is unnecessary since it does not change the type of the expression
project: projectId,
runtime,
region: [processField(location || api.functionsDefaultRegion())],
};

let endpoint: build.Endpoint;

if (isV2 && v2Resource.properties) {
const props = v2Resource.properties;
if (props.eventTrigger) {
const eventTrigger: build.EventTrigger = {
eventType: props.eventTrigger.eventType,
retry: props.eventTrigger.retryPolicy === "RETRY_POLICY_RETRY",
};

if (props.eventTrigger.eventFilters) {
eventTrigger.eventFilters = {};
for (const filter of props.eventTrigger.eventFilters) {
eventTrigger.eventFilters[filter.attribute] = processField(filter.value);
}
}
if (props.eventTrigger.channel) {
eventTrigger.channel = processField(props.eventTrigger.channel);
}
if (props.eventTrigger.triggerRegion) {
eventTrigger.region = processField(props.eventTrigger.triggerRegion);
}

endpoint = { ...baseEndpoint, eventTrigger };
} else {
endpoint = { ...baseEndpoint, httpsTrigger: {} };
}
} else if (!isV2 && v1Resource.properties) {
const props = v1Resource.properties;
if (props.eventTrigger) {
const eventTrigger: build.EventTrigger = {
eventType: props.eventTrigger.eventType,
retry: false,
};

if (props.eventTrigger.resource || props.eventTrigger.service) {
eventTrigger.eventFilters = {};
if (props.eventTrigger.resource) {
eventTrigger.eventFilters.resource = processField(props.eventTrigger.resource);
}
if (props.eventTrigger.service) {
eventTrigger.eventFilters.service = processField(props.eventTrigger.service);
}
}

endpoint = { ...baseEndpoint, eventTrigger };
} else if (props.scheduleTrigger) {
endpoint = {
...baseEndpoint,
scheduleTrigger: {
schedule: processField(props.scheduleTrigger.schedule) || "",
timeZone: processField(props.scheduleTrigger.timeZone) || null,
},
};
} else if (props.taskQueueTrigger) {
const taskQueueTrigger: build.TaskQueueTrigger = {};
if (props.taskQueueTrigger.rateLimits) {
taskQueueTrigger.rateLimits = processField(props.taskQueueTrigger.rateLimits);
}
if (props.taskQueueTrigger.retryConfig) {
taskQueueTrigger.retryConfig = processField(props.taskQueueTrigger.retryConfig);
}
endpoint = { ...baseEndpoint, taskQueueTrigger };
} else {
endpoint = { ...baseEndpoint, httpsTrigger: {} };
}
} else {
endpoint = { ...baseEndpoint, httpsTrigger: {} };
}

if (!isV2 && v1Resource.properties) {
if (v1Resource.properties.timeout) {
const timeout = v1Resource.properties.timeout;
if (typeof timeout === "string" && (timeout.includes("${param:") || timeout.includes("${"))) {
endpoint.timeoutSeconds = processField(timeout);
} else {
endpoint.timeoutSeconds = proto.secondsFromDuration(timeout);
}
}
if (v1Resource.properties.availableMemoryMb !== undefined) {
endpoint.availableMemoryMb = processField(v1Resource.properties.availableMemoryMb);
}
} else if (isV2 && v2Resource.properties?.serviceConfig) {
const serviceConfig = v2Resource.properties.serviceConfig;
proto.copyIfPresent(endpoint, serviceConfig, "timeoutSeconds");
if (endpoint.timeoutSeconds !== undefined) {
endpoint.timeoutSeconds = processField(endpoint.timeoutSeconds);
}
if (serviceConfig.availableMemory !== undefined) {
const mem = serviceConfig.availableMemory;
if (typeof mem === "string" && (mem.includes("${param:") || mem.includes("${"))) {
endpoint.availableMemoryMb = processField(mem);
} else if (typeof mem === "string") {
endpoint.availableMemoryMb = parseInt(mem);
} else {
endpoint.availableMemoryMb = mem;
}
}
proto.renameIfPresent(endpoint, serviceConfig, "minInstances", "minInstanceCount");
proto.renameIfPresent(endpoint, serviceConfig, "maxInstances", "maxInstanceCount");
if (endpoint.minInstances !== undefined) {
endpoint.minInstances = processField(endpoint.minInstances);
}
if (endpoint.maxInstances !== undefined) {
endpoint.maxInstances = processField(endpoint.maxInstances);
}
}

return endpoint;
}

/**
* Convert extension params to build params
*/
function convertParam(param: Param): params.Param {
if (param.type === "secret") {
return {
type: "secret",
name: param.param,
};
}

const stringParam: params.StringParam = {
type: "string",
name: param.param,
label: param.label,
};

if (param.description !== undefined) stringParam.description = param.description;
if (param.immutable !== undefined) stringParam.immutable = param.immutable;
if (param.default !== undefined) stringParam.default = processField(String(param.default));

// Handle different input types
switch (param.type) {
case "select": {
if (param.options) {
stringParam.input = {
select: {
options: param.options.map((opt) => ({
label: opt.label || String(opt.value),
value: String(opt.value),
})),
},
};
}
break;
}
case "multiSelect": {
if (param.options) {
stringParam.input = {
multiSelect: {
options: param.options.map((opt) => ({
label: opt.label || String(opt.value),
value: String(opt.value),
})),
},
};
}
break;
}
case "selectResource":
stringParam.input = {
resource: {
type: param.resourceType || "storage.googleapis.com/Bucket",
},
};
break;
default:
if (param.validationRegex) {
const text: any = {

Check warning on line 260 in src/deploy/functions/extensionAdapter.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
validationRegex: param.validationRegex,
};
if (param.validationErrorMessage !== undefined) {
text.validationErrorMessage = param.validationErrorMessage;

Check warning on line 264 in src/deploy/functions/extensionAdapter.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .validationErrorMessage on an `any` value
}
if (param.example !== undefined) {
text.example = param.example;

Check warning on line 267 in src/deploy/functions/extensionAdapter.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .example on an `any` value
}
stringParam.input = { text };

Check warning on line 269 in src/deploy/functions/extensionAdapter.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
}
}

return stringParam;
}

/**
* Detect and convert extension.yaml to functions Build format for deployment
*/
export async function detectAndAdaptExtension(
projectDir: string,
projectId: string,
): Promise<build.Build | undefined> {
const extensionYamlPath = path.join(projectDir, "extension.yaml");

try {
await fs.promises.access(extensionYamlPath);
} catch {
return undefined;
}

const extensionSpec = await readExtensionYaml(projectDir);

if (!extensionSpec.name || !extensionSpec.version) {
throw new FirebaseError("extension.yaml is missing required fields: name or version");
}

if (!extensionSpec.resources || extensionSpec.resources.length === 0) {
throw new FirebaseError("extension.yaml must contain at least one resource");
}

logger.info(`Detected extension "${extensionSpec.name}" v${extensionSpec.version}`);
logger.info("Adapting extension.yaml for functions deployment...");

// TODO: Handle IAM roles - extensions can require specific IAM roles for their service account
// TODO: Support lifecycle events (onInstall, onUpdate, onConfigure) - these are task queue functions

const functionsBuild = build.empty();

functionsBuild.endpoints = {};
for (const resource of extensionSpec.resources) {
if (validFunctionTypes.includes(resource.type)) {
functionsBuild.endpoints[resource.name] = createEndpoint(resource, projectId);
}
}

if (Object.keys(functionsBuild.endpoints).length === 0) {
throw new FirebaseError("No function resources found in extension.yaml");
}

logger.info(`Found ${Object.keys(functionsBuild.endpoints).length} function(s) in extension`);

functionsBuild.params = (extensionSpec.params || []).map(convertParam);

if (extensionSpec.apis) {
functionsBuild.requiredAPIs = extensionSpec.apis.map((api) => ({
api: api.apiName,
reason: api.reason,
}));
}

return functionsBuild;
}
2 changes: 1 addition & 1 deletion src/deploy/functions/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
};
}

interface SecretParam {
export interface SecretParam {
type: "secret";

// name of the param. Will be exposed as an environment variable with this name
Expand Down Expand Up @@ -268,7 +268,7 @@
return pv;
}

setDelimiter(delimiter: string) {

Check warning on line 271 in src/deploy/functions/params.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
this.delimiter = delimiter;
}

Expand All @@ -295,7 +295,7 @@
if (this.rawValue.includes("[")) {
// Convert quotes to apostrophes
const unquoted = this.rawValue.replace(/'/g, '"');
return JSON.parse(unquoted);

Check warning on line 298 in src/deploy/functions/params.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe return of an `any` typed value
}

// Continue to handle something like "a,b,c"
Expand Down Expand Up @@ -413,7 +413,7 @@
}
if (paramDefault && !canSatisfyParam(param, paramDefault)) {
throw new FirebaseError(
"Parameter " + param.name + " has default value " + paramDefault + " of wrong type",

Check warning on line 416 in src/deploy/functions/params.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Operands of '+' operation must either be both strings or both numbers. Consider using a template literal
);
}
paramValues[param.name] = await promptParam(param, firebaseConfig.projectId, paramDefault);
Expand Down Expand Up @@ -460,7 +460,7 @@
* to read its environment variables. They are instead provided through GCF's own
* Secret Manager integration.
*/
async function handleSecret(secretParam: SecretParam, projectId: string) {

Check warning on line 463 in src/deploy/functions/params.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
const metadata = await secretManager.getSecretMetadata(projectId, secretParam.name, "latest");
if (!metadata.secret) {
const secretValue = await password({
Expand Down
3 changes: 2 additions & 1 deletion src/extensions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export interface FunctionResourceProperties {
scheduleTrigger?: Record<string, string>;
taskQueueTrigger?: {
rateLimits?: {
maxConcurrentDispatchs?: number;
maxConcurrentDispatches?: number;
maxDispatchesPerSecond?: number;
};
retryConfig?: {
Expand Down Expand Up @@ -267,6 +267,7 @@ export interface Param {
immutable?: boolean;
example?: string;
advanced?: boolean;
resourceType?: string; // Required when type is SELECT_RESOURCE or selectResource
}

export enum ParamType {
Expand Down
Loading
Loading