artifact.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Artifact = void 0;
  6. var _fs = _interopRequireDefault(require("fs"));
  7. var _utils = require("../utils");
  8. var _manualPromise = require("../utils/manualPromise");
  9. var _instrumentation = require("./instrumentation");
  10. var _errors = require("./errors");
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * Copyright (c) Microsoft Corporation.
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License");
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. class Artifact extends _instrumentation.SdkObject {
  28. constructor(parent, localPath, unaccessibleErrorMessage, cancelCallback) {
  29. super(parent, 'artifact');
  30. this._localPath = void 0;
  31. this._unaccessibleErrorMessage = void 0;
  32. this._cancelCallback = void 0;
  33. this._finishedPromise = new _manualPromise.ManualPromise();
  34. this._saveCallbacks = [];
  35. this._finished = false;
  36. this._deleted = false;
  37. this._failureError = void 0;
  38. this._localPath = localPath;
  39. this._unaccessibleErrorMessage = unaccessibleErrorMessage;
  40. this._cancelCallback = cancelCallback;
  41. }
  42. finishedPromise() {
  43. return this._finishedPromise;
  44. }
  45. localPath() {
  46. return this._localPath;
  47. }
  48. async localPathAfterFinished() {
  49. if (this._unaccessibleErrorMessage) throw new Error(this._unaccessibleErrorMessage);
  50. await this._finishedPromise;
  51. if (this._failureError) throw this._failureError;
  52. return this._localPath;
  53. }
  54. saveAs(saveCallback) {
  55. if (this._unaccessibleErrorMessage) throw new Error(this._unaccessibleErrorMessage);
  56. if (this._deleted) throw new Error(`File already deleted. Save before deleting.`);
  57. if (this._failureError) throw this._failureError;
  58. if (this._finished) {
  59. saveCallback(this._localPath).catch(() => {});
  60. return;
  61. }
  62. this._saveCallbacks.push(saveCallback);
  63. }
  64. async failureError() {
  65. var _this$_failureError;
  66. if (this._unaccessibleErrorMessage) return this._unaccessibleErrorMessage;
  67. await this._finishedPromise;
  68. return ((_this$_failureError = this._failureError) === null || _this$_failureError === void 0 ? void 0 : _this$_failureError.message) || null;
  69. }
  70. async cancel() {
  71. (0, _utils.assert)(this._cancelCallback !== undefined);
  72. return this._cancelCallback();
  73. }
  74. async delete() {
  75. if (this._unaccessibleErrorMessage) return;
  76. const fileName = await this.localPathAfterFinished();
  77. if (this._deleted) return;
  78. this._deleted = true;
  79. if (fileName) await _fs.default.promises.unlink(fileName).catch(e => {});
  80. }
  81. async deleteOnContextClose() {
  82. // Compared to "delete", this method does not wait for the artifact to finish.
  83. // We use it when closing the context to avoid stalling.
  84. if (this._deleted) return;
  85. this._deleted = true;
  86. if (!this._unaccessibleErrorMessage) await _fs.default.promises.unlink(this._localPath).catch(e => {});
  87. await this.reportFinished(new _errors.TargetClosedError());
  88. }
  89. async reportFinished(error) {
  90. if (this._finished) return;
  91. this._finished = true;
  92. this._failureError = error;
  93. if (error) {
  94. for (const callback of this._saveCallbacks) await callback('', error);
  95. } else {
  96. for (const callback of this._saveCallbacks) await callback(this._localPath);
  97. }
  98. this._saveCallbacks = [];
  99. this._finishedPromise.resolve();
  100. }
  101. }
  102. exports.Artifact = Artifact;