SkillAgentSearch skills...

Zlib

Pure javascript implementation of Zlib nodejs core module.The zlib module provides compression functionality implemented using Gzip and Deflate/Inflate.

Install / Use

/learn @kpanuragh/Zlib
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

react-zlib-js

npm version License: MIT

Pure JavaScript implementation of Node.js zlib module for React Native, browsers, and JavaScript environments where native modules are not available.

Compatible with Node.js 22.x LTS API

Table of Contents

Features

| Feature | Description | Node.js Version | |---------|-------------|-----------------| | Gzip/Gunzip | Standard gzip compression | All | | Deflate/Inflate | Raw deflate with zlib header | All | | DeflateRaw/InflateRaw | Raw deflate without header | All | | Unzip | Auto-detect and decompress | All | | Brotli | Modern compression, better ratios | 11.7.0+ | | CRC32 | Checksum calculation | 22.2.0+ | | Stream Support | Node.js Transform streams | All | | Sync & Async APIs | Both synchronous and callback | All |

Installation

npm install react-zlib-js

Or with yarn:

yarn add react-zlib-js

Quick Start

const zlib = require('react-zlib-js');

// ============================================
// Gzip Compression
// ============================================

// Async
zlib.gzip('Hello, World!', (err, compressed) => {
  if (err) throw err;

  zlib.gunzip(compressed, (err, decompressed) => {
    console.log(decompressed.toString()); // 'Hello, World!'
  });
});

// Sync
const compressed = zlib.gzipSync('Hello, World!');
const decompressed = zlib.gunzipSync(compressed);
console.log(decompressed.toString()); // 'Hello, World!'

// ============================================
// Brotli Compression (Better compression ratio)
// ============================================

const brotliCompressed = zlib.brotliCompressSync('Hello, World!');
const brotliDecompressed = zlib.brotliDecompressSync(brotliCompressed);
console.log(brotliDecompressed.toString()); // 'Hello, World!'

// ============================================
// CRC32 Checksum
// ============================================

const checksum = zlib.crc32('Hello, World!');
console.log(checksum); // 3957769958

Promise-Based API

All async methods now support both promises and callbacks, allowing you to use modern async/await syntax without manual promise wrapping.

Using async/await

const zlib = require('react-zlib-js');

async function compress() {
  try {
    const data = 'Hello, World!';

    // Gzip compression with async/await
    const compressed = await zlib.gzip(data);
    const decompressed = await zlib.gunzip(compressed);
    console.log(decompressed.toString()); // 'Hello, World!'

    // With compression level
    const fast = await zlib.gzip(data, { level: 1 });
    const best = await zlib.gzip(data, { level: 9 });

    // Brotli compression
    const brotli = await zlib.brotliCompress(data);
    const unbrotli = await zlib.brotliDecompress(brotli);

  } catch (err) {
    console.error('Compression failed:', err);
  }
}

compress();

Both patterns work together

You can mix promises and callbacks in the same codebase:

const zlib = require('react-zlib-js');

async function mixedPatterns() {
  // Promise pattern with async/await
  const compressed = await zlib.gzip('data');

  // Callback pattern still works
  zlib.gzip('data', (err, result) => {
    if (err) console.error(err);
    else console.log(result);
  });

  // Mix both patterns in the same code
  try {
    const compressed = await zlib.gzip('data');
    zlib.deflate('data', (err, result) => {
      // both work together seamlessly
    });
  } catch (err) {
    console.error(err);
  }
}

mixedPatterns();

API Reference

Convenience Methods (Async)

All async methods follow the pattern: method(buffer[, options], callback)

// Callback signature: (error, result) => void

| Method | Description | |--------|-------------| | zlib.gzip(buffer[, options], callback) | Compress using Gzip | | zlib.gunzip(buffer[, options], callback) | Decompress Gzip | | zlib.deflate(buffer[, options], callback) | Compress using Deflate | | zlib.inflate(buffer[, options], callback) | Decompress Deflate | | zlib.deflateRaw(buffer[, options], callback) | Compress using raw Deflate (no header) | | zlib.inflateRaw(buffer[, options], callback) | Decompress raw Deflate | | zlib.unzip(buffer[, options], callback) | Auto-detect and decompress (gzip or deflate) | | zlib.brotliCompress(buffer[, options], callback) | Compress using Brotli | | zlib.brotliDecompress(buffer[, options], callback) | Decompress Brotli |

Example:

zlib.gzip('Hello', { level: 9 }, (err, result) => {
  if (err) {
    console.error('Compression failed:', err);
    return;
  }
  console.log('Compressed size:', result.length);
});

Convenience Methods (Sync)

