compressedObject.js 2.8 KB

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