Zstandard (zstd) Compression Node-API Bindings

0.0.12 · active · verified Tue Apr 21

This package, `zstd-napi`, provides high-performance TypeScript bindings to the native Zstandard (zstd) compression library, utilizing the Node-API (N-API) for native addon interfacing. As of its latest stable version, `0.0.12`, the library emphasizes reliability and performance, having been proven in production environments with invocation rates exceeding a million times per second. While a specific release cadence isn't explicitly stated, the frequent minor version bumps (0.0.3 to 0.0.12 in recent history) suggest active development. Key differentiators include comprehensive exposure of the stable Zstandard API, supporting advanced features like dictionary compression and multithreading, and offering both high-level and low-level APIs. It ships with pre-built binaries for common platforms and provides robust Node.js version support, including current and active LTS releases, making it a reliable choice for high-throughput compression tasks in Node.js applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic Zstandard compression and decompression using `zstd-napi`'s high-level API, including writing and reading compressed data to a temporary file, and showing how to apply a specific compression level.

import * as zstd from 'zstd-napi';
import { readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { tmpdir } from 'os';

async function runCompressionExample() {
  const originalData = Buffer.from('This is a test string that will be compressed using Zstandard. It is relatively short but should demonstrate the basic functionality of the zstd-napi library.', 'utf8');
  console.log(`Original data length: ${originalData.length} bytes`);

  try {
    // High-level compress API
    const compressedData = zstd.compress(originalData);
    console.log(`Compressed data length: ${compressedData.length} bytes`);

    // High-level decompress API
    const decompressedData = zstd.decompress(compressedData);
    console.log(`Decompressed data length: ${decompressedData.length} bytes`);

    if (decompressedData.equals(originalData)) {
      console.log('Decompression successful: Data matches original.');
    } else {
      console.error('Decompression failed: Data mismatch!');
    }

    // Example with compression level (default is 3)
    const compressedDataLevel10 = zstd.compress(originalData, 10);
    console.log(`Compressed data (level 10) length: ${compressedDataLevel10.length} bytes`);
    
    // Demonstrate writing/reading to a file
    const tempFilePath = join(tmpdir(), `test_data.zst`);
    writeFileSync(tempFilePath, compressedData);
    console.log(`Compressed data written to: ${tempFilePath}`);

    const readCompressedData = readFileSync(tempFilePath);
    const reDecompressedData = zstd.decompress(readCompressedData);
    if (reDecompressedData.equals(originalData)) {
        console.log('File-based decompression successful.');
    } else {
        console.error('File-based decompression failed: Data mismatch!');
    }

  } catch (error) {
    console.error('An error occurred during compression/decompression:', error);
  }
}

runCompressionExample();

view raw JSON →