Skip to content

Commit

Permalink
Fix redisUrl and redisOptions handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dschmidt committed Jul 20, 2023
1 parent 1c727d2 commit a083001
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/@uppy/companion/src/companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ module.exports.app = (optionsArg = {}) => {
logger.setMaskables(getMaskableSecrets(options))

// create singleton redis client
if (options.redisOptions) {
redis.client(options.redisOptions)
if (options.redisUrl || options.redisOptions) {
redis.client(options.redisUrl, options.redisOptions)
}
const emitter = createEmitter(options.redisOptions, options.redisPubSubScope)

Expand Down
17 changes: 11 additions & 6 deletions packages/@uppy/companion/src/server/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@ let redisClient
* A Singleton module that provides a single redis client through out
* the lifetime of the server
*
* @param {Record<string, any>} [opts] node-redis client options
* @param {string} [redisUrl] ioredis url
* @param {Record<string, any>} [redisOptions] ioredis client options
*/
function createClient (opts) {
function createClient (redisUrl, redisOptions) {
if (!redisClient) {
redisClient = new Redis(opts)
if (redisUrl) {
redisClient = new Redis(redisUrl, redisOptions)
} else {
redisClient = new Redis(redisOptions)
}

redisClient.on('error', err => logger.error('redis error', err.toString()))
}

return redisClient
}

module.exports.client = (redisOptions) => {
if (!redisOptions) {
module.exports.client = (redisUrl, redisOptions) => {
if (!redisUrl && !redisOptions) {
return redisClient
}

return createClient(redisOptions)
return createClient(redisUrl, redisOptions)
}
10 changes: 6 additions & 4 deletions packages/@uppy/companion/src/standalone/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,20 @@ const getConfigFromEnv = () => {
? parseInt(process.env.COMPANION_PERIODIC_PING_COUNT, 10) : undefined,
filePath: process.env.COMPANION_DATADIR,
redisPubSubScope: process.env.COMPANION_REDIS_PUBSUB_SCOPE,
redisUrl: process.env.COMPANION_REDIS_URL,
// adding redisOptions to keep all companion options easily visible
// redisOptions refers to https://redis.github.io/ioredis/index.html#RedisOptions
redisOptions: (() => {
try {
if (!process.env.COMPANION_REDIS_OPTIONS) {
return undefined
}
const obj = JSON.parse(process.env.COMPANION_REDIS_OPTIONS)
return obj
} catch (e) {
if (process.env.COMPANION_REDIS_OPTIONS) {
console.log('COMPANION_REDIS_OPTIONS parse error', e)
}
return process.env.COMPANION_REDIS_URL
console.log('COMPANION_REDIS_OPTIONS parse error', e)
}
return undefined
})(),
sendSelfEndpoint: process.env.COMPANION_SELF_ENDPOINT,
uploadUrls: uploadUrls ? uploadUrls.split(',') : null,
Expand Down
4 changes: 2 additions & 2 deletions packages/@uppy/companion/src/standalone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ module.exports = function server (inputCompanionOptions) {
saveUninitialized: true,
}

if (companionOptions.redisUrl) {
const redisClient = redis.client(companionOptions)
if (companionOptions.redisUrl || companionOptions.redisOptions) {
const redisClient = redis.client(companionOptions.redisUrl, companionOptions.redisOptions)
// todo next major: change default prefix to something like "companion-session:" and possibly remove this option
sessionOptions.store = new RedisStore({ client: redisClient, prefix: process.env.COMPANION_REDIS_EXPRESS_SESSION_PREFIX || 'sess:' })
}
Expand Down

0 comments on commit a083001

Please sign in to comment.