UNPKG

hypershield

Version:

Middleware suite for high-performance and resilient APIs

83 lines 3.69 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CompressionService = void 0; const zlib_1 = require("zlib"); const util_1 = require("util"); const constants_1 = require("../../../core/constants/constants"); const gzipAsync = (0, util_1.promisify)(zlib_1.gzip); const gunzipAsync = (0, util_1.promisify)(zlib_1.gunzip); class CompressionService { constructor(options = {}) { var _a, _b, _c; this.options = { level: (_a = options.level) !== null && _a !== void 0 ? _a : constants_1.COMPRESSION.GZIP.DEFAULT_LEVEL, threshold: (_b = options.threshold) !== null && _b !== void 0 ? _b : constants_1.COMPRESSION.GZIP.MIN_SIZE_TO_COMPRESS, memLevel: (_c = options.memLevel) !== null && _c !== void 0 ? _c : constants_1.COMPRESSION.GZIP.DEFAULT_MEM_LEVEL }; } shouldCompress(data) { var _a; const size = Buffer.byteLength(data); return size >= ((_a = this.options.threshold) !== null && _a !== void 0 ? _a : 0); } compress(data) { return __awaiter(this, void 0, void 0, function* () { try { if (!data || (typeof data === 'string' && !data.length) || (Buffer.isBuffer(data) && !data.length)) { throw new Error('Invalid input: empty data'); } if (!this.shouldCompress(data)) { return Buffer.from(data); } const input = Buffer.isBuffer(data) ? data : Buffer.from(data); return yield gzipAsync(input, { level: this.options.level, memLevel: this.options.memLevel }); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; throw new Error(`Compression failed: ${errorMessage}`); } }); } decompress(data) { return __awaiter(this, void 0, void 0, function* () { try { if (!this.isCompressed(data)) { return data; } return yield gunzipAsync(data); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; throw new Error(`Decompression failed: ${errorMessage}`); } }); } compressString(data) { return __awaiter(this, void 0, void 0, function* () { return this.compress(data); }); } decompressToString(data) { return __awaiter(this, void 0, void 0, function* () { const decompressed = yield this.decompress(data); return decompressed.toString(); }); } isCompressed(data) { return data.length >= 2 && data[0] === 0x1f && data[1] === 0x8b; } } exports.CompressionService = CompressionService; //# sourceMappingURL=compressionService.js.map