artifact.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Artifact = void 0;
  6. var fs = _interopRequireWildcard(require("fs"));
  7. var _stream = require("./stream");
  8. var _fileUtils = require("../utils/fileUtils");
  9. var _channelOwner = require("./channelOwner");
  10. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  11. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  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 _channelOwner.ChannelOwner {
  28. static from(channel) {
  29. return channel._object;
  30. }
  31. async pathAfterFinished() {
  32. if (this._connection.isRemote()) throw new Error(`Path is not available when connecting remotely. Use saveAs() to save a local copy.`);
  33. return (await this._channel.pathAfterFinished()).value;
  34. }
  35. async saveAs(path) {
  36. if (!this._connection.isRemote()) {
  37. await this._channel.saveAs({
  38. path
  39. });
  40. return;
  41. }
  42. const result = await this._channel.saveAsStream();
  43. const stream = _stream.Stream.from(result.stream);
  44. await (0, _fileUtils.mkdirIfNeeded)(path);
  45. await new Promise((resolve, reject) => {
  46. stream.stream().pipe(fs.createWriteStream(path)).on('finish', resolve).on('error', reject);
  47. });
  48. }
  49. async failure() {
  50. return (await this._channel.failure()).error || null;
  51. }
  52. async createReadStream() {
  53. const result = await this._channel.stream();
  54. const stream = _stream.Stream.from(result.stream);
  55. return stream.stream();
  56. }
  57. async readIntoBuffer() {
  58. const stream = await this.createReadStream();
  59. return await new Promise((resolve, reject) => {
  60. const chunks = [];
  61. stream.on('data', chunk => {
  62. chunks.push(chunk);
  63. });
  64. stream.on('end', () => {
  65. resolve(Buffer.concat(chunks));
  66. });
  67. stream.on('error', reject);
  68. });
  69. }
  70. async cancel() {
  71. return await this._channel.cancel();
  72. }
  73. async delete() {
  74. return await this._channel.delete();
  75. }
  76. }
  77. exports.Artifact = Artifact;