@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.esm.js CHANGED
@@ -2,7 +2,7 @@ import { isValid, parse } from 'psl';
2
2
  import { lru } from 'tiny-lru';
3
3
  import { promises } from 'node:dns';
4
4
  import { stringSimilarity } from 'string-similarity-js';
5
- import net from 'node:net';
5
+ import * as net from 'node:net';
6
6
 
7
7
  class LRUAdapter {
8
8
  constructor(maxSize = 1e3, ttlMs = 36e5) {
@@ -40,67 +40,71 @@ class LRUAdapter {
40
40
  }
41
41
  }
42
42
 
43
- const DEFAULT_CACHE_TTL = {
44
- mx: 36e5,
45
- // 1 hour
46
- disposable: 864e5,
47
- // 24 hours
48
- free: 864e5,
49
- // 24 hours
50
- domainValid: 864e5,
51
- // 24 hours
52
- smtp: 18e5,
53
- // 30 minutes
54
- domainSuggestion: 864e5,
55
- // 24 hours
56
- whois: 36e5
57
- // 1 hour
58
- };
59
- const DEFAULT_CACHE_SIZE = {
60
- mx: 500,
61
- disposable: 1e3,
62
- free: 1e3,
63
- domainValid: 1e3,
64
- smtp: 500,
65
- domainSuggestion: 1e3,
66
- whois: 200
43
+ const DEFAULT_CACHE_OPTIONS = {
44
+ ttl: {
45
+ mx: 36e5,
46
+ // 1 hour
47
+ disposable: 864e5,
48
+ // 24 hours
49
+ free: 864e5,
50
+ // 24 hours
51
+ domainValid: 864e5,
52
+ // 24 hours
53
+ smtp: 18e5,
54
+ // 30 minutes
55
+ domainSuggestion: 864e5,
56
+ // 24 hours
57
+ whois: 36e5
58
+ // 1 hour
59
+ },
60
+ maxSize: {
61
+ mx: 500,
62
+ disposable: 1e3,
63
+ free: 1e3,
64
+ domainValid: 1e3,
65
+ smtp: 500,
66
+ domainSuggestion: 1e3,
67
+ whois: 200
68
+ }
67
69
  };
68
-
69
- const mxCache = lru(500, 36e5);
70
- const disposableCache = lru(1e3, 864e5);
71
- const freeCache = lru(1e3, 864e5);
72
- const domainValidCache = lru(1e3, 864e5);
73
- const smtpCache = lru(500, 18e5);
74
- const domainSuggestionCache = lru(1e3, 864e5);
75
- const whoisCache = lru(200, 36e5);
76
- function getCacheStore(defaultLru, cacheType, passedCache) {
77
- if (passedCache && passedCache[cacheType]) {
78
- return passedCache[cacheType];
79
- }
80
- return new LRUAdapter(DEFAULT_CACHE_SIZE[cacheType], DEFAULT_CACHE_TTL[cacheType]);
70
+ let defaultCacheInstance = null;
71
+ function getDefaultCache() {
72
+ if (!defaultCacheInstance) {
73
+ defaultCacheInstance = {
74
+ mx: new LRUAdapter(DEFAULT_CACHE_OPTIONS.maxSize.mx, DEFAULT_CACHE_OPTIONS.ttl.mx),
75
+ disposable: new LRUAdapter(DEFAULT_CACHE_OPTIONS.maxSize.disposable, DEFAULT_CACHE_OPTIONS.ttl.disposable),
76
+ free: new LRUAdapter(DEFAULT_CACHE_OPTIONS.maxSize.free, DEFAULT_CACHE_OPTIONS.ttl.free),
77
+ domainValid: new LRUAdapter(DEFAULT_CACHE_OPTIONS.maxSize.domainValid, DEFAULT_CACHE_OPTIONS.ttl.domainValid),
78
+ smtp: new LRUAdapter(DEFAULT_CACHE_OPTIONS.maxSize.smtp, DEFAULT_CACHE_OPTIONS.ttl.smtp),
79
+ domainSuggestion: new LRUAdapter(DEFAULT_CACHE_OPTIONS.maxSize.domainSuggestion, DEFAULT_CACHE_OPTIONS.ttl.domainSuggestion),
80
+ whois: new LRUAdapter(DEFAULT_CACHE_OPTIONS.maxSize.whois, DEFAULT_CACHE_OPTIONS.ttl.whois)
81
+ };
82
+ }
83
+ return defaultCacheInstance;
84
+ }
85
+ function getCacheStore(cache, key) {
86
+ return (cache === null || cache === void 0 ? void 0 : cache[key]) || getDefaultCache()[key];
87
+ }
88
+ function clearDefaultCache() {
89
+ if (defaultCacheInstance) {
90
+ defaultCacheInstance.mx.clear();
91
+ defaultCacheInstance.disposable.clear();
92
+ defaultCacheInstance.free.clear();
93
+ defaultCacheInstance.domainValid.clear();
94
+ defaultCacheInstance.smtp.clear();
95
+ defaultCacheInstance.domainSuggestion.clear();
96
+ defaultCacheInstance.whois.clear();
97
+ }
81
98
  }
82
- const mxCacheStore = (passedCache) => getCacheStore(mxCache, "mx", passedCache);
83
- const disposableCacheStore = (passedCache) => getCacheStore(disposableCache, "disposable", passedCache);
84
- const freeCacheStore = (passedCache) => getCacheStore(freeCache, "free", passedCache);
85
- const domainValidCacheStore = (passedCache) => getCacheStore(domainValidCache, "domainValid", passedCache);
86
- const smtpCacheStore = (passedCache) => getCacheStore(smtpCache, "smtp", passedCache);
87
- const domainSuggestionCacheStore = (passedCache) => getCacheStore(domainSuggestionCache, "domainSuggestion", passedCache);
88
- const whoisCacheStore = (passedCache) => getCacheStore(whoisCache, "whois", passedCache);
89
- function clearAllCaches() {
90
- mxCache.clear();
91
- disposableCache.clear();
92
- freeCache.clear();
93
- domainValidCache.clear();
94
- smtpCache.clear();
95
- domainSuggestionCache.clear();
96
- whoisCache.clear();
99
+ function resetDefaultCache() {
100
+ defaultCacheInstance = null;
97
101
  }
98
102
 
99
103
  async function resolveMxRecords(params) {
100
104
  const { domain, cache, logger } = params;
101
105
  const log = logger || (() => {
102
106
  });
103
- const cacheStore = mxCacheStore(cache);
107
+ const cacheStore = getCacheStore(cache, "mx");
104
108
  const cached = await cacheStore.get(domain);
105
109
  if (cached !== null && cached !== void 0) {
106
110
  log(`[resolveMxRecords] Cache hit for ${domain}: ${cached.length} MX records`);
@@ -264,24 +268,24 @@ function defaultDomainSuggestionMethod(domain, commonDomains) {
264
268
  }
265
269
  return null;
266
270
  }
267
- async function defaultDomainSuggestionMethodAsync(domain, commonDomains) {
268
- return defaultDomainSuggestionMethodImpl(domain, commonDomains);
271
+ async function defaultDomainSuggestionMethodAsync(domain, commonDomains, cache) {
272
+ return defaultDomainSuggestionMethodImpl(domain, commonDomains, cache);
269
273
  }
270
- async function defaultDomainSuggestionMethodImpl(domain, commonDomains) {
274
+ async function defaultDomainSuggestionMethodImpl(domain, commonDomains, cache) {
271
275
  if (!domain || domain.length < 3) {
272
276
  return null;
273
277
  }
274
278
  const domainsToCheck = commonDomains || COMMON_EMAIL_DOMAINS;
275
279
  const lowerDomain = domain.toLowerCase();
276
280
  const cacheKey = `${lowerDomain}:${domainsToCheck.length}`;
277
- const cache = domainSuggestionCacheStore();
278
- const cached = cache.get(cacheKey);
281
+ const cacheStore = getCacheStore(cache, "domainSuggestion");
282
+ const cached = await cacheStore.get(cacheKey);
279
283
  const resolved = cached && typeof cached === "object" && "then" in cached ? await cached : cached;
280
284
  if (resolved !== null && resolved !== void 0) {
281
- return resolved ? { original: domain, suggested: resolved.suggested, confidence: resolved.confidence } : null;
285
+ return resolved;
282
286
  }
283
287
  if (domainsToCheck.includes(lowerDomain)) {
284
- await cache.set(cacheKey, null);
288
+ await cacheStore.set(cacheKey, null);
285
289
  return null;
286
290
  }
287
291
  for (const [correctDomain, typos] of Object.entries(TYPO_PATTERNS)) {
@@ -292,7 +296,7 @@ async function defaultDomainSuggestionMethodImpl(domain, commonDomains) {
292
296
  confidence: 0.95
293
297
  // High confidence for known typo patterns
294
298
  };
295
- await cache.set(cacheKey, { suggested: result.suggested, confidence: result.confidence });
299
+ await cacheStore.set(cacheKey, result);
296
300
  return result;
297
301
  }
298
302
  }
@@ -320,7 +324,7 @@ async function defaultDomainSuggestionMethodImpl(domain, commonDomains) {
320
324
  }
321
325
  if (bestMatch) {
322
326
  if (bestMatch.domain.charAt(0) !== lowerDomain.charAt(0) && bestMatch.similarity < 0.9) {
323
- await cache.set(cacheKey, null);
327
+ await cacheStore.set(cacheKey, null);
324
328
  return null;
325
329
  }
326
330
  const result = {
@@ -328,10 +332,10 @@ async function defaultDomainSuggestionMethodImpl(domain, commonDomains) {
328
332
  suggested: bestMatch.domain,
329
333
  confidence: bestMatch.similarity
330
334
  };
331
- await cache.set(cacheKey, { suggested: result.suggested, confidence: result.confidence });
335
+ await cacheStore.set(cacheKey, result);
332
336
  return result;
333
337
  }
334
- await cache.set(cacheKey, null);
338
+ await cacheStore.set(cacheKey, null);
335
339
  return null;
336
340
  }
337
341
  function suggestDomain(params) {
@@ -348,7 +352,7 @@ function suggestDomain(params) {
348
352
  }
349
353
  return defaultDomainSuggestionMethod(domain, commonDomains);
350
354
  }
351
- async function suggestEmailDomain(email, commonDomains) {
355
+ async function suggestEmailDomain(email, commonDomains, cache) {
352
356
  if (!email || !email.includes("@")) {
353
357
  return null;
354
358
  }
@@ -356,7 +360,7 @@ async function suggestEmailDomain(email, commonDomains) {
356
360
  if (!domain || !localPart) {
357
361
  return null;
358
362
  }
359
- const suggestion = await defaultDomainSuggestionMethodAsync(domain, commonDomains);
363
+ const suggestion = await defaultDomainSuggestionMethodAsync(domain, commonDomains, cache);
360
364
  if (suggestion) {
361
365
  return {
362
366
  original: email,
@@ -1236,7 +1240,7 @@ async function isValidEmailDomain(emailOrDomain, cache) {
1236
1240
  if (!emailDomain) {
1237
1241
  return false;
1238
1242
  }
1239
- const cacheStore = domainValidCacheStore(cache);
1243
+ const cacheStore = getCacheStore(cache, "domainValid");
1240
1244
  const cached = await cacheStore.get(emailDomain);
1241
1245
  if (cached !== null && cached !== void 0) {
1242
1246
  return cached;
@@ -1774,14 +1778,14 @@ function queryWhoisServer(domain, server, timeout = 5e3, debug = false) {
1774
1778
  });
1775
1779
  });
1776
1780
  }
1777
- async function getWhoisData(domain, timeout = 5e3, debug = false) {
1781
+ async function getWhoisData(domain, timeout = 5e3, debug = false, cache) {
1778
1782
  var _a;
1779
1783
  const log = debug ? console.debug : (..._args) => {
1780
1784
  };
1781
1785
  const cacheKey = `whois:${domain}`;
1782
- const cache = whoisCacheStore();
1786
+ const cacheStore = getCacheStore(cache, "whois");
1783
1787
  log(`[whois] getting WHOIS data for ${domain}`);
1784
- const cached = await cache.get(cacheKey);
1788
+ const cached = await cacheStore.get(cacheKey);
1785
1789
  if (cached !== null && cached !== void 0) {
1786
1790
  log(`[whois] using cached data for ${domain}`);
1787
1791
  return cached;
@@ -1803,19 +1807,19 @@ async function getWhoisData(domain, timeout = 5e3, debug = false) {
1803
1807
  log(`[whois] IANA referred to ${referredServer} for ${domain}`);
1804
1808
  const whoisResponse2 = await queryWhoisServer(domain, referredServer, timeout, debug);
1805
1809
  const whoisData3 = parseWhoisData({ rawData: whoisResponse2, domain });
1806
- await cache.set(cacheKey, whoisData3);
1810
+ await cacheStore.set(cacheKey, whoisData3);
1807
1811
  log(`[whois] successfully retrieved and cached WHOIS data from referred server for ${domain}`);
1808
1812
  return whoisData3;
1809
1813
  }
1810
1814
  const whoisData2 = parseWhoisData({ rawData: ianaResponse, domain });
1811
- await cache.set(cacheKey, whoisData2);
1815
+ await cacheStore.set(cacheKey, whoisData2);
1812
1816
  log(`[whois] successfully retrieved and cached WHOIS data from IANA for ${domain}`);
1813
1817
  return whoisData2;
1814
1818
  }
1815
1819
  log(`[whois] using WHOIS server ${whoisServer} for TLD ${tld}`);
1816
1820
  const whoisResponse = await queryWhoisServer(domain, whoisServer, timeout, debug);
1817
1821
  const whoisData = parseWhoisData({ rawData: whoisResponse, domain });
1818
- await cache.set(cacheKey, whoisData);
1822
+ await cacheStore.set(cacheKey, whoisData);
1819
1823
  log(`[whois] successfully retrieved and cached WHOIS data for ${domain}`);
1820
1824
  return whoisData;
1821
1825
  } catch (_error) {
@@ -1823,7 +1827,7 @@ async function getWhoisData(domain, timeout = 5e3, debug = false) {
1823
1827
  return null;
1824
1828
  }
1825
1829
  }
1826
- async function getDomainAge(domain, timeout = 5e3, debug = false) {
1830
+ async function getDomainAge(domain, timeout = 5e3, debug = false, cache) {
1827
1831
  const log = debug ? console.debug : (..._args) => {
1828
1832
  };
1829
1833
  try {
@@ -1837,7 +1841,7 @@ async function getDomainAge(domain, timeout = 5e3, debug = false) {
1837
1841
  log(`[whois] domain validation failed: ${cleanDomain}`);
1838
1842
  return null;
1839
1843
  }
1840
- const whoisData = await getWhoisData(cleanDomain, timeout, debug);
1844
+ const whoisData = await getWhoisData(cleanDomain, timeout, debug, cache);
1841
1845
  if (!whoisData || !whoisData.creationDate) {
1842
1846
  log(`[whois] no creation date found for ${cleanDomain}`);
1843
1847
  return null;
@@ -1861,7 +1865,7 @@ async function getDomainAge(domain, timeout = 5e3, debug = false) {
1861
1865
  return null;
1862
1866
  }
1863
1867
  }
1864
- async function getDomainRegistrationStatus(domain, timeout = 5e3, debug = false) {
1868
+ async function getDomainRegistrationStatus(domain, timeout = 5e3, debug = false, cache) {
1865
1869
  const log = debug ? console.debug : (..._args) => {
1866
1870
  };
1867
1871
  try {
@@ -1875,7 +1879,7 @@ async function getDomainRegistrationStatus(domain, timeout = 5e3, debug = false)
1875
1879
  log(`[whois] domain validation failed: ${cleanDomain}`);
1876
1880
  return null;
1877
1881
  }
1878
- const whoisData = await getWhoisData(cleanDomain, timeout, debug);
1882
+ const whoisData = await getWhoisData(cleanDomain, timeout, debug, cache);
1879
1883
  if (!whoisData || whoisData.isAvailable) {
1880
1884
  log(`[whois] domain ${cleanDomain} is available or not registered`);
1881
1885
  return {
@@ -2121,7 +2125,7 @@ async function isDisposableEmail(params) {
2121
2125
  if (!emailDomain) {
2122
2126
  return false;
2123
2127
  }
2124
- const cacheStore = disposableCacheStore(cache);
2128
+ const cacheStore = getCacheStore(cache, "disposable");
2125
2129
  let cached;
2126
2130
  try {
2127
2131
  cached = await cacheStore.get(emailDomain);
@@ -2154,7 +2158,7 @@ async function isFreeEmail(params) {
2154
2158
  if (!emailDomain) {
2155
2159
  return false;
2156
2160
  }
2157
- const cacheStore = freeCacheStore(cache);
2161
+ const cacheStore = getCacheStore(cache, "free");
2158
2162
  let cached;
2159
2163
  try {
2160
2164
  cached = await cacheStore.get(emailDomain);
@@ -2265,7 +2269,7 @@ async function verifyEmail(params) {
2265
2269
  if (checkDomainAge && !shouldSkipDomainWhois) {
2266
2270
  log(`[verifyEmail] Checking domain age for ${domain}`);
2267
2271
  try {
2268
- result.domainAge = await getDomainAge(domain, whoisTimeout, debug);
2272
+ result.domainAge = await getDomainAge(domain, whoisTimeout, debug, params.cache);
2269
2273
  log(`[verifyEmail] Domain age result:`, result.domainAge ? `${result.domainAge.ageInDays} days` : "null");
2270
2274
  } catch (err) {
2271
2275
  log("[verifyEmail] Failed to get domain age", err);
@@ -2277,7 +2281,7 @@ async function verifyEmail(params) {
2277
2281
  if (checkDomainRegistration && !shouldSkipDomainWhois) {
2278
2282
  log(`[verifyEmail] Checking domain registration status for ${domain}`);
2279
2283
  try {
2280
- result.domainRegistration = await getDomainRegistrationStatus(domain, whoisTimeout, debug);
2284
+ result.domainRegistration = await getDomainRegistrationStatus(domain, whoisTimeout, debug, params.cache);
2281
2285
  log(`[verifyEmail] Domain registration result:`, ((_a = result.domainRegistration) === null || _a === void 0 ? void 0 : _a.isRegistered) ? "registered" : "not registered");
2282
2286
  } catch (err) {
2283
2287
  log("[verifyEmail] Failed to get domain registration status", err);
@@ -2297,7 +2301,7 @@ async function verifyEmail(params) {
2297
2301
  }
2298
2302
  if (verifySmtp && mxRecords.length > 0) {
2299
2303
  const cacheKey = `${emailAddress}:smtp`;
2300
- const smtpCacheInstance = smtpCacheStore(params.cache);
2304
+ const smtpCacheInstance = getCacheStore(params.cache, "smtp");
2301
2305
  const cachedSmtp = await smtpCacheInstance.get(cacheKey);
2302
2306
  if (cachedSmtp !== null && cachedSmtp !== void 0) {
2303
2307
  result.validSmtp = cachedSmtp;
@@ -2355,5 +2359,5 @@ async function verifyEmail(params) {
2355
2359
  return result;
2356
2360
  }
2357
2361
 
2358
- export { COMMON_EMAIL_DOMAINS, DEFAULT_CACHE_SIZE, DEFAULT_CACHE_TTL, LRUAdapter, RedisAdapter, VerificationErrorCode, clearAllCaches, defaultDomainSuggestionMethod, defaultNameDetectionMethod, detectName, detectNameFromEmail, domainPorts, getDomainAge, getDomainRegistrationStatus, getDomainSimilarity, isCommonDomain, isDisposableEmail, isFreeEmail, isValidEmail, isValidEmailDomain, suggestDomain, suggestEmailDomain, verifyEmail, verifyEmailBatch };
2362
+ export { COMMON_EMAIL_DOMAINS, DEFAULT_CACHE_OPTIONS, LRUAdapter, RedisAdapter, VerificationErrorCode, clearDefaultCache, defaultDomainSuggestionMethod, defaultNameDetectionMethod, detectName, detectNameFromEmail, domainPorts, getCacheStore, getDefaultCache, getDomainAge, getDomainRegistrationStatus, getDomainSimilarity, isCommonDomain, isDisposableEmail, isFreeEmail, isValidEmail, isValidEmailDomain, resetDefaultCache, suggestDomain, suggestEmailDomain, verifyEmail, verifyEmailBatch };
2359
2363
  //# sourceMappingURL=index.esm.js.map