No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

convertConfig.js 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.convertConfig = exports.findEndpointForRewrite = void 0;
  4. const error_1 = require("../../error");
  5. const backend = require("../functions/backend");
  6. const utils_1 = require("../../utils");
  7. const proto = require("../../gcp/proto");
  8. const colorette_1 = require("colorette");
  9. const runTags = require("../../hosting/runTags");
  10. const functional_1 = require("../../functional");
  11. const experiments = require("../../experiments");
  12. const logger_1 = require("../../logger");
  13. function extractPattern(type, source) {
  14. let glob;
  15. let regex;
  16. if ("source" in source) {
  17. glob = source.source;
  18. }
  19. if ("glob" in source) {
  20. glob = source.glob;
  21. }
  22. if ("regex" in source) {
  23. regex = source.regex;
  24. }
  25. if (glob && regex) {
  26. throw new error_1.FirebaseError(`Cannot specify a ${type} pattern with both a glob and regex.`);
  27. }
  28. else if (glob) {
  29. return { glob };
  30. }
  31. else if (regex) {
  32. return { regex };
  33. }
  34. throw new error_1.FirebaseError(`Cannot specify a ${type} with no pattern (either a glob or regex required).`);
  35. }
  36. function findEndpointForRewrite(site, targetBackend, id, region) {
  37. const endpoints = backend.allEndpoints(targetBackend).filter((e) => e.id === id);
  38. if (endpoints.length === 0) {
  39. return { matchingEndpoint: undefined, foundMatchingId: false };
  40. }
  41. if (endpoints.length === 1) {
  42. if (region && region !== endpoints[0].region) {
  43. return { matchingEndpoint: undefined, foundMatchingId: true };
  44. }
  45. return { matchingEndpoint: endpoints[0], foundMatchingId: true };
  46. }
  47. if (!region) {
  48. const us = endpoints.find((e) => e.region === "us-central1");
  49. if (!us) {
  50. throw new error_1.FirebaseError(`More than one backend found for function name: ${id}. If the function is deployed in multiple regions, you must specify a region.`);
  51. }
  52. (0, utils_1.logLabeledBullet)(`hosting[${site}]`, `Function \`${id}\` found in multiple regions, defaulting to \`us-central1\`. ` +
  53. `To rewrite to a different region, specify a \`region\` for the rewrite in \`firebase.json\`.`);
  54. return { matchingEndpoint: us, foundMatchingId: true };
  55. }
  56. return {
  57. matchingEndpoint: endpoints.find((e) => e.region === region),
  58. foundMatchingId: true,
  59. };
  60. }
  61. exports.findEndpointForRewrite = findEndpointForRewrite;
  62. async function convertConfig(context, functionsPayload, deploy) {
  63. var _a, _b, _c, _d;
  64. const config = {};
  65. const hasBackends = !!((_a = deploy.config.rewrites) === null || _a === void 0 ? void 0 : _a.some((r) => "function" in r || "run" in r));
  66. const wantBackend = backend.merge(...Object.values(functionsPayload.functions || {}).map((c) => c.wantBackend));
  67. let haveBackend = backend.empty();
  68. if (hasBackends) {
  69. try {
  70. haveBackend = await backend.existingBackend(context);
  71. }
  72. catch (err) {
  73. if (err instanceof error_1.FirebaseError) {
  74. if (err.status === 403) {
  75. logger_1.logger.debug(`Deploying hosting site ${deploy.config.site}, did not have permissions to check for backends: `, err);
  76. }
  77. }
  78. else {
  79. throw err;
  80. }
  81. }
  82. }
  83. config.rewrites = (_b = deploy.config.rewrites) === null || _b === void 0 ? void 0 : _b.map((rewrite) => {
  84. const target = extractPattern("rewrite", rewrite);
  85. if ("destination" in rewrite) {
  86. return Object.assign(Object.assign({}, target), { path: rewrite.destination });
  87. }
  88. if ("function" in rewrite) {
  89. if (typeof rewrite.function === "string") {
  90. throw new error_1.FirebaseError("Expected firebase config to be normalized, but got legacy functions format");
  91. }
  92. const id = rewrite.function.functionId;
  93. const region = rewrite.function.region;
  94. const deployingEndpointSearch = findEndpointForRewrite(deploy.config.site, wantBackend, id, region);
  95. const existingEndpointSearch = !deployingEndpointSearch.foundMatchingId && !deployingEndpointSearch.matchingEndpoint
  96. ? findEndpointForRewrite(deploy.config.site, haveBackend, id, region)
  97. : undefined;
  98. const endpoint = deployingEndpointSearch.matchingEndpoint
  99. ? deployingEndpointSearch.matchingEndpoint
  100. : existingEndpointSearch === null || existingEndpointSearch === void 0 ? void 0 : existingEndpointSearch.matchingEndpoint;
  101. if (!endpoint) {
  102. if (deployingEndpointSearch.foundMatchingId || (existingEndpointSearch === null || existingEndpointSearch === void 0 ? void 0 : existingEndpointSearch.foundMatchingId)) {
  103. throw new error_1.FirebaseError(`Unable to find a valid endpoint for function. Functions matching the rewrite
  104. are present but in the wrong region.`);
  105. }
  106. (0, utils_1.logLabeledWarning)(`hosting[${deploy.config.site}]`, `Unable to find a valid endpoint for function \`${id}\`, but still including it in the config`);
  107. const apiRewrite = Object.assign(Object.assign({}, target), { function: id });
  108. if (region) {
  109. apiRewrite.functionRegion = region;
  110. }
  111. return apiRewrite;
  112. }
  113. if (endpoint.platform === "gcfv1") {
  114. if (!backend.isHttpsTriggered(endpoint) && !backend.isCallableTriggered(endpoint)) {
  115. throw new error_1.FirebaseError(`Function ${endpoint.id} is a gen 1 function and therefore must be an https function type`);
  116. }
  117. if (rewrite.function.pinTag) {
  118. throw new error_1.FirebaseError(`Function ${endpoint.id} is a gen 1 function and therefore does not support the ${(0, colorette_1.bold)("pinTag")} option`);
  119. }
  120. return Object.assign(Object.assign({}, target), { function: endpoint.id, functionRegion: endpoint.region });
  121. }
  122. const apiRewrite = Object.assign(Object.assign({}, target), { run: {
  123. serviceId: endpoint.id,
  124. region: endpoint.region,
  125. } });
  126. if (rewrite.function.pinTag) {
  127. experiments.assertEnabled("pintags", "pin a function version");
  128. apiRewrite.run.tag = runTags.TODO_TAG_NAME;
  129. }
  130. return apiRewrite;
  131. }
  132. if ("dynamicLinks" in rewrite) {
  133. if (!rewrite.dynamicLinks) {
  134. throw new error_1.FirebaseError("Can only set dynamicLinks to true in a rewrite");
  135. }
  136. return Object.assign(Object.assign({}, target), { dynamicLinks: true });
  137. }
  138. if ("run" in rewrite) {
  139. const apiRewrite = Object.assign(Object.assign({}, target), { run: Object.assign({ region: "us-central1" }, rewrite.run) });
  140. if (apiRewrite.run.tag) {
  141. experiments.assertEnabled("pintags", "pin to a run service revision");
  142. }
  143. return apiRewrite;
  144. }
  145. (0, functional_1.assertExhaustive)(rewrite);
  146. });
  147. if (config.rewrites) {
  148. const versionId = (0, utils_1.last)(deploy.version.split("/"));
  149. await runTags.setRewriteTags(config.rewrites, context.projectId, versionId);
  150. }
  151. config.redirects = (_c = deploy.config.redirects) === null || _c === void 0 ? void 0 : _c.map((redirect) => {
  152. const apiRedirect = Object.assign(Object.assign({}, extractPattern("redirect", redirect)), { location: redirect.destination });
  153. if (redirect.type) {
  154. apiRedirect.statusCode = redirect.type;
  155. }
  156. return apiRedirect;
  157. });
  158. config.headers = (_d = deploy.config.headers) === null || _d === void 0 ? void 0 : _d.map((header) => {
  159. const headers = {};
  160. for (const { key, value } of header.headers || []) {
  161. headers[key] = value;
  162. }
  163. return Object.assign(Object.assign({}, extractPattern("header", header)), { headers });
  164. });
  165. proto.copyIfPresent(config, deploy.config, "cleanUrls", "appAssociation", "i18n");
  166. proto.convertIfPresent(config, deploy.config, "trailingSlashBehavior", "trailingSlash", (b) => b ? "ADD" : "REMOVE");
  167. proto.pruneUndefiends(config);
  168. return config;
  169. }
  170. exports.convertConfig = convertConfig;