UNPKG

@salesforce/core

Version:

Core libraries to interact with SFDX projects, orgs, and APIs.

57 lines 2.22 kB
"use strict"; /* * Copyright (c) 2021, salesforce.com, inc. * All rights reserved. * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZipWriter = void 0; const node_stream_1 = require("node:stream"); const jszip_1 = __importDefault(require("jszip")); const logger_1 = require("../logger/logger"); const sfError_1 = require("../sfError"); class ZipWriter extends node_stream_1.Writable { rootDestination; zip = (0, jszip_1.default)(); zipBuffer; logger; constructor(rootDestination) { super({ objectMode: true }); this.rootDestination = rootDestination; const destination = rootDestination ? `for: ${rootDestination}` : 'in memory'; this.logger = logger_1.Logger.childFromRoot(this.constructor.name); this.logger.debug(`generating zip ${destination}`); } get buffer() { if (!this.zipBuffer) { throw new sfError_1.SfError('Must finalize the ZipWriter before getting a buffer'); } return this.zipBuffer; } async addToStore(contents, path) { // Ensure only posix paths are added to zip files const posixPath = path.replace(/\\/g, '/'); this.zip.file(posixPath, contents); return Promise.resolve(); } async finalize() { // compression-/speed+ (0)<---(3)---------->(9) compression+/speed- // 3 appears to be a decent balance of compression and speed. It felt like // higher values = diminishing returns on compression and made conversion slower this.zipBuffer = await this.zip.generateAsync({ type: 'nodebuffer', compression: 'DEFLATE', compressionOptions: { level: 3 }, }); this.logger.debug('Generated zip complete'); } getDestinationPath() { return this.rootDestination; } } exports.ZipWriter = ZipWriter; //# sourceMappingURL=zipWriter.js.map