Skip to content

Commit

Permalink
keyv - adding in compress and decompress properties (#1190)
Browse files Browse the repository at this point in the history
* keyv - adding in compress and decompress properties

* Update README.md
  • Loading branch information
jaredwray authored Nov 6, 2024
1 parent cbe7fb6 commit f0e24ac
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/keyv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,34 @@ keyv.deserialize = value => parseInt(value);
console.log(keyv.deserialize); // value => parseInt(value)
```

## .compress

Type: `Function`<br />
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`<br />
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:
Expand Down
40 changes: 39 additions & 1 deletion packages/keyv/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export class Keyv<GenericValue = any> 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<any, any>} store to be provided or just the options
Expand Down Expand Up @@ -150,15 +153,18 @@ export class Keyv<GenericValue = any> 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);
}

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;
}
Expand Down Expand Up @@ -226,6 +232,38 @@ export class Keyv<GenericValue = any> 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.
Expand Down
16 changes: 16 additions & 0 deletions packages/keyv/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit f0e24ac

Please sign in to comment.