UNPKG

8.22 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.onCall = exports.onRequest = exports.HttpsError = void 0;
25/**
26 * Cloud functions to handle HTTPS request or callable RPCs.
27 * @packageDocumentation
28 */
29const cors = require("cors");
30const encoding_1 = require("../../common/encoding");
31const trace_1 = require("../trace");
32const debug_1 = require("../../common/debug");
33const https_1 = require("../../common/providers/https");
34Object.defineProperty(exports, "HttpsError", { enumerable: true, get: function () { return https_1.HttpsError; } });
35const manifest_1 = require("../../runtime/manifest");
36const options = require("../options");
37const onInit_1 = require("../../common/onInit");
38function onRequest(optsOrHandler, handler) {
39 let opts;
40 if (arguments.length === 1) {
41 opts = {};
42 handler = optsOrHandler;
43 }
44 else {
45 opts = optsOrHandler;
46 }
47 if ((0, debug_1.isDebugFeatureEnabled)("enableCors") || "cors" in opts) {
48 let origin = opts.cors;
49 if ((0, debug_1.isDebugFeatureEnabled)("enableCors")) {
50 // Respect `cors: false` to turn off cors even if debug feature is enabled.
51 origin = opts.cors === false ? false : true;
52 }
53 // Arrays cause the access-control-allow-origin header to be dynamic based
54 // on the origin header of the request. If there is only one element in the
55 // array, this is unnecessary.
56 if (Array.isArray(origin) && origin.length === 1) {
57 origin = origin[0];
58 }
59 const middleware = cors({ origin });
60 const userProvidedHandler = handler;
61 handler = (req, res) => {
62 return new Promise((resolve) => {
63 res.on("finish", resolve);
64 middleware(req, res, () => {
65 resolve(userProvidedHandler(req, res));
66 });
67 });
68 };
69 }
70 handler = (0, trace_1.wrapTraceContext)((0, onInit_1.withInit)(handler));
71 Object.defineProperty(handler, "__trigger", {
72 get: () => {
73 const baseOpts = options.optionsToTriggerAnnotations(options.getGlobalOptions());
74 // global options calls region a scalar and https allows it to be an array,
75 // but optionsToTriggerAnnotations handles both cases.
76 const specificOpts = options.optionsToTriggerAnnotations(opts);
77 const trigger = {
78 platform: "gcfv2",
79 ...baseOpts,
80 ...specificOpts,
81 labels: {
82 ...baseOpts === null || baseOpts === void 0 ? void 0 : baseOpts.labels,
83 ...specificOpts === null || specificOpts === void 0 ? void 0 : specificOpts.labels,
84 },
85 httpsTrigger: {
86 allowInsecure: false,
87 },
88 };
89 (0, encoding_1.convertIfPresent)(trigger.httpsTrigger, options.getGlobalOptions(), "invoker", "invoker", encoding_1.convertInvoker);
90 (0, encoding_1.convertIfPresent)(trigger.httpsTrigger, opts, "invoker", "invoker", encoding_1.convertInvoker);
91 return trigger;
92 },
93 });
94 const globalOpts = options.getGlobalOptions();
95 const baseOpts = options.optionsToEndpoint(globalOpts);
96 // global options calls region a scalar and https allows it to be an array,
97 // but optionsToTriggerAnnotations handles both cases.
98 const specificOpts = options.optionsToEndpoint(opts);
99 const endpoint = {
100 ...(0, manifest_1.initV2Endpoint)(globalOpts, opts),
101 platform: "gcfv2",
102 ...baseOpts,
103 ...specificOpts,
104 labels: {
105 ...baseOpts === null || baseOpts === void 0 ? void 0 : baseOpts.labels,
106 ...specificOpts === null || specificOpts === void 0 ? void 0 : specificOpts.labels,
107 },
108 httpsTrigger: {},
109 };
110 (0, encoding_1.convertIfPresent)(endpoint.httpsTrigger, globalOpts, "invoker", "invoker", encoding_1.convertInvoker);
111 (0, encoding_1.convertIfPresent)(endpoint.httpsTrigger, opts, "invoker", "invoker", encoding_1.convertInvoker);
112 handler.__endpoint = endpoint;
113 return handler;
114}
115exports.onRequest = onRequest;
116function onCall(optsOrHandler, handler) {
117 var _a;
118 let opts;
119 if (arguments.length === 1) {
120 opts = {};
121 handler = optsOrHandler;
122 }
123 else {
124 opts = optsOrHandler;
125 }
126 let origin = (0, debug_1.isDebugFeatureEnabled)("enableCors") ? true : "cors" in opts ? opts.cors : true;
127 // Arrays cause the access-control-allow-origin header to be dynamic based
128 // on the origin header of the request. If there is only one element in the
129 // array, this is unnecessary.
130 if (Array.isArray(origin) && origin.length === 1) {
131 origin = origin[0];
132 }
133 // onCallHandler sniffs the function length to determine which API to present.
134 // fix the length to prevent api versions from being mismatched.
135 const fixedLen = (req) => (0, onInit_1.withInit)(handler)(req);
136 let func = (0, https_1.onCallHandler)({
137 cors: { origin, methods: "POST" },
138 enforceAppCheck: (_a = opts.enforceAppCheck) !== null && _a !== void 0 ? _a : options.getGlobalOptions().enforceAppCheck,
139 consumeAppCheckToken: opts.consumeAppCheckToken,
140 }, fixedLen);
141 func = (0, trace_1.wrapTraceContext)(func);
142 Object.defineProperty(func, "__trigger", {
143 get: () => {
144 const baseOpts = options.optionsToTriggerAnnotations(options.getGlobalOptions());
145 // global options calls region a scalar and https allows it to be an array,
146 // but optionsToTriggerAnnotations handles both cases.
147 const specificOpts = options.optionsToTriggerAnnotations(opts);
148 return {
149 platform: "gcfv2",
150 ...baseOpts,
151 ...specificOpts,
152 labels: {
153 ...baseOpts === null || baseOpts === void 0 ? void 0 : baseOpts.labels,
154 ...specificOpts === null || specificOpts === void 0 ? void 0 : specificOpts.labels,
155 "deployment-callable": "true",
156 },
157 httpsTrigger: {
158 allowInsecure: false,
159 },
160 };
161 },
162 });
163 const baseOpts = options.optionsToEndpoint(options.getGlobalOptions());
164 // global options calls region a scalar and https allows it to be an array,
165 // but optionsToEndpoint handles both cases.
166 const specificOpts = options.optionsToEndpoint(opts);
167 func.__endpoint = {
168 ...(0, manifest_1.initV2Endpoint)(options.getGlobalOptions(), opts),
169 platform: "gcfv2",
170 ...baseOpts,
171 ...specificOpts,
172 labels: {
173 ...baseOpts === null || baseOpts === void 0 ? void 0 : baseOpts.labels,
174 ...specificOpts === null || specificOpts === void 0 ? void 0 : specificOpts.labels,
175 },
176 callableTrigger: {},
177 };
178 func.run = (0, onInit_1.withInit)(handler);
179 return func;
180}
181exports.onCall = onCall;