initial commit

This commit is contained in:
2025-09-01 22:12:29 +02:00
parent b1873f9c1d
commit 02a54f61c0
5598 changed files with 903558 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
/*! firebase-admin v13.5.0 */
/*!
* @license
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { PrefixedFirebaseError } from '../utils/error';
export declare const EMULATED_SERVICE_ACCOUNT_DEFAULT = "emulated-service-acct@email.com";
/**
* Task is a limited subset of https://cloud.google.com/tasks/docs/reference/rest/v2/projects.locations.queues.tasks#resource:-task
* containing the relevant fields for enqueueing tasks that tirgger Cloud Functions.
*/
export interface Task {
name?: string;
scheduleTime?: string;
dispatchDeadline?: string;
httpRequest: {
url: string;
oidcToken?: {
serviceAccountEmail: string;
};
body: string;
headers: {
[key: string]: string;
};
};
}
export declare const FUNCTIONS_ERROR_CODE_MAPPING: {
[key: string]: FunctionsErrorCode;
};
export type FunctionsErrorCode = 'aborted' | 'invalid-argument' | 'invalid-credential' | 'internal-error' | 'failed-precondition' | 'permission-denied' | 'unauthenticated' | 'not-found' | 'unknown-error' | 'task-already-exists';
/**
* Firebase Functions error code structure. This extends PrefixedFirebaseError.
*
* @param code - The error code.
* @param message - The error message.
* @constructor
*/
export declare class FirebaseFunctionsError extends PrefixedFirebaseError {
constructor(code: FunctionsErrorCode, message: string);
}

View File

