compress-utils
WASM compression bindings for zstd, brotli, zlib, bz2, lz4, xz — tree-shakeable per-algorithm imports.
Last updated 6 months ago by dupontcyborg .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install compress-utils 
SYNC missed versions from official npm registry.

compress-utils

npm version npm downloads types: built-in License: MIT

Unified WebAssembly bindings for eight compression algorithms: Brotli, bzip2, LZ4, XZ/LZMA, zlib, Zstandard, Snappy, and gzip. Same API for every algorithm, in every JavaScript runtime, with tree-shakeable imports so bundlers ship only what you use.

Installation

npm install compress-utils
# or: pnpm add / yarn add / bun add / deno add npm:compress-utils

CDN (no bundler)

The package is published as ESM. You can pull it straight from a CDN — no install step, no build step.

<script type="module">
    import { compress, decompress } from 
      "https://esm.sh/compress-utils/zstd";

    const out = await compress(
      new TextEncoder().encode("hello")
      );
</script>
CDN Per-algo URL
esm.sh https://esm.sh/compress-utils/<algo>
jsDelivr https://cdn.jsdelivr.net/npm/compress-utils/dist/algorithms/<algo>/index.js
unpkg https://unpkg.com/compress-utils/dist/algorithms/<algo>/index.js

Quick start

Each algorithm is its own subpath import. Pick the one you need; the rest stay out of your bundle.

import { compress, decompress } from "compress-utils/zstd";

const input = new TextEncoder().encode("the quick brown fox " .repeat(50));

const compressed = await compress(input, { level: 9 });
const restored   = await decompress(compressed);

Available subpaths: compress-utils/zstd, /brotli, /zlib, /bz2, /lz4, /xz, /snappy, /gzip. Identical surface across all eight. Each also has /<algo>/decompress and /<algo>/compress variants that ship only one direction for a much smaller .wasm — see Bundle size.

Web Streams

import { compressionStream, decompressionStream } from "compress-utils/zstd";

await fetch("/big-file.json")
    .then(r => r.body!.pipeThrough(compressionStream({ level: 3 })))
    .then(s => s.pipeTo(uploadDestination));

Manual streaming (with using for auto-cleanup)

import { createCompressStream } from "compress-utils/brotli";

using cs = await createCompressStream({ level: 5 });
const chunks: Uint8Array[] = [];
for (const chunk of input) chunks.push(cs.write(chunk));
chunks.push(cs.finish());
// cs is destroyed automatically on scope exit (TS 5.2+ / Node 22+).

If using isn't available in your toolchain, call cs.destroy() explicitly — or rely on the GC backstop (a FinalizationRegistry frees the C-side handle eventually).

Supported algorithms

Algorithm Subpath Wire format produced
zlib compress-utils/zlib zlib wrapper (RFC 1950)
bzip2 compress-utils/bz2 bzip2 stream
XZ/LZMA compress-utils/xz XZ stream with CRC64
LZ4 compress-utils/lz4 LZ4 frame (compatible with .lz4 files)
Zstandard compress-utils/zstd ZSTD frame with content size
Brotli compress-utils/brotli raw Brotli stream
Snappy compress-utils/snappy Raw Snappy block
gzip compress-utils/gzip gzip stream (RFC 1952)
LZO compress-utils/lzo/decompress Raw LZO1X block (decompress only; pass {expectedSize} as output capacity)

Imports are independent — import "compress-utils/zstd" and import "compress-utils/brotli" pull in two separate .wasm modules, not a combined bundle. Files marked "sideEffects": false so unused exports are tree-shaken aggressively. For per-module .wasm sizes (and the smaller decode-only / encode-only builds), see Bundle size.

Bundle size

Importing compress-utils/<algo> gives you the full codec — compress and decompress. If you only need one direction (a browser that just decompresses fetched assets is the common case), import a directional subpath and the other half is dead-stripped from the .wasm entirely:

// decoder only, 90 KB
import { decompress } from "compress-utils/zstd/decompress"; 

// or, encoder only:
import { compress }   from "compress-utils/zstd/compress";
  • /decompress exposes decompress, createDecompressStream, decompressionStream, setMaxDecompressedSize, version.
  • /compress exposes compress, createCompressStream, compressionStream, version.
  • The default subpath keeps both

Comparison of the gzipped sizes:

algo full decompress-only compress-only
zstd 128K 36K 106K
brotli 169K 84K 154K
lz4 39K 15K 35K
bz2 33K 22K 25K
xz 62K 43K 51K
zlib 33K 25K 26K
snappy 36K 32K 28K
gzip 80K 52K 60K

The .wasm is the bulk of what ships; the JS glue is a few KB, shared, and tree-shaken to the methods you import.

Compression levels

Every algorithm accepts a level from 1 (fastest) to 10 (smallest). The binding maps that to each algorithm's native range internally — you don't need to know that Zstandard goes 1–22 or zlib goes 1–9. Defaults to 5.

await compress(input, { level: 1 });   // fastest
await compress(input, { level: 10 });  // smallest

Runtime support

Tested in CI on every release:

Runtime Status
Node 20, 22, 24 ✅
Bun ✅
Deno 2.x ✅
Chromium / Firefox / WebKit (Playwright) ✅

TypeScript

The package is TypeScript-first. .d.ts files ship alongside the JS for every subpath; no @types/ package needed.

import { compress, type CompressOptions, type CompressError } from "compress-utils/zstd";

const opts: CompressOptions = { level: 7 };
const compressed: Uint8Array = await compress(input, opts);

Preloading and synchronous calls

Each algorithm and direction-specific subpath exports preload(). It loads and initializes that subpath's WASM module once; concurrent calls share initialization. A failed initialization can be retried. No codec work is performed by preload.

import { preload, compressSync, decompressSync } from "compress-utils/zlib";
await preload();
const compressed = compressSync(new TextEncoder().encode("hello"));
const restored = decompressSync(compressed);

compressSync, decompressSync, createCompressStreamSync, and createDecompressStreamSync are available for the directions exposed by the subpath. They throw with a preload() instruction before initialization completes. Existing asynchronous APIs initialize automatically and also enable subsequent synchronous calls. Initializing one subpath does not initialize another: encoder and decoder subpaths use different WASM modules. Sync calls block the calling thread; use a worker for expensive operations.

License

MIT. See LICENSE.


Built by Nico Dupont.

Current Tags

  • 0.12.1                                ...           latest (15 days ago)

11 Versions

  • 0.12.1                                ...           15 days ago
  • 0.12.0                                ...           a month ago
  • 0.11.2                                ...           a month ago
  • 0.11.1                                ...           a month ago
  • 0.11.0                                ...           a month ago
  • 0.10.1                                ...           a month ago
  • 0.9.0                                ...           2 months ago
  • 0.8.0                                ...           4 months ago
  • 0.7.1                                ...           6 months ago
  • 0.7.0                                ...           6 months ago
  • 0.7.0-rc.1                                ...           6 months ago
Maintainers (1)
Downloads
Total 13
Today 0
This Week 0
This Month 10
Last Day 0
Last Week 10
Last Month 3
Dependencies (0)
None
Dev Dependencies (6)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE