isFQDN.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isFQDN;
  6. var _assertString = _interopRequireDefault(require("./util/assertString"));
  7. var _merge = _interopRequireDefault(require("./util/merge"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. var default_fqdn_options = {
  10. require_tld: true,
  11. allow_underscores: false,
  12. allow_trailing_dot: false,
  13. allow_numeric_tld: false,
  14. allow_wildcard: false,
  15. ignore_max_length: false
  16. };
  17. function isFQDN(str, options) {
  18. (0, _assertString.default)(str);
  19. options = (0, _merge.default)(options, default_fqdn_options);
  20. /* Remove the optional trailing dot before checking validity */
  21. if (options.allow_trailing_dot && str[str.length - 1] === '.') {
  22. str = str.substring(0, str.length - 1);
  23. }
  24. /* Remove the optional wildcard before checking validity */
  25. if (options.allow_wildcard === true && str.indexOf('*.') === 0) {
  26. str = str.substring(2);
  27. }
  28. var parts = str.split('.');
  29. var tld = parts[parts.length - 1];
  30. if (options.require_tld) {
  31. // disallow fqdns without tld
  32. if (parts.length < 2) {
  33. return false;
  34. }
  35. if (!options.allow_numeric_tld && !/^([a-z\u00A1-\u00A8\u00AA-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) {
  36. return false;
  37. } // disallow spaces
  38. if (/\s/.test(tld)) {
  39. return false;
  40. }
  41. } // reject numeric TLDs
  42. if (!options.allow_numeric_tld && /^\d+$/.test(tld)) {
  43. return false;
  44. }
  45. return parts.every(function (part) {
  46. if (part.length > 63 && !options.ignore_max_length) {
  47. return false;
  48. }
  49. if (!/^[a-z_\u00a1-\uffff0-9-]+$/i.test(part)) {
  50. return false;
  51. } // disallow full-width chars
  52. if (/[\uff01-\uff5e]/.test(part)) {
  53. return false;
  54. } // disallow parts starting or ending with hyphen
  55. if (/^-|-$/.test(part)) {
  56. return false;
  57. }
  58. if (!options.allow_underscores && /_/.test(part)) {
  59. return false;
  60. }
  61. return true;
  62. });
  63. }
  64. module.exports = exports.default;
  65. module.exports.default = exports.default;