Extract json-replacer

This commit is contained in:
Daniel García Aubert 2018-04-03 13:21:41 +02:00
parent cc61a89c68
commit a21d7db390
2 changed files with 39 additions and 52 deletions

View File

@ -1,22 +1,24 @@
const express = require('express');
const cors = require('./middleware/cors');
const user = require('./middleware/user');
const bodyParser = require('body-parser');
const _ = require('underscore'); const _ = require('underscore');
const express = require('express');
const StatsClient = require('./stats/client');
const stats = require('./middleware/stats');
const { mapnik } = require('windshaft'); const { mapnik } = require('windshaft');
const lzmaMiddleware = require('./middleware/lzma'); const jsonReplacer = require('./utils/json-replacer');
const errorMiddleware = require('./middleware/error-middleware');
const syntaxError = require('./middleware/syntax-error');
const servedByHostHeader = require('./middleware/served-by-host-header');
const logger = require('./middleware/logger'); const logger = require('./middleware/logger');
const bodyParser = require('body-parser');
const servedByHostHeader = require('./middleware/served-by-host-header');
const stats = require('./middleware/stats');
const lzmaMiddleware = require('./middleware/lzma');
const cors = require('./middleware/cors');
const user = require('./middleware/user');
const ControllersFactory = require('./controllers/factory'); const ControllersFactory = require('./controllers/factory');
const syntaxError = require('./middleware/syntax-error');
const errorMiddleware = require('./middleware/error-middleware');
const StatsClient = require('./stats/client');
module.exports = function createServer (serverOptions) { module.exports = function createServer (serverOptions) {
validateOptions(serverOptions); validateOptions(serverOptions);
@ -27,21 +29,21 @@ module.exports = function createServer (serverOptions) {
bootstrapFonts(serverOptions); bootstrapFonts(serverOptions);
const app = bootstrap(serverOptions); const app = express();
app.enable('jsonp callback');
app.disable('x-powered-by');
app.disable('etag');
app.set('json replacer', jsonReplacer());
app.use(logger(serverOptions)); app.use(logger(serverOptions));
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(servedByHostHeader()); app.use(servedByHostHeader());
app.use(stats({ app.use(stats({
enabled: serverOptions.useProfiler, enabled: serverOptions.useProfiler,
statsClient: global.statsClient statsClient: global.statsClient
})); }));
app.use(lzmaMiddleware()); app.use(lzmaMiddleware());
app.use(cors()); app.use(cors());
app.use(user()); app.use(user());
@ -73,40 +75,6 @@ function bootstrapFonts(opts) {
} }
} }
function bootstrap(opts) {
var app;
if (_.isObject(opts.https)) {
// use https if possible
app = express.createServer(opts.https);
} else {
// fall back to http by default
app = express();
}
app.enable('jsonp callback');
app.disable('x-powered-by');
app.disable('etag');
// Fix: https://github.com/CartoDB/Windshaft-cartodb/issues/705
// See: http://expressjs.com/en/4x/api.html#app.set
app.set('json replacer', function (key, value) {
if (value !== value) {
return 'NaN';
}
if (value === Infinity) {
return 'Infinity';
}
if (value === -Infinity) {
return '-Infinity';
}
return value;
});
return app;
}
function mapnikVersion(opts) { function mapnikVersion(opts) {
return opts.grainstore.mapnik_version || mapnik.versions.mapnik; return opts.grainstore.mapnik_version || mapnik.versions.mapnik;
} }

View File

@ -0,0 +1,19 @@
module.exports = function jsonReplacerFactory () {
// Fix: https://github.com/CartoDB/Windshaft-cartodb/issues/705
// See: http://expressjs.com/en/4x/api.html#app.set
return function jsonReplacer (key, value) {
if (value !== value) {
return 'NaN';
}
if (value === Infinity) {
return 'Infinity';
}
if (value === -Infinity) {
return '-Infinity';
}
return value;
};
};