mem100x
Version:
⚡ The FASTEST MCP memory server ever built - 66k+ entities/sec with intelligent context detection
105 lines • 3.65 kB
JavaScript
;
/**
* High-Performance Compression Utilities
* Using built-in zlib for observation compression
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompressionUtils = void 0;
const zlib_1 = require("zlib");
const fast_json_js_1 = require("./fast-json.js");
class CompressionUtils {
static MIN_COMPRESS_LENGTH = 100; // Don't compress small strings
static COMPRESSION_VERSION = 1;
/**
* Compress observations array to save space
* Only compresses if it saves space
*/
static compressObservations(observations) {
const json = (0, fast_json_js_1.stringifyObservations)(observations);
// Skip compression for small data
if (json.length < this.MIN_COMPRESS_LENGTH) {
return json;
}
try {
// Compress individual large observations
const processedObservations = observations.map((obs, index) => {
if (obs.length > this.MIN_COMPRESS_LENGTH) {
const compressed = (0, zlib_1.gzipSync)(obs);
const base64 = compressed.toString('base64');
if (base64.length < obs.length) {
return `COMPRESSED:${this.COMPRESSION_VERSION}:${base64}`;
}
}
return obs;
});
return (0, fast_json_js_1.stringifyObservations)(processedObservations);
}
catch (error) {
// Fall back to uncompressed on error
console.error('Compression error:', error);
}
return json;
}
/**
* Decompress observations if compressed
*/
static decompressObservations(data) {
const observations = (0, fast_json_js_1.parseObservations)(data);
return observations.map((obs) => {
if (obs.startsWith('COMPRESSED:')) {
try {
// Handle versioned format: COMPRESSED:1:base64data
const parts = obs.split(':');
if (parts.length >= 3) {
const base64 = parts.slice(2).join(':'); // Handle colons in base64
const compressed = Buffer.from(base64, 'base64');
return (0, zlib_1.gunzipSync)(compressed).toString();
}
}
catch (error) {
console.error('Decompression error:', error);
return obs; // Return original if decompression fails
}
}
return obs;
});
}
/**
* Calculate compression ratio
*/
static getCompressionRatio(original, compressed) {
return compressed.length / original.length;
}
/**
* Check if text should be compressed
*/
static shouldCompress(text) {
return text.length > this.MIN_COMPRESS_LENGTH;
}
/**
* Compress a single string
*/
static compress(text) {
try {
const compressed = (0, zlib_1.gzipSync)(text);
return compressed.toString('base64');
}
catch (error) {
throw new Error(`Compression failed: ${error}`);
}
}
/**
* Decompress a single string
*/
static decompress(compressed) {
try {
const buffer = Buffer.from(compressed, 'base64');
return (0, zlib_1.gunzipSync)(buffer).toString();
}
catch (error) {
throw new Error(`Decompression failed: ${error}`);
}
}
}
exports.CompressionUtils = CompressionUtils;
//# sourceMappingURL=compression.js.map