// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
/**
 * **UNSTABLE**: New API, yet to be vetted.
 *
 * Gets the IPv4 or IPv6 network address of the machine.
 *
 * This is inspired by the util of the same name in
 * {@linkcode https://www.npmjs.com/package/serve | npm:serve}.
 *
 * For more advanced use, use {@linkcode Deno.networkInterfaces} directly.
 *
 * @see {@link https://github.com/vercel/serve/blob/1ea55b1b5004f468159b54775e4fb3090fedbb2b/source/utilities/http.ts#L33}
 *
 * @param family The IP protocol version of the interface to get the address of.
 * @returns The IPv4 network address of the machine.
 *
 * @example Get the IPv4 network address (default)
 * ```ts no-assert no-eval
 * import { getNetworkAddress } from "@std/net/get-network-address";
 *
 * const hostname = getNetworkAddress();
 *
 * Deno.serve({ port: 0, hostname }, () => new Response("Hello, world!"));
 * ```
 *
 * @example Get the IPv6 network address
 * ```ts no-assert no-eval
 * import { getNetworkAddress } from "@std/net/get-network-address";
 *
 * const hostname = getNetworkAddress("IPv6");
 *
 * Deno.serve({ port: 0, hostname }, () => new Response("Hello, world!"));
 * ```
 *
 * @experimental
 */
export function getNetworkAddress(
  family: Deno.NetworkInterfaceInfo["family"] = "IPv4",
): string | undefined {
  return Deno.networkInterfaces()
    .find((i) =>
      i.family === family &&
      (family === "IPv4"
        // Cannot lie within 127.0.0.0/8
        ? !i.address.startsWith("127")
        // Cannot be loopback or link-local addresses
        : !(i.address === "::1" || i.address === "fe80::1") && i.scopeid === 0)
    )
    ?.address;
}

// denoCacheMetadata={"headers":{"alt-svc":"h3=\":443\"; ma=86400","cf-ray":"a223e658bd617db6-LAX","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","content-type":"text/typescript","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","x-robots-tag":"noindex","accept-ranges":"bytes","age":"126790","date":"Tue, 28 Jul 2026 12:31:15 GMT","vary":"Origin","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=0mDSZ1Bh5p2u2CijO8IBpxpWKr98R2sJyAOKbS1BUtWKpREcwuQPoDem6tnUfccZdgO5J8r1aXd11H1l8lispci7n0I07F98cTVlclMBuqM%2FX3ckz15A85Y%3D\"}]}","etag":"\"94235b1260f01eb12a7772c0a085561f\"","x-jsr-backend":"modules","content-length":"1678","cache-control":"public, max-age=31536000, immutable","set-cookie":"__cf_bm=zWzjKg4J5UKup1BaA5JIPzrNBvtNLt9dsGlcEC8Hmq4-1785241875.3222492-1.0.1.1-3CuywPEFxnADZz_z6eUcImPoMzJ_bEz.C61vfNSc1npvobzYGEbowsr8J0SOoiVpHHSSN1slmw9Tze5nenwrR3Et61whhK9ndYCZqjFsXGAbqRsArZVw21bm7RHEHr._; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","server":"cloudflare","access-control-expose-headers":"*","cross-origin-resource-policy":"cross-origin","cf-cache-status":"HIT","access-control-allow-origin":"*","x-content-type-options":"nosniff"},"url":"https://jsr.io/@std/net/0.224.5/get_network_address.ts","time":1785241875}