Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse Firebase extensions from code #1590

Merged
merged 12 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Future Extensions support
taeold marked this conversation as resolved.
Show resolved Hide resolved
46 changes: 46 additions & 0 deletions scripts/bin-test/extsdks/local/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* TaskQueue/LifecycleEvent/RuntimeStatus Tester SDK for [email protected]
*
* When filing bugs or feature requests please specify:
* "Extensions SDK v1.0.0 for Local extension.
* https://github.com/firebase/firebase-tools/issues/new/choose
*
* GENERATED FILE. DO NOT EDIT.
*/
export type DoBackfillParam = "True" | "False";
export type LocationParam =
| "us-central1"
| "us-east1"
| "us-east4"
| "europe-west1"
| "europe-west2"
| "europe-west3"
| "asia-east2"
| "asia-northeast1";
/**
* Parameters for [email protected] extension
*/
export interface BackfillParams {
/**
* Do a backfill
*/
DO_BACKFILL: DoBackfillParam;
/**
* Cloud Functions location
*/
LOCATION: LocationParam;
}
export declare function backfill(instanceId: string, params: BackfillParams): Backfill;
/**
* TaskQueue/LifecycleEvent/RuntimeStatus Tester
* A tester for the TaskQueue/LCE/RuntimeStatus project
*/
export declare class Backfill {
private instanceId;
private params;
readonly FIREBASE_EXTENSION_LOCAL_PATH =
"./functions/generated/extensions/local/backfill/0.0.2/src";
constructor(instanceId: string, params: BackfillParams);
getInstanceId(): string;
getParams(): BackfillParams;
}
30 changes: 30 additions & 0 deletions scripts/bin-test/extsdks/local/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict";
/**
* TaskQueue/LifecycleEvent/RuntimeStatus Tester SDK for [email protected]
*
* When filing bugs or feature requests please specify:
* "Extensions SDK v1.0.0 for Local extension.
* https://github.com/firebase/firebase-tools/issues/new/choose
*
* GENERATED FILE. DO NOT EDIT.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.backfill = exports.backfill = void 0;
function backfill(instanceId, params) {
return new Backfill(instanceId, params);
}
exports.backfill = backfill;
/**
* TaskQueue/LifecycleEvent/RuntimeStatus Tester
* A tester for the TaskQueue/LCE/RuntimeStatus project
*/
class Backfill {
constructor(instanceId, params) {
this.instanceId = instanceId;
this.params = params;
this.FIREBASE_EXTENSION_LOCAL_PATH = "./functions/generated/extensions/local/backfill/0.0.2/src";
}
getInstanceId() { return this.instanceId; }
getParams() { return this.params; }
}
exports.Backfill = Backfill;
4 changes: 4 additions & 0 deletions scripts/bin-test/extsdks/local/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@firebase-extensions/local-backfill-sdk",
"main": "./index.js"
}
169 changes: 169 additions & 0 deletions scripts/bin-test/extsdks/translate/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/**
* Translate Text in Firestore SDK for [email protected]
*
* When filing bugs or feature requests please specify:
* "Extensions SDK v1.0.0 for [email protected]"
* https://github.com/firebase/firebase-tools/issues/new/choose
*
* GENERATED FILE. DO NOT EDIT.
*/
import { CloudEvent } from "../../../../v2";
import { EventarcTriggerOptions } from "../../../../v2/eventarc";
export type EventCallback<T> = (event: CloudEvent<T>) => unknown | Promise<unknown>;
export type SimpleEventarcTriggerOptions = Omit<
EventarcTriggerOptions,
"eventType" | "channel" | "region"
>;
export type EventArcRegionType = "us-central1" | "us-west1" | "europe-west4" | "asia-northeast1";
export type SystemFunctionVpcConnectorEgressSettingsParam =
| "VPC_CONNECTOR_EGRESS_SETTINGS_UNSPECIFIED"
| "PRIVATE_RANGES_ONLY"
| "ALL_TRAFFIC";
export type SystemFunctionIngressSettingsParam =
| "ALLOW_ALL"
| "ALLOW_INTERNAL_ONLY"
| "ALLOW_INTERNAL_AND_GCLB";
export type SystemFunctionLocationParam =
| "us-central1"
| "us-east1"
| "us-east4"
| "us-west1"
| "us-west2"
| "us-west3"
| "us-west4"
| "europe-central2"
| "europe-west1"
| "europe-west2"
| "europe-west3"
| "europe-west6"
| "asia-east1"
| "asia-east2"
| "asia-northeast1"
| "asia-northeast2"
| "asia-northeast3"
| "asia-south1"
| "asia-southeast1"
| "asia-southeast2"
| "northamerica-northeast1"
| "southamerica-east1"
| "australia-southeast1";
export type SystemFunctionMemoryParam = "128" | "256" | "512" | "1024" | "2048" | "4096" | "8192";
/**
* Parameters for [email protected] extension
*/
export interface FirestoreTranslateTextParams {
/**
* Target languages for translations, as a comma-separated list
*/
LANGUAGES: string;
/**
* Collection path
*/
COLLECTION_PATH: string;
/**
* Input field name
*/
INPUT_FIELD_NAME: string;
/**
* Translations output field name
*/
OUTPUT_FIELD_NAME: string;
/**
* Languages field name
*/
LANGUAGES_FIELD_NAME?: string;
/**
* Event Arc Region
*/
_EVENT_ARC_REGION?: EventArcRegionType;
/**
* Function timeout seconds
*/
_FUNCTION_TIMEOUT_SECONDS?: string;
/**
* VPC Connector
*/
_FUNCTION_VPC_CONNECTOR?: string;
/**
* VPC Connector Egress settings
*/
_FUNCTION_VPC_CONNECTOR_EGRESS_SETTINGS?: SystemFunctionVpcConnectorEgressSettingsParam;
/**
* Minimum function instances
*/
_FUNCTION_MIN_INSTANCES?: string;
/**
* Maximum function instances
*/
_FUNCTION_MAX_INSTANCES?: string;
/**
* Function ingress settings
*/
_FUNCTION_INGRESS_SETTINGS?: SystemFunctionIngressSettingsParam;
/**
* Function labels
*/
_FUNCTION_LABELS?: string;
/**
* KMS key name
*/
_FUNCTION_KMS_KEY_NAME?: string;
/**
* Docker repository
*/
_FUNCTION_DOCKER_REPOSITORY?: string;
/**
* Cloud Functions location
*/
_FUNCTION_LOCATION: SystemFunctionLocationParam;
/**
* Function memory
*/
_FUNCTION_MEMORY?: SystemFunctionMemoryParam;
}
export declare function firestoreTranslateText(
instanceId: string,
params: FirestoreTranslateTextParams
): FirestoreTranslateText;
/**
* Translate Text in Firestore
* Translates strings written to a Cloud Firestore collection into multiple languages (uses Cloud Translation API).
*/
export declare class FirestoreTranslateText {
private instanceId;
private params;
events: string[];
readonly FIREBASE_EXTENSION_REFERENCE = "firebase/[email protected]";
readonly EXTENSION_VERSION = "0.1.18";
constructor(instanceId: string, params: FirestoreTranslateTextParams);
getInstanceId(): string;
getParams(): FirestoreTranslateTextParams;
/**
* Occurs when a trigger has been called within the Extension, and will include data such as the context of the trigger request.
*/
onStart<T = unknown>(
callback: EventCallback<T>,
options?: SimpleEventarcTriggerOptions
): import("firebase-functions/v2").CloudFunction<CloudEvent<T>>;
/**
* Occurs when image resizing completes successfully. The event will contain further details about specific formats and sizes.
*/
onSuccess<T = unknown>(
callback: EventCallback<T>,
options?: SimpleEventarcTriggerOptions
): import("firebase-functions/v2").CloudFunction<CloudEvent<T>>;
/**
* Occurs when an issue has been experienced in the Extension. This will include any error data that has been included within the Error Exception.
*/
onError<T = unknown>(
callback: EventCallback<T>,
options?: SimpleEventarcTriggerOptions
): import("firebase-functions/v2").CloudFunction<CloudEvent<T>>;
/**
* Occurs when the function is settled. Provides no customized data other than the context.
*/
onCompletion<T = unknown>(
callback: EventCallback<T>,
options?: SimpleEventarcTriggerOptions
): import("firebase-functions/v2").CloudFunction<CloudEvent<T>>;
}
61 changes: 61 additions & 0 deletions scripts/bin-test/extsdks/translate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"use strict";
/**
* Translate Text in Firestore SDK for [email protected]
*
* When filing bugs or feature requests please specify:
* "Extensions SDK v1.0.0 for [email protected]"
* https://github.com/firebase/firebase-tools/issues/new/choose
*
* GENERATED FILE. DO NOT EDIT.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.FirestoreTranslateText = exports.firestoreTranslateText = void 0;
const eventarc_1 = require("firebase-functions/v2/eventarc");
function firestoreTranslateText(instanceId, params) {
return new FirestoreTranslateText(instanceId, params);
}
exports.firestoreTranslateText = firestoreTranslateText;
/**
* Translate Text in Firestore
* Translates strings written to a Cloud Firestore collection into multiple languages (uses Cloud Translation API).
*/
class FirestoreTranslateText {
constructor(instanceId, params) {
this.instanceId = instanceId;
this.params = params;
this.events = [];
this.FIREBASE_EXTENSION_REFERENCE = "firebase/[email protected]";
this.EXTENSION_VERSION = "0.1.18";
}
getInstanceId() { return this.instanceId; }
getParams() { return this.params; }
/**
* Occurs when a trigger has been called within the Extension, and will include data such as the context of the trigger request.
*/
onStart(callback, options) {
this.events.push("firebase.extensions.firestore-translate-text.v1.onStart");
return (0, eventarc_1.onCustomEventPublished)(Object.assign(Object.assign({}, options), { "eventType": "firebase.extensions.firestore-translate-text.v1.onStart", "channel": `projects/locations/${this.params._EVENT_ARC_REGION}/channels/firebase`, "region": `${this.params._EVENT_ARC_REGION}` }), callback);
}
/**
* Occurs when image resizing completes successfully. The event will contain further details about specific formats and sizes.
*/
onSuccess(callback, options) {
this.events.push("firebase.extensions.firestore-translate-text.v1.onSuccess");
return (0, eventarc_1.onCustomEventPublished)(Object.assign(Object.assign({}, options), { "eventType": "firebase.extensions.firestore-translate-text.v1.onSuccess", "channel": `projects/locations/${this.params._EVENT_ARC_REGION}/channels/firebase`, "region": `${this.params._EVENT_ARC_REGION}` }), callback);
}
/**
* Occurs when an issue has been experienced in the Extension. This will include any error data that has been included within the Error Exception.
*/
onError(callback, options) {
this.events.push("firebase.extensions.firestore-translate-text.v1.onError");
return (0, eventarc_1.onCustomEventPublished)(Object.assign(Object.assign({}, options), { "eventType": "firebase.extensions.firestore-translate-text.v1.onError", "channel": `projects/locations/${this.params._EVENT_ARC_REGION}/channels/firebase`, "region": `${this.params._EVENT_ARC_REGION}` }), callback);
}
/**
* Occurs when the function is settled. Provides no customized data other than the context.
*/
onCompletion(callback, options) {
this.events.push("firebase.extensions.firestore-translate-text.v1.onCompletion");
return (0, eventarc_1.onCustomEventPublished)(Object.assign(Object.assign({}, options), { "eventType": "firebase.extensions.firestore-translate-text.v1.onCompletion", "channel": `projects/locations/${this.params._EVENT_ARC_REGION}/channels/firebase`, "region": `${this.params._EVENT_ARC_REGION}` }), callback);
}
}
exports.FirestoreTranslateText = FirestoreTranslateText;
4 changes: 4 additions & 0 deletions scripts/bin-test/extsdks/translate/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@firebase-extensions/firebase-firestore-translate-text-sdk",
"main": "./index.js"
}
9 changes: 8 additions & 1 deletion scripts/bin-test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ set -ex # Immediately exit on failure
npm run build
npm link

