Skip to content

Commit

Permalink
feat: automatically fallback hostname for invalid hosts (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Sep 6, 2023
1 parent 7d4d6bb commit 8517b93
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
17 changes: 14 additions & 3 deletions src/_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class GetPortError extends Error {

export function _log(verbose: boolean, message: string) {
if (verbose) {
console.log("[get-port]", message);
console.log(`[get-port] ${message}`);
}
}

Expand Down Expand Up @@ -77,9 +77,20 @@ export function _fmtOnHost(hostname: string | undefined) {

const HOSTNAME_RE = /^(?!-)[\d.A-Za-z-]{1,63}(?<!-)$/;

export function _validateHostname(hostname: string | undefined) {
export function _validateHostname(
hostname: string | undefined,
_public: boolean,
verbose: boolean,
) {
if (hostname && !HOSTNAME_RE.test(hostname)) {
throw new GetPortError(`Invalid host: ${JSON.stringify(hostname)}`);
const fallbackHost = _public ? "0.0.0.0" : "127.0.0.1";
_log(
verbose,
`Invalid hostname: ${JSON.stringify(hostname)}. Using ${JSON.stringify(
fallbackHost,
)}`,
);
return fallbackHost;
}
return hostname;
}
6 changes: 5 additions & 1 deletion src/get-port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export async function getPort(
verbose: false,
..._userOptions,
port: _port,
host: _validateHostname(_userOptions.host ?? process.env.HOST),
host: _validateHostname(
_userOptions.host ?? process.env.HOST,
_userOptions.public,
_userOptions.verbose,
),
} as GetPortOptions;

if (options.random) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface GetPortOptions {
alternativePortRange: [from: number, to: number];
host: string;
verbose?: boolean;
public?: boolean;
}

export interface WaitForPortOptions {
Expand Down
27 changes: 21 additions & 6 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Server } from "node:net";
import { describe, test, expect, afterEach } from "vitest";
import { describe, test, expect, afterEach, vi } from "vitest";
import { getPort } from "../src";
import { blockPort } from "./utils";

Expand Down Expand Up @@ -86,15 +86,30 @@ describe("getPort: random", () => {

describe("errors", () => {
test("invalid hostname", async () => {
const error = await getPort({ host: "http://localhost:8080" }).catch(
vi.spyOn(console, "log");
await getPort({ host: "http://localhost:8080", verbose: true }).catch(
(error) => error,
);
expect(error.toString()).toMatchInlineSnapshot(
'"GetPortError: Invalid host: \\"http://localhost:8080\\""',
expect(console.log).toHaveBeenCalledWith(
'[get-port] Invalid hostname: "http://localhost:8080". Using "127.0.0.1"',
);
vi.resetAllMocks();
});

test("invalid hostname (public)", async () => {
vi.spyOn(console, "log");
await getPort({
host: "http://localhost:8080",
verbose: true,
public: true,
}).catch((error) => error);
expect(console.log).toHaveBeenCalledWith(
'[get-port] Invalid hostname: "http://localhost:8080". Using "0.0.0.0"',
);
vi.resetAllMocks();
});

test.skipIf(isWindows)("unavailable hostname", async () => {
test.skipIf(isWindows)("unavailable port", async () => {
const error = await getPort({
host: "192.168.1.999",
}).catch((error) => error);
Expand All @@ -103,7 +118,7 @@ describe("errors", () => {
);
});

test.skipIf(isWindows)("unavailable hostname (no random)", async () => {
test.skipIf(isWindows)("unavailable port (no random)", async () => {
const error = await getPort({
host: "192.168.1.999",
random: false,
Expand Down

0 comments on commit 8517b93

Please sign in to comment.