@simplycubed/email-guard 0.0.0-stage → 0.1.0
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/package.json +69 -4
- 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 +50 -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/src/types.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/** Result of one MX lookup. A lookup that could not be completed is `error`, never a guess. */
|
|
2
|
+
export type MxLookup =
|
|
3
|
+
| {
|
|
4
|
+
kind: "records";
|
|
5
|
+
/** Mail exchanger hostnames. An empty list means the domain exists but takes no mail. */
|
|
6
|
+
exchanges: readonly string[];
|
|
7
|
+
}
|
|
8
|
+
| { kind: "nxdomain" }
|
|
9
|
+
| { kind: "error"; message: string };
|
|
10
|
+
|
|
11
|
+
/** Injectable DNS resolver. The default is `createDohResolver()`. */
|
|
12
|
+
export interface DnsResolver {
|
|
13
|
+
resolveMx(domain: string): Promise<MxLookup>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type AddressVerdict = "valid" | "risky" | "invalid";
|
|
17
|
+
|
|
18
|
+
export type AddressReason =
|
|
19
|
+
| "invalid_syntax"
|
|
20
|
+
| "domain_not_found"
|
|
21
|
+
| "no_mx"
|
|
22
|
+
| "null_mx"
|
|
23
|
+
| "mx_lookup_failed"
|
|
24
|
+
| "disposable_domain"
|
|
25
|
+
| "typo_domain"
|
|
26
|
+
| "role_address";
|
|
27
|
+
|
|
28
|
+
export interface AddressCheck {
|
|
29
|
+
verdict: AddressVerdict;
|
|
30
|
+
reasons: AddressReason[];
|
|
31
|
+
/** The normalized address (see `normalizeAddress`). */
|
|
32
|
+
address: string;
|
|
33
|
+
/** Present with `typo_domain`: the full address the user probably meant. */
|
|
34
|
+
suggestion?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface CheckAddressOptions {
|
|
38
|
+
/** Resolver for the MX layer. Defaults to a Cloudflare DNS-over-HTTPS resolver. */
|
|
39
|
+
resolver?: DnsResolver;
|
|
40
|
+
/** Skip the MX layer entirely (no network). Defaults to false. */
|
|
41
|
+
skipMx?: boolean;
|
|
42
|
+
/** Extra throwaway domains, added to the bundled seed list. */
|
|
43
|
+
extraDisposableDomains?: Iterable<string>;
|
|
44
|
+
/** `invalid` (default) rejects disposable domains; `risky` only flags them. */
|
|
45
|
+
disposablePolicy?: "invalid" | "risky";
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type ConsentDenyReason =
|
|
49
|
+
"suppressed" | "account_closed" | "not_confirmed" | "daily_cap" | "monthly_cap";
|
|
50
|
+
|
|
51
|
+
export interface MarketingState {
|
|
52
|
+
/** The address clicked a double opt-in confirm link (a lead). */
|
|
53
|
+
confirmed: boolean;
|
|
54
|
+
/** The address belongs to an account whose email was verified. */
|
|
55
|
+
accountVerified?: boolean;
|
|
56
|
+
/** The account is closed or deactivated. */
|
|
57
|
+
accountClosed?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* The address is on a suppression list. Required, and anything but an explicit `false`
|
|
60
|
+
* denies, so forgetting the lookup can never read as "not suppressed".
|
|
61
|
+
*/
|
|
62
|
+
suppressed: boolean;
|
|
63
|
+
/** Epoch milliseconds of each marketing email already sent to this address. */
|
|
64
|
+
lastMarketingSentAt: readonly number[];
|
|
65
|
+
/** Epoch milliseconds, supplied by the caller so the decision stays pure. */
|
|
66
|
+
now: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface MarketingCaps {
|
|
70
|
+
/** Maximum marketing emails per rolling 24 hours. Default 1. */
|
|
71
|
+
perDay?: number;
|
|
72
|
+
/** Maximum marketing emails per rolling 30 days. Default 12. */
|
|
73
|
+
perMonth?: number;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export type MarketingDecision =
|
|
77
|
+
{ allow: true; reason: "ok" } | { allow: false; reason: ConsentDenyReason; retryAt?: number };
|
|
78
|
+
|
|
79
|
+
export interface TokenPayload {
|
|
80
|
+
/** What the token is for, for example `confirm` or `unsubscribe`. Binds the token. */
|
|
81
|
+
purpose: string;
|
|
82
|
+
address: string;
|
|
83
|
+
/** Epoch milliseconds. Defaults to now. */
|
|
84
|
+
issuedAt?: number;
|
|
85
|
+
/** Lifetime in seconds. */
|
|
86
|
+
ttlSeconds: number;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type TokenFailure =
|
|
90
|
+
"malformed" | "bad_signature" | "wrong_purpose" | "expired" | "not_yet_valid";
|
|
91
|
+
|
|
92
|
+
export type TokenResult =
|
|
93
|
+
| {
|
|
94
|
+
valid: true;
|
|
95
|
+
purpose: string;
|
|
96
|
+
/** Normalized address. */
|
|
97
|
+
address: string;
|
|
98
|
+
issuedAt: number;
|
|
99
|
+
expiresAt: number;
|
|
100
|
+
}
|
|
101
|
+
| { valid: false; reason: TokenFailure };
|
|
102
|
+
|
|
103
|
+
export interface VerifyTokenOptions {
|
|
104
|
+
/** The purpose the caller expects. A correctly signed token for another purpose fails. */
|
|
105
|
+
purpose: string;
|
|
106
|
+
/** Epoch milliseconds. Defaults to now. */
|
|
107
|
+
now?: number;
|
|
108
|
+
/** Tolerated clock skew for `issuedAt` in the future, in seconds. Default 60. */
|
|
109
|
+
clockSkewSeconds?: number;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface ThrottleRule {
|
|
113
|
+
limit: number;
|
|
114
|
+
windowSeconds: number;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface ThrottleCounter {
|
|
118
|
+
count: number;
|
|
119
|
+
/** Epoch milliseconds at which the counter resets. */
|
|
120
|
+
expiresAt: number;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Counter storage supplied by the consumer. `increment` MUST be atomic and MUST set the
|
|
125
|
+
* expiry only when it creates the counter (fixed window). Workers KV is not atomic and is
|
|
126
|
+
* the wrong backing store; use a Durable Object, SQL database or Postgres upsert.
|
|
127
|
+
*/
|
|
128
|
+
export interface ThrottleStore {
|
|
129
|
+
get(key: string): Promise<ThrottleCounter | null>;
|
|
130
|
+
increment(key: string, ttlSeconds: number): Promise<ThrottleCounter>;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface ThrottleResult {
|
|
134
|
+
allowed: boolean;
|
|
135
|
+
remaining: number;
|
|
136
|
+
retryAfterSeconds: number;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface Throttle {
|
|
140
|
+
/** Count one attempt against `rule` for `id` and say whether it is within the limit. */
|
|
141
|
+
consume(rule: string, id: string): Promise<ThrottleResult>;
|
|
142
|
+
/** Read the current state without counting an attempt. */
|
|
143
|
+
peek(rule: string, id: string): Promise<ThrottleResult>;
|
|
144
|
+
/** Count one attempt against every pair. Allowed only if all are. */
|
|
145
|
+
consumeAll(pairs: ReadonlyArray<readonly [rule: string, id: string]>): Promise<ThrottleResult>;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export type SuppressionReason = "unsubscribe" | "hard_bounce" | "complaint" | "manual";
|
|
149
|
+
|
|
150
|
+
export interface SuppressionEntry {
|
|
151
|
+
/** Always normalized. */
|
|
152
|
+
address: string;
|
|
153
|
+
reason: SuppressionReason;
|
|
154
|
+
/** Which product or system recorded it. Free text chosen by the consumer. */
|
|
155
|
+
source: string;
|
|
156
|
+
/** Epoch milliseconds. */
|
|
157
|
+
addedAt: number;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Suppression storage supplied by the consumer. Receives normalized addresses only. */
|
|
161
|
+
export interface SuppressionStore {
|
|
162
|
+
add(entry: SuppressionEntry): Promise<void>;
|
|
163
|
+
has(address: string): Promise<boolean>;
|
|
164
|
+
}
|