UNPKG

@actions/artifact

Version:
83 lines 4.15 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { BlobClient } from '@azure/storage-blob'; import { getUploadChunkSize, getConcurrency, getUploadChunkTimeout } from '../shared/config.js'; import * as core from '@actions/core'; import * as crypto from 'crypto'; import * as stream from 'stream'; import { NetworkError } from '../shared/errors.js'; export function uploadToBlobStorage(authenticatedUploadURL, uploadStream, contentType) { return __awaiter(this, void 0, void 0, function* () { let uploadByteCount = 0; let lastProgressTime = Date.now(); const abortController = new AbortController(); const chunkTimer = (interval) => __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { const timer = setInterval(() => { if (Date.now() - lastProgressTime > interval) { reject(new Error('Upload progress stalled.')); } }, interval); abortController.signal.addEventListener('abort', () => { clearInterval(timer); resolve(); }); }); }); const maxConcurrency = getConcurrency(); const bufferSize = getUploadChunkSize(); const blobClient = new BlobClient(authenticatedUploadURL); const blockBlobClient = blobClient.getBlockBlobClient(); core.debug(`Uploading artifact to blob storage with maxConcurrency: ${maxConcurrency}, bufferSize: ${bufferSize}, contentType: ${contentType}`); const uploadCallback = (progress) => { core.info(`Uploaded bytes ${progress.loadedBytes}`); uploadByteCount = progress.loadedBytes; lastProgressTime = Date.now(); }; const options = { blobHTTPHeaders: { blobContentType: contentType }, onProgress: uploadCallback, abortSignal: abortController.signal }; let sha256Hash = undefined; const blobUploadStream = new stream.PassThrough(); const hashStream = crypto.createHash('sha256'); uploadStream.pipe(blobUploadStream); // This stream is used for the upload uploadStream.pipe(hashStream).setEncoding('hex'); // This stream is used to compute a hash of the content for integrity check core.info('Beginning upload of artifact content to blob storage'); try { yield Promise.race([ blockBlobClient.uploadStream(blobUploadStream, bufferSize, maxConcurrency, options), chunkTimer(getUploadChunkTimeout()) ]); } catch (error) { if (NetworkError.isNetworkErrorCode(error === null || error === void 0 ? void 0 : error.code)) { throw new NetworkError(error === null || error === void 0 ? void 0 : error.code); } throw error; } finally { abortController.abort(); } core.info('Finished uploading artifact content to blob storage!'); hashStream.end(); sha256Hash = hashStream.read(); core.info(`SHA256 digest of uploaded artifact is ${sha256Hash}`); if (uploadByteCount === 0) { core.warning(`No data was uploaded to blob storage. Reported upload byte count is 0.`); } return { uploadSize: uploadByteCount, sha256Hash }; }); } //# sourceMappingURL=blob-upload.js.map