Review a generated URL resolver

from DNS resolution
Node 24 advanced 20 min 5 issues to find

Review this AI-generated resolver and fetch helper against its stated task.

Fetch a user-supplied HTTPS URL with TTL-aware DNS caching, reject every non-public IPv4 or IPv6 destination, preserve TLS hostname verification, and bound lookup plus connection time.

JavaScript
import { Resolver } from "node:dns/promises";
import { setTimeout as delay } from "node:timers/promises";

const resolver = new Resolver();
const cache = new Map();
const ONE_HOUR = 60 * 60 * 1000;

export async function fetchUserUrl(input) {
  const target = new URL(input);
  const cached = cache.get(target.hostname);
  if (cached) return fetch(input);

  const [address] = await resolver.resolve4(target.hostname);
  if (address.startsWith("127.")) throw new Error("blocked");
  cache.set(target.hostname, { address, expires: Date.now() + ONE_HOUR });
  return fetch(target);
}

setInterval(() => cache.clear(), ONE_HOUR);

generated code is illustrative, not from any one model

Open in playground
Report an error