2022-09-19 14:05:13 +01:00
/ * *
* @ author Matt C [ me @ mitt . dev ]
* @ copyright Crown Copyright 2022
* @ license Apache - 2.0
* /
import Operation from "../Operation.mjs" ;
import OperationError from "../errors/OperationError.mjs" ;
import { compress } from "@blu3r4y/lzma" ;
2022-09-19 17:33:55 +01:00
import { isWorkerEnvironment } from "../Utils.mjs" ;
2022-09-19 14:05:13 +01:00
/ * *
* LZMA Compress operation
* /
class LZMACompress extends Operation {
/ * *
* LZMACompress constructor
* /
constructor ( ) {
super ( ) ;
this . name = "LZMA Compress" ;
this . module = "Compression" ;
this . description = "Compresses data using the Lempel\u2013Ziv\u2013Markov chain algorithm. Compression mode determines the speed and effectiveness of the compression: 1 is fastest and less effective, 9 is slowest and most effective" ;
this . infoURL = "https://wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Markov_chain_algorithm" ;
this . inputType = "ArrayBuffer" ;
this . outputType = "ArrayBuffer" ;
this . args = [
{
name : "Compression Mode" ,
type : "option" ,
value : [
"1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9"
] ,
"defaultIndex" : 6
}
] ;
}
/ * *
* @ param { ArrayBuffer } input
* @ param { Object [ ] } args
* @ returns { ArrayBuffer }
* /
2022-09-19 17:33:55 +01:00
async run ( input , args ) {
2022-09-19 14:20:27 +01:00
const mode = Number ( args [ 0 ] ) ;
2022-09-19 14:05:13 +01:00
return new Promise ( ( resolve , reject ) => {
2022-09-19 14:20:27 +01:00
compress ( new Uint8Array ( input ) , mode , ( result , error ) => {
2022-09-19 14:05:13 +01:00
if ( error ) {
reject ( new OperationError ( ` Failed to compress input: ${ error . message } ` ) ) ;
}
// The compression returns as an Int8Array, but we can just get the unsigned data from the buffer
resolve ( new Int8Array ( result ) . buffer ) ;
} , ( percent ) => {
2022-09-19 17:33:55 +01:00
if ( isWorkerEnvironment ( ) ) self . sendStatusMessage ( ` Compressing input: ${ ( percent * 100 ) . toFixed ( 2 ) } % ` ) ;
2022-09-19 14:05:13 +01:00
} ) ;
} ) ;
}
}
export default LZMACompress ;