artifact.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var artifact_exports = {};
  30. __export(artifact_exports, {
  31. Artifact: () => Artifact
  32. });
  33. module.exports = __toCommonJS(artifact_exports);
  34. var import_fs = __toESM(require("fs"));
  35. var import_utils = require("../utils");
  36. var import_errors = require("./errors");
  37. var import_instrumentation = require("./instrumentation");
  38. var import_manualPromise = require("../utils/isomorphic/manualPromise");
  39. class Artifact extends import_instrumentation.SdkObject {
  40. constructor(parent, localPath, unaccessibleErrorMessage, cancelCallback) {
  41. super(parent, "artifact");
  42. this._finishedPromise = new import_manualPromise.ManualPromise();
  43. this._saveCallbacks = [];
  44. this._finished = false;
  45. this._deleted = false;
  46. this._localPath = localPath;
  47. this._unaccessibleErrorMessage = unaccessibleErrorMessage;
  48. this._cancelCallback = cancelCallback;
  49. }
  50. finishedPromise() {
  51. return this._finishedPromise;
  52. }
  53. localPath() {
  54. return this._localPath;
  55. }
  56. async localPathAfterFinished() {
  57. if (this._unaccessibleErrorMessage)
  58. throw new Error(this._unaccessibleErrorMessage);
  59. await this._finishedPromise;
  60. if (this._failureError)
  61. throw this._failureError;
  62. return this._localPath;
  63. }
  64. saveAs(saveCallback) {
  65. if (this._unaccessibleErrorMessage)
  66. throw new Error(this._unaccessibleErrorMessage);
  67. if (this._deleted)
  68. throw new Error(`File already deleted. Save before deleting.`);
  69. if (this._failureError)
  70. throw this._failureError;
  71. if (this._finished) {
  72. saveCallback(this._localPath).catch(() => {
  73. });
  74. return;
  75. }
  76. this._saveCallbacks.push(saveCallback);
  77. }
  78. async failureError() {
  79. if (this._unaccessibleErrorMessage)
  80. return this._unaccessibleErrorMessage;
  81. await this._finishedPromise;
  82. return this._failureError?.message || null;
  83. }
  84. async cancel() {
  85. (0, import_utils.assert)(this._cancelCallback !== void 0);
  86. return this._cancelCallback();
  87. }
  88. async delete() {
  89. if (this._unaccessibleErrorMessage)
  90. return;
  91. const fileName = await this.localPathAfterFinished();
  92. if (this._deleted)
  93. return;
  94. this._deleted = true;
  95. if (fileName)
  96. await import_fs.default.promises.unlink(fileName).catch((e) => {
  97. });
  98. }
  99. async deleteOnContextClose() {
  100. if (this._deleted)
  101. return;
  102. this._deleted = true;
  103. if (!this._unaccessibleErrorMessage)
  104. await import_fs.default.promises.unlink(this._localPath).catch((e) => {
  105. });
  106. await this.reportFinished(new import_errors.TargetClosedError(this.closeReason()));
  107. }
  108. async reportFinished(error) {
  109. if (this._finished)
  110. return;
  111. this._finished = true;
  112. this._failureError = error;
  113. if (error) {
  114. for (const callback of this._saveCallbacks)
  115. await callback("", error);
  116. } else {
  117. for (const callback of this._saveCallbacks)
  118. await callback(this._localPath);
  119. }
  120. this._saveCallbacks = [];
  121. this._finishedPromise.resolve();
  122. }
  123. }
  124. // Annotate the CommonJS export names for ESM import in node:
  125. 0 && (module.exports = {
  126. Artifact
  127. });