diff --git a/packages/keyv/src/index.ts b/packages/keyv/src/index.ts index 8dab3fc1a..4d814b2d2 100644 --- a/packages/keyv/src/index.ts +++ b/packages/keyv/src/index.ts @@ -109,8 +109,8 @@ export class Keyv extends EventManager { */ private _store: KeyvStoreAdapter | Map | any = new Map(); - private _serialize: Serialize = defaultSerialize; - private _deserialize: Deserialize = defaultDeserialize; + private _serialize: Serialize | undefined = defaultSerialize; + private _deserialize: Deserialize | undefined = defaultDeserialize; private _compression: CompressionAdapter | undefined; @@ -292,7 +292,7 @@ export class Keyv extends EventManager { * Get the current serialize function. * @returns {Serialize} The current serialize function. */ - public get serialize(): Serialize { + public get serialize(): Serialize | undefined { return this._serialize; } @@ -300,7 +300,7 @@ export class Keyv extends EventManager { * Set the current serialize function. * @param {Serialize} serialize The serialize function to set. */ - public set serialize(serialize: Serialize) { + public set serialize(serialize: Serialize | undefined) { this.opts.serialize = serialize; this._serialize = serialize; } @@ -309,7 +309,7 @@ export class Keyv extends EventManager { * Get the current deserialize function. * @returns {Deserialize} The current deserialize function. */ - public get deserialize(): Deserialize { + public get deserialize(): Deserialize | undefined { return this._deserialize; } @@ -317,7 +317,7 @@ export class Keyv extends EventManager { * Set the current deserialize function. * @param {Deserialize} deserialize The deserialize function to set. */ - public set deserialize(deserialize: Deserialize) { + public set deserialize(deserialize: Deserialize | undefined) { this.opts.deserialize = deserialize; this._deserialize = deserialize; } @@ -625,7 +625,11 @@ export class Keyv extends EventManager { } } - public async serializeData(data: DeserializedData): Promise { + public async serializeData(data: DeserializedData): Promise> { + if (!this._serialize) { + return data; + } + if (this._compression?.compress) { return this._serialize({value: await this._compression.compress(data.value), expires: data.expires}); } @@ -633,13 +637,21 @@ export class Keyv extends EventManager { return this._serialize(data); } - public async deserializeData(data: string): Promise | undefined> { - if (this._compression?.decompress) { + public async deserializeData(data: string | DeserializedData): Promise | undefined> { + if (!this._deserialize) { + return data as DeserializedData; + } + + if (this._compression?.decompress && typeof data === 'string') { const result = await this._deserialize(data); return {value: await this._compression.decompress(result?.value), expires: result?.expires}; } - return this._deserialize(data); + if (typeof data === 'string') { + return this._deserialize(data); + } + + return undefined; } } diff --git a/packages/keyv/test/test.ts b/packages/keyv/test/test.ts index ded7a9385..246037b32 100644 --- a/packages/keyv/test/test.ts +++ b/packages/keyv/test/test.ts @@ -805,3 +805,21 @@ test.it('Keyv will not prefix if there is no namespace', async t => { const values = await keyv.get(['foo', 'foo1', 'foo2']) as string[]; t.expect(values).toStrictEqual(['bar', 'bar1', 'bar2']); }); + +test.it('Keyv will not serialize / deserialize / compress if it is undefined', async t => { + const keyv = new Keyv({compression: new KeyvGzip()}); + keyv.serialize = undefined; + keyv.deserialize = undefined; + const complexObject = {foo: 'bar', fizz: 'buzz'}; + await keyv.set('foo-complex', complexObject); + await keyv.set('foo', 'bar'); + t.expect(await keyv.get('foo')).toBe('bar'); + t.expect(await keyv.get('foo-complex')).toStrictEqual(complexObject); +}); + +test.it('Keyv deserlize will return undefined if not string', async t => { + const keyv = new Keyv({compression: new KeyvGzip()}); + const complexObject = {foo: 'bar', fizz: 'buzz'}; + const result = await keyv.deserializeData({value: complexObject}); + t.expect(result).toBeUndefined(); +});