wildduck/lib/forward.js

41 lines
988 B
JavaScript
Raw Normal View History

2017-04-24 21:51:50 +08:00
'use strict';
2017-07-16 19:37:33 +08:00
const config = require('wild-config');
2017-04-25 03:59:38 +08:00
const maildrop = require('./maildrop');
2017-04-24 21:51:50 +08:00
module.exports = (options, callback) => {
2017-04-25 03:59:38 +08:00
if (!config.sender.enabled) {
2017-04-24 21:51:50 +08:00
return callback(null, false);
}
2017-06-03 14:51:58 +08:00
let message = maildrop(
{
from: options.sender,
to: options.recipient,
2017-05-07 20:09:14 +08:00
2017-06-03 14:51:58 +08:00
forward: options.forward,
http: !!options.targetUrl,
targeUrl: options.targetUrl,
2017-05-07 20:09:14 +08:00
2017-06-03 14:51:58 +08:00
interface: 'forwarder'
},
callback
);
2017-04-24 21:51:50 +08:00
2017-04-25 02:20:06 +08:00
setImmediate(() => {
let pos = 0;
let writeNextChunk = () => {
if (pos >= options.chunks.length) {
2017-04-25 03:59:38 +08:00
return message.end();
2017-04-25 02:20:06 +08:00
}
let chunk = options.chunks[pos++];
2017-04-25 03:59:38 +08:00
if (!message.write(chunk)) {
return message.once('drain', writeNextChunk);
2017-04-25 02:20:06 +08:00
} else {
setImmediate(writeNextChunk);
}
};
setImmediate(writeNextChunk);
});
};