|
| 1 | +const Redis = require('redis') |
| 2 | + |
| 3 | +const { REDIS_MIN_DB, REDIS_MAX_DB } = process.env |
| 4 | + |
| 5 | +// By default, every Redis instance supports database numbers 0 - 15 |
| 6 | +const redisMinDb = REDIS_MIN_DB || 0 |
| 7 | +const redisMaxDb = REDIS_MAX_DB || 15 |
| 8 | + |
| 9 | +function formatRedisError (error = {}) { |
| 10 | + const { code } = error |
| 11 | + const preamble = error.constructor.name + (code ? ` with code "${code}"` : '') |
| 12 | + return preamble + error.toString() |
| 13 | +} |
| 14 | + |
| 15 | +module.exports = function createClient (options = {}) { |
| 16 | + const { db, name, url } = options |
| 17 | + |
| 18 | + // If no Redis URL is provided, bail out |
| 19 | + // NOTE: Could support other options like `host`, `port`, and `path` but |
| 20 | + // choosing not to for the time being! |
| 21 | + if (!url) return null |
| 22 | + |
| 23 | + // Verify database number is within range |
| 24 | + if (db != null) { |
| 25 | + if (!Number.isInteger(db) || db < redisMinDb || db > redisMaxDb) { |
| 26 | + throw new TypeError( |
| 27 | + `Redis database number must be an integer between ${redisMinDb} and ${redisMaxDb} but was: ${JSON.stringify(db)}` |
| 28 | + ) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Create the client |
| 33 | + const client = Redis.createClient(url, { |
| 34 | + // Only add this configuration for TLS-enabled Redis URL values. |
| 35 | + // Otherwise, it breaks for local Redis instances without TLS enabled. |
| 36 | + ...url.startsWith('rediss://') && { |
| 37 | + tls: { |
| 38 | + // Required for production Heroku Redis |
| 39 | + rejectUnauthorized: false |
| 40 | + } |
| 41 | + }, |
| 42 | + |
| 43 | + // Expand whatever other options and overrides were provided |
| 44 | + ...options |
| 45 | + }) |
| 46 | + |
| 47 | + // If a `name` was provided, use it in the prefix for logging event messages |
| 48 | + const logPrefix = '[redis' + (name ? ` (${name})` : '') + '] ' |
| 49 | + |
| 50 | + // Add event listeners for basic logging |
| 51 | + client.on('connect', () => { console.log(logPrefix, 'Connection opened') }) |
| 52 | + client.on('ready', () => { console.log(logPrefix, 'Ready to receive commands') }) |
| 53 | + client.on( |
| 54 | + 'reconnecting', |
| 55 | + ({ |
| 56 | + attempt, |
| 57 | + delay, |
| 58 | + // The rest are unofficial properties but currently supported |
| 59 | + error, |
| 60 | + total_retry_time: totalRetryTime, |
| 61 | + times_connected: timesConnected |
| 62 | + }) => { |
| 63 | + console.log( |
| 64 | + logPrefix, |
| 65 | + 'Reconnecting,', |
| 66 | + `attempt ${attempt}`, |
| 67 | + `with ${delay} delay`, |
| 68 | + `due to ${formatRedisError(error)}.`, |
| 69 | + `Elapsed time: ${totalRetryTime}.`, |
| 70 | + `Successful connections: ${timesConnected}.` |
| 71 | + ) |
| 72 | + } |
| 73 | + ) |
| 74 | + client.on('end', () => { console.log(logPrefix, 'Connection closed') }) |
| 75 | + client.on('warning', (msg) => { console.warn(logPrefix, 'Warning:', msg) }) |
| 76 | + client.on('error', (error) => { console.error(logPrefix, formatRedisError(error)) }) |
| 77 | + |
| 78 | + return client |
| 79 | +} |
0 commit comments