wildduck/lib/forward.js
Andris Reinman ceb0934549 v1.0.27
2017-05-07 15:09:14 +03:00

37 lines
933 B
JavaScript

'use strict';
const config = require('config');
const maildrop = require('./maildrop');
module.exports = (options, callback) => {
if (!config.sender.enabled) {
return callback(null, false);
}
let message = maildrop({
from: options.sender,
to: options.recipient,
forward: options.forward,
http: !!options.targetUrl,
targeUrl: options.targetUrl,
interface: 'forwarder'
}, callback);
setImmediate(() => {
let pos = 0;
let writeNextChunk = () => {
if (pos >= options.chunks.length) {
return message.end();
}
let chunk = options.chunks[pos++];
if (!message.write(chunk)) {
return message.once('drain', writeNextChunk);
} else {
setImmediate(writeNextChunk);
}
};
setImmediate(writeNextChunk);
});
};