GSI - Employe Self Service Mobile
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.

52 lines
2.3 KiB

2 months ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const apiv2_1 = require("../apiv2");
  4. const api_1 = require("../api");
  5. const proxy_1 = require("./proxy");
  6. const error_1 = require("../error");
  7. const logger_1 = require("../logger");
  8. const projectUtils_1 = require("../projectUtils");
  9. const cloudRunCache = {};
  10. const apiClient = new apiv2_1.Client({ urlPrefix: api_1.cloudRunApiOrigin, apiVersion: "v1" });
  11. async function getCloudRunUrl(rewrite, projectId) {
  12. var _a;
  13. const alreadyFetched = cloudRunCache[`${rewrite.run.region}/${rewrite.run.serviceId}`];
  14. if (alreadyFetched) {
  15. return Promise.resolve(alreadyFetched);
  16. }
  17. const path = `/projects/${projectId}/locations/${rewrite.run.region || "us-central1"}/services/${rewrite.run.serviceId}`;
  18. try {
  19. logger_1.logger.info(`[hosting] Looking up Cloud Run service "${path}" for its URL`);
  20. const res = await apiClient.get(path);
  21. const url = (_a = res.body.status) === null || _a === void 0 ? void 0 : _a.url;
  22. if (!url) {
  23. throw new error_1.FirebaseError("Cloud Run URL doesn't exist in response.");
  24. }
  25. cloudRunCache[`${rewrite.run.region}/${rewrite.run.serviceId}`] = url;
  26. return url;
  27. }
  28. catch (err) {
  29. throw new error_1.FirebaseError(`Error looking up URL for Cloud Run service: ${err}`, {
  30. original: err,
  31. });
  32. }
  33. }
  34. function default_1(options) {
  35. return async (rewrite) => {
  36. if (!rewrite.run) {
  37. return (0, proxy_1.errorRequestHandler)('Cloud Run rewrites must have a valid "run" field.');
  38. }
  39. if (!rewrite.run.serviceId) {
  40. return (0, proxy_1.errorRequestHandler)("Cloud Run rewrites must supply a service ID.");
  41. }
  42. if (!rewrite.run.region) {
  43. rewrite.run.region = "us-central1";
  44. }
  45. logger_1.logger.info(`[hosting] Cloud Run rewrite ${JSON.stringify(rewrite)} triggered`);
  46. const textIdentifier = `Cloud Run service "${rewrite.run.serviceId}" for region "${rewrite.run.region}"`;
  47. return getCloudRunUrl(rewrite, (0, projectUtils_1.needProjectId)(options))
  48. .then((url) => (0, proxy_1.proxyRequestHandler)(url, textIdentifier))
  49. .catch(proxy_1.errorRequestHandler);
  50. };
  51. }
  52. exports.default = default_1;