UNPKG

5.45 kBJavaScriptView Raw
1"use strict";
2// The MIT License (MIT)
3//
4// Copyright (c) 2021 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.__getSpec = exports.optionsToEndpoint = exports.optionsToTriggerAnnotations = exports.getGlobalOptions = exports.setGlobalOptions = exports.RESET_VALUE = void 0;
25/**
26 * Options to configure cloud functions.
27 * @packageDocumentation
28 */
29const encoding_1 = require("../common/encoding");
30const options_1 = require("../common/options");
31const params_1 = require("../params");
32const types_1 = require("../params/types");
33const logger = require("../logger");
34var options_2 = require("../common/options");
35Object.defineProperty(exports, "RESET_VALUE", { enumerable: true, get: function () { return options_2.RESET_VALUE; } });
36const MemoryOptionToMB = {
37 "128MiB": 128,
38 "256MiB": 256,
39 "512MiB": 512,
40 "1GiB": 1024,
41 "2GiB": 2048,
42 "4GiB": 4096,
43 "8GiB": 8192,
44 "16GiB": 16384,
45 "32GiB": 32768,
46};
47let globalOptions;
48/**
49 * Sets default options for all functions written using the 2nd gen SDK.
50 * @param options Options to set as default
51 */
52function setGlobalOptions(options) {
53 if (globalOptions) {
54 logger.warn("Calling setGlobalOptions twice leads to undefined behavior");
55 }
56 globalOptions = options;
57}
58exports.setGlobalOptions = setGlobalOptions;
59/**
60 * Get the currently set default options.
61 * Used only for trigger generation.
62 * @internal
63 */
64function getGlobalOptions() {
65 return globalOptions || {};
66}
67exports.getGlobalOptions = getGlobalOptions;
68/**
69 * Apply GlobalOptions to trigger definitions.
70 * @internal
71 */
72function optionsToTriggerAnnotations(opts) {
73 const annotation = {};
74 (0, encoding_1.copyIfPresent)(annotation, opts, "concurrency", "minInstances", "maxInstances", "ingressSettings", "labels", "vpcConnector", "vpcConnectorEgressSettings", "secrets");
75 (0, encoding_1.convertIfPresent)(annotation, opts, "availableMemoryMb", "memory", (mem) => {
76 return MemoryOptionToMB[mem];
77 });
78 (0, encoding_1.convertIfPresent)(annotation, opts, "regions", "region", (region) => {
79 if (typeof region === "string") {
80 return [region];
81 }
82 return region;
83 });
84 (0, encoding_1.convertIfPresent)(annotation, opts, "serviceAccountEmail", "serviceAccount", encoding_1.serviceAccountFromShorthand);
85 (0, encoding_1.convertIfPresent)(annotation, opts, "timeout", "timeoutSeconds", encoding_1.durationFromSeconds);
86 (0, encoding_1.convertIfPresent)(annotation, opts, "failurePolicy", "retry", (retry) => {
87 return retry ? { retry: true } : null;
88 });
89 return annotation;
90}
91exports.optionsToTriggerAnnotations = optionsToTriggerAnnotations;
92/**
93 * Apply GlobalOptions to endpoint manifest.
94 * @internal
95 */
96function optionsToEndpoint(opts) {
97 const endpoint = {};
98 (0, encoding_1.copyIfPresent)(endpoint, opts, "omit", "concurrency", "minInstances", "maxInstances", "ingressSettings", "labels", "timeoutSeconds", "cpu");
99 (0, encoding_1.convertIfPresent)(endpoint, opts, "serviceAccountEmail", "serviceAccount");
100 if (opts.vpcConnector !== undefined) {
101 if (opts.vpcConnector === null || opts.vpcConnector instanceof options_1.ResetValue) {
102 endpoint.vpc = options_1.RESET_VALUE;
103 }
104 else {
105 const vpc = { connector: opts.vpcConnector };
106 (0, encoding_1.convertIfPresent)(vpc, opts, "egressSettings", "vpcConnectorEgressSettings");
107 endpoint.vpc = vpc;
108 }
109 }
110 (0, encoding_1.convertIfPresent)(endpoint, opts, "availableMemoryMb", "memory", (mem) => {
111 return typeof mem === "object" ? mem : MemoryOptionToMB[mem];
112 });
113 (0, encoding_1.convertIfPresent)(endpoint, opts, "region", "region", (region) => {
114 if (typeof region === "string") {
115 return [region];
116 }
117 return region;
118 });
119 (0, encoding_1.convertIfPresent)(endpoint, opts, "secretEnvironmentVariables", "secrets", (secrets) => secrets.map((secret) => ({ key: secret instanceof types_1.SecretParam ? secret.name : secret })));
120 return endpoint;
121}
122exports.optionsToEndpoint = optionsToEndpoint;
123/**
124 * @hidden
125 * @alpha
126 */
127function __getSpec() {
128 return {
129 globalOptions: getGlobalOptions(),
130 params: params_1.declaredParams.map((p) => p.toSpec()),
131 };
132}
133exports.__getSpec = __getSpec;