firebase-tools 15.25.0 → 15.25.1
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.
- package/lib/appcheck/prompts.js +2 -2
- package/lib/commands/appcheck-debugtokens-delete.js +1 -1
- package/lib/commands/appcheck-debugtokens-list.js +1 -1
- package/lib/deploy/functions/build.js +1 -1
- package/lib/deploy/functions/functionsDeployHelper.js +18 -12
- package/lib/deploy/functions/prepare.js +16 -3
- package/lib/deploy/functions/release/executor.js +51 -8
- package/lib/deploy/functions/release/fabricator.js +34 -5
- package/lib/emulator/controller.js +34 -15
- package/lib/experiments.js +6 -0
- package/lib/functions/projectConfig.js +98 -12
- package/lib/serve/functions.js +25 -7
- package/lib/tsconfig.compile.tsbuildinfo +1 -1
- package/lib/tsconfig.publish.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/schema/firebase-config.json +93 -0
package/lib/appcheck/prompts.js
CHANGED
|
@@ -8,7 +8,7 @@ const appUtils_1 = require("../appUtils");
|
|
|
8
8
|
const error_1 = require("../error");
|
|
9
9
|
const logger_1 = require("../logger");
|
|
10
10
|
const clc = require("colorette");
|
|
11
|
-
async function getOrPromptAppId(options) {
|
|
11
|
+
async function getOrPromptAppId(options, message = "Select the app to register a debug token for:") {
|
|
12
12
|
const projectId = (0, projectUtils_1.needProjectId)(options);
|
|
13
13
|
logger_1.logger.info(`Active Project: ${clc.bold(projectId)}`);
|
|
14
14
|
if (options.app) {
|
|
@@ -34,7 +34,7 @@ async function getOrPromptAppId(options) {
|
|
|
34
34
|
throw new error_1.FirebaseError(`Project ${projectId} has multiple apps, must specify an app id.`);
|
|
35
35
|
}
|
|
36
36
|
const selectedApp = await (0, apps_1.selectAppInteractively)(apps, apps_1.AppPlatform.ANY, {
|
|
37
|
-
message
|
|
37
|
+
message,
|
|
38
38
|
});
|
|
39
39
|
return { projectId, appId: selectedApp.appId };
|
|
40
40
|
}
|
|
@@ -16,7 +16,7 @@ exports.command = new command_1.Command("appcheck:debugtokens:delete <debugToken
|
|
|
16
16
|
.option("--force", "attempt to delete debug token without prompting for confirmation")
|
|
17
17
|
.before(requireAuth_1.requireAuth)
|
|
18
18
|
.action(async (debugTokenId, options) => {
|
|
19
|
-
const { appId } = await (0, prompts_1.getOrPromptProjectAndAppId)(options);
|
|
19
|
+
const { appId } = await (0, prompts_1.getOrPromptProjectAndAppId)(options, "Select the app to delete a debug token from:");
|
|
20
20
|
const projectNumber = await (0, projectUtils_1.needProjectNumber)(options);
|
|
21
21
|
let debugTokenName = debugTokenId;
|
|
22
22
|
if (!debugTokenName.startsWith("projects/")) {
|
|
@@ -27,7 +27,7 @@ exports.command = new command_1.Command("appcheck:debugtokens:list")
|
|
|
27
27
|
.option("--app <appId>", "the app id of your Firebase app")
|
|
28
28
|
.before(requireAuth_1.requireAuth)
|
|
29
29
|
.action(async (options) => {
|
|
30
|
-
const { appId } = await (0, prompts_1.getOrPromptProjectAndAppId)(options);
|
|
30
|
+
const { appId } = await (0, prompts_1.getOrPromptProjectAndAppId)(options, "Select the app to list debug tokens for:");
|
|
31
31
|
const projectNumber = await (0, projectUtils_1.needProjectNumber)(options);
|
|
32
32
|
const debugTokens = await (0, utils_1.promiseWithSpinner)(async () => await (0, api_1.listDebugTokens)(projectNumber, appId), `Listing App Check debug tokens for app ${clc.bold(appId)}`);
|
|
33
33
|
logDebugTokensList(debugTokens);
|
|
@@ -82,7 +82,7 @@ exports.AllIngressSettings = [
|
|
|
82
82
|
"ALLOW_INTERNAL_AND_GCLB",
|
|
83
83
|
];
|
|
84
84
|
async function resolveBackend(opts) {
|
|
85
|
-
const { paramValues: paramValues, secretRefs: secretRefs } = await params.resolveParams(opts.build.params, opts.firebaseConfig, envWithTypes(opts.build.params, opts.userEnvs), opts.nonInteractive, opts.
|
|
85
|
+
const { paramValues: paramValues, secretRefs: secretRefs } = await params.resolveParams(opts.build.params, opts.firebaseConfig, envWithTypes(opts.build.params, opts.userEnvs), opts.nonInteractive, opts.force, opts.isEmulator);
|
|
86
86
|
return { backend: toBackend(opts.build, paramValues), envs: paramValues, secretRefs: secretRefs };
|
|
87
87
|
}
|
|
88
88
|
function envWithTypes(definedParams, rawEnvs) {
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.endpointMatchesAnyFilter = endpointMatchesAnyFilter;
|
|
4
4
|
exports.endpointMatchesFilter = endpointMatchesFilter;
|
|
5
|
+
exports.getCodebasesFromConfig = getCodebasesFromConfig;
|
|
5
6
|
exports.parseFunctionSelector = parseFunctionSelector;
|
|
6
7
|
exports.getEndpointFilters = getEndpointFilters;
|
|
7
8
|
exports.getHumanFriendlyPlatformName = getHumanFriendlyPlatformName;
|
|
@@ -39,13 +40,24 @@ function endpointMatchesFilter(endpoint, filter) {
|
|
|
39
40
|
}
|
|
40
41
|
return true;
|
|
41
42
|
}
|
|
43
|
+
function getCodebasesFromConfig(config) {
|
|
44
|
+
return [
|
|
45
|
+
...new Set(config.flatMap((c) => ((0, projectConfig_1.isKitConfig)(c) ? Object.keys(c.instances) : [c.codebase]))),
|
|
46
|
+
];
|
|
47
|
+
}
|
|
42
48
|
function parseFunctionSelector(selector, config) {
|
|
43
49
|
const fragments = selector.split(":");
|
|
50
|
+
const target = fragments[0];
|
|
51
|
+
const codebaseNames = getCodebasesFromConfig(config);
|
|
52
|
+
if (codebaseNames.includes(target)) {
|
|
53
|
+
return [
|
|
54
|
+
{
|
|
55
|
+
codebase: target,
|
|
56
|
+
...(fragments.length > 1 ? { idChunks: fragments[1].split(/[-.]/) } : {}),
|
|
57
|
+
},
|
|
58
|
+
];
|
|
59
|
+
}
|
|
44
60
|
if (fragments.length < 2) {
|
|
45
|
-
const codebaseNames = config.map((c) => c.codebase);
|
|
46
|
-
if (codebaseNames.includes(fragments[0])) {
|
|
47
|
-
return [{ codebase: fragments[0] }];
|
|
48
|
-
}
|
|
49
61
|
return [{ codebase: projectConfig_1.DEFAULT_CODEBASE, idChunks: fragments[0].split(/[-.]/) }];
|
|
50
62
|
}
|
|
51
63
|
return [
|
|
@@ -94,7 +106,7 @@ function getFunctionLabel(fn) {
|
|
|
94
106
|
return id;
|
|
95
107
|
}
|
|
96
108
|
function targetCodebases(config, filters) {
|
|
97
|
-
const codebasesFromConfig =
|
|
109
|
+
const codebasesFromConfig = getCodebasesFromConfig(config);
|
|
98
110
|
if (!filters) {
|
|
99
111
|
return [...codebasesFromConfig];
|
|
100
112
|
}
|
|
@@ -104,13 +116,7 @@ function targetCodebases(config, filters) {
|
|
|
104
116
|
if (codebasesFromFilters.length === 0) {
|
|
105
117
|
return [...codebasesFromConfig];
|
|
106
118
|
}
|
|
107
|
-
|
|
108
|
-
for (const codebase of codebasesFromConfig) {
|
|
109
|
-
if (codebasesFromFilters.includes(codebase)) {
|
|
110
|
-
intersections.push(codebase);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return intersections;
|
|
119
|
+
return codebasesFromConfig.filter((codebase) => codebasesFromFilters.includes(codebase));
|
|
114
120
|
}
|
|
115
121
|
function groupEndpointsByCodebase(wantBackends, haveEndpoints) {
|
|
116
122
|
const grouped = {};
|
|
@@ -161,7 +161,12 @@ async function prepare(context, options, payload) {
|
|
|
161
161
|
context.firebaseConfig = firebaseConfig;
|
|
162
162
|
context.codebaseDeployEvents = {};
|
|
163
163
|
let runtimeConfig = { firebase: firebaseConfig };
|
|
164
|
-
const targetedCodebaseConfigs = context.config.filter((cfg) =>
|
|
164
|
+
const targetedCodebaseConfigs = context.config.filter((cfg) => {
|
|
165
|
+
if ((0, projectConfig_1.isKitConfig)(cfg)) {
|
|
166
|
+
return cfg.instances && Object.keys(cfg.instances).some((inst) => codebases.includes(inst));
|
|
167
|
+
}
|
|
168
|
+
return cfg.codebase && codebases.includes(cfg.codebase);
|
|
169
|
+
});
|
|
165
170
|
if (checkAPIsEnabled[1] && targetedCodebaseConfigs.some(projectConfig_1.shouldUseRuntimeConfig)) {
|
|
166
171
|
runtimeConfig = { ...runtimeConfig, ...(await (0, prepareFunctionsUpload_1.getFunctionsConfig)(projectId)) };
|
|
167
172
|
}
|
|
@@ -186,7 +191,12 @@ async function prepare(context, options, payload) {
|
|
|
186
191
|
projectId: projectId,
|
|
187
192
|
projectAlias: options.projectAlias,
|
|
188
193
|
};
|
|
189
|
-
|
|
194
|
+
if ((0, projectConfig_1.isKitConfig)(localCfg) && codebase in localCfg.instances) {
|
|
195
|
+
userEnvOpt.configDir = options.config.path(localCfg.instances[codebase]);
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
proto.convertIfPresent(userEnvOpt, localCfg, "configDir", (cd) => options.config.path(cd));
|
|
199
|
+
}
|
|
190
200
|
const rawUserEnvs = functionsEnv.loadUserEnvs(userEnvOpt);
|
|
191
201
|
const { userEnvs: userEnvs, secretRefs: secretRefs } = partitionUserEnvs(rawUserEnvs);
|
|
192
202
|
const envs = { ...userEnvs, ...firebaseEnvs };
|
|
@@ -498,7 +508,10 @@ async function loadCodebases(config, options, firebaseConfig, runtimeConfig, fil
|
|
|
498
508
|
GOOGLE_CLOUD_QUOTA_PROJECT: projectId,
|
|
499
509
|
});
|
|
500
510
|
discoveredBuild.runtime = codebaseConfig.runtime;
|
|
501
|
-
|
|
511
|
+
const prefix = (0, projectConfig_1.isKitConfig)(codebaseConfig)
|
|
512
|
+
? (0, projectConfig_1.addKitPrefix)(codebase)
|
|
513
|
+
: codebaseConfig.prefix || "";
|
|
514
|
+
build.applyPrefix(discoveredBuild, prefix);
|
|
502
515
|
wantBuilds[codebase] = discoveredBuild;
|
|
503
516
|
}
|
|
504
517
|
return wantBuilds;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.InlineExecutor = exports.QueueExecutor = exports.
|
|
3
|
+
exports.InlineExecutor = exports.QueueExecutor = exports.isCloudRunResourceExhausted = exports.isServiceAccount404 = exports.isTransientError = exports.isServiceUnavailable = exports.isConflict = exports.isQuotaExhaustion = void 0;
|
|
4
|
+
exports.parseErrorCode = parseErrorCode;
|
|
5
|
+
exports.hasErrorCode = hasErrorCode;
|
|
4
6
|
const queue_1 = require("../../../throttler/queue");
|
|
5
|
-
exports.DEFAULT_RETRY_CODES = [429, 409, 503];
|
|
6
7
|
function parseErrorCode(err) {
|
|
7
8
|
return (err.status ||
|
|
8
9
|
err.code ||
|
|
@@ -10,29 +11,71 @@ function parseErrorCode(err) {
|
|
|
10
11
|
err.original?.code ||
|
|
11
12
|
err.original?.context?.response?.statusCode);
|
|
12
13
|
}
|
|
14
|
+
function hasErrorCode(...codes) {
|
|
15
|
+
return (err) => codes.includes(parseErrorCode(err));
|
|
16
|
+
}
|
|
17
|
+
const isQuotaExhaustion = (err) => parseErrorCode(err) === 429;
|
|
18
|
+
exports.isQuotaExhaustion = isQuotaExhaustion;
|
|
19
|
+
const isConflict = (err) => parseErrorCode(err) === 409;
|
|
20
|
+
exports.isConflict = isConflict;
|
|
21
|
+
const isServiceUnavailable = (err) => parseErrorCode(err) === 503;
|
|
22
|
+
exports.isServiceUnavailable = isServiceUnavailable;
|
|
23
|
+
const isTransientError = (err) => (0, exports.isQuotaExhaustion)(err) || (0, exports.isConflict)(err) || (0, exports.isServiceUnavailable)(err);
|
|
24
|
+
exports.isTransientError = isTransientError;
|
|
25
|
+
const isServiceAccount404 = (err) => {
|
|
26
|
+
if (parseErrorCode(err) !== 404) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
let message = "";
|
|
30
|
+
try {
|
|
31
|
+
message = [
|
|
32
|
+
err?.message,
|
|
33
|
+
err?.original?.message,
|
|
34
|
+
err?.context?.body?.error?.message,
|
|
35
|
+
String(err),
|
|
36
|
+
]
|
|
37
|
+
.filter(Boolean)
|
|
38
|
+
.join(" ")
|
|
39
|
+
.toLowerCase();
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
message = String(err).toLowerCase();
|
|
43
|
+
}
|
|
44
|
+
return (message.includes("serviceaccount") ||
|
|
45
|
+
message.includes("service account") ||
|
|
46
|
+
/\b[a-z0-9._%+-]+@[a-z0-9.-]+\.gserviceaccount\.com\b/i.test(message) ||
|
|
47
|
+
/\bgserviceaccount\.com\b/i.test(message));
|
|
48
|
+
};
|
|
49
|
+
exports.isServiceAccount404 = isServiceAccount404;
|
|
50
|
+
const isCloudRunResourceExhausted = (err) => parseErrorCode(err) === 8;
|
|
51
|
+
exports.isCloudRunResourceExhausted = isCloudRunResourceExhausted;
|
|
13
52
|
async function handler(op) {
|
|
14
53
|
try {
|
|
15
54
|
op.result = await op.func();
|
|
16
55
|
}
|
|
17
56
|
catch (err) {
|
|
18
|
-
const
|
|
19
|
-
if (
|
|
57
|
+
const shouldRetry = op.retryPredicates.some((predicate) => predicate(err));
|
|
58
|
+
if (shouldRetry) {
|
|
20
59
|
throw err;
|
|
21
60
|
}
|
|
22
|
-
err.code =
|
|
61
|
+
err.code = parseErrorCode(err);
|
|
23
62
|
op.error = err;
|
|
24
63
|
}
|
|
25
64
|
return;
|
|
26
65
|
}
|
|
27
66
|
class QueueExecutor {
|
|
28
67
|
constructor(options) {
|
|
29
|
-
|
|
68
|
+
const { defaultRetryPredicates, ...throttlerOptions } = options;
|
|
69
|
+
this.defaultRetryPredicates = defaultRetryPredicates || [exports.isTransientError];
|
|
70
|
+
this.queue = new queue_1.Queue({ ...throttlerOptions, handler });
|
|
30
71
|
}
|
|
31
72
|
async run(func, opts) {
|
|
32
|
-
const
|
|
73
|
+
const retryPredicates = opts?.retryPredicates
|
|
74
|
+
? [...opts.retryPredicates]
|
|
75
|
+
: [...this.defaultRetryPredicates];
|
|
33
76
|
const op = {
|
|
34
77
|
func,
|
|
35
|
-
|
|
78
|
+
retryPredicates,
|
|
36
79
|
};
|
|
37
80
|
await this.queue.run(op);
|
|
38
81
|
if (op.error) {
|
|
@@ -66,11 +66,13 @@ class Fabricator {
|
|
|
66
66
|
this.projectId = args.projectId;
|
|
67
67
|
}
|
|
68
68
|
async grantNewRoles(plan, codebase) {
|
|
69
|
+
let createdSA = false;
|
|
69
70
|
if (plan.serviceAccountToCreate) {
|
|
70
71
|
utils.logLabeledBullet("functions", `Creating managed service account ${plan.serviceAccountToCreate}...`);
|
|
71
72
|
const saName = plan.serviceAccountToCreate.split("@")[0];
|
|
72
73
|
try {
|
|
73
74
|
await iam.createServiceAccount(this.projectId, saName, `Managed by Firebase CLI for codebase ${codebase}`, `Firebase Functions ${codebase}`);
|
|
75
|
+
createdSA = true;
|
|
74
76
|
}
|
|
75
77
|
catch (e) {
|
|
76
78
|
throw new error_1.FirebaseError("Cannot enable declarative security because you do not have permissions necessary to create the service account. Please ask an IAM administrator to perform the next deploy.", { original: e });
|
|
@@ -87,6 +89,14 @@ class Fabricator {
|
|
|
87
89
|
await resourcemanager.addServiceAccountRoles(this.projectId, plan.managedServiceAccount, plan.rolesToAdd, true);
|
|
88
90
|
}
|
|
89
91
|
catch (e) {
|
|
92
|
+
if (createdSA && plan.serviceAccountToCreate) {
|
|
93
|
+
try {
|
|
94
|
+
await iam.deleteServiceAccount(this.projectId, plan.serviceAccountToCreate);
|
|
95
|
+
}
|
|
96
|
+
catch (cleanupErr) {
|
|
97
|
+
logger_1.logger.debug("Failed to clean up newly created service account after role grant error", cleanupErr);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
90
100
|
throw new error_1.FirebaseError("The declarative security roles for this codebase have changed, but you do not have access to see what has changed. Please ask an IAM administrator to perform the next deploy.", { original: e });
|
|
91
101
|
}
|
|
92
102
|
}
|
|
@@ -154,6 +164,7 @@ class Fabricator {
|
|
|
154
164
|
logger_1.logger.debug("Fabricator.applyUpserts returned an unhandled exception.", JSON.stringify(r.reason, null, 2));
|
|
155
165
|
return acc;
|
|
156
166
|
}, []);
|
|
167
|
+
await this.cleanupUnusedServiceAccounts(plan, summary.results);
|
|
157
168
|
const hasFailures = summary.results.some((r) => r.error);
|
|
158
169
|
if (hasFailures) {
|
|
159
170
|
utils.logLabeledWarning("functions", "Deploys failed. Skipping deletes.");
|
|
@@ -183,6 +194,24 @@ class Fabricator {
|
|
|
183
194
|
summary.totalTime = timer.stop();
|
|
184
195
|
return summary;
|
|
185
196
|
}
|
|
197
|
+
async cleanupUnusedServiceAccounts(plan, results) {
|
|
198
|
+
for (const [codebase, codebasePlan] of Object.entries(plan)) {
|
|
199
|
+
if (!codebasePlan.serviceAccountToCreate) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const codebaseSuccesses = results.filter((r) => r.endpoint.codebase === codebase && !r.error);
|
|
203
|
+
if (codebaseSuccesses.length > 0) {
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
utils.logLabeledWarning("functions", `Cleaning up managed service account ${codebasePlan.serviceAccountToCreate} due to 100% deployment failure for codebase ${codebase}.`);
|
|
207
|
+
try {
|
|
208
|
+
await iam.deleteServiceAccount(this.projectId, codebasePlan.serviceAccountToCreate);
|
|
209
|
+
}
|
|
210
|
+
catch (e) {
|
|
211
|
+
logger_1.logger.debug(`Failed to delete managed service account ${codebasePlan.serviceAccountToCreate} during failure cleanup`, e);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
186
215
|
async applyUpserts(changes, scraperV1, scraperV2) {
|
|
187
216
|
const ops = [];
|
|
188
217
|
for (const endpoint of changes.endpointsToCreate) {
|
|
@@ -289,7 +318,7 @@ class Fabricator {
|
|
|
289
318
|
operationResourceName: op.name,
|
|
290
319
|
onPoll: scraper.poller,
|
|
291
320
|
});
|
|
292
|
-
})
|
|
321
|
+
}, { retryPredicates: [executor_1.isTransientError, executor_1.isServiceAccount404] })
|
|
293
322
|
.catch(rethrowAs(endpoint, "create"));
|
|
294
323
|
endpoint.uri = resultFunction?.httpsTrigger?.url;
|
|
295
324
|
if (backend.isHttpsTriggered(endpoint)) {
|
|
@@ -395,7 +424,7 @@ class Fabricator {
|
|
|
395
424
|
operationResourceName: op.name,
|
|
396
425
|
onPoll: scraper.poller,
|
|
397
426
|
});
|
|
398
|
-
})
|
|
427
|
+
}, { retryPredicates: [executor_1.isTransientError, executor_1.isServiceAccount404] })
|
|
399
428
|
.catch(async (err) => {
|
|
400
429
|
scraper.abort();
|
|
401
430
|
if (err.code === CLOUD_RUN_RESOURCE_EXHAUSTED_CODE) {
|
|
@@ -522,7 +551,7 @@ class Fabricator {
|
|
|
522
551
|
operationResourceName: op.name,
|
|
523
552
|
onPoll: scraper.poller,
|
|
524
553
|
});
|
|
525
|
-
}, {
|
|
554
|
+
}, { retryPredicates: [executor_1.isTransientError, executor_1.isCloudRunResourceExhausted, executor_1.isServiceAccount404] })
|
|
526
555
|
.catch((err) => {
|
|
527
556
|
scraper.abort();
|
|
528
557
|
logger_1.logger.error(err.message);
|
|
@@ -592,7 +621,7 @@ class Fabricator {
|
|
|
592
621
|
operationResourceName: op.name,
|
|
593
622
|
};
|
|
594
623
|
await poller.pollOperation(pollerOptions);
|
|
595
|
-
}, {
|
|
624
|
+
}, { retryPredicates: [executor_1.isTransientError, executor_1.isCloudRunResourceExhausted, executor_1.isServiceAccount404] })
|
|
596
625
|
.catch(rethrowAs(endpoint, "delete"));
|
|
597
626
|
}
|
|
598
627
|
async createRunFunction(endpoint) {
|
|
@@ -615,7 +644,7 @@ class Fabricator {
|
|
|
615
644
|
const op = await runV2.createService(endpoint.project, endpoint.region, endpoint.id, service);
|
|
616
645
|
endpoint.uri = op.uri;
|
|
617
646
|
endpoint.runServiceId = endpoint.id;
|
|
618
|
-
})
|
|
647
|
+
}, { retryPredicates: [executor_1.isTransientError, executor_1.isServiceAccount404] })
|
|
619
648
|
.catch(rethrowAs(endpoint, "create"));
|
|
620
649
|
const serviceName = `projects/${endpoint.project}/locations/${endpoint.region}/services/${endpoint.runServiceId}`;
|
|
621
650
|
if (backend.isHttpsTriggered(endpoint)) {
|
|
@@ -11,7 +11,6 @@ const clc = require("colorette");
|
|
|
11
11
|
const fs = require("fs");
|
|
12
12
|
const path = require("path");
|
|
13
13
|
const fsConfig = require("../firestore/fsConfig");
|
|
14
|
-
const proto = require("../gcp/proto");
|
|
15
14
|
const logger_1 = require("../logger");
|
|
16
15
|
const track_1 = require("../track");
|
|
17
16
|
const utils = require("../utils");
|
|
@@ -346,20 +345,40 @@ async function startAll(options, showUI = true, runningTestScript = false) {
|
|
|
346
345
|
if (runtime && !(0, supported_1.isRuntime)(runtime)) {
|
|
347
346
|
throw new error_1.FirebaseError(`Cannot load functions from ${functionsDir} because it has invalid runtime ${runtime}`);
|
|
348
347
|
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
348
|
+
if ((0, projectConfig_1.isKitConfig)(localCfg)) {
|
|
349
|
+
for (const [instanceId, configDir] of Object.entries(localCfg.instances)) {
|
|
350
|
+
const backend = {
|
|
351
|
+
functionsDir,
|
|
352
|
+
runtime,
|
|
353
|
+
codebase: instanceId,
|
|
354
|
+
prefix: (0, projectConfig_1.addKitPrefix)(instanceId),
|
|
355
|
+
configDir: path.join(projectDir, configDir),
|
|
356
|
+
env: {
|
|
357
|
+
...options.extDevEnv,
|
|
358
|
+
},
|
|
359
|
+
secretEnv: [],
|
|
360
|
+
predefinedTriggers: options.extDevTriggers,
|
|
361
|
+
ignore: localCfg.ignore,
|
|
362
|
+
};
|
|
363
|
+
emulatableBackends.push(backend);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
const backend = {
|
|
368
|
+
functionsDir,
|
|
369
|
+
runtime,
|
|
370
|
+
codebase: localCfg.codebase,
|
|
371
|
+
...(localCfg.prefix ? { prefix: localCfg.prefix } : {}),
|
|
372
|
+
...(localCfg.configDir ? { configDir: path.join(projectDir, localCfg.configDir) } : {}),
|
|
373
|
+
env: {
|
|
374
|
+
...options.extDevEnv,
|
|
375
|
+
},
|
|
376
|
+
secretEnv: [],
|
|
377
|
+
predefinedTriggers: options.extDevTriggers,
|
|
378
|
+
ignore: localCfg.ignore,
|
|
379
|
+
};
|
|
380
|
+
emulatableBackends.push(backend);
|
|
381
|
+
}
|
|
363
382
|
}
|
|
364
383
|
}
|
|
365
384
|
if (extensionEmulator) {
|
package/lib/experiments.js
CHANGED
|
@@ -78,6 +78,12 @@ exports.ALL_EXPERIMENTS = experiments({
|
|
|
78
78
|
public: true,
|
|
79
79
|
default: false,
|
|
80
80
|
},
|
|
81
|
+
kits: {
|
|
82
|
+
shortDescription: "Enable Functions Kits.",
|
|
83
|
+
fullDescription: "Adds support for Function Kits.",
|
|
84
|
+
public: false,
|
|
85
|
+
default: false,
|
|
86
|
+
},
|
|
81
87
|
emulatoruisnapshot: {
|
|
82
88
|
shortDescription: "Load pre-release versions of the emulator UI",
|
|
83
89
|
},
|
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_CODEBASE = void 0;
|
|
3
|
+
exports.KIT_PREFIX = exports.DEFAULT_CODEBASE = void 0;
|
|
4
4
|
exports.normalize = normalize;
|
|
5
5
|
exports.validateCodebase = validateCodebase;
|
|
6
|
+
exports.validateKit = validateKit;
|
|
6
7
|
exports.validatePrefix = validatePrefix;
|
|
7
8
|
exports.assertUnique = assertUnique;
|
|
9
|
+
exports.validateKitInstances = validateKitInstances;
|
|
8
10
|
exports.validate = validate;
|
|
9
11
|
exports.normalizeAndValidate = normalizeAndValidate;
|
|
10
12
|
exports.configForCodebase = configForCodebase;
|
|
11
13
|
exports.isLocalConfig = isLocalConfig;
|
|
12
14
|
exports.isRemoteConfig = isRemoteConfig;
|
|
15
|
+
exports.isKitConfig = isKitConfig;
|
|
16
|
+
exports.addKitPrefix = addKitPrefix;
|
|
13
17
|
exports.requireLocal = requireLocal;
|
|
14
18
|
exports.resolveConfigDir = resolveConfigDir;
|
|
15
19
|
exports.shouldUseRuntimeConfig = shouldUseRuntimeConfig;
|
|
16
20
|
const error_1 = require("../error");
|
|
21
|
+
const experiments = require("../experiments");
|
|
17
22
|
exports.DEFAULT_CODEBASE = "default";
|
|
18
23
|
function normalize(config) {
|
|
19
24
|
if (!config) {
|
|
@@ -33,6 +38,12 @@ function validateCodebase(codebase) {
|
|
|
33
38
|
"can contain only lowercase letters, numeric characters, underscores, and dashes.");
|
|
34
39
|
}
|
|
35
40
|
}
|
|
41
|
+
function validateKit(kit) {
|
|
42
|
+
if (kit.length === 0 || kit.length > 40 || !/^[a-z0-9_-]+$/.test(kit)) {
|
|
43
|
+
throw new error_1.FirebaseError("Invalid kit name. Kit name must be 40 characters or less and " +
|
|
44
|
+
"can contain only lowercase letters, numeric characters, underscores, and dashes.");
|
|
45
|
+
}
|
|
46
|
+
}
|
|
36
47
|
function validatePrefix(prefix) {
|
|
37
48
|
if (prefix.length > 30) {
|
|
38
49
|
throw new error_1.FirebaseError("Invalid prefix. Prefix must be 30 characters or less.");
|
|
@@ -42,6 +53,31 @@ function validatePrefix(prefix) {
|
|
|
42
53
|
}
|
|
43
54
|
}
|
|
44
55
|
function validateSingle(config) {
|
|
56
|
+
if ("kit" in config) {
|
|
57
|
+
experiments.assertEnabled("kits", "use functions kits");
|
|
58
|
+
if (!config.kit) {
|
|
59
|
+
throw new error_1.FirebaseError("Must specify 'kit' name in a functions kit config.");
|
|
60
|
+
}
|
|
61
|
+
validateKit(config.kit);
|
|
62
|
+
if (config.codebase) {
|
|
63
|
+
throw new error_1.FirebaseError("Cannot specify both 'kit' and 'codebase' in a single functions config.");
|
|
64
|
+
}
|
|
65
|
+
if (config.remoteSource) {
|
|
66
|
+
throw new error_1.FirebaseError("Cannot specify both 'kit' and 'remoteSource' in a single functions config.");
|
|
67
|
+
}
|
|
68
|
+
if (config.prefix) {
|
|
69
|
+
throw new error_1.FirebaseError("Cannot specify 'prefix' in a functions kit config.");
|
|
70
|
+
}
|
|
71
|
+
if (!config.source) {
|
|
72
|
+
throw new error_1.FirebaseError("Must specify 'source' in a functions kit config.");
|
|
73
|
+
}
|
|
74
|
+
if (!config.instances ||
|
|
75
|
+
typeof config.instances !== "object" ||
|
|
76
|
+
Array.isArray(config.instances)) {
|
|
77
|
+
throw new error_1.FirebaseError("Must specify 'instances' as an object mapping instance IDs to configuration paths in a functions kit config.");
|
|
78
|
+
}
|
|
79
|
+
return config;
|
|
80
|
+
}
|
|
45
81
|
const { source, remoteSource, runtime, codebase: providedCodebase, ...rest } = config;
|
|
46
82
|
if (source && remoteSource) {
|
|
47
83
|
throw new error_1.FirebaseError("Cannot specify both 'source' and 'remoteSource' in a single functions config. Please choose one.");
|
|
@@ -84,10 +120,12 @@ function assertUnique(config, property, propval) {
|
|
|
84
120
|
}
|
|
85
121
|
for (const single of config) {
|
|
86
122
|
const value = single[property];
|
|
87
|
-
if (
|
|
88
|
-
|
|
123
|
+
if (value !== undefined) {
|
|
124
|
+
if (values.has(value)) {
|
|
125
|
+
throw new error_1.FirebaseError(`functions.${String(property)} must be unique but '${String(value)}' was used more than once.`);
|
|
126
|
+
}
|
|
127
|
+
values.add(value);
|
|
89
128
|
}
|
|
90
|
-
values.add(value);
|
|
91
129
|
}
|
|
92
130
|
}
|
|
93
131
|
function assertUniqueSourcePrefixPair(config) {
|
|
@@ -113,38 +151,86 @@ function assertUniqueSourcePrefixPair(config) {
|
|
|
113
151
|
sourcePrefixPairs.add(key);
|
|
114
152
|
}
|
|
115
153
|
}
|
|
154
|
+
function validateKitInstances(instances, allProjectInstanceIds) {
|
|
155
|
+
for (const instanceId of Object.keys(instances)) {
|
|
156
|
+
if (instanceId.length === 0 ||
|
|
157
|
+
instanceId.length > 40 ||
|
|
158
|
+
!/^[a-z0-9_](?:[a-z0-9_-]*[a-z0-9_])?$/.test(instanceId)) {
|
|
159
|
+
throw new error_1.FirebaseError(`Invalid kit instance ID '${instanceId}'. Instance ID must be 40 characters or less, ` +
|
|
160
|
+
"can contain only lowercase letters, numeric characters, underscores, and dashes, and cannot start or end with a dash.");
|
|
161
|
+
}
|
|
162
|
+
if (allProjectInstanceIds.has(instanceId)) {
|
|
163
|
+
throw new error_1.FirebaseError(`functions kit instance ID must be unique across all kits, but '${instanceId}' was used more than once.`);
|
|
164
|
+
}
|
|
165
|
+
allProjectInstanceIds.add(instanceId);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function assertUniqueKitInstancesAndCodebases(config) {
|
|
169
|
+
const codebases = new Set();
|
|
170
|
+
const instanceIds = new Set();
|
|
171
|
+
for (const c of config) {
|
|
172
|
+
if ("codebase" in c && c.codebase) {
|
|
173
|
+
codebases.add(c.codebase);
|
|
174
|
+
}
|
|
175
|
+
if ("instances" in c && c.instances) {
|
|
176
|
+
validateKitInstances(c.instances, instanceIds);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
for (const instanceId of instanceIds) {
|
|
180
|
+
if (codebases.has(instanceId)) {
|
|
181
|
+
throw new error_1.FirebaseError(`functions codebase name and kit instance ID must be mutually exclusive, but '${instanceId}' was used as both a codebase name and a kit instance ID.`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
116
185
|
function validate(config) {
|
|
117
186
|
const validated = config.map((cfg) => validateSingle(cfg));
|
|
118
187
|
assertUnique(validated, "codebase");
|
|
188
|
+
assertUnique(validated, "kit");
|
|
119
189
|
assertUniqueSourcePrefixPair(validated);
|
|
190
|
+
assertUniqueKitInstancesAndCodebases(validated);
|
|
120
191
|
return validated;
|
|
121
192
|
}
|
|
122
193
|
function normalizeAndValidate(config) {
|
|
123
194
|
return validate(normalize(config));
|
|
124
195
|
}
|
|
125
|
-
function configForCodebase(config,
|
|
126
|
-
const codebaseCfg = config.find((c) =>
|
|
196
|
+
function configForCodebase(config, codebaseOrInstance) {
|
|
197
|
+
const codebaseCfg = config.find((c) => {
|
|
198
|
+
if (isKitConfig(c)) {
|
|
199
|
+
return c.instances && codebaseOrInstance in c.instances;
|
|
200
|
+
}
|
|
201
|
+
return c.codebase === codebaseOrInstance;
|
|
202
|
+
});
|
|
127
203
|
if (!codebaseCfg) {
|
|
128
|
-
throw new error_1.FirebaseError(`No functions config found for codebase ${
|
|
204
|
+
throw new error_1.FirebaseError(`No functions config found for codebase or kit instance ${codebaseOrInstance}`);
|
|
129
205
|
}
|
|
130
206
|
return codebaseCfg;
|
|
131
207
|
}
|
|
132
208
|
function isLocalConfig(c) {
|
|
133
|
-
return
|
|
209
|
+
return "source" in c && !("kit" in c);
|
|
134
210
|
}
|
|
135
211
|
function isRemoteConfig(c) {
|
|
136
|
-
return
|
|
212
|
+
return "remoteSource" in c;
|
|
213
|
+
}
|
|
214
|
+
function isKitConfig(c) {
|
|
215
|
+
return "kit" in c;
|
|
216
|
+
}
|
|
217
|
+
exports.KIT_PREFIX = "kit-";
|
|
218
|
+
function addKitPrefix(instanceId) {
|
|
219
|
+
return `${exports.KIT_PREFIX}${instanceId}`;
|
|
137
220
|
}
|
|
138
221
|
function requireLocal(c, purpose) {
|
|
139
|
-
if (
|
|
222
|
+
if (isRemoteConfig(c)) {
|
|
140
223
|
const msg = purpose ??
|
|
141
224
|
"This operation requires a local functions source directory, but the codebase is configured with a remote source.";
|
|
142
225
|
throw new error_1.FirebaseError(msg);
|
|
143
226
|
}
|
|
144
227
|
return c;
|
|
145
228
|
}
|
|
146
|
-
function resolveConfigDir(c) {
|
|
147
|
-
|
|
229
|
+
function resolveConfigDir(c, instanceId) {
|
|
230
|
+
if (isKitConfig(c)) {
|
|
231
|
+
return instanceId ? c.instances[instanceId] : undefined;
|
|
232
|
+
}
|
|
233
|
+
return c.configDir || (isLocalConfig(c) ? c.source : undefined);
|
|
148
234
|
}
|
|
149
235
|
function shouldUseRuntimeConfig(cfg) {
|
|
150
236
|
return isLocalConfig(cfg) && cfg.disallowLegacyRuntimeConfig !== true;
|
package/lib/serve/functions.js
CHANGED
|
@@ -23,13 +23,31 @@ class FunctionsServer {
|
|
|
23
23
|
for (const cfg of config) {
|
|
24
24
|
const localCfg = projectConfig.requireLocal(cfg, "Remote sources are not supported in the Functions emulator.");
|
|
25
25
|
const functionsDir = path.join(options.config.projectDir, localCfg.source);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
if (projectConfig.isKitConfig(localCfg)) {
|
|
27
|
+
for (const [instanceId, configDir] of Object.entries(localCfg.instances)) {
|
|
28
|
+
backends.push({
|
|
29
|
+
functionsDir,
|
|
30
|
+
codebase: instanceId,
|
|
31
|
+
prefix: projectConfig.addKitPrefix(instanceId),
|
|
32
|
+
configDir: path.join(options.config.projectDir, configDir),
|
|
33
|
+
runtime: localCfg.runtime,
|
|
34
|
+
env: {},
|
|
35
|
+
secretEnv: [],
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
backends.push({
|
|
41
|
+
functionsDir,
|
|
42
|
+
codebase: localCfg.codebase,
|
|
43
|
+
...(localCfg.configDir
|
|
44
|
+
? { configDir: path.join(options.config.projectDir, localCfg.configDir) }
|
|
45
|
+
: {}),
|
|
46
|
+
runtime: localCfg.runtime,
|
|
47
|
+
env: {},
|
|
48
|
+
secretEnv: [],
|
|
49
|
+
});
|
|
50
|
+
}
|
|
33
51
|
}
|
|
34
52
|
this.backends = backends;
|
|
35
53
|
const account = (0, auth_1.getProjectDefaultAccount)(options.config.projectDir);
|