From 01c47fe35de481774dfdfbdb2a7a112320aed929 Mon Sep 17 00:00:00 2001 From: Jared Wray Date: Sat, 4 Jan 2025 12:19:13 -0800 Subject: [PATCH] keyv - chore: removing xo lint rules from package.json (#1270) * keyv - chore: removing xo lint rules from package.json * removing unicorn/no-typeof-undefined * removing unicorn/prefer-event-target * removing import/no-extraneous-dependencies * removing import/extensions * removing no-await-in-loop * removing guard-for-in * removing @typescript-eslint/no-for-in-array * removing @typescript-eslint/no-var-requires * removing @typescript-eslint/naming-convention * removing @typescript-eslint/consistent-type-assertions * removing @typescript-eslint/no-confusing-void-expression --- packages/keyv/package.json | 18 +++--------------- packages/keyv/src/generic-store.ts | 1 + packages/keyv/src/hooks-manager.ts | 2 +- packages/keyv/src/index.ts | 22 ++++++++++++++-------- packages/keyv/test/test.ts | 25 +++++++++++++------------ packages/keyv/vitest.config.ts | 2 +- 6 files changed, 33 insertions(+), 37 deletions(-) diff --git a/packages/keyv/package.json b/packages/keyv/package.json index 116a403b1..29a709643 100644 --- a/packages/keyv/package.json +++ b/packages/keyv/package.json @@ -21,26 +21,12 @@ }, "xo": { "rules": { - "unicorn/prefer-module": "off", - "unicorn/prefer-node-protocol": "off", - "@typescript-eslint/consistent-type-definitions": "off", - "unicorn/no-typeof-undefined": "off", - "unicorn/prefer-event-target": "off", - "import/no-extraneous-dependencies": "off", - "import/extensions": "off", "@typescript-eslint/ban-ts-comment": "off", "@typescript-eslint/no-unsafe-call": "off", - "@typescript-eslint/no-for-in-array": "off", - "guard-for-in": "off", - "no-await-in-loop": "off", "@typescript-eslint/no-unsafe-return": "off", "@typescript-eslint/no-unsafe-assignment": "off", "@typescript-eslint/no-unsafe-argument": "off", - "@typescript-eslint/naming-convention": "off", - "@typescript-eslint/consistent-type-assertions": "off", - "@typescript-eslint/no-confusing-void-expression": "off", - "@typescript-eslint/no-var-requires": "off", - "@typescript-eslint/prefer-ts-expect-error": "off" + "@typescript-eslint/no-confusing-void-expression": "off" } }, "repository": { @@ -84,9 +70,11 @@ "@keyv/mongo": "workspace:^", "@keyv/sqlite": "workspace:^", "@keyv/test-suite": "workspace:^", + "@vitest/coverage-v8": "^2.1.8", "rimraf": "^6.0.1", "timekeeper": "^2.3.1", "tsd": "^0.31.2", + "vitest": "^2.1.8", "xo": "^0.60.0" }, "tsd": { diff --git a/packages/keyv/src/generic-store.ts b/packages/keyv/src/generic-store.ts index b65ded8ef..f61158e89 100644 --- a/packages/keyv/src/generic-store.ts +++ b/packages/keyv/src/generic-store.ts @@ -147,6 +147,7 @@ export class KeyvGenericStore extends EventManager implements KeyvStoreAdapter { async getMany(keys: string[]): Promise>> { const values = []; for (const key of keys) { + // eslint-disable-next-line no-await-in-loop const value = await this.get(key); values.push(value as T); } diff --git a/packages/keyv/src/hooks-manager.ts b/packages/keyv/src/hooks-manager.ts index 3418956ca..91e3f5f2e 100644 --- a/packages/keyv/src/hooks-manager.ts +++ b/packages/keyv/src/hooks-manager.ts @@ -39,7 +39,7 @@ class HooksManager extends EventManager { try { handler(data); } catch (error) { - this.emit('error', new Error(`Error in hook handler for event "${event}": ${(error).message}`)); + this.emit('error', new Error(`Error in hook handler for event "${event}": ${(error as Error).message}`)); } } } diff --git a/packages/keyv/src/index.ts b/packages/keyv/src/index.ts index e4415ce03..dc7c2d3b2 100644 --- a/packages/keyv/src/index.ts +++ b/packages/keyv/src/index.ts @@ -9,12 +9,12 @@ export type DeserializedData = { expires?: number | null; }; -export interface CompressionAdapter { +export type CompressionAdapter = { compress(value: any, options?: any): Promise; decompress(value: any, options?: any): Promise; serialize(data: DeserializedData): Promise | string; deserialize(data: string): Promise | undefined> | DeserializedData | undefined; -} +}; export type Serialize = (data: DeserializedData) => Promise | string; @@ -37,11 +37,12 @@ export type StoredDataRaw = DeserializedData | undefined; export type StoredData = StoredDataNoRaw | StoredDataRaw; -export interface IEventEmitter { - on(event: string, listener: (...arguments_: any[]) => void): this; -} +// eslint-disable-next-line @typescript-eslint/naming-convention +export type IEventEmitter = { + on(event: string, listener: (...arguments_: any[]) => void): IEventEmitter; +}; -export interface KeyvStoreAdapter extends IEventEmitter { +export type KeyvStoreAdapter = { opts: any; namespace?: string; get(key: string): Promise | undefined>; @@ -55,7 +56,7 @@ export interface KeyvStoreAdapter extends IEventEmitter { disconnect?(): Promise; deleteMany?(key: string[]): Promise; iterator?(namespace?: string): AsyncGenerator | undefined>, void>; -} +} & IEventEmitter; export type KeyvOptions = { /** Emit errors */ @@ -78,6 +79,7 @@ export type KeyvOptions = { useKeyPrefix?: boolean; }; +// eslint-disable-next-line @typescript-eslint/naming-convention type KeyvOptions_ = Omit & {store: KeyvStoreAdapter | Map & KeyvStoreAdapter}; type IteratorFunction = (argument: any) => AsyncGenerator; @@ -139,6 +141,7 @@ export class Keyv extends EventManager { constructor(store?: KeyvStoreAdapter | KeyvOptions, options?: Omit) { super(); options ??= {}; + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions store ??= {} as KeyvOptions; this.opts = { @@ -465,10 +468,12 @@ export class Keyv extends EventManager { const rawData = await store.getMany(keyPrefixed as string[]); const result = []; + // eslint-disable-next-line guard-for-in, @typescript-eslint/no-for-in-array for (const index in rawData) { let row = rawData[index]; if ((typeof row === 'string')) { + // eslint-disable-next-line no-await-in-loop row = await this.deserializeData(row); } @@ -478,6 +483,7 @@ export class Keyv extends EventManager { } if (isDataExpired(row as DeserializedData)) { + // eslint-disable-next-line no-await-in-loop await this.delete(key[index]); result.push(undefined); continue; @@ -525,7 +531,7 @@ export class Keyv extends EventManager { async set(key: string, value: Value, ttl?: number): Promise { this.hooks.trigger(KeyvHooks.PRE_SET, {key, value, ttl}); const keyPrefixed = this._getKeyPrefix(key); - if (typeof ttl === 'undefined') { + if (ttl === undefined) { ttl = this._ttl; } diff --git a/packages/keyv/test/test.ts b/packages/keyv/test/test.ts index 2165db78b..6ae07eec8 100644 --- a/packages/keyv/test/test.ts +++ b/packages/keyv/test/test.ts @@ -188,6 +188,7 @@ test.it('Keyv supports async serializer/deserializer', async t => { test.it('Keyv should wait for the expired get', async t => { t.expect.assertions(4); const _store = new Map(); + // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const store = { get: async (key: string) => _store.get(key), set(key: string, value: any) { @@ -404,8 +405,8 @@ test.it('iterator should exists with url', t => { test.it( 'keyv iterator() doesn\'t yield values from other namespaces with compression', async t => { - const KeyvStore = new Map(); - const keyv1 = new Keyv({store: KeyvStore, namespace: 'keyv1', compression: new KeyvGzip()}); + const keyvStore = new Map(); + const keyv1 = new Keyv({store: keyvStore, namespace: 'keyv1', compression: new KeyvGzip()}); const map1 = new Map( Array.from({length: 5}) .fill(0) @@ -417,7 +418,7 @@ test.it( } await Promise.all(toResolve); - const keyv2 = new Keyv({store: KeyvStore, namespace: 'keyv2', compression: new KeyvGzip()}); + const keyv2 = new Keyv({store: keyvStore, namespace: 'keyv2', compression: new KeyvGzip()}); const map2 = new Map( Array.from({length: 5}) .fill(0) @@ -443,9 +444,9 @@ test.it( test.it( 'keyv iterator() doesn\'t yield values from other namespaces', async t => { - const KeyvStore = new Map(); + const keyvStore = new Map(); - const keyv1 = new Keyv({store: KeyvStore, namespace: 'keyv1'}); + const keyv1 = new Keyv({store: keyvStore, namespace: 'keyv1'}); const map1 = new Map( Array.from({length: 5}) .fill(0) @@ -458,7 +459,7 @@ test.it( await Promise.all(toResolve); - const keyv2 = new Keyv({store: KeyvStore, namespace: 'keyv2'}); + const keyv2 = new Keyv({store: keyvStore, namespace: 'keyv2'}); const map2 = new Map( Array.from({length: 5}) .fill(0) @@ -484,14 +485,14 @@ test.it( test.it( 'keyv iterator() doesn\'t yield values from other namespaces with custom serializer/deserializer', async t => { - const KeyvStore = new Map(); + const keyvStore = new Map(); const serialize = (data: Record) => JSON.stringify(data); const deserialize = (data: string) => JSON.parse(data); const keyv1 = new Keyv({ - store: KeyvStore, serialize, deserialize, namespace: 'keyv1', + store: keyvStore, serialize, deserialize, namespace: 'keyv1', }); const map1 = new Map( Array.from({length: 5}) @@ -506,7 +507,7 @@ test.it( await Promise.all(toResolve); const keyv2 = new Keyv({ - store: KeyvStore, serialize, deserialize, namespace: 'keyv2', + store: keyvStore, serialize, deserialize, namespace: 'keyv2', }); const map2 = new Map( Array.from({length: 5}) @@ -533,13 +534,13 @@ test.it( test.it( 'keyv iterator() doesn\'t yield values from other namespaces with custom serializer/deserializer and compression', async t => { - const KeyvStore = new Map(); + const keyvStore = new Map(); const serialize = (data: Record) => JSON.stringify(data); const deserialize = (data: string) => JSON.parse(data); const keyv1 = new Keyv({ - store: KeyvStore, serialize, deserialize, namespace: 'keyv1', compression: new KeyvGzip(), + store: keyvStore, serialize, deserialize, namespace: 'keyv1', compression: new KeyvGzip(), }); const map1 = new Map( Array.from({length: 5}) @@ -554,7 +555,7 @@ test.it( await Promise.all(toResolve); const keyv2 = new Keyv({ - store: KeyvStore, serialize, deserialize, namespace: 'keyv2', + store: keyvStore, serialize, deserialize, namespace: 'keyv2', }); const map2 = new Map( Array.from({length: 5}) diff --git a/packages/keyv/vitest.config.ts b/packages/keyv/vitest.config.ts index 1ee8dcc07..772d21fe2 100644 --- a/packages/keyv/vitest.config.ts +++ b/packages/keyv/vitest.config.ts @@ -1,4 +1,4 @@ import {defineConfig, mergeConfig} from 'vitest/config'; -import vitestConfig from '../../vitest.general.config'; +import vitestConfig from '../../vitest.general.config.js'; export default mergeConfig(vitestConfig, defineConfig({}));