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.

59 lines
2.1 KiB

2 months ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.pollOperation = exports.OperationPoller = void 0;
  4. const apiv2_1 = require("./apiv2");
  5. const error_1 = require("./error");
  6. const queue_1 = require("./throttler/queue");
  7. const DEFAULT_INITIAL_BACKOFF_DELAY_MILLIS = 250;
  8. const DEFAULT_MASTER_TIMEOUT_MILLIS = 30000;
  9. class OperationPoller {
  10. async poll(options) {
  11. const queue = new queue_1.Queue({
  12. name: options.pollerName || "LRO Poller",
  13. concurrency: 1,
  14. retries: Number.MAX_SAFE_INTEGER,
  15. maxBackoff: options.maxBackoff,
  16. backoff: options.backoff || DEFAULT_INITIAL_BACKOFF_DELAY_MILLIS,
  17. });
  18. const masterTimeout = options.masterTimeout || DEFAULT_MASTER_TIMEOUT_MILLIS;
  19. const { response, error } = await queue.run(this.getPollingTask(options), masterTimeout);
  20. queue.close();
  21. if (error) {
  22. throw error instanceof error_1.FirebaseError
  23. ? error
  24. : new error_1.FirebaseError(error.message, { status: error.code });
  25. }
  26. return response;
  27. }
  28. getPollingTask(options) {
  29. const apiClient = new apiv2_1.Client({
  30. urlPrefix: options.apiOrigin,
  31. apiVersion: options.apiVersion,
  32. auth: true,
  33. });
  34. return async () => {
  35. let res;
  36. try {
  37. res = await apiClient.get(options.operationResourceName);
  38. }
  39. catch (err) {
  40. if (err.status === 500 || err.status === 503) {
  41. throw err;
  42. }
  43. return { error: err };
  44. }
  45. if (options.onPoll) {
  46. options.onPoll(res.body);
  47. }
  48. if (!res.body.done) {
  49. throw new Error("Polling incomplete, should trigger retry with backoff");
  50. }
  51. return res.body;
  52. };
  53. }
  54. }
  55. exports.OperationPoller = OperationPoller;
  56. function pollOperation(options) {
  57. return new OperationPoller().poll(options);
  58. }
  59. exports.pollOperation = pollOperation;