@emailcheck/email-validator-js 2.13.1-beta.7 → 2.13.1-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -6,6 +6,25 @@ var node_dns = require('node:dns');
6
6
  var stringSimilarityJs = require('string-similarity-js');
7
7
  var net = require('node:net');
8
8
 
9
+ function _interopNamespaceDefault(e) {
10
+ var n = Object.create(null);
11
+ if (e) {
12
+ Object.keys(e).forEach(function (k) {
13
+ if (k !== 'default') {
14
+ var d = Object.getOwnPropertyDescriptor(e, k);
15
+ Object.defineProperty(n, k, d.get ? d : {
16
+ enumerable: true,
17
+ get: function () { return e[k]; }
18
+ });
19
+ }
20
+ });
21
+ }
22
+ n.default = e;
23
+ return Object.freeze(n);
24
+ }
25
+
26
+ var net__namespace = /*#__PURE__*/_interopNamespaceDefault(net);
27
+
9
28
  class LRUAdapter {
10
29
  constructor(maxSize = 1e3, ttlMs = 36e5) {
11
30
  this.lru = tinyLru.lru(maxSize, ttlMs);
@@ -42,67 +61,71 @@ class LRUAdapter {
42
61
  }
43
62
  }
44
63
 
45
- const DEFAULT_CACHE_TTL = {
46
- mx: 36e5,
47
- // 1 hour
48
- disposable: 864e5,
49
- // 24 hours
50
- free: 864e5,
51
- // 24 hours
52
- domainValid: 864e5,
53
- // 24 hours
54
- smtp: 18e5,
55
- // 30 minutes
56
- domainSuggestion: 864e5,
57
- // 24 hours
58
- whois: 36e5
59
- // 1 hour
60
- };
61
- const DEFAULT_CACHE_SIZE = {
62
- mx: 500,
63
- disposable: 1e3,
64
- free: 1e3,
65
- domainValid: 1e3,
66
- smtp: 500,
67
- domainSuggestion: 1e3,
68
- whois: 200
64
+ const DEFAULT_CACHE_OPTIONS = {
65
+ ttl: {
66
+ mx: 36e5,
67
+ // 1 hour
68
+ disposable: 864e5,
69
+ // 24 hours
70
+ free: 864e5,
71
+ // 24 hours
72
+ domainValid: 864e5,
73
+ // 24 hours
74
+ smtp: 18e5,
75
+ // 30 minutes
76
+ domainSuggestion: 864e5,
77
+ // 24 hours
78
+ whois: 36e5
79
+ // 1 hour
80
+ },
81
+ maxSize: {
82
+ mx: 500,
83
+ disposable: 1e3,
84
+ free: 1e3,
85
+ domainValid: 1e3,
86
+ smtp: 500,
87
+ domainSuggestion: 1e3,
88
+ whois: 200
89
+ }
69
90
  };
70
-
71
- const mxCache = tinyLru.lru(500, 36e5);
72
- const disposableCache = tinyLru.lru(1e3, 864e5);
73
- const freeCache = tinyLru.lru(1e3, 864e5);
74
- const domainValidCache = tinyLru.lru(1e3, 864e5);
75
- const smtpCache = tinyLru.lru(500, 18e5);
76
- const domainSuggestionCache = tinyLru.lru(1e3, 864e5);
77
- const whoisCache = tinyLru.lru(200, 36e5);
78
- function getCacheStore(defaultLru, cacheType, passedCache) {
79
- if (passedCache && passedCache[cacheType]) {
80
- return passedCache[cacheType];
81
- }
82
- return new LRUAdapter(DEFAULT_CACHE_SIZE[cacheType], DEFAULT_CACHE_TTL[cacheType]);
91
+ let defaultCacheInstance = null;
92
+ function getDefaultCache() {
93
+ if (!defaultCacheInstance) {
94
+ defaultCacheInstance = {
95
+ mx: new LRUAdapter(DEFAULT_CACHE_OPTIONS.maxSize.mx, DEFAULT_CACHE_OPTIONS.ttl.mx),
96
+ disposable: new LRUAdapter(DEFAULT_CACHE_OPTIONS.maxSize.disposable, DEFAULT_CACHE_OPTIONS.ttl.disposable),
97
+ free: new LRUAdapter(DEFAULT_CACHE_OPTIONS.maxSize.free, DEFAULT_CACHE_OPTIONS.ttl.free),
98
+ domainValid: new LRUAdapter(DEFAULT_CACHE_OPTIONS.maxSize.domainValid, DEFAULT_CACHE_OPTIONS.ttl.domainValid),
99
+ smtp: new LRUAdapter(DEFAULT_CACHE_OPTIONS.maxSize.smtp, DEFAULT_CACHE_OPTIONS.ttl.smtp),
100
+ domainSuggestion: new LRUAdapter(DEFAULT_CACHE_OPTIONS.maxSize.domainSuggestion, DEFAULT_CACHE_OPTIONS.ttl.domainSuggestion),
101
+ whois: new LRUAdapter(DEFAULT_CACHE_OPTIONS.maxSize.whois, DEFAULT_CACHE_OPTIONS.ttl.whois)
102
+ };
103
+ }
104
+ return defaultCacheInstance;
105
+ }
106
+ function getCacheStore(cache, key) {
107
+ return (cache === null || cache === void 0 ? void 0 : cache[key]) || getDefaultCache()[key];
108
+ }
109
+ function clearDefaultCache() {
110
+ if (defaultCacheInstance) {
111
+ defaultCacheInstance.mx.clear();
112
+ defaultCacheInstance.disposable.clear();
113
+ defaultCacheInstance.free.clear();
114
+ defaultCacheInstance.domainValid.clear();
115
+ defaultCacheInstance.smtp.clear();
116
+ defaultCacheInstance.domainSuggestion.clear();
117
+ defaultCacheInstance.whois.clear();
118
+ }
83
119
  }
84
- const mxCacheStore = (passedCache) => getCacheStore(mxCache, "mx", passedCache);
85
- const disposableCacheStore = (passedCache) => getCacheStore(disposableCache, "disposable", passedCache);
86
- const freeCacheStore = (passedCache) => getCacheStore(freeCache, "free", passedCache);
87
- const domainValidCacheStore = (passedCache) => getCacheStore(domainValidCache, "domainValid", passedCache);
88
- const smtpCacheStore = (passedCache) => getCacheStore(smtpCache, "smtp", passedCache);
89
- const domainSuggestionCacheStore = (passedCache) => getCacheStore(domainSuggestionCache, "domainSuggestion", passedCache);
90
- const whoisCacheStore = (passedCache) => getCacheStore(whoisCache, "whois", passedCache);
91
- function clearAllCaches() {
92
- mxCache.clear();
93
- disposableCache.clear();
94
- freeCache.clear();
95
- domainValidCache.clear();
96
- smtpCache.clear();
97
- domainSuggestionCache.clear();
98
- whoisCache.clear();
120
+ function resetDefaultCache() {
121
+ defaultCacheInstance = null;
99
122
  }
100
123
 
101
124
  async function resolveMxRecords(params) {
102
125
  const { domain, cache, logger } = params;
103
126
  const log = logger || (() => {
104
127
  });
105
- const cacheStore = mxCacheStore(cache);
128
+ const cacheStore = getCacheStore(cache, "mx");
106
129
  const cached = await cacheStore.get(domain);
107
130
  if (cached !== null && cached !== void 0) {
108
131
  log(`[resolveMxRecords] Cache hit for ${domain}: ${cached.length} MX records`);
@@ -266,24 +289,24 @@ function defaultDomainSuggestionMethod(domain, commonDomains) {
266
289
  }
267
290
  return null;
268
291
  }
269
- async function defaultDomainSuggestionMethodAsync(domain, commonDomains) {
270
- return defaultDomainSuggestionMethodImpl(domain, commonDomains);
292
+ async function defaultDomainSuggestionMethodAsync(domain, commonDomains, cache) {
293
+ return defaultDomainSuggestionMethodImpl(domain, commonDomains, cache);
271
294
  }
272
- async function defaultDomainSuggestionMethodImpl(domain, commonDomains) {
295
+ async function defaultDomainSuggestionMethodImpl(domain, commonDomains, cache) {
273
296
  if (!domain || domain.length < 3) {
274
297
  return null;
275
298
  }
276
299
  const domainsToCheck = commonDomains || COMMON_EMAIL_DOMAINS;
277
300
  const lowerDomain = domain.toLowerCase();
278
301
  const cacheKey = `${lowerDomain}:${domainsToCheck.length}`;
279
- const cache = domainSuggestionCacheStore();
280
- const cached = cache.get(cacheKey);
302
+ const cacheStore = getCacheStore(cache, "domainSuggestion");
303
+ const cached = await cacheStore.get(cacheKey);
281
304
  const resolved = cached && typeof cached === "object" && "then" in cached ? await cached : cached;
282
305
  if (resolved !== null && resolved !== void 0) {
283
- return resolved ? { original: domain, suggested: resolved.suggested, confidence: resolved.confidence } : null;
306
+ return resolved;
284
307
  }
285
308
  if (domainsToCheck.includes(lowerDomain)) {
286
- await cache.set(cacheKey, null);
309
+ await cacheStore.set(cacheKey, null);
287
310
  return null;
288
311
  }
289
312
  for (const [correctDomain, typos] of Object.entries(TYPO_PATTERNS)) {
@@ -294,7 +317,7 @@ async function defaultDomainSuggestionMethodImpl(domain, commonDomains) {
294
317
  confidence: 0.95
295
318
  // High confidence for known typo patterns
296
319
  };
297
- await cache.set(cacheKey, { suggested: result.suggested, confidence: result.confidence });
320
+ await cacheStore.set(cacheKey, result);
298
321
  return result;
299
322
  }
300
323
  }
@@ -322,7 +345,7 @@ async function defaultDomainSuggestionMethodImpl(domain, commonDomains) {
322
345
  }
323
346
  if (bestMatch) {
324
347
  if (bestMatch.domain.charAt(0) !== lowerDomain.charAt(0) && bestMatch.similarity < 0.9) {
325
- await cache.set(cacheKey, null);
348
+ await cacheStore.set(cacheKey, null);
326
349
  return null;
327
350
  }
328
351
  const result = {
@@ -330,10 +353,10 @@ async function defaultDomainSuggestionMethodImpl(domain, commonDomains) {
330
353
  suggested: bestMatch.domain,
331
354
  confidence: bestMatch.similarity
332
355
  };
333
- await cache.set(cacheKey, { suggested: result.suggested, confidence: result.confidence });
356
+ await cacheStore.set(cacheKey, result);
334
357
  return result;
335
358
  }
336
- await cache.set(cacheKey, null);
359
+ await cacheStore.set(cacheKey, null);
337
360
  return null;
338
361
  }
339
362
  function suggestDomain(params) {
@@ -350,7 +373,7 @@ function suggestDomain(params) {
350
373
  }
351
374
  return defaultDomainSuggestionMethod(domain, commonDomains);
352
375
  }
353
- async function suggestEmailDomain(email, commonDomains) {
376
+ async function suggestEmailDomain(email, commonDomains, cache) {
354
377
  if (!email || !email.includes("@")) {
355
378
  return null;
356
379
  }
@@ -358,7 +381,7 @@ async function suggestEmailDomain(email, commonDomains) {
358
381
  if (!domain || !localPart) {
359
382
  return null;
360
383
  }
361
- const suggestion = await defaultDomainSuggestionMethodAsync(domain, commonDomains);
384
+ const suggestion = await defaultDomainSuggestionMethodAsync(domain, commonDomains, cache);
362
385
  if (suggestion) {
363
386
  return {
364
387
  original: email,
@@ -1139,7 +1162,7 @@ async function attemptVerification(params) {
1139
1162
  const { mxRecord, local, domain, port, timeout, log, attempt } = params;
1140
1163
  return new Promise((resolve) => {
1141
1164
  log(`[verifyMailboxSMTP] connecting to ${mxRecord}:${port} (attempt ${attempt + 1})`);
1142
- const socket = net.connect({
1165
+ const socket = net__namespace.connect({
1143
1166
  host: mxRecord,
1144
1167
  port
1145
1168
  });
@@ -1238,7 +1261,7 @@ async function isValidEmailDomain(emailOrDomain, cache) {
1238
1261
  if (!emailDomain) {
1239
1262
  return false;
1240
1263
  }
1241
- const cacheStore = domainValidCacheStore(cache);
1264
+ const cacheStore = getCacheStore(cache, "domainValid");
1242
1265
  const cached = await cacheStore.get(emailDomain);
1243
1266
  if (cached !== null && cached !== void 0) {
1244
1267
  return cached;
@@ -1746,7 +1769,7 @@ function queryWhoisServer(domain, server, timeout = 5e3, debug = false) {
1746
1769
  const log = debug ? console.debug : (..._args) => {
1747
1770
  };
1748
1771
  return new Promise((resolve, reject) => {
1749
- const client = new net.Socket();
1772
+ const client = new net__namespace.Socket();
1750
1773
  let data = "";
1751
1774
  log(`[whois] querying ${server} for domain ${domain}`);
1752
1775
  const timer = setTimeout(() => {
@@ -1776,14 +1799,14 @@ function queryWhoisServer(domain, server, timeout = 5e3, debug = false) {
1776
1799
  });
1777
1800
  });
1778
1801
  }
1779
- async function getWhoisData(domain, timeout = 5e3, debug = false) {
1802
+ async function getWhoisData(domain, timeout = 5e3, debug = false, cache) {
1780
1803
  var _a;
1781
1804
  const log = debug ? console.debug : (..._args) => {
1782
1805
  };
1783
1806
  const cacheKey = `whois:${domain}`;
1784
- const cache = whoisCacheStore();
1807
+ const cacheStore = getCacheStore(cache, "whois");
1785
1808
  log(`[whois] getting WHOIS data for ${domain}`);
1786
- const cached = await cache.get(cacheKey);
1809
+ const cached = await cacheStore.get(cacheKey);
1787
1810
  if (cached !== null && cached !== void 0) {
1788
1811
  log(`[whois] using cached data for ${domain}`);
1789
1812
  return cached;
@@ -1805,19 +1828,19 @@ async function getWhoisData(domain, timeout = 5e3, debug = false) {
1805
1828
  log(`[whois] IANA referred to ${referredServer} for ${domain}`);
1806
1829
  const whoisResponse2 = await queryWhoisServer(domain, referredServer, timeout, debug);
1807
1830
  const whoisData3 = parseWhoisData({ rawData: whoisResponse2, domain });
1808
- await cache.set(cacheKey, whoisData3);
1831
+ await cacheStore.set(cacheKey, whoisData3);
1809
1832
  log(`[whois] successfully retrieved and cached WHOIS data from referred server for ${domain}`);
1810
1833
  return whoisData3;
1811
1834
  }
1812
1835
  const whoisData2 = parseWhoisData({ rawData: ianaResponse, domain });
1813
- await cache.set(cacheKey, whoisData2);
1836
+ await cacheStore.set(cacheKey, whoisData2);
1814
1837
  log(`[whois] successfully retrieved and cached WHOIS data from IANA for ${domain}`);
1815
1838
  return whoisData2;
1816
1839
  }
1817
1840
  log(`[whois] using WHOIS server ${whoisServer} for TLD ${tld}`);
1818
1841
  const whoisResponse = await queryWhoisServer(domain, whoisServer, timeout, debug);
1819
1842
  const whoisData = parseWhoisData({ rawData: whoisResponse, domain });
1820
- await cache.set(cacheKey, whoisData);
1843
+ await cacheStore.set(cacheKey, whoisData);
1821
1844
  log(`[whois] successfully retrieved and cached WHOIS data for ${domain}`);
1822
1845
  return whoisData;
1823
1846
  } catch (_error) {
@@ -1825,7 +1848,7 @@ async function getWhoisData(domain, timeout = 5e3, debug = false) {
1825
1848
  return null;
1826
1849
  }
1827
1850
  }
1828
- async function getDomainAge(domain, timeout = 5e3, debug = false) {
1851
+ async function getDomainAge(domain, timeout = 5e3, debug = false, cache) {
1829
1852
  const log = debug ? console.debug : (..._args) => {
1830
1853
  };
1831
1854
  try {
@@ -1839,7 +1862,7 @@ async function getDomainAge(domain, timeout = 5e3, debug = false) {
1839
1862
  log(`[whois] domain validation failed: ${cleanDomain}`);
1840
1863
  return null;
1841
1864
  }
1842
- const whoisData = await getWhoisData(cleanDomain, timeout, debug);
1865
+ const whoisData = await getWhoisData(cleanDomain, timeout, debug, cache);
1843
1866
  if (!whoisData || !whoisData.creationDate) {
1844
1867
  log(`[whois] no creation date found for ${cleanDomain}`);
1845
1868
  return null;
@@ -1863,7 +1886,7 @@ async function getDomainAge(domain, timeout = 5e3, debug = false) {
1863
1886
  return null;
1864
1887
  }
1865
1888
  }
1866
- async function getDomainRegistrationStatus(domain, timeout = 5e3, debug = false) {
1889
+ async function getDomainRegistrationStatus(domain, timeout = 5e3, debug = false, cache) {
1867
1890
  const log = debug ? console.debug : (..._args) => {
1868
1891
  };
1869
1892
  try {
@@ -1877,7 +1900,7 @@ async function getDomainRegistrationStatus(domain, timeout = 5e3, debug = false)
1877
1900
  log(`[whois] domain validation failed: ${cleanDomain}`);
1878
1901
  return null;
1879
1902
  }
1880
- const whoisData = await getWhoisData(cleanDomain, timeout, debug);
1903
+ const whoisData = await getWhoisData(cleanDomain, timeout, debug, cache);
1881
1904
  if (!whoisData || whoisData.isAvailable) {
1882
1905
  log(`[whois] domain ${cleanDomain} is available or not registered`);
1883
1906
  return {
@@ -2123,7 +2146,7 @@ async function isDisposableEmail(params) {
2123
2146
  if (!emailDomain) {
2124
2147
  return false;
2125
2148
  }
2126
- const cacheStore = disposableCacheStore(cache);
2149
+ const cacheStore = getCacheStore(cache, "disposable");
2127
2150
  let cached;
2128
2151
  try {
2129
2152
  cached = await cacheStore.get(emailDomain);
@@ -2156,7 +2179,7 @@ async function isFreeEmail(params) {
2156
2179
  if (!emailDomain) {
2157
2180
  return false;
2158
2181
  }
2159
- const cacheStore = freeCacheStore(cache);
2182
+ const cacheStore = getCacheStore(cache, "free");
2160
2183
  let cached;
2161
2184
  try {
2162
2185
  cached = await cacheStore.get(emailDomain);
@@ -2267,7 +2290,7 @@ async function verifyEmail(params) {
2267
2290
  if (checkDomainAge && !shouldSkipDomainWhois) {
2268
2291
  log(`[verifyEmail] Checking domain age for ${domain}`);
2269
2292
  try {
2270
- result.domainAge = await getDomainAge(domain, whoisTimeout, debug);
2293
+ result.domainAge = await getDomainAge(domain, whoisTimeout, debug, params.cache);
2271
2294
  log(`[verifyEmail] Domain age result:`, result.domainAge ? `${result.domainAge.ageInDays} days` : "null");
2272
2295
  } catch (err) {
2273
2296
  log("[verifyEmail] Failed to get domain age", err);
@@ -2279,7 +2302,7 @@ async function verifyEmail(params) {
2279
2302
  if (checkDomainRegistration && !shouldSkipDomainWhois) {
2280
2303
  log(`[verifyEmail] Checking domain registration status for ${domain}`);
2281
2304
  try {
2282
- result.domainRegistration = await getDomainRegistrationStatus(domain, whoisTimeout, debug);
2305
+ result.domainRegistration = await getDomainRegistrationStatus(domain, whoisTimeout, debug, params.cache);
2283
2306
  log(`[verifyEmail] Domain registration result:`, ((_a = result.domainRegistration) === null || _a === void 0 ? void 0 : _a.isRegistered) ? "registered" : "not registered");
2284
2307
  } catch (err) {
2285
2308
  log("[verifyEmail] Failed to get domain registration status", err);
@@ -2299,7 +2322,7 @@ async function verifyEmail(params) {
2299
2322
  }
2300
2323
  if (verifySmtp && mxRecords.length > 0) {
2301
2324
  const cacheKey = `${emailAddress}:smtp`;
2302
- const smtpCacheInstance = smtpCacheStore(params.cache);
2325
+ const smtpCacheInstance = getCacheStore(params.cache, "smtp");
2303
2326
  const cachedSmtp = await smtpCacheInstance.get(cacheKey);
2304
2327
  if (cachedSmtp !== null && cachedSmtp !== void 0) {
2305
2328
  result.validSmtp = cachedSmtp;
@@ -2358,16 +2381,17 @@ async function verifyEmail(params) {
2358
2381
  }
2359
2382
 
2360
2383
  exports.COMMON_EMAIL_DOMAINS = COMMON_EMAIL_DOMAINS;
2361
- exports.DEFAULT_CACHE_SIZE = DEFAULT_CACHE_SIZE;
2362
- exports.DEFAULT_CACHE_TTL = DEFAULT_CACHE_TTL;
2384
+ exports.DEFAULT_CACHE_OPTIONS = DEFAULT_CACHE_OPTIONS;
2363
2385
  exports.LRUAdapter = LRUAdapter;
2364
2386
  exports.RedisAdapter = RedisAdapter;
2365
- exports.clearAllCaches = clearAllCaches;
2387
+ exports.clearDefaultCache = clearDefaultCache;
2366
2388
  exports.defaultDomainSuggestionMethod = defaultDomainSuggestionMethod;
2367
2389
  exports.defaultNameDetectionMethod = defaultNameDetectionMethod;
2368
2390
  exports.detectName = detectName;
2369
2391
  exports.detectNameFromEmail = detectNameFromEmail;
2370
2392
  exports.domainPorts = domainPorts;
2393
+ exports.getCacheStore = getCacheStore;
2394
+ exports.getDefaultCache = getDefaultCache;
2371
2395
  exports.getDomainAge = getDomainAge;
2372
2396
  exports.getDomainRegistrationStatus = getDomainRegistrationStatus;
2373
2397
  exports.getDomainSimilarity = getDomainSimilarity;
@@ -2376,6 +2400,7 @@ exports.isDisposableEmail = isDisposableEmail;
2376
2400
  exports.isFreeEmail = isFreeEmail;
2377
2401
  exports.isValidEmail = isValidEmail;
2378
2402
  exports.isValidEmailDomain = isValidEmailDomain;
2403
+ exports.resetDefaultCache = resetDefaultCache;
2379
2404
  exports.suggestDomain = suggestDomain;
2380
2405
  exports.suggestEmailDomain = suggestEmailDomain;
2381
2406
  exports.verifyEmail = verifyEmail;