No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dns.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Resolver = exports.IPV6_UNSPECIFIED = exports.IPV4_UNSPECIFIED = exports.IPV6_LOOPBACK = exports.IPV4_LOOPBACK = void 0;
  4. const node_dns_1 = require("node:dns");
  5. const node_net_1 = require("node:net");
  6. const logger_1 = require("../logger");
  7. exports.IPV4_LOOPBACK = { address: "127.0.0.1", family: 4 };
  8. exports.IPV6_LOOPBACK = { address: "::1", family: 6 };
  9. exports.IPV4_UNSPECIFIED = { address: "0.0.0.0", family: 4 };
  10. exports.IPV6_UNSPECIFIED = { address: "::", family: 6 };
  11. class Resolver {
  12. constructor(lookup = node_dns_1.promises.lookup) {
  13. this.lookup = lookup;
  14. this.cache = new Map([
  15. ["localhost", [exports.IPV4_LOOPBACK, exports.IPV6_LOOPBACK]],
  16. ]);
  17. }
  18. async lookupFirst(hostname) {
  19. const addresses = await this.lookupAll(hostname);
  20. if (addresses.length === 1) {
  21. return addresses[0];
  22. }
  23. const result = addresses[0];
  24. const discarded = [];
  25. for (let i = 1; i < addresses.length; i++) {
  26. discarded.push(result.address);
  27. }
  28. logger_1.logger.debug(`Resolved hostname "${hostname}" to the first result "${result.address}" (ignoring candidates: ${discarded.join(",")}).`);
  29. return result;
  30. }
  31. async lookupAll(hostname) {
  32. const family = (0, node_net_1.isIP)(hostname);
  33. if (family > 0) {
  34. return [{ family, address: hostname }];
  35. }
  36. const cached = this.cache.get(hostname);
  37. if (cached) {
  38. return cached;
  39. }
  40. const addresses = await this.lookup(hostname, {
  41. verbatim: false,
  42. all: true,
  43. });
  44. this.cache.set(hostname, addresses);
  45. return addresses;
  46. }
  47. }
  48. exports.Resolver = Resolver;
  49. Resolver.DEFAULT = new Resolver();