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

64 lines
1.6 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
});
this.connection = options.connection;
}
_transform(obj, encoding, done) {
if (!obj) {
return done();
}
if (typeof obj.pipe === 'function') {
// pipe stream to socket and wait until it finishes before continuing
this.connection.logger.debug(
2017-06-03 14:51:58 +08:00
{
tnx: 'pipeout',
cid: this.connection.id
},
'[%s] S: %s<pipe message stream to socket>',
this.connection.id,
obj.description || ''
);
2017-04-04 22:09:39 +08:00
obj.pipe(this.connection[!this.connection.compression ? '_socket' : '_deflate'], {
2017-03-06 05:45:50 +08:00
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 = imapHandler.compiler(obj);
this.connection.logger.debug(
2017-06-03 14:51:58 +08:00
{
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;