Update index.js

This commit is contained in:
SrGooglo 2025-04-24 06:08:36 +00:00
parent 80acb13912
commit 652cd747b1

View File

@ -1,11 +1,6 @@
import Redis from "ioredis"
export function composeURL({
host,
port,
username,
password,
} = {}) {
export function composeURL({ host, port, username, password } = {}) {
let url = "redis://"
if (username && password) {
@ -21,36 +16,34 @@ export function composeURL({
return url
}
export default () => {
let { REDIS_HOST, REDIS_PORT, REDIS_NO_AUTH, REDIS_AUTH, REDIS_DB } = process.env
REDIS_NO_AUTH = ToBoolean(REDIS_NO_AUTH)
export default (params = {}) => {
let { REDIS_HOST, REDIS_PORT, REDIS_NO_AUTH, REDIS_AUTH, REDIS_DB } =
process.env
let clientOptions = {
host: REDIS_HOST,
port: REDIS_PORT,
host: REDIS_HOST ?? "localhost",
port: REDIS_PORT ?? 6379,
lazyConnect: true,
autoConnect: false
autoConnect: false,
...params,
}
if (!REDIS_NO_AUTH) {
if (REDIS_AUTH) {
// if redis auth is provided, set username and password
if (!ToBoolean(REDIS_NO_AUTH) && REDIS_AUTH) {
const [user, password] = REDIS_AUTH.split(":")
clientOptions.username = user
clientOptions.password = password
}
} else {
console.log("⚠️ Redis auth is disabled")
}
// if redis db is provided, set db
if (REDIS_DB) {
clientOptions.db = REDIS_DB
}
clientOptions = composeURL(clientOptions)
let client = new Redis(clientOptions.host, clientOptions.port, clientOptions)
let client = new Redis(clientOptions)
client.on("error", (error) => {
console.error("❌ Redis client error:", error)
@ -74,6 +67,6 @@ export default () => {
return {
client,
initialize
initialize,
}
}