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