]>
Commit | Line | Data |
---|---|---|
5bdfa604 C |
1 | import MailDev from '@peertube/maildev' |
2 | ||
3 | class MockSMTPServer { | |
4 | ||
5 | private static instance: MockSMTPServer | |
6 | private started = false | |
5b94394a | 7 | private maildev: any |
5bdfa604 C |
8 | private emails: object[] |
9 | ||
10 | collectEmails (port: number, emailsCollection: object[]) { | |
11 | return new Promise<number>((res, rej) => { | |
12 | this.emails = emailsCollection | |
13 | ||
14 | if (this.started) { | |
15 | return res(undefined) | |
16 | } | |
17 | ||
5b94394a | 18 | this.maildev = new MailDev({ |
5bdfa604 C |
19 | ip: '127.0.0.1', |
20 | smtp: port, | |
21 | disableWeb: true, | |
22 | silent: true | |
23 | }) | |
24 | ||
5b94394a | 25 | this.maildev.on('new', email => { |
5bdfa604 C |
26 | this.emails.push(email) |
27 | }) | |
28 | ||
5b94394a | 29 | this.maildev.listen(err => { |
5bdfa604 C |
30 | if (err) return rej(err) |
31 | ||
32 | this.started = true | |
33 | ||
34 | return res(port) | |
35 | }) | |
36 | }) | |
37 | } | |
38 | ||
39 | kill () { | |
5b94394a | 40 | if (!this.maildev) return |
5bdfa604 | 41 | |
5b94394a | 42 | this.maildev.close() |
5bdfa604 | 43 | |
5b94394a | 44 | this.maildev = null |
5bdfa604 C |
45 | MockSMTPServer.instance = null |
46 | } | |
47 | ||
48 | static get Instance () { | |
49 | return this.instance || (this.instance = new this()) | |
50 | } | |
51 | } | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
55 | export { | |
56 | MockSMTPServer | |
57 | } |