@onesy/zip
Version:
136 lines (108 loc) • 4.78 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import is from '@onesy/utils/is';
import to from '@onesy/utils/to';
import parse from '@onesy/utils/parse';
import serialize from '@onesy/utils/serialize';
import OnesyDate from '@onesy/date/OnesyDate';
import duration from '@onesy/date/duration';
import OnesyHuffmanCode from '@onesy/huffman-code';
import OnesyLZ77 from '@onesy/lz77';
import merge from '@onesy/utils/merge';
class OnesyZipResponse {
constructor(value, original_byte_size, value_byte_size, compression_ratio, compression_percentage, positive, performance_milliseconds, performance) {
this.value = value;
this.original_byte_size = original_byte_size;
this.value_byte_size = value_byte_size;
this.compression_ratio = compression_ratio;
this.compression_percentage = compression_percentage;
this.positive = positive;
this.performance_milliseconds = performance_milliseconds;
this.performance = performance;
}
}
const optionsDefault = {
encode_values: true,
huffman_code: 'auto'
};
class OnesyZip {
static get OnesyZipResponse() {
return OnesyZipResponse;
}
static decode(value) {
return new OnesyZip().decode(value);
}
get encoded() {
return this.response;
}
constructor(value) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : optionsDefault;
this.value = value;
_defineProperty(this, "options", {});
_defineProperty(this, "serialized", false);
_defineProperty(this, "response", void 0);
this.options = merge(options, optionsDefault);
if (value !== undefined) this.init();
}
init() {
if (!['uint8array', 'buffer', 'string'].some(item => is(item, this.value))) {
this.value = serialize(this.value);
this.serialized = true;
} // Encode
this.encode();
}
encode() {
const startTime = OnesyDate.milliseconds;
const lz77 = new OnesyLZ77(this.value);
let value = lz77.response.value;
const options = [this.serialized ? 1 : 0];
if (['auto', true].includes(this.options.huffman_code)) {
var _this$options;
const huffmanCode = new OnesyHuffmanCode(lz77.response.value, {
encode_values: (_this$options = this.options) === null || _this$options === void 0 ? void 0 : _this$options.encode_values
});
if (huffmanCode.response.positive || this.options.huffman_code === true) {
value = "".concat(huffmanCode.response.values_encoded, ", ").concat(huffmanCode.response.value);
options.unshift(1);
} else options.unshift(0);
} else options.unshift(0);
value = "".concat(options.join('')).concat(value);
const response = new OnesyZipResponse(value);
response.performance_milliseconds = OnesyDate.milliseconds - startTime;
response.performance = duration(response.performance_milliseconds) || '0 milliseconds';
response.original_byte_size = to(this.value, 'byte-size');
response.value_byte_size = to(response.value, 'byte-size');
response.compression_ratio = Number(((response.value_byte_size + response.original_byte_size) / response.value_byte_size - 1).toFixed(2));
response.compression_percentage = response.original_byte_size === 0 ? response.value_byte_size === 0 ? 0 : response.value_byte_size * -100 : Number(((response.original_byte_size - response.value_byte_size) / response.original_byte_size * 100).toFixed(2));
response.positive = response.compression_ratio > 1;
this.response = response;
return response;
}
decode(value_) {
const response = new OnesyZipResponse();
const startTime = OnesyDate.milliseconds;
if (is('string', value_)) {
const huffmanCode = value_[0] === '1';
const serialized = value_[1] === '1';
let value = value_.slice(2);
const separator = value.indexOf(', '); // Huffman code
if (huffmanCode) {
let huffmanValues = value.substring(0, separator);
const huffmanValue = value.substring(separator + 4);
if (huffmanValue && huffmanValues) {
huffmanValues = OnesyHuffmanCode.decodeValues(huffmanValues);
const huffman = OnesyHuffmanCode.decode(huffmanValue, huffmanValues);
value = huffman.value;
}
} // lz77
const lz77 = OnesyLZ77.decode(value);
response.value = lz77.value;
if (serialized) response.value = parse(response.value);
response.performance_milliseconds = OnesyDate.milliseconds - startTime;
response.performance = duration(response.performance_milliseconds) || '0 milliseconds';
response.original_byte_size = to(response.value, 'byte-size');
response.value_byte_size = to(value_, 'byte-size');
}
return response;
}
}
export default OnesyZip;