TrinetraLabs/Placebo_AI
0
1"use strict";2 3var external = require("./external");4var DataWorker = require("./stream/DataWorker");5var Crc32Probe = require("./stream/Crc32Probe");6var DataLengthProbe = require("./stream/DataLengthProbe");7 8/**9 * Represent a compressed object, with everything needed to decompress it.10 * @constructor11 * @param {number} compressedSize the size of the data compressed.12 * @param {number} uncompressedSize the size of the data after decompression.13 * @param {number} crc32 the crc32 of the decompressed file.14 * @param {object} compression the type of compression, see lib/compressions.js.15 * @param {String|ArrayBuffer|Uint8Array|Buffer} data the compressed data.16 */17function CompressedObject(compressedSize, uncompressedSize, crc32, compression, data) {18 this.compressedSize = compressedSize;19 this.uncompressedSize = uncompressedSize;20 this.crc32 = crc32;21 this.compression = compression;22 this.compressedContent = data;23}24 25CompressedObject.prototype = {26 /**27 * Create a worker to get the uncompressed content.28 * @return {GenericWorker} the worker.29 */30 getContentWorker: function () {31 var worker = new DataWorker(external.Promise.resolve(this.compressedContent))32 .pipe(this.compression.uncompressWorker())33 .pipe(new DataLengthProbe("data_length"));34 35 var that = this;36 worker.on("end", function () {37 if (this.streamInfo["data_length"] !== that.uncompressedSize) {38 throw new Error("Bug : uncompressed data size mismatch");39 }40 });41 return worker;42 },43 /**44 * Create a worker to get the compressed content.45 * @return {GenericWorker} the worker.46 */47 getCompressedWorker: function () {48 return new DataWorker(external.Promise.resolve(this.compressedContent))49 .withStreamInfo("compressedSize", this.compressedSize)50 .withStreamInfo("uncompressedSize", this.uncompressedSize)51 .withStreamInfo("crc32", this.crc32)52 .withStreamInfo("compression", this.compression)53 ;54 }55};56 57/**58 * Chain the given worker with other workers to compress the content with the59 * given compression.60 * @param {GenericWorker} uncompressedWorker the worker to pipe.61 * @param {Object} compression the compression object.62 * @param {Object} compressionOptions the options to use when compressing.63 * @return {GenericWorker} the new worker compressing the content.64 */65CompressedObject.createWorkerFrom = function (uncompressedWorker, compression, compressionOptions) {66 return uncompressedWorker67 .pipe(new Crc32Probe())68 .pipe(new DataLengthProbe("uncompressedSize"))69 .pipe(compression.compressWorker(compressionOptions))70 .pipe(new DataLengthProbe("compressedSize"))71 .withStreamInfo("compression", compression);72};73 74module.exports = CompressedObject;75 