"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CompressCacheInterceptor = void 0; const common_1 = require("@nestjs/common"); const rxjs_1 = require("rxjs"); const operators_1 = require("rxjs/operators"); const cache_manager_1 = require("@nestjs/cache-manager"); const zlib = __importStar(require("zlib")); const util_1 = require("util"); const core_1 = require("@nestjs/core"); const gzip = (0, util_1.promisify)(zlib.gzip); const gunzip = (0, util_1.promisify)(zlib.gunzip); const COMPRESSION_THRESHOLD = 100; let CompressCacheInterceptor = class CompressCacheInterceptor { constructor(cacheManager, httpAdapterHost) { this.cacheManager = cacheManager; this.httpAdapterHost = httpAdapterHost; } async intercept(context, next) { const httpAdapter = this.httpAdapterHost.httpAdapter; const request = context.getArgByIndex(0); const response = context.getArgByIndex(1); if (httpAdapter.getRequestMethod(request) !== 'GET') { return next.handle(); } const cacheKey = httpAdapter.getRequestUrl(request); let cachedData = await this.cacheManager.get(cacheKey); if (cachedData) { try { if (Buffer.isBuffer(cachedData) && cachedData.length > 2 && cachedData[0] === 0x1f && cachedData[1] === 0x8b) { const decompressed = await gunzip(cachedData); const jsonString = decompressed.toString('utf8'); httpAdapter.setHeader(response, 'Content-Type', 'application/json'); httpAdapter.setHeader(response, 'X-Cache', 'HIT (Compressed)'); return (0, rxjs_1.of)(JSON.parse(jsonString)); } else { const jsonString = cachedData.toString(); httpAdapter.setHeader(response, 'Content-Type', 'application/json'); httpAdapter.setHeader(response, 'X-Cache', 'HIT (Uncompressed)'); return (0, rxjs_1.of)(JSON.parse(jsonString)); } } catch (e) { console.error(`[Cache] Lỗi giải nén dữ liệu cache cho key ${cacheKey}:`, e); await this.cacheManager.del(cacheKey); } } httpAdapter.setHeader(response, 'X-Cache', 'MISS'); return next.handle().pipe((0, operators_1.tap)(async (data) => { if (!data) return; const jsonString = JSON.stringify(data); const ttl = 60000; if (jsonString.length > COMPRESSION_THRESHOLD) { try { const compressed = await gzip(Buffer.from(jsonString, 'utf8')); await this.cacheManager.set(cacheKey, compressed, ttl); console.log(`[Cache] 📦 Đã nén dữ liệu cho: ${cacheKey} (${jsonString.length} -> ${compressed.length} bytes)`); } catch (e) { console.error(`[Cache] Lỗi nén dữ liệu cho key ${cacheKey}:`, e); await this.cacheManager.set(cacheKey, jsonString, ttl); } } else { await this.cacheManager.set(cacheKey, jsonString, ttl); } })); } }; exports.CompressCacheInterceptor = CompressCacheInterceptor; exports.CompressCacheInterceptor = CompressCacheInterceptor = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(cache_manager_1.CACHE_MANAGER)), __metadata("design:paramtypes", [Object, core_1.HttpAdapterHost]) ], CompressCacheInterceptor); //# sourceMappingURL=compress-cache.interceptor.js.map