Compress and encode data as URL-safe base64 string
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.
For gzip
compression and decompression, it uses the 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.
npm install --save base64-compressor
Encode/decode JSON-serializable JavaScript value
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)
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)