Bzip2Decompress.mjs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @author Matt C [me@mitt.dev]
  3. * @copyright Crown Copyright 2019
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation";
  7. import OperationError from "../errors/OperationError";
  8. import Bzip2 from "libbzip2-wasm";
  9. /**
  10. * Bzip2 Decompress operation
  11. */
  12. class Bzip2Decompress extends Operation {
  13. /**
  14. * Bzip2Decompress constructor
  15. */
  16. constructor() {
  17. super();
  18. this.name = "Bzip2 Decompress";
  19. this.module = "Compression";
  20. this.description = "Decompresses data using the Bzip2 algorithm.";
  21. this.infoURL = "https://wikipedia.org/wiki/Bzip2";
  22. this.inputType = "ArrayBuffer";
  23. this.outputType = "ArrayBuffer";
  24. this.args = [
  25. {
  26. name: "Use low-memory, slower decompression algorithm",
  27. type: "boolean",
  28. value: false
  29. }
  30. ];
  31. this.patterns = [
  32. {
  33. "match": "^\\x42\\x5a\\x68",
  34. "flags": "",
  35. "args": []
  36. }
  37. ];
  38. }
  39. /**
  40. * @param {byteArray} input
  41. * @param {Object[]} args
  42. * @returns {string}
  43. */
  44. run(input, args) {
  45. const [small] = args;
  46. if (input.byteLength <= 0) {
  47. throw new OperationError("Please provide an input.");
  48. }
  49. if (ENVIRONMENT_IS_WORKER()) self.sendStatusMessage("Loading Bzip2...");
  50. return new Promise((resolve, reject) => {
  51. Bzip2().then(bzip2 => {
  52. if (ENVIRONMENT_IS_WORKER()) self.sendStatusMessage("Decompressing data...");
  53. const inpArray = new Uint8Array(input);
  54. const bzip2cc = bzip2.decompressBZ2(inpArray, small ? 1 : 0);
  55. if (bzip2cc.error !== 0) {
  56. reject(new OperationError(bzip2cc.error_msg));
  57. } else {
  58. const output = bzip2cc.output;
  59. resolve(output.buffer.slice(output.byteOffset, output.byteLength + output.byteOffset));
  60. }
  61. });
  62. });
  63. }
  64. }
  65. export default Bzip2Decompress;