mailsure 0.0.1 → 1.0.1
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/CHANGELOG.md +25 -0
- package/README.md +266 -4
- package/dist/chunk-Y3744BYV.js +1043 -0
- package/dist/dns.cjs +1018 -0
- package/dist/dns.d.cts +40 -0
- package/dist/dns.d.ts +40 -0
- package/dist/dns.js +73 -0
- package/dist/index.cjs +1065 -0
- package/dist/index.d.cts +92 -0
- package/dist/index.d.ts +92 -0
- package/dist/index.js +28 -0
- package/package.json +52 -9
- package/index.d.ts +0 -1
- package/index.js +0 -2
package/dist/dns.d.cts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { CheckResult, CheckOptions } from './index.cjs';
|
|
2
|
+
|
|
3
|
+
/** The DNS lookups hasMx needs. Pass your own to test or to use a custom DNS server. */
|
|
4
|
+
interface MxResolver {
|
|
5
|
+
resolveMx(domain: string): Promise<Array<{
|
|
6
|
+
exchange: string;
|
|
7
|
+
priority: number;
|
|
8
|
+
}>>;
|
|
9
|
+
resolve4(domain: string): Promise<string[]>;
|
|
10
|
+
resolve6(domain: string): Promise<string[]>;
|
|
11
|
+
}
|
|
12
|
+
interface MxOptions {
|
|
13
|
+
/** Milliseconds to wait for DNS before giving up. Default 5000. */
|
|
14
|
+
timeout?: number;
|
|
15
|
+
/** Custom resolver. Defaults to a `node:dns/promises` Resolver. */
|
|
16
|
+
resolver?: MxResolver;
|
|
17
|
+
}
|
|
18
|
+
interface DeliverableResult extends CheckResult {
|
|
19
|
+
/**
|
|
20
|
+
* true: the domain accepts mail (MX records, or an A/AAAA record).
|
|
21
|
+
* false: it cannot receive mail (no such domain, null MX, no records).
|
|
22
|
+
* null: DNS did not answer in time or failed, so we don't know.
|
|
23
|
+
*/
|
|
24
|
+
mx: boolean | null;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Checks whether a domain (or the domain of an address) can receive email:
|
|
28
|
+
* MX records, or an A/AAAA record as RFC 5321's implicit MX. A null MX
|
|
29
|
+
* (RFC 7505) means no mail. Resolves true, false, or null when DNS failed or
|
|
30
|
+
* timed out. Never throws.
|
|
31
|
+
*/
|
|
32
|
+
declare function hasMx(domainOrEmail: string, options?: MxOptions): Promise<boolean | null>;
|
|
33
|
+
/**
|
|
34
|
+
* Runs `check` and, when the address is valid, `hasMx` on its domain. When the
|
|
35
|
+
* domain can't receive mail, `valid` is false with reason "mx". When DNS can't
|
|
36
|
+
* tell (`mx: null`), `valid` keeps the result of `check`. Never throws.
|
|
37
|
+
*/
|
|
38
|
+
declare function checkDeliverable(email: unknown, options?: CheckOptions & MxOptions): Promise<DeliverableResult>;
|
|
39
|
+
|
|
40
|
+
export { type DeliverableResult, type MxOptions, type MxResolver, checkDeliverable, hasMx };
|
package/dist/dns.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { CheckResult, CheckOptions } from './index.js';
|
|
2
|
+
|
|
3
|
+
/** The DNS lookups hasMx needs. Pass your own to test or to use a custom DNS server. */
|
|
4
|
+
interface MxResolver {
|
|
5
|
+
resolveMx(domain: string): Promise<Array<{
|
|
6
|
+
exchange: string;
|
|
7
|
+
priority: number;
|
|
8
|
+
}>>;
|
|
9
|
+
resolve4(domain: string): Promise<string[]>;
|
|
10
|
+
resolve6(domain: string): Promise<string[]>;
|
|
11
|
+
}
|
|
12
|
+
interface MxOptions {
|
|
13
|
+
/** Milliseconds to wait for DNS before giving up. Default 5000. */
|
|
14
|
+
timeout?: number;
|
|
15
|
+
/** Custom resolver. Defaults to a `node:dns/promises` Resolver. */
|
|
16
|
+
resolver?: MxResolver;
|
|
17
|
+
}
|
|
18
|
+
interface DeliverableResult extends CheckResult {
|
|
19
|
+
/**
|
|
20
|
+
* true: the domain accepts mail (MX records, or an A/AAAA record).
|
|
21
|
+
* false: it cannot receive mail (no such domain, null MX, no records).
|
|
22
|
+
* null: DNS did not answer in time or failed, so we don't know.
|
|
23
|
+
*/
|
|
24
|
+
mx: boolean | null;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Checks whether a domain (or the domain of an address) can receive email:
|
|
28
|
+
* MX records, or an A/AAAA record as RFC 5321's implicit MX. A null MX
|
|
29
|
+
* (RFC 7505) means no mail. Resolves true, false, or null when DNS failed or
|
|
30
|
+
* timed out. Never throws.
|
|
31
|
+
*/
|
|
32
|
+
declare function hasMx(domainOrEmail: string, options?: MxOptions): Promise<boolean | null>;
|
|
33
|
+
/**
|
|
34
|
+
* Runs `check` and, when the address is valid, `hasMx` on its domain. When the
|
|
35
|
+
* domain can't receive mail, `valid` is false with reason "mx". When DNS can't
|
|
36
|
+
* tell (`mx: null`), `valid` keeps the result of `check`. Never throws.
|
|
37
|
+
*/
|
|
38
|
+
declare function checkDeliverable(email: unknown, options?: CheckOptions & MxOptions): Promise<DeliverableResult>;
|
|
39
|
+
|
|
40
|
+
export { type DeliverableResult, type MxOptions, type MxResolver, checkDeliverable, hasMx };
|
package/dist/dns.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {
|
|
2
|
+
check
|
|
3
|
+
} from "./chunk-Y3744BYV.js";
|
|
4
|
+
|
|
5
|
+
// src/dns.ts
|
|
6
|
+
import { Resolver } from "node:dns/promises";
|
|
7
|
+
import { domainToASCII } from "node:url";
|
|
8
|
+
var TIMED_OUT = /* @__PURE__ */ Symbol("timeout");
|
|
9
|
+
var NO_RECORDS = /* @__PURE__ */ new Set(["ENOTFOUND", "ENODATA", "NXDOMAIN", "ENONAME"]);
|
|
10
|
+
function codeOf(error) {
|
|
11
|
+
return typeof error === "object" && error !== null && "code" in error ? String(error.code) : "";
|
|
12
|
+
}
|
|
13
|
+
function withTimeout(promise, ms) {
|
|
14
|
+
let timer;
|
|
15
|
+
const timeout = new Promise((resolve) => {
|
|
16
|
+
timer = setTimeout(() => resolve(TIMED_OUT), ms);
|
|
17
|
+
timer.unref?.();
|
|
18
|
+
});
|
|
19
|
+
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
|
|
20
|
+
}
|
|
21
|
+
function domainFrom(input) {
|
|
22
|
+
const s = input.trim();
|
|
23
|
+
let d = s.slice(s.lastIndexOf("@") + 1).toLowerCase();
|
|
24
|
+
if (d.endsWith(".")) d = d.slice(0, -1);
|
|
25
|
+
if (!d || d.length > 253 || d.startsWith("[")) return "";
|
|
26
|
+
if (!check(`x@${d}`).valid) return "";
|
|
27
|
+
return domainToASCII(d);
|
|
28
|
+
}
|
|
29
|
+
async function lookup(run, ms) {
|
|
30
|
+
try {
|
|
31
|
+
const r = await withTimeout(run(), ms);
|
|
32
|
+
if (r === TIMED_OUT) return { state: "unknown", records: [] };
|
|
33
|
+
return { state: r.length > 0 ? "yes" : "no", records: r };
|
|
34
|
+
} catch (error) {
|
|
35
|
+
return { state: NO_RECORDS.has(codeOf(error)) ? "no" : "unknown", records: [] };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async function hasMx(domainOrEmail, options = {}) {
|
|
39
|
+
try {
|
|
40
|
+
if (typeof domainOrEmail !== "string") return false;
|
|
41
|
+
const domain = domainFrom(domainOrEmail);
|
|
42
|
+
if (!domain) return false;
|
|
43
|
+
const timeout = typeof options.timeout === "number" && options.timeout > 0 ? options.timeout : 5e3;
|
|
44
|
+
const resolver = options.resolver ?? new Resolver({ timeout, tries: 2 });
|
|
45
|
+
const mx = await lookup(() => resolver.resolveMx(domain), timeout);
|
|
46
|
+
if (mx.state === "yes") {
|
|
47
|
+
const real = mx.records.filter((r) => r && typeof r.exchange === "string" && r.exchange !== "" && r.exchange !== ".");
|
|
48
|
+
return real.length > 0;
|
|
49
|
+
}
|
|
50
|
+
if (mx.state === "unknown") return null;
|
|
51
|
+
const [a, aaaa] = await Promise.all([
|
|
52
|
+
lookup(() => resolver.resolve4(domain), timeout),
|
|
53
|
+
lookup(() => resolver.resolve6(domain), timeout)
|
|
54
|
+
]);
|
|
55
|
+
if (a.state === "yes" || aaaa.state === "yes") return true;
|
|
56
|
+
if (a.state === "unknown" || aaaa.state === "unknown") return null;
|
|
57
|
+
return false;
|
|
58
|
+
} catch {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function checkDeliverable(email, options = {}) {
|
|
63
|
+
const result = check(email, options);
|
|
64
|
+
if (!result.valid) return { ...result, mx: null };
|
|
65
|
+
if (result.domain.startsWith("[")) return { ...result, mx: null };
|
|
66
|
+
const mx = await hasMx(result.domain, options);
|
|
67
|
+
if (mx === false) return { ...result, valid: false, reason: "mx", mx };
|
|
68
|
+
return { ...result, mx };
|
|
69
|
+
}
|
|
70
|
+
export {
|
|
71
|
+
checkDeliverable,
|
|
72
|
+
hasMx
|
|
73
|
+
};
|