Skip to content

Commit

Permalink
Merge pull request #252 from n4ze3m/next
Browse files Browse the repository at this point in the history
1.8.1
  • Loading branch information
n4ze3m committed Apr 22, 2024
2 parents 40ae6be + e95ee8a commit 97083b5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "app",
"private": true,
"version": "1.8.0",
"version": "1.8.1",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 4 additions & 5 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -101,7 +100,7 @@ const worker = new Worker("vector", workerUrl, {
host,
port,
password,
username,
username: process?.env?.DB_REDIS_USERNAME,
},
concurrency,
useWorkerThreads: workerThreads === "true",
Expand Down
8 changes: 3 additions & 5 deletions server/src/plugins/bull.ts
Original file line number Diff line number Diff line change
@@ -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<any>;
Expand All @@ -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,
},
});

Expand Down
78 changes: 78 additions & 0 deletions server/src/utils/redis.ts
Original file line number Diff line number Diff line change
@@ -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<IRedisUrl>();
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;
}

0 comments on commit 97083b5

Please sign in to comment.