diff --git a/package.json b/package.json index 77810f6e6e..5a760c0284 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,6 @@ "@bull-board/express": "3.8.1", "@elastic/elasticsearch": "7.16.0", "@elastic/elasticsearch-mock": "0.3.1", - "@senecacdot/satellite": "^1.x", "@wordpress/wordcount": "2.15.2", "babel-jest": "27.4.6", "bull": "3.29.3", @@ -65,6 +64,8 @@ "helmet": "4.6.0", "highlight.js": "11.3.1", "http-proxy-middleware": "2.0.1", + "ioredis": "4.28.2", + "ioredis-mock": "5.8.1", "jsdom": "18.1.1", "node-fetch": "2.6.6", "normalize-url": "6.1.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f2003d9c4e..42f6599dd2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -12,7 +12,6 @@ importers: '@bull-board/express': 3.8.1 '@elastic/elasticsearch': 7.16.0 '@elastic/elasticsearch-mock': 0.3.1 - '@senecacdot/satellite': ^1.x '@types/jest': 27.4.0 '@typescript-eslint/eslint-plugin': 4.33.0 '@typescript-eslint/parser': 4.33.0 @@ -52,6 +51,8 @@ importers: highlight.js: 11.3.1 http-proxy-middleware: 2.0.1 husky: 7.0.4 + ioredis: 4.28.2 + ioredis-mock: 5.8.1 jest: 27.4.7 jest-fetch-mock: 3.0.3 jest-playwright-preset: 1.7.0 @@ -81,7 +82,6 @@ importers: '@bull-board/express': 3.8.1 '@elastic/elasticsearch': 7.16.0 '@elastic/elasticsearch-mock': 0.3.1 - '@senecacdot/satellite': 1.17.0 '@wordpress/wordcount': 2.15.2 babel-jest: 27.4.6_@babel+core@7.16.7 bull: 3.29.3 @@ -101,6 +101,8 @@ importers: helmet: 4.6.0 highlight.js: 11.3.1 http-proxy-middleware: 2.0.1 + ioredis: 4.28.2 + ioredis-mock: 5.8.1_ioredis@4.28.2 jsdom: 18.1.1 node-fetch: 2.6.6 normalize-url: 6.1.0 @@ -247,6 +249,8 @@ importers: '@senecacdot/satellite': ^1.x bull: 3.29.3 express-validator: 6.14.0 + ioredis: 4.28.2 + ioredis-mock: 5.8.1 jsdom: 18.1.1 normalize-url: 6.1.0 supertest: 6.1.6 @@ -254,6 +258,8 @@ importers: '@senecacdot/satellite': 1.17.0 bull: 3.29.3 express-validator: 6.14.0 + ioredis: 4.28.2 + ioredis-mock: 5.8.1_ioredis@4.28.2 jsdom: 18.1.1 normalize-url: 6.1.0 devDependencies: diff --git a/src/api/posts/package.json b/src/api/posts/package.json index 92ede69928..b7174835da 100644 --- a/src/api/posts/package.json +++ b/src/api/posts/package.json @@ -19,6 +19,8 @@ "@senecacdot/satellite": "^1.x", "bull": "3.29.3", "express-validator": "6.14.0", + "ioredis": "4.28.2", + "ioredis-mock": "5.8.1", "jsdom": "18.1.1", "normalize-url": "6.1.0" }, diff --git a/src/backend/lib/redis.js b/src/backend/lib/redis.js index f6b7e4d63a..02594bfe3c 100644 --- a/src/backend/lib/redis.js +++ b/src/backend/lib/redis.js @@ -1,8 +1,39 @@ -const { Redis } = require('@senecacdot/satellite'); +require('./config'); +const Redis = require('ioredis'); +const MockRedis = require('ioredis-mock'); +const { logger } = require('../utils/logger'); +const parseUrl = require('../utils/url-parser'); + +// If you need to set the Redis URL, do it in REDIS_URL +const redisUrl = + parseUrl(process.env.REDIS_URL, process.env.REDIS_PORT) || 'redis://127.0.0.1:6379'; + +// Set MOCK_REDIS=1 to mock, MOCK_REDIS= to use real redis +const useMockRedis = process.env.MOCK_REDIS; + +// RedisConstructor is one of Redis or MockRedis +const RedisConstructor = useMockRedis ? MockRedis : Redis; + +function createRedisClient() { + try { + const { port, host } = new URL(redisUrl); + return new RedisConstructor(port, host, { password: process.env.REDIS_PASSWORD }); + } catch (error) { + const message = `Unable to parse port and host from "${redisUrl}"`; + logger.error({ error }, message); + throw new Error(message); + } +} + +// If using MockRedis, shim info() until https://github.com/stipsan/ioredis-mock/issues/841 ships +if (useMockRedis && typeof MockRedis.prototype.info !== 'function') { + logger.debug('Shimming MockRedis info() method'); + MockRedis.prototype.info = () => Promise.resolve('redis_version:999.999.999'); +} module.exports = { // If callers need to create a new redis instance, they'll use the ctor - createRedisClient: Redis, + createRedisClient, // Otherwise they can use this shared instance (most should use this) - redis: Redis(), + redis: createRedisClient(), };