UNPKG

packfs-core

Version:

Semantic filesystem operations for LLM agent frameworks with natural language understanding. See LLM_AGENT_GUIDE.md for copy-paste examples.

110 lines 4.11 kB
import * as zlib from 'zlib'; import { promisify } from 'util'; import { CompressionStrategy } from './CompressionStrategy'; const brotliCompress = promisify(zlib.brotliCompress); const brotliDecompress = promisify(zlib.brotliDecompress); /** * Brotli compression strategy - excellent for text-based files like JavaScript */ export class BrotliStrategy extends CompressionStrategy { constructor(dictionary) { super(); this.name = 'brotli'; this.priority = 'size'; this.supportsStreaming = true; this.dictionary = dictionary; } async compress(data, hints) { const startTime = performance.now(); const options = { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: this.getQualityLevel(hints), [zlib.constants.BROTLI_PARAM_SIZE_HINT]: data.length, } }; // Use dictionary for JavaScript files if (this.dictionary && this.isJavaScript(hints.mimeType)) { options.params[zlib.constants.BROTLI_PARAM_LARGE_WINDOW] = 1; } const compressed = await brotliCompress(data, options); const compressionTime = performance.now() - startTime; return { data: compressed, algorithm: this.name, originalSize: data.length, compressedSize: compressed.length, dictionary: this.dictionary ? 'js-patterns' : undefined, metadata: { compressionTime, quality: options.params[zlib.constants.BROTLI_PARAM_QUALITY], mimeType: hints.mimeType } }; } async decompress(chunk) { const startTime = performance.now(); const result = await brotliDecompress(chunk.data); const decompressionTime = performance.now() - startTime; // Update metadata with decompression time chunk.metadata.decompressionTime = decompressionTime; return result; } createDecompressor(chunk) { const { Readable } = require('stream'); const decompressor = zlib.createBrotliDecompress(); // Create a readable stream from the compressed data const inputStream = new Readable({ read() { } }); // Push the compressed data and signal end inputStream.push(chunk.data); inputStream.push(null); // Pipe through the decompressor return inputStream.pipe(decompressor); } estimateRatio(_data, hints) { // Brotli performs exceptionally well on text data if (this.isTextFile(hints.mimeType)) { return this.dictionary ? 0.15 : 0.25; // 85% or 75% compression } // Less effective on binary data return 0.6; // 40% compression } shouldUse(data, hints) { // Perfect for JavaScript, CSS, HTML, JSON if (this.isTextFile(hints.mimeType)) { return true; } // Good for large files where compression ratio matters more than speed if (data.length > 100 * 1024 && !hints.isHot) { return true; } return false; } getQualityLevel(hints) { if (hints.isHot) { return 4; // Fast compression for frequently accessed files } if (hints.accessFrequency > 0.8) { return 6; // Balanced for moderately accessed files } return 11; // Maximum compression for rarely accessed files } isJavaScript(mimeType) { return mimeType.includes('javascript') || mimeType.includes('typescript') || mimeType.endsWith('.js') || mimeType.endsWith('.ts') || mimeType.endsWith('.jsx') || mimeType.endsWith('.tsx'); } isTextFile(mimeType) { return mimeType.startsWith('text/') || mimeType.includes('javascript') || mimeType.includes('json') || mimeType.includes('css') || mimeType.includes('html') || mimeType.includes('xml'); } } //# sourceMappingURL=BrotliStrategy.js.map