From 009e1d01f70a99dda7ed24d61f7f61a064639b3d Mon Sep 17 00:00:00 2001 From: John McLear Date: Sun, 7 Jun 2020 14:56:45 +0100 Subject: [PATCH] staleCode: Async update --- src/node/utils/caching_middleware.js | 58 ++++++++++++++++------------ src/package.json | 2 +- src/static/js/pluginfw/hooks.js | 24 ++++++------ 3 files changed, 47 insertions(+), 37 deletions(-) diff --git a/src/node/utils/caching_middleware.js b/src/node/utils/caching_middleware.js index bc387ca2c..aaef2506e 100644 --- a/src/node/utils/caching_middleware.js +++ b/src/node/utils/caching_middleware.js @@ -14,7 +14,6 @@ * limitations under the License. */ -var async = require('async'); var Buffer = require('buffer').Buffer; var fs = require('fs'); var path = require('path'); @@ -145,30 +144,39 @@ CachingMiddleware.prototype = new function () { res.write = function(data, encoding) { buffer += data.toString(encoding); }; - res.end = function(data, encoding) { - async.parallel([ - function (callback) { - var path = CACHE_DIR + 'minified_' + cacheKey; - fs.writeFile(path, buffer, function (error, stats) { - callback(); - }); - } - , function (callback) { - var path = CACHE_DIR + 'minified_' + cacheKey + '.gz'; - zlib.gzip(buffer, function(error, content) { - if (error) { - callback(); - } else { - fs.writeFile(path, content, function (error, stats) { - callback(); - }); - } - }); - } - ], function () { - responseCache[cacheKey] = {statusCode: status, headers: headers}; - respond(); - }); + res.end = async function(data, encoding) { + // Parallel. + Promise.all([compressMinified(), compressMinifiedGz()]) + + async function compressMinified() { + var path = CACHE_DIR + 'minified_' + cacheKey; + fs.writeFile(path, buffer, function (error, stats) { + if(error){ + return Promise.reject(error) + } + else{ + return Promise.resolve(stats) + } + }); + } + + async function compressMinifiedGz() { + var path = CACHE_DIR + 'minified_' + cacheKey + '.gz'; + zlib.gzip(buffer, function(error, content) { + if (error) { + return Promise.reject(error) + } else { + fs.writeFile(path, content, function (error, stats) { + if(error){ + return Promise.reject(error) + } + return Promise.resolve('ok') + }); + } + }); + } + responseCache[cacheKey] = {statusCode: status, headers: headers}; + respond(); }; } else if (status == 304) { // Nothing new changed from the cached version. diff --git a/src/package.json b/src/package.json index a459ea5ac..c749cc2a0 100644 --- a/src/package.json +++ b/src/package.json @@ -30,7 +30,7 @@ } ], "dependencies": { - "async": "0.9.0", + "async": "3.2.0", "async-stacktrace": "0.0.2", "channels": "0.0.4", "cheerio": "0.22.0", diff --git a/src/static/js/pluginfw/hooks.js b/src/static/js/pluginfw/hooks.js index 489709e33..4329fc9e1 100644 --- a/src/static/js/pluginfw/hooks.js +++ b/src/static/js/pluginfw/hooks.js @@ -1,4 +1,3 @@ -var async = require("async"); var _ = require("underscore"); exports.bubbleExceptions = true @@ -78,19 +77,22 @@ exports.callAll = function (hook_name, args) { } } -function aCallAll(hook_name, args, cb) { +async function aCallAll(hook_name, args, cb) { if (!args) args = {}; if (!cb) cb = function () {}; if (exports.plugins.hooks[hook_name] === undefined) return cb(null, []); - async.map( - exports.plugins.hooks[hook_name], - function (hook, cb) { - hookCallWrapper(hook, hook_name, args, function (res) { cb(null, res); }); - }, - function (err, res) { - cb(null, _.flatten(res, true)); - } - ); + + var newArray = []; + // This should be a map. + await exports.plugins.hooks[hook_name].forEach(async function(hook, index){ + let test = await hookCallWrapper(hook, hook_name, args, function (res) { + return Promise.resolve(res); + }); + newArray.push(test) + }); + + // after forEach + cb(null, _.flatten(newArray, true)); } /* return a Promise if cb is not supplied */