staleCode: Async update

This commit is contained in:
John McLear 2020-06-07 14:56:45 +01:00 committed by GitHub
parent 0669280af0
commit 009e1d01f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 37 deletions

View file

@ -14,7 +14,6 @@
* limitations under the License. * limitations under the License.
*/ */
var async = require('async');
var Buffer = require('buffer').Buffer; var Buffer = require('buffer').Buffer;
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
@ -145,30 +144,39 @@ CachingMiddleware.prototype = new function () {
res.write = function(data, encoding) { res.write = function(data, encoding) {
buffer += data.toString(encoding); buffer += data.toString(encoding);
}; };
res.end = function(data, encoding) { res.end = async function(data, encoding) {
async.parallel([ // Parallel.
function (callback) { Promise.all([compressMinified(), compressMinifiedGz()])
var path = CACHE_DIR + 'minified_' + cacheKey;
fs.writeFile(path, buffer, function (error, stats) { async function compressMinified() {
callback(); var path = CACHE_DIR + 'minified_' + cacheKey;
}); fs.writeFile(path, buffer, function (error, stats) {
} if(error){
, function (callback) { return Promise.reject(error)
var path = CACHE_DIR + 'minified_' + cacheKey + '.gz'; }
zlib.gzip(buffer, function(error, content) { else{
if (error) { return Promise.resolve(stats)
callback(); }
} else { });
fs.writeFile(path, content, function (error, stats) { }
callback();
}); async function compressMinifiedGz() {
} var path = CACHE_DIR + 'minified_' + cacheKey + '.gz';
}); zlib.gzip(buffer, function(error, content) {
} if (error) {
], function () { return Promise.reject(error)
responseCache[cacheKey] = {statusCode: status, headers: headers}; } else {
respond(); 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) { } else if (status == 304) {
// Nothing new changed from the cached version. // Nothing new changed from the cached version.

View file

@ -30,7 +30,7 @@
} }
], ],
"dependencies": { "dependencies": {
"async": "0.9.0", "async": "3.2.0",
"async-stacktrace": "0.0.2", "async-stacktrace": "0.0.2",
"channels": "0.0.4", "channels": "0.0.4",
"cheerio": "0.22.0", "cheerio": "0.22.0",

View file

@ -1,4 +1,3 @@
var async = require("async");
var _ = require("underscore"); var _ = require("underscore");
exports.bubbleExceptions = true 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 (!args) args = {};
if (!cb) cb = function () {}; if (!cb) cb = function () {};
if (exports.plugins.hooks[hook_name] === undefined) return cb(null, []); if (exports.plugins.hooks[hook_name] === undefined) return cb(null, []);
async.map(
exports.plugins.hooks[hook_name], var newArray = [];
function (hook, cb) { // This should be a map.
hookCallWrapper(hook, hook_name, args, function (res) { cb(null, res); }); await exports.plugins.hooks[hook_name].forEach(async function(hook, index){
}, let test = await hookCallWrapper(hook, hook_name, args, function (res) {
function (err, res) { return Promise.resolve(res);
cb(null, _.flatten(res, true)); });
} newArray.push(test)
); });
// after forEach
cb(null, _.flatten(newArray, true));
} }
/* return a Promise if cb is not supplied */ /* return a Promise if cb is not supplied */