domain.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. const Url = require('url');
  3. const internals = {
  4. minDomainSegments: 2,
  5. nonAsciiRx: /[^\x00-\x7f]/,
  6. domainControlRx: /[\x00-\x20@\:\/]/, // Control + space + separators
  7. tldSegmentRx: /^[a-zA-Z](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$/,
  8. domainSegmentRx: /^[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$/,
  9. URL: Url.URL || URL // $lab:coverage:ignore$
  10. };
  11. exports.analyze = function (domain, options = {}) {
  12. if (typeof domain !== 'string') {
  13. throw new Error('Invalid input: domain must be a string');
  14. }
  15. if (!domain) {
  16. return { error: 'Domain must be a non-empty string' };
  17. }
  18. if (domain.length > 256) {
  19. return { error: 'Domain too long' };
  20. }
  21. const ascii = !internals.nonAsciiRx.test(domain);
  22. if (!ascii) {
  23. if (options.allowUnicode === false) { // Defaults to true
  24. return { error: 'Domain contains forbidden Unicode characters' };
  25. }
  26. domain = domain.normalize('NFC');
  27. }
  28. if (internals.domainControlRx.test(domain)) {
  29. return { error: 'Domain contains invalid character' };
  30. }
  31. domain = internals.punycode(domain);
  32. // https://tools.ietf.org/html/rfc1035 section 2.3.1
  33. const minDomainSegments = options.minDomainSegments || internals.minDomainSegments;
  34. const segments = domain.split('.');
  35. if (segments.length < minDomainSegments) {
  36. return { error: 'Domain lacks the minimum required number of segments' };
  37. }
  38. const tlds = options.tlds;
  39. if (tlds) {
  40. const tld = segments[segments.length - 1].toLowerCase();
  41. if (tlds.deny && tlds.deny.has(tld) ||
  42. tlds.allow && !tlds.allow.has(tld)) {
  43. return { error: 'Domain uses forbidden TLD' };
  44. }
  45. }
  46. for (let i = 0; i < segments.length; ++i) {
  47. const segment = segments[i];
  48. if (!segment.length) {
  49. return { error: 'Domain contains empty dot-separated segment' };
  50. }
  51. if (segment.length > 63) {
  52. return { error: 'Domain contains dot-separated segment that is too long' };
  53. }
  54. if (i < segments.length - 1) {
  55. if (!internals.domainSegmentRx.test(segment)) {
  56. return { error: 'Domain contains invalid character' };
  57. }
  58. }
  59. else {
  60. if (!internals.tldSegmentRx.test(segment)) {
  61. return { error: 'Domain contains invalid tld character' };
  62. }
  63. }
  64. }
  65. };
  66. exports.isValid = function (domain, options) {
  67. return !exports.analyze(domain, options);
  68. };
  69. internals.punycode = function (domain) {
  70. try {
  71. return new internals.URL(`http://${domain}`).host;
  72. }
  73. catch (err) {
  74. return domain;
  75. }
  76. };