UNPKG

@o1s/redis-testbench

Version:

Redis (single instance and cluster) test bench

130 lines (129 loc) 4.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildClient = exports.buildOptionsFromEnv = void 0; const ioredis_1 = __importDefault(require("ioredis")); /** * Run against redis cluster, inside docker container: * REDIS_AUTH="rate-limit" \ * REDIS_HOSTS="redis-node-0" \ * node test * * Run against redis cluster, outside docker container: * REDIS_AUTH="rate-limit" \ * REDIS_PORTS=6380,6381,6382,6383,6384,6385 \ * node test * * Run out or inside docker container against single redis instance: * node test */ const commonOptions = { password: process.env.REDIS_AUTH }; /** * * @param port */ const buildNatEntry = async (port) => { const redis = new ioredis_1.default(port, 'localhost', commonOptions); /** * 02bd5cce38a98b78695941c9b260bfd349fa3fd4 :0@0 master,noaddr - 1637600707305 1637600707305 2 disconnected 5461-10922 * 28b6eb1e117ff9d52449c5811e9f558e344a1745 172.21.0.18:6379@16379 slave 02bd5cce38a98b78695941c9b260bfd349fa3fd4 0 1637609467235 2 connected * 68926e862b2fa792420b9253ffe182faedfe18e8 172.21.0.3:6379@16379 slave d994f114f284a6f0fa2e2afb84e7cdb9c4c780f3 0 1637609466231 3 connected * d994f114f284a6f0fa2e2afb84e7cdb9c4c780f3 172.21.0.2:6379@16379 master - 0 1637609468238 3 connected 10923-16383 * 7e19538fc2650358ff2b4e45ed9753b0260b6506 172.21.0.6:6379@16379 slave 2191412b364ca3383dbba96c9a864c0e40c25962 0 1637609469241 1 connected * 2191412b364ca3383dbba96c9a864c0e40c25962 172.21.0.5:6379@16379 myself,master - 0 1637609467000 1 connected 0-5460 */ const nodeListing = await redis.cluster('nodes'); return nodeListing .split("\n") .find((line) => line.match('myself')) /** * at this point, the line would look like: * node_hash_id ip:6379:16379 myself,$type primary_hash_id ... * We want: "ip:6379" */ .split(' ')[1] .replace(/@.*/, ''); }; /** * Returns `buildClient` options from environment variables: * * Accessing redis cluster from outside of docker-compose: * - REDIS_PORTS=6380,6381,6382,6383,6384,6385 * * Accessing redis cluster from inside the docker-compose: * - REDIS_HOSTS=redis-node-0 * * Accessing redis instance from outside of docker-compose: * - REDIS_PORT=6379 * * Accessing redis instance from inside of docker-compose: * - REDIS_PORT=6379 */ const buildOptionsFromEnv = () => { if (process.env.REDIS_PORTS) { return { ports: process.env.REDIS_PORTS.split(',') .map((sPort) => parseInt(sPort, 10)) }; } if (process.env.REDIS_HOSTS) { return { hosts: process.env.REDIS_HOSTS.split(',') }; } return { host: process.env.REDIS_HOST || '127.0.0.1', port: parseInt(process.env.REDIS_PORT || '6379', 10) }; }; exports.buildOptionsFromEnv = buildOptionsFromEnv; /** * Create either a redis or redis cluster client (from IORedis package). * If a redis cluster client is requested, IORedis natMap is resolved. * * Accessing redis cluster from outside of docker-compose: * - buildClient({ ports: [ 6380, 6381, 6382, 6383, 6384, 6385 ] }) * * Accessing redis cluster from inside the docker-compose: * - buildClient({ hosts: [ 'redis-node-0' ] }) * * Accessing redis instance from outside of docker-compose: * - buildClient({ port: 6379 }) * * Accessing redis instance from inside of docker-compose: * - buildClient({ port: 6379 }) */ const buildClient = async (options) => { if ("ports" in options && options.ports) { /** running against the local redis cluster, outside of docker-composer */ const nodes = []; const natMap = {}; await Promise.all(options.ports .map(async (port) => { nodes.push({ port }); natMap[await buildNatEntry(port)] = { host: 'localhost', port }; })); return new ioredis_1.default.Cluster(nodes, { natMap, redisOptions: commonOptions, scaleReads: options.noReplicas ? 'master' : 'slave' }); } else if ("hosts" in options && options.hosts) { /** running against the local redis cluster, inside of docker-composer */ const host = options.hosts[0]; return new ioredis_1.default.Cluster([{ host }], { redisOptions: commonOptions, scaleReads: options.noReplicas ? 'master' : 'slave' }); } else { /** running against a single redis instance */ return new ioredis_1.default(("port" in options && options.port) || 6379, ("host" in options && options.host) || '127.0.0.1', commonOptions); } }; exports.buildClient = buildClient;