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.

adminSdkConfig.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getProjectAdminSdkConfigOrCached = exports.constructDefaultAdminSdkConfig = void 0;
  4. const api_1 = require("../api");
  5. const apiv2 = require("../apiv2");
  6. const configstore_1 = require("../configstore");
  7. const error_1 = require("../error");
  8. const logger_1 = require("../logger");
  9. const constants_1 = require("./constants");
  10. const _CONFIGSTORE_KEY = "adminsdkconfig";
  11. function constructDefaultAdminSdkConfig(projectId) {
  12. return {
  13. projectId: projectId,
  14. databaseURL: process.env.DATABASE_URL || `https://${projectId}.firebaseio.com`,
  15. storageBucket: process.env.STORAGE_BUCKET_URL || `${projectId}.appspot.com`,
  16. };
  17. }
  18. exports.constructDefaultAdminSdkConfig = constructDefaultAdminSdkConfig;
  19. async function getProjectAdminSdkConfigOrCached(projectId) {
  20. if (constants_1.Constants.isDemoProject(projectId)) {
  21. return constructDefaultAdminSdkConfig(projectId);
  22. }
  23. try {
  24. const config = await getProjectAdminSdkConfig(projectId);
  25. setCacheAdminSdkConfig(projectId, config);
  26. return config;
  27. }
  28. catch (e) {
  29. logger_1.logger.debug(`Failed to get Admin SDK config for ${projectId}, falling back to cache`, e);
  30. return getCachedAdminSdkConfig(projectId);
  31. }
  32. }
  33. exports.getProjectAdminSdkConfigOrCached = getProjectAdminSdkConfigOrCached;
  34. async function getProjectAdminSdkConfig(projectId) {
  35. const apiClient = new apiv2.Client({
  36. auth: true,
  37. apiVersion: "v1beta1",
  38. urlPrefix: api_1.firebaseApiOrigin,
  39. });
  40. try {
  41. const res = await apiClient.get(`projects/${projectId}/adminSdkConfig`);
  42. return res.body;
  43. }
  44. catch (err) {
  45. throw new error_1.FirebaseError(`Failed to get Admin SDK for Firebase project ${projectId}. ` +
  46. "Please make sure the project exists and your account has permission to access it.", { exit: 2, original: err });
  47. }
  48. }
  49. function setCacheAdminSdkConfig(projectId, config) {
  50. const allConfigs = configstore_1.configstore.get(_CONFIGSTORE_KEY) || {};
  51. allConfigs[projectId] = config;
  52. configstore_1.configstore.set(_CONFIGSTORE_KEY, allConfigs);
  53. }
  54. function getCachedAdminSdkConfig(projectId) {
  55. const allConfigs = configstore_1.configstore.get(_CONFIGSTORE_KEY) || {};
  56. return allConfigs[projectId];
  57. }