https://github.com/eliot-akira/base64-compressor
Compress and encode data as URL-safe base64 string
https://github.com/eliot-akira/base64-compressor
base64 base64url compression-stream gzip url
Last synced: about 1 year ago
JSON representation
Compress and encode data as URL-safe base64 string
- Host: GitHub
- URL: https://github.com/eliot-akira/base64-compressor
- Owner: eliot-akira
- License: other
- Created: 2023-08-12T01:16:33.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-14T16:52:04.000Z (over 2 years ago)
- Last Synced: 2025-02-28T04:45:07.962Z (over 1 year ago)
- Topics: base64, base64url, compression-stream, gzip, url
- Language: TypeScript
- Homepage: https://eliot-akira.github.io/base64-compressor/
- Size: 184 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# Base64 Compressor
> Compress and encode data as URL-safe base64 string

## Why
It can be useful to encode data, such as application state or binary file, into a string that is compressed and safe to use in URL query string or hash, as well as in JSON.
## How
For `gzip` compression and decompression, it uses the [Compression Streams API](https://developer.mozilla.org/en-US/docs/Web/API/Compression_Streams_API), well-supported by browsers and on server side.
Binary to text encoding is done according to the specification for [Base 64 Encoding with URL and Filename Safe Alphabet](https://datatracker.ietf.org/doc/html/rfc4648#section-5).
## Install
```sh
npm install --save base64-compressor
```
## Usage
Encode/decode JSON-serializable JavaScript value
```ts
import { encode, decode } from 'base64-compressor'
const object = {
key: 'value'
}
const base64string = await encode(object)
const decoded = await decode(base64string)
assert.deepEqual(decoded, object)
```
Encode/decode binary (array buffer)
```ts
import { encodeBinary, decodeBinary } from 'base64-compressor'
const buffer = new ArrayBuffer(8)
const base64string = await encodeBinary(buffer)
const decoded = await decodeBinary(base64string)
assert.deepEqual(decoded, buffer)
```