← Back
antfu

antfu/lz-string-es

View on GitHub ↗
Stars
5
Forks
0
Watchers
5
Open issues
0
Contributors
1
Language
TypeScript
License
MIT License
Default branch
main
Created Oct 1, 2026Updated Oct 1, 2026

Star growth

Today—
This week—
This month—

Star history will appear here once this repo has been tracked for a couple of days.

README

lz-string-es

npm version npm downloads bundle JSDocs License

An ESM-only build of lz-string, the LZ-based compression library for JavaScript.

About

This package is not a fork or a rewrite. The original pieroxy/lz-string repository is included as a git submodule in vendor/lz-string and stays the single source of truth for the algorithm. src/index.ts only re-exports its APIs, and tsdown recompiles and bundles them from that TypeScript source into a modern distribution:

  • ESM only
  • Named exports only (no default export), tree-shakable, sideEffects: false
  • Bundled type declarations
  • No Node.js dependency: the loadBinaryFile / saveBinaryFile helpers and the CLI are not included

Any fix or change to the compression logic belongs upstream.

Install

npm i lz-string-es

Usage

import { compressToBase64, decompressFromBase64 } from 'lz-string-es'

const compressed = compressToBase64('Hello, Hello, Hello, Hello!')
const text = decompressFromBase64(compressed)

The API is the same as lz-string: compress, compressToBase64, compressToUTF16, compressToUint8Array, compressToEncodedURIComponent, compressToCustom, and their decompress* counterparts. See the upstream documentation for details.

Usage as an object

As lz-string-es no longer ships a default export, you need to import it as an object to access all its functions.

import * as lz from 'lz-string-es'

const compressed = lz.compressToBase64('Hello, Hello, Hello, Hello!')
const text = lz.decompressFromBase64(compressed)

loadBinaryFile / saveBinaryFile

The loadBinaryFile and saveBinaryFile helpers are not included in this package, as it has no Node.js dependency. You can implement them with:

import { readFileSync, writeFileSync } from 'node:fs'
import { convertFromUint8Array, convertToUint8Array } from 'lz-string-es'

export function saveBinaryFile(fileName: PathOrFileDescriptor, data: string | Uint8Array) {
  writeFileSync(fileName, typeof data === 'string' ? convertToUint8Array(data)! : data, null)
}

export function loadBinaryFile(fileName: PathOrFileDescriptor) {
  return convertFromUint8Array(readFileSync(fileName, null))
}

Development

git clone --recurse-submodules https://github.com/antfu/lz-string-es.git
pnpm install # also runs `git submodule update --init` via the prepare script
pnpm build

To update to the latest upstream:

git -C vendor/lz-string pull origin master
git add vendor/lz-string

Credits

All credit for the compression algorithm and its implementation goes to Pieroxy and the lz-string contributors. The algorithm is based on LZ compression; this package only changes how it is distributed.

## Sponsors

Sponsors

License

MIT License

  • © 2013 Pieroxy, original lz-string
  • © 2025-PRESENT Anthony Fu, ESM distribution