"use strict"; 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()); }); }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs = __importStar(require("fs")); const zlib = __importStar(require("zlib")); const util_1 = require("util"); const stat = util_1.promisify(fs.stat); /** * Creates a Gzip compressed file of an original file at the provided temporary filepath location * @param {string} originalFilePath filepath of whatever will be compressed. The original file will be unmodified * @param {string} tempFilePath the location of where the Gzip file will be created * @returns the size of gzip file that gets created */ function createGZipFileOnDisk(originalFilePath, tempFilePath) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { const inputStream = fs.createReadStream(originalFilePath); const gzip = zlib.createGzip(); const outputStream = fs.createWriteStream(tempFilePath); inputStream.pipe(gzip).pipe(outputStream); outputStream.on('finish', () => __awaiter(this, void 0, void 0, function* () { // wait for stream to finish before calculating the size which is needed as part of the Content-Length header when starting an upload const size = (yield stat(tempFilePath)).size; resolve(size); })); outputStream.on('error', error => { // eslint-disable-next-line no-console console.log(error); reject; }); }); }); } exports.createGZipFileOnDisk = createGZipFileOnDisk; /** * Creates a GZip file in memory using a buffer. Should be used for smaller files to reduce disk I/O * @param originalFilePath the path to the original file that is being GZipped * @returns a buffer with the GZip file */ function createGZipFileInBuffer(originalFilePath) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () { var e_1, _a; const inputStream = fs.createReadStream(originalFilePath); const gzip = zlib.createGzip(); inputStream.pipe(gzip); // read stream into buffer, using experimental async iterators see https://github.com/nodejs/readable-stream/issues/403#issuecomment-479069043 const chunks = []; try { for (var gzip_1 = __asyncValues(gzip), gzip_1_1; gzip_1_1 = yield gzip_1.next(), !gzip_1_1.done;) { const chunk = gzip_1_1.value; chunks.push(chunk); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (gzip_1_1 && !gzip_1_1.done && (_a = gzip_1.return)) yield _a.call(gzip_1); } finally { if (e_1) throw e_1.error; } } resolve(Buffer.concat(chunks)); })); }); } exports.createGZipFileInBuffer = createGZipFileInBuffer; //# sourceMappingURL=upload-gzip.js.map