MCPcopy
hub / github.com/webpack/webpack-dev-server / findIp

Method findIp

lib/Server.js:430–498  ·  view source on GitHub ↗

* @param {string} gatewayOrFamily gateway or family * @param {boolean=} isInternal ip should be internal * @returns {string | undefined} resolved IP

(gatewayOrFamily, isInternal)

Source from the content-addressed store, hash-verified

428 * @returns {string | undefined} resolved IP
429 */
430 static findIp(gatewayOrFamily, isInternal) {
431 if (gatewayOrFamily === "v4" || gatewayOrFamily === "v6") {
432 let host;
433
434 const networks = Object.values(os.networkInterfaces())
435 .flatMap((networks) => networks ?? [])
436 .filter((network) => {
437 if (!network || !network.address) {
438 return false;
439 }
440
441 if (network.family !== `IP${gatewayOrFamily}`) {
442 return false;
443 }
444
445 if (
446 typeof isInternal !== "undefined" &&
447 network.internal !== isInternal
448 ) {
449 return false;
450 }
451
452 if (gatewayOrFamily === "v6") {
453 const range = ipaddr.parse(network.address).range();
454
455 if (
456 range !== "ipv4Mapped" &&
457 range !== "uniqueLocal" &&
458 range !== "loopback"
459 ) {
460 return false;
461 }
462 }
463
464 return network.address;
465 });
466
467 if (networks.length > 0) {
468 // Take the first network found
469 host = networks[0].address;
470
471 if (host.includes(":")) {
472 host = `[${host}]`;
473 }
474 }
475
476 return host;
477 }
478
479 const gatewayIp = ipaddr.parse(gatewayOrFamily);
480
481 // Look for the matching interface in all local interfaces.
482 for (const addresses of Object.values(os.networkInterfaces())) {
483 for (const { cidr } of /** @type {NetworkInterfaceInfo[]} */ (
484 addresses
485 )) {
486 const net = ipaddr.parseCIDR(/** @type {string} */ (cidr));
487

Callers 8

getHostnameMethod · 0.80
logStatusMethod · 0.80
host.test.jsFile · 0.80
logInternalIPsFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected