UNPKG

3.97 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.convertInvoker = exports.serviceAccountFromShorthand = exports.convertIfPresent = exports.copyIfPresent = exports.durationFromSeconds = void 0;
25const params_1 = require("../params");
26/** Get a google.protobuf.Duration for a number of seconds. */
27function durationFromSeconds(s) {
28 return `${s}s`;
29}
30exports.durationFromSeconds = durationFromSeconds;
31/**
32 * Utility function to help copy fields from type A to B.
33 * As a safety net, catches typos or fields that aren't named the same
34 * in A and B, but cannot verify that both Src and Dest have the same type for the same field.
35 */
36function copyIfPresent(dest, src, ...fields) {
37 if (!src) {
38 return;
39 }
40 for (const field of fields) {
41 if (!Object.prototype.hasOwnProperty.call(src, field)) {
42 continue;
43 }
44 dest[field] = src[field];
45 }
46}
47exports.copyIfPresent = copyIfPresent;
48function convertIfPresent(dest, src, destField, srcField, converter = (from) => {
49 return from;
50}) {
51 if (!src) {
52 return;
53 }
54 if (!Object.prototype.hasOwnProperty.call(src, srcField)) {
55 return;
56 }
57 dest[destField] = converter(src[srcField]);
58}
59exports.convertIfPresent = convertIfPresent;
60function serviceAccountFromShorthand(serviceAccount) {
61 if (serviceAccount === "default") {
62 return null;
63 }
64 else if (serviceAccount instanceof params_1.Expression) {
65 return serviceAccount;
66 }
67 else if (serviceAccount.endsWith("@")) {
68 if (!process.env.GCLOUD_PROJECT) {
69 throw new Error(`Unable to determine email for service account '${serviceAccount}' because process.env.GCLOUD_PROJECT is not set.`);
70 }
71 return `${serviceAccount}${process.env.GCLOUD_PROJECT}.iam.gserviceaccount.com`;
72 }
73 else if (serviceAccount.includes("@")) {
74 return serviceAccount;
75 }
76 else {
77 throw new Error(`Invalid option for serviceAccount: '${serviceAccount}'. Valid options are 'default', a service account email, or '{serviceAccountName}@'`);
78 }
79}
80exports.serviceAccountFromShorthand = serviceAccountFromShorthand;
81function convertInvoker(invoker) {
82 if (typeof invoker === "string") {
83 invoker = [invoker];
84 }
85 if (invoker.length === 0) {
86 throw new Error("Invalid option for invoker: Must be a non-empty array.");
87 }
88 if (invoker.find((inv) => inv.length === 0)) {
89 throw new Error("Invalid option for invoker: Must be a non-empty string.");
90 }
91 if (invoker.length > 1 && invoker.find((inv) => inv === "public" || inv === "private")) {
92 throw new Error("Invalid option for invoker: Cannot have 'public' or 'private' in an array of service accounts.");
93 }
94 return invoker;
95}
96exports.convertInvoker = convertInvoker;