Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.8.1 #252

Merged
merged 1 commit into from
Apr 22, 2024
Merged

1.8.1 #252

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}