Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keyv - adding compression property #1191

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions packages/keyv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -532,32 +532,20 @@ keyv.deserialize = value => parseInt(value);
console.log(keyv.deserialize); // value => parseInt(value)
```

## .compress
## .compression

Type: `Function`<br />
Type: `CompressionAdapter`<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.
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`<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.
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
Expand Down
46 changes: 15 additions & 31 deletions packages/keyv/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ 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;
private _compression: CompressionAdapter | undefined;

/**
* Keyv Constructor
Expand Down Expand Up @@ -153,18 +152,15 @@ export class Keyv<GenericValue = any> 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;
}
Expand Down Expand Up @@ -234,34 +230,22 @@ export class Keyv<GenericValue = any> 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);
}
}

/**
Expand Down
17 changes: 5 additions & 12 deletions packages/keyv/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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);
});
Loading