diff --git a/src/connection.ts b/src/connection.ts index 890b4c11..308e8c4b 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -30,7 +30,13 @@ import { Response } from "./responses/response" import { SaslAuthenticateResponse } from "./responses/sasl_authenticate_response" import { SaslHandshakeResponse } from "./responses/sasl_handshake_response" import { TuneResponse } from "./responses/tune_response" -import { DEFAULT_FRAME_MAX, DEFAULT_UNLIMITED_FRAME_MAX, REQUIRED_MANAGEMENT_VERSION, removeFrom } from "./util" +import { + DEFAULT_FRAME_MAX, + DEFAULT_UNLIMITED_FRAME_MAX, + REQUIRED_MANAGEMENT_VERSION, + isString, + removeFrom, +} from "./util" import { Version, checkServerDeclaredVersions, getClientSupportedVersions } from "./versions" import { WaitingResponse } from "./waiting_response" import { ClientListenersParams, ClientParams, ClosingParams, QueryOffsetParams, StoreOffsetParams } from "./client" @@ -474,7 +480,7 @@ export class Connection { private async open(params: { virtualHost: string }) { this.logger.debug(`Open ...`) - if (this.virtualHostIsNotValid(params.virtualHost)) { + if (!this.virtualHostIsValid(params.virtualHost)) { const errorMessage = `[ERROR]: VirtualHost '${params.virtualHost}' is not valid` this.logger.error(errorMessage) return { ok: false, error: new Error(errorMessage) } @@ -487,8 +493,8 @@ export class Connection { return { ok: true, response: res } } - private virtualHostIsNotValid(virtualHost: string) { - if (!virtualHost) { + private virtualHostIsValid(virtualHost: string) { + if (isString(virtualHost) && virtualHost.length > 0) { return true } diff --git a/src/util.ts b/src/util.ts index 438957da..dfc7e4f7 100644 --- a/src/util.ts +++ b/src/util.ts @@ -50,3 +50,5 @@ export const ResponseCode = { StreamDoesNotExist: 2, SubscriptionIdDoesNotExist: 4, } as const + +export const isString = (value: unknown): boolean => typeof value === "string" diff --git a/test/unit/util.test.ts b/test/unit/util.test.ts new file mode 100644 index 00000000..973fe897 --- /dev/null +++ b/test/unit/util.test.ts @@ -0,0 +1,30 @@ +import { expect } from "chai" +import { isString } from "../../src/util" + +describe("Util tests", () => { + describe("isString", () => { + it("return false with a number", () => { + const value = 1 + + expect(isString(value)).false + }) + + it("return false with a boolean", () => { + const value = false + + expect(isString(value)).false + }) + + it("return true with a string", () => { + const value = "abc" + + expect(isString(value)).true + }) + + it("return true with an empty string", () => { + const value = "" + + expect(isString(value)).true + }) + }) +})