-
Notifications
You must be signed in to change notification settings - Fork 5
/
stubTransport.js
50 lines (42 loc) · 1.26 KB
/
stubTransport.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
const EventEmitter = require('events')
const emitter = new EventEmitter()
const iwm = require('./interactsWithMail')
/**
* Custom LimeDeck Stub transport for NodeMailer, used for testing ONLY.
*
* @type {{name: string, version: string, send: Function}}
*/
module.exports = {
name: 'LDStub',
version: '1.0.0',
send: function (mail, callback) {
let envelope = mail.data.envelope || mail.message.getEnvelope()
let chunks = []
let chunkLength = 0
emitter.emit('envelope', envelope)
let input = mail.message.createReadStream()
input.on('data', chunk => {
chunks.push(chunk)
chunkLength += chunk.length
emitter.emit('data', chunk.toString())
})
input.on('end', () => {
setImmediate(() => {
let messageId = (mail.message.getHeader('message-id')).replace(/[<>\s]/g, '')
let response = Buffer.concat(chunks, chunkLength)
let info = {
messageId,
response,
from: envelope.from,
to: envelope.to,
content: mail.message.content,
contentType: mail.message.contentType,
subject: mail.message.getHeader('subject') || ''
}
iwm.newMail(info)
emitter.emit('end', info)
callback(null, info)
})
})
}
}