UNPKG

5.09 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.onTaskDispatched = void 0;
25/**
26 * Cloud functions to handle Tasks enqueued with Google Cloud Tasks.
27 * @packageDocumentation
28 */
29const encoding_1 = require("../../common/encoding");
30const tasks_1 = require("../../common/providers/tasks");
31const options = require("../options");
32const trace_1 = require("../trace");
33const manifest_1 = require("../../runtime/manifest");
34const onInit_1 = require("../../common/onInit");
35function onTaskDispatched(optsOrHandler, handler) {
36 let opts;
37 if (arguments.length === 1) {
38 opts = {};
39 handler = optsOrHandler;
40 }
41 else {
42 opts = optsOrHandler;
43 }
44 // onDispatchHandler sniffs the function length to determine which API to present.
45 // fix the length to prevent api versions from being mismatched.
46 const fixedLen = (req) => handler(req);
47 const func = (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)((0, tasks_1.onDispatchHandler)(fixedLen)));
48 Object.defineProperty(func, "__trigger", {
49 get: () => {
50 const baseOpts = options.optionsToTriggerAnnotations(options.getGlobalOptions());
51 // global options calls region a scalar and https allows it to be an array,
52 // but optionsToTriggerAnnotations handles both cases.
53 const specificOpts = options.optionsToTriggerAnnotations(opts);
54 const taskQueueTrigger = {};
55 (0, encoding_1.copyIfPresent)(taskQueueTrigger, opts, "retryConfig", "rateLimits");
56 (0, encoding_1.convertIfPresent)(taskQueueTrigger, options.getGlobalOptions(), "invoker", "invoker", encoding_1.convertInvoker);
57 (0, encoding_1.convertIfPresent)(taskQueueTrigger, opts, "invoker", "invoker", encoding_1.convertInvoker);
58 return {
59 platform: "gcfv2",
60 ...baseOpts,
61 ...specificOpts,
62 labels: {
63 ...baseOpts === null || baseOpts === void 0 ? void 0 : baseOpts.labels,
64 ...specificOpts === null || specificOpts === void 0 ? void 0 : specificOpts.labels,
65 },
66 taskQueueTrigger,
67 };
68 },
69 });
70 const baseOpts = options.optionsToEndpoint(options.getGlobalOptions());
71 // global options calls region a scalar and https allows it to be an array,
72 // but optionsToManifestEndpoint handles both cases.
73 const specificOpts = options.optionsToEndpoint(opts);
74 func.__endpoint = {
75 platform: "gcfv2",
76 ...(0, manifest_1.initV2Endpoint)(options.getGlobalOptions(), opts),
77 ...baseOpts,
78 ...specificOpts,
79 labels: {
80 ...baseOpts === null || baseOpts === void 0 ? void 0 : baseOpts.labels,
81 ...specificOpts === null || specificOpts === void 0 ? void 0 : specificOpts.labels,
82 },
83 taskQueueTrigger: (0, manifest_1.initTaskQueueTrigger)(options.getGlobalOptions(), opts),
84 };
85 (0, encoding_1.copyIfPresent)(func.__endpoint.taskQueueTrigger.retryConfig, opts.retryConfig, "maxAttempts", "maxBackoffSeconds", "maxDoublings", "maxRetrySeconds", "minBackoffSeconds");
86 (0, encoding_1.copyIfPresent)(func.__endpoint.taskQueueTrigger.rateLimits, opts.rateLimits, "maxConcurrentDispatches", "maxDispatchesPerSecond");
87 (0, encoding_1.convertIfPresent)(func.__endpoint.taskQueueTrigger, options.getGlobalOptions(), "invoker", "invoker", encoding_1.convertInvoker);
88 (0, encoding_1.convertIfPresent)(func.__endpoint.taskQueueTrigger, opts, "invoker", "invoker", encoding_1.convertInvoker);
89 (0, encoding_1.copyIfPresent)(func.__endpoint.taskQueueTrigger, opts, "retry", "retry");
90 func.__requiredAPIs = [
91 {
92 api: "cloudtasks.googleapis.com",
93 reason: "Needed for task queue functions",
94 },
95 ];
96 func.run = handler;
97 return func;
98}
99exports.onTaskDispatched = onTaskDispatched;