export interface AmqpConnectOptions { hostname?: string;
port?: number;
username?: string;
password?: string;
vhost?: string;
heartbeatInterval?: number;
frameMax?: number;
loglevel?: "debug" | "none"; tls?: boolean;}
export interface AmqpConnectParameters { hostname: string; port: number; username: string; password: string; vhost: string; heartbeatInterval?: number; frameMax?: number; loglevel: "debug" | "none"; tls?: boolean;}
function resolvePort(url: URL): number { if (url.port) { return parseInt(url.port); }
if (url.protocol === "amqp:") { return 5672; }
if (url.protocol === "amqps:") { return 5671; }
throw new Error("Unsupported protocol");}
function resolveTls(url: URL): boolean { return url.protocol === "amqps:";}
function parseUrl(value: string): AmqpConnectOptions { const url = new URL(value);
if (!["amqp:", "amqps:"].includes(url.protocol)) { throw new Error("Unsupported protocol"); }
const heartbeatParam = url.searchParams.get("heartbeat"); const heartbeat = heartbeatParam ? parseInt(heartbeatParam) : undefined; if (heartbeat !== undefined && isNaN(heartbeat)) { throw new Error(`Invalid heartbeat parameter ${heartbeatParam}`); }
const frameMaxParam = url.searchParams.get("frame_max"); const frameMax = frameMaxParam ? parseInt(frameMaxParam) : undefined; if (frameMax !== undefined && isNaN(frameMax)) { throw new Error(`Invalid frame_max parameter ${frameMaxParam}`); }
return { hostname: url.hostname, port: resolvePort(url), username: url.username || "guest", password: url.password || "guest", tls: resolveTls(url), vhost: url.pathname.length > 0 ? decodeURIComponent(url.pathname.substring(1)) : "/", heartbeatInterval: heartbeat, frameMax: frameMax, };}
export function parseOptions( optionsOrString: string | AmqpConnectOptions = {},): AmqpConnectParameters { const { hostname = "localhost", port = 5672, username = "guest", password = "guest", heartbeatInterval, loglevel = "none", vhost = "/", frameMax, tls = false, } = typeof optionsOrString === "string" ? parseUrl(optionsOrString) : optionsOrString;
return { hostname, port, username, password, heartbeatInterval, loglevel, vhost, frameMax, tls, };}