diff --git a/package.json b/package.json index 6a4a607..f308825 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,6 @@ "@types/nodemailer": "^6.4.0", "@types/nunjucks": "^3.1.3", "@types/on-finished": "^2.3.1", - "@types/redis": "^2.8.18", "@types/require-from-string": "^1.2.0", "@types/supertest": "^2.0.10", "@types/uuid": "^8.0.0", @@ -91,7 +90,7 @@ "normalize.css": "^8.0.1", "nunjucks": "^3.2.1", "on-finished": "^2.3.0", - "redis": "^3.0.2", + "redis": "^4.0.4", "require-from-string": "^2.0.2", "rollup": "^2.42.3", "rollup-plugin-css-only": "^3.1.0", diff --git a/src/components/RedisComponent.ts b/src/components/RedisComponent.ts index d31953f..99c5280 100644 --- a/src/components/RedisComponent.ts +++ b/src/components/RedisComponent.ts @@ -1,7 +1,7 @@ import config from "config"; import {Express} from "express"; import session, {Store} from "express-session"; -import redis, {RedisClient} from "redis"; +import {createClient, RedisClientType} from "redis"; import ApplicationComponent from "../ApplicationComponent.js"; import CacheProvider from "../CacheProvider.js"; @@ -9,22 +9,31 @@ import {logger} from "../Logger.js"; export default class RedisComponent extends ApplicationComponent implements CacheProvider { private readonly prefix: string = config.get('redis.prefix'); - private redisClient?: RedisClient; + private redisClient?: RedisClientType; private store: Store = new RedisStore(this); public async start(_app: Express): Promise { - this.redisClient = redis.createClient(config.get('redis.port'), config.get('redis.host'), { + const redisUrl = `redis://${config.get('redis.host')}:${config.get('redis.port')}`; + console.log(redisUrl); + this.redisClient = createClient({ + url: redisUrl, password: config.has('redis.password') ? config.get('redis.password') : undefined, }); this.redisClient.on('error', (err: Error) => { logger.error(err, 'An error occurred with redis.'); }); + + await this.redisClient.connect(); } public async stop(): Promise { const redisClient = this.redisClient; if (redisClient) { - await this.close('Redis connection', callback => redisClient.quit(callback)); + await this.close('Redis connection', callback => { + redisClient.quit() + .then(() => callback()) + .catch(callback); + }); } } @@ -33,24 +42,14 @@ export default class RedisComponent extends ApplicationComponent implements Cach } public isReady(): boolean { - return this.redisClient !== undefined && this.redisClient.connected; + return this.redisClient !== undefined && this.redisClient.isOpen; } public async get(key: string, defaultValue?: T): Promise { - return await new Promise((resolve, reject) => { - if (!this.redisClient) { - reject(`Redis client was not initialized.`); - return; - } - - this.redisClient.get(this.prefix + key, (err, val) => { - if (err) { - reject(err); - return; - } - resolve((val || defaultValue || undefined) as T); - }); - }); + if (!this.redisClient) { + throw new Error(`Redis client was not initialized.`); + } + return (await this.redisClient.get(this.prefix + key)|| defaultValue || undefined) as T; } public async has(key: string): Promise { @@ -58,49 +57,27 @@ export default class RedisComponent extends ApplicationComponent implements Cach } public async forget(key: string): Promise { - return await new Promise((resolve, reject) => { - if (!this.redisClient) { - reject(`Redis client was not initialized.`); - return; - } + if (!this.redisClient) { + throw new Error(`Redis client was not initialized.`); + } - this.redisClient.del(this.prefix + key, (err) => { - if (err) { - reject(err); - return; - } - - resolve(); - }); - }); + await this.redisClient.del(this.prefix + key); } public async remember(key: string, value: string, ttl: number): Promise { - return await new Promise((resolve, reject) => { - if (!this.redisClient) { - reject(`Redis client was not initialized.`); - return; - } + if (!this.redisClient) { + throw new Error(`Redis client was not initialized.`); + } - this.redisClient.psetex(this.prefix + key, ttl, value, (err) => { - if (err) return reject(err); - resolve(); - }); - }); + await this.redisClient.pSetEx(this.prefix + key, ttl, value); } public async persist(key: string, ttl: number): Promise { - return await new Promise((resolve, reject) => { - if (!this.redisClient) { - reject(`Redis client was not initialized.`); - return; - } + if (!this.redisClient) { + throw new Error(`Redis client was not initialized.`); + } - this.redisClient.pexpire(this.prefix + key, ttl, (err) => { - if (err) return reject(err); - resolve(); - }); - }); + await this.redisClient.pExpire(this.prefix + key, ttl); } } diff --git a/yarn.lock b/yarn.lock index fec2b56..acfa842 100644 --- a/yarn.lock +++ b/yarn.lock @@ -559,6 +559,41 @@ semver "^7.3.5" tar "^6.1.11" +"@node-redis/bloom@1.0.1": + version "1.0.1" + resolved "https://registry.yarnpkg.com/@node-redis/bloom/-/bloom-1.0.1.tgz#144474a0b7dc4a4b91badea2cfa9538ce0a1854e" + integrity sha512-mXEBvEIgF4tUzdIN89LiYsbi6//EdpFA7L8M+DHCvePXg+bfHWi+ct5VI6nHUFQE5+ohm/9wmgihCH3HSkeKsw== + +"@node-redis/client@1.0.4": + version "1.0.4" + resolved "https://registry.yarnpkg.com/@node-redis/client/-/client-1.0.4.tgz#fe185750df3bcc07524f63fe8dbc8d14d22d6cbb" + integrity sha512-IM/NRAqg7MvNC3bIRQipXGrEarunrdgvrbAzsd3ty93LSHi/M+ybQulOERQi8a3M+P5BL8HenwXjiIoKm6ml2g== + dependencies: + cluster-key-slot "1.1.0" + generic-pool "3.8.2" + redis-parser "3.0.0" + yallist "4.0.0" + +"@node-redis/graph@1.0.0": + version "1.0.0" + resolved "https://registry.yarnpkg.com/@node-redis/graph/-/graph-1.0.0.tgz#baf8eaac4a400f86ea04d65ec3d65715fd7951ab" + integrity sha512-mRSo8jEGC0cf+Rm7q8mWMKKKqkn6EAnA9IA2S3JvUv/gaWW/73vil7GLNwion2ihTptAm05I9LkepzfIXUKX5g== + +"@node-redis/json@1.0.2": + version "1.0.2" + resolved "https://registry.yarnpkg.com/@node-redis/json/-/json-1.0.2.tgz#8ad2d0f026698dc1a4238cc3d1eb099a3bee5ab8" + integrity sha512-qVRgn8WfG46QQ08CghSbY4VhHFgaTY71WjpwRBGEuqGPfWwfRcIf3OqSpR7Q/45X+v3xd8mvYjywqh0wqJ8T+g== + +"@node-redis/search@1.0.3": + version "1.0.3" + resolved "https://registry.yarnpkg.com/@node-redis/search/-/search-1.0.3.tgz#7c3d026bf994caf82019fd0c3924cfc09f041a29" + integrity sha512-rsrzkGWI84di/uYtEctS/4qLusWt0DESx/psjfB0TFpORDhe7JfC0h8ary+eHulTksumor244bXLRSqQXbFJmw== + +"@node-redis/time-series@1.0.2": + version "1.0.2" + resolved "https://registry.yarnpkg.com/@node-redis/time-series/-/time-series-1.0.2.tgz#5dd3638374edd85ebe0aa6b0e87addc88fb9df69" + integrity sha512-HGQ8YooJ8Mx7l28tD7XjtB3ImLEjlUxG1wC1PAjxu6hPJqjPshUZxAICzDqDjtIbhDTf48WXXUcx8TQJB1XTKA== + "@nodelib/fs.scandir@2.1.5": version "2.1.5" resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5" @@ -968,13 +1003,6 @@ resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.4.tgz#cd667bcfdd025213aafb7ca5915a932590acdcdc" integrity sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw== -"@types/redis@^2.8.18": - version "2.8.32" - resolved "https://registry.yarnpkg.com/@types/redis/-/redis-2.8.32.tgz#1d3430219afbee10f8cfa389dad2571a05ecfb11" - integrity sha512-7jkMKxcGq9p242exlbsVzuJb57KqHRhNl4dHoQu2Y5v9bCAbtIXXH0R3HleSQW4CTOqpHIYUW3t6tpUj4BVQ+w== - dependencies: - "@types/node" "*" - "@types/require-from-string@^1.2.0": version "1.2.1" resolved "https://registry.yarnpkg.com/@types/require-from-string/-/require-from-string-1.2.1.tgz#50292d824e2244628c0b961c6a1a583fdc494554" @@ -1906,6 +1934,11 @@ clone-response@1.0.2, clone-response@^1.0.2: dependencies: mimic-response "^1.0.0" +cluster-key-slot@1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/cluster-key-slot/-/cluster-key-slot-1.1.0.tgz#30474b2a981fb12172695833052bc0d01336d10d" + integrity sha512-2Nii8p3RwAPiFwsnZvukotvow2rIHM+yQ6ZcBXGHdniadkYGZYiGmkHJIbZPIV9nfv7m/U1IPMVVcAhoWFeklw== + co@^4.6.0: version "4.6.0" resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184" @@ -2435,11 +2468,6 @@ delegates@^1.0.0: resolved "https://registry.yarnpkg.com/delegates/-/delegates-1.0.0.tgz#84c6e159b81904fdca59a0ef44cd870d31250f9a" integrity sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o= -denque@^1.5.0: - version "1.5.1" - resolved "https://registry.yarnpkg.com/denque/-/denque-1.5.1.tgz#07f670e29c9a78f8faecb2566a1e2c11929c5cbf" - integrity sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw== - depd@~1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9" @@ -3459,6 +3487,11 @@ gauge@^3.0.0: strip-ansi "^6.0.1" wide-align "^1.1.2" +generic-pool@3.8.2: + version "3.8.2" + resolved "https://registry.yarnpkg.com/generic-pool/-/generic-pool-3.8.2.tgz#aab4f280adb522fdfbdc5e5b64d718d3683f04e9" + integrity sha512-nGToKy6p3PAbYQ7p1UlWl6vSPwfwU6TMSWK7TTu+WUY4ZjyZQGniGGt2oNVvyNSpyZYSB43zMXVLcBm08MTMkg== + gensync@^1.0.0-beta.2: version "1.0.0-beta.2" resolved "https://registry.yarnpkg.com/gensync/-/gensync-1.0.0-beta.2.tgz#32a6ee76c3d7f52d46b2b1ae5d93fea8580a25e0" @@ -6537,32 +6570,29 @@ redent@^1.0.0: indent-string "^2.1.0" strip-indent "^1.0.1" -redis-commands@^1.7.0: - version "1.7.0" - resolved "https://registry.yarnpkg.com/redis-commands/-/redis-commands-1.7.0.tgz#15a6fea2d58281e27b1cd1acfb4b293e278c3a89" - integrity sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ== - -redis-errors@^1.0.0, redis-errors@^1.2.0: +redis-errors@^1.0.0: version "1.2.0" resolved "https://registry.yarnpkg.com/redis-errors/-/redis-errors-1.2.0.tgz#eb62d2adb15e4eaf4610c04afe1529384250abad" integrity sha1-62LSrbFeTq9GEMBK/hUpOEJQq60= -redis-parser@^3.0.0: +redis-parser@3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/redis-parser/-/redis-parser-3.0.0.tgz#b66d828cdcafe6b4b8a428a7def4c6bcac31c8b4" integrity sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ= dependencies: redis-errors "^1.0.0" -redis@^3.0.2: - version "3.1.2" - resolved "https://registry.yarnpkg.com/redis/-/redis-3.1.2.tgz#766851117e80653d23e0ed536254677ab647638c" - integrity sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw== +redis@^4.0.4: + version "4.0.4" + resolved "https://registry.yarnpkg.com/redis/-/redis-4.0.4.tgz#b567f82f59086df38433982f7f424b48e924ec7a" + integrity sha512-KaM1OAj/nGrSeybmmOWSMY0LXTGT6FVWgUZZrd2MYzXKJ+VGtqVaciGQeNMfZiQX+kDM8Ke4uttb54m2rm6V0A== dependencies: - denque "^1.5.0" - redis-commands "^1.7.0" - redis-errors "^1.2.0" - redis-parser "^3.0.0" + "@node-redis/bloom" "1.0.1" + "@node-redis/client" "1.0.4" + "@node-redis/graph" "1.0.0" + "@node-redis/json" "1.0.2" + "@node-redis/search" "1.0.3" + "@node-redis/time-series" "1.0.2" regenerator-runtime@^0.13.4: version "0.13.9" @@ -8080,16 +8110,16 @@ y18n@^5.0.5: resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.8.tgz#7f4934d0f7ca8c56f95314939ddcd2dd91ce1d55" integrity sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA== +yallist@4.0.0, yallist@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72" + integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A== + yallist@^2.1.2: version "2.1.2" resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52" integrity sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI= -yallist@^4.0.0: - version "4.0.0" - resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72" - integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A== - yargs-parser@20.x, yargs-parser@^20.2.2: version "20.2.9" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.9.tgz#2eb7dc3b0289718fc295f362753845c41a0c94ee"