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 7d50875
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
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)
}
9 changes: 5 additions & 4 deletions packages/@uppy/companion/src/standalone/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,15 @@ const getConfigFromEnv = () => {
// 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 7d50875

Please sign in to comment.