forked from bennymeg/AcademicEmailVerifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (92 loc) · 3.65 KB
/
index.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const fs = require('fs');
const path = require('path');
const blacklist = require('./database/domains-blacklist');
const tld = require('./database/academic-tlds');
/**
* Exposes academic email verifier API
* @author Benny Megidish
*/
class Verifier {
static get domainsBasePath() {
return path.resolve(__dirname, './domains');
}
/**
* check's if the email address is valid (in an proper email format - RFC 2822)
* @param {string} emailAddress email address to be checked
* @returns {boolean} true, if the email address is valid, false, otherwise
*/
static isValidEmailAddress(emailAddress) {
let re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(emailAddress).toLowerCase());
}
/**
* converts tld to url format
* @param {string} emailAddress email address to be converted
* @returns {string} tld as url
*/
static domainToUrl(domain) {
let domainUrl = domain.split('.');
domainUrl[0] = domainUrl[0] + '.txt';
domainUrl = domainUrl.reverse().join('/');
return domainUrl;
}
/**
* check's if the email address is academic
* @param {string} emailAddress email address to be checked
* @returns {Promise<boolean>} true, if the email address is academic, false, otherwise
*/
static isAcademic(emailAddress) {
return new Promise((resolve, reject) => {
if (!this.isValidEmailAddress(emailAddress)) {
reject(Error("Email address is not properly formated"));
} else {
let domain = emailAddress.split("@").reverse()[0];
let domainUrl = this.domainToUrl(domain);
let domainPath = path.join(this.domainsBasePath, domainUrl);
// check if the domain is blacklisted
if (blacklist.indexOf(domain) === -1) {
// check if the domain file exist
fs.access(domainPath, fs.constants.F_OK, (error) => {
if (error) {
resolve(false);
} else {
resolve(true);
}
});
} else {
resolve(false);
}
}
});
}
/**
* query email address institution name
* @param {string} emailAddress email address to be checked
* @returns {Promise<string>} academic institution name, if exist, '', otherwise
*/
static getInstitutionName(emailAddress) {
return new Promise((resolve, reject) => {
if (!this.isValidEmailAddress(emailAddress)) {
reject(Error("Email address is not properly formated"));
} else {
let domain = emailAddress.split("@").reverse()[0];
let domainUrl = this.domainToUrl(domain);
let domainPath = path.join(this.domainsBasePath, domainUrl);
// check if the domain is blacklisted
if (blacklist.indexOf(domain) === -1) {
// try to read the domain institution name
fs.readFile(domainPath, (error, data) => {
if (error) {
reject(error);
} else {
resolve(data.toString('utf8'));
}
});
} else {
resolve('');
}
}
});
}
}
module.exports = Verifier;