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

36 lines
901 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('')
.map(function(c) {
return c.charCodeAt(0) - 128;
});
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));
}
});
};
};