firebase-tools 9.20.0 → 9.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/lib/apiv2.js +4 -2
  3. package/lib/commands/crashlytics-symbols-upload.js +1 -1
  4. package/lib/commands/ext-dev-unpublish.js +10 -3
  5. package/lib/commands/functions-delete.js +53 -42
  6. package/lib/commands/functions-list.js +11 -11
  7. package/lib/deploy/functions/backend.js +77 -115
  8. package/lib/deploy/functions/checkIam.js +8 -8
  9. package/lib/deploy/functions/deploy.js +4 -10
  10. package/lib/deploy/functions/functionsDeployHelper.js +3 -68
  11. package/lib/deploy/functions/prepare.js +61 -29
  12. package/lib/deploy/functions/pricing.js +17 -17
  13. package/lib/deploy/functions/prompts.js +22 -21
  14. package/lib/deploy/functions/release/executor.js +39 -0
  15. package/lib/deploy/functions/release/fabricator.js +362 -0
  16. package/lib/deploy/functions/release/index.js +69 -0
  17. package/lib/deploy/functions/release/planner.js +159 -0
  18. package/lib/deploy/functions/release/reporter.js +162 -0
  19. package/lib/deploy/functions/release/sourceTokenScraper.js +28 -0
  20. package/lib/deploy/functions/release/timer.js +14 -0
  21. package/lib/deploy/functions/runtimes/discovery/v1alpha1.js +102 -127
  22. package/lib/deploy/functions/runtimes/node/parseTriggers.js +22 -43
  23. package/lib/deploy/functions/triggerRegionHelper.js +28 -20
  24. package/lib/deploy/functions/validate.js +1 -24
  25. package/lib/emulator/auth/apiSpec.js +37 -6
  26. package/lib/emulator/auth/operations.js +18 -8
  27. package/lib/emulator/auth/server.js +16 -2
  28. package/lib/emulator/auth/state.js +34 -15
  29. package/lib/emulator/downloadableEmulators.js +7 -7
  30. package/lib/emulator/functionsEmulator.js +15 -3
  31. package/lib/emulator/storage/cloudFunctions.js +37 -7
  32. package/lib/extensions/extensionsHelper.js +1 -1
  33. package/lib/gcp/cloudfunctions.js +1 -68
  34. package/lib/gcp/cloudfunctionsv2.js +2 -94
  35. package/lib/gcp/cloudscheduler.js +22 -16
  36. package/lib/gcp/pubsub.js +1 -9
  37. package/lib/utils.js +30 -1
  38. package/package.json +1 -1
  39. package/lib/deploy/functions/deploymentPlanner.js +0 -113
  40. package/lib/deploy/functions/deploymentTimer.js +0 -23
  41. package/lib/deploy/functions/errorHandler.js +0 -75
  42. package/lib/deploy/functions/release.js +0 -116
  43. package/lib/deploy/functions/tasks.js +0 -324
  44. package/lib/functions/listFunctions.js +0 -10
  45. package/lib/functionsDelete.js +0 -60
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.triggerTag = exports.printAbortedErrors = exports.printErrors = exports.logAndTrackDeployStats = exports.AbortedDeploymentError = exports.DeploymentError = void 0;
4
+ const backend = require("../backend");
5
+ const clc = require("cli-color");
6
+ const logger_1 = require("../../../logger");
7
+ const track = require("../../../track");
8
+ const utils = require("../../../utils");
9
+ const functionsDeployHelper_1 = require("../functionsDeployHelper");
10
+ class DeploymentError extends Error {
11
+ constructor(endpoint, op, original) {
12
+ super(`Failed to ${op} function ${endpoint.id} in region ${endpoint.region}`);
13
+ this.endpoint = endpoint;
14
+ this.op = op;
15
+ this.original = original;
16
+ }
17
+ }
18
+ exports.DeploymentError = DeploymentError;
19
+ class AbortedDeploymentError extends DeploymentError {
20
+ constructor(endpoint) {
21
+ super(endpoint, "delete", new Error("aborted"));
22
+ this.endpoint = endpoint;
23
+ }
24
+ }
25
+ exports.AbortedDeploymentError = AbortedDeploymentError;
26
+ async function logAndTrackDeployStats(summary) {
27
+ let totalTime = 0;
28
+ let totalErrors = 0;
29
+ let totalSuccesses = 0;
30
+ let totalAborts = 0;
31
+ const reports = [];
32
+ for (const result of summary.results) {
33
+ const tag = triggerTag(result.endpoint);
34
+ totalTime += result.durationMs;
35
+ if (!result.error) {
36
+ totalSuccesses++;
37
+ reports.push(track.track("function_deploy_success", tag, result.durationMs));
38
+ }
39
+ else if (result.error instanceof AbortedDeploymentError) {
40
+ totalAborts++;
41
+ reports.push(track.track("function_deploy_abort", tag, result.durationMs));
42
+ }
43
+ else {
44
+ totalErrors++;
45
+ reports.push(track.track("function_deploy_failure", tag, result.durationMs));
46
+ }
47
+ }
48
+ const gcfv1 = summary.results.find((r) => r.endpoint.platform === "gcfv1");
49
+ const gcfv2 = summary.results.find((r) => r.endpoint.platform === "gcfv2");
50
+ const tag = gcfv1 && gcfv2 ? "v1+v2" : gcfv1 ? "v1" : "v2";
51
+ reports.push(track.track("functions_codebase_deploy", tag, summary.results.length));
52
+ const avgTime = totalTime / (totalSuccesses + totalErrors);
53
+ logger_1.logger.debug(`Total Function Deployment time: ${summary.totalTime}`);
54
+ logger_1.logger.debug(`${totalErrors + totalSuccesses + totalAborts} Functions Deployed`);
55
+ logger_1.logger.debug(`${totalErrors} Functions Errored`);
56
+ logger_1.logger.debug(`${totalAborts} Function Deployments Aborted`);
57
+ logger_1.logger.debug(`Average Function Deployment time: ${avgTime}`);
58
+ if (totalErrors + totalSuccesses > 0) {
59
+ if (totalErrors === 0) {
60
+ reports.push(track.track("functions_deploy_result", "success", totalSuccesses));
61
+ }
62
+ else if (totalSuccesses > 0) {
63
+ reports.push(track.track("functions_deploy_result", "partial_success", totalSuccesses));
64
+ reports.push(track.track("functions_deploy_result", "partial_failure", totalErrors));
65
+ reports.push(track.track("functions_deploy_result", "partial_error_ratio", totalErrors / (totalSuccesses + totalErrors)));
66
+ }
67
+ else {
68
+ reports.push(track.track("functions_deploy_result", "failure", totalErrors));
69
+ }
70
+ }
71
+ await utils.allSettled(reports);
72
+ }
73
+ exports.logAndTrackDeployStats = logAndTrackDeployStats;
74
+ function printErrors(summary) {
75
+ const errored = summary.results.filter((r) => r.error);
76
+ if (errored.length === 0) {
77
+ return;
78
+ }
79
+ errored.sort((left, right) => backend.compareFunctions(left.endpoint, right.endpoint));
80
+ logger_1.logger.info("");
81
+ logger_1.logger.info("Functions deploy had errors with the following functions:" +
82
+ errored
83
+ .filter((r) => !(r.error instanceof AbortedDeploymentError))
84
+ .map((result) => `\n\t${functionsDeployHelper_1.getFunctionLabel(result.endpoint)}`)
85
+ .join(""));
86
+ printIamErrors(errored);
87
+ printQuotaErrors(errored);
88
+ printAbortedErrors(errored);
89
+ }
90
+ exports.printErrors = printErrors;
91
+ function printIamErrors(results) {
92
+ const iamFailures = results.filter((r) => r.error instanceof DeploymentError && r.error.op === "set invoker");
93
+ if (!iamFailures.length) {
94
+ return;
95
+ }
96
+ logger_1.logger.info("");
97
+ logger_1.logger.info("Unable to set the invoker for the IAM policy on the following functions:" +
98
+ iamFailures.map((result) => `\n\t${functionsDeployHelper_1.getFunctionLabel(result.endpoint)}`).join(""));
99
+ logger_1.logger.info("");
100
+ logger_1.logger.info("Some common causes of this:");
101
+ logger_1.logger.info("");
102
+ logger_1.logger.info("- You may not have the roles/functions.admin IAM role. Note that " +
103
+ "roles/functions.developer does not allow you to change IAM policies.");
104
+ logger_1.logger.info("");
105
+ logger_1.logger.info("- An organization policy that restricts Network Access on your project.");
106
+ const hadImplicitMakePublic = iamFailures.find((r) => backend.isHttpsTriggered(r.endpoint) && !r.endpoint.httpsTrigger.invoker);
107
+ if (!hadImplicitMakePublic) {
108
+ return;
109
+ }
110
+ logger_1.logger.info("");
111
+ logger_1.logger.info("One or more functions were being implicitly made publicly available on function create.");
112
+ logger_1.logger.info("Functions are not implicitly made public on updates. To try to make " +
113
+ "these functions public on next deploy, configure these functions with " +
114
+ `${clc.bold("invoker")} set to ${clc.bold(`"public"`)}`);
115
+ }
116
+ function printQuotaErrors(results) {
117
+ const hadQuotaError = results.find((r) => {
118
+ var _a, _b, _c, _d, _e, _f;
119
+ if (!(r.error instanceof DeploymentError)) {
120
+ return false;
121
+ }
122
+ const original = r.error.original;
123
+ const code = (original === null || original === void 0 ? void 0 : original.status) || (original === null || original === void 0 ? void 0 : original.code) || ((_b = (_a = original === null || original === void 0 ? void 0 : original.context) === null || _a === void 0 ? void 0 : _a.response) === null || _b === void 0 ? void 0 : _b.statusCode) || ((_c = original === null || original === void 0 ? void 0 : original.original) === null || _c === void 0 ? void 0 : _c.code) || ((_f = (_e = (_d = original === null || original === void 0 ? void 0 : original.original) === null || _d === void 0 ? void 0 : _d.context) === null || _e === void 0 ? void 0 : _e.response) === null || _f === void 0 ? void 0 : _f.statusCode);
124
+ return code === 429 || code === 409;
125
+ });
126
+ if (!hadQuotaError) {
127
+ return;
128
+ }
129
+ logger_1.logger.info("");
130
+ logger_1.logger.info("Exceeded maximum retries while deploying functions. " +
131
+ "If you are deploying a large number of functions, " +
132
+ "please deploy your functions in batches by using the --only flag, " +
133
+ "and wait a few minutes before deploying again. " +
134
+ "Go to https://firebase.google.com/docs/cli/#partial_deploys to learn more.");
135
+ }
136
+ function printAbortedErrors(results) {
137
+ const aborted = results.filter((r) => r.error instanceof AbortedDeploymentError);
138
+ if (!aborted) {
139
+ return;
140
+ }
141
+ logger_1.logger.info("");
142
+ logger_1.logger.info("Because there were errors creating or updating functions, the following " +
143
+ "functions were not deleted" +
144
+ aborted.map((result) => `\n\t${functionsDeployHelper_1.getFunctionLabel(result.endpoint)}`).join(""));
145
+ logger_1.logger.info(`To delete these, use ${clc.bold("firebase functions:delete")}`);
146
+ }
147
+ exports.printAbortedErrors = printAbortedErrors;
148
+ function triggerTag(endpoint) {
149
+ var _a;
150
+ const prefix = endpoint.platform === "gcfv1" ? "v1" : "v2";
151
+ if (backend.isScheduleTriggered(endpoint)) {
152
+ return `${prefix}.scheduled`;
153
+ }
154
+ if (backend.isHttpsTriggered(endpoint)) {
155
+ if ((_a = endpoint.labels) === null || _a === void 0 ? void 0 : _a["deployment-callable"]) {
156
+ return `${prefix}.callable`;
157
+ }
158
+ return `${prefix}.https`;
159
+ }
160
+ return endpoint.eventTrigger.eventType;
161
+ }
162
+ exports.triggerTag = triggerTag;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SourceTokenScraper = void 0;
4
+ const logger_1 = require("../../../logger");
5
+ class SourceTokenScraper {
6
+ constructor() {
7
+ this.firstCall = true;
8
+ this.promise = new Promise((resolve) => (this.resolve = resolve));
9
+ }
10
+ tokenPromise() {
11
+ if (this.firstCall) {
12
+ this.firstCall = false;
13
+ return Promise.resolve(undefined);
14
+ }
15
+ return this.promise;
16
+ }
17
+ get poller() {
18
+ return (op) => {
19
+ var _a, _b, _c, _d, _e;
20
+ if (((_a = op.metadata) === null || _a === void 0 ? void 0 : _a.sourceToken) || op.done) {
21
+ const [, , , region] = ((_c = (_b = op.metadata) === null || _b === void 0 ? void 0 : _b.target) === null || _c === void 0 ? void 0 : _c.split("/")) || [];
22
+ logger_1.logger.debug(`Got source token ${(_d = op.metadata) === null || _d === void 0 ? void 0 : _d.sourceToken} for region ${region}`);
23
+ this.resolve((_e = op.metadata) === null || _e === void 0 ? void 0 : _e.sourceToken);
24
+ }
25
+ };
26
+ }
27
+ }
28
+ exports.SourceTokenScraper = SourceTokenScraper;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Timer = void 0;
4
+ class Timer {
5
+ constructor() {
6
+ this.start = process.hrtime.bigint();
7
+ }
8
+ stop() {
9
+ const stop = process.hrtime.bigint();
10
+ const elapsedNanos = stop - this.start;
11
+ return Number(elapsedNanos / BigInt(1e6));
12
+ }
13
+ }
14
+ exports.Timer = Timer;
@@ -2,151 +2,126 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.backendFromV1Alpha1 = void 0;
4
4
  const backend = require("../../backend");
