Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

functionsConfig.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.parseUnsetArgs = exports.parseSetArgs = exports.materializeAll = exports.materializeConfig = exports.setVariablesRecursive = exports.getFirebaseConfig = exports.getAppEngineLocation = exports.idsToVarName = exports.varNameToIds = exports.ensureApi = exports.RESERVED_NAMESPACES = void 0;
  4. const _ = require("lodash");
  5. const clc = require("colorette");
  6. const api_1 = require("./api");
  7. const apiv2_1 = require("./apiv2");
  8. const ensureApiEnabled_1 = require("./ensureApiEnabled");
  9. const error_1 = require("./error");
  10. const projectUtils_1 = require("./projectUtils");
  11. const runtimeconfig = require("./gcp/runtimeconfig");
  12. exports.RESERVED_NAMESPACES = ["firebase"];
  13. const apiClient = new apiv2_1.Client({ urlPrefix: api_1.firebaseApiOrigin });
  14. function keyToIds(key) {
  15. const keyParts = key.split(".");
  16. const variable = keyParts.slice(1).join("/");
  17. return {
  18. config: keyParts[0],
  19. variable: variable,
  20. };
  21. }
  22. function setVariable(projectId, configId, varPath, val) {
  23. if (configId === "" || varPath === "") {
  24. const msg = "Invalid argument, each config value must have a 2-part key (e.g. foo.bar).";
  25. throw new error_1.FirebaseError(msg);
  26. }
  27. return runtimeconfig.variables.set(projectId, configId, varPath, val);
  28. }
  29. function isReservedNamespace(id) {
  30. return exports.RESERVED_NAMESPACES.some((reserved) => {
  31. return id.config.toLowerCase().startsWith(reserved);
  32. });
  33. }
  34. async function ensureApi(options) {
  35. const projectId = (0, projectUtils_1.needProjectId)(options);
  36. return (0, ensureApiEnabled_1.ensure)(projectId, "runtimeconfig.googleapis.com", "runtimeconfig", true);
  37. }
  38. exports.ensureApi = ensureApi;
  39. function varNameToIds(varName) {
  40. return {
  41. config: varName.match(new RegExp("/configs/(.+)/variables/"))[1],
  42. variable: varName.match(new RegExp("/variables/(.+)"))[1],
  43. };
  44. }
  45. exports.varNameToIds = varNameToIds;
  46. function idsToVarName(projectId, configId, varId) {
  47. return ["projects", projectId, "configs", configId, "variables", varId].join("/");
  48. }
  49. exports.idsToVarName = idsToVarName;
  50. function getAppEngineLocation(config) {
  51. let appEngineLocation = config.locationId;
  52. if (appEngineLocation && appEngineLocation.match(/[^\d]$/)) {
  53. appEngineLocation = appEngineLocation + "1";
  54. }
  55. return appEngineLocation || "us-central1";
  56. }
  57. exports.getAppEngineLocation = getAppEngineLocation;
  58. async function getFirebaseConfig(options) {
  59. const projectId = (0, projectUtils_1.needProjectId)(options);
  60. const response = await apiClient.get(`/v1beta1/projects/${projectId}/adminSdkConfig`);
  61. return response.body;
  62. }
  63. exports.getFirebaseConfig = getFirebaseConfig;
  64. async function setVariablesRecursive(projectId, configId, varPath, val) {
  65. let parsed = val;
  66. if (typeof val === "string") {
  67. try {
  68. parsed = JSON.parse(val);
  69. }
  70. catch (e) {
  71. }
  72. }
  73. if (typeof parsed === "object" && parsed !== null) {
  74. return Promise.all(Object.entries(parsed).map(([key, item]) => {
  75. const newVarPath = varPath ? [varPath, key].join("/") : key;
  76. return setVariablesRecursive(projectId, configId, newVarPath, item);
  77. }));
  78. }
  79. return setVariable(projectId, configId, varPath, val);
  80. }
  81. exports.setVariablesRecursive = setVariablesRecursive;
  82. async function materializeConfig(configName, output) {
  83. const materializeVariable = async function (varName) {
  84. const variable = await runtimeconfig.variables.get(varName);
  85. const id = exports.varNameToIds(variable.name);
  86. const key = id.config + "." + id.variable.split("/").join(".");
  87. _.set(output, key, variable.text);
  88. };
  89. const traverseVariables = async function (variables) {
  90. return Promise.all(variables.map((variable) => {
  91. return materializeVariable(variable.name);
  92. }));
  93. };
  94. const variables = await runtimeconfig.variables.list(configName);
  95. await traverseVariables(variables);
  96. return output;
  97. }
  98. exports.materializeConfig = materializeConfig;
  99. async function materializeAll(projectId) {
  100. const output = {};
  101. const configs = await runtimeconfig.configs.list(projectId);
  102. if (!Array.isArray(configs) || !configs.length) {
  103. return output;
  104. }
  105. await Promise.all(configs.map((config) => {
  106. if (config.name.match(new RegExp("configs/firebase"))) {
  107. return;
  108. }
  109. return exports.materializeConfig(config.name, output);
  110. }));
  111. return output;
  112. }
  113. exports.materializeAll = materializeAll;
  114. function parseSetArgs(args) {
  115. const parsed = [];
  116. for (const arg of args) {
  117. const parts = arg.split("=");
  118. const key = parts[0];
  119. if (parts.length < 2) {
  120. throw new error_1.FirebaseError("Invalid argument " + clc.bold(arg) + ", must be in key=val format");
  121. }
  122. if (/[A-Z]/.test(key)) {
  123. throw new error_1.FirebaseError("Invalid config name " + clc.bold(key) + ", cannot use upper case.");
  124. }
  125. const id = keyToIds(key);
  126. if (isReservedNamespace(id)) {
  127. throw new error_1.FirebaseError("Cannot set to reserved namespace " + clc.bold(id.config));
  128. }
  129. const val = parts.slice(1).join("=");
  130. parsed.push({
  131. configId: id.config,
  132. varId: id.variable,
  133. val: val,
  134. });
  135. }
  136. return parsed;
  137. }
  138. exports.parseSetArgs = parseSetArgs;
  139. function parseUnsetArgs(args) {
  140. const parsed = [];
  141. let splitArgs = [];
  142. for (const arg of args) {
  143. splitArgs = Array.from(new Set([...splitArgs, ...arg.split(",")]));
  144. }
  145. for (const key of splitArgs) {
  146. const id = keyToIds(key);
  147. if (isReservedNamespace(id)) {
  148. throw new error_1.FirebaseError("Cannot unset reserved namespace " + clc.bold(id.config));
  149. }
  150. parsed.push({
  151. configId: id.config,
  152. varId: id.variable,
  153. });
  154. }
  155. return parsed;
  156. }
  157. exports.parseUnsetArgs = parseUnsetArgs;