email.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. 'use strict';
  2. const Util = require('util');
  3. const Domain = require('./domain');
  4. const internals = {
  5. nonAsciiRx: /[^\x00-\x7f]/,
  6. encoder: new (Util.TextEncoder || TextEncoder)() // $lab:coverage:ignore$
  7. };
  8. exports.analyze = function (email, options) {
  9. return internals.email(email, options);
  10. };
  11. exports.isValid = function (email, options) {
  12. return !internals.email(email, options);
  13. };
  14. internals.email = function (email, options = {}) {
  15. if (typeof email !== 'string') {
  16. throw new Error('Invalid input: email must be a string');
  17. }
  18. if (!email) {
  19. return { error: 'Address must be a non-empty string' };
  20. }
  21. // Unicode
  22. const ascii = !internals.nonAsciiRx.test(email);
  23. if (!ascii) {
  24. if (options.allowUnicode === false) { // Defaults to true
  25. return { error: 'Address contains forbidden Unicode characters' };
  26. }
  27. email = email.normalize('NFC');
  28. }
  29. // Basic structure
  30. const parts = email.split('@');
  31. if (parts.length !== 2) {
  32. return { error: parts.length > 2 ? 'Address cannot contain more than one @ character' : 'Address must contain one @ character' };
  33. }
  34. const [local, domain] = parts;
  35. if (!local) {
  36. return { error: 'Address local part cannot be empty' };
  37. }
  38. if (!options.ignoreLength) {
  39. if (email.length > 254) { // http://tools.ietf.org/html/rfc5321#section-4.5.3.1.3
  40. return { error: 'Address too long' };
  41. }
  42. if (internals.encoder.encode(local).length > 64) { // http://tools.ietf.org/html/rfc5321#section-4.5.3.1.1
  43. return { error: 'Address local part too long' };
  44. }
  45. }
  46. // Validate parts
  47. return internals.local(local, ascii) || Domain.analyze(domain, options);
  48. };
  49. internals.local = function (local, ascii) {
  50. const segments = local.split('.');
  51. for (const segment of segments) {
  52. if (!segment.length) {
  53. return { error: 'Address local part contains empty dot-separated segment' };
  54. }
  55. if (ascii) {
  56. if (!internals.atextRx.test(segment)) {
  57. return { error: 'Address local part contains invalid character' };
  58. }
  59. continue;
  60. }
  61. for (const char of segment) {
  62. if (internals.atextRx.test(char)) {
  63. continue;
  64. }
  65. const binary = internals.binary(char);
  66. if (!internals.atomRx.test(binary)) {
  67. return { error: 'Address local part contains invalid character' };
  68. }
  69. }
  70. }
  71. };
  72. internals.binary = function (char) {
  73. return Array.from(internals.encoder.encode(char)).map((v) => String.fromCharCode(v)).join('');
  74. };
  75. /*
  76. From RFC 5321:
  77. Mailbox = Local-part "@" ( Domain / address-literal )
  78. Local-part = Dot-string / Quoted-string
  79. Dot-string = Atom *("." Atom)
  80. Atom = 1*atext
  81. atext = ALPHA / DIGIT / "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "/" / "=" / "?" / "^" / "_" / "`" / "{" / "|" / "}" / "~"
  82. Domain = sub-domain *("." sub-domain)
  83. sub-domain = Let-dig [Ldh-str]
  84. Let-dig = ALPHA / DIGIT
  85. Ldh-str = *( ALPHA / DIGIT / "-" ) Let-dig
  86. ALPHA = %x41-5A / %x61-7A ; a-z, A-Z
  87. DIGIT = %x30-39 ; 0-9
  88. From RFC 6531:
  89. sub-domain =/ U-label
  90. atext =/ UTF8-non-ascii
  91. UTF8-non-ascii = UTF8-2 / UTF8-3 / UTF8-4
  92. UTF8-2 = %xC2-DF UTF8-tail
  93. UTF8-3 = %xE0 %xA0-BF UTF8-tail /
  94. %xE1-EC 2( UTF8-tail ) /
  95. %xED %x80-9F UTF8-tail /
  96. %xEE-EF 2( UTF8-tail )
  97. UTF8-4 = %xF0 %x90-BF 2( UTF8-tail ) /
  98. %xF1-F3 3( UTF8-tail ) /
  99. %xF4 %x80-8F 2( UTF8-tail )
  100. UTF8-tail = %x80-BF
  101. Note: The following are not supported:
  102. RFC 5321: address-literal, Quoted-string
  103. RFC 5322: obs-*, CFWS
  104. */
  105. internals.atextRx = /^[\w!#\$%&'\*\+\-/=\?\^`\{\|\}~]+$/; // _ included in \w
  106. internals.atomRx = new RegExp([
  107. // %xC2-DF UTF8-tail
  108. '(?:[\\xc2-\\xdf][\\x80-\\xbf])',
  109. // %xE0 %xA0-BF UTF8-tail %xE1-EC 2( UTF8-tail ) %xED %x80-9F UTF8-tail %xEE-EF 2( UTF8-tail )
  110. '(?:\\xe0[\\xa0-\\xbf][\\x80-\\xbf])|(?:[\\xe1-\\xec][\\x80-\\xbf]{2})|(?:\\xed[\\x80-\\x9f][\\x80-\\xbf])|(?:[\\xee-\\xef][\\x80-\\xbf]{2})',
  111. // %xF0 %x90-BF 2( UTF8-tail ) %xF1-F3 3( UTF8-tail ) %xF4 %x80-8F 2( UTF8-tail )
  112. '(?:\\xf0[\\x90-\\xbf][\\x80-\\xbf]{2})|(?:[\\xf1-\\xf3][\\x80-\\xbf]{3})|(?:\\xf4[\\x80-\\x8f][\\x80-\\xbf]{2})'
  113. ].join('|'));