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.

sourceTokenScraper.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SourceTokenScraper = void 0;
  4. const error_1 = require("../../../error");
  5. const functional_1 = require("../../../functional");
  6. const logger_1 = require("../../../logger");
  7. class SourceTokenScraper {
  8. constructor(validDurationMs = 1500000) {
  9. this.tokenValidDurationMs = validDurationMs;
  10. this.promise = new Promise((resolve) => (this.resolve = resolve));
  11. this.fetchState = "NONE";
  12. }
  13. async getToken() {
  14. if (this.fetchState === "NONE") {
  15. this.fetchState = "FETCHING";
  16. return undefined;
  17. }
  18. else if (this.fetchState === "FETCHING") {
  19. return this.promise;
  20. }
  21. else if (this.fetchState === "VALID") {
  22. if (this.isTokenExpired()) {
  23. this.fetchState = "FETCHING";
  24. this.promise = new Promise((resolve) => (this.resolve = resolve));
  25. return undefined;
  26. }
  27. return this.promise;
  28. }
  29. else {
  30. (0, functional_1.assertExhaustive)(this.fetchState);
  31. }
  32. }
  33. isTokenExpired() {
  34. if (this.expiry === undefined) {
  35. throw new error_1.FirebaseError("Your deployment is checking the expiration of a source token that has not yet been polled. " +
  36. "Hitting this case should never happen and should be considered a bug. " +
  37. "Please file an issue at https://github.com/firebase/firebase-tools/issues " +
  38. "and try deploying your functions again.");
  39. }
  40. return Date.now() >= this.expiry;
  41. }
  42. get poller() {
  43. return (op) => {
  44. var _a, _b, _c, _d, _e;
  45. if (((_a = op.metadata) === null || _a === void 0 ? void 0 : _a.sourceToken) || op.done) {
  46. const [, , , region] = ((_c = (_b = op.metadata) === null || _b === void 0 ? void 0 : _b.target) === null || _c === void 0 ? void 0 : _c.split("/")) || [];
  47. logger_1.logger.debug(`Got source token ${(_d = op.metadata) === null || _d === void 0 ? void 0 : _d.sourceToken} for region ${region}`);
  48. this.resolve((_e = op.metadata) === null || _e === void 0 ? void 0 : _e.sourceToken);
  49. this.fetchState = "VALID";
  50. this.expiry = Date.now() + this.tokenValidDurationMs;
  51. }
  52. };
  53. }
  54. }
  55. exports.SourceTokenScraper = SourceTokenScraper;