5
+ const proto_1 = require("../../../../gcp/proto");
5
6
  const parsing_1 = require("./parsing");
7
+ const error_1 = require("../../../../error");
6
8
  function backendFromV1Alpha1(yaml, project, region, runtime) {
7
- const bkend = JSON.parse(JSON.stringify(yaml));
8
- delete bkend.specVersion;
9
- tryValidate(bkend);
10
- fillDefaults(bkend, project, region, runtime);
9
+ const manifest = JSON.parse(JSON.stringify(yaml));
10
+ const bkend = backend.empty();
11
+ bkend.requiredAPIs = parseRequiredAPIs(manifest);
12
+ parsing_1.requireKeys("", manifest, "endpoints");
13
+ parsing_1.assertKeyTypes("", manifest, {
14
+ specVersion: "string",
15
+ requiredAPIs: "object",
16
+ endpoints: "object",
17
+ });
18
+ for (const id of Object.keys(manifest.endpoints)) {
19
+ for (const parsed of parseEndpoints(manifest, id, project, region, runtime)) {
20
+ bkend.endpoints[parsed.region] = bkend.endpoints[parsed.region] || {};
21
+ bkend.endpoints[parsed.region][parsed.id] = parsed;
22
+ }
23
+ }
11
24
  return bkend;
12
25
  }
