-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
167 lines (144 loc) · 4.83 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/******************************************************************************
/* Module: email_address
/* Purpose: Email Address Validation and Introspection
/* using different forms including standard and conventional.
/* Author: Allen Fair <allen.fair@gmail.com> @allenfair
/*****************************************************************************/
const md5 = require("md5");
const sha1 = require("sha1");
const Local = require("./lib/local");
const Host = require("./lib/host");
const Config= require("./lib/config");
const CanonicalEmailAddress= require("./lib/canonical");
const RedactedEmailAddress = require("./lib/redacted");
var globalConfig = undefined;
class EmailAddress {
constructor(emailAddressString, config={}) {
if (globalConfig) this.config = Config(globalConfig);
this.config = Config(config, this.config);
if (typeof emailAddressString !== 'string'
|| emailAddressString instanceof String) {
emailAddressString = "";
}
this.original = emailAddressString;
this.errorCode = null;
this.parse();
this.host = new Host(this.hostString, this.config); // Must be before local!
this.local = new Local(this.localString, this.config);
this.setFormat(this.config.format);
this.isValid();
}
static async info(email) {
let emailAddress = new EmailAddress(email)
await emailAddress.isValid(true)
if (emailAddress.hostMx) {
await emailAddress.hostMx.exchangerIps()
}
return emailAddress.data()
}
/****************************************************************************
/* Operations and Validations
/****************************************************************************/
data() {
return {
original: this.original,
format: this.config.format,
address: this.toString(),
canonical: this.canonical(),
redacted: this.redacted(),
md5: this.digest('md5'),
sha1: this.digest('sha1'),
local: this.local.data(),
host: this.host.data(),
exchangers: this.hostMx === undefined ? [] : this.hostMx.mx,
valid: this.valid,
errorCode: this.errorCode
};
}
toString() {
return this.formatter.toString();
}
canonical() {
var version = new CanonicalEmailAddress(this, this.config);
return version.toString();
}
redacted() {
var version = new RedactedEmailAddress(this, this.config);
return version.toString();
}
digest(method='md5') {
let ea = this.toString().toLowerCase();
if (method === 'md5') {
return md5(ea);
} else if (method === 'sha1') {
return sha1(ea);
}
}
async isValid(checkDns=false) {
this.errorCode = this.formatter.error();
this.valid = this.errorCode ? false : true;
if (checkDns) {
if (!this.valid) {
//fn(false, this,errorCode);
} else {
const HostMx = require("./lib/hostmx");
this.hostMx = new HostMx(this.host.hostname, this.config);
await this.hostMx.validMailhost();
}
}
return this.valid;
}
error() {
if (this.isValid()) {
return null;
} else {
return this.config.errors[this.errorCode] || this.errorCode;
}
}
/****************************************************************************
/* Static Methods and Helpers
/****************************************************************************/
// EmailAddress.isValid("allen@example.com");
static isValid(emailAddressString, config={}) {
return (new EmailAddress(emailAddressString, config)).isValid();
}
static error(emailAddressString, config={}) {
return (new EmailAddress(emailAddressString, config)).error();
}
// EmailAddress.config({setting:value,...});
static config(object) {
if (!globalConfig) globalConfig = {};
globalConfig = Object.assign(globalConfig, object);
}
/****************************************************************************
/* Private and Internals
/****************************************************************************/
parse() {
var at = this.original.lastIndexOf("@");
if (at === -1) { // No @host
this.localString = this.original;
this.hostString = "";
// Or resolve.conf search parameter?
} else if (at === 0) { // No local
this.localString = "";
this.hostString = this.original.substr(1);
} else {
this.localString = this.original.substr(0, at);
this.hostString = this.original.substr(at + 1);
}
}
setFormat(f) {
let formatter;
if (f === 'conventional') {
formatter = require('./lib/conventional');
}
else if (f === 'standard') {
formatter = require('./lib/standard');
}
else {
throw "EmailAddress formatter " + f + " is not implemented";
}
return this.formatter = new formatter(this, this.config);
}
}
module.exports = EmailAddress;