diff --git a/nats-base-client/servers.ts b/nats-base-client/servers.ts index be18d417..78a10bae 100644 --- a/nats-base-client/servers.ts +++ b/nats-base-client/servers.ts @@ -28,6 +28,7 @@ import { isIP } from "./ipparser.ts"; function hostPort( u: string, ): { listen: string; hostname: string; port: number } { + u = u.trim(); // remove any protocol that may have been provided if (u.match(/^(.*:\/\/)(.*)/m)) { u = u.replace(/^(.*:\/\/)(.*)/gm, "$2"); @@ -36,12 +37,20 @@ function hostPort( // that means that protocols other than HTTP/S are not // parsable correctly. const url = new URL(`http://${u}`); + + let sport = ""; if (!url.port) { - url.port = `${DEFAULT_PORT}`; + if (u.endsWith(":80")) { + sport = "80"; + } else if (u.endsWith(":443")) { + sport = "443"; + } else { + url.port = `${DEFAULT_PORT}`; + } } const listen = url.host; const hostname = url.hostname; - const port = parseInt(url.port, 10); + const port = sport !== "" ? parseInt(sport, 10) : parseInt(url.port, 10); return { listen, hostname, port }; } diff --git a/tests/jsm_test.ts b/tests/jsm_test.ts index 9c24ede5..4c5e40f9 100644 --- a/tests/jsm_test.ts +++ b/tests/jsm_test.ts @@ -44,6 +44,7 @@ import { import { connect } from "../src/mod.ts"; import { assertThrowsAsyncErrorCode, notCompatible } from "./helpers/mod.ts"; import { validateName } from "../nats-base-client/jsutil.ts"; +import { JsMsgImpl } from "../nats-base-client/jsmsg.ts"; const StreamNameRequired = "stream name required"; const ConsumerNameRequired = "durable name required"; diff --git a/tests/servers_test.ts b/tests/servers_test.ts index ed4f0b8a..6af7e824 100644 --- a/tests/servers_test.ts +++ b/tests/servers_test.ts @@ -15,7 +15,7 @@ */ import { Servers } from "../nats-base-client/servers.ts"; import { assertEquals } from "https://deno.land/std@0.95.0/testing/asserts.ts"; -import type { ServerInfo } from "../nats-base-client/internal_mod.ts"; +import type { ServerInfo } from "../nats-base-client/types.ts"; import { setTransportFactory } from "../nats-base-client/internal_mod.ts"; Deno.test("servers - single", () => { @@ -110,3 +110,14 @@ Deno.test("servers - save tls name", () => { assertEquals(sn.tlsName, "h"); }); }); + +Deno.test("servers - port 80", () => { + function t(hp: string, port: number) { + let servers = new Servers([hp]); + assertEquals(servers.getCurrentServer().port, port); + } + t("localhost:80", 80); + t("localhost:443", 443); + t("localhost:201", 201); + t("localhost", 4222); +});