13
26
  exports.backendFromV1Alpha1 = backendFromV1Alpha1;
14
- function tryValidate(typed) {
15
- var _a, _b;
16
- parsing_1.assertKeyTypes("", typed, {
17
- requiredAPIs: "object",
18
- endpoints: "array",
19
- cloudFunctions: "array",
20
- topics: "array",
21
- schedules: "array",
27
+ function parseRequiredAPIs(manifest) {
28
+ const requiredAPIs = {};
29
+ if (typeof manifest !== "object" || Array.isArray(manifest)) {
30
+ throw new error_1.FirebaseError("Expected requiredApis to be a map of string to string");
31
+ }
32
+ for (const [api, reason] of Object.entries(manifest.requiredAPIs || {})) {
33
+ if (typeof reason !== "string") {
34
+ throw new error_1.FirebaseError(`Invalid reason "${JSON.stringify(reason)} for API ${api}. Expected string`);
35
+ }
36
+ requiredAPIs[api] = reason;
37
+ }
38
+ return requiredAPIs;
39
+ }
40
+ function parseEndpoints(manifest, id, project, defaultRegion, runtime) {
41
+ const allParsed = [];
42
+ const prefix = `endpoints[${id}]`;
43
+ const ep = manifest.endpoints[id];
44
+ parsing_1.assertKeyTypes(prefix, ep, {
45
+ region: "array",
46
+ platform: "string",
47
+ entryPoint: "string",
48
+ availableMemoryMb: "number",
49
+ maxInstances: "number",
50
+ minInstances: "number",
51
+ concurrency: "number",
52
+ serviceAccountEmail: "string",
53
+ timeout: "string",
54
+ vpcConnector: "string",
55
+ vpcConnectorEgressSettings: "string",
56
+ labels: "object",
57
+ ingressSettings: "string",
22
58
  environmentVariables: "object",
59
+ httpsTrigger: "object",
60
+ eventTrigger: "object",
61
+ scheduleTrigger: "object",
23
62
  });
24
- parsing_1.requireKeys("", typed, "cloudFunctions");
25
- for (let ndx = 0; ndx < typed.cloudFunctions.length; ndx++) {
26
- const prefix = `cloudFunctions[${ndx}]`;
27
- const func = typed.cloudFunctions[ndx];
28
- parsing_1.requireKeys(prefix, func, "platform", "id", "entryPoint", "trigger");
29
- parsing_1.assertKeyTypes(prefix, func, {
30
- platform: "string",
31
- id: "string",
32
- region: "string",
33
- project: "string",
34
- runtime: "string",
35
- entryPoint: "string",
36
- availableMemoryMb: "number",
37
- maxInstances: "number",
38
- minInstances: "number",
39
- concurrency: "number",
40
- serviceAccountEmail: "string",
41
- timeout: "string",
42
- trigger: "object",
43
- vpcConnector: "string",
44
- vpcConnectorEgressSettings: "string",
45
- labels: "object",
46
- ingressSettings: "string",
47
- environmentVariables: "omit",
48
- uri: "omit",
49
- sourceUploadUrl: "omit",
50
- });
51
- if (backend.isEventTrigger(func.trigger)) {
52
- parsing_1.requireKeys(prefix + ".trigger", func.trigger, "eventType", "eventFilters");
53
- parsing_1.assertKeyTypes(prefix + ".trigger", func.trigger, {
63
+ let triggerCount = 0;
64
+ if (ep.httpsTrigger) {
65
+ triggerCount++;
66
+ }
67
+ if (ep.eventTrigger) {
68
+ triggerCount++;
69
+ }
70
+ if (ep.scheduleTrigger) {
71
+ triggerCount++;
72
+ }
73
+ if (!triggerCount) {
74
+ throw new error_1.FirebaseError("Expected trigger in endpoint" + id);
75
+ }
76
+ if (triggerCount > 1) {
77
+ throw new error_1.FirebaseError("Multiple triggers defined for endpoint" + id);
78
+ }
79
+ for (const region of ep.region || [defaultRegion]) {
80
+ let triggered;
81
+ if (backend.isEventTriggered(ep)) {
82
+ parsing_1.requireKeys(prefix + ".eventTrigger", ep.eventTrigger, "eventType", "eventFilters");
83
+ parsing_1.assertKeyTypes(prefix + ".eventTrigger", ep.eventTrigger, {
54
84
  eventFilters: "object",
55
85
  eventType: "string",
56
86
  retry: "boolean",
57
87
  region: "string",
58
88
  serviceAccountEmail: "string",
59
89
  });
90
+ triggered = { eventTrigger: ep.eventTrigger };
60
91
  }
61
- else {
62
- parsing_1.assertKeyTypes(prefix + ".trigger", func.trigger, {
92
+ else if (backend.isHttpsTriggered(ep)) {
93
+ parsing_1.assertKeyTypes(prefix + ".httpsTrigger", ep.httpsTrigger, {
63
94
  invoker: "array",
64
95
  });
96
+ triggered = { httpsTrigger: {} };
97
+ proto_1.copyIfPresent(triggered.httpsTrigger, ep.httpsTrigger, "invoker");
65
98
  }
66
- }
67
- for (let ndx = 0; ndx < ((_a = typed.topics) === null || _a === void 0 ? void 0 : _a.length); ndx++) {
68
- let prefix = `topics[${ndx}]`;
69
- const topic = typed.topics[ndx];
70
- parsing_1.requireKeys(prefix, topic, "id", "targetService");
71
- parsing_1.assertKeyTypes(prefix, topic, {
72
- id: "string",
73
- labels: "object",
74
- project: "string",
75
- targetService: "object",
76
- });
77
- prefix += ".targetService";
78
- parsing_1.requireKeys(prefix, topic.targetService, "id");
79
- parsing_1.assertKeyTypes(prefix, topic.targetService, {
80
- id: "string",
81
- project: "string",
82
- region: "string",
83
- });
84
- }
85
- for (let ndx = 0; ndx < ((_b = typed.schedules) === null || _b === void 0 ? void 0 : _b.length); ndx++) {
86
- let prefix = `schedules[${ndx}]`;
87
- const schedule = typed.schedules[ndx];
88
- parsing_1.requireKeys(prefix, schedule, "id", "schedule", "transport", "targetService");
89
- parsing_1.assertKeyTypes(prefix, schedule, {
90
- id: "string",
91
- project: "string",
92
- retryConfig: "object",
93
- schedule: "string",
94
- timeZone: "string",
95
- transport: "string",
96
- targetService: "object",
97
- });
98
- parsing_1.assertKeyTypes(prefix + ".retryConfig", schedule.retryConfig, {
99
- maxBackoffDuration: "string",
100
- minBackoffDuration: "string",
101
- maxDoublings: "number",
102
- maxRetryDuration: "string",
103
- retryCount: "number",
104
- });
105
- parsing_1.requireKeys((prefix = ".targetService"), schedule.targetService, "id");
106
- parsing_1.assertKeyTypes(prefix + ".targetService", schedule.targetService, {
107
- id: "string",
108
- project: "string",
109
- region: "string",
110
- });
111
- }
112
- }
113
- function fillDefaults(want, project, region, runtime) {
114
- want.requiredAPIs = want.requiredAPIs || {};
115
- want.environmentVariables = want.environmentVariables || {};
116
- want.schedules = want.schedules || [];
117
- want.topics = want.topics || [];
118
- want.endpoints = want.endpoints || [];
119
- for (const cloudFunction of want.cloudFunctions) {
120
- if (!cloudFunction.project) {
121
- cloudFunction.project = project;
122
- }
123
- if (!cloudFunction.region) {
124
- cloudFunction.region = region;
125
- }
126
- if (!cloudFunction.runtime) {
127
- cloudFunction.runtime = runtime;
128
- }
129
- }
130
- for (const topic of want.topics) {
131
- if (!topic.project) {
132
- topic.project = project;
133
- }
134
- if (!topic.targetService.project) {
135
- topic.targetService.project = project;
136
- }
137
- if (!topic.targetService.region) {
138
- topic.targetService.region = region;
139
- }
140
- }
141
- for (const schedule of want.schedules) {
142
- if (!schedule.project) {
143
- schedule.project = project;
144
- }
145
- if (!schedule.targetService.project) {
146
- schedule.targetService.project = project;
99
+ else if (backend.isScheduleTriggered(ep)) {
100
+ parsing_1.assertKeyTypes(prefix + ".scheduleTrigger", ep.scheduleTrigger, {
101
+ schedule: "string",
102
+ timeZone: "string",
103
+ retryConfig: "object",
104
+ });
105
+ parsing_1.assertKeyTypes(prefix + ".scheduleTrigger.retryConfig", ep.scheduleTrigger.retryConfig, {
106
+ retryCount: "number",
107
+ maxDoublings: "number",
108
+ minBackoffDuration: "string",
109
+ maxBackoffDuration: "string",
110
+ maxRetryDuration: "string",
111
+ });
112
+ triggered = { scheduleTrigger: ep.scheduleTrigger };
147
113
  }
148
- if (!schedule.targetService.region) {
149
- schedule.targetService.region = region;
114
+ else {
115
+ throw new error_1.FirebaseError(`Do not recognize trigger type for endpoint ${id}. Try upgrading ` +
116
+ "firebase-tools with npm install -g firebase-tools@latest");
150
117
  }
118
+ parsing_1.requireKeys(prefix, ep, "entryPoint");
119
+ const parsed = Object.assign({ platform: ep.platform || "gcfv2", id,
120
+ region,
121
+ project,
122
+ runtime, entryPoint: ep.entryPoint }, triggered);
123
+ proto_1.copyIfPresent(parsed, ep, "availableMemoryMb", "maxInstances", "minInstances", "concurrency", "serviceAccountEmail", "timeout", "vpcConnector", "vpcConnectorEgressSettings", "labels", "ingressSettings", "environmentVariables");
124
+ allParsed.push(parsed);
151
125
  }
126
+ return allParsed;
152
127
  }
@@ -67,71 +67,50 @@ function addResourcesToBackend(projectId, runtime, annotation, want) {
67
67
  var _a;
68
68
  Object.freeze(annotation);
69
69
  for (const region of annotation.regions || [api.functionsDefaultRegion]) {
70
- let trigger;
70
+ let triggered;
71
71
  if (!!annotation.httpsTrigger == !!annotation.eventTrigger) {
72
72
  throw new error_1.FirebaseError("Unexpected annotation generated by the Firebase Functions SDK. This should never happen.");
73
73
  }
74
74
  if (annotation.httpsTrigger) {
75
- trigger = {};
75
+ const trigger = {};
76
76
  if (annotation.failurePolicy) {
77
77
  logger_1.logger.warn(`Ignoring retry policy for HTTPS function ${annotation.name}`);
78
78
  }
79
- proto.copyIfPresent(trigger, annotation.httpsTrigger, "invoker", "invoker");
79
+ proto.copyIfPresent(trigger, annotation.httpsTrigger, "invoker");
80
+ triggered = { httpsTrigger: trigger };
81
+ }
82
+ else if (annotation.schedule) {
83
+ want.requiredAPIs["pubsub"] = "pubsub.googleapis.com";
84
+ want.requiredAPIs["scheduler"] = "cloudscheduler.googleapis.com";
85
+ triggered = { scheduleTrigger: annotation.schedule };
80
86
  }
81
87
  else {
82
- trigger = {
83
- eventType: annotation.eventTrigger.eventType,
84
- eventFilters: {
85
- resource: annotation.eventTrigger.resource,
88
+ triggered = {
89
+ eventTrigger: {
90
+ eventType: annotation.eventTrigger.eventType,
91
+ eventFilters: {
92
+ resource: annotation.eventTrigger.resource,
93
+ },
94
+ retry: !!annotation.failurePolicy,
86
95
  },
87
- retry: !!annotation.failurePolicy,
88
96
  };
89
97
  if (exports.GCS_EVENTS.has(((_a = annotation.eventTrigger) === null || _a === void 0 ? void 0 : _a.eventType) || "")) {
90
- trigger.eventFilters = {
98
+ triggered.eventTrigger.eventFilters = {
91
99
  bucket: annotation.eventTrigger.resource,
92
100
  };
93
101
  }
94
102
  }
95
- const cloudFunctionName = {
96
- id: annotation.name,
97
- region: region,
98
- project: projectId,
99
- };
100
- const cloudFunction = Object.assign(Object.assign({ platform: annotation.platform || "gcfv1" }, cloudFunctionName), { entryPoint: annotation.entryPoint, runtime: runtime, trigger: trigger });
103
+ const endpoint = Object.assign({ platform: annotation.platform || "gcfv1", id: annotation.name, region: region, project: projectId, entryPoint: annotation.entryPoint, runtime: runtime }, triggered);
101
104
  if (annotation.vpcConnector) {
102
105
  let maybeId = annotation.vpcConnector;
103
106
  if (!maybeId.includes("/")) {
104
107
  maybeId = `projects/${projectId}/locations/${region}/connectors/${maybeId}`;
105
108
  }
106
- cloudFunction.vpcConnector = maybeId;
107
- }
108
- proto.copyIfPresent(cloudFunction, annotation, "concurrency", "serviceAccountEmail", "labels", "vpcConnectorEgressSettings", "ingressSettings", "timeout", "maxInstances", "minInstances", "availableMemoryMb");
109
- if (annotation.schedule) {
110
- want.requiredAPIs["pubsub"] = "pubsub.googleapis.com";
111
- want.requiredAPIs["scheduler"] = "cloudscheduler.googleapis.com";
112
- const id = backend.scheduleIdForFunction(cloudFunctionName);
113
- const schedule = {
114
- id,
115
- project: projectId,
116
- schedule: annotation.schedule.schedule,
117
- transport: "pubsub",
118
- targetService: cloudFunctionName,
119
- };
120
- proto.copyIfPresent(schedule, annotation.schedule, "timeZone", "retryConfig");
121
- want.schedules.push(schedule);
122
- const topic = {
123
- id,
124
- project: projectId,
125
- labels: backend.SCHEDULED_FUNCTION_LABEL,
126
- targetService: cloudFunctionName,
127
- };
128
- want.topics.push(topic);
129
- if (backend.isEventTrigger(cloudFunction.trigger)) {
130
- cloudFunction.trigger.eventFilters.resource = `${cloudFunction.trigger.eventFilters.resource}/${id}`;
131
- }
132
- cloudFunction.labels = Object.assign(Object.assign({}, cloudFunction.labels), { "deployment-scheduled": "true" });
109
+ endpoint.vpcConnector = maybeId;
133
110
  }
134
- want.cloudFunctions.push(cloudFunction);
111
+ proto.copyIfPresent(endpoint, annotation, "concurrency", "serviceAccountEmail", "labels", "vpcConnectorEgressSettings", "ingressSettings", "timeout", "maxInstances", "minInstances", "availableMemoryMb");
112
+ want.endpoints[region] = want.endpoints[region] || {};
113
+ want.endpoints[region][endpoint.id] = endpoint;
135
114
  }
136
115
  }
137
116
  exports.addResourcesToBackend = addResourcesToBackend;
@@ -1,32 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.setTriggerRegion = void 0;
3
+ exports.lookupMissingTriggerRegions = void 0;
4
4
  const backend = require("./backend");
5
5
  const storage = require("../../gcp/storage");
6
6
  const error_1 = require("../../error");
7
- async function setTriggerRegion(want, have) {
8
- var _a;
9
- for (const wantFn of want) {
10
- if (wantFn.platform === "gcfv1" || !backend.isEventTrigger(wantFn.trigger)) {
7
+ const logger_1 = require("../../logger");
8
+ const noop = () => Promise.resolve();
9
+ const LOOKUP_BY_EVENT_TYPE = {
10
+ "google.cloud.pubsub.topic.v1.messagePublished": noop,
11
+ "google.cloud.storage.object.v1.finalized": lookupBucketRegion,
12
+ "google.cloud.storage.object.v1.archived": lookupBucketRegion,
13
+ "google.cloud.storage.object.v1.deleted": lookupBucketRegion,
14
+ "google.cloud.storage.object.v1.metadataUpdated": lookupBucketRegion,
15
+ };
16
+ async function lookupMissingTriggerRegions(want) {
17
+ const regionLookups = [];
18
+ for (const ep of backend.allEndpoints(want)) {
19
+ if (ep.platform === "gcfv1" || !backend.isEventTriggered(ep) || ep.eventTrigger.region) {
11
20
  continue;
12
21
  }
13
- const match = (_a = have.find(backend.sameFunctionName(wantFn))) === null || _a === void 0 ? void 0 : _a.trigger;
14
- if (match === null || match === void 0 ? void 0 : match.region) {
15
- wantFn.trigger.region = match.region;
16
- }
17
- else {
18
- await setTriggerRegionFromTriggerType(wantFn.trigger);
22
+ const lookup = LOOKUP_BY_EVENT_TYPE[ep.eventTrigger.eventType];
23
+ if (!lookup) {
24
+ logger_1.logger.debug("Don't know how to look up trigger region for event type", ep.eventTrigger.eventType, ". Deploy will fail unless this event type is global");
25
+ continue;
19
26
  }
27
+ regionLookups.push(lookup(ep));
20
28
  }
29
+ await Promise.all(regionLookups);
21
30
  }
22
- exports.setTriggerRegion = setTriggerRegion;
23
- async function setTriggerRegionFromTriggerType(trigger) {
24
- if (trigger.eventFilters.bucket) {
25
- try {
26
- trigger.region = (await storage.getBucket(trigger.eventFilters.bucket)).location.toLowerCase();
27
- }
28
- catch (err) {
29
- throw new error_1.FirebaseError("Can't find the storage bucket region", { original: err });
30
- }
31
+ exports.lookupMissingTriggerRegions = lookupMissingTriggerRegions;
32
+ async function lookupBucketRegion(endpoint) {
33
+ try {
34
+ const bucket = await storage.getBucket(endpoint.eventTrigger.eventFilters.bucket);
35
+ endpoint.eventTrigger.region = bucket.location.toLowerCase();
36
+ }
37
+ catch (err) {
38
+ throw new error_1.FirebaseError("Can't find the storage bucket region", { original: err });
31
39
  }
32
40
  }
@@ -1,10 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.checkForInvalidChangeOfTrigger = exports.functionIdsAreValid = exports.functionsDirectoryExists = void 0;
3
+ exports.functionIdsAreValid = exports.functionsDirectoryExists = void 0;
4
4
  const clc = require("cli-color");
5
5
  const error_1 = require("../../error");
6
- const functionsDeployHelper_1 = require("./functionsDeployHelper");
7
- const backend = require("./backend");
8
6
  const fsutils = require("../../fsutils");
9
7
  const projectPath = require("../../projectPath");
10
8
  function functionsDirectoryExists(options, sourceDirName) {
@@ -36,24 +34,3 @@ function functionIdsAreValid(functions) {
36
34
  }
37
35
  }
38
36
  exports.functionIdsAreValid = functionIdsAreValid;
39
- function checkForInvalidChangeOfTrigger(fn, exFn) {
40
- var _a, _b;
41
- const wantEventTrigger = backend.isEventTrigger(fn.trigger);
42
- const haveEventTrigger = backend.isEventTrigger(exFn.trigger);
43
- if (!wantEventTrigger && haveEventTrigger) {
44
- throw new error_1.FirebaseError(`[${functionsDeployHelper_1.getFunctionLabel(fn)}] Changing from a background triggered function to an HTTPS function is not allowed. Please delete your function and create a new one instead.`);
45
- }
46
- if (wantEventTrigger && !haveEventTrigger) {
47
- throw new error_1.FirebaseError(`[${functionsDeployHelper_1.getFunctionLabel(fn)}] Changing from an HTTPS function to an background triggered function is not allowed. Please delete your function and create a new one instead.`);
48
- }
49
- if (fn.platform == "gcfv2" && exFn.platform == "gcfv1") {
50
- throw new error_1.FirebaseError(`[${functionsDeployHelper_1.getFunctionLabel(fn)}] Upgrading from GCFv1 to GCFv2 is not yet supported. Please delete your old function or wait for this feature to be ready.`);
51
- }
52
- if (fn.platform == "gcfv1" && exFn.platform == "gcfv2") {
53
- throw new error_1.FirebaseError(`[${functionsDeployHelper_1.getFunctionLabel(fn)}] Functions cannot be downgraded from GCFv2 to GCFv1`);
54
- }
55
- if (((_a = exFn.labels) === null || _a === void 0 ? void 0 : _a["deployment-scheduled"]) && !((_b = fn.labels) === null || _b === void 0 ? void 0 : _b["deployment-scheduled"])) {
56
- throw new error_1.FirebaseError(`[${functionsDeployHelper_1.getFunctionLabel(fn)}] Scheduled functions cannot be changed to event handler or HTTP functions`);
57
- }
58
- }
59
- exports.checkForInvalidChangeOfTrigger = checkForInvalidChangeOfTrigger;