From cd413e15435947aa855e27aab16d9cd9e65ad493 Mon Sep 17 00:00:00 2001 From: Leonard Martin Date: Thu, 10 Mar 2016 11:37:21 +0000 Subject: [PATCH] Force async-ness in template rendering methods There is a long-standing open bug in consolidate - https://github.com/tj/consolidate.js/issues/143 - which causes errors in the callback functions passed to rendering methods to propogate back into the try/catch contained in consolidate and cause the callback to be fired twice. Since this bug has been open in consolidate for over two years, not much hope should be held of having it resolved there. As such, add protection in Sails against errors in the callback functions being caught inside consolidate and the callback fired twice by forcing the callback to be called asynchronously. --- lib/hooks/views/configure.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/hooks/views/configure.js b/lib/hooks/views/configure.js index 69cb5d3f1..8f23a73a1 100644 --- a/lib/hooks/views/configure.js +++ b/lib/hooks/views/configure.js @@ -73,7 +73,10 @@ module.exports = function configure ( sails ) { } // Save reference to view rendering function - sails.config.views.engine.fn = fn; + // munge callback to protect against https://github.com/tj/consolidate.js/issues/143 + sails.config.views.engine.fn = function (str, options, cb) { + fn(str, options, function (e, result) { setImmediate(function () { cb(e, result); }); }); + } sails.log.silly('Configured view engine, `' + engineName + '`'); }