1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | Object.defineProperty(exports, "__esModule", { value: true });
|
24 | exports.onCall = exports.onRequest = exports.HttpsError = void 0;
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | const cors = require("cors");
|
30 | const encoding_1 = require("../../common/encoding");
|
31 | const trace_1 = require("../trace");
|
32 | const debug_1 = require("../../common/debug");
|
33 | const https_1 = require("../../common/providers/https");
|
34 | Object.defineProperty(exports, "HttpsError", { enumerable: true, get: function () { return https_1.HttpsError; } });
|
35 | const manifest_1 = require("../../runtime/manifest");
|
36 | const options = require("../options");
|
37 | const onInit_1 = require("../../common/onInit");
|
38 | function 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 |
|
51 | origin = opts.cors === false ? false : true;
|
52 | }
|
53 |
|
54 |
|
55 |
|
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 |
|
75 |
|
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 |
|
97 |
|
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 | }
|
115 | exports.onRequest = onRequest;
|
116 | function 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 |
|
128 |
|
129 |
|
130 | if (Array.isArray(origin) && origin.length === 1) {
|
131 | origin = origin[0];
|
132 | }
|
133 |
|
134 |
|
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 |
|
146 |
|
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 |
|
165 |
|
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 | }
|
181 | exports.onCall = onCall;
|