CoolFace
Apppublic

TrinetraLabs/Placebo_AI

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
zipObject.js134 linesDownload Raw Back to lib
1"use strict";2 3var StreamHelper = require("./stream/StreamHelper");4var DataWorker = require("./stream/DataWorker");5var utf8 = require("./utf8");6var CompressedObject = require("./compressedObject");7var GenericWorker = require("./stream/GenericWorker");8 9/**10 * A simple object representing a file in the zip file.11 * @constructor12 * @param {string} name the name of the file13 * @param {String|ArrayBuffer|Uint8Array|Buffer} data the data14 * @param {Object} options the options of the file15 */16var ZipObject = function(name, data, options) {17    this.name = name;18    this.dir = options.dir;19    this.date = options.date;20    this.comment = options.comment;21    this.unixPermissions = options.unixPermissions;22    this.dosPermissions = options.dosPermissions;23 24    this._data = data;25    this._dataBinary = options.binary;26    // keep only the compression27    this.options = {28        compression : options.compression,29        compressionOptions : options.compressionOptions30    };31};32 33ZipObject.prototype = {34    /**35     * Create an internal stream for the content of this object.36     * @param {String} type the type of each chunk.37     * @return StreamHelper the stream.38     */39    internalStream: function (type) {40        var result = null, outputType = "string";41        try {42            if (!type) {43                throw new Error("No output type specified.");44            }45            outputType = type.toLowerCase();46            var askUnicodeString = outputType === "string" || outputType === "text";47            if (outputType === "binarystring" || outputType === "text") {48                outputType = "string";49            }50            result = this._decompressWorker();51 52            var isUnicodeString = !this._dataBinary;53 54            if (isUnicodeString && !askUnicodeString) {55                result = result.pipe(new utf8.Utf8EncodeWorker());56            }57            if (!isUnicodeString && askUnicodeString) {58                result = result.pipe(new utf8.Utf8DecodeWorker());59            }60        } catch (e) {61            result = new GenericWorker("error");62            result.error(e);63        }64 65        return new StreamHelper(result, outputType, "");66    },67 68    /**69     * Prepare the content in the asked type.70     * @param {String} type the type of the result.71     * @param {Function} onUpdate a function to call on each internal update.72     * @return Promise the promise of the result.73     */74    async: function (type, onUpdate) {75        return this.internalStream(type).accumulate(onUpdate);76    },77 78    /**79     * Prepare the content as a nodejs stream.80     * @param {String} type the type of each chunk.81     * @param {Function} onUpdate a function to call on each internal update.82     * @return Stream the stream.83     */84    nodeStream: function (type, onUpdate) {85        return this.internalStream(type || "nodebuffer").toNodejsStream(onUpdate);86    },87 88    /**89     * Return a worker for the compressed content.90     * @private91     * @param {Object} compression the compression object to use.92     * @param {Object} compressionOptions the options to use when compressing.93     * @return Worker the worker.94     */95    _compressWorker: function (compression, compressionOptions) {96        if (97            this._data instanceof CompressedObject &&98            this._data.compression.magic === compression.magic99        ) {100            return this._data.getCompressedWorker();101        } else {102            var result = this._decompressWorker();103            if(!this._dataBinary) {104                result = result.pipe(new utf8.Utf8EncodeWorker());105            }106            return CompressedObject.createWorkerFrom(result, compression, compressionOptions);107        }108    },109    /**110     * Return a worker for the decompressed content.111     * @private112     * @return Worker the worker.113     */114    _decompressWorker : function () {115        if (this._data instanceof CompressedObject) {116            return this._data.getContentWorker();117        } else if (this._data instanceof GenericWorker) {118            return this._data;119        } else {120            return new DataWorker(this._data);121        }122    }123};124 125var removedMethods = ["asText", "asBinary", "asNodeBuffer", "asUint8Array", "asArrayBuffer"];126var removedFn = function () {127    throw new Error("This method has been removed in JSZip 3.0, please check the upgrade guide.");128};129 130for(var i = 0; i < removedMethods.length; i++) {131    ZipObject.prototype[removedMethods[i]] = removedFn;132}133module.exports = ZipObject;134