From e95ee8ae2fea3cb6c0d742312cc735dc5bad9eb8 Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Mon, 22 Apr 2024 23:14:27 +0530 Subject: [PATCH] Update package versions and Redis connection handling --- app/ui/package.json | 2 +- package.json | 2 +- server/src/app.ts | 9 ++--- server/src/plugins/bull.ts | 8 ++-- server/src/utils/redis.ts | 78 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 server/src/utils/redis.ts diff --git a/app/ui/package.json b/app/ui/package.json index 3ad61cd2..2ca75cf6 100644 --- a/app/ui/package.json +++ b/app/ui/package.json @@ -1,7 +1,7 @@ { "name": "app", "private": true, - "version": "1.8.0", + "version": "1.8.1", "type": "module", "scripts": { "dev": "vite", diff --git a/package.json b/package.json index 7a4a121c..29b9b80f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dialoqbase", - "version": "1.8.0", + "version": "1.8.1", "description": "Create chatbots with ease", "scripts": { "ui:dev": "pnpm run --filter ui dev", diff --git a/server/src/app.ts b/server/src/app.ts index 7bf351fe..252429be 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -12,6 +12,7 @@ import swagger from "@fastify/swagger"; import swaggerUi from "@fastify/swagger-ui"; import { pathToFileURL } from "url"; import { Worker } from "bullmq"; +import { parseRedisUrl } from "./utils/redis"; declare module "fastify" { interface Session { is_bot_allowed: boolean; @@ -88,10 +89,8 @@ const redis_url = process.env.DB_REDIS_URL || process.env.REDIS_URL; if (!redis_url) { throw new Error("Redis url is not defined"); } -const username = redis_url.split(":")[1].replace("//", ""); -const password = redis_url.split(":")[2].split("@")[0]; -const host = redis_url.split("@")[1].split(":")[0]; -const port = parseInt(redis_url.split(":")[3]); + +const { host, port, password } = parseRedisUrl(redis_url); const path = join(__dirname, "./queue/index.js"); const workerUrl = pathToFileURL(path); const concurrency = parseInt(process.env.DB_QUEUE_CONCURRENCY || "1"); @@ -101,7 +100,7 @@ const worker = new Worker("vector", workerUrl, { host, port, password, - username, + username: process?.env?.DB_REDIS_USERNAME, }, concurrency, useWorkerThreads: workerThreads === "true", diff --git a/server/src/plugins/bull.ts b/server/src/plugins/bull.ts index 3966e490..a1d2c5f3 100644 --- a/server/src/plugins/bull.ts +++ b/server/src/plugins/bull.ts @@ -1,6 +1,7 @@ import fp from "fastify-plugin"; import { FastifyPluginAsync } from "fastify"; import { Queue } from "bullmq"; +import { parseRedisUrl } from "../utils/redis"; declare module "fastify" { interface FastifyInstance { queue: Queue; @@ -12,17 +13,14 @@ const bullPlugin: FastifyPluginAsync = fp(async (server, options) => { if (!redis_url) { throw new Error("Redis url is not defined"); } - const username = redis_url.split(":")[1].replace("//", ""); - const password = redis_url.split(":")[2].split("@")[0]; - const host = redis_url.split("@")[1].split(":")[0]; - const port = parseInt(redis_url.split(":")[3]); + const { host, port, password } = parseRedisUrl(redis_url); const queue = new Queue("vector", { connection: { host, port, password, - username, + username: process?.env?.DB_REDIS_USERNAME, }, }); diff --git a/server/src/utils/redis.ts b/server/src/utils/redis.ts new file mode 100644 index 00000000..cf649a8d --- /dev/null +++ b/server/src/utils/redis.ts @@ -0,0 +1,78 @@ +// copied from https://github.com/glani/parse-redis-url-simple/blob/master/src/index.ts +import { parse } from "url"; + +const redisDefaultPort = 6379; +const sentinelDefaultPort = 26379; + +export interface IRedisUrl { + database?: string; + host: string; + password?: string; + port: number; +} + +const predefinedSeparatorRegexp = /,|;|\s/; + +function preparePassword(auth: string | null, encoding?: BufferEncoding) { + if (!auth) { + return undefined; + } + + const vv = (encoding ? Buffer.from(auth, encoding).toString() : auth).split( + ":" + ); + return vv.length > 1 ? vv[1] : vv[0]; +} + +function prepareResult( + v: string, + sentinel: boolean, + encoding?: BufferEncoding +): IRedisUrl { + if (v.search("://") === -1) { + v = "redis://" + v; + } + const urlWithStringQuery = parse(v); + + return { + database: sentinel + ? undefined + : (urlWithStringQuery.pathname || "/0").substr(1) || "0", + host: urlWithStringQuery.hostname || "localhost", + password: sentinel + ? undefined + : preparePassword(urlWithStringQuery.auth, encoding), + port: Number( + urlWithStringQuery.port || + (sentinel ? sentinelDefaultPort : redisDefaultPort) + ), + }; +} + +export function parseRedisUrl( + value?: string, + sentinel: boolean = false, + separatorRegexp: RegExp = predefinedSeparatorRegexp, + encoding?: BufferEncoding +): IRedisUrl | undefined { + if (!value) { + return { + database: sentinel ? undefined : "0", + host: "localhost", + port: sentinel ? sentinelDefaultPort : redisDefaultPort, + }; + } + + const result = new Array(); + const urlValues = value + .split(separatorRegexp) + .map((value1) => value1.trim()) + .filter((value1) => value1 && value1.length); + + for (const urlValue of urlValues) { + const parsedResult = prepareResult(urlValue, sentinel, encoding); + result.push(parsedResult); + } + + return result.length > 0 ? result[0] : undefined; +}