From 31a7f83b82e78927f89689f323fcb9185144d6ff Mon Sep 17 00:00:00 2001 From: n1474335 Date: Fri, 11 Nov 2022 16:27:14 +0000 Subject: [PATCH] Added 'LZ4 Compress' and 'LZ4 Decompress' operations. Closes #1116 --- package-lock.json | 11 +++++++ package.json | 1 + src/core/config/Categories.json | 4 ++- src/core/operations/LZ4Compress.mjs | 43 +++++++++++++++++++++++++++ src/core/operations/LZ4Decompress.mjs | 43 +++++++++++++++++++++++++++ tests/operations/tests/Compress.mjs | 30 +++++++++++++++++++ 6 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/core/operations/LZ4Compress.mjs create mode 100644 src/core/operations/LZ4Decompress.mjs diff --git a/package-lock.json b/package-lock.json index cbb61cc9..300fc6a2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -58,6 +58,7 @@ "loglevel": "^1.8.0", "loglevel-message-prefix": "^3.0.0", "lz-string": "^1.4.4", + "lz4js": "^0.2.0", "markdown-it": "^13.0.1", "moment": "^2.29.3", "moment-timezone": "^0.5.34", @@ -9608,6 +9609,11 @@ "lz-string": "bin/bin.js" } }, + "node_modules/lz4js": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/lz4js/-/lz4js-0.2.0.tgz", + "integrity": "sha512-gY2Ia9Lm7Ep8qMiuGRhvUq0Q7qUereeldZPP1PMEJxPtEWHJLqw9pgX68oHajBH0nzJK4MaZEA/YNV3jT8u8Bg==" + }, "node_modules/make-dir": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz", @@ -21609,6 +21615,11 @@ "resolved": "https://registry.npmjs.org/lz-string/-/lz-string-1.4.4.tgz", "integrity": "sha512-0ckx7ZHRPqb0oUm8zNr+90mtf9DQB60H1wMCjBtfi62Kl3a7JbHob6gA2bC+xRvZoOL+1hzUK8jeuEIQE8svEQ==" }, + "lz4js": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/lz4js/-/lz4js-0.2.0.tgz", + "integrity": "sha512-gY2Ia9Lm7Ep8qMiuGRhvUq0Q7qUereeldZPP1PMEJxPtEWHJLqw9pgX68oHajBH0nzJK4MaZEA/YNV3jT8u8Bg==" + }, "make-dir": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz", diff --git a/package.json b/package.json index 9f7ab0f7..3c34ebcc 100644 --- a/package.json +++ b/package.json @@ -134,6 +134,7 @@ "loglevel": "^1.8.0", "loglevel-message-prefix": "^3.0.0", "lz-string": "^1.4.4", + "lz4js": "^0.2.0", "markdown-it": "^13.0.1", "moment": "^2.29.3", "moment-timezone": "^0.5.34", diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 43d5dc4e..211eae64 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -333,7 +333,9 @@ "LZString Decompress", "LZString Compress", "LZMA Decompress", - "LZMA Compress" + "LZMA Compress", + "LZ4 Decompress", + "LZ4 Compress" ] }, { diff --git a/src/core/operations/LZ4Compress.mjs b/src/core/operations/LZ4Compress.mjs new file mode 100644 index 00000000..1f43785f --- /dev/null +++ b/src/core/operations/LZ4Compress.mjs @@ -0,0 +1,43 @@ +/** + * @author n1474335 [n1474335@gmail.com] + * @copyright Crown Copyright 2022 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import lz4 from "lz4js"; + +/** + * LZ4 Compress operation + */ +class LZ4Compress extends Operation { + + /** + * LZ4Compress constructor + */ + constructor() { + super(); + + this.name = "LZ4 Compress"; + this.module = "Compression"; + this.description = "LZ4 is a lossless data compression algorithm that is focused on compression and decompression speed. It belongs to the LZ77 family of byte-oriented compression schemes."; + this.infoURL = "https://wikipedia.org/wiki/LZ4_(compression_algorithm)"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; + this.args = []; + } + + /** + * @param {ArrayBuffer} input + * @param {Object[]} args + * @returns {ArrayBuffer} + */ + run(input, args) { + const inBuf = new Uint8Array(input); + const compressed = lz4.compress(inBuf); + return compressed.buffer; + } + +} + +export default LZ4Compress; diff --git a/src/core/operations/LZ4Decompress.mjs b/src/core/operations/LZ4Decompress.mjs new file mode 100644 index 00000000..2ba50416 --- /dev/null +++ b/src/core/operations/LZ4Decompress.mjs @@ -0,0 +1,43 @@ +/** + * @author n1474335 [n1474335@gmail.com] + * @copyright Crown Copyright 2022 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import lz4 from "lz4js"; + +/** + * LZ4 Decompress operation + */ +class LZ4Decompress extends Operation { + + /** + * LZ4Decompress constructor + */ + constructor() { + super(); + + this.name = "LZ4 Decompress"; + this.module = "Compression"; + this.description = "LZ4 is a lossless data compression algorithm that is focused on compression and decompression speed. It belongs to the LZ77 family of byte-oriented compression schemes."; + this.infoURL = "https://wikipedia.org/wiki/LZ4_(compression_algorithm)"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; + this.args = []; + } + + /** + * @param {ArrayBuffer} input + * @param {Object[]} args + * @returns {ArrayBuffer} + */ + run(input, args) { + const inBuf = new Uint8Array(input); + const decompressed = lz4.decompress(inBuf); + return decompressed.buffer; + } + +} + +export default LZ4Decompress; diff --git a/tests/operations/tests/Compress.mjs b/tests/operations/tests/Compress.mjs index 015277b1..60117c67 100644 --- a/tests/operations/tests/Compress.mjs +++ b/tests/operations/tests/Compress.mjs @@ -75,4 +75,34 @@ TestRegister.addTests([ } ], }, + { + name: "LZ4 Compress", + input: "The cat sat on the mat.", + expectedOutput: "04224d184070df170000805468652063617420736174206f6e20746865206d61742e00000000", + recipeConfig: [ + { + "op": "LZ4 Compress", + "args": [] + }, + { + "op": "To Hex", + "args": ["None", 0] + } + ], + }, + { + name: "LZ4 Decompress", + input: "04224d184070df170000805468652063617420736174206f6e20746865206d61742e00000000", + expectedOutput: "The cat sat on the mat.", + recipeConfig: [ + { + "op": "From Hex", + "args": ["None"] + }, + { + "op": "LZ4 Decompress", + "args": [] + } + ], + }, ]);