-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
141 lines (123 loc) · 3.34 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
const https = require('https');
const crypto = require('crypto');
const url = require('url');
const API_URL = 'api4.temp-mail.org';
/**
* Generate MD5 hash by source
* @param {string} source
* @return {string}
*/
function md5(source) {
return crypto.createHash('md5').update(source).digest('hex');
}
/**
* Makes a GET request, expects application/json in response
* @param {string} path
* @return {Promise<string>}
*/
function getJSON(pathname) {
const path = url.resolve(pathname, 'format/json');
return new Promise((resolve, reject) => {
https
.get({ host: API_URL, path }, (res) => {
if (res.statusCode < 200 || res.statusCode > 299) {
return reject(new Error(`Request failed: ${res.statusCode}`));
}
let data = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => resolve(data));
res.on('error', reject);
})
.on('error', reject);
});
}
/**
* Returns a random array item
* @param {Array<T>} array
* @return {T}
*/
function getRandomArrayItem(array) {
return array[Math.round(Math.random() * (array.length - 1))];
}
/**
* Returns a random email
* @param {string[]} domains
* @return {string}
*/
function getRandomEmail(domains, len) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const randomChars = new Array(len).fill('').map(() => getRandomArrayItem(chars));
const domain = getRandomArrayItem(domains);
const name = randomChars.join('');
return name + domain;
}
/**
* Returns a domains list from temp-mail.org
* @return {Promise<string[]>}
*/
function getDomainsList() {
return getJSON('/request/domains/');
}
/**
* Returns a random email
* @param {number} [len]
* @return {string}
*/
function generateEmail(len = 7) {
return getDomainsList().then((domains) => getRandomEmail(domains, len));
}
/**
* Receives messages from temp-mail.org
* @param {string} email
* @return {Promise<Array<object> | object>}
*/
function getAllMessages(email) {
if (!email) {
throw new Error('Please specify email');
}
return getJSON(`/request/mail/id/${md5(email)}/`);
}
/**
* Returns a message object from temp-mail.org
* @param {string} mailId - unique message identifier (md5 hash)
* @return {object}
*/
function getMessage(mailId) {
if (!mailId) {
throw new Error('Please specify mailId');
}
return getJSON(`/request/one_mail/id/${mailId}/`);
}
/**
* Returns a message source from temp-mail.org
* @param {string} mailId - unique message identifier (md5 hash)
* @return {Array<string>}
*/
function getMessageSource(mailId) {
if (!mailId) {
throw new Error('Please specify mailId');
}
return getJSON(`/request/source/id/${mailId}/`);
}
/**
* Delete message from temp-mail.org
* @param {string} mailId - unique message identifier (md5 hash)
* @return {{success: true | false}}
*/
function deleteMessage(mailId) {
if (!mailId) {
throw new Error('Please specify mailId');
}
return getJSON(`/request/delete/id/${mailId}/`);
}
module.exports = {
deleteMessage,
getMessageSource,
getMessage,
getAllMessages,
generateEmail,
getDomainsList,
};