refactor: remove async.each/reduce from hooks for better stack traces

v1.18.x
Barış Soner Uşaklı 4 years ago
parent 0d3979efd0
commit d05d7091ae

@ -1,8 +1,7 @@
'use strict'; 'use strict';
const util = require('util');
const winston = require('winston'); const winston = require('winston');
const async = require('async');
const utils = require('../utils');
const plugins = require('.'); const plugins = require('.');
const Hooks = module.exports; const Hooks = module.exports;
@ -116,21 +115,21 @@ async function fireFilterHook(hook, hookList, params) {
return params; return params;
} }
return await async.reduce(hookList, params, (params, hookObj, next) => { for (const hookObj of hookList) {
if (typeof hookObj.method !== 'function') { if (typeof hookObj.method !== 'function') {
if (global.env === 'development') { if (global.env === 'development') {
winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`); winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
} }
return next(null, params); } else {
} let hookFn = hookObj.method;
const returned = hookObj.method(params, next); if (hookFn.constructor && hookFn.constructor.name !== 'AsyncFunction') {
if (utils.isPromise(returned)) { hookFn = util.promisify(hookFn);
returned.then( }
payload => setImmediate(next, null, payload), // eslint-disable-next-line
err => setImmediate(next, err) params = await hookFn(params);
);
} }
}); }
return params;
} }
async function fireActionHook(hook, hookList, params) { async function fireActionHook(hook, hookList, params) {
@ -143,7 +142,7 @@ async function fireActionHook(hook, hookList, params) {
winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`); winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
} }
} else { } else {
/* eslint-disable no-await-in-loop */ // eslint-disable-next-line
await hookObj.method(params); await hookObj.method(params);
} }
} }
@ -155,59 +154,46 @@ async function fireStaticHook(hook, hookList, params) {
} }
// don't bubble errors from these hooks, so bad plugins don't stop startup // don't bubble errors from these hooks, so bad plugins don't stop startup
const noErrorHooks = ['static:app.load', 'static:assets.prepare', 'static:app.preload']; const noErrorHooks = ['static:app.load', 'static:assets.prepare', 'static:app.preload'];
await async.each(hookList, (hookObj, next) => {
if (typeof hookObj.method !== 'function') {
return next();
}
let timedOut = false; for (const hookObj of hookList) {
const timeoutId = setTimeout(() => { if (typeof hookObj.method !== 'function') {
winston.warn(`[plugins] Callback timed out, hook '${hook}' in plugin '${hookObj.id}'`); if (global.env === 'development') {
timedOut = true; winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
next();
}, 5000);
const callback = (err) => {
clearTimeout(timeoutId);
if (err) {
winston.error(`[plugins] Error executing '${hook}' in plugin '${hookObj.id}'`);
winston.error(err.stack);
} }
if (!timedOut) { } else {
next(noErrorHooks.includes(hook) ? null : err); let hookFn = hookObj.method;
if (hookFn.constructor && hookFn.constructor.name !== 'AsyncFunction') {
hookFn = util.promisify(hookFn);
} }
}; try {
try { // eslint-disable-next-line
const returned = hookObj.method(params, callback); await hookFn(params);
if (utils.isPromise(returned)) { } catch (err) {
returned.then( winston.error(`[plugins] Error executing '${hook}' in plugin '${hookObj.id}'\n${err.stack}`);
payload => setImmediate(callback, null, payload), if (!noErrorHooks.includes(hook)) {
err => setImmediate(callback, err) throw err;
); }
} }
} catch (err) {
callback(err);
} }
}); }
} }
async function fireResponseHook(hook, hookList, params) { async function fireResponseHook(hook, hookList, params) {
if (!Array.isArray(hookList) || !hookList.length) { if (!Array.isArray(hookList) || !hookList.length) {
return; return;
} }
await async.eachSeries(hookList, async (hookObj) => { for (const hookObj of hookList) {
if (typeof hookObj.method !== 'function') { if (typeof hookObj.method !== 'function') {
if (global.env === 'development') { if (global.env === 'development') {
winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`); winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
} }
return; } else {
} // Skip remaining hooks if headers have been sent
if (params.res.headersSent) {
// Skip remaining hooks if headers have been sent return;
if (params.res.headersSent) { }
return; // eslint-disable-next-line
await hookObj.method(params);
} }
}
await hookObj.method(params);
});
} }

Loading…
Cancel
Save