diff --git a/packages/keyv/README.md b/packages/keyv/README.md index f1d4de7d..ad33ab53 100644 --- a/packages/keyv/README.md +++ b/packages/keyv/README.md @@ -532,6 +532,34 @@ keyv.deserialize = value => parseInt(value); console.log(keyv.deserialize); // value => parseInt(value) ``` +## .compress + +Type: `Function`
+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. + +```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. + +```js +const keyv = new Keyv(); +console.log(keyv.decompress); // undefined +keyv.decompress = value => zlib.gunzipSync(value); +console.log(keyv.decompress); // value => zlib.gunzipSync(value) +``` + # How to Contribute We welcome contributions to Keyv! 🎉 Here are some guides to get you started with contributing: diff --git a/packages/keyv/src/index.ts b/packages/keyv/src/index.ts index b4a55cac..2711cca1 100644 --- a/packages/keyv/src/index.ts +++ b/packages/keyv/src/index.ts @@ -108,6 +108,9 @@ 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; + /** * Keyv Constructor * @param {KeyvStoreAdapter | KeyvOptions | Map} store to be provided or just the options @@ -150,8 +153,8 @@ export class Keyv extends EventManager { this._store = this.opts.store; + const compression = this.opts.compression; if (this.opts.compression) { - const compression = this.opts.compression; this.opts.serialize = compression.serialize.bind(compression); this.opts.deserialize = compression.deserialize.bind(compression); } @@ -159,6 +162,9 @@ export class Keyv extends EventManager { 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; } @@ -226,6 +232,38 @@ export class Keyv extends EventManager { } } + /** + * Get the current compression function + * @returns {CompressionAdapter['compress']} The current compression function + */ + public get compress(): CompressionAdapter['compress'] | undefined { + return this._compress; + } + + /** + * 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 + */ + 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; + } + /** * Get the current namespace. * @returns {string | undefined} The current namespace. diff --git a/packages/keyv/test/test.ts b/packages/keyv/test/test.ts index fa8c3309..927c38da 100644 --- a/packages/keyv/test/test.ts +++ b/packages/keyv/test/test.ts @@ -742,3 +742,19 @@ test.it('Keyv does get and set on serialize / deserialize function', async t => keyv.deserialize = deserialize; t.expect(keyv.deserialize).toBe(deserialize); }); + +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); +}); + +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); +});