Skip to content

Commit

Permalink
feat: public option and --host flag to disable network expose in …
Browse files Browse the repository at this point in the history
…development by default (#98)
  • Loading branch information
pi0 committed Aug 12, 2023
1 parent aba93a8 commit 70cc93a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,17 @@ export const main = defineCommand({
},
publicURL: {
type: "string",
description: "Displayed public URL (used for qr code)",
required: false,
},
qr: {
type: "boolean",
description: "Display The QR code of public URL when available",
required: false,
},
public: {
type: "boolean",
description: "Listen to all network interfaces",
required: false,
},
},
Expand All @@ -82,6 +89,7 @@ export const main = defineCommand({
name: args.name,
qr: args.qr,
publicURL: args.publicURL,
public: args.public,
https: args.https, // TODO: Support custom cert
};

Expand Down
25 changes: 19 additions & 6 deletions src/listen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,26 @@ export async function listen(
handle: RequestListener,
_options: Partial<ListenOptions> = {},
): Promise<Listener> {
const _isProd = _options.isProd ?? process.env.NODE_ENV === "production";
const _isTest = _options.isTest ?? process.env.NODE_ENV === "test";
const _hostname = process.env.HOST ?? _options.hostname;
const _public =
_options.public ??
(process.argv.includes("--host") ? true : undefined) ??
(_hostname === "localhost" ? false : _isProd);

const listhenOptions = defu<ListenOptions, ListenOptions[]>(_options, {
name: "",
https: false,
port: process.env.PORT || 3000,
hostname: process.env.HOST || "",
hostname: _hostname ?? (_public ? "" : "localhost"),
showURL: true,
baseURL: "/",
open: false,
clipboard: false,
isTest: process.env.NODE_ENV === "test",
isProd: process.env.NODE_ENV === "production",
isTest: _isTest,
isProd: _isProd,
public: _public,
autoClose: true,
});

Expand Down Expand Up @@ -152,9 +161,13 @@ export async function listen(
}
} else {
lines.push(
` > Listening${name}: ${formatURL(
getURL(undefined, baseURL),
)} ${add}`,
` > Local${name}: ${formatURL(getURL(undefined, baseURL))} ${add}`,
);
}

if (!listhenOptions.public) {
lines.push(
colors.gray(` > Network: use ${colors.white("--host")} to expose`),
);
}

Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export interface ListenOptions {
* @default true
*/
qr?: boolean;
/**
* When enabled, listhen tries to listen to all network interfaces
*
* @default `false` for development and `true` for production
*/
public: boolean;
}

export type GetURLOptions = Pick<Partial<ListenOptions>, "baseURL">;
Expand Down

0 comments on commit 70cc93a

Please sign in to comment.