From 50242d35e6765301618fbabf04c93aa31339c3db Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Mon, 26 Sep 2022 12:30:37 -0400 Subject: [PATCH] Use node URL to generate redis connection url --- index.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 88c9384..ea6c1e9 100644 --- a/index.js +++ b/index.js @@ -1,16 +1,20 @@ import { createClient } from 'redis'; +import {URL} from 'url'; export const generateConnectUrl = (config = {}) => { - let auth = ''; - let connection = `${config.host}:${config.port ?? 6379}`; - let db = ''; - if (config.username || config.password) { - auth = `${config.user ?? ''}${config.password !== undefined ? `:${config.password}` : ''}@`; + const redisUrl = new URL(`redis://${config.host}`); + redisUrl.port = `${config.port ?? 6379}`; + if(config.username !== undefined) { + redisUrl.username = config.username; } - if (config.db !== undefined) { - db = `/${config.db}`; + if(config.password !== undefined) { + redisUrl.password = config.password; } - return `redis://${auth}${connection}${db}`; + if(config.db !== undefined) { + redisUrl.pathname = `/${config.db}`; + } + + return redisUrl.href; } const redisStore = (args) => {