Windshaft-cartodb/lib/api/middlewares/lzma.js

36 lines
903 B
JavaScript
Raw Normal View History

'use strict';
const LZMA = require('lzma').LZMA;
2018-03-02 01:45:04 +08:00
module.exports = function lzma () {
const lzmaWorker = new LZMA();
2018-03-02 01:45:04 +08:00
return function lzmaMiddleware (req, res, next) {
if (!req.query.hasOwnProperty('lzma')) {
return next();
}
2018-03-02 01:45:04 +08:00
// Decode (from base64)
var lzma = new Buffer(req.query.lzma, 'base64')
.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;
});
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
req.profiler.done('lzma');
2018-03-02 01:45:04 +08:00
next();
} catch (err) {
next(new Error('Error parsing lzma as JSON: ' + err));
}
});
};
};