Skip to content

Commit be82855

Browse files
l4mbymagne
andauthored
chore: minor refactor in virtual host check (#250)
Co-authored-by: magne <[email protected]>
1 parent 70e6955 commit be82855

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/connection.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ import { Response } from "./responses/response"
3030
import { SaslAuthenticateResponse } from "./responses/sasl_authenticate_response"
3131
import { SaslHandshakeResponse } from "./responses/sasl_handshake_response"
3232
import { TuneResponse } from "./responses/tune_response"
33-
import { DEFAULT_FRAME_MAX, DEFAULT_UNLIMITED_FRAME_MAX, REQUIRED_MANAGEMENT_VERSION, removeFrom } from "./util"
33+
import {
34+
DEFAULT_FRAME_MAX,
35+
DEFAULT_UNLIMITED_FRAME_MAX,
36+
REQUIRED_MANAGEMENT_VERSION,
37+
isString,
38+
removeFrom,
39+
} from "./util"
3440
import { Version, checkServerDeclaredVersions, getClientSupportedVersions } from "./versions"
3541
import { WaitingResponse } from "./waiting_response"
3642
import { ClientListenersParams, ClientParams, ClosingParams, QueryOffsetParams, StoreOffsetParams } from "./client"
@@ -474,7 +480,7 @@ export class Connection {
474480

475481
private async open(params: { virtualHost: string }) {
476482
this.logger.debug(`Open ...`)
477-
if (this.virtualHostIsNotValid(params.virtualHost)) {
483+
if (!this.virtualHostIsValid(params.virtualHost)) {
478484
const errorMessage = `[ERROR]: VirtualHost '${params.virtualHost}' is not valid`
479485
this.logger.error(errorMessage)
480486
return { ok: false, error: new Error(errorMessage) }
@@ -487,8 +493,8 @@ export class Connection {
487493
return { ok: true, response: res }
488494
}
489495

490-
private virtualHostIsNotValid(virtualHost: string) {
491-
if (!virtualHost) {
496+
private virtualHostIsValid(virtualHost: string) {
497+
if (isString(virtualHost) && virtualHost.length > 0) {
492498
return true
493499
}
494500

src/util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ export const ResponseCode = {
5050
StreamDoesNotExist: 2,
5151
SubscriptionIdDoesNotExist: 4,
5252
} as const
53+
54+
export const isString = (value: unknown): boolean => typeof value === "string"

test/unit/util.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect } from "chai"
2+
import { isString } from "../../src/util"
3+
4+
describe("Util tests", () => {
5+
describe("isString", () => {
6+
it("return false with a number", () => {
7+
const value = 1
8+
9+
expect(isString(value)).false
10+
})
11+
12+
it("return false with a boolean", () => {
13+
const value = false
14+
15+
expect(isString(value)).false
16+
})
17+
18+
it("return true with a string", () => {
19+
const value = "abc"
20+
21+
expect(isString(value)).true
22+
})
23+
24+
it("return true with an empty string", () => {
25+
const value = ""
26+
27+
expect(isString(value)).true
28+
})
29+
})
30+
})

0 commit comments

Comments
 (0)