forked from boywithkeyboard-archive/is-disposable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
37 lines (28 loc) · 1.03 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import blocklist from './blocklist.json' assert { type: 'json' }
let remoteBlocklist: { cached_at: number; domains: string[] } | undefined
export default async function isDisposable(
email: string,
{ remote = false } = {},
) {
if (/^[\w\-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(email) === false) {
throw new Error('invalid email')
}
if (!remote) {
return (blocklist as string[]).includes(email.split('@')[1])
}
if (remoteBlocklist && Date.now() - remoteBlocklist.cached_at < 3_600_000) { // update cached list every hour
return remoteBlocklist.domains.includes(email.split('@')[1])
}
const response = await fetch(
'https://raw.githubusercontent.com/disposable-email-domains/disposable-email-domains/master/disposable_email_blocklist.conf',
)
if (!response.ok) {
throw new Error('failed to download remote blocklist')
}
const data = await response.text()
remoteBlocklist = {
cached_at: Date.now(),
domains: data.split('\n').slice(0, -1),
}
return remoteBlocklist.domains.includes(data)
}