wildduck/imap-core/lib/imap-composer.js

73 lines
2 KiB
JavaScript
Raw Normal View History

2017-03-06 05:45:50 +08:00
'use strict';
2017-06-03 14:51:58 +08:00
const imapHandler = require('./handler/imap-handler');
const Transform = require('stream').Transform;
2017-03-06 05:45:50 +08:00
class IMAPComposer extends Transform {
constructor(options) {
super();
Transform.call(this, {
writableObjectMode: true
});
2018-11-16 16:01:18 +08:00
options = options || {};
2017-03-06 05:45:50 +08:00
this.connection = options.connection;
2018-11-16 16:01:18 +08:00
this.skipFetchLog = options.skipFetchLog;
2017-03-06 05:45:50 +08:00
}
_transform(obj, encoding, done) {
if (!obj) {
return done();
}
if (typeof obj.pipe === 'function') {
// pipe stream to socket and wait until it finishes before continuing
2018-11-14 16:40:59 +08:00
2018-11-16 16:01:18 +08:00
if (!this.skipFetchLog) {
let description = [obj.description, obj._mailbox, obj._message, obj._uid].filter(v => v).join('/');
this.connection.logger.debug(
{
tnx: 'pipeout',
cid: this.connection.id
},
'[%s] S: <fetch response%s>',
this.connection.id,
description ? ' ' + description : ''
);
}
obj.pipe(this.connection[!this.connection.compression ? '_socket' : '_deflate'], {
end: false
});
2017-08-09 02:14:04 +08:00
obj.once('error', err => this.emit('error', err));
obj.once('end', () => {
2017-03-06 05:45:50 +08:00
this.push('\r\n');
done();
});
return;
}
let compiled = obj.compiled ? obj.compiled : imapHandler.compiler(obj);
2017-03-06 05:45:50 +08:00
2020-02-05 17:36:41 +08:00
if (!this.skipFetchLog || (!obj.compiled && this.skipFetchLog)) {
this.connection.logger.debug(
{
tnx: 'send',
cid: this.connection.id
},
'[%s] S:',
this.connection.id,
compiled
);
}
2017-05-16 18:57:04 +08:00
2018-05-11 19:39:23 +08:00
this.push(Buffer.from(compiled + '\r\n', 'binary'));
2017-03-06 05:45:50 +08:00
done();
}
_flush(done) {
done();
}
}
module.exports.IMAPComposer = IMAPComposer;