UNPKG

4.33 kBJavaScriptView Raw
1"use strict";
2// The MIT License (MIT)
3//
4// Copyright (c) 2022 Firebase
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.taskQueue = exports.TaskQueueBuilder = void 0;
25const encoding_1 = require("../../common/encoding");
26const tasks_1 = require("../../common/providers/tasks");
27const manifest_1 = require("../../runtime/manifest");
28const cloud_functions_1 = require("../cloud-functions");
29/**
30 * Builder for creating a `TaskQueueFunction`.
31 */
32class TaskQueueBuilder {
33 /** @internal */
34 constructor(tqOpts, depOpts) {
35 this.tqOpts = tqOpts;
36 this.depOpts = depOpts;
37 }
38 /**
39 * Creates a handler for tasks sent to a Google Cloud Tasks queue.
40 * @param handler - A callback to handle task requests.
41 * @returns A function you can export and deploy.
42 */
43 onDispatch(handler) {
44 var _a, _b;
45 // onEnqueueHandler sniffs the function length of the passed-in callback
46 // and the user could have only tried to listen to data. Wrap their handler
47 // in another handler to avoid accidentally triggering the v2 API
48 const fixedLen = (data, context) => handler(data, context);
49 const func = (0, tasks_1.onDispatchHandler)(fixedLen);
50 func.__trigger = {
51 ...(0, cloud_functions_1.optionsToTrigger)(this.depOpts || {}),
52 taskQueueTrigger: {},
53 };
54 (0, encoding_1.copyIfPresent)(func.__trigger.taskQueueTrigger, this.tqOpts, "retryConfig");
55 (0, encoding_1.copyIfPresent)(func.__trigger.taskQueueTrigger, this.tqOpts, "rateLimits");
56 (0, encoding_1.convertIfPresent)(func.__trigger.taskQueueTrigger, this.tqOpts, "invoker", "invoker", encoding_1.convertInvoker);
57 func.__endpoint = {
58 platform: "gcfv1",
59 ...(0, manifest_1.initV1Endpoint)(this.depOpts),
60 ...(0, cloud_functions_1.optionsToEndpoint)(this.depOpts),
61 taskQueueTrigger: (0, manifest_1.initTaskQueueTrigger)(this.depOpts),
62 };
63 (0, encoding_1.copyIfPresent)(func.__endpoint.taskQueueTrigger.retryConfig, ((_a = this.tqOpts) === null || _a === void 0 ? void 0 : _a.retryConfig) || {}, "maxAttempts", "maxBackoffSeconds", "maxDoublings", "maxRetrySeconds", "minBackoffSeconds");
64 (0, encoding_1.copyIfPresent)(func.__endpoint.taskQueueTrigger.rateLimits, ((_b = this.tqOpts) === null || _b === void 0 ? void 0 : _b.rateLimits) || {}, "maxConcurrentDispatches", "maxDispatchesPerSecond");
65 (0, encoding_1.convertIfPresent)(func.__endpoint.taskQueueTrigger, this.tqOpts, "invoker", "invoker", encoding_1.convertInvoker);
66 func.__requiredAPIs = [
67 {
68 api: "cloudtasks.googleapis.com",
69 reason: "Needed for task queue functions",
70 },
71 ];
72 func.run = handler;
73 return func;
74 }
75}
76exports.TaskQueueBuilder = TaskQueueBuilder;
77/**
78 * Declares a function that can handle tasks enqueued using the Firebase Admin SDK.
79 * @param options - Configuration for the Task Queue that feeds into this function.
80 * Omitting options will configure a Task Queue with default settings.
81 */
82function taskQueue(options) {
83 return new TaskQueueBuilder(options);
84}
85exports.taskQueue = taskQueue;