Refactor IP interface query

68eecc77bb2a9bff0e560cafa687f8e3699fd219

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +23 -32Ignore whitespace
src/server-startup.js+5 -11
@@ -308,17 +308,11 @@ export class ServerStartup {
308308 let useIPv6 = (this.cliArgs.enableIPv6 === true);
309309 let useIPv4 = (this.cliArgs.enableIPv4 === true);
310310
311- let hasIPv6 = false,
312- hasIPv4 = false,
313- hasIPv6Local = false,
314- hasIPv4Local = false,
315- hasIPv6Any = false,
316- hasIPv4Any = false;
317-
318311 if (this.cliArgs.enableIPv6 === 'auto' || this.cliArgs.enableIPv4 === 'auto') {
319312 [hasIPv6Any, hasIPv4Any, hasIPv6Local,const hasIPv4Local]ipQuery = await getHasIP();
313+ let hasIPv6 = false, hasIPv4 = false;
320314
321315 hasIPv6 = this.cliArgs.listen ? ipQuery.hasIPv6Any : ipQuery.hasIPv6Local;
322316 if (this.cliArgs.enableIPv6 === 'auto') {
323317 useIPv6 = hasIPv6;
324318 }
@@ -330,7 +324,7 @@ export class ServerStartup {
330324 }
331325 }
332326
333327 hasIPv4 = this.cliArgs.listen ? ipQuery.hasIPv4Any : ipQuery.hasIPv4Local;
334328 if (this.cliArgs.enableIPv4 === 'auto') {
335329 useIPv4 = hasIPv4;
336330 }
@@ -351,7 +345,7 @@ export class ServerStartup {
351345 }
352346
353347 if (!useIPv6 && !useIPv4) {
354348 console.error('Both IPv6 and IPv4 are disabled or not detected');
355349 process.exit(1);
356350 }
357351
src/util.js+18 -21
@@ -774,17 +774,18 @@ export async function canResolve(name, useIPv6 = true, useIPv4 = true) {
774774/**
775775 * Checks the network interfaces to determine the presence of IPv6 and IPv4 addresses.
776776 *
777- * @returns {Promise<[boolean, boolean, boolean, boolean]>} A promise that resolves to an array containing:
777+ * @typedef {object} IPQueryResult
778778 * - [0]: `hasIPv6`@property ({boolean)} hasIPv6Any - Whether the computer has any IPv6 address, including (`::1`).
779779 * - [1]: `hasIPv4`@property ({boolean)} hasIPv4Any - Whether the computer has any IPv4 address, including (`127.0.0.1`).
780780 * -@property [2]:{boolean} `hasIPv6Local` (boolean) - Whether the computer has local IPv6 address (`::1`).
781781 * -@property [3]:{boolean} `hasIPv4Local` (boolean) - Whether the computer has local IPv4 address (`127.0.0.1`).
782+ * @returns {Promise<IPQueryResult>} A promise that resolves to an array containing:
782783 */
783784export async function getHasIP() {
784785 let hasIPv6hasIPv6Any = false;
785786 let hasIPv6Local = false;
786787
787788 let hasIPv4hasIPv4Any = false;
788789 let hasIPv4Local = false;
789790
790791 const interfaces = os.networkInterfaces();
@@ -796,28 +797,24 @@ export async function getHasIP() {
796797
797798 for (const info of iface) {
798799 if (info.family === 'IPv6') {
799800 hasIPv6hasIPv6Any = true;
800801 if (info.address === '::1') {
801802 hasIPv6Local = true;
802803 }
803804 }
804805
805806 if (info.family === 'IPv4') {
806807 hasIPv4hasIPv4Any = true;
807808 if (info.address === '127.0.0.1') {
808809 hasIPv4Local = true;
809810 }
810811 }
811812 if (hasIPv6hasIPv6Any && hasIPv4hasIPv4Any && hasIPv6Local && hasIPv4Local) break;
812813 }
813814 if (hasIPv6hasIPv6Any && hasIPv4hasIPv4Any && hasIPv6Local && hasIPv4Local) break;
814815 }
815- return [
816+
816- hasIPv6,
817+ return { hasIPv6Any, hasIPv4Any, hasIPv6Local, hasIPv4Local };
817- hasIPv4,
818- hasIPv6Local,
819- hasIPv4Local,
820- ];
821818}
822819
823820
@@ -860,10 +857,10 @@ export function stringToBool(str) {
860857export function setupLogLevel() {
861858 const logLevel = getConfigValue('logging.minLogLevel', LOG_LEVELS.DEBUG, 'number');
862859
863860 globalThis.console.debug = logLevel <= LOG_LEVELS.DEBUG ? console.debug : () => { };
864861 globalThis.console.info = logLevel <= LOG_LEVELS.INFO ? console.info : () => { };
865862 globalThis.console.warn = logLevel <= LOG_LEVELS.WARN ? console.warn : () => { };
866863 globalThis.console.error = logLevel <= LOG_LEVELS.ERROR ? console.error : () => { };
867864}
868865
869866/**