artifactDispatcher.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ArtifactDispatcher = void 0;
  6. var _dispatcher = require("./dispatcher");
  7. var _streamDispatcher = require("./streamDispatcher");
  8. var _fs = _interopRequireDefault(require("fs"));
  9. var _fileUtils = require("../../utils/fileUtils");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /**
  12. * Copyright (c) Microsoft Corporation.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the 'License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. class ArtifactDispatcher extends _dispatcher.Dispatcher {
  27. static from(parentScope, artifact) {
  28. return ArtifactDispatcher.fromNullable(parentScope, artifact);
  29. }
  30. static fromNullable(parentScope, artifact) {
  31. if (!artifact) return undefined;
  32. const result = (0, _dispatcher.existingDispatcher)(artifact);
  33. return result || new ArtifactDispatcher(parentScope, artifact);
  34. }
  35. constructor(scope, artifact) {
  36. super(scope, artifact, 'Artifact', {
  37. absolutePath: artifact.localPath()
  38. });
  39. this._type_Artifact = true;
  40. }
  41. async pathAfterFinished() {
  42. const path = await this._object.localPathAfterFinished();
  43. return {
  44. value: path
  45. };
  46. }
  47. async saveAs(params) {
  48. return await new Promise((resolve, reject) => {
  49. this._object.saveAs(async (localPath, error) => {
  50. if (error) {
  51. reject(error);
  52. return;
  53. }
  54. try {
  55. await (0, _fileUtils.mkdirIfNeeded)(params.path);
  56. await _fs.default.promises.copyFile(localPath, params.path);
  57. resolve();
  58. } catch (e) {
  59. reject(e);
  60. }
  61. });
  62. });
  63. }
  64. async saveAsStream() {
  65. return await new Promise((resolve, reject) => {
  66. this._object.saveAs(async (localPath, error) => {
  67. if (error) {
  68. reject(error);
  69. return;
  70. }
  71. try {
  72. const readable = _fs.default.createReadStream(localPath, {
  73. highWaterMark: 1024 * 1024
  74. });
  75. const stream = new _streamDispatcher.StreamDispatcher(this, readable);
  76. // Resolve with a stream, so that client starts saving the data.
  77. resolve({
  78. stream
  79. });
  80. // Block the Artifact until the stream is consumed.
  81. await new Promise(resolve => {
  82. readable.on('close', resolve);
  83. readable.on('end', resolve);
  84. readable.on('error', resolve);
  85. });
  86. } catch (e) {
  87. reject(e);
  88. }
  89. });
  90. });
  91. }
  92. async stream() {
  93. const fileName = await this._object.localPathAfterFinished();
  94. const readable = _fs.default.createReadStream(fileName, {
  95. highWaterMark: 1024 * 1024
  96. });
  97. return {
  98. stream: new _streamDispatcher.StreamDispatcher(this, readable)
  99. };
  100. }
  101. async failure() {
  102. const error = await this._object.failureError();
  103. return {
  104. error: error || undefined
  105. };
  106. }
  107. async cancel() {
  108. await this._object.cancel();
  109. }
  110. async delete(_, metadata) {
  111. metadata.potentiallyClosesScope = true;
  112. await this._object.delete();
  113. this._dispose();
  114. }
  115. }
  116. exports.ArtifactDispatcher = ArtifactDispatcher;