diff --git a/packages/backend/server/package.json b/packages/backend/server/package.json index 49d9026702..9a705b08df 100644 --- a/packages/backend/server/package.json +++ b/packages/backend/server/package.json @@ -70,7 +70,7 @@ "graphql-upload": "^16.0.2", "html-validate": "^8.20.1", "ioredis": "^5.3.2", - "keyv": "^4.5.4", + "keyv": "^5.0.0", "lodash-es": "^4.17.21", "mixpanel": "^0.18.0", "mustache": "^4.2.0", diff --git a/packages/backend/server/src/fundamentals/cache/local.ts b/packages/backend/server/src/fundamentals/cache/local.ts index 6e6ac148ac..ca46e17bd3 100644 --- a/packages/backend/server/src/fundamentals/cache/local.ts +++ b/packages/backend/server/src/fundamentals/cache/local.ts @@ -1,17 +1,17 @@ -import Keyv from 'keyv'; +import Keyv, { KeyvOptions } from 'keyv'; import type { Cache, CacheSetOptions } from './def'; export class LocalCache implements Cache { private readonly kv: Keyv; - constructor(opts: Keyv.Options = {}) { + constructor(opts: KeyvOptions = {}) { this.kv = new Keyv(opts); } // standard operation async get(key: string): Promise { - return this.kv.get(key).catch(() => undefined); + return this.kv.get(key).catch(() => undefined); } async set( @@ -74,14 +74,14 @@ export class LocalCache implements Cache { // list operations private async getArray(key: string) { - const raw = await this.kv.get(key, { raw: true }); + const raw = await this.kv.get(key, { raw: true }); if (raw && !Array.isArray(raw.value)) { throw new Error( `Expect an Array keyed by ${key}, but found ${raw.value}` ); } - return raw as Keyv.DeserializedData; + return raw; } private async setArray( @@ -97,7 +97,9 @@ export class LocalCache implements Cache { let ttl: number | undefined = undefined; const raw = await this.getArray(key); if (raw) { - list = raw.value; + if (raw.value) { + list = raw.value; + } if (raw.expires) { ttl = raw.expires - Date.now(); } @@ -112,7 +114,9 @@ export class LocalCache implements Cache { let ttl: number | undefined = undefined; const raw = await this.getArray(key); if (raw) { - list = raw.value; + if (raw.value) { + list = raw.value; + } if (raw.expires) { ttl = raw.expires - Date.now(); } @@ -123,7 +127,7 @@ export class LocalCache implements Cache { } async len(key: string): Promise { - return this.getArray(key).then(v => v?.value.length ?? 0); + return this.getArray(key).then(v => v?.value?.length ?? 0); } /** @@ -147,7 +151,7 @@ export class LocalCache implements Cache { private async trim(key: string, start: number, end: number) { const raw = await this.getArray(key); - if (raw) { + if (raw && raw.value) { start = (raw.value.length + start) % raw.value.length; // make negative end index work, and end indice is inclusive end = ((raw.value.length + end) % raw.value.length) + 1; @@ -173,7 +177,7 @@ export class LocalCache implements Cache { // map operations private async getMap(map: string) { - const raw = await this.kv.get(map, { raw: true }); + const raw = await this.kv.get>(map, { raw: true }); if (raw) { if (typeof raw.value !== 'object') { @@ -187,7 +191,7 @@ export class LocalCache implements Cache { } } - return raw as Keyv.DeserializedData>; + return raw; } private async setMap( @@ -263,7 +267,7 @@ export class LocalCache implements Cache { async mapKeys(map: string): Promise { const raw = await this.getMap(map); - if (raw) { + if (raw?.value) { return Object.keys(raw.value); } @@ -277,6 +281,6 @@ export class LocalCache implements Cache { async mapLen(map: string): Promise { const raw = await this.getMap(map); - return raw ? Object.keys(raw.value).length : 0; + return raw?.value ? Object.keys(raw.value).length : 0; } } diff --git a/yarn.lock b/yarn.lock index ecdb5d0195..fb153283c7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -868,7 +868,7 @@ __metadata: graphql-upload: "npm:^16.0.2" html-validate: "npm:^8.20.1" ioredis: "npm:^5.3.2" - keyv: "npm:^4.5.4" + keyv: "npm:^5.0.0" lodash-es: "npm:^4.17.21" mixpanel: "npm:^0.18.0" mustache: "npm:^4.2.0" @@ -25637,7 +25637,7 @@ __metadata: languageName: node linkType: hard -"keyv@npm:*": +"keyv@npm:*, keyv@npm:^5.0.0": version: 5.0.1 resolution: "keyv@npm:5.0.1" dependencies: @@ -25646,7 +25646,7 @@ __metadata: languageName: node linkType: hard -"keyv@npm:^4.0.0, keyv@npm:^4.5.3, keyv@npm:^4.5.4": +"keyv@npm:^4.0.0, keyv@npm:^4.5.3": version: 4.5.4 resolution: "keyv@npm:4.5.4" dependencies: