Skip to content

Commit

Permalink
keyv - chore: removing xo lint rules from package.json (#1270)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jaredwray authored Jan 4, 2025
1 parent d4a7493 commit 01c47fe
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 37 deletions.
18 changes: 3 additions & 15 deletions packages/keyv/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions packages/keyv/src/generic-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export class KeyvGenericStore extends EventManager implements KeyvStoreAdapter {
async getMany<T>(keys: string[]): Promise<Array<StoredData<T | undefined>>> {
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);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/keyv/src/hooks-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>error).message}`));
this.emit('error', new Error(`Error in hook handler for event "${event}": ${(error as Error).message}`));
}
}
}
Expand Down
22 changes: 14 additions & 8 deletions packages/keyv/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export type DeserializedData<Value> = {
expires?: number | null;
};

export interface CompressionAdapter {
export type CompressionAdapter = {
compress(value: any, options?: any): Promise<any>;
decompress(value: any, options?: any): Promise<any>;
serialize<Value>(data: DeserializedData<Value>): Promise<string> | string;
deserialize<Value>(data: string): Promise<DeserializedData<Value> | undefined> | DeserializedData<Value> | undefined;
}
};

export type Serialize = <Value>(data: DeserializedData<Value>) => Promise<string> | string;

Expand All @@ -37,11 +37,12 @@ export type StoredDataRaw<Value> = DeserializedData<Value> | undefined;

export type StoredData<Value> = StoredDataNoRaw<Value> | StoredDataRaw<Value>;

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<Value>(key: string): Promise<StoredData<Value> | undefined>;
Expand All @@ -55,7 +56,7 @@ export interface KeyvStoreAdapter extends IEventEmitter {
disconnect?(): Promise<void>;
deleteMany?(key: string[]): Promise<boolean>;
iterator?<Value>(namespace?: string): AsyncGenerator<Array<string | Awaited<Value> | undefined>, void>;
}
} & IEventEmitter;

export type KeyvOptions = {
/** Emit errors */
Expand All @@ -78,6 +79,7 @@ export type KeyvOptions = {
useKeyPrefix?: boolean;
};

// eslint-disable-next-line @typescript-eslint/naming-convention
type KeyvOptions_ = Omit<KeyvOptions, 'store'> & {store: KeyvStoreAdapter | Map<any, any> & KeyvStoreAdapter};

type IteratorFunction = (argument: any) => AsyncGenerator<any, void>;
Expand Down Expand Up @@ -139,6 +141,7 @@ export class Keyv<GenericValue = any> extends EventManager {
constructor(store?: KeyvStoreAdapter | KeyvOptions, options?: Omit<KeyvOptions, 'store'>) {
super();
options ??= {};
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
store ??= {} as KeyvOptions;

this.opts = {
Expand Down Expand Up @@ -465,10 +468,12 @@ export class Keyv<GenericValue = any> extends EventManager {
const rawData = await store.getMany<Value>(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<Value>(row);
}

Expand All @@ -478,6 +483,7 @@ export class Keyv<GenericValue = any> extends EventManager {
}

if (isDataExpired(row as DeserializedData<Value>)) {
// eslint-disable-next-line no-await-in-loop
await this.delete(key[index]);
result.push(undefined);
continue;
Expand Down Expand Up @@ -525,7 +531,7 @@ export class Keyv<GenericValue = any> extends EventManager {
async set<Value = GenericValue>(key: string, value: Value, ttl?: number): Promise<boolean> {
this.hooks.trigger(KeyvHooks.PRE_SET, {key, value, ttl});
const keyPrefixed = this._getKeyPrefix(key);
if (typeof ttl === 'undefined') {
if (ttl === undefined) {
ttl = this._ttl;
}

Expand Down
25 changes: 13 additions & 12 deletions packages/keyv/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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<string, unknown>) => 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})
Expand All @@ -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})
Expand All @@ -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<string, unknown>) => 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})
Expand All @@ -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})
Expand Down
2 changes: 1 addition & 1 deletion packages/keyv/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -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({}));

0 comments on commit 01c47fe

Please sign in to comment.