isFQDN.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. };
  14. function isFQDN(str, options) {
  15. (0, _assertString.default)(str);
  16. options = (0, _merge.default)(options, default_fqdn_options);
  17. /* Remove the optional trailing dot before checking validity */
  18. if (options.allow_trailing_dot && str[str.length - 1] === '.') {
  19. str = str.substring(0, str.length - 1);
  20. }
  21. var parts = str.split('.');
  22. for (var i = 0; i < parts.length; i++) {
  23. if (parts[i].length > 63) {
  24. return false;
  25. }
  26. }
  27. if (options.require_tld) {
  28. var tld = parts.pop();
  29. if (!parts.length || !/^([a-z\u00a1-\uffff]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)) {
  30. return false;
  31. } // disallow spaces
  32. if (/[\s\u2002-\u200B\u202F\u205F\u3000\uFEFF\uDB40\uDC20]/.test(tld)) {
  33. return false;
  34. }
  35. }
  36. for (var part, _i = 0; _i < parts.length; _i++) {
  37. part = parts[_i];
  38. if (options.allow_underscores) {
  39. part = part.replace(/_/g, '');
  40. }
  41. if (!/^[a-z\u00a1-\uffff0-9-]+$/i.test(part)) {
  42. return false;
  43. } // disallow full-width chars
  44. if (/[\uff01-\uff5e]/.test(part)) {
  45. return false;
  46. }
  47. if (part[0] === '-' || part[part.length - 1] === '-') {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. module.exports = exports.default;
  54. module.exports.default = exports.default;