]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/miscs/email.ts
move from trending routes to alg param
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / miscs / email.ts
CommitLineData
7c3b7976
C
1import { ChildProcess, fork } from 'child_process'
2import { randomInt } from '../../core-utils/miscs/miscs'
3import { parallelTests } from '../server/servers'
af37210c
JM
4
5class MockSmtpServer {
6
7 private static instance: MockSmtpServer
8 private started = false
89ada4e2 9 private emailChildProcess: ChildProcess
af37210c
JM
10 private emails: object[]
11
12 private constructor () {
89ada4e2
C
13 this.emailChildProcess = fork(`${__dirname}/email-child-process`, [])
14
d9699428 15 this.emailChildProcess.on('message', (msg: any) => {
af37210c
JM
16 if (msg.email) {
17 return this.emails.push(msg.email)
18 }
19 })
a4101923
C
20
21 process.on('exit', () => this.kill())
af37210c
JM
22 }
23
24 collectEmails (emailsCollection: object[]) {
7c3b7976
C
25 return new Promise<number>((res, rej) => {
26 const port = parallelTests() ? randomInt(1000, 2000) : 1025
27
af37210c
JM
28 if (this.started) {
29 this.emails = emailsCollection
30 return res()
31 }
32
33 // ensure maildev isn't started until
34 // unexpected exit can be reported to test runner
7c3b7976 35 this.emailChildProcess.send({ start: true, port })
af37210c
JM
36 this.emailChildProcess.on('exit', () => {
37 return rej(new Error('maildev exited unexpectedly, confirm port not in use'))
38 })
d9699428 39 this.emailChildProcess.on('message', (msg: any) => {
faa9d434
C
40 if (msg.err) return rej(new Error(msg.err))
41
af37210c
JM
42 this.started = true
43 this.emails = emailsCollection
faa9d434 44
7c3b7976 45 return res(port)
af37210c
JM
46 })
47 })
48 }
49
89ada4e2 50 kill () {
a4101923
C
51 if (!this.emailChildProcess) return
52
89ada4e2
C
53 process.kill(this.emailChildProcess.pid)
54
55 this.emailChildProcess = null
56 MockSmtpServer.instance = null
57 }
58
af37210c
JM
59 static get Instance () {
60 return this.instance || (this.instance = new this())
61 }
f076daa7
C
62}
63
64// ---------------------------------------------------------------------------
65
66export {
af37210c 67 MockSmtpServer
f076daa7 68}