-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
79 lines (63 loc) · 2.13 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
const debug = require('debug')('10minutemail')
const EventEmitter = require('events').EventEmitter
const request = require('request').defaults({jar: true})
module.exports = create
function noop(){}
function create({timeout:timeout=10000, startup: startup=true}={}) {
let ev = new EventEmitter()
let mailTimer = 0
let countTimer = 0
ev.start = timeoutMail
ev.stop = () => {
clearTimeout(mailTimer)
clearTimeout(countTimer)
}
ev.reset = () => {
return reqCommon('https://10minutemail.com/10MinuteMail/resources/session/reset', 'reset')
}
ev.secondsLeft = () => {
return reqCommon('https://10minutemail.com/10MinuteMail/resources/session/secondsLeft', 'secondsLeft')
}
request('https://10minutemail.com/10MinuteMail/index.html', (err) => {
if (err) { debug('ERR', err) }
reqAddress()
.then((address) => {
debug(`address: ${address}`)
ev.address = address
ev.emit('address', address)
})
.then(startup ? timeoutMail : noop)
})
function timeoutMail() {
reqMail()
.then((body) => {
debug('mail body', body)
if (body.length) {
ev.emit('mail', body)
}
})
.then(() => mailTimer = setTimeout(reqMail, timeout))
.catch((err) => ev.emit('error', err))
reqCount()
.then((count) => {
debug('count body', count)
ev.emit('count', count)
})
.then(() => countTimer = setTimeout(reqCount, timeout))
.catch((err) => ev.emit('error', err))
}
return ev
}
//////////////////////////////////
let reqAddress = reqCommon.bind(null, 'https://10minutemail.com/10MinuteMail/resources/session/address', 'address')
let reqMail = reqCommon.bind(null, 'https://10minutemail.com/10MinuteMail/resources/messages/messagesAfter/0', 'mail')
let reqCount = reqCommon.bind(null, 'https://10minutemail.com/10MinuteMail/resources/messages/messageCount', 'count')
function reqCommon(url, type) {
debug(`requesting ${type}`)
return new Promise((res, rej) => {
request(url, (err, response, body) => {
if (err) {return rej(err)}
res(body)
})
})
}