@simplycubed/email-guard 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +249 -2
- package/dist/address.d.ts +6 -0
- package/dist/address.d.ts.map +1 -0
- package/dist/address.js +86 -0
- package/dist/address.js.map +1 -0
- package/dist/consent.d.ts +3 -0
- package/dist/consent.d.ts.map +1 -0
- package/dist/consent.js +36 -0
- package/dist/consent.js.map +1 -0
- package/dist/disposable-domains.d.ts +4 -0
- package/dist/disposable-domains.d.ts.map +1 -0
- package/dist/disposable-domains.js +92 -0
- package/dist/disposable-domains.js.map +1 -0
- package/dist/doh.d.ts +16 -0
- package/dist/doh.d.ts.map +1 -0
- package/dist/doh.js +78 -0
- package/dist/doh.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/normalize.d.ts +7 -0
- package/dist/normalize.d.ts.map +1 -0
- package/dist/normalize.js +22 -0
- package/dist/normalize.js.map +1 -0
- package/dist/suppression.d.ts +10 -0
- package/dist/suppression.d.ts.map +1 -0
- package/dist/suppression.js +21 -0
- package/dist/suppression.js.map +1 -0
- package/dist/testing/fake-resolver.d.ts +6 -0
- package/dist/testing/fake-resolver.d.ts.map +1 -0
- package/dist/testing/fake-resolver.js +11 -0
- package/dist/testing/fake-resolver.js.map +1 -0
- package/dist/testing/index.d.ts +6 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +4 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/memory-suppression-store.d.ts +6 -0
- package/dist/testing/memory-suppression-store.d.ts.map +1 -0
- package/dist/testing/memory-suppression-store.js +17 -0
- package/dist/testing/memory-suppression-store.js.map +1 -0
- package/dist/testing/memory-throttle-store.d.ts +3 -0
- package/dist/testing/memory-throttle-store.d.ts.map +1 -0
- package/dist/testing/memory-throttle-store.js +28 -0
- package/dist/testing/memory-throttle-store.js.map +1 -0
- package/dist/throttle.d.ts +6 -0
- package/dist/throttle.d.ts.map +1 -0
- package/dist/throttle.js +66 -0
- package/dist/throttle.js.map +1 -0
- package/dist/token.d.ts +4 -0
- package/dist/token.d.ts.map +1 -0
- package/dist/token.js +140 -0
- package/dist/token.js.map +1 -0
- package/dist/types.d.ts +103 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +67 -3
- package/src/address.ts +117 -0
- package/src/consent.ts +60 -0
- package/src/disposable-domains.ts +101 -0
- package/src/doh.ts +115 -0
- package/src/index.ts +11 -0
- package/src/normalize.ts +62 -0
- package/src/suppression.ts +41 -0
- package/src/testing/fake-resolver.ts +21 -0
- package/src/testing/index.ts +9 -0
- package/src/testing/memory-suppression-store.ts +23 -0
- package/src/testing/memory-throttle-store.ts +31 -0
- package/src/throttle.ts +114 -0
- package/src/token.ts +180 -0
- package/src/types.ts +164 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SimplyCubed GK
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,250 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @simplycubed/email-guard
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Never email someone who did not ask for it.
|
|
4
|
+
|
|
5
|
+
A small TypeScript library of the checks a signup or lead form needs before it mails anybody:
|
|
6
|
+
address checks, signed double opt-in and unsubscribe tokens, rate limiting, and a shared
|
|
7
|
+
suppression list. It has **zero runtime dependencies**, uses only Web Crypto and `fetch`, and
|
|
8
|
+
runs on Cloudflare Workers and Node 24 or newer.
|
|
9
|
+
|
|
10
|
+
It owns no database and no mail provider. You hand it storage and a DNS resolver through
|
|
11
|
+
small interfaces, and you send the mail yourself.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pnpm add @simplycubed/email-guard
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## What a valid address does and does not prove
|
|
20
|
+
|
|
21
|
+
A `valid` verdict means the address is well formed, its domain accepts mail, and the domain is
|
|
22
|
+
not a known throwaway provider. It does **not** prove the owner asked for your mail: a bot can
|
|
23
|
+
type anybody's real address into a form. The only proof of consent is the owner clicking a link
|
|
24
|
+
you mailed to that address (double opt-in), which is what the `consent` module is for.
|
|
25
|
+
|
|
26
|
+
## Modules
|
|
27
|
+
|
|
28
|
+
### Address checks
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { checkAddress } from "@simplycubed/email-guard";
|
|
32
|
+
|
|
33
|
+
const result = await checkAddress("Sam@gmial.com");
|
|
34
|
+
// { verdict: "risky", reasons: ["typo_domain"], address: "sam@gmial.com",
|
|
35
|
+
// suggestion: "sam@gmail.com" }
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Layers, cheapest first: syntax, typo suggestion, disposable domain, role address (`info@`),
|
|
39
|
+
then an MX lookup over DNS-over-HTTPS. Verdicts are `valid`, `risky` (let the user continue but
|
|
40
|
+
think twice) and `invalid` (do not send). Reasons are stable codes: `invalid_syntax`,
|
|
41
|
+
`typo_domain`, `disposable_domain`, `role_address`, `domain_not_found`, `no_mx`, `null_mx`,
|
|
42
|
+
`mx_lookup_failed`.
|
|
43
|
+
|
|
44
|
+
Options: `resolver` (inject your own `DnsResolver`), `skipMx`, `extraDisposableDomains`, and
|
|
45
|
+
`disposablePolicy` (`"invalid"` by default, or `"risky"`). A DNS failure is only ever `risky`.
|
|
46
|
+
|
|
47
|
+
There is deliberately **no SMTP mailbox probing**. Large providers answer every probe the
|
|
48
|
+
same way, the answers that do differ are unreliable, and probing from a sending network can get
|
|
49
|
+
that network flagged.
|
|
50
|
+
|
|
51
|
+
The bundled disposable list is a small seed. Add domains for yourself with
|
|
52
|
+
`extraDisposableDomains`, or send a pull request to extend the seed.
|
|
53
|
+
|
|
54
|
+
### Consent: signed tokens and the send decision
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { signToken, verifyToken, canSendMarketing } from "@simplycubed/email-guard";
|
|
58
|
+
|
|
59
|
+
const token = await signToken(
|
|
60
|
+
{ purpose: "confirm", address: "sam@example.com", ttlSeconds: 48 * 3600 },
|
|
61
|
+
[env.TOKEN_SECRET_CURRENT, env.TOKEN_SECRET_PREVIOUS], // first signs, any verifies
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const result = await verifyToken(token, [env.TOKEN_SECRET_CURRENT, env.TOKEN_SECRET_PREVIOUS], {
|
|
65
|
+
purpose: "confirm",
|
|
66
|
+
});
|
|
67
|
+
if (result.valid) {
|
|
68
|
+
// result.address is the normalized address that clicked.
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const decision = canSendMarketing({
|
|
72
|
+
confirmed: true,
|
|
73
|
+
suppressed: false, // from your own suppression lookup; anything but `false` denies
|
|
74
|
+
lastMarketingSentAt: [], // epoch ms of earlier marketing sends to this address
|
|
75
|
+
now: Date.now(),
|
|
76
|
+
});
|
|
77
|
+
// { allow: true, reason: "ok" }
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Tokens are stateless: `base64url(payload).base64url(HMAC-SHA256)`. They are bound to a purpose
|
|
81
|
+
(a confirm link cannot be replayed as an unsubscribe link), expire, and verify in constant time.
|
|
82
|
+
Secrets must be at least 32 characters (use 32 random bytes). Tokens are signed, not
|
|
83
|
+
encrypted, and are replayable until they expire, so make your confirm handler idempotent. `issuedAt` and `now` are epoch milliseconds, and
|
|
84
|
+
`ttlSeconds` is in seconds.
|
|
85
|
+
|
|
86
|
+
`canSendMarketing` is a pure function over facts you supply. It requires a confirmed lead or a
|
|
87
|
+
verified, open account, never allows a suppressed address, and caps sends at 1 per rolling 24
|
|
88
|
+
hours and 12 per rolling 30 days (override with the second argument).
|
|
89
|
+
|
|
90
|
+
### Throttle
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import { createThrottle } from "@simplycubed/email-guard";
|
|
94
|
+
|
|
95
|
+
const throttle = createThrottle(myStore, {
|
|
96
|
+
address: { limit: 3, windowSeconds: 3600 },
|
|
97
|
+
ip: { limit: 20, windowSeconds: 3600 },
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const gate = await throttle.consumeAll([
|
|
101
|
+
["address", normalizeAddress(email)],
|
|
102
|
+
["ip", request.headers.get("cf-connecting-ip") ?? "unknown"],
|
|
103
|
+
]);
|
|
104
|
+
if (!gate.allowed)
|
|
105
|
+
return new Response("Slow down", {
|
|
106
|
+
status: 429,
|
|
107
|
+
headers: { "retry-after": String(gate.retryAfterSeconds) },
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`myStore` implements `ThrottleStore` (`get` and an atomic `increment` that sets the expiry only
|
|
112
|
+
when it creates the counter). Use a Durable Object, a SQL database or a Postgres upsert. Workers KV is not
|
|
113
|
+
atomic and is the wrong store.
|
|
114
|
+
|
|
115
|
+
### Suppression
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { suppress, isSuppressed } from "@simplycubed/email-guard";
|
|
119
|
+
|
|
120
|
+
await suppress(mySuppressionStore, { address, reason: "unsubscribe", source: "my-app" });
|
|
121
|
+
if (await isSuppressed(mySuppressionStore, address)) return;
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Addresses are normalized by `normalizeAddress`: trim, lowercase and drop a trailing domain dot,
|
|
125
|
+
nothing more. Mail the normalized address, never the raw input. Plus tags and
|
|
126
|
+
provider dot rules are **not** folded, because they are provider-specific guesses and folding
|
|
127
|
+
the wrong way would silence a different person.
|
|
128
|
+
|
|
129
|
+
## A Worker flow: signup form and lead form
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import {
|
|
133
|
+
checkAddress,
|
|
134
|
+
createThrottle,
|
|
135
|
+
isSuppressed,
|
|
136
|
+
normalizeAddress,
|
|
137
|
+
signToken,
|
|
138
|
+
verifyToken,
|
|
139
|
+
canSendMarketing,
|
|
140
|
+
suppress,
|
|
141
|
+
} from "@simplycubed/email-guard";
|
|
142
|
+
|
|
143
|
+
export default {
|
|
144
|
+
async fetch(request: Request, env: Env): Promise<Response> {
|
|
145
|
+
const url = new URL(request.url);
|
|
146
|
+
const secrets = [env.TOKEN_SECRET_CURRENT, env.TOKEN_SECRET_PREVIOUS];
|
|
147
|
+
const throttle = createThrottle(env.throttleStore, {
|
|
148
|
+
address: { limit: 3, windowSeconds: 3600 },
|
|
149
|
+
ip: { limit: 20, windowSeconds: 3600 },
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// 1. Lead form: never enroll on submit. Mail a confirm link and stop.
|
|
153
|
+
if (url.pathname === "/lead" && request.method === "POST") {
|
|
154
|
+
const { email } = (await request.json()) as { email: string };
|
|
155
|
+
const address = normalizeAddress(email);
|
|
156
|
+
|
|
157
|
+
const gate = await throttle.consumeAll([
|
|
158
|
+
["address", address],
|
|
159
|
+
["ip", request.headers.get("cf-connecting-ip") ?? "unknown"],
|
|
160
|
+
]);
|
|
161
|
+
if (!gate.allowed) return new Response("Too many requests", { status: 429 });
|
|
162
|
+
|
|
163
|
+
const check = await checkAddress(address);
|
|
164
|
+
if (check.verdict === "invalid")
|
|
165
|
+
return Response.json({ ok: false, reasons: check.reasons }, { status: 422 });
|
|
166
|
+
if (await isSuppressed(env.suppressionStore, address)) return Response.json({ ok: true }); // say nothing
|
|
167
|
+
|
|
168
|
+
const token = await signToken(
|
|
169
|
+
{ purpose: "confirm", address, ttlSeconds: 48 * 3600 },
|
|
170
|
+
secrets,
|
|
171
|
+
);
|
|
172
|
+
await env.sendMail({ to: address, link: `${url.origin}/confirm?t=${token}` });
|
|
173
|
+
return Response.json({ ok: true, suggestion: check.suggestion });
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// 2. Confirm link: only now is the lead enrolled. Use a POST from a confirm page, so a
|
|
177
|
+
// mail scanner that pre-fetches links cannot confirm for the owner.
|
|
178
|
+
if (url.pathname === "/confirm" && request.method === "POST") {
|
|
179
|
+
const result = await verifyToken(url.searchParams.get("t") ?? "", secrets, {
|
|
180
|
+
purpose: "confirm",
|
|
181
|
+
});
|
|
182
|
+
if (!result.valid) return new Response("Link invalid or expired", { status: 400 });
|
|
183
|
+
await env.enrollLead(result.address);
|
|
184
|
+
return Response.json({ ok: true });
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// 3. Unsubscribe link: stateless, then recorded in the shared suppression list.
|
|
188
|
+
if (url.pathname === "/unsubscribe" && request.method === "POST") {
|
|
189
|
+
const result = await verifyToken(url.searchParams.get("t") ?? "", secrets, {
|
|
190
|
+
purpose: "unsubscribe",
|
|
191
|
+
});
|
|
192
|
+
if (!result.valid) return new Response("Link invalid or expired", { status: 400 });
|
|
193
|
+
await suppress(env.suppressionStore, {
|
|
194
|
+
address: result.address,
|
|
195
|
+
reason: "unsubscribe",
|
|
196
|
+
source: "my-app",
|
|
197
|
+
});
|
|
198
|
+
return Response.json({ ok: true });
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return new Response("Not found", { status: 404 });
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// 4. Before every marketing send (a cron stepper, for example):
|
|
206
|
+
async function mayMail(env: Env, address: string): Promise<boolean> {
|
|
207
|
+
const row = await env.loadContactFacts(address); // your database
|
|
208
|
+
const decision = canSendMarketing({
|
|
209
|
+
confirmed: row.confirmed,
|
|
210
|
+
accountVerified: row.accountVerified,
|
|
211
|
+
accountClosed: row.accountClosed,
|
|
212
|
+
suppressed: await isSuppressed(env.suppressionStore, address),
|
|
213
|
+
lastMarketingSentAt: row.sentTimestamps,
|
|
214
|
+
now: Date.now(),
|
|
215
|
+
});
|
|
216
|
+
return decision.allow;
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Testing your integration
|
|
221
|
+
|
|
222
|
+
`@simplycubed/email-guard/testing` exports in-memory stores and a fake resolver so your tests
|
|
223
|
+
never touch a network or a database:
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
import {
|
|
227
|
+
createMemoryThrottleStore,
|
|
228
|
+
createMemorySuppressionStore,
|
|
229
|
+
createFakeResolver,
|
|
230
|
+
} from "@simplycubed/email-guard/testing";
|
|
231
|
+
|
|
232
|
+
const resolver = createFakeResolver({
|
|
233
|
+
"example.com": { kind: "records", exchanges: ["mx.example.com"] },
|
|
234
|
+
});
|
|
235
|
+
await checkAddress("sam@example.com", { resolver });
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## Development
|
|
239
|
+
|
|
240
|
+
```sh
|
|
241
|
+
pnpm install
|
|
242
|
+
pnpm check # lint, typecheck, tests with coverage thresholds, build, leak scan
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Node 24 or newer. The leak scan (`scripts/no-leaks.mjs`) keeps product names, internal hostnames
|
|
246
|
+
and secret-shaped strings out of everything that ships.
|
|
247
|
+
|
|
248
|
+
## License
|
|
249
|
+
|
|
250
|
+
MIT
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AddressCheck, CheckAddressOptions } from "./types.js";
|
|
2
|
+
export declare function isValidSyntax(email: string): boolean;
|
|
3
|
+
export declare function isDisposableDomain(domain: string, extra?: Iterable<string>): boolean;
|
|
4
|
+
export declare function suggestDomain(domain: string): string | null;
|
|
5
|
+
export declare function checkAddress(email: string, options?: CheckAddressOptions): Promise<AddressCheck>;
|
|
6
|
+
//# sourceMappingURL=address.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../src/address.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAiB,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAenF,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAUpD;AAGD,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,GAAG,OAAO,CAWpF;AAGD,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAO3D;AAiBD,wBAAsB,YAAY,CAChC,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,mBAAwB,GAChC,OAAO,CAAC,YAAY,CAAC,CA4CvB"}
|
package/dist/address.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { DISPOSABLE_DOMAINS, ROLE_LOCAL_PARTS, TYPO_DOMAINS } from "./disposable-domains.js";
|
|
2
|
+
import { createDohResolver } from "./doh.js";
|
|
3
|
+
import { normalizeAddress, splitAddress } from "./normalize.js";
|
|
4
|
+
const LOCAL_PATTERN = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*$/;
|
|
5
|
+
const LABEL_PATTERN = /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
|
|
6
|
+
const TLD_PATTERN = /^(?:[a-z]{2,63}|xn--[a-z0-9-]{1,59})$/;
|
|
7
|
+
const DISPOSABLE_SEED = new Set(DISPOSABLE_DOMAINS);
|
|
8
|
+
const ROLE_SET = new Set(ROLE_LOCAL_PARTS);
|
|
9
|
+
export function isValidSyntax(email) {
|
|
10
|
+
const normalized = normalizeAddress(email);
|
|
11
|
+
if (normalized.length > 254)
|
|
12
|
+
return false;
|
|
13
|
+
const parts = splitAddress(normalized);
|
|
14
|
+
if (parts === null || parts.local.length > 64 || !LOCAL_PATTERN.test(parts.local))
|
|
15
|
+
return false;
|
|
16
|
+
if (parts.domain.length > 253)
|
|
17
|
+
return false;
|
|
18
|
+
const labels = parts.domain.split(".");
|
|
19
|
+
if (labels.length < 2)
|
|
20
|
+
return false;
|
|
21
|
+
if (!labels.every((label) => label.length <= 63 && LABEL_PATTERN.test(label)))
|
|
22
|
+
return false;
|
|
23
|
+
return TLD_PATTERN.test(labels[labels.length - 1]);
|
|
24
|
+
}
|
|
25
|
+
export function isDisposableDomain(domain, extra) {
|
|
26
|
+
if (domain.length > 253)
|
|
27
|
+
return false;
|
|
28
|
+
const extras = extra ? new Set([...extra].map((d) => d.trim().toLowerCase())) : null;
|
|
29
|
+
const labels = domain.trim().toLowerCase().split(".");
|
|
30
|
+
for (let i = 0; i < labels.length - 1; i++) {
|
|
31
|
+
const candidate = labels.slice(i).join(".");
|
|
32
|
+
if (DISPOSABLE_SEED.has(candidate) || extras?.has(candidate))
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
export function suggestDomain(domain) {
|
|
38
|
+
const lower = domain.trim().toLowerCase();
|
|
39
|
+
const known = TYPO_DOMAINS[lower];
|
|
40
|
+
if (known !== undefined)
|
|
41
|
+
return known;
|
|
42
|
+
const match = /^(.+)\.(?:con|cmo|ocm|vom)$/.exec(lower);
|
|
43
|
+
return match ? `${match[1]}.com` : null;
|
|
44
|
+
}
|
|
45
|
+
export async function checkAddress(email, options = {}) {
|
|
46
|
+
const address = normalizeAddress(email);
|
|
47
|
+
if (!isValidSyntax(address)) {
|
|
48
|
+
return { verdict: "invalid", reasons: ["invalid_syntax"], address };
|
|
49
|
+
}
|
|
50
|
+
const { local, domain } = splitAddress(address);
|
|
51
|
+
const invalid = [];
|
|
52
|
+
const risky = [];
|
|
53
|
+
let suggestion;
|
|
54
|
+
const corrected = suggestDomain(domain);
|
|
55
|
+
if (corrected !== null) {
|
|
56
|
+
risky.push("typo_domain");
|
|
57
|
+
suggestion = `${local}@${corrected}`;
|
|
58
|
+
}
|
|
59
|
+
if (isDisposableDomain(domain, options.extraDisposableDomains)) {
|
|
60
|
+
(options.disposablePolicy === "risky" ? risky : invalid).push("disposable_domain");
|
|
61
|
+
}
|
|
62
|
+
if (ROLE_SET.has(local))
|
|
63
|
+
risky.push("role_address");
|
|
64
|
+
if (!options.skipMx && invalid.length === 0 && corrected === null) {
|
|
65
|
+
const resolver = options.resolver ?? createDohResolver();
|
|
66
|
+
const lookup = await resolver.resolveMx(domain);
|
|
67
|
+
if (lookup.kind === "nxdomain") {
|
|
68
|
+
invalid.push("domain_not_found");
|
|
69
|
+
}
|
|
70
|
+
else if (lookup.kind === "error") {
|
|
71
|
+
risky.push("mx_lookup_failed");
|
|
72
|
+
}
|
|
73
|
+
else if (lookup.exchanges.length === 0) {
|
|
74
|
+
invalid.push("no_mx");
|
|
75
|
+
}
|
|
76
|
+
else if (lookup.exchanges.length === 1 && lookup.exchanges[0] === ".") {
|
|
77
|
+
invalid.push("null_mx");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const reasons = [...invalid, ...risky];
|
|
81
|
+
const verdict = invalid.length > 0 ? "invalid" : risky.length > 0 ? "risky" : "valid";
|
|
82
|
+
return suggestion === undefined
|
|
83
|
+
? { verdict, reasons, address }
|
|
84
|
+
: { verdict, reasons, address, suggestion };
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=address.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"address.js","sourceRoot":"","sources":["../src/address.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC7F,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAGhE,MAAM,aAAa,GAAG,mEAAmE,CAAC;AAC1F,MAAM,aAAa,GAAG,mCAAmC,CAAC;AAC1D,MAAM,WAAW,GAAG,uCAAuC,CAAC;AAE5D,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC,kBAAkB,CAAC,CAAC;AACpD,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,gBAAgB,CAAC,CAAC;AAQ3C,MAAM,UAAU,aAAa,CAAC,KAAa;IACzC,MAAM,UAAU,GAAG,gBAAgB,CAAC,KAAK,CAAC,CAAC;IAC3C,IAAI,UAAU,CAAC,MAAM,GAAG,GAAG;QAAE,OAAO,KAAK,CAAC;IAC1C,MAAM,KAAK,GAAG,YAAY,CAAC,UAAU,CAAC,CAAC;IACvC,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,EAAE,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAChG,IAAI,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,GAAG;QAAE,OAAO,KAAK,CAAC;IAC5C,MAAM,MAAM,GAAG,KA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AAO,UAAU,KAAK,SAAS;QAC7B,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE;QAC/B,CAAC,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consent.d.ts","sourceRoot":"","sources":["../src/consent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AA6BnF,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,cAAc,EACrB,IAAI,GAAE,aAAkB,GACvB,iBAAiB,CA2BnB"}
|
package/dist/consent.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
2
|
+
const MONTH_MS = 30 * DAY_MS;
|
|
3
|
+
function cap(name, value, fallback) {
|
|
4
|
+
if (value === undefined)
|
|
5
|
+
return fallback;
|
|
6
|
+
if (!Number.isInteger(value) || value < 1) {
|
|
7
|
+
throw new Error(`email-guard: caps.${name} must be an integer of at least 1`);
|
|
8
|
+
}
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
export function canSendMarketing(state, caps = {}) {
|
|
12
|
+
if (!Number.isFinite(state.now))
|
|
13
|
+
throw new Error("email-guard: state.now must be a number");
|
|
14
|
+
const perDay = cap("perDay", caps.perDay, 1);
|
|
15
|
+
const perMonth = cap("perMonth", caps.perMonth, 12);
|
|
16
|
+
if (state.suppressed !== false)
|
|
17
|
+
return { allow: false, reason: "suppressed" };
|
|
18
|
+
if (state.accountClosed === true)
|
|
19
|
+
return { allow: false, reason: "account_closed" };
|
|
20
|
+
if (state.confirmed !== true && state.accountVerified !== true) {
|
|
21
|
+
return { allow: false, reason: "not_confirmed" };
|
|
22
|
+
}
|
|
23
|
+
const sent = state.lastMarketingSentAt.map((t) => (Number.isFinite(t) ? t : state.now));
|
|
24
|
+
const inDay = sent.filter((t) => t > state.now - DAY_MS).sort((a, b) => a - b);
|
|
25
|
+
if (inDay.length >= perDay) {
|
|
26
|
+
const pivot = inDay[inDay.length - perDay];
|
|
27
|
+
return { allow: false, reason: "daily_cap", retryAt: pivot + DAY_MS };
|
|
28
|
+
}
|
|
29
|
+
const inMonth = sent.filter((t) => t > state.now - MONTH_MS).sort((a, b) => a - b);
|
|
30
|
+
if (inMonth.length >= perMonth) {
|
|
31
|
+
const pivot = inMonth[inMonth.length - perMonth];
|
|
32
|
+
return { allow: false, reason: "monthly_cap", retryAt: pivot + MONTH_MS };
|
|
33
|
+
}
|
|
34
|
+
return { allow: true, reason: "ok" };
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=consent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consent.js","sourceRoot":"","sources":["../src/consent.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AACnC,MAAM,QAAQ,GAAG,EAAE,GAAG,MAAM,CAAC;AAE7B,SAAS,GAAG,CAAC,IAAY,EAAE,KAAyB,EAAE,QAAgB;IACpE,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,QAAQ,CAAC;IACzC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,KAAK,CAAC,qBAAqB,IAAI,mCAAmC,CAAC,CAAC;IAChF,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAkBD,MAAM,UAAU,gBAAgB,CAC9B,KAAqB,EACrB,OAAsB,EAAE;IAExB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;IAC5F,MAAM,MAAM,GAAG,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AAE,CAAC;IACxE,CAAC;IACD,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,GAAG,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IACnF,IAAI,OAAO,CAAC,MAAM,IAAI,QAAQ,EAAE,CAAC;QAC/B,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,QAAQ,CAAW,CAAC;QAC3D,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,OAAO,EAAE,KAAK,GAAG,QAAQ,EAAE,CAAC;IAC5E,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"disposable-domains.d.ts","sourceRoot":"","sources":["../src/disposable-domains.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,kBAAkB,EAAE,SAAS,MAAM,EAyC/C,CAAC;AAGF,eAAO,MAAM,gBAAgB,EAAE,SAAS,MAAM,EAyB7C,CAAC;AAGF,eAAO,MAAM,YAAY,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAsBzD,CAAC"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export const DISPOSABLE_DOMAINS = [
|
|
2
|
+
"10minutemail.com",
|
|
3
|
+
"10minutemail.net",
|
|
4
|
+
"20minutemail.com",
|
|
5
|
+
"dispostable.com",
|
|
6
|
+
"dropmail.me",
|
|
7
|
+
"emailondeck.com",
|
|
8
|
+
"fakeinbox.com",
|
|
9
|
+
"getairmail.com",
|
|
10
|
+
"getnada.com",
|
|
11
|
+
"guerrillamail.biz",
|
|
12
|
+
"guerrillamail.com",
|
|
13
|
+
"guerrillamail.de",
|
|
14
|
+
"guerrillamail.net",
|
|
15
|
+
"guerrillamail.org",
|
|
16
|
+
"harakirimail.com",
|
|
17
|
+
"inboxbear.com",
|
|
18
|
+
"maildrop.cc",
|
|
19
|
+
"mailinator.com",
|
|
20
|
+
"mailinator.net",
|
|
21
|
+
"mailnesia.com",
|
|
22
|
+
"mailtemp.net",
|
|
23
|
+
"mintemail.com",
|
|
24
|
+
"moakt.com",
|
|
25
|
+
"mohmal.com",
|
|
26
|
+
"mytemp.email",
|
|
27
|
+
"sharklasers.com",
|
|
28
|
+
"spam4.me",
|
|
29
|
+
"spamgourmet.com",
|
|
30
|
+
"temp-mail.io",
|
|
31
|
+
"temp-mail.org",
|
|
32
|
+
"tempail.com",
|
|
33
|
+
"tempinbox.com",
|
|
34
|
+
"tempmail.com",
|
|
35
|
+
"tempmailo.com",
|
|
36
|
+
"throwawaymail.com",
|
|
37
|
+
"trashmail.com",
|
|
38
|
+
"trashmail.net",
|
|
39
|
+
"yopmail.com",
|
|
40
|
+
"yopmail.fr",
|
|
41
|
+
"yopmail.net",
|
|
42
|
+
];
|
|
43
|
+
export const ROLE_LOCAL_PARTS = [
|
|
44
|
+
"abuse",
|
|
45
|
+
"admin",
|
|
46
|
+
"administrator",
|
|
47
|
+
"billing",
|
|
48
|
+
"contact",
|
|
49
|
+
"do-not-reply",
|
|
50
|
+
"donotreply",
|
|
51
|
+
"enquiries",
|
|
52
|
+
"help",
|
|
53
|
+
"hostmaster",
|
|
54
|
+
"hello",
|
|
55
|
+
"info",
|
|
56
|
+
"mail",
|
|
57
|
+
"marketing",
|
|
58
|
+
"no-reply",
|
|
59
|
+
"noreply",
|
|
60
|
+
"office",
|
|
61
|
+
"postmaster",
|
|
62
|
+
"root",
|
|
63
|
+
"sales",
|
|
64
|
+
"security",
|
|
65
|
+
"support",
|
|
66
|
+
"team",
|
|
67
|
+
"webmaster",
|
|
68
|
+
];
|
|
69
|
+
export const TYPO_DOMAINS = {
|
|
70
|
+
"gamil.com": "gmail.com",
|
|
71
|
+
"gmai.com": "gmail.com",
|
|
72
|
+
"gmail.co": "gmail.com",
|
|
73
|
+
"gmail.con": "gmail.com",
|
|
74
|
+
"gmal.com": "gmail.com",
|
|
75
|
+
"gmial.com": "gmail.com",
|
|
76
|
+
"gnail.com": "gmail.com",
|
|
77
|
+
"googlemail.con": "googlemail.com",
|
|
78
|
+
"hotmai.com": "hotmail.com",
|
|
79
|
+
"hotmial.com": "hotmail.com",
|
|
80
|
+
"hotnail.com": "hotmail.com",
|
|
81
|
+
"hotmail.co": "hotmail.com",
|
|
82
|
+
"icloud.con": "icloud.com",
|
|
83
|
+
"iclod.com": "icloud.com",
|
|
84
|
+
"outlok.com": "outlook.com",
|
|
85
|
+
"outlook.co": "outlook.com",
|
|
86
|
+
"protonmail.con": "protonmail.com",
|
|
87
|
+
"yaho.com": "yahoo.com",
|
|
88
|
+
"yahoo.con": "yahoo.com",
|
|
89
|
+
"yahooo.com": "yahoo.com",
|
|
90
|
+
"yhoo.com": "yahoo.com",
|
|
91
|
+
};
|
|
92
|
+
//# sourceMappingURL=disposable-domains.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"disposable-domains.js","sourceRoot":"","sources":["../src/disposable-domains.ts"],"names":[],"mappings":"AAMA,MAAM,CAAC,MAAM,kBAAkB,GAAsB;IACnD,kBAAkB;IAClB,kBAAkB;IAClB,kBAAkB;IAClB,iBAAiB;IACjB,aAAa;IACb,iBAAiB;IACjB,eAAe;IACf,gBAAgB;IAChB,aAAa;IACb,mBAAmB;IACnB,mBAAmB;IACnB,kBAAkB;IAClB,mBAAmB;IACnB,mBAAmB;IACnB,kBAAkB;IAClB,eAAe;IACf,aAAa;IACb,gBAAgB;IAChB,gBAAgB;IAChB,eAAe;IACf,cAAc;IACd,eAAe;IACf,WAAW;IACX,YAAY;IACZ,cAAc;IACd,iBAAiB;IACjB,UAAU;IACV,iBAAiB;IACjB,cAAc;IACd,eAAe;IACf,aAAa;IACb,eAAe;IACf,cAAc;IACd,eAAe;IACf,mBAAmB;IACnB,eAAe;IACf,eAAe;IACf,aAAa;IACb,YAAY;IACZ,aAAa;CACd,CAAC;AAGF,MAAM,CAAC,MAAM,gBAAgB,GAAsB;IACjD,OAAO;IACP,OAAO;IACP,eAAe;IACf,SAAS;IACT,SAAS;IACT,cAAc;IACd,YAAY;IACZ,WAAW;IACX,MAAM;IACN,YAAY;IACZ,OAAO;IACP,MAAM;IACN,MAAM;IACN,WAAW;IACX,UAAU;IACV,SAAS;IACT,QAAQ;IACR,YAAY;IACZ,MAAM;IACN,OAAO;IACP,UAAU;IACV,SAAS;IACT,MAAM;IACN,WAAW;CACZ,CAAC;AAGF,MAAM,CAAC,MAAM,YAAY,GAAqC;IAC5D,WAAW,EAAE,WAAW;IACxB,UAAU,EAAE,WAAW;IACvB,UAAU,EAAE,WAAW;IACvB,WAAW,EAAE,WAAW;IACxB,UAAU,EAAE,WAAW;IACvB,WAAW,EAAE,WAAW;IACxB,WAAW,EAAE,WAAW;IACxB,gBAAgB,EAAE,gBAAgB;IAClC,YAAY,EAAE,aAAa;IAC3B,aAAa,EAAE,aAAa;IAC5B,aAAa,EAAE,aAAa;IAC5B,YAAY,EAAE,aAAa;IAC3B,YAAY,EAAE,YAAY;IAC1B,WAAW,EAAE,YAAY;IACzB,YAAY,EAAE,aAAa;IAC3B,YAAY,EAAE,aAAa;IAC3B,gBAAgB,EAAE,gBAAgB;IAClC,UAAU,EAAE,WAAW;IACvB,WAAW,EAAE,WAAW;IACxB,YAAY,EAAE,WAAW;IACzB,UAAU,EAAE,WAAW;CACxB,CAAC"}
|
package/dist/doh.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { DnsResolver } from "./types.js";
|
|
2
|
+
export type DohFetch = (url: string, init: {
|
|
3
|
+
headers: Record<string, string>;
|
|
4
|
+
signal: AbortSignal;
|
|
5
|
+
}) => Promise<{
|
|
6
|
+
ok: boolean;
|
|
7
|
+
status: number;
|
|
8
|
+
json(): Promise<unknown>;
|
|
9
|
+
}>;
|
|
10
|
+
export interface DohResolverOptions {
|
|
11
|
+
endpoint?: string;
|
|
12
|
+
fetch?: DohFetch;
|
|
13
|
+
timeoutMs?: number;
|
|
14
|
+
}
|
|
15
|
+
export declare function createDohResolver(options?: DohResolverOptions): DnsResolver;
|
|
16
|
+
//# sourceMappingURL=doh.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"doh.d.ts","sourceRoot":"","sources":["../src/doh.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAY,MAAM,YAAY,CAAC;AAGxD,MAAM,MAAM,QAAQ,GAAG,CACrB,GAAG,EAAE,MAAM,EACX,IAAI,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAAC,MAAM,EAAE,WAAW,CAAA;CAAE,KAC3D,OAAO,CAAC;IAAE,EAAE,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,IAAI,OAAO,CAAC,OAAO,CAAC,CAAA;CAAE,CAAC,CAAC;AAExE,MAAM,WAAW,kBAAkB;IAKjC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB,KAAK,CAAC,EAAE,QAAQ,CAAC;IAEjB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AA6CD,wBAAgB,iBAAiB,CAAC,OAAO,GAAE,kBAAuB,GAAG,WAAW,CAmD/E"}
|
package/dist/doh.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const DEFAULT_ENDPOINT = "https://cloudflare-dns.com/dns-query";
|
|
2
|
+
const TYPE_A = 1;
|
|
3
|
+
const TYPE_AAAA = 28;
|
|
4
|
+
const TYPE_MX = 15;
|
|
5
|
+
const RCODE_NOERROR = 0;
|
|
6
|
+
const RCODE_NXDOMAIN = 3;
|
|
7
|
+
const MAX_ANSWERS = 64;
|
|
8
|
+
const MAX_HOST_LENGTH = 255;
|
|
9
|
+
function parseReply(body) {
|
|
10
|
+
if (typeof body !== "object" || body === null)
|
|
11
|
+
return null;
|
|
12
|
+
const record = body;
|
|
13
|
+
if (typeof record.Status !== "number")
|
|
14
|
+
return null;
|
|
15
|
+
const answers = [];
|
|
16
|
+
if (Array.isArray(record.Answer)) {
|
|
17
|
+
for (const item of record.Answer.slice(0, MAX_ANSWERS)) {
|
|
18
|
+
if (typeof item !== "object" || item === null)
|
|
19
|
+
continue;
|
|
20
|
+
const { type, data } = item;
|
|
21
|
+
if (typeof type === "number" && typeof data === "string")
|
|
22
|
+
answers.push({ type, data });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return { rcode: record.Status, answers };
|
|
26
|
+
}
|
|
27
|
+
export function createDohResolver(options = {}) {
|
|
28
|
+
const endpoint = new URL(options.endpoint ?? DEFAULT_ENDPOINT);
|
|
29
|
+
if (endpoint.protocol !== "https:") {
|
|
30
|
+
throw new Error("email-guard: the DNS-over-HTTPS endpoint must be an https URL");
|
|
31
|
+
}
|
|
32
|
+
const timeoutMs = options.timeoutMs ?? 3000;
|
|
33
|
+
const doFetch = options.fetch ?? ((url, init) => fetch(url, init));
|
|
34
|
+
async function query(name, type) {
|
|
35
|
+
const target = new URL(endpoint);
|
|
36
|
+
target.searchParams.set("name", name);
|
|
37
|
+
target.searchParams.set("type", String(type));
|
|
38
|
+
const response = await doFetch(target.toString(), {
|
|
39
|
+
headers: { accept: "application/dns-json" },
|
|
40
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
41
|
+
});
|
|
42
|
+
if (!response.ok)
|
|
43
|
+
throw new Error(`DNS-over-HTTPS endpoint returned HTTP ${response.status}`);
|
|
44
|
+
const reply = parseReply(await response.json());
|
|
45
|
+
if (reply === null)
|
|
46
|
+
throw new Error("DNS-over-HTTPS endpoint returned an unreadable reply");
|
|
47
|
+
return reply;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
async resolveMx(domain) {
|
|
51
|
+
try {
|
|
52
|
+
const mx = await query(domain, TYPE_MX);
|
|
53
|
+
if (mx.rcode === RCODE_NXDOMAIN)
|
|
54
|
+
return { kind: "nxdomain" };
|
|
55
|
+
if (mx.rcode !== RCODE_NOERROR)
|
|
56
|
+
return { kind: "error", message: `DNS rcode ${mx.rcode}` };
|
|
57
|
+
const exchanges = mx.answers
|
|
58
|
+
.filter((a) => a.type === TYPE_MX)
|
|
59
|
+
.map((a) => a.data.trim().split(/\s+/)[1])
|
|
60
|
+
.filter((host) => typeof host === "string" && host.length > 0 && host.length <= MAX_HOST_LENGTH)
|
|
61
|
+
.map((host) => (host === "." ? host : host.replace(/\.$/, "").toLowerCase()));
|
|
62
|
+
if (exchanges.length > 0)
|
|
63
|
+
return { kind: "records", exchanges };
|
|
64
|
+
for (const type of [TYPE_A, TYPE_AAAA]) {
|
|
65
|
+
const addr = await query(domain, type);
|
|
66
|
+
if (addr.answers.some((a) => a.type === type)) {
|
|
67
|
+
return { kind: "records", exchanges: [domain] };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return { kind: "records", exchanges: [] };
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
return { kind: "error", message: err instanceof Error ? err.message : String(err) };
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=doh.js.map
|
package/dist/doh.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"doh.js","sourceRoot":"","sources":["../src/doh.ts"],"names":[],"mappings":"AAoBA,MAAM,gBAAgB,GAAG,sCAAsC,CAAC;AAChE,MAAM,MAAM,GAAG,CAAC,CAAC;AACjB,MAAM,SAAS,GAAG,EAAE,CAAC;AACrB,MAAM,OAAO,GAAG,EAAE,CAAC;AACnB,MAAM,aAAa,GAAG,CAAC,CAAC;AACxB,MAAM,cAAc,GAAG,CAAC,CAAC;AAEzB,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