wildduck/imap-core/lib/commands/compress.js

76 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-04-04 21:35:56 +08:00
'use strict';
const zlib = require('zlib');
// tag COMPRESS DEFLATE
module.exports = {
state: ['Authenticated', 'Selected'],
schema: [{
name: 'mechanism',
type: 'string'
}],
handler(command, callback) {
let mechanism = (command.attributes[0] && command.attributes[0].value || '').toString().toUpperCase().trim();
if (!mechanism) {
return callback(null, {
response: 'BAD'
});
}
if (mechanism !== 'DEFLATE') {
return callback(null, {
response: 'BAD',
code: 'CANNOT',
message: 'Unsupported compression mechanism'
});
}
setImmediate(() => {
this.compression = true;
2017-04-04 22:09:39 +08:00
this._deflate = zlib.createDeflateRaw({
windowBits: 15
});
2017-04-04 21:38:44 +08:00
this._inflate = zlib.createInflateRaw();
2017-04-04 21:35:56 +08:00
this._deflate.once('error', err => {
2017-04-04 22:09:39 +08:00
this._server.logger.debug('[%s] Deflate error %s', this.id, err.message);
this.close();
2017-04-04 21:35:56 +08:00
});
2017-04-04 22:09:39 +08:00
this._inflate.once('error', err => {
this._server.logger.debug('[%s] Inflate error %s', this.id, err.message);
this.close();
});
2017-04-04 21:35:56 +08:00
this.writeStream.unpipe(this._socket);
2017-04-04 22:09:39 +08:00
this._deflate.pipe(this._socket);
let readNext = () => {
let chunk;
while ((chunk = this.writeStream.read()) !== null) {
if (this._deflate && this._deflate.write(chunk) === false) {
2017-04-04 22:09:39 +08:00
return this._deflate.once('drain', readNext);
}
}
// flush data to socket
if (this._deflate) {
this._deflate.flush();
}
2017-04-04 22:09:39 +08:00
};
this.writeStream.on('readable', readNext);
2017-04-04 21:35:56 +08:00
this._socket.unpipe(this._parser);
this._socket.pipe(this._inflate).pipe(this._parser);
});
callback(null, {
response: 'OK',
message: 'DEFLATE active'
});
}
};