2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2017-09-22 22:46:39 +08:00
|
|
|
const LZMA = require('lzma').LZMA;
|
2017-08-04 23:30:46 +08:00
|
|
|
|
2018-03-02 01:45:04 +08:00
|
|
|
module.exports = function lzma () {
|
|
|
|
const lzmaWorker = new LZMA();
|
2017-08-04 23:30:46 +08:00
|
|
|
|
2018-03-02 01:45:04 +08:00
|
|
|
return function lzmaMiddleware (req, res, next) {
|
|
|
|
if (!req.query.hasOwnProperty('lzma')) {
|
|
|
|
return next();
|
|
|
|
}
|
2017-08-04 23:30:46 +08:00
|
|
|
|
2018-03-02 01:45:04 +08:00
|
|
|
// Decode (from base64)
|
|
|
|
var lzma = new Buffer(req.query.lzma, 'base64')
|
|
|
|
.toString('binary')
|
|
|
|
.split('')
|
|
|
|
.map(function(c) {
|
|
|
|
return c.charCodeAt(0) - 128;
|
|
|
|
});
|
2017-08-04 23:30:46 +08:00
|
|
|
|
2018-03-02 01:45:04 +08:00
|
|
|
// Decompress
|
|
|
|
lzmaWorker.decompress(lzma, function(result) {
|
|
|
|
try {
|
|
|
|
delete req.query.lzma;
|
|
|
|
Object.assign(req.query, JSON.parse(result));
|
2018-03-02 02:09:11 +08:00
|
|
|
|
|
|
|
req.profiler.done('lzma');
|
|
|
|
|
2018-03-02 01:45:04 +08:00
|
|
|
next();
|
|
|
|
} catch (err) {
|
|
|
|
next(new Error('Error parsing lzma as JSON: ' + err));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2017-08-04 23:30:46 +08:00
|
|
|
};
|