isFQDN.js 2.0 KB

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