Skip to content

Commit

Permalink
refactor: logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Aug 6, 2024
1 parent 15ff95b commit e66c61f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 37 deletions.
82 changes: 59 additions & 23 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,30 +379,66 @@ class Server {
}

/**
* @param {"v4" | "v6"} family
* @param {boolean} isInternal
* @param {string} gatewayOrFamily or family
* @param {boolean} [isInternal=true] ip should be internal
* @returns {string | undefined}
*/
static findIp(family, isInternal = false) {
let host;

Object.values(os.networkInterfaces())
.flatMap((networks) => networks ?? [])
.filter(
(network) =>
network &&
network.address &&
network.family === `IP${family}` &&
network.internal === isInternal,
)
.forEach((network) => {
host = network.address;
if (host.includes(":")) {
host = `[${host}]`;
}
});
static findIp(gatewayOrFamily, isInternal = true) {
if (gatewayOrFamily === "v4" || gatewayOrFamily === "v6") {
let host;

Object.values(os.networkInterfaces())
.flatMap((networks) => networks ?? [])
.filter((network) => {
if (!network || !network.address) {
return false;
}

if (network.family !== `IP${gatewayOrFamily}`) {
return false;
}

if (network.internal !== isInternal) {
return false;
}

if (
gatewayOrFamily === "v6" &&
ipaddr.parse(network.address).range() !== "uniqueLocal"
) {
return false;
}

return host;
return network.address;
})
.forEach((network) => {
host = network.address;
if (host.includes(":")) {
host = `[${host}]`;
}
});

return host;
}

const gatewayIp = ipaddr.parse(gatewayOrFamily);

// Look for the matching interface in all local interfaces.
for (const addresses of Object.values(os.networkInterfaces())) {
for (const { cidr } of /** @type {NetworkInterfaceInfo[]} */ (
addresses
)) {
const net = ipaddr.parseCIDR(/** @type {string} */ (cidr));

if (
net[0] &&
net[0].kind() === gatewayIp.kind() &&
gatewayIp.match(net)
) {
return net[0].toString();
}
}
}
}

/**
Expand Down Expand Up @@ -2765,13 +2801,13 @@ class Server {
if (parsedIP.range() === "unspecified") {
localhost = prettyPrintURL("localhost");

const networkIPv4 = await Server.internalIP("v4");
const networkIPv4 = Server.findIp("v4", false);

if (networkIPv4) {
networkUrlIPv4 = prettyPrintURL(networkIPv4);
}

const networkIPv6 = await Server.internalIP("v6");
const networkIPv6 = Server.findIp("v6", false);

if (networkIPv6) {
networkUrlIPv6 = prettyPrintURL(networkIPv6);
Expand Down
30 changes: 16 additions & 14 deletions types/lib/Server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,6 @@ declare class Server<
};
ServerEnum: {
enum: string[];
/** @type {ClientConfiguration} */
cli: {
exclude: boolean;
};
Expand All @@ -980,7 +979,6 @@ declare class Server<
type: string;
minLength: number;
cli: {
/** @type {{ type: WebSocketServerConfiguration["type"], options: NonNullable<WebSocketServerConfiguration["options"]> }} */
exclude: boolean;
};
};
Expand All @@ -989,7 +987,9 @@ declare class Server<
properties: {
type: {
anyOf: {
$ref: string;
$ref: string /**
* @type {string[]}
*/;
}[];
};
options: {
Expand All @@ -1000,13 +1000,14 @@ declare class Server<
};
ServerOptions: {
type: string;
/** @type {ServerConfiguration} */ additionalProperties: boolean;
additionalProperties: boolean;
properties: {
passphrase: {
type: string;
description: string;
};
requestCert: {
/** @type {{ type: WebSocketServerConfiguration["type"], options: NonNullable<WebSocketServerConfiguration["options"]> }} */
type: string;
description: string;
cli: {
Expand Down Expand Up @@ -1129,7 +1130,7 @@ declare class Server<
}
)[];
};
instanceof?: undefined;
/** @type {number | string} */ instanceof?: undefined;
}
| {
type: string;
Expand Down Expand Up @@ -1181,12 +1182,12 @@ declare class Server<
}
)[];
description: string;
} /** @type {ClientConfiguration} */;
};
};
};
SetupExitSignals: {
type: string;
description: string;
/** @type {string} */ description: string;
link: string;
cli: {
exclude: boolean;
Expand Down Expand Up @@ -1311,10 +1312,7 @@ declare class Server<
items: {
anyOf: {
$ref: string;
}[] /**
* @param {WatchOptions & { aggregateTimeout?: number, ignored?: WatchOptions["ignored"], poll?: number | boolean }} watchOptions
* @returns {WatchOptions}
*/;
}[];
};
$ref?: undefined;
}
Expand Down Expand Up @@ -1406,6 +1404,7 @@ declare class Server<
options: {
type: string;
additionalProperties: boolean;
/** @type {NormalizedStatic} */
cli: {
exclude: boolean;
};
Expand Down Expand Up @@ -1497,11 +1496,14 @@ declare class Server<
*/
static isAbsoluteURL(URL: string): boolean;
/**
* @param {"v4" | "v6"} family
* @param {boolean} isInternal
* @param {string} gatewayOrFamily or family
* @param {boolean} [isInternal=true] ip should be internal
* @returns {string | undefined}
*/
static findIp(family: "v4" | "v6", isInternal?: boolean): string | undefined;
static findIp(
gatewayOrFamily: string,
isInternal?: boolean | undefined,
): string | undefined;
/**
* @param {"v4" | "v6"} family
* @returns {Promise<string | undefined>}
Expand Down

0 comments on commit e66c61f

Please sign in to comment.