暫無描述

compressedObject.js 2.8KB

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