2019-04-02 11:55:59 +01:00
/ * *
* @ author Matt C [ me @ mitt . dev ]
* @ copyright Crown Copyright 2019
* @ license Apache - 2.0
* /
2019-07-09 12:23:59 +01:00
import Operation from "../Operation.mjs" ;
import OperationError from "../errors/OperationError.mjs" ;
2019-04-02 11:55:59 +01:00
import Bzip2 from "libbzip2-wasm" ;
2019-07-09 12:23:59 +01:00
import { isWorkerEnvironment } from "../Utils.mjs" ;
2019-04-02 11:55:59 +01:00
/ * *
* Bzip2 Compress operation
* /
class Bzip2Compress extends Operation {
/ * *
* Bzip2Compress constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Bzip2 Compress" ;
this . module = "Compression" ;
2019-04-02 12:05:17 +01:00
this . description = "Bzip2 is a compression library developed by Julian Seward (of GHC fame) that uses the Burrows-Wheeler algorithm. It only supports compressing single files and its compression is slow, however is more effective than Deflate (.gz & .zip)." ;
this . infoURL = "https://wikipedia.org/wiki/Bzip2" ;
2019-04-02 11:55:59 +01:00
this . inputType = "ArrayBuffer" ;
2019-04-02 16:47:38 +01:00
this . outputType = "ArrayBuffer" ;
2019-04-02 11:55:59 +01:00
this . args = [
{
name : "Block size (100s of kb)" ,
type : "number" ,
2019-06-27 16:45:16 +01:00
value : 9 ,
min : 1 ,
max : 9
2019-04-02 11:55:59 +01:00
} ,
{
name : "Work factor" ,
type : "number" ,
value : 30
}
] ;
}
/ * *
* @ param { ArrayBuffer } input
* @ param { Object [ ] } args
* @ returns { File }
* /
run ( input , args ) {
2019-04-02 17:01:47 +01:00
const [ blockSize , workFactor ] = args ;
2019-04-02 11:55:59 +01:00
if ( input . byteLength <= 0 ) {
throw new OperationError ( "Please provide an input." ) ;
}
2019-07-05 11:35:59 +01:00
if ( isWorkerEnvironment ( ) ) self . sendStatusMessage ( "Loading Bzip2..." ) ;
2019-04-02 11:55:59 +01:00
return new Promise ( ( resolve , reject ) => {
Bzip2 ( ) . then ( bzip2 => {
2019-07-05 11:35:59 +01:00
if ( isWorkerEnvironment ( ) ) self . sendStatusMessage ( "Compressing data..." ) ;
2019-04-02 11:55:59 +01:00
const inpArray = new Uint8Array ( input ) ;
const bzip2cc = bzip2 . compressBZ2 ( inpArray , blockSize , workFactor ) ;
if ( bzip2cc . error !== 0 ) {
reject ( new OperationError ( bzip2cc . error _msg ) ) ;
} else {
2019-04-02 16:47:38 +01:00
const output = bzip2cc . output ;
resolve ( output . buffer . slice ( output . byteOffset , output . byteLength + output . byteOffset ) ) ;
2019-04-02 11:55:59 +01:00
}
} ) ;
} ) ;
}
}
export default Bzip2Compress ;