diff --git a/packages/cacher/src/adapters/union.ts b/packages/cacher/src/adapters/union.ts index 9280584..074af6a 100644 --- a/packages/cacher/src/adapters/union.ts +++ b/packages/cacher/src/adapters/union.ts @@ -2,7 +2,7 @@ import { IAdapter, IAdapterIncrementOptions, IAdapterSetOptions, - IAdapterTouchOptions + IAdapterTouchOptions, } from './adapter'; export interface IUnionAdapterOptions { @@ -23,7 +23,7 @@ export class UnionAdapter implements IAdapter { adapters, replicaTtl, - replicaMode = true + replicaMode = true, }: IUnionAdapterOptions) { this.adapters = adapters; @@ -38,7 +38,7 @@ export class UnionAdapter implements IAdapter { const keysForSecond = keys.filter((item, index) => ( firstItems[index] === undefined - )) as string[]; + )) ; if (keysForSecond.length === 0) { return firstItems; @@ -60,7 +60,7 @@ export class UnionAdapter implements IAdapter { key, value, - ttl: this.replicaTtl + ttl: this.replicaTtl, }); }); @@ -90,7 +90,7 @@ export class UnionAdapter implements IAdapter { const [firstKeys, secondKeys] = await Promise.all([ firstAdapter.increment(keys), - secondAdapter.increment(keys) + secondAdapter.increment(keys), ]); return keys.map((_, index) => firstKeys[index] || secondKeys[index]); diff --git a/packages/cacher/src/cacher.ts b/packages/cacher/src/cacher.ts index 20cf6ed..9b41439 100644 --- a/packages/cacher/src/cacher.ts +++ b/packages/cacher/src/cacher.ts @@ -9,7 +9,7 @@ import { Serializer, Deserializer, - AllowArray + AllowArray, } from './types'; import { arraify } from './helpers'; @@ -29,7 +29,7 @@ export class Cacher { namespace, serializer = JSON.stringify, - deserializer = JSON.parse + deserializer = JSON.parse, }: ICacherOptions) { this.adapter = adapter; @@ -69,7 +69,7 @@ export class Cacher { return this.adapter.set(keys.map(item => ({ key: this.getNamespaceKey(item.key), value: this.serializer(item.value), - ttl: item.ttl + ttl: item.ttl, }))); } @@ -78,7 +78,7 @@ export class Cacher { return this.adapter.increment(keys.map(item => ({ key: this.getNamespaceKey(item.key), - value: item.value + value: item.value, }))); } @@ -95,7 +95,7 @@ export class Cacher { return this.adapter.touch(keys.map(item => ({ key: this.getNamespaceKey(item.key), - ttl: item.ttl + ttl: item.ttl, }))); } diff --git a/packages/redis/src/redis.ts b/packages/redis/src/redis.ts index 49efa15..97cf5b9 100644 --- a/packages/redis/src/redis.ts +++ b/packages/redis/src/redis.ts @@ -4,7 +4,7 @@ import { IAdapter, IAdapterIncrementOptions, IAdapterSetOptions, - IAdapterTouchOptions + IAdapterTouchOptions, } from '@cacher/cacher'; export interface IRedisAdapterConnectionOptions { @@ -18,11 +18,11 @@ export interface IRedisAdapterConnectionOptions { } export interface IRedisAdapterOptions { - connection?: IRedisAdapterConnectionOptions | IORedis.Redis | string; + connection?: IRedisAdapterConnectionOptions | IORedis | string; } export class RedisAdapter implements IAdapter { - protected readonly redis: IORedis.Redis; + protected readonly redis: IORedis; public constructor({ connection = {} }: IRedisAdapterOptions = {}) { if (typeof connection === 'string') { @@ -35,7 +35,7 @@ export class RedisAdapter implements IAdapter { port: connection.port, username: connection.username, password: connection.password, - db: connection.database + db: connection.database, }); } } @@ -95,7 +95,7 @@ export class RedisAdapter implements IAdapter { const result = await this.redis.incrbyfloat(key, value); - return [result]; + return [Number(result)]; } const pipeline = this.redis.multi(); @@ -106,10 +106,10 @@ export class RedisAdapter implements IAdapter { const { 1: result } = await Promise.all([ pipeline.exec(), - promise + promise, ]); - return result; + return result.map(Number); } public async delete(keys: string[]): Promise { @@ -134,17 +134,17 @@ export class RedisAdapter implements IAdapter { await Promise.all([ pipeline.exec(), - promise + promise, ]); } public async clear(namespace: string): Promise { const stream = this.redis.scanStream({ - match: `${namespace}:*` + match: `${namespace}:*`, }); for await (const keys of stream) { - await this.redis.del(...keys); + await this.redis.del(...keys as string[]); } } } diff --git a/rollup.config.js b/rollup.config.js index 40993f4..e807e92 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,22 +1,26 @@ import typescriptPlugin from 'rollup-plugin-typescript2'; -import { tmpdir } from 'os'; -import { builtinModules } from 'module'; -import { join as pathJoin } from 'path'; +import { tmpdir } from 'node:os'; +import { fileURLToPath } from 'node:url'; +import { builtinModules } from 'node:module'; +import { dirname, join as pathJoin } from 'node:path'; const MODULES = [ 'cacher', - 'redis' + 'redis', ]; const coreModules = builtinModules.filter(name => ( !/(^_|\/)/.test(name) )); +const rootDir = dirname(fileURLToPath(import.meta.url)); + const cacheRoot = pathJoin(tmpdir(), '.rpt2_cache'); +/** @param {string} path */ const getModulePath = path => ( - pathJoin(__dirname, 'packages', path) + pathJoin(rootDir, 'packages', path) ); // eslint-disable-next-line import/no-default-export @@ -25,8 +29,20 @@ export default async () => ( MODULES .map(getModulePath) .map(async (modulePath) => { + /** + * @type {{ + * dependencies?: Record, + * peerDependencies?: Record, + * }} + */ + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const modulePkg = await import( - pathJoin(modulePath, 'package.json') + pathJoin(modulePath, 'package.json'), + { + assert: { + type: 'json', + }, + } ); const src = pathJoin(modulePath, 'src'); @@ -43,28 +59,28 @@ export default async () => ( tsconfigOverride: { outDir: lib, rootDir: src, - include: [src] - } - }) + include: [src], + }, + }), ], external: [ ...Object.keys(modulePkg.dependencies || {}), ...Object.keys(modulePkg.peerDependencies || {}), // TODO: To make better ...MODULES.map(moduleName => `@cacher/${moduleName}`), - ...coreModules + ...coreModules, ], output: [ { file: pathJoin(modulePath, 'lib/index.js'), format: 'cjs', - exports: 'named' + exports: 'named', }, { file: pathJoin(modulePath, 'lib/index.mjs'), - format: 'esm' - } - ] + format: 'esm', + }, + ], }; }) )