Skip to content
Merged
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
7 changes: 6 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,10 @@ export class Client {
}

static async connect(params: ClientParams, logger?: Logger): Promise<Client> {
return new Client(logger ?? new NullLogger(), params).start()
return new Client(logger ?? new NullLogger(), {
...params,
vhost: getVhostOrDefault(params.vhost),
}).start()
}
}

Expand Down Expand Up @@ -837,3 +840,5 @@ const extractConsumerId = (extendedConsumerId: string) => {
const extractPublisherId = (extendedPublisherId: string) => {
return parseInt(extendedPublisherId.split("@").shift() ?? "0")
}

const getVhostOrDefault = (vhost: string) => vhost ?? "/"
18 changes: 16 additions & 2 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ export class Connection {
mechanism: this.params.mechanism ?? "PLAIN",
})
const { heartbeat } = await this.tune(this.params.heartbeat ?? 0)
await this.open({ virtualHost: this.params.vhost })
const connectionOpened = await this.open({ virtualHost: this.params.vhost })
if (!connectionOpened.ok) return rej(connectionOpened.error)
if (!this.heartbeat.started) this.heartbeat.start(heartbeat)
await this.exchangeCommandVersions()
this.setupCompleted = true
Expand Down Expand Up @@ -461,12 +462,25 @@ export class Connection {

private async open(params: { virtualHost: string }) {
this.logger.debug(`Open ...`)
if (this.virtualHostIsNotValid(params.virtualHost)) {
const errorMessage = `[ERROR]: VirtualHost '${params.virtualHost}' is not valid`
this.logger.error(errorMessage)
return { ok: false, error: new Error(errorMessage) }
}
const res = await this.sendAndWait<OpenResponse>(new OpenRequest(params))
this.logger.debug(`Open response: ${res.ok} - '${inspect(res.properties)}'`)
const advertisedHost = res.properties["advertised_host"] ?? ""
const advertisedPort = parseInt(res.properties["advertised_port"] ?? "5552")
this.serverEndpoint = { host: advertisedHost, port: advertisedPort }
return res
return { ok: true, response: res }
}

private virtualHostIsNotValid(virtualHost: string) {
if (!virtualHost || virtualHost.split("/").length !== 2) {
return true
}

return false
}

private async tune(heartbeatInterval: number): Promise<{ heartbeat: number }> {
Expand Down
22 changes: 21 additions & 1 deletion test/e2e/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from "chai"
import { Client, connect } from "../../src"
import { createClient } from "../support/fake_data"
import { Rabbit } from "../support/rabbit"
import { eventually, username, password, getTestNodesFromEnv } from "../support/util"
import { eventually, username, password, getTestNodesFromEnv, expectToThrowAsync } from "../support/util"
import { Version } from "../../src/versions"
import { randomUUID } from "node:crypto"
import { readFile } from "node:fs/promises"
Expand Down Expand Up @@ -46,6 +46,26 @@ describe("connect", () => {
}, 5000)
}).timeout(10000)

it("throw exception if vhost is not valid", async () => {
const [firstNode] = getTestNodesFromEnv()

await expectToThrowAsync(
async () => {
client = await connect({
hostname: firstNode.host,
port: firstNode.port,
username,
password,
vhost: "",
frameMax: 0,
heartbeat: 0,
})
},
Error,
`[ERROR]: VirtualHost '' is not valid`
)
}).timeout(10000)

it("using EXTERNAL auth", async () => {
client = await createTlsClient()

Expand Down