-
Notifications
You must be signed in to change notification settings - Fork 417
feat(functions): Implement TaskQueue Scopes #3210
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
Open
inlined
wants to merge
6
commits into
firebase:main
Choose a base branch
from
inlined:feat/task-queue-scopes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3f12b70
Security audit: update send-email action dependencies
inlined 8387aad
Security audit: rebuild send-email action dist
inlined e7f717b
feat(functions): Implement TaskQueue Scopes
inlined fa363cc
chore: address pull request review comments
inlined d36ba25
Merge remote-tracking branch 'origin/main' into feat/task-queue-scopes
inlined 94f6d08
chore(functions): Rename KIT_INSTANCE_ID to FIREBASE_KIT_INSTANCE_ID
inlined File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,9 +24,17 @@ import { FirebaseError, toHttpResponse } from '../utils/error'; | |
| import { FirebaseFunctionsError, FunctionsErrorCode, FUNCTIONS_ERROR_CODE_MAPPING } from './error'; | ||
| import * as utils from '../utils/index'; | ||
| import * as validator from '../utils/validator'; | ||
| import { TaskOptions } from './functions-api'; | ||
| import { TaskOptions, FunctionScope } from './functions-api'; | ||
| import { ApplicationDefaultCredential } from '../app/credential-internal'; | ||
|
|
||
| export type InternalFunctionScope = FunctionScope | { | ||
| scope: 'kit'; | ||
| instance: string; | ||
| } | { | ||
| scope: 'extensionOrKit'; | ||
| instance: string; | ||
| }; | ||
|
|
||
| const CLOUD_TASKS_API_RESOURCE_PATH = 'projects/{projectId}/locations/{locationId}/queues/{resourceId}/tasks'; | ||
| const CLOUD_TASKS_API_URL_FORMAT = 'https://cloudtasks.googleapis.com/v2/' + CLOUD_TASKS_API_RESOURCE_PATH; | ||
| const FIREBASE_FUNCTION_URL_FORMAT = 'https://{locationId}-{projectId}.cloudfunctions.net/{resourceId}'; | ||
|
|
@@ -66,9 +74,13 @@ export class FunctionsApiClient { | |
| * | ||
| * @param id - The ID of the task to delete. | ||
| * @param functionName - The function name of the queue. | ||
| * @param extensionId - Optional canonical ID of the extension. | ||
| * @param scope - Optional FunctionScope configuration. | ||
| */ | ||
| public async delete(id: string, functionName: string, extensionId?: string): Promise<void> { | ||
| public async delete( | ||
| id: string, | ||
| functionName: string, | ||
| scope?: InternalFunctionScope | ||
| ): Promise<void> { | ||
| if (!validator.isNonEmptyString(functionName)) { | ||
| throw new FirebaseFunctionsError({ | ||
| code: 'invalid-argument', | ||
|
|
@@ -101,13 +113,13 @@ export class FunctionsApiClient { | |
| message: 'No valid function name specified to enqueue tasks for.' | ||
| }); | ||
| } | ||
| if (typeof extensionId !== 'undefined' && validator.isNonEmptyString(extensionId)) { | ||
| resources.resourceId = `ext-${extensionId}-${resources.resourceId}`; | ||
| } | ||
|
|
||
| const { resourceId } = this.resolveResourceId(resources.resourceId, scope); | ||
| const targetResources = { ...resources, resourceId }; | ||
|
|
||
| try { | ||
| const serviceUrl = tasksEmulatorUrl(resources, this.emulatorHost)?.concat('/', id) | ||
| ?? await this.getUrl(resources, CLOUD_TASKS_API_URL_FORMAT.concat('/', id)); | ||
| const serviceUrl = tasksEmulatorUrl(targetResources, this.emulatorHost)?.concat('/', id) | ||
| ?? await this.getUrl(targetResources, CLOUD_TASKS_API_URL_FORMAT.concat('/', id)); | ||
| const request: HttpRequestConfig = { | ||
| method: 'DELETE', | ||
| url: serviceUrl, | ||
|
|
@@ -116,10 +128,6 @@ export class FunctionsApiClient { | |
| await this.httpClient.send(request); | ||
| } catch (err: unknown) { | ||
| if (err instanceof RequestResponseError) { | ||
| if (err.response.status === 404) { | ||
| // if no task with the provided ID exists, then ignore the delete. | ||
| return; | ||
| } | ||
| throw this.toFirebaseError(err); | ||
| } else { | ||
| throw err; | ||
|
|
@@ -132,10 +140,15 @@ export class FunctionsApiClient { | |
| * | ||
| * @param data - The data payload of the task. | ||
| * @param functionName - The functionName of the queue. | ||
| * @param extensionId - Optional canonical ID of the extension. | ||
| * @param scope - Optional FunctionScope configuration. | ||
| * @param opts - Optional options when enqueuing a new task. | ||
| */ | ||
| public async enqueue(data: any, functionName: string, extensionId?: string, opts?: TaskOptions): Promise<void> { | ||
| public async enqueue( | ||
| data: any, | ||
| functionName: string, | ||
| scope?: InternalFunctionScope, | ||
| opts?: TaskOptions | ||
| ): Promise<void> { | ||
| if (!validator.isNonEmptyString(functionName)) { | ||
| throw new FirebaseFunctionsError({ | ||
| code: 'invalid-argument', | ||
|
|
@@ -161,17 +174,17 @@ export class FunctionsApiClient { | |
| message: 'No valid function name specified to enqueue tasks for.' | ||
| }); | ||
| } | ||
| if (typeof extensionId !== 'undefined' && validator.isNonEmptyString(extensionId)) { | ||
| resources.resourceId = `ext-${extensionId}-${resources.resourceId}`; | ||
| } | ||
|
|
||
| const task = this.validateTaskOptions(data, resources, opts); | ||
| const { resourceId, extensionOrKitId } = this.resolveResourceId(resources.resourceId, scope); | ||
| const targetResources = { ...resources, resourceId }; | ||
|
|
||
| try { | ||
| const task = this.validateTaskOptions(data, targetResources, opts); | ||
| const serviceUrl = | ||
| tasksEmulatorUrl(resources, this.emulatorHost) ?? | ||
| await this.getUrl(resources, CLOUD_TASKS_API_URL_FORMAT); | ||
| tasksEmulatorUrl(targetResources, this.emulatorHost) ?? | ||
| await this.getUrl(targetResources, CLOUD_TASKS_API_URL_FORMAT); | ||
|
|
||
| const taskPayload = await this.updateTaskPayload(task, resources, extensionId); | ||
| const taskPayload = await this.updateTaskPayload(task, targetResources, extensionOrKitId); | ||
| const request: HttpRequestConfig = { | ||
| method: 'POST', | ||
| url: serviceUrl, | ||
|
|
@@ -199,6 +212,54 @@ export class FunctionsApiClient { | |
| } | ||
| } | ||
|
|
||
| private resolveResourceId( | ||
| resourceId: string, | ||
| scope?: InternalFunctionScope | ||
| ): { resourceId: string; extensionOrKitId?: string } { | ||
| if (typeof scope === 'undefined') { | ||
| return this.resolveResourceId(resourceId, { scope: 'current' }); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use a default parameter for private resolveResourceId (
resourceId : string ,
scope : InternalFunctionScope = { scope: 'current' }
)... |
||
| } | ||
|
|
||
| switch (scope.scope) { | ||
| case 'current': { | ||
| const extInstanceId = process.env.EXT_INSTANCE_ID; | ||
| if (validator.isNonEmptyString(extInstanceId)) { | ||
| return { | ||
| resourceId: `ext-${extInstanceId}-${resourceId}`, | ||
| extensionOrKitId: extInstanceId, | ||
| }; | ||
| } | ||
| const kitInstanceId = process.env.FIREBASE_KIT_INSTANCE_ID; | ||
| if (validator.isNonEmptyString(kitInstanceId)) { | ||
| return { | ||
| resourceId: `kit-${kitInstanceId}-${resourceId}`, | ||
| extensionOrKitId: kitInstanceId, | ||
| }; | ||
| } | ||
| return { resourceId }; | ||
| } | ||
| case 'global': | ||
| return { resourceId }; | ||
| case 'extension': | ||
| return { | ||
| resourceId: `ext-${scope.instance}-${resourceId}`, | ||
| extensionOrKitId: scope.instance, | ||
| }; | ||
| case 'kit': // kit scope is secretly accepted for forward compatibility | ||
| return { | ||
| resourceId: `kit-${scope.instance}-${resourceId}`, | ||
| extensionOrKitId: scope.instance, | ||
| }; | ||
| case 'extensionOrKit': | ||
| return { | ||
| resourceId: `ext-${scope.instance}-${resourceId}`, | ||
| extensionOrKitId: scope.instance, | ||
| }; | ||
| default: | ||
| return { resourceId }; | ||
| } | ||
| } | ||
|
|
||
| private getUrl(resourceName: utils.ParsedResource, urlFormat: string): Promise<string> { | ||
| let { locationId } = resourceName; | ||
| const { projectId, resourceId } = resourceName; | ||
|
|
@@ -349,7 +410,11 @@ export class FunctionsApiClient { | |
| return task; | ||
| } | ||
|
|
||
| private async updateTaskPayload(task: Task, resources: utils.ParsedResource, extensionId?: string): Promise<Task> { | ||
| private async updateTaskPayload( | ||
| task: Task, | ||
| resources: utils.ParsedResource, | ||
| extensionOrKitId?: string | ||
| ): Promise<Task> { | ||
| const defaultUrl = this.emulatorHost ? | ||
| '' | ||
| : await this.getUrl(resources, FIREBASE_FUNCTION_URL_FORMAT); | ||
|
|
@@ -360,7 +425,7 @@ export class FunctionsApiClient { | |
|
|
||
| task.httpRequest.url = functionUrl; | ||
| // When run from a deployed extension, we should be using ComputeEngineCredentials | ||
| if (validator.isNonEmptyString(extensionId) && this.app.options.credential | ||
| if (validator.isNonEmptyString(extensionOrKitId) && this.app.options.credential | ||
| instanceof ApplicationDefaultCredential && await this.app.options.credential.isComputeEngineCredential()) { | ||
| const idToken = await this.app.options.credential.getIDToken(functionUrl); | ||
| task.httpRequest.headers = { ...task.httpRequest.headers, 'Authorization': `Bearer ${idToken}` }; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we get a TW review on these new docstrings, please?