@@ -0,0 +1,372 @@
/*! firebase-admin v13.5.0 */
"use strict";
/*!
* @license
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.FirebaseFunctionsError = exports.FUNCTIONS_ERROR_CODE_MAPPING = exports.FunctionsApiClient = exports.EMULATED_SERVICE_ACCOUNT_DEFAULT = void 0;
const api_request_1 = require("../utils/api-request");
const error_1 = require("../utils/error");
const utils = require("../utils/index");
const validator = require("../utils/validator");
const credential_internal_1 = require("../app/credential-internal");
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}';
exports.EMULATED_SERVICE_ACCOUNT_DEFAULT = 'emulated-service-acct@email.com';
const FIREBASE_FUNCTIONS_CONFIG_HEADERS = {
'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`
};
// Default canonical location ID of the task queue.
const DEFAULT_LOCATION = 'us-central1';
/**
* Class that facilitates sending requests to the Firebase Functions backend API.
*
* @internal
*/
class FunctionsApiClient {
constructor(app) {
this.app = app;
if (!validator.isNonNullObject(app) || !('options' in app)) {
throw new FirebaseFunctionsError('invalid-argument', 'First argument passed to getFunctions() must be a valid Firebase app instance.');
}
this.httpClient = new FunctionsHttpClient(app);
}
/**
* Deletes a task from a queue.
*
* @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.
*/
async delete(id, functionName, extensionId) {
if (!validator.isNonEmptyString(functionName)) {
throw new FirebaseFunctionsError('invalid-argument', 'Function name must be a non empty string');
}
if (!validator.isTaskId(id)) {
throw new FirebaseFunctionsError('invalid-argument', 'id can contain only letters ([A-Za-z]), numbers ([0-9]), '
+ 'hyphens (-), or underscores (_). The maximum length is 500 characters.');
}
let resources;
try {
resources = utils.parseResourceName(functionName, 'functions');
}
catch (err) {
throw new FirebaseFunctionsError('invalid-argument', 'Function name must be a single string or a qualified resource name');
}
resources.projectId = resources.projectId || await this.getProjectId();
resources.locationId = resources.locationId || DEFAULT_LOCATION;
if (!validator.isNonEmptyString(resources.resourceId)) {
throw new FirebaseFunctionsError('invalid-argument', 'No valid function name specified to enqueue tasks for.');
}
if (typeof extensionId !== 'undefined' && validator.isNonEmptyString(extensionId)) {
resources.resourceId = `ext-${extensionId}-${resources.resourceId}`;
}
try {
const serviceUrl = tasksEmulatorUrl(resources)?.concat('/', id)
?? await this.getUrl(resources, CLOUD_TASKS_API_URL_FORMAT.concat('/', id));
const request = {
method: 'DELETE',
url: serviceUrl,
headers: FIREBASE_FUNCTIONS_CONFIG_HEADERS,
};
await this.httpClient.send(request);
}
catch (err) {
if (err instanceof api_request_1.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;
}
}
}
/**
* Creates a task and adds it to a queue.
*
* @param data - The data payload of the task.
* @param functionName - The functionName of the queue.
* @param extensionId - Optional canonical ID of the extension.
* @param opts - Optional options when enqueuing a new task.
*/
async enqueue(data, functionName, extensionId, opts) {
if (!validator.isNonEmptyString(functionName)) {
throw new FirebaseFunctionsError('invalid-argument', 'Function name must be a non empty string');
}
let resources;
try {
resources = utils.parseResourceName(functionName, 'functions');
}
catch (err) {
throw new FirebaseFunctionsError('invalid-argument', 'Function name must be a single string or a qualified resource name');
}
resources.projectId = resources.projectId || await this.getProjectId();
resources.locationId = resources.locationId || DEFAULT_LOCATION;
if (!validator.isNonEmptyString(resources.resourceId)) {
throw new FirebaseFunctionsError('invalid-argument', '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);
try {
const serviceUrl = tasksEmulatorUrl(resources) ??
await this.getUrl(resources, CLOUD_TASKS_API_URL_FORMAT);
const taskPayload = await this.updateTaskPayload(task, resources, extensionId);
const request = {
method: 'POST',
url: serviceUrl,
headers: FIREBASE_FUNCTIONS_CONFIG_HEADERS,
data: {
task: taskPayload,
}
};
await this.httpClient.send(request);
}
catch (err) {
if (err instanceof api_request_1.RequestResponseError) {
if (err.response.status === 409) {
throw new FirebaseFunctionsError('task-already-exists', `A task with ID ${opts?.id} already exists`);
}
else {
throw this.toFirebaseError(err);
}
}
else {
throw err;
}
}
}
getUrl(resourceName, urlFormat) {
let { locationId } = resourceName;
const { projectId, resourceId } = resourceName;
if (typeof locationId === 'undefined' || !validator.isNonEmptyString(locationId)) {
locationId = DEFAULT_LOCATION;
}
return Promise.resolve()
.then(() => {
if (typeof projectId !== 'undefined' && validator.isNonEmptyString(projectId)) {
return projectId;
}
return this.getProjectId();
})
.then((projectId) => {
const urlParams = {
projectId,
locationId,
resourceId,
};
// Formats a string of form 'project/{projectId}/{api}' and replaces
// with corresponding arguments {projectId: '1234', api: 'resource'}
// and returns output: 'project/1234/resource'.
return utils.formatString(urlFormat, urlParams);
});
}
getProjectId() {
if (this.projectId) {
return Promise.resolve(this.projectId);
}
return utils.findProjectId(this.app)
.then((projectId) => {
if (!validator.isNonEmptyString(projectId)) {
throw new FirebaseFunctionsError('unknown-error', 'Failed to determine project ID. Initialize the '
+ 'SDK with service account credentials or set project ID as an app option. '
+ 'Alternatively, set the GOOGLE_CLOUD_PROJECT environment variable.');
}
this.projectId = projectId;
return projectId;
});
}
getServiceAccount() {
if (this.accountId) {
return Promise.resolve(this.accountId);
}
return utils.findServiceAccountEmail(this.app)
.then((accountId) => {
if (!validator.isNonEmptyString(accountId)) {
throw new FirebaseFunctionsError('unknown-error', 'Failed to determine service account. Initialize the '
+ 'SDK with service account credentials or set service account ID as an app option.');
}
this.accountId = accountId;
return accountId;
});
}
validateTaskOptions(data, resources, opts) {
const task = {
httpRequest: {
url: '',
oidcToken: {
serviceAccountEmail: '',
},
body: Buffer.from(JSON.stringify({ data })).toString('base64'),
headers: {
'Content-Type': 'application/json',
...opts?.headers,
}
}
};
if (typeof opts !== 'undefined') {
if (!validator.isNonNullObject(opts)) {
throw new FirebaseFunctionsError('invalid-argument', 'TaskOptions must be a non-null object');
}
if ('scheduleTime' in opts && 'scheduleDelaySeconds' in opts) {
throw new FirebaseFunctionsError('invalid-argument', 'Both scheduleTime and scheduleDelaySeconds are provided. '
+ 'Only one value should be set.');
}
if ('scheduleTime' in opts && typeof opts.scheduleTime !== 'undefined') {
if (!(opts.scheduleTime instanceof Date)) {
throw new FirebaseFunctionsError('invalid-argument', 'scheduleTime must be a valid Date object.');
}
task.scheduleTime = opts.scheduleTime.toISOString();
}
if ('scheduleDelaySeconds' in opts && typeof opts.scheduleDelaySeconds !== 'undefined') {
if (!validator.isNumber(opts.scheduleDelaySeconds) || opts.scheduleDelaySeconds < 0) {
throw new FirebaseFunctionsError('invalid-argument', 'scheduleDelaySeconds must be a non-negative duration in seconds.');
}
const date = new Date();
date.setSeconds(date.getSeconds() + opts.scheduleDelaySeconds);
task.scheduleTime = date.toISOString();
}
if (typeof opts.dispatchDeadlineSeconds !== 'undefined') {
if (!validator.isNumber(opts.dispatchDeadlineSeconds) || opts.dispatchDeadlineSeconds < 15
|| opts.dispatchDeadlineSeconds > 1800) {
throw new FirebaseFunctionsError('invalid-argument', 'dispatchDeadlineSeconds must be a non-negative duration in seconds '
+ 'and must be in the range of 15s to 30 mins.');
}
task.dispatchDeadline = `${opts.dispatchDeadlineSeconds}s`;
}
if ('id' in opts && typeof opts.id !== 'undefined') {
if (!validator.isTaskId(opts.id)) {
throw new FirebaseFunctionsError('invalid-argument', 'id can contain only letters ([A-Za-z]), numbers ([0-9]), '
+ 'hyphens (-), or underscores (_). The maximum length is 500 characters.');
}
const resourcePath = utils.formatString(CLOUD_TASKS_API_RESOURCE_PATH, {
projectId: resources.projectId,
locationId: resources.locationId,
resourceId: resources.resourceId,
});
task.name = resourcePath.concat('/', opts.id);
}
if (typeof opts.uri !== 'undefined') {
if (!validator.isURL(opts.uri)) {
throw new FirebaseFunctionsError('invalid-argument', 'uri must be a valid URL string.');
}
task.httpRequest.url = opts.uri;
}
}
return task;
}
async updateTaskPayload(task, resources, extensionId) {
const defaultUrl = process.env.CLOUD_TASKS_EMULATOR_HOST ?
''
: await this.getUrl(resources, FIREBASE_FUNCTION_URL_FORMAT);
const functionUrl = validator.isNonEmptyString(task.httpRequest.url)
? task.httpRequest.url
: defaultUrl;
task.httpRequest.url = functionUrl;
// When run from a deployed extension, we should be using ComputeEngineCredentials
if (validator.isNonEmptyString(extensionId) && this.app.options.credential
instanceof credential_internal_1.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}` };
// Don't send httpRequest.oidcToken if we set Authorization header, or Cloud Tasks will overwrite it.
delete task.httpRequest.oidcToken;
}
else {
try {
const account = await this.getServiceAccount();
task.httpRequest.oidcToken = { serviceAccountEmail: account };
}
catch (e) {
if (process.env.CLOUD_TASKS_EMULATOR_HOST) {
task.httpRequest.oidcToken = { serviceAccountEmail: exports.EMULATED_SERVICE_ACCOUNT_DEFAULT };
}
else {
throw e;
}
}
}
return task;
}
toFirebaseError(err) {
if (err instanceof error_1.PrefixedFirebaseError) {
return err;
}
const response = err.response;
if (!response.isJson()) {
return new FirebaseFunctionsError('unknown-error', `Unexpected response with status: ${response.status} and body: ${response.text}`);
}
const error = response.data.error || {};
let code = 'unknown-error';
if (error.status && error.status in exports.FUNCTIONS_ERROR_CODE_MAPPING) {
code = exports.FUNCTIONS_ERROR_CODE_MAPPING[error.status];
}
const message = error.message || `Unknown server error: ${response.text}`;
return new FirebaseFunctionsError(code, message);
}
}
exports.FunctionsApiClient = FunctionsApiClient;
/**
* Functions-specific HTTP client which uses the special "owner" token
* when communicating with the Emulator.
*/
class FunctionsHttpClient extends api_request_1.AuthorizedHttpClient {
getToken() {
if (process.env.CLOUD_TASKS_EMULATOR_HOST) {
return Promise.resolve('owner');
}
return super.getToken();
}
}
exports.FUNCTIONS_ERROR_CODE_MAPPING = {
ABORTED: 'aborted',
INVALID_ARGUMENT: 'invalid-argument',
INVALID_CREDENTIAL: 'invalid-credential',
INTERNAL: 'internal-error',
FAILED_PRECONDITION: 'failed-precondition',
PERMISSION_DENIED: 'permission-denied',
UNAUTHENTICATED: 'unauthenticated',
NOT_FOUND: 'not-found',
UNKNOWN: 'unknown-error',
};
/**
* Firebase Functions error code structure. This extends PrefixedFirebaseError.
*
* @param code - The error code.
* @param message - The error message.
* @constructor
*/
class FirebaseFunctionsError extends error_1.PrefixedFirebaseError {
constructor(code, message) {
super('functions', code, message);
/* tslint:disable:max-line-length */
// Set the prototype explicitly. See the following link for more details:
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
/* tslint:enable:max-line-length */
this.__proto__ = FirebaseFunctionsError.prototype;
}
}
exports.FirebaseFunctionsError = FirebaseFunctionsError;
function tasksEmulatorUrl(resources) {
if (process.env.CLOUD_TASKS_EMULATOR_HOST) {
return `http://${process.env.CLOUD_TASKS_EMULATOR_HOST}/projects/${resources.projectId}/locations/${resources.locationId}/queues/${resources.resourceId}/tasks`;
}
return undefined;
}

View File

@@ -0,0 +1,99 @@
/*! firebase-admin v13.5.0 */
/*!
* @license
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Interface representing task options with delayed delivery.
*/
export interface DelayDelivery {
/**
* The duration of delay of the time when the task is scheduled to be attempted or retried.
* This delay is added to the current time.
*/
scheduleDelaySeconds?: number;
/** @alpha */
scheduleTime?: never;
}
/**
* Interface representing task options with absolute delivery.
*/
export interface AbsoluteDelivery {
/**
* The time when the task is scheduled to be attempted or retried.
*/
scheduleTime?: Date;
/** @alpha */
scheduleDelaySeconds?: never;
}
/**
* Type representing delivery schedule options.
* `DeliverySchedule` is a union type of {@link DelayDelivery} and {@link AbsoluteDelivery} types.
*/
export type DeliverySchedule = DelayDelivery | AbsoluteDelivery;
/**
* Type representing task options.
*/
export type TaskOptions = DeliverySchedule & TaskOptionsExperimental & {
/**
* The deadline for requests sent to the worker. If the worker does not respond by this deadline
* then the request is cancelled and the attempt is marked as a DEADLINE_EXCEEDED failure.
* Cloud Tasks will retry the task according to the `RetryConfig`.
* The default is 10 minutes. The deadline must be in the range of 15 seconds and 30 minutes.
*/
dispatchDeadlineSeconds?: number;
/**
* The ID to use for the enqueued event.
* If not provided, one will be automatically generated.
* If provided, an explicitly specified task ID enables task de-duplication. If a task's ID is
* identical to that of an existing task or a task that was deleted or executed recently then
* the call will throw an error with code "functions/task-already-exists". Another task with
* the same ID can't be created for ~1hour after the original task was deleted or executed.
*
* Because there is an extra lookup cost to identify duplicate task IDs, setting ID
* significantly increases latency. Using hashed strings for the task ID or for the prefix of
* the task ID is recommended. Choosing task IDs that are sequential or have sequential
* prefixes, for example using a timestamp, causes an increase in latency and error rates in
* all task commands. The infrastructure relies on an approximately uniform distribution of
* task IDs to store and serve tasks efficiently.
*
* "Push IDs" from the Firebase Realtime Database make poor IDs because they are based on
* timestamps and will cause contention (slowdowns) in your task queue. Reversed push IDs
* however form a perfect distribution and are an ideal key. To reverse a string in
* javascript use `someString.split("").reverse().join("")`
*/
id?: string;
/**
* HTTP request headers to include in the request to the task queue function.
* These headers represent a subset of the headers that will accompany the task's HTTP
* request. Some HTTP request headers will be ignored or replaced, e.g. Authorization, Host, Content-Length,
* User-Agent etc. cannot be overridden.
*
* By default, Content-Type is set to 'application/json'.
*
* The size of the headers must be less than 80KB.
*/
headers?: Record<string, string>;
};
/**
* Type representing experimental (beta) task options.
*/
export interface TaskOptionsExperimental {
/**
* The full URL path that the request will be sent to. Must be a valid URL.
* @beta
*/
uri?: string;
}

View File

@@ -0,0 +1,19 @@
/*! firebase-admin v13.5.0 */
"use strict";
/*!
* @license
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });

View File

@@ -0,0 +1,69 @@
/*! firebase-admin v13.5.0 */
/*!
* @license
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { App } from '../app';
import { TaskOptions } from './functions-api';
/**
* The Firebase `Functions` service interface.
*/
export declare class Functions {
readonly app: App;
private readonly client;
/**
* Creates a reference to a {@link TaskQueue} for a given function name.
* The function name can be either:
*
* 1) A fully qualified function resource name:
* `projects/{project}/locations/{location}/functions/{functionName}`
*
* 2) A partial resource name with location and function name, in which case
* the runtime project ID is used:
* `locations/{location}/functions/{functionName}`
*
* 3) A partial function name, in which case the runtime project ID and the default location,
* `us-central1`, is used:
* `{functionName}`
*
* @param functionName - The name of the function.
* @param extensionId - Optional Firebase extension ID.
* @returns A promise that fulfills with a `TaskQueue`.
*/
taskQueue<Args = Record<string, any>>(functionName: string, extensionId?: string): TaskQueue<Args>;
}
/**
* The `TaskQueue` interface.
*/
export declare class TaskQueue<Args = Record<string, any>> {
private readonly functionName;
private readonly client;
private readonly extensionId?;
/**
* Creates a task and adds it to the queue. Tasks cannot be updated after creation.
* This action requires `cloudtasks.tasks.create` IAM permission on the service account.
*
* @param data - The data payload of the task.
* @param opts - Optional options when enqueuing a new task.
* @returns A promise that resolves when the task has successfully been added to the queue.
*/
enqueue(data: Args, opts?: TaskOptions): Promise<void>;
/**
* Deletes an enqueued task if it has not yet completed.
* @param id - the ID of the task, relative to this queue.
* @returns A promise that resolves when the task has been deleted.
*/
delete(id: string): Promise<void>;
}

View File

@@ -0,0 +1,105 @@
/*! firebase-admin v13.5.0 */
"use strict";
/*!
* @license
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskQueue = exports.Functions = void 0;
const functions_api_client_internal_1 = require("./functions-api-client-internal");
const validator = require("../utils/validator");
/**
* The Firebase `Functions` service interface.
*/
class Functions {
/**
* @param app - The app for this `Functions` service.
* @constructor
* @internal
*/
constructor(app) {
this.app = app;
this.client = new functions_api_client_internal_1.FunctionsApiClient(app);
}
/**
* Creates a reference to a {@link TaskQueue} for a given function name.
* The function name can be either:
*
* 1) A fully qualified function resource name:
* `projects/{project}/locations/{location}/functions/{functionName}`
*
* 2) A partial resource name with location and function name, in which case
* the runtime project ID is used:
* `locations/{location}/functions/{functionName}`
*
* 3) A partial function name, in which case the runtime project ID and the default location,
* `us-central1`, is used:
* `{functionName}`
*
* @param functionName - The name of the function.
* @param extensionId - Optional Firebase extension ID.
* @returns A promise that fulfills with a `TaskQueue`.
*/
taskQueue(functionName, extensionId) {
return new TaskQueue(functionName, this.client, extensionId);
}
}
exports.Functions = Functions;
/**
* The `TaskQueue` interface.
*/
class TaskQueue {
/**
* @param functionName - The name of the function.
* @param client - The `FunctionsApiClient` instance.
* @param extensionId - Optional canonical ID of the extension.
* @constructor
* @internal
*/
constructor(functionName, client, extensionId) {
this.functionName = functionName;
this.client = client;
this.extensionId = extensionId;
if (!validator.isNonEmptyString(functionName)) {
throw new functions_api_client_internal_1.FirebaseFunctionsError('invalid-argument', '`functionName` must be a non-empty string.');
}
if (!validator.isNonNullObject(client) || !('enqueue' in client)) {
throw new functions_api_client_internal_1.FirebaseFunctionsError('invalid-argument', 'Must provide a valid FunctionsApiClient instance to create a new TaskQueue.');
}
if (typeof extensionId !== 'undefined' && !validator.isString(extensionId)) {
throw new functions_api_client_internal_1.FirebaseFunctionsError('invalid-argument', '`extensionId` must be a string.');
}
}
/**
* Creates a task and adds it to the queue. Tasks cannot be updated after creation.
* This action requires `cloudtasks.tasks.create` IAM permission on the service account.
*
* @param data - The data payload of the task.
* @param opts - Optional options when enqueuing a new task.
* @returns A promise that resolves when the task has successfully been added to the queue.
*/
enqueue(data, opts) {
return this.client.enqueue(data, this.functionName, this.extensionId, opts);
}
/**
* Deletes an enqueued task if it has not yet completed.
* @param id - the ID of the task, relative to this queue.
* @returns A promise that resolves when the task has been deleted.
*/
delete(id) {
return this.client.delete(id, this.functionName, this.extensionId);
}
}
exports.TaskQueue = TaskQueue;

View File

@@ -0,0 +1,53 @@
/*! firebase-admin v13.5.0 */
/*!
* @license
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Firebase Functions service.
*
* @packageDocumentation
*/
import { App } from '../app';
import { Functions } from './functions';
export { DelayDelivery, AbsoluteDelivery, DeliverySchedule, TaskOptions, TaskOptionsExperimental, } from './functions-api';
export { Functions, TaskQueue } from './functions';
/**
* Gets the {@link Functions} service for the default app
* or a given app.
*
* `getFunctions()` can be called with no arguments to access the default
* app's `Functions` service or as `getFunctions(app)` to access the
* `Functions` service associated with a specific app.
*
* @example
* ```javascript
* // Get the `Functions` service for the default app
* const defaultFunctions = getFunctions();
* ```
*
* @example
* ```javascript
* // Get the `Functions` service for a given app
* const otherFunctions = getFunctions(otherApp);
* ```
*
* @param app - Optional app for which to return the `Functions` service.
* If not provided, the default `Functions` service is returned.
*
* @returns The default `Functions` service if no app is provided, or the `Functions`
* service associated with the provided app.
*/
export declare function getFunctions(app?: App): Functions;

View File

@@ -0,0 +1,64 @@
/*! firebase-admin v13.5.0 */
"use strict";
/*!
* @license
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskQueue = exports.Functions = void 0;
exports.getFunctions = getFunctions;
/**
* Firebase Functions service.
*
* @packageDocumentation
*/
const app_1 = require("../app");
const functions_1 = require("./functions");
var functions_2 = require("./functions");
Object.defineProperty(exports, "Functions", { enumerable: true, get: function () { return functions_2.Functions; } });
Object.defineProperty(exports, "TaskQueue", { enumerable: true, get: function () { return functions_2.TaskQueue; } });
/**
* Gets the {@link Functions} service for the default app
* or a given app.
*
* `getFunctions()` can be called with no arguments to access the default
* app's `Functions` service or as `getFunctions(app)` to access the
* `Functions` service associated with a specific app.
*
* @example
* ```javascript
* // Get the `Functions` service for the default app
* const defaultFunctions = getFunctions();
* ```
*
* @example
* ```javascript
* // Get the `Functions` service for a given app
* const otherFunctions = getFunctions(otherApp);
* ```
*
* @param app - Optional app for which to return the `Functions` service.
* If not provided, the default `Functions` service is returned.
*
* @returns The default `Functions` service if no app is provided, or the `Functions`
* service associated with the provided app.
*/
function getFunctions(app) {
if (typeof app === 'undefined') {
app = (0, app_1.getApp)();
}
const firebaseApp = app;
return firebaseApp.getOrInitService('functions', (app) => new functions_1.Functions(app));
}