diff --git a/package-lock.json b/package-lock.json index 223ee14..e9de10e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34,6 +34,7 @@ "cross-env": "^7.0.3", "dir-compare": "^4.0.0", "eslint": "^6.6.0", + "ioredis-mock": "^8.2.2", "jest": "^24.9.0", "prettier": "^1.19.1", "rimraf": "^3.0.2", @@ -1847,6 +1848,12 @@ "integrity": "sha512-0o4l6pZC+hI88+bzuaX/6BgOvQVhbt2PfmxauVaYOGgbsAw14wdKyvMCZXnsnsHys94iadcF+RG/wZyx6+ZZBw==", "dev": true }, + "node_modules/@ioredis/as-callback": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@ioredis/as-callback/-/as-callback-3.0.0.tgz", + "integrity": "sha512-Kqv1rZ3WbgOrS+hgzJ5xG5WQuhvzzSTRYvNeyPMLOAM78MHSnuKI20JeJGbpuAt//LCuP0vsexZcorqW7kWhJg==", + "dev": true + }, "node_modules/@ioredis/commands": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@ioredis/commands/-/commands-1.2.0.tgz", @@ -6080,6 +6087,32 @@ "bser": "2.1.1" } }, + "node_modules/fengari": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/fengari/-/fengari-0.1.4.tgz", + "integrity": "sha512-6ujqUuiIYmcgkGz8MGAdERU57EIluGGPSUgGPTsco657EHa+srq0S3/YUl/r9kx1+D+d4rGfYObd+m8K22gB1g==", + "dev": true, + "dependencies": { + "readline-sync": "^1.4.9", + "sprintf-js": "^1.1.1", + "tmp": "^0.0.33" + } + }, + "node_modules/fengari-interop": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/fengari-interop/-/fengari-interop-0.1.3.tgz", + "integrity": "sha512-EtZ+oTu3kEwVJnoymFPBVLIbQcCoy9uWCVnMA6h3M/RqHkUBsLYp29+RRHf9rKr6GwjubWREU1O7RretFIXjHw==", + "dev": true, + "peerDependencies": { + "fengari": "^0.1.0" + } + }, + "node_modules/fengari/node_modules/sprintf-js": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.2.tgz", + "integrity": "sha512-VE0SOVEHCk7Qc8ulkWw3ntAzXuqf7S2lvwQaDLRnUeIEaKNQJzV6BwmLKhOqT61aGhfUMrXeaBk+oDGCzvhcug==", + "dev": true + }, "node_modules/figures": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/figures/-/figures-3.2.0.tgz", @@ -7171,6 +7204,24 @@ "url": "https://opencollective.com/ioredis" } }, + "node_modules/ioredis-mock": { + "version": "8.2.2", + "resolved": "https://registry.npmjs.org/ioredis-mock/-/ioredis-mock-8.2.2.tgz", + "integrity": "sha512-XyJfcF6pqcLHwAYtldkzaLtjRxPw7d8U0FUfjgQ5U/d0vVhFxiXbqsILR4FEOp+ygzyZgBA8xye+uPKu74IH1A==", + "dev": true, + "dependencies": { + "@ioredis/as-callback": "^3.0.0", + "@ioredis/commands": "^1.1.1", + "fengari": "^0.1.4", + "fengari-interop": "^0.1.3" + }, + "engines": { + "node": ">=12" + }, + "peerDependencies": { + "ioredis": "5.x" + } + }, "node_modules/ioredis/node_modules/denque": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz", @@ -13989,6 +14040,15 @@ "node": ">=0.10.0" } }, + "node_modules/readline-sync": { + "version": "1.4.10", + "resolved": "https://registry.npmjs.org/readline-sync/-/readline-sync-1.4.10.tgz", + "integrity": "sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw==", + "dev": true, + "engines": { + "node": ">= 0.8.0" + } + }, "node_modules/realpath-native": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/realpath-native/-/realpath-native-1.1.0.tgz", @@ -18270,6 +18330,12 @@ "integrity": "sha512-0o4l6pZC+hI88+bzuaX/6BgOvQVhbt2PfmxauVaYOGgbsAw14wdKyvMCZXnsnsHys94iadcF+RG/wZyx6+ZZBw==", "dev": true }, + "@ioredis/as-callback": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@ioredis/as-callback/-/as-callback-3.0.0.tgz", + "integrity": "sha512-Kqv1rZ3WbgOrS+hgzJ5xG5WQuhvzzSTRYvNeyPMLOAM78MHSnuKI20JeJGbpuAt//LCuP0vsexZcorqW7kWhJg==", + "dev": true + }, "@ioredis/commands": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@ioredis/commands/-/commands-1.2.0.tgz", @@ -21611,6 +21677,32 @@ "bser": "2.1.1" } }, + "fengari": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/fengari/-/fengari-0.1.4.tgz", + "integrity": "sha512-6ujqUuiIYmcgkGz8MGAdERU57EIluGGPSUgGPTsco657EHa+srq0S3/YUl/r9kx1+D+d4rGfYObd+m8K22gB1g==", + "dev": true, + "requires": { + "readline-sync": "^1.4.9", + "sprintf-js": "^1.1.1", + "tmp": "^0.0.33" + }, + "dependencies": { + "sprintf-js": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.2.tgz", + "integrity": "sha512-VE0SOVEHCk7Qc8ulkWw3ntAzXuqf7S2lvwQaDLRnUeIEaKNQJzV6BwmLKhOqT61aGhfUMrXeaBk+oDGCzvhcug==", + "dev": true + } + } + }, + "fengari-interop": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/fengari-interop/-/fengari-interop-0.1.3.tgz", + "integrity": "sha512-EtZ+oTu3kEwVJnoymFPBVLIbQcCoy9uWCVnMA6h3M/RqHkUBsLYp29+RRHf9rKr6GwjubWREU1O7RretFIXjHw==", + "dev": true, + "requires": {} + }, "figures": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/figures/-/figures-3.2.0.tgz", @@ -22448,6 +22540,18 @@ } } }, + "ioredis-mock": { + "version": "8.2.2", + "resolved": "https://registry.npmjs.org/ioredis-mock/-/ioredis-mock-8.2.2.tgz", + "integrity": "sha512-XyJfcF6pqcLHwAYtldkzaLtjRxPw7d8U0FUfjgQ5U/d0vVhFxiXbqsILR4FEOp+ygzyZgBA8xye+uPKu74IH1A==", + "dev": true, + "requires": { + "@ioredis/as-callback": "^3.0.0", + "@ioredis/commands": "^1.1.1", + "fengari": "^0.1.4", + "fengari-interop": "^0.1.3" + } + }, "is-accessor-descriptor": { "version": "0.1.6", "resolved": "https://registry.npmjs.org/is-accessor-descriptor/-/is-accessor-descriptor-0.1.6.tgz", @@ -27542,6 +27646,12 @@ } } }, + "readline-sync": { + "version": "1.4.10", + "resolved": "https://registry.npmjs.org/readline-sync/-/readline-sync-1.4.10.tgz", + "integrity": "sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw==", + "dev": true + }, "realpath-native": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/realpath-native/-/realpath-native-1.1.0.tgz", diff --git a/package.json b/package.json index 76977cb..939bbc5 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "cross-env": "^7.0.3", "dir-compare": "^4.0.0", "eslint": "^6.6.0", + "ioredis-mock": "^8.2.2", "jest": "^24.9.0", "prettier": "^1.19.1", "rimraf": "^3.0.2", diff --git a/src/plugin.ts b/src/plugin.ts index d44db38..c3ffaed 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -26,10 +26,13 @@ export default class RedisStorage implements IPluginStorage { public redisClient: Redis; public db: Database; - public constructor(config: RedisConfig, options: PluginOptions) { + public constructor(config: RedisConfig, options: PluginOptions, redisClient?: Redis) { this.config = config; this.logger = options.logger; - this.redisClient = redisCreateClient(this.config, this.logger); + if (redisClient !== undefined) + this.redisClient = redisClient; + else + this.redisClient = redisCreateClient(this.config, this.logger); this.db = new Database(this.redisClient, this.logger); } diff --git a/tests/commands.test.ts b/tests/commands.test.ts index 0ccbf24..5deb563 100644 --- a/tests/commands.test.ts +++ b/tests/commands.test.ts @@ -5,6 +5,7 @@ import os from 'os'; import rimraf from 'rimraf'; import { compareSync } from 'dir-compare'; +import Redis from "ioredis-mock"; import RedisStorage from '../src/plugin'; import { TEST_REDIS_PREFIX } from '../src/utils'; import { restoreWithContext, ICommandContext, dumpWithContext } from '../src/commands'; @@ -20,7 +21,8 @@ describe('redis storage CLI test', () => { beforeEach(done => { // Create redis storage const defaultConfig = { logger, config }; - redisStorage = new RedisStorage(config, defaultConfig); + const redisClient = new Redis(config); + redisStorage = new RedisStorage(config, defaultConfig, redisClient); // Create dump dir const tempDirPrefix = path.join(os.tmpdir(), 'verdaccio-redis-storage-'); fs.mkdtemp(tempDirPrefix, (err, folder) => { diff --git a/tests/mocks/config.ts b/tests/mocks/config.ts index 183dc20..f378f82 100644 --- a/tests/mocks/config.ts +++ b/tests/mocks/config.ts @@ -1,4 +1,5 @@ import { RedisConfig } from '../../types'; +import { TEST_REDIS_PREFIX } from '../constants'; const config: RedisConfig = { user_agent: 'string', diff --git a/tests/plugin.test.ts b/tests/plugin.test.ts index 20c1262..9585197 100644 --- a/tests/plugin.test.ts +++ b/tests/plugin.test.ts @@ -2,6 +2,7 @@ import { IPackageStorage, ILocalPackageManager, Token } from '@verdaccio/types'; import { HTTP_STATUS, VerdaccioError, getBadRequest, getInternalError } from '@verdaccio/commons-api'; import RedisStorage from '../src/plugin'; +import Redis from "ioredis-mock"; import { TEST_REDIS_PREFIX, REDIS_KEY, bufferStreamToBase64String } from '../src/utils'; import StoragePluginManager from '../src/PackageStorage'; @@ -16,8 +17,9 @@ const TarballBuffer = new Buffer(TarballBase64, 'base64'); describe('redis storage unit test', () => { let redisStorage: RedisStorage; beforeEach(() => { - const defaultConfig = { logger, config: null }; - redisStorage = new RedisStorage(config, defaultConfig); + const defaultConfig = { logger, config }; + const redisClient = new Redis(config); + redisStorage = new RedisStorage(config, defaultConfig, redisClient); }); afterEach(async () => {