1.0.2 • Published 6 months ago
@shgysk8zer0/squish v1.0.2
@shgysk8zer0/squish
Tiny JS compression/decompression library using CompressionStream
Efficient Data Compression and Decompression
Squish is a tiny JavaScript library that provides functionalities for efficient data compression and decompression. It supports various input and output formats, making it a versatile tool for optimizing data transmission and storage.
Features
- Highly Compressed: The library itself is minified and compressed to an exceptionally small size, minimizing its footprint in your project.
- Versatile Input: Accepts a wide range of input data types, including
ReadableStream
,ArrayBuffer
,Uint8Array
,Blob
,Response
,Request
, and even strings. - Compression Algorithms: Supports both
gzip
anddeflate
compression algorithms, providing flexibility for different use cases. - Decompression: Decompresses data compressed with
gzip
anddeflate
algorithms. - Rich Output Formats: Offers a variety of output formats, including
blob
,stream
,response
,buffer
,bytes
,hex
,base64
,base64url
,url
, andtext
.
Installation
You can install the library using npm or by importing from a CDN such as unpkg.com
:
npm install `@shgysk8zer0/squish`
or with a <script type="importmap">
<script type="importmap">
{
"imports": {
"@shgysk8zer0/squish": "https://unpkg.com/@shgysk8zer0/squish@1.0.0/squish.min.js"
}
}
</script>
Usage
The library provides several functions for compression and decompression:
compress(data, options)
: Compresses the given data using the specified options.decompress(data, options)
: Decompresses the given data using the specified options.gzip(data, options)
: Compresses the data using the gzip algorithm.gunzip(data, options)
: Decompresses the data compressed with gzip.deflate(data, options)
: Compresses the data using the deflate algorithm.inflate(data, options)
: Decompresses the data compressed with deflate.
Usage Example
Basic usage
import { gzip, gunzip } from '@shgysk8zer0/squish';
const compressed = await gzip('Hello, World!');
const decompressed = await gunzip(compressed, { output: 'text' });
Node usage (CJS - ESM also supported)
const { inflate } = require('@shgysk8zer0/squish');
const { readFile } = require('node:js');
async function sendFile(file) {
const buffer = readFile(file);
const compressed = await inflate(buffer);
return new Response(compressed);
}
Requirements
This library requires many new features that are not yet wideley supported. A polyfill is provided in node, but you may want to proved your own for certain usages in browsers.