index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import punycode from 'punycode/punycode.js';
  2. import rules from './data/rules.js';
  3. //
  4. // Parse rules from file.
  5. //
  6. const rulesByPunySuffix = rules.reduce(
  7. (map, rule) => {
  8. const suffix = rule.replace(/^(\*\.|\!)/, '');
  9. const punySuffix = punycode.toASCII(suffix);
  10. const firstChar = rule.charAt(0);
  11. if (map.has(punySuffix)) {
  12. throw new Error(`Multiple rules found for ${rule} (${punySuffix})`);
  13. }
  14. map.set(punySuffix, {
  15. rule,
  16. suffix,
  17. punySuffix,
  18. wildcard: firstChar === '*',
  19. exception: firstChar === '!'
  20. });
  21. return map;
  22. },
  23. new Map(),
  24. );
  25. //
  26. // Find rule for a given domain.
  27. //
  28. const findRule = (domain) => {
  29. const punyDomain = punycode.toASCII(domain);
  30. const punyDomainChunks = punyDomain.split('.');
  31. for (let i = 0; i < punyDomainChunks.length; i++) {
  32. const suffix = punyDomainChunks.slice(i).join('.');
  33. const matchingRules = rulesByPunySuffix.get(suffix);
  34. if (matchingRules) {
  35. return matchingRules;
  36. }
  37. }
  38. return null;
  39. };
  40. //
  41. // Error codes and messages.
  42. //
  43. export const errorCodes = {
  44. DOMAIN_TOO_SHORT: 'Domain name too short.',
  45. DOMAIN_TOO_LONG: 'Domain name too long. It should be no more than 255 chars.',
  46. LABEL_STARTS_WITH_DASH: 'Domain name label can not start with a dash.',
  47. LABEL_ENDS_WITH_DASH: 'Domain name label can not end with a dash.',
  48. LABEL_TOO_LONG: 'Domain name label should be at most 63 chars long.',
  49. LABEL_TOO_SHORT: 'Domain name label should be at least 1 character long.',
  50. LABEL_INVALID_CHARS: 'Domain name label can only contain alphanumeric characters or dashes.'
  51. };
  52. //
  53. // Validate domain name and throw if not valid.
  54. //
  55. // From wikipedia:
  56. //
  57. // Hostnames are composed of series of labels concatenated with dots, as are all
  58. // domain names. Each label must be between 1 and 63 characters long, and the
  59. // entire hostname (including the delimiting dots) has a maximum of 255 chars.
  60. //
  61. // Allowed chars:
  62. //
  63. // * `a-z`
  64. // * `0-9`
  65. // * `-` but not as a starting or ending character
  66. // * `.` as a separator for the textual portions of a domain name
  67. //
  68. // * http://en.wikipedia.org/wiki/Domain_name
  69. // * http://en.wikipedia.org/wiki/Hostname
  70. //
  71. const validate = (input) => {
  72. // Before we can validate we need to take care of IDNs with unicode chars.
  73. const ascii = punycode.toASCII(input);
  74. if (ascii.length < 1) {
  75. return 'DOMAIN_TOO_SHORT';
  76. }
  77. if (ascii.length > 255) {
  78. return 'DOMAIN_TOO_LONG';
  79. }
  80. // Check each part's length and allowed chars.
  81. const labels = ascii.split('.');
  82. let label;
  83. for (let i = 0; i < labels.length; ++i) {
  84. label = labels[i];
  85. if (!label.length) {
  86. return 'LABEL_TOO_SHORT';
  87. }
  88. if (label.length > 63) {
  89. return 'LABEL_TOO_LONG';
  90. }
  91. if (label.charAt(0) === '-') {
  92. return 'LABEL_STARTS_WITH_DASH';
  93. }
  94. if (label.charAt(label.length - 1) === '-') {
  95. return 'LABEL_ENDS_WITH_DASH';
  96. }
  97. if (!/^[a-z0-9\-_]+$/.test(label)) {
  98. return 'LABEL_INVALID_CHARS';
  99. }
  100. }
  101. };
  102. //
  103. // Public API
  104. //
  105. //
  106. // Parse domain.
  107. //
  108. export const parse = (input) => {
  109. if (typeof input !== 'string') {
  110. throw new TypeError('Domain name must be a string.');
  111. }
  112. // Force domain to lowercase.
  113. let domain = input.slice(0).toLowerCase();
  114. // Handle FQDN.
  115. // TODO: Simply remove trailing dot?
  116. if (domain.charAt(domain.length - 1) === '.') {
  117. domain = domain.slice(0, domain.length - 1);
  118. }
  119. // Validate and sanitise input.
  120. const error = validate(domain);
  121. if (error) {
  122. return {
  123. input: input,
  124. error: {
  125. message: errorCodes[error],
  126. code: error
  127. }
  128. };
  129. }
  130. const parsed = {
  131. input: input,
  132. tld: null,
  133. sld: null,
  134. domain: null,
  135. subdomain: null,
  136. listed: false
  137. };
  138. const domainParts = domain.split('.');
  139. // Non-Internet TLD
  140. if (domainParts[domainParts.length - 1] === 'local') {
  141. return parsed;
  142. }
  143. const handlePunycode = () => {
  144. if (!/xn--/.test(domain)) {
  145. return parsed;
  146. }
  147. if (parsed.domain) {
  148. parsed.domain = punycode.toASCII(parsed.domain);
  149. }
  150. if (parsed.subdomain) {
  151. parsed.subdomain = punycode.toASCII(parsed.subdomain);
  152. }
  153. return parsed;
  154. };
  155. const rule = findRule(domain);
  156. // Unlisted tld.
  157. if (!rule) {
  158. if (domainParts.length < 2) {
  159. return parsed;
  160. }
  161. parsed.tld = domainParts.pop();
  162. parsed.sld = domainParts.pop();
  163. parsed.domain = [parsed.sld, parsed.tld].join('.');
  164. if (domainParts.length) {
  165. parsed.subdomain = domainParts.pop();
  166. }
  167. return handlePunycode();
  168. }
  169. // At this point we know the public suffix is listed.
  170. parsed.listed = true;
  171. const tldParts = rule.suffix.split('.');
  172. const privateParts = domainParts.slice(0, domainParts.length - tldParts.length);
  173. if (rule.exception) {
  174. privateParts.push(tldParts.shift());
  175. }
  176. parsed.tld = tldParts.join('.');
  177. if (!privateParts.length) {
  178. return handlePunycode();
  179. }
  180. if (rule.wildcard) {
  181. tldParts.unshift(privateParts.pop());
  182. parsed.tld = tldParts.join('.');
  183. }
  184. if (!privateParts.length) {
  185. return handlePunycode();
  186. }
  187. parsed.sld = privateParts.pop();
  188. parsed.domain = [parsed.sld, parsed.tld].join('.');
  189. if (privateParts.length) {
  190. parsed.subdomain = privateParts.join('.');
  191. }
  192. return handlePunycode();
  193. };
  194. //
  195. // Get domain.
  196. //
  197. export const get = (domain) => {
  198. if (!domain) {
  199. return null;
  200. }
  201. return parse(domain).domain || null;
  202. };
  203. //
  204. // Check whether domain belongs to a known public suffix.
  205. //
  206. export const isValid = (domain) => {
  207. const parsed = parse(domain);
  208. return Boolean(parsed.domain && parsed.listed);
  209. };
  210. export default { parse, get, isValid };