@keyv/compress-brotli

Brotli compression for Keyv

build codecov npm npm

Brotli compression for Keyv.

Brotli is a data compression algorithm that is designed to be fast and efficient.

Table of Contents

Install

npm install --save keyv @keyv/compress-brotli

Usage

import Keyv from 'keyv';
import KeyvBrotli from '@keyv/compress-brotli';

const keyv = new Keyv({store: new Map(), compression: new KeyvBrotli()});

API

@keyv/compress-brotli([options])

options

@keyv/compress-brotli uses Node's built-in node:zlib Brotli functions. Both options are optional:

  • compressOptions - BrotliOptions passed to zlib.brotliCompress.
  • decompressOptions - BrotliOptions passed to zlib.brotliDecompress.
import { constants } from 'node:zlib';

const brotli = new KeyvBrotli({
  compressOptions: { params: { [constants.BROTLI_PARAM_QUALITY]: 5 } },
});

Migrating to v6

  • KeyvBrotli implements the v6 KeyvCompressionAdapter interface: compress(value: string): Promise<string> and decompress(value: string): Promise<string>. Keyv passes it the whole serialized entry, and compress returns a base64 string.
  • The v5 serialize and deserialize methods and the per-call options argument of compress and decompress were removed. Pass options to the constructor instead.
  • The compress-brotli dependency was dropped in favor of node:zlib. The only options are compressOptions and decompressOptions; the old enable, serialize, deserialize and iltorb options are gone.
  • Values that Keyv v5 wrote with @keyv/compress-brotli can't be read by v6. v5 compressed only the value field inside the JSON envelope, so reading an old entry fails with a decompression error. Keyv emits error, and get returns undefined when an error listener is attached or rejects when none is. Treat a compressed store as a cache that v6 repopulates, or read the old entries with v5 and write them again with v6. See the v5 to v6 migration guide.
  • Compression needs serialization, which is on by default. With serialization: false, Keyv stores values without compressing them.

License

MIT © Jared Wray

Edit this page