Skip to content

Commit

Permalink
Keyv - handing complex objects with no serialize / deserialize (#1195)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray authored Nov 9, 2024
1 parent b2fb600 commit 01a95f8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
32 changes: 22 additions & 10 deletions packages/keyv/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ export class Keyv<GenericValue = any> extends EventManager {
*/
private _store: KeyvStoreAdapter | Map<any, any> | 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;

Expand Down Expand Up @@ -292,15 +292,15 @@ export class Keyv<GenericValue = any> 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;
}

/**
* 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;
}
Expand All @@ -309,15 +309,15 @@ export class Keyv<GenericValue = any> 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;
}

/**
* 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;
}
Expand Down Expand Up @@ -625,21 +625,33 @@ export class Keyv<GenericValue = any> extends EventManager {
}
}

public async serializeData<T>(data: DeserializedData<T>): Promise<string> {
public async serializeData<T>(data: DeserializedData<T>): Promise<string | DeserializedData<T>> {
if (!this._serialize) {
return data;
}

if (this._compression?.compress) {
return this._serialize({value: await this._compression.compress(data.value), expires: data.expires});
}

return this._serialize(data);
}

public async deserializeData<T>(data: string): Promise<DeserializedData<T> | undefined> {
if (this._compression?.decompress) {
public async deserializeData<T>(data: string | DeserializedData<T>): Promise<DeserializedData<T> | undefined> {
if (!this._deserialize) {
return data as DeserializedData<T>;
}

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;
}
}

Expand Down
18 changes: 18 additions & 0 deletions packages/keyv/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,3 +805,21 @@ test.it('Keyv will not prefix if there is no namespace', async t => {
const values = await keyv.get<string>(['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();
});

0 comments on commit 01a95f8

Please sign in to comment.