Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better options handling for public host #112

Merged
merged 6 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export function formatURL(url: string) {
);
}

const localhostRegex = /^127(\.\d{1,3}){3}$|^localhost$|^::1$/;
export function isLocalhost(hostname: string | undefined) {
return hostname === undefined ? false : localhostRegex.test(hostname);
}

const anyhostRegex = /^$|^0\.0\.0\.0$|^::$/;
export function isAnyhost(hostname: string | undefined) {
return hostname === undefined ? false : anyhostRegex.test(hostname);
}

export function getPublicURL(
urls: ListenURL[],
listhenOptions: ListenOptions,
Expand Down
23 changes: 22 additions & 1 deletion src/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
formatAddress,
formatURL,
getNetworkInterfaces,
isLocalhost,
isAnyhost,
getPublicURL,
} from "./_utils";
import { resolveCertificate } from "./_cert";
Expand All @@ -36,8 +38,10 @@
const _hostname = process.env.HOST ?? _options.hostname;
const _public =
_options.public ??
(isLocalhost(_hostname) ? false : undefined) ??
(isAnyhost(_hostname) ? true : undefined) ??
(process.argv.includes("--host") ? true : undefined) ??
(_hostname === "localhost" ? false : _isProd);
_isProd;

const listhenOptions = defu<ListenOptions, ListenOptions[]>(_options, {
name: "",
Expand All @@ -54,6 +58,23 @@
autoClose: true,
});

if (listhenOptions.public && isLocalhost(listhenOptions.hostname)) {
console.warn(
`[listhen] Trying to listhen on private host ${JSON.stringify(
listhenOptions.hostname,
)} with public option disabled.`,
);
listhenOptions.public = false;

Check warning on line 67 in src/listen.ts

View check run for this annotation

Codecov / codecov/patch

src/listen.ts#L62-L67

Added lines #L62 - L67 were not covered by tests
} else if (!listhenOptions.public && isAnyhost(listhenOptions.hostname)) {
console.warn(
`[listhen] Trying to listhen on public host ${JSON.stringify(
listhenOptions.hostname,
)} with public option disabled. Using "localhost".`,
);
listhenOptions.public = false;
listhenOptions.hostname = "localhost";
}

Check warning on line 76 in src/listen.ts

View check run for this annotation

Codecov / codecov/patch

src/listen.ts#L69-L76

Added lines #L69 - L76 were not covered by tests

if (listhenOptions.isTest) {
listhenOptions.showURL = false;
}
Expand Down