Skip to content

Commit 6843499

Browse files
l4mbymagne
andauthored
fix: get default vhost if empty string is provided (coders51#241)
* fix: get default vhost if empty string is provided * fix: reject on invalid vhost --------- Co-authored-by: magne <[email protected]>
1 parent 3f35c40 commit 6843499

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/client.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,10 @@ export class Client {
704704
}
705705

706706
static async connect(params: ClientParams, logger?: Logger): Promise<Client> {
707-
return new Client(logger ?? new NullLogger(), params).start()
707+
return new Client(logger ?? new NullLogger(), {
708+
...params,
709+
vhost: getVhostOrDefault(params.vhost),
710+
}).start()
708711
}
709712
}
710713

@@ -837,3 +840,5 @@ const extractConsumerId = (extendedConsumerId: string) => {
837840
const extractPublisherId = (extendedPublisherId: string) => {
838841
return parseInt(extendedPublisherId.split("@").shift() ?? "0")
839842
}
843+
844+
const getVhostOrDefault = (vhost: string) => vhost ?? "/"

src/connection.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ export class Connection {
152152
mechanism: this.params.mechanism ?? "PLAIN",
153153
})
154154
const { heartbeat } = await this.tune(this.params.heartbeat ?? 0)
155-
await this.open({ virtualHost: this.params.vhost })
155+
const connectionOpened = await this.open({ virtualHost: this.params.vhost })
156+
if (!connectionOpened.ok) return rej(connectionOpened.error)
156157
if (!this.heartbeat.started) this.heartbeat.start(heartbeat)
157158
await this.exchangeCommandVersions()
158159
this.setupCompleted = true
@@ -461,12 +462,25 @@ export class Connection {
461462

462463
private async open(params: { virtualHost: string }) {
463464
this.logger.debug(`Open ...`)
465+
if (this.virtualHostIsNotValid(params.virtualHost)) {
466+
const errorMessage = `[ERROR]: VirtualHost '${params.virtualHost}' is not valid`
467+
this.logger.error(errorMessage)
468+
return { ok: false, error: new Error(errorMessage) }
469+
}
464470
const res = await this.sendAndWait<OpenResponse>(new OpenRequest(params))
465471
this.logger.debug(`Open response: ${res.ok} - '${inspect(res.properties)}'`)
466472
const advertisedHost = res.properties["advertised_host"] ?? ""
467473
const advertisedPort = parseInt(res.properties["advertised_port"] ?? "5552")
468474
this.serverEndpoint = { host: advertisedHost, port: advertisedPort }
469-
return res
475+
return { ok: true, response: res }
476+
}
477+
478+
private virtualHostIsNotValid(virtualHost: string) {
479+
if (!virtualHost || virtualHost.split("/").length !== 2) {
480+
return true
481+
}
482+
483+
return false
470484
}
471485

472486
private async tune(heartbeatInterval: number): Promise<{ heartbeat: number }> {

test/e2e/connect.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from "chai"
22
import { Client, connect } from "../../src"
33
import { createClient } from "../support/fake_data"
44
import { Rabbit } from "../support/rabbit"
5-
import { eventually, username, password, getTestNodesFromEnv } from "../support/util"
5+
import { eventually, username, password, getTestNodesFromEnv, expectToThrowAsync } from "../support/util"
66
import { Version } from "../../src/versions"
77
import { randomUUID } from "node:crypto"
88
import { readFile } from "node:fs/promises"
@@ -46,6 +46,26 @@ describe("connect", () => {
4646
}, 5000)
4747
}).timeout(10000)
4848

49+
it("throw exception if vhost is not valid", async () => {
50+
const [firstNode] = getTestNodesFromEnv()
51+
52+
await expectToThrowAsync(
53+
async () => {
54+
client = await connect({
55+
hostname: firstNode.host,
56+
port: firstNode.port,
57+
username,
58+
password,
59+
vhost: "",
60+
frameMax: 0,
61+
heartbeat: 0,
62+
})
63+
},
64+
Error,
65+
`[ERROR]: VirtualHost '' is not valid`
66+
)
67+
}).timeout(10000)
68+
4969
it("using EXTERNAL auth", async () => {
5070
client = await createTlsClient()
5171

0 commit comments

Comments
 (0)