Skip to content

Commit

Permalink
refactor: get rid of util.promisify on async function
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Oct 22, 2023
1 parent 574f95f commit b1ff93f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/controllers/404.js
Expand Up @@ -9,7 +9,7 @@ const plugins = require('../plugins');
const middleware = require('../middleware');
const helpers = require('../middleware/helpers');

exports.handle404 = function handle404(req, res) {
exports.handle404 = helpers.try(async (req, res) => {
const relativePath = nconf.get('relative_path');
const isClientScript = new RegExp(`^${relativePath}\\/assets\\/src\\/.+\\.js(\\?v=\\w+)?$`);

Expand Down Expand Up @@ -38,13 +38,13 @@ exports.handle404 = function handle404(req, res) {
}

meta.errors.log404(req.path.replace(/^\/api/, '') || '');
exports.send404(req, res);
await exports.send404(req, res);
} else {
res.status(404).type('txt').send('Not found');
}
};
});

exports.send404 = async function (req, res) {
exports.send404 = helpers.try(async (req, res) => {
res.status(404);
const path = String(req.path || '');
if (res.locals.isAPI) {
Expand All @@ -66,4 +66,4 @@ exports.send404 = async function (req, res) {
bodyClass: helpers.buildBodyClass(req, res),
icon: icons[Math.floor(Math.random() * icons.length)],
});
};
});
16 changes: 10 additions & 6 deletions src/middleware/header.js
@@ -1,7 +1,5 @@
'use strict';

const util = require('util');

const user = require('../user');
const plugins = require('../plugins');
const helpers = require('./helpers');
Expand All @@ -13,6 +11,15 @@ const controllers = {
const middleware = module.exports;

middleware.buildHeader = helpers.try(async (req, res, next) => {
await doBuildHeader(req, res);
next();
});

middleware.buildHeaderAsync = async (req, res) => {
await doBuildHeader(req, res);
};

async function doBuildHeader(req, res) {
res.locals.renderHeader = true;
res.locals.isAPI = false;
if (req.method === 'GET') {
Expand All @@ -33,7 +40,4 @@ middleware.buildHeader = helpers.try(async (req, res, next) => {
}

res.locals.config = config;
next();
});

middleware.buildHeaderAsync = util.promisify(middleware.buildHeader);
}

0 comments on commit b1ff93f

Please sign in to comment.