Repository: louislam/uptime-kuma
File: server/monitor-types/dns.js (JavaScript)

Code before:
    }

    /**
     * Parses a comma-separated list of DNS resolver servers and resolves any hostnames
     * to their corresponding IPv4 and/or IPv6 addresses.
     *
     * We are primarily doing this to support hostnames of docker containers like adguard.
     *
     * - Whitespace is removed from the input string
     * - Empty entries are ignored
     * - IP literals (IPv4 / IPv6) are accepted as-is
     * - Hostnames are resolved to both A and AAAA records in parallel
     * - Invalid or unresolvable entries are logged and skipped
     * @param {string} dnsResolveServer - Comma-separated list of resolver servers (IPs or hostnames)
     * @returns {Promise<Array<string>>} Array of resolved IP addresses
     * @throws {Error} If no valid resolver servers could be parsed or resolved
     */
    async resolveDnsResolverServers(dnsResolveServer) {
        // Remove all spaces, split into array, remove all elements that are empty
        const addresses = dnsResolveServer.replace(/\s/g, "").split(",").filter((x) => x !== "");
        if (!addresses.length) {
            throw new Error("No Resolver Servers specified. Please specifiy at least one resolver server like 1.1.1.1 or a hostname");
        }
        const resolver = new Resolver();

        // Make promises to be resolved in parallel
        const promises = addresses.map(async (e) => {
            if (net.isIP(e)) { // If IPv4 or IPv6 addr, immediately return
                return [ e ];
            }

            // Otherwise, attempt to resolve hostname
            const [ v4, v6 ] = await Promise.allSettled([
                resolver.resolve4(e),
                resolver.resolve6(e),
            ]);

            const addrs = [
                ...(v4.status === "fulfilled" ? v4.value : []),
                ...(v6.status === "fulfilled" ? v6.value : []),
            ];

            if (!addrs.length) {
                log.error("DNS", `Invalid resolver server ${e}`);
            }
            return addrs;
        });

        // [[ips of hostname1],[ips hostname2],...]
        const ips = await Promise.all(promises);
        // Append all the ips in [[]] to a single []

Review comment (suggestion):
pet peeve

```suggestion
        // Make promises to be resolved concurrently
```

Code after:
    }

    /**
     * Parses a comma-separated list of DNS resolver servers and resolves any hostnames
     * to their corresponding IPv4 and/or IPv6 addresses.
     *
     * We are primarily doing this to support hostnames of docker containers like adguard.
     *
     * - Whitespace is removed from the input string
     * - Empty entries are ignored
     * - IP literals (IPv4 / IPv6) are accepted as-is
     * - Hostnames are resolved to both A and AAAA records in parallel
     * - Invalid or unresolvable entries are logged and skipped
     * @param {string} dnsResolveServer - Comma-separated list of resolver servers (IPs or hostnames)
     * @returns {Promise<Array<string>>} Array of resolved IP addresses
     * @throws {Error} If no valid resolver servers could be parsed or resolved
     */
    async resolveDnsResolverServers(dnsResolveServer) {
        // Remove all spaces, split into array, remove all elements that are empty
        const addresses = dnsResolveServer.replace(/\s/g, "").split(",").filter((x) => x !== "");
        if (!addresses.length) {
            throw new Error("No Resolver Servers specified. Please specifiy at least one resolver server like 1.1.1.1 or a hostname");
        }
        const resolver = new Resolver();

        // Make promises to be resolved concurrently
        const promises = addresses.map(async (e) => {
            if (net.isIP(e)) { // If IPv4 or IPv6 addr, immediately return
                return [ e ];
            }

            // Otherwise, attempt to resolve hostname
            const [ v4, v6 ] = await Promise.allSettled([
                resolver.resolve4(e),
                resolver.resolve6(e),
            ]);

            const addrs = [
                ...(v4.status === "fulfilled" ? v4.value : []),
                ...(v6.status === "fulfilled" ? v6.value : []),
            ];

            if (!addrs.length) {
                log.error("DNS", `Invalid resolver server ${e}`);
            }
            return addrs;
        });

        // [[ips of hostname1],[ips hostname2],...]
        const ips = await Promise.all(promises);
        // Append all the ips in [[]] to a single []