From 2c5f6f585e102beafbdcf9216def44ea8e9ee579 Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Mon, 26 Sep 2022 12:27:42 -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..1e35ce7 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) => {