firebase-tools 9.17.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 (95) hide show
  1. package/CHANGELOG.md +3 -7
  2. package/lib/api.js +1 -0
  3. package/lib/apiv2.js +5 -3
  4. package/lib/appdistribution/client.js +84 -72
  5. package/lib/appdistribution/distribution.js +8 -26
  6. package/lib/appdistribution/options-parser-util.js +51 -0
  7. package/lib/command.js +1 -1
  8. package/lib/commands/appdistribution-distribute.js +74 -91
  9. package/lib/commands/appdistribution-testers-add.js +18 -0
  10. package/lib/commands/appdistribution-testers-remove.js +32 -0
  11. package/lib/commands/crashlytics-symbols-upload.js +146 -0
  12. package/lib/commands/ext-configure.js +9 -1
  13. package/lib/commands/ext-dev-extension-delete.js +2 -1
  14. package/lib/commands/ext-dev-publish.js +11 -4
  15. package/lib/commands/ext-dev-unpublish.js +12 -4
  16. package/lib/commands/ext-install.js +115 -48
  17. package/lib/commands/ext-uninstall.js +6 -0
  18. package/lib/commands/ext-update.js +61 -18
  19. package/lib/commands/functions-config-export.js +115 -0
  20. package/lib/commands/functions-delete.js +45 -25
  21. package/lib/commands/functions-list.js +54 -0
  22. package/lib/commands/functions-log.js +5 -22
  23. package/lib/commands/hosting-channel-deploy.js +6 -4
  24. package/lib/commands/index.js +12 -0
  25. package/lib/deploy/functions/backend.js +118 -121
  26. package/lib/deploy/functions/checkIam.js +8 -8
  27. package/lib/deploy/functions/containerCleaner.js +5 -1
  28. package/lib/deploy/functions/deploy.js +11 -15
  29. package/lib/deploy/functions/functionsDeployHelper.js +3 -68
  30. package/lib/deploy/functions/prepare.js +67 -33
  31. package/lib/deploy/functions/pricing.js +17 -17
  32. package/lib/deploy/functions/prompts.js +24 -41
  33. package/lib/deploy/functions/release/executor.js +39 -0
  34. package/lib/deploy/functions/release/fabricator.js +362 -0
  35. package/lib/deploy/functions/release/index.js +69 -0
  36. package/lib/deploy/functions/release/planner.js +159 -0
  37. package/lib/deploy/functions/release/reporter.js +162 -0
  38. package/lib/deploy/functions/release/sourceTokenScraper.js +28 -0
  39. package/lib/deploy/functions/release/timer.js +14 -0
  40. package/lib/deploy/functions/runtimes/discovery/v1alpha1.js +102 -126
  41. package/lib/deploy/functions/runtimes/node/parseTriggers.js +34 -50
  42. package/lib/deploy/functions/triggerRegionHelper.js +40 -0
  43. package/lib/deploy/functions/validate.js +1 -24
  44. package/lib/downloadUtils.js +37 -0
  45. package/lib/emulator/auth/apiSpec.js +1788 -403
  46. package/lib/emulator/auth/handlers.js +6 -5
  47. package/lib/emulator/auth/operations.js +439 -40
  48. package/lib/emulator/auth/server.js +32 -11
  49. package/lib/emulator/auth/state.js +205 -5
  50. package/lib/emulator/auth/widget_ui.js +2 -2
  51. package/lib/emulator/download.js +2 -31
  52. package/lib/emulator/downloadableEmulators.js +7 -7
  53. package/lib/emulator/emulatorLogger.js +0 -3
  54. package/lib/emulator/events/types.js +16 -0
  55. package/lib/emulator/functionsEmulator.js +117 -20
  56. package/lib/emulator/functionsEmulatorRuntime.js +46 -121
  57. package/lib/emulator/functionsEmulatorShared.js +51 -7
  58. package/lib/emulator/functionsEmulatorShell.js +1 -1
  59. package/lib/emulator/pubsubEmulator.js +61 -40
  60. package/lib/emulator/storage/cloudFunctions.js +37 -7
  61. package/lib/extensions/askUserForConsent.js +16 -13
  62. package/lib/extensions/askUserForParam.js +72 -3
  63. package/lib/extensions/billingMigrationHelper.js +1 -11
  64. package/lib/extensions/changelog.js +2 -1
  65. package/lib/extensions/displayExtensionInfo.js +35 -33
  66. package/lib/extensions/emulator/optionsHelper.js +3 -3
  67. package/lib/extensions/emulator/triggerHelper.js +2 -32
  68. package/lib/extensions/extensionsApi.js +67 -94
  69. package/lib/extensions/extensionsHelper.js +49 -35
  70. package/lib/extensions/paramHelper.js +79 -36
  71. package/lib/extensions/refs.js +59 -0
  72. package/lib/extensions/secretsUtils.js +58 -0
  73. package/lib/extensions/updateHelper.js +21 -45
  74. package/lib/extensions/warnings.js +1 -7
  75. package/lib/functional.js +64 -0
  76. package/lib/functions/env.js +26 -13
  77. package/lib/functions/functionslog.js +40 -0
  78. package/lib/functions/runtimeConfigExport.js +137 -0
  79. package/lib/gcp/cloudfunctions.js +46 -38
  80. package/lib/gcp/cloudfunctionsv2.js +47 -47
  81. package/lib/gcp/cloudlogging.js +27 -21
  82. package/lib/gcp/cloudscheduler.js +22 -16
  83. package/lib/gcp/pubsub.js +1 -9
  84. package/lib/gcp/secretManager.js +111 -0
  85. package/lib/gcp/storage.js +16 -0
  86. package/lib/previews.js +1 -1
  87. package/lib/requireInteractive.js +12 -0
  88. package/lib/utils.js +30 -1
  89. package/package.json +5 -4
  90. package/lib/deploy/functions/deploymentPlanner.js +0 -113
  91. package/lib/deploy/functions/deploymentTimer.js +0 -23
  92. package/lib/deploy/functions/errorHandler.js +0 -75
  93. package/lib/deploy/functions/release.js +0 -116
  94. package/lib/deploy/functions/tasks.js +0 -324
  95. package/lib/functionsDelete.js +0 -60
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkForV2Upgrade = exports.checkForIllegalUpdate = exports.upgradedScheduleFromV1ToV2 = exports.changedV2PubSubTopic = exports.changedTriggerRegion = exports.upgradedToGCFv2WithoutSettingConcurrency = exports.createDeploymentPlan = exports.calculateUpdate = exports.calculateRegionalChanges = void 0;
4
+ const functionsDeployHelper_1 = require("../functionsDeployHelper");
5
+ const functionsDeployHelper_2 = require("../functionsDeployHelper");
6
+ const deploymentTool_1 = require("../../../deploymentTool");
7
+ const error_1 = require("../../../error");
8
+ const utils = require("../../../utils");
9
+ const backend = require("../backend");
10
+ const gcfv2 = require("../../../gcp/cloudfunctionsv2");
11
+ function calculateRegionalChanges(want, have, options) {
12
+ const endpointsToCreate = Object.keys(want)
13
+ .filter((id) => !have[id])
14
+ .map((id) => want[id]);
15
+ const endpointsToDelete = Object.keys(have)
16
+ .filter((id) => !want[id])
17
+ .filter((id) => options.deleteAll || deploymentTool_1.isFirebaseManaged(have[id].labels || {}))
18
+ .map((id) => have[id]);
19
+ const endpointsToUpdate = Object.keys(want)
20
+ .filter((id) => have[id])
21
+ .map((id) => calculateUpdate(want[id], have[id]));
22
+ return { endpointsToCreate, endpointsToUpdate, endpointsToDelete };
23
+ }
24
+ exports.calculateRegionalChanges = calculateRegionalChanges;
25
+ function calculateUpdate(want, have) {
26
+ checkForIllegalUpdate(want, have);
27
+ const update = {
28
+ endpoint: want,
29
+ };
30
+ const needsDelete = changedTriggerRegion(want, have) ||
31
+ changedV2PubSubTopic(want, have) ||
32
+ upgradedScheduleFromV1ToV2(want, have);
33
+ if (needsDelete) {
34
+ update.deleteAndRecreate = have;
35
+ }
36
+ return update;
37
+ }
38
+ exports.calculateUpdate = calculateUpdate;
39
+ function createDeploymentPlan(want, have, options = {}) {
40
+ const deployment = {};
41
+ want = backend.matchingBackend(want, (endpoint) => {
42
+ return functionsDeployHelper_1.functionMatchesAnyGroup(endpoint, options.filters || []);
43
+ });
44
+ have = backend.matchingBackend(have, (endpoint) => {
45
+ return functionsDeployHelper_1.functionMatchesAnyGroup(endpoint, options.filters || []);
46
+ });
47
+ const regions = new Set([...Object.keys(want.endpoints), ...Object.keys(have.endpoints)]);
48
+ for (const region of regions) {
49
+ deployment[region] = calculateRegionalChanges(want.endpoints[region] || {}, have.endpoints[region] || {}, options);
50
+ }
51
+ if (upgradedToGCFv2WithoutSettingConcurrency(want, have)) {
52
+ utils.logLabeledBullet("functions", "You are updating one or more functions to Google Cloud Functions v2, " +
53
+ "which introduces support for concurrent execution. New functions " +
54
+ "default to 80 concurrent executions, but existing functions keep the " +
55
+ "old default of 1. You can change this with the 'concurrency' option.");
56
+ }
57
+ return deployment;
58
+ }
59
+ exports.createDeploymentPlan = createDeploymentPlan;
60
+ function upgradedToGCFv2WithoutSettingConcurrency(want, have) {
61
+ return backend.someEndpoint(want, (endpoint) => {
62
+ var _a, _b;
63
+ if (((_b = (_a = have.endpoints[endpoint.region]) === null || _a === void 0 ? void 0 : _a[endpoint.id]) === null || _b === void 0 ? void 0 : _b.platform) !== "gcfv1") {
64
+ return false;
65
+ }
66
+ if (endpoint.platform !== "gcfv2") {
67
+ return false;
68
+ }
69
+ if (endpoint.concurrency) {
70
+ return false;
71
+ }
72
+ return true;
73
+ });
74
+ }
75
+ exports.upgradedToGCFv2WithoutSettingConcurrency = upgradedToGCFv2WithoutSettingConcurrency;
76
+ function changedTriggerRegion(want, have) {
77
+ if (want.platform != "gcfv2") {
78
+ return false;
79
+ }
80
+ if (have.platform != "gcfv2") {
81
+ return false;
82
+ }
83
+ if (!backend.isEventTriggered(want)) {
84
+ return false;
85
+ }
86
+ if (!backend.isEventTriggered(have)) {
87
+ return false;
88
+ }
89
+ return want.eventTrigger.region != have.eventTrigger.region;
90
+ }
91
+ exports.changedTriggerRegion = changedTriggerRegion;
92
+ function changedV2PubSubTopic(want, have) {
93
+ if (want.platform !== "gcfv2") {
94
+ return false;
95
+ }
96
+ if (have.platform !== "gcfv2") {
97
+ return false;
98
+ }
99
+ if (!backend.isEventTriggered(want)) {
100
+ return false;
101
+ }
102
+ if (!backend.isEventTriggered(have)) {
103
+ return false;
104
+ }
105
+ if (want.eventTrigger.eventType != gcfv2.PUBSUB_PUBLISH_EVENT) {
106
+ return false;
107
+ }
108
+ if (have.eventTrigger.eventType !== gcfv2.PUBSUB_PUBLISH_EVENT) {
109
+ return false;
110
+ }
111
+ return have.eventTrigger.eventFilters["resource"] != want.eventTrigger.eventFilters["resource"];
112
+ }
113
+ exports.changedV2PubSubTopic = changedV2PubSubTopic;
114
+ function upgradedScheduleFromV1ToV2(want, have) {
115
+ if (have.platform !== "gcfv1") {
116
+ return false;
117
+ }
118
+ if (want.platform !== "gcfv2") {
119
+ return false;
120
+ }
121
+ if (!backend.isScheduleTriggered(have)) {
122
+ return false;
123
+ }
124
+ if (!backend.isScheduleTriggered(want)) {
125
+ return false;
126
+ }
127
+ return true;
128
+ }
129
+ exports.upgradedScheduleFromV1ToV2 = upgradedScheduleFromV1ToV2;
130
+ function checkForIllegalUpdate(want, have) {
131
+ const triggerType = (e) => {
132
+ if (backend.isHttpsTriggered(e)) {
133
+ return "an HTTPS";
134
+ }
135
+ else if (backend.isEventTriggered(e)) {
136
+ return "a background triggered";
137
+ }
138
+ else if (backend.isScheduleTriggered(e)) {
139
+ return "a scheduled";
140
+ }
141
+ throw Error("Functions release planner is not able to handle an unknown trigger type");
142
+ };
143
+ const wantType = triggerType(want);
144
+ const haveType = triggerType(have);
145
+ if (wantType != haveType) {
146
+ throw new error_1.FirebaseError(`[${functionsDeployHelper_2.getFunctionLabel(want)}] Changing from ${haveType} function to ${wantType} function is not allowed. Please delete your function and create a new one instead.`);
147
+ }
148
+ if (want.platform == "gcfv1" && have.platform == "gcfv2") {
149
+ throw new error_1.FirebaseError(`[${functionsDeployHelper_2.getFunctionLabel(want)}] Functions cannot be downgraded from GCFv2 to GCFv1`);
150
+ }
151
+ exports.checkForV2Upgrade(want, have);
152
+ }
153
+ exports.checkForIllegalUpdate = checkForIllegalUpdate;
154
+ function checkForV2Upgrade(want, have) {
155
+ if (want.platform == "gcfv2" && have.platform == "gcfv1") {
156
+ throw new error_1.FirebaseError(`[${functionsDeployHelper_2.getFunctionLabel(have)}] Upgrading from GCFv1 to GCFv2 is not yet supported. Please delete your old function or wait for this feature to be ready.`);
157
+ }
158
+ }
159
+ exports.checkForV2Upgrade = checkForV2Upgrade;
@@ -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,150 +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
- cloudFunctions: "array",
19
- topics: "array",
20
- 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",
21
58
  environmentVariables: "object",
