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

compress-gzip - fix: set any value before getting value #1118

Merged
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
18 changes: 13 additions & 5 deletions packages/compress-brotli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,25 @@ export class KeyvBrotli {
return this.brotli.compress(value, options);
}

async decompress<T>(data: InputType, options?: BrotliOptions): Promise<T> {
return await this.brotli.decompress(data, options) as T;
async decompress<T>(data?: InputType, options?: BrotliOptions): Promise<T> {
if (data) {
return await this.brotli.decompress(data, options) as T;
}

return undefined as unknown as T;
}

async serialize({value, expires}: Serialize): Promise<SerializeResult> {
return defaultSerialize({value: await this.compress(value), expires});
}

async deserialize(data: CompressResult): Promise<Serialize> {
const {value, expires}: Serialize = defaultDeserialize(data);
return {value: await this.decompress(value), expires};
async deserialize(data?: CompressResult): Promise<Serialize> {
if (data) {
const {value, expires}: Serialize = defaultDeserialize(data);
return {value: await this.decompress(value), expires};
}

return {value: undefined, expires: undefined};
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/compress-brotli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type BrotliSerialize<T> = (source: InputType) => T;
type BrotliDeserialize<T> = (source: CompressResult) => T;

export type Serialize = {
value: InputType;
value?: InputType;
expires?: number;
};

Expand Down
11 changes: 11 additions & 0 deletions packages/compress-brotli/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,14 @@ test.it('decompression using number array with v8', async t => {
t.expect(decompressed).toEqual({help: [1, 2, 4]});
});

test.it('decompress should not throw error when empty with brotli', async t => {
const keyv = new KeyvBrotli();
await t.expect(keyv.decompress()).resolves.not.toThrowError();
});

test.it('should return empty object when empty', async t => {
const keyv = new KeyvBrotli();
const result = await keyv.deserialize();
t.expect(result).toEqual({value: undefined, expires: undefined});
});

14 changes: 9 additions & 5 deletions packages/compress-gzip/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pako from 'pako';
import {inflate, deflate} from 'pako';
import {defaultSerialize, defaultDeserialize} from '@keyv/serialize';
import type {Options, Serialize} from './types.js';

Expand All @@ -12,24 +12,28 @@ export class KeyvGzip {
}

async compress(value: pako.Data | string, options?: Options) {
return pako.deflate(value, options || this.opts);
return deflate(value, options || this.opts);
}

async decompress(value: pako.Data, options?: Options) {
if (options) {
options.to = 'string';
}

return pako.inflate(value, options || this.opts);
return inflate(value, options || this.opts);
}

async serialize({value, expires}: Serialize) {
return defaultSerialize({value: await this.compress(value), expires});
}

async deserialize(data: string) {
const {value, expires}: Serialize = defaultDeserialize(data);
return {value: await this.decompress(value as pako.Data), expires};
if (data) {
const {value, expires}: Serialize = defaultDeserialize(data);
return {value: await this.decompress(value as pako.Data), expires};
}

return {value: undefined, expires: undefined};
}
}

Expand Down
11 changes: 11 additions & 0 deletions packages/compress-gzip/test/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as test from 'vitest';
import {keyvCompresstionTests} from '@keyv/test-suite';
import {Keyv} from 'keyv';
import KeyvGzip from '../src/index.js';

// @ts-expect-error - KeyvGzip type
Expand Down Expand Up @@ -48,3 +49,13 @@ test.it('decompression with decompression options', async t => {
const decompressed = await keyv.decompress(compressed, options);
t.expect(decompressed).toBe('whatever');
});

test.it('decompress should not throw error when empty with gzip', async t => {
const keyv = new Keyv({store: new Map(), compression: new KeyvGzip()});
await t.expect(keyv.get('foo')).resolves.not.toThrowError();
});

test.it('should not throw error when empty', async t => {
const keyv = new Keyv({store: new Map()});
await t.expect(keyv.get('foo')).resolves.not.toThrowError();
});
Loading