fflate

repository·master·Indexed 24 days ago

https://github.com/101arrowz/fflate

A high-performance, ultra-lightweight pure JavaScript library for DEFLATE, GZIP, and Zlib compression and decompression. Optimized for speed and minimal bundle size (approx. 8kB), it supports both browser and Node.js environments. Features include ZIP archiving, streaming support, asynchronous APIs using Web/Node Workers for non-blocking operations, and support for files up to 4GB.

Tokens
24.4K
Snippets
10
Records
227
Agent score
83%

What's inside fflate

  1. Overview of fflate

    master
    fflate (fast flate) is a high-performance, lightweight pure JavaScript library for compression and decompression. It supports DEFLATE, GZIP, and Zlib data formats. It is designed to be extremely small (approx. 8kB base bundle size) and fast, outperforming other libraries like pako or UZIP.js in both compression and decompression benchmarks. It is compatible with both browser and Node.js environments.
  2. Explore fflate API surface

    master

    fflate provides a comprehensive suite of tools for compression and decompression. The API is organized into several categories:

    • Classes: High-level objects for managing compression/decompression streams and processes (e.g., Zip, Gzip, AsyncDecompress, Unzip).
    • Interfaces: Configuration options for various operations (e.g., ZipOptions, GzipOptions, UnzipOptions) and error handling (FlateError).
    • Functions: Direct, functional access to compression and decompression algorithms (e.g., zip, unzip, gzip, gunzip, deflate, inflate).
    • Type Aliases: Callback and handler types for stream processing (e.g., UnzipFileHandler, FlateCallback).
    • Variables: Error codes for troubleshooting (FlateErrorCode).
  3. Load fflate via CDN in the browser

    master

    You can load fflate directly in the browser using UNPKG or jsDelivr. Note that tree shaking is not supported when using a CDN. For a buildless ESM approach, use Skypack.

    <!-- Using UNPKG or jsDelivr (choose one) -->
    <script src="https://unpkg.com/fflate@0.8.3"></script>
    <script src="https://cdn.jsdelivr.net/npm/fflate@0.8.3/umd/index.js"></script>
    <!-- The global variable 'fflate' will contain the library -->
    
    <!-- Using Skypack for ESM (buildless) -->
    <script type="module">
      import * as fflate from 'https://cdn.skypack.dev/fflate@0.8.3?min';
    </script>
  4. Import fflate in Node.js (CommonJS)

    master

    If your environment does not support ES Modules, use require. Note that in the browser, this will import all components, which is not recommended for bundle size.

    const fflate = require('fflate');
  5. Verify fflate performance and correctness

    master

    To validate that the module is working as expected, ensure outputs are within 5% of competitors at max compression, and generate performance metrics, run the test suite using npm:

    npm test

    Performance metrics are output to test/results. Note that the CLI completion time is not an accurate representation of package performance; check the JSON output for accurate measurements.

  6. Import fflate in Deno

    master

    When using Deno, import from Skypack and use the @deno-types comment to provide TypeScript typings.

    // @deno-types="https://cdn.skypack.dev/fflate@0.8.3/lib/index.d.ts"
    import * as fflate from 'https://cdn.skypack.dev/fflate@0.8.3?min';
  7. Compare fflate with other compression libraries

    master

    When choosing a compression library, consider these comparisons:

    • vs pako: fflate is up to 50% faster and significantly smaller (core build ~8kB vs 45kB). pako is a near line-for-line JavaScript port of Zlib and does not optimize for JavaScript engine performance.
    • vs tiny-inflate: tiny-inflate is smaller (3kB) but typically performs 40% worse than pako (and thus significantly worse than fflate).
    • vs UZIP.js: fflate is approximately 25% faster, supports GZIP and Zlib directly (whereas UZIP.js requires manual header removal), and is more robustly designed.
    • vs Native Zlib (Node.js): For Node.js environments, native Zlib bindings offer the best performance. fflate is roughly 30% slower in decompression and 10% slower in compression compared to native Zlib, but can achieve better compression ratios.
    • vs CompressionStream API (Browser): fflate is generally faster for data already in memory (including files dozens of MBs large) and for small files under 1MB due to lower marshalling overhead. fflate also provides control over compression levels and supports ZIP, which the native API does not.
  8. Use fflate without a bundler

    master

    If you are not using a bundler, you can import directly from the ESM files. For the browser, use fflate/esm/browser.js. For older Node.js versions, use fflate/esm.

    // For the browser
    import * as fflate from 'fflate/esm/browser.js';
    
    // For Node.js (if standard ESM import fails)
    import * as fflate from 'fflate/esm';
  9. Configure Unix permissions with ZipPassThrough.attrs

    master

    To set Unix permissions on a file within a ZIP archive using ZipPassThrough, you must set the attrs property by bit-shifting the desired permissions by 16.

    Important: For Unix permissions to work, you must also set the os property to 3 (Unix).

    Example for setting permissions to 0o644:

    stream.os = 3;
    stream.attrs = 0o644 << 16;