All sync methods follow the pattern: methodSync(buffer[, options])

| Method | Description | |--------|-------------| | zlib.gzipSync(buffer[, options]) | Compress using Gzip | | zlib.gunzipSync(buffer[, options]) | Decompress Gzip | | zlib.deflateSync(buffer[, options]) | Compress using Deflate | | zlib.inflateSync(buffer[, options]) | Decompress Deflate | | zlib.deflateRawSync(buffer[, options]) | Compress using raw Deflate | | zlib.inflateRawSync(buffer[, options]) | Decompress raw Deflate | | zlib.unzipSync(buffer[, options]) | Auto-detect and decompress | | zlib.brotliCompressSync(buffer[, options]) | Compress using Brotli | | zlib.brotliDecompressSync(buffer[, options]) | Decompress Brotli |

Example:

try {
  const compressed = zlib.gzipSync('Hello', { level: 9 });
  const decompressed = zlib.gunzipSync(compressed);
  console.log(decompressed.toString());
} catch (err) {
  console.error('Error:', err);
}

Stream Factory Methods

Create Transform streams for piping data.

| Method | Description | |--------|-------------| | zlib.createGzip([options]) | Create Gzip compression stream | | zlib.createGunzip([options]) | Create Gzip decompression stream | | zlib.createDeflate([options]) | Create Deflate compression stream | | zlib.createInflate([options]) | Create Deflate decompression stream | | zlib.createDeflateRaw([options]) | Create raw Deflate compression stream | | zlib.createInflateRaw([options]) | Create raw Deflate decompression stream | | zlib.createUnzip([options]) | Create auto-detect decompression stream | | zlib.createBrotliCompress([options]) | Create Brotli compression stream | | zlib.createBrotliDecompress([options]) | Create Brotli decompression stream |

Example:

const gzip = zlib.createGzip();
const gunzip = zlib.createGunzip();

// Pipe data through compression and decompression
inputStream.pipe(gzip).pipe(gunzip).pipe(outputStream);

Stream Classes

Direct class constructors (also available via factory methods).

| Class | Description | |-------|-------------| | zlib.Gzip | Gzip compression class | | zlib.Gunzip | Gzip decompression class | | zlib.Deflate | Deflate compression class | | zlib.Inflate | Deflate decompression class | | zlib.DeflateRaw | Raw Deflate compression class | | zlib.InflateRaw | Raw Deflate decompression class | | zlib.Unzip | Auto-detect decompression class | | zlib.BrotliCompress | Brotli compression class | | zlib.BrotliDecompress | Brotli decompression class |

Example:

const gzip = new zlib.Gzip({ level: 9 });

Utility Functions

zlib.crc32(data[, value])

Computes a 32-bit CRC checksum (IEEE CRC-32 polynomial).

Parameters:

  • data <string> | <Buffer> | <TypedArray> | <DataView> - Input data
  • value <number> - Optional starting CRC value for chaining (default: 0)

Returns: <number> - 32-bit unsigned integer

Example:

// Basic usage
const crc = zlib.crc32('hello');
console.log(crc);  // 907060870

// With Buffer
const crc2 = zlib.crc32(Buffer.from('hello'));
console.log(crc2); // 907060870

// Chained CRC32 for streaming
let runningCrc = zlib.crc32('hello');
runningCrc = zlib.crc32(' world', runningCrc);
console.log(runningCrc); // Combined checksum

Options

Zlib Options (Gzip, Deflate, etc.)

{
  // Flush behavior during compression
  flush: zlib.Z_NO_FLUSH,

  // Flush behavior at end of stream
  finishFlush: zlib.Z_FINISH,

  // Internal buffer size (default: 16KB)
  chunkSize: 16 * 1024,

  // Window size bits: 8-15 (default: 15)
  // Higher = better compression, more memory
  windowBits: 15,

  // Compression level: -1 to 9
  // -1 = default, 0 = none, 1 = fastest, 9 = best
  level: zlib.Z_DEFAULT_COMPRESSION,

  // Memory usage level: 1-9 (default: 8)
  // Higher = faster, more memory
  memLevel: 8,

  // Compression strategy
  strategy: zlib.Z_DEFAULT_STRATEGY,

  // Preset dictionary for compression
  dictionary: Buffer
}

Compression Level Examples:

// No compression (fastest, largest output)
zlib.deflateSync(data, { level: zlib.Z_NO_COMPRESSION });

// Best speed (level 1)
zlib.deflateSync(data, { leve
View on GitHub
GitHub Stars19
CategoryDevelopment
Updated8d ago
Forks12

Languages

JavaScript

Security Score

80/100

Audited on Mar 24, 2026

No findings