mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-27 17:16:14 +01:00
refactor: async/await middleware
This commit is contained in:
@@ -1,118 +1,89 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var nconf = require('nconf');
|
||||
var validator = require('validator');
|
||||
var winston = require('winston');
|
||||
const util = require('util');
|
||||
const nconf = require('nconf');
|
||||
const validator = require('validator');
|
||||
const winston = require('winston');
|
||||
|
||||
var plugins = require('../plugins');
|
||||
var meta = require('../meta');
|
||||
var translator = require('../translator');
|
||||
var widgets = require('../widgets');
|
||||
var utils = require('../utils');
|
||||
const plugins = require('../plugins');
|
||||
const meta = require('../meta');
|
||||
const translator = require('../translator');
|
||||
const widgets = require('../widgets');
|
||||
const utils = require('../utils');
|
||||
|
||||
module.exports = function (middleware) {
|
||||
const renderHeaderFooterAsync = util.promisify(renderHeaderFooter);
|
||||
|
||||
middleware.processRender = function processRender(req, res, next) {
|
||||
// res.render post-processing, modified from here: https://gist.github.com/mrlannigan/5051687
|
||||
var render = res.render;
|
||||
res.render = function renderOverride(template, options, fn) {
|
||||
var self = this;
|
||||
var req = this.req;
|
||||
var defaultFn = function (err, str) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
self.send(str);
|
||||
};
|
||||
const render = res.render;
|
||||
res.render = async function renderOverride(template, options, fn) {
|
||||
const self = this;
|
||||
const req = this.req;
|
||||
|
||||
options = options || {};
|
||||
if (typeof options === 'function') {
|
||||
fn = options;
|
||||
options = {};
|
||||
}
|
||||
if (typeof fn !== 'function') {
|
||||
fn = defaultFn;
|
||||
|
||||
options.loggedIn = req.uid > 0;
|
||||
options.relative_path = nconf.get('relative_path');
|
||||
options.template = { name: template, [template]: true };
|
||||
options.url = (req.baseUrl + req.path.replace(/^\/api/, ''));
|
||||
options.bodyClass = buildBodyClass(req, res, options);
|
||||
|
||||
const buildResult = await plugins.fireHook('filter:' + template + '.build', { req: req, res: res, templateData: options });
|
||||
const templateToRender = buildResult.templateData.templateToRender || template;
|
||||
|
||||
const renderResult = await plugins.fireHook('filter:middleware.render', { req: req, res: res, templateData: buildResult.templateData });
|
||||
options = renderResult.templateData;
|
||||
options._header = {
|
||||
tags: await meta.tags.parse(req, renderResult, res.locals.metaTags, res.locals.linkTags),
|
||||
};
|
||||
options.widgets = await widgets.render(req.uid, {
|
||||
template: template + '.tpl',
|
||||
url: options.url,
|
||||
templateData: options,
|
||||
req: req,
|
||||
res: res,
|
||||
});
|
||||
res.locals.template = template;
|
||||
options._locals = undefined;
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
if (req.route && req.route.path === '/api/') {
|
||||
options.title = '[[pages:home]]';
|
||||
}
|
||||
req.app.set('json spaces', global.env === 'development' || req.query.pretty ? 4 : 0);
|
||||
return res.json(options);
|
||||
}
|
||||
const ajaxifyData = JSON.stringify(options).replace(/<\//g, '<\\/');
|
||||
|
||||
var ajaxifyData;
|
||||
var templateToRender;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
options.loggedIn = req.uid > 0;
|
||||
options.relative_path = nconf.get('relative_path');
|
||||
options.template = { name: template };
|
||||
options.template[template] = true;
|
||||
options.url = (req.baseUrl + req.path.replace(/^\/api/, ''));
|
||||
options.bodyClass = buildBodyClass(req, res, options);
|
||||
plugins.fireHook('filter:' + template + '.build', { req: req, res: res, templateData: options }, next);
|
||||
},
|
||||
function (data, next) {
|
||||
templateToRender = data.templateData.templateToRender || template;
|
||||
plugins.fireHook('filter:middleware.render', { req: req, res: res, templateData: data.templateData }, next);
|
||||
},
|
||||
function parseTags(data, next) {
|
||||
meta.tags.parse(req, data, res.locals.metaTags, res.locals.linkTags, function (err, tags) {
|
||||
options._header = {
|
||||
tags: tags,
|
||||
};
|
||||
next(err, data);
|
||||
});
|
||||
},
|
||||
function (data, next) {
|
||||
options = data.templateData;
|
||||
const renderAsync = util.promisify((templateToRender, options, next) => render.call(self, templateToRender, options, next));
|
||||
|
||||
widgets.render(req.uid, {
|
||||
template: template + '.tpl',
|
||||
url: options.url,
|
||||
templateData: options,
|
||||
req: req,
|
||||
res: res,
|
||||
}, next);
|
||||
},
|
||||
function (data, next) {
|
||||
options.widgets = data;
|
||||
const results = await utils.promiseParallel({
|
||||
header: renderHeaderFooterAsync('renderHeader', req, res, options),
|
||||
content: renderAsync(templateToRender, options),
|
||||
footer: renderHeaderFooterAsync('renderFooter', req, res, options),
|
||||
});
|
||||
|
||||
res.locals.template = template;
|
||||
options._locals = undefined;
|
||||
const str = results.header +
|
||||
(res.locals.postHeader || '') +
|
||||
results.content + '<script id="ajaxify-data"></script>' +
|
||||
(res.locals.preFooter || '') +
|
||||
results.footer;
|
||||
|
||||
if (res.locals.isAPI) {
|
||||
if (req.route && req.route.path === '/api/') {
|
||||
options.title = '[[pages:home]]';
|
||||
}
|
||||
req.app.set('json spaces', global.env === 'development' || req.query.pretty ? 4 : 0);
|
||||
return res.json(options);
|
||||
}
|
||||
let translated = await translate(str, req, res);
|
||||
translated = translated.replace('<script id="ajaxify-data"></script>', function () {
|
||||
return '<script id="ajaxify-data" type="application/json">' + ajaxifyData + '</script>';
|
||||
});
|
||||
|
||||
ajaxifyData = JSON.stringify(options).replace(/<\//g, '<\\/');
|
||||
|
||||
async.parallel({
|
||||
header: function (next) {
|
||||
renderHeaderFooter('renderHeader', req, res, options, next);
|
||||
},
|
||||
content: function (next) {
|
||||
render.call(self, templateToRender, options, next);
|
||||
},
|
||||
footer: function (next) {
|
||||
renderHeaderFooter('renderFooter', req, res, options, next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
var str = results.header +
|
||||
(res.locals.postHeader || '') +
|
||||
results.content + '<script id="ajaxify-data"></script>' +
|
||||
(res.locals.preFooter || '') +
|
||||
results.footer;
|
||||
|
||||
translate(str, req, res, next);
|
||||
},
|
||||
function (translated, next) {
|
||||
translated = translated.replace('<script id="ajaxify-data"></script>', function () {
|
||||
return '<script id="ajaxify-data" type="application/json">' + ajaxifyData + '</script>';
|
||||
});
|
||||
next(null, translated);
|
||||
},
|
||||
], fn);
|
||||
if (typeof fn !== 'function') {
|
||||
self.send(translated);
|
||||
} else {
|
||||
fn(null, translated);
|
||||
}
|
||||
};
|
||||
|
||||
next();
|
||||
@@ -128,20 +99,19 @@ module.exports = function (middleware) {
|
||||
}
|
||||
}
|
||||
|
||||
function translate(str, req, res, next) {
|
||||
var language = (res.locals.config && res.locals.config.userLang) || 'en-GB';
|
||||
async function translate(str, req, res) {
|
||||
let language = (res.locals.config && res.locals.config.userLang) || 'en-GB';
|
||||
if (res.locals.renderAdminHeader) {
|
||||
language = (res.locals.config && res.locals.config.acpLang) || 'en-GB';
|
||||
}
|
||||
language = req.query.lang ? validator.escape(String(req.query.lang)) : language;
|
||||
translator.translate(str, language, function (translated) {
|
||||
next(null, translator.unescape(translated));
|
||||
});
|
||||
const translated = await translator.translate(str, language);
|
||||
return translator.unescape(translated);
|
||||
}
|
||||
|
||||
function buildBodyClass(req, res, templateData) {
|
||||
var clean = req.path.replace(/^\/api/, '').replace(/^\/|\/$/g, '');
|
||||
var parts = clean.split('/').slice(0, 3);
|
||||
const clean = req.path.replace(/^\/api/, '').replace(/^\/|\/$/g, '');
|
||||
const parts = clean.split('/').slice(0, 3);
|
||||
parts.forEach(function (p, index) {
|
||||
try {
|
||||
p = utils.slugify(decodeURIComponent(p));
|
||||
|
||||
Reference in New Issue
Block a user