diff --git a/packages/keyv/README.md b/packages/keyv/README.md
index ad33ab534..129db05eb 100644
--- a/packages/keyv/README.md
+++ b/packages/keyv/README.md
@@ -532,32 +532,20 @@ keyv.deserialize = value => parseInt(value);
console.log(keyv.deserialize); // value => parseInt(value)
```
-## .compress
+## .compression
-Type: `Function`
+Type: `CompressionAdapter`
Default: `undefined`
-A custom compression function used for any value. This is usually set by the compression adapters in on the constructor but can be set directly.
+this is the compression package to use. See [Compression](#compression) for more details. If it is undefined it will not compress (default).
```js
-const keyv = new Keyv();
-console.log(keyv.compress); // undefined
-keyv.compress = value => zlib.gzipSync(value);
-console.log(keyv.compress); // value => zlib.gzipSync(value)
-```
-
-## .decompress
-
-Type: `Function`
-Default: `undefined`
-
-A custom decompression function used for any value. This is usually set by the compression adapters in on the constructor but can be set directly.
+import KeyvGzip from '@keyv/compress-gzip';
-```js
const keyv = new Keyv();
-console.log(keyv.decompress); // undefined
-keyv.decompress = value => zlib.gunzipSync(value);
-console.log(keyv.decompress); // value => zlib.gunzipSync(value)
+console.log(keyv.compression); // undefined
+keyv.compression = new KeyvGzip();
+console.log(keyv.compression); // KeyvGzip
```
# How to Contribute
diff --git a/packages/keyv/src/index.ts b/packages/keyv/src/index.ts
index 2711cca1f..c701a4fac 100644
--- a/packages/keyv/src/index.ts
+++ b/packages/keyv/src/index.ts
@@ -108,8 +108,7 @@ export class Keyv extends EventManager {
private _serialize: CompressionAdapter['serialize'] = defaultSerialize;
private _deserialize: CompressionAdapter['deserialize'] = defaultDeserialize;
- private _compress: CompressionAdapter['compress'] | undefined;
- private _decompress: CompressionAdapter['decompress'] | undefined;
+ private _compression: CompressionAdapter | undefined;
/**
* Keyv Constructor
@@ -153,18 +152,15 @@ export class Keyv extends EventManager {
this._store = this.opts.store;
- const compression = this.opts.compression;
- if (this.opts.compression) {
- this.opts.serialize = compression.serialize.bind(compression);
- this.opts.deserialize = compression.deserialize.bind(compression);
+ this._compression = this.opts.compression;
+ if (this._compression) {
+ this.opts.serialize = this._compression.serialize.bind(this._compression);
+ this.opts.deserialize = this._compression.deserialize.bind(this._compression);
}
this._serialize = this.opts.serialize!;
this._deserialize = this.opts.deserialize!;
- this._compress = compression?.compress.bind(compression);
- this._decompress = compression?.decompress.bind(compression);
-
if (this.opts.namespace) {
this._namespace = this.opts.namespace;
}
@@ -234,34 +230,22 @@ export class Keyv extends EventManager {
/**
* Get the current compression function
- * @returns {CompressionAdapter['compress']} The current compression function
+ * @returns {CompressionAdapter} The current compression function
*/
- public get compress(): CompressionAdapter['compress'] | undefined {
- return this._compress;
+ public get compression(): CompressionAdapter | undefined {
+ return this._compression;
}
/**
* Set the current compression function
- * @param {CompressionAdapter['compress']} compress The compression function to set
- */
- public set compress(compress: CompressionAdapter['compress'] | undefined) {
- this._compress = compress;
- }
-
- /**
- * Get the current decompression function
- * @returns {CompressionAdapter['decompress']} The current decompression function
+ * @param {CompressionAdapter} compress The compression function to set
*/
- public get decompress(): CompressionAdapter['decompress'] | undefined {
- return this._decompress;
- }
-
- /**
- * Set the current decompression function
- * @param {CompressionAdapter['decompress']} decompress The decompression function to set
- */
- public set decompress(decompress: CompressionAdapter['decompress'] | undefined) {
- this._decompress = decompress;
+ public set compression(compress: CompressionAdapter | undefined) {
+ this._compression = compress;
+ if (this._compression) {
+ this.opts.serialize = this._compression.serialize.bind(this._compression);
+ this.opts.deserialize = this._compression.deserialize.bind(this._compression);
+ }
}
/**
diff --git a/packages/keyv/test/test.ts b/packages/keyv/test/test.ts
index 927c38dae..3a7cf3534 100644
--- a/packages/keyv/test/test.ts
+++ b/packages/keyv/test/test.ts
@@ -6,7 +6,7 @@ import KeyvMongo from '@keyv/mongo';
import KeyvBrotli from '@keyv/compress-brotli';
import KeyvGzip from '@keyv/compress-gzip';
import KeyvMemcache from '@keyv/memcache';
-import Keyv, {type KeyvStoreAdapter, type StoredDataNoRaw} from '../src/index.js';
+import Keyv, {type KeyvStoreAdapter, type StoredDataNoRaw, type CompressionAdapter} from '../src/index.js';
const keyvMemcache = new KeyvMemcache('localhost:11211');
@@ -745,16 +745,9 @@ test.it('Keyv does get and set on serialize / deserialize function', async t =>
test.it('Keyv can get and set the compress property', async t => {
const keyv = new Keyv();
- t.expect(keyv.compress).not.toBeDefined();
- const compressFunction = (value: any) => value;
- keyv.compress = compressFunction;
- t.expect(keyv.compress).toBe(compressFunction);
+ const gzip = new KeyvGzip();
+ t.expect(keyv.compression).not.toBeDefined();
+ keyv.compression = gzip as CompressionAdapter;
+ t.expect(keyv.compression).toBe(gzip);
});
-test.it('Keyv can get and set the decompress property', async t => {
- const keyv = new Keyv();
- t.expect(keyv.decompress).not.toBeDefined();
- const decompressFunction = async (value: any) => value;
- keyv.decompress = decompressFunction;
- t.expect(keyv.decompress).toBe(decompressFunction);
-});