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

34 lines
883 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 (!Object.prototype.hasOwnProperty.call(req.query, 'lzma')) {
2018-03-02 01:45:04 +08:00
return next();
}
2018-03-02 01:45:04 +08:00
// Decode (from base64)
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;
});
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));
}
});
};
};