Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/queue/src/connection.ts
T

104 lines
3.5 KiB
TypeScript

export type ConnectionOptions = {
db?: number;
host: string;
maxRetriesPerRequest: null;
password?: string;
port: number;
tls?: Record<string, never>;
username?: string;
};
export type RedisConnectionEnv = {
REDIS_URL?: string;
BULLMQ_REDIS_URL?: string;
};
export type ResolveRedisConnectionOptions = {
env?: RedisConnectionEnv;
required?: boolean;
};
export function resolveRedisConnectionOptions(options: ResolveRedisConnectionOptions & { required: true }): ConnectionOptions;
export function resolveRedisConnectionOptions(options?: ResolveRedisConnectionOptions): ConnectionOptions | undefined;
export function resolveRedisConnectionOptions(options: ResolveRedisConnectionOptions = {}): ConnectionOptions | undefined {
const redisUrl = selectRedisUrl(options.env);
if (!redisUrl) {
if (options.required) {
throw new Error("Set BULLMQ_REDIS_URL or REDIS_URL before starting queue workers.");
}
return undefined;
}
return parseRedisUrl(redisUrl.name, redisUrl.value);
}
export function resolveRedisConnectionOptionsFromProcessEnv(options: Omit<ResolveRedisConnectionOptions, "env"> & { required: true }): ConnectionOptions;
export function resolveRedisConnectionOptionsFromProcessEnv(options?: Omit<ResolveRedisConnectionOptions, "env">): ConnectionOptions | undefined;
export function resolveRedisConnectionOptionsFromProcessEnv(options: Omit<ResolveRedisConnectionOptions, "env"> = {}): ConnectionOptions | undefined {
return resolveRedisConnectionOptions({ ...options, env: process.env as RedisConnectionEnv });
}
export function parseRedisUrl(redisUrl: string): ConnectionOptions;
export function parseRedisUrl(name: keyof RedisConnectionEnv, redisUrl: string): ConnectionOptions;
export function parseRedisUrl(nameOrRedisUrl: keyof RedisConnectionEnv | string, redisUrl?: string): ConnectionOptions {
const name = redisUrl === undefined ? "BULLMQ_REDIS_URL" : nameOrRedisUrl;
const value = redisUrl ?? nameOrRedisUrl;
let url: URL;
try {
url = new URL(value);
} catch {
throw new Error(`${name} must be a valid Redis URL.`);
}
if (url.protocol !== "redis:" && url.protocol !== "rediss:") {
throw new Error(`${name} must use redis:// or rediss://.`);
}
return {
db: parseRedisDatabase(name, url),
host: url.hostname,
maxRetriesPerRequest: null,
password: url.password ? decodeURIComponent(url.password) : undefined,
port: url.port ? Number(url.port) : 6379,
tls: url.protocol === "rediss:" ? {} : undefined,
username: url.username ? decodeURIComponent(url.username) : undefined,
};
}
function selectRedisUrl(env: RedisConnectionEnv | undefined): { name: keyof RedisConnectionEnv; value: string } | undefined {
const bullmqUrl = normalizeRedisUrl(env?.BULLMQ_REDIS_URL);
if (bullmqUrl) {
return { name: "BULLMQ_REDIS_URL", value: bullmqUrl };
}
const redisUrl = normalizeRedisUrl(env?.REDIS_URL);
if (redisUrl) {
return { name: "REDIS_URL", value: redisUrl };
}
return undefined;
}
function normalizeRedisUrl(value: string | undefined): string | undefined {
const normalized = value?.trim();
return normalized ? normalized : undefined;
}
function parseRedisDatabase(name: keyof RedisConnectionEnv | string, url: URL): number | undefined {
const database = url.pathname.replace(/^\//, "");
if (!database) {
return undefined;
}
if (!/^\d+$/.test(database)) {
throw new Error(`${name} database index must be a non-negative integer.`);
}
return Number(database);
}