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) {
|
2019-10-22 05:33:27 +08:00
|
|
|
if (!Object.prototype.hasOwnProperty.call(req.query, 'lzma')) {
|
2018-03-02 01:45:04 +08:00
|
|
|
return next();
|
|
|
|
}
|
2017-08-04 23:30:46 +08:00
|
|
|
|
2018-03-02 01:45:04 +08:00
|
|
|
// Decode (from base64)
|
2019-10-22 02:05:51 +08:00
|
|
|
var lzma = Buffer.from(req.query.lzma, 'base64')
|
2018-03-02 01:45:04 +08:00
|
|
|
.toString('binary')
|
|
|
|
.split('')
|
2019-10-22 01:07:24 +08:00
|
|
|
.map(function (c) {
|
2018-03-02 01:45:04 +08:00
|
|
|
return c.charCodeAt(0) - 128;
|
|
|
|
});
|
2017-08-04 23:30:46 +08:00
|
|
|
|
2018-03-02 01:45:04 +08:00
|
|
|
// Decompress
|
2019-10-22 01:07:24 +08:00
|
|
|
lzmaWorker.decompress(lzma, function (result) {
|
2018-03-02 01:45:04 +08:00
|
|
|
try {
|
|
|
|
delete req.query.lzma;
|
|
|
|
Object.assign(req.query, JSON.parse(result));
|
2018-03-02 02:09:11 +08:00
|
|
|
|
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
|
|
|
};
|