2026-09-15 13:00:32 +02:00
|
|
|
import * as packageData from '../package-info.js';
|
|
|
|
|
import * as shared from '../shared/index.js';
|
|
|
|
|
import LeWindows from '../mime-node/le-windows.js';
|
|
|
|
|
import LeUnix from '../mime-node/le-unix.js';
|
2020-02-29 23:01:03 +01:00
|
|
|
/**
|
|
|
|
|
* Generates a Transport object for streaming
|
|
|
|
|
*
|
|
|
|
|
* Possible options can be the following:
|
|
|
|
|
*
|
|
|
|
|
* * **buffer** if true, then returns the message as a Buffer object instead of a stream
|
|
|
|
|
* * **newline** either 'windows' or 'unix'
|
|
|
|
|
*
|
|
|
|
|
* @constructor
|
2026-09-15 13:00:32 +02:00
|
|
|
* @param optional config parameter
|
2020-02-29 23:01:03 +01:00
|
|
|
*/
|
|
|
|
|
class StreamTransport {
|
|
|
|
|
constructor(options) {
|
|
|
|
|
options = options || {};
|
2026-04-28 12:50:45 +02:00
|
|
|
this.options = options;
|
2020-02-29 23:01:03 +01:00
|
|
|
this.name = 'StreamTransport';
|
|
|
|
|
this.version = packageData.version;
|
|
|
|
|
this.logger = shared.getLogger(this.options, {
|
|
|
|
|
component: this.options.component || 'stream-transport'
|
|
|
|
|
});
|
|
|
|
|
this.winbreak = ['win', 'windows', 'dos', '\r\n'].includes((options.newline || '').toString().toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Compiles a mailcomposer message and forwards it to handler that sends it
|
|
|
|
|
*
|
2026-09-15 13:00:32 +02:00
|
|
|
* @param mail MailComposer object
|
|
|
|
|
* @param done Callback function to run when the sending is completed
|
2020-02-29 23:01:03 +01:00
|
|
|
*/
|
|
|
|
|
send(mail, done) {
|
2026-09-15 13:00:32 +02:00
|
|
|
// We probably need this in the output. send() runs after the message was compiled,
|
|
|
|
|
// so mail.message is set
|
2020-02-29 23:01:03 +01:00
|
|
|
mail.message.keepBcc = true;
|
2026-09-07 07:05:39 +02:00
|
|
|
const envelope = mail.message.getEnvelope();
|
2026-04-28 12:50:45 +02:00
|
|
|
const messageId = mail.message.messageId();
|
|
|
|
|
const recipients = [].concat(envelope.to || []);
|
2020-02-29 23:01:03 +01:00
|
|
|
if (recipients.length > 3) {
|
|
|
|
|
recipients.push('...and ' + recipients.splice(2).length + ' more');
|
|
|
|
|
}
|
2026-09-15 13:00:32 +02:00
|
|
|
this.logger.info({
|
|
|
|
|
tnx: 'send',
|
|
|
|
|
messageId
|
|
|
|
|
}, 'Sending message %s to <%s> using %s line breaks', messageId, recipients.join(', '), this.winbreak ? '<CR><LF>' : '<LF>');
|
2020-02-29 23:01:03 +01:00
|
|
|
setImmediate(() => {
|
|
|
|
|
let stream;
|
|
|
|
|
try {
|
2021-07-14 08:08:23 +02:00
|
|
|
stream = mail.message.createReadStream();
|
2026-06-15 07:32:52 +02:00
|
|
|
if (this.options.newline) {
|
|
|
|
|
// apply the transport-level line ending transform; the message-level
|
|
|
|
|
// `newline` option is handled by MimeNode in createReadStream()
|
|
|
|
|
const sourceStream = stream;
|
|
|
|
|
stream = sourceStream.pipe(this.winbreak ? new LeWindows() : new LeUnix());
|
|
|
|
|
sourceStream.once('error', err => stream.emit('error', err));
|
|
|
|
|
}
|
2026-09-15 13:00:32 +02:00
|
|
|
}
|
|
|
|
|
catch (E) {
|
|
|
|
|
this.logger.error({
|
|
|
|
|
err: E,
|
|
|
|
|
tnx: 'send',
|
|
|
|
|
messageId
|
|
|
|
|
}, 'Creating send stream failed for %s. %s', messageId, E.message);
|
2020-02-29 23:01:03 +01:00
|
|
|
return done(E);
|
|
|
|
|
}
|
|
|
|
|
if (!this.options.buffer) {
|
|
|
|
|
stream.once('error', err => {
|
2026-09-15 13:00:32 +02:00
|
|
|
this.logger.error({
|
|
|
|
|
err,
|
|
|
|
|
tnx: 'send',
|
|
|
|
|
messageId
|
|
|
|
|
}, 'Failed creating message for %s. %s', messageId, err.message);
|
2020-02-29 23:01:03 +01:00
|
|
|
});
|
|
|
|
|
return done(null, {
|
2026-04-28 12:50:45 +02:00
|
|
|
envelope,
|
2020-02-29 23:01:03 +01:00
|
|
|
messageId,
|
|
|
|
|
message: stream
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-04-28 12:50:45 +02:00
|
|
|
const chunks = [];
|
2020-02-29 23:01:03 +01:00
|
|
|
let chunklen = 0;
|
|
|
|
|
stream.on('readable', () => {
|
|
|
|
|
let chunk;
|
|
|
|
|
while ((chunk = stream.read()) !== null) {
|
|
|
|
|
chunks.push(chunk);
|
|
|
|
|
chunklen += chunk.length;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
stream.once('error', err => {
|
2026-09-15 13:00:32 +02:00
|
|
|
this.logger.error({
|
|
|
|
|
err,
|
|
|
|
|
tnx: 'send',
|
|
|
|
|
messageId
|
|
|
|
|
}, 'Failed creating message for %s. %s', messageId, err.message);
|
2020-02-29 23:01:03 +01:00
|
|
|
return done(err);
|
|
|
|
|
});
|
2026-09-15 13:00:32 +02:00
|
|
|
stream.on('end', () => done(null, {
|
|
|
|
|
envelope,
|
|
|
|
|
messageId,
|
|
|
|
|
message: Buffer.concat(chunks, chunklen)
|
|
|
|
|
}));
|
2020-02-29 23:01:03 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-15 13:00:32 +02:00
|
|
|
export default StreamTransport;
|