# Link local SDK to all test sources.
# Link the extensions SDKs for the testing environment.
(cd scripts/bin-test/extsdks/local && npm link)
(cd scripts/bin-test/extsdks/translate && npm link)
(cd scripts/bin-test/extsdks/translate && npm link firebase-functions)

# Link SDKs to all test sources.
for f in scripts/bin-test/sources/*; do
if [ -d "$f" ]; then
(cd "$f" && npm link firebase-functions)
(cd "$f" && npm link @firebase-extensions/firebase-firestore-translate-text-sdk)
(cd "$f" && npm link @firebase-extensions/local-backfill-sdk)
Comment on lines +17 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of adding extensions "functions" as a test case on every single set up, can we instead separate out extensions test cases as unique ones?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's the same if-else code chunk being tested... I don't want someone to "forget" to run half the tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think anybody runs this test individually but instead runs it via this test script.

I think it'd be cleaner to separate out extension use case - I see it as a specialized use case and isn't representative of most CF3 users set up.

fi
done

Expand Down
27 changes: 27 additions & 0 deletions scripts/bin-test/sources/commonjs-grouped/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const functions = require("firebase-functions");
const functionsv2 = require("firebase-functions/v2");
const firestoreTranslateText = require("@firebase-extensions/firebase-firestore-translate-text-sdk").firestoreTranslateText;
const backfill = require("@firebase-extensions/local-backfill-sdk").backfill;


exports.v1http = functions.https.onRequest((req, resp) => {
resp.status(200).send("PASS");
Expand All @@ -17,4 +20,28 @@ exports.v2callable = functionsv2.https.onCall(() => {
return "PASS";
});

// A Firebase extension by ref
const extRef1 = firestoreTranslateText("extRef1", {
"COLLECTION_PATH": "collection1",
"INPUT_FIELD_NAME": "input1",
"LANGUAGES": "de,es",
"OUTPUT_FIELD_NAME": "translated",
"_EVENT_ARC_REGION": "us-central1",
"_FUNCTION_LOCATION": "us-central1",
});
exports.extRef1 = extRef1;

// A Firebase function defined by extension event
const ttOnStart = extRef1.onStart((event) => {
console.log("onStart got event: " + JSON.stringify(event, null, 2));
});
exports.ttOnStart = ttOnStart;

// A Firebase extension by localPath
const extLocal2 = backfill("extLocal2", {
DO_BACKFILL: "False",
LOCATION: "us-central1",
});
exports.extLocal2 = extLocal2;

exports.g1 = require("./g1");
Loading
Loading