{"library":"minizlib","title":"Minizlib Compression Streams","description":"minizlib is a high-performance JavaScript library designed for stream-based compression and decompression using zlib, gzip, Brotli, and Zstd algorithms. It leverages Node.js's native zlib bindings for optimal speed, providing a synchronous streaming API built on top of the 'minipass' stream implementation. The current stable version is 3.1.0, actively maintained with a focus on efficiency. A key differentiator is its synchronous operation on the main thread, which minimizes overhead and offers immediate processing, making it suitable for CPU-bound tasks in scenarios where consistent asynchronous behavior of Node's core `stream.Transform` might introduce undesirable latency. Unlike Node.js's built-in `zlib` module, minizlib exclusively provides stream interfaces and does not offer convenience methods for buffer-to-buffer compression/decompression, requiring users to compose streams for such tasks. It was developed to meet the demanding requirements of projects like 'node-tar' and 'minipass-fetch'.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install minizlib"],"cli":null},"imports":["import { Deflate } from 'minizlib'","import { BrotliDecompress } from 'minizlib'","import { Gzip } from 'minizlib'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { BrotliDecompress } from 'minizlib';\nimport { Readable, Writable } from 'stream'; // Node.js built-in streams for simulation\n\n// Simulate a source of compressed data using a custom Readable stream\nclass CompressedDataSource extends Readable {\n  private chunks: Buffer[];\n  constructor(data: Buffer) {\n    super();\n    this.chunks = [data]; // In a real app, 'data' would be actual compressed content\n  }\n  _read() {\n    if (this.chunks.length > 0) {\n      this.push(this.chunks.shift());\n    } else {\n      this.push(null); // Signal end of stream\n    }\n  }\n}\n\n// Simulate a destination for decoded data using a custom Writable stream\nclass DecodedDataDestination extends Writable {\n  private receivedData: Buffer[] = [];\n  constructor() {\n    super();\n  }\n  _write(chunk: Buffer, encoding: string, callback: (error?: Error | null) => void) {\n    this.receivedData.push(chunk);\n    callback();\n  }\n  getData(): Buffer {\n    return Buffer.concat(this.receivedData);\n  }\n}\n\nasync function runDecompression() {\n  // For demonstration, we use a simple Brotli-compressed hex string.\n  // In a real application, this would come from a file, network, etc.\n  // Note: minizlib doesn't provide direct buffer compression, so this input is pre-compressed.\n  const compressedBrotliHex = 'C323040B001402000000A1302A0B50616464656420546869732069732073616D706C65206461746120746F20626520636F6D7072657373656420616E64207468656E206465636F6D70726573736564207573696E67206D696E697A6C69622E';\n  const mockCompressedBuffer = Buffer.from(compressedBrotliHex, 'hex');\n\n  const inputSource = new CompressedDataSource(mockCompressedBuffer);\n  const decompressStream = new BrotliDecompress();\n  const outputDestination = new DecodedDataDestination();\n\n  await new Promise<void>((resolve, reject) => {\n    inputSource.pipe(decompressStream).pipe(outputDestination)\n      .on('finish', () => {\n        console.log('Decompression finished successfully.');\n        console.log('Decoded data:', outputDestination.getData().toString('utf8'));\n        resolve();\n      })\n      .on('error', (err) => {\n        console.error('Decompression error:', err);\n        reject(err);\n      });\n  });\n}\n\nrunDecompression().catch(console.error);\n","lang":"typescript","description":"Demonstrates how to decompress a Brotli-compressed data stream using `minizlib`. It sets up a simulated readable stream for compressed input and a writable stream for decompressed output, then pipes data through `BrotliDecompress`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}