59
+ httpsTrigger: "object",
60
+ eventTrigger: "object",
61
+ scheduleTrigger: "object",
22
62
  });
23
- parsing_1.requireKeys("", typed, "cloudFunctions");
24
- for (let ndx = 0; ndx < typed.cloudFunctions.length; ndx++) {
25
- const prefix = `cloudFunctions[${ndx}]`;
26
- const func = typed.cloudFunctions[ndx];
27
- parsing_1.requireKeys(prefix, func, "platform", "id", "entryPoint", "trigger");
28
- parsing_1.assertKeyTypes(prefix, func, {
29
- platform: "string",
30
- id: "string",
31
- region: "string",
32
- project: "string",
33
- runtime: "string",
34
- entryPoint: "string",
35
- availableMemoryMb: "number",
36
- maxInstances: "number",
37
- minInstances: "number",
38
- concurrency: "number",
39
- serviceAccountEmail: "string",
40
- timeout: "string",
41
- trigger: "object",
42
- vpcConnector: "string",
43
- vpcConnectorEgressSettings: "string",
44
- labels: "object",
45
- ingressSettings: "string",
46
- environmentVariables: "omit",
47
- uri: "omit",
48
- sourceUploadUrl: "omit",
49
- });
50
- if (backend.isEventTrigger(func.trigger)) {
51
- parsing_1.requireKeys(prefix + ".trigger", func.trigger, "eventType", "eventFilters");
52
- 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, {
53
84
  eventFilters: "object",
54
85
  eventType: "string",
55
86
  retry: "boolean",
56
87
  region: "string",
57
88
  serviceAccountEmail: "string",
58
89
  });
90
+ triggered = { eventTrigger: ep.eventTrigger };
59
91
  }
60
- else {
61
- parsing_1.assertKeyTypes(prefix + ".trigger", func.trigger, {
62
- allowInsecure: "boolean",
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
- for (const cloudFunction of want.cloudFunctions) {
119
- if (!cloudFunction.project) {
120
- cloudFunction.project = project;
121
- }
122
- if (!cloudFunction.region) {
123
- cloudFunction.region = region;
124
- }
125
- if (!cloudFunction.runtime) {
126
- cloudFunction.runtime = runtime;
127
- }
128
- }
129
- for (const topic of want.topics) {
130
- if (!topic.project) {
131
- topic.project = project;
132
- }
133
- if (!topic.targetService.project) {
134
- topic.targetService.project = project;
135
- }
136
- if (!topic.targetService.region) {
137
- topic.targetService.region = region;
138
- }
139
- }
140
- for (const schedule of want.schedules) {
141
- if (!schedule.project) {
142
- schedule.project = project;
143
- }
144
- if (!schedule.targetService.project) {
145
- 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 };
146
113
  }
147
- if (!schedule.targetService.region) {
148
- 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");
149
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);
150
125
  }
126
+ return allParsed;
151
127
  }