packfs-core
Version:
Semantic filesystem operations for LLM agent frameworks with natural language understanding. See LLM_AGENT_GUIDE.md for copy-paste examples.
104 lines • 3.7 kB
JavaScript
import { CompressionStrategy } from './CompressionStrategy';
import { compress as zstdCompress, decompress as zstdDecompress } from '@mongodb-js/zstd';
/**
* Zstd compression strategy - provides excellent balance between compression ratio and speed
*/
export class ZstdStrategy extends CompressionStrategy {
constructor(compressionLevel = 3) {
super();
this.name = 'zstd';
this.priority = 'balanced';
this.supportsStreaming = true;
this.compressionLevel = compressionLevel;
}
async compress(data, hints) {
const startTime = performance.now();
// Use real Zstd compression
const level = this.getCompressionLevel(hints);
const compressed = await zstdCompress(data, level);
const compressionTime = performance.now() - startTime;
return {
data: compressed,
algorithm: this.name,
originalSize: data.length,
compressedSize: compressed.length,
metadata: {
compressionTime,
level,
dictionary: hints.ecosystem ? `${hints.ecosystem}-dict` : undefined
}
};
}
async decompress(chunk) {
const startTime = performance.now();
// Use real Zstd decompression
const result = await zstdDecompress(chunk.data);
const decompressionTime = performance.now() - startTime;
chunk.metadata.decompressionTime = decompressionTime;
return result;
}
createDecompressor(chunk) {
const { Readable } = require('stream');
return new Readable({
async read() {
try {
// Use real Zstd decompression
const decompressed = await zstdDecompress(chunk.data);
this.push(decompressed);
this.push(null);
}
catch (error) {
this.emit('error', error);
}
}
});
}
estimateRatio(_data, hints) {
if (this.isStructuredData(hints.mimeType)) {
return 0.2; // 80% compression for JSON, config files
}
if (this.isCodeFile(hints.mimeType)) {
return 0.3; // 70% compression for source code
}
return 0.5; // 50% compression for other data
}
shouldUse(data, hints) {
// Excellent for structured data (JSON, YAML, etc.)
if (this.isStructuredData(hints.mimeType)) {
return true;
}
// Good balance for most use cases
if (hints.accessFrequency >= 0.3 && hints.accessFrequency < 0.8) {
return true;
}
// Great for medium-sized files
if (data.length > 10 * 1024 && data.length < 1024 * 1024) {
return true;
}
return false;
}
getCompressionLevel(hints) {
if (hints.isHot) {
return 1; // Fastest compression
}
if (hints.accessFrequency >= 0.5) {
return 3; // Balanced
}
return Math.min(19, this.compressionLevel + 5); // Higher compression for cold data
}
isStructuredData(mimeType) {
return mimeType.includes('json') ||
mimeType.includes('yaml') ||
mimeType.includes('toml') ||
mimeType.includes('xml') ||
mimeType.endsWith('.config');
}
isCodeFile(mimeType) {
return mimeType.includes('javascript') ||
mimeType.includes('typescript') ||
mimeType.includes('python') ||
mimeType.includes('rust') ||
mimeType.includes('go');
}
}
//# sourceMappingURL=ZstdStrategy.js.map