progress.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var progress_exports = {};
  20. __export(progress_exports, {
  21. ProgressController: () => ProgressController,
  22. isAbortError: () => isAbortError,
  23. raceUncancellableOperationWithCleanup: () => raceUncancellableOperationWithCleanup
  24. });
  25. module.exports = __toCommonJS(progress_exports);
  26. var import_errors = require("./errors");
  27. var import_utils = require("../utils");
  28. var import_manualPromise = require("../utils/isomorphic/manualPromise");
  29. class ProgressController {
  30. constructor(metadata, onCallLog) {
  31. this._forceAbortPromise = new import_manualPromise.ManualPromise();
  32. this._donePromise = new import_manualPromise.ManualPromise();
  33. this._state = "before";
  34. this.metadata = metadata || { id: "", startTime: 0, endTime: 0, type: "Internal", method: "", params: {}, log: [], internal: true };
  35. this._onCallLog = onCallLog;
  36. this._forceAbortPromise.catch((e) => null);
  37. this._controller = new AbortController();
  38. }
  39. static createForSdkObject(sdkObject, callMetadata) {
  40. const logName = sdkObject.logName || "api";
  41. return new ProgressController(callMetadata, (message) => {
  42. import_utils.debugLogger.log(logName, message);
  43. sdkObject.instrumentation.onCallLog(sdkObject, callMetadata, logName, message);
  44. });
  45. }
  46. async abort(error) {
  47. if (this._state === "running") {
  48. error[kAbortErrorSymbol] = true;
  49. this._state = { error };
  50. this._forceAbortPromise.reject(error);
  51. this._controller.abort(error);
  52. }
  53. await this._donePromise;
  54. }
  55. async run(task, timeout) {
  56. const deadline = timeout ? (0, import_utils.monotonicTime)() + timeout : 0;
  57. (0, import_utils.assert)(this._state === "before");
  58. this._state = "running";
  59. let timer;
  60. const progress = {
  61. timeout: timeout ?? 0,
  62. deadline,
  63. disableTimeout: () => {
  64. clearTimeout(timer);
  65. },
  66. log: (message) => {
  67. if (this._state === "running")
  68. this.metadata.log.push(message);
  69. this._onCallLog?.(message);
  70. },
  71. metadata: this.metadata,
  72. race: (promise) => {
  73. const promises = Array.isArray(promise) ? promise : [promise];
  74. if (!promises.length)
  75. return Promise.resolve();
  76. return Promise.race([...promises, this._forceAbortPromise]);
  77. },
  78. wait: async (timeout2) => {
  79. let timer2;
  80. const promise = new Promise((f) => timer2 = setTimeout(f, timeout2));
  81. return progress.race(promise).finally(() => clearTimeout(timer2));
  82. },
  83. signal: this._controller.signal
  84. };
  85. if (deadline) {
  86. const timeoutError = new import_errors.TimeoutError(`Timeout ${timeout}ms exceeded.`);
  87. timer = setTimeout(() => {
  88. if (this.metadata.pauseStartTime && !this.metadata.pauseEndTime)
  89. return;
  90. if (this._state === "running") {
  91. this._state = { error: timeoutError };
  92. this._forceAbortPromise.reject(timeoutError);
  93. this._controller.abort(timeoutError);
  94. }
  95. }, deadline - (0, import_utils.monotonicTime)());
  96. }
  97. try {
  98. const result = await task(progress);
  99. this._state = "finished";
  100. return result;
  101. } catch (error) {
  102. this._state = { error };
  103. throw error;
  104. } finally {
  105. clearTimeout(timer);
  106. this._donePromise.resolve();
  107. }
  108. }
  109. }
  110. const kAbortErrorSymbol = Symbol("kAbortError");
  111. function isAbortError(error) {
  112. return error instanceof import_errors.TimeoutError || !!error[kAbortErrorSymbol];
  113. }
  114. async function raceUncancellableOperationWithCleanup(progress, run, cleanup) {
  115. let aborted = false;
  116. try {
  117. return await progress.race(run().then(async (t) => {
  118. if (aborted)
  119. await cleanup(t);
  120. return t;
  121. }));
  122. } catch (error) {
  123. aborted = true;
  124. throw error;
  125. }
  126. }
  127. // Annotate the CommonJS export names for ESM import in node:
  128. 0 && (module.exports = {
  129. ProgressController,
  130. isAbortError,
  131. raceUncancellableOperationWithCleanup
  132. });