|
@@ -5,13 +5,12 @@
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
|
|
-var debug = require('debug')('ylt:weightChecker');
|
|
|
|
-
|
|
|
|
-var request = require('request');
|
|
|
|
-var http = require('http');
|
|
|
|
-var async = require('async');
|
|
|
|
|
|
+var debug = require('debug')('ylt:weightChecker');
|
|
var Q = require('q');
|
|
var Q = require('q');
|
|
|
|
+var http = require('http');
|
|
var zlib = require('zlib');
|
|
var zlib = require('zlib');
|
|
|
|
+var async = require('async');
|
|
|
|
+var request = require('request');
|
|
|
|
|
|
var WeightChecker = function() {
|
|
var WeightChecker = function() {
|
|
|
|
|
|
@@ -28,7 +27,18 @@ var WeightChecker = function() {
|
|
// Transform every request into a download function with a callback when done
|
|
// Transform every request into a download function with a callback when done
|
|
var redownloadList = requestsList.map(function(entry) {
|
|
var redownloadList = requestsList.map(function(entry) {
|
|
return function(callback) {
|
|
return function(callback) {
|
|
- redownloadEntry(entry, callback);
|
|
|
|
|
|
+
|
|
|
|
+ redownloadEntry(entry)
|
|
|
|
+
|
|
|
|
+ .then(recompressIfImage)
|
|
|
|
+
|
|
|
|
+ .then(function(entry) {
|
|
|
|
+ callback(null, entry);
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ .fail(function(err) {
|
|
|
|
+ callback(err);
|
|
|
|
+ });
|
|
};
|
|
};
|
|
});
|
|
});
|
|
|
|
|
|
@@ -118,26 +128,25 @@ var WeightChecker = function() {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- function redownloadEntry(entry, callback) {
|
|
|
|
|
|
+ function redownloadEntry(entry) {
|
|
|
|
+ var deferred = Q.defer();
|
|
|
|
|
|
function onError(message) {
|
|
function onError(message) {
|
|
debug('Could not download %s Error: %s', entry.url, message);
|
|
debug('Could not download %s Error: %s', entry.url, message);
|
|
entry.weightCheck = {
|
|
entry.weightCheck = {
|
|
message: message
|
|
message: message
|
|
};
|
|
};
|
|
- setImmediate(function() {
|
|
|
|
- callback(null, entry);
|
|
|
|
- });
|
|
|
|
|
|
+ deferred.reject();
|
|
}
|
|
}
|
|
|
|
|
|
if (entry.method !== 'GET') {
|
|
if (entry.method !== 'GET') {
|
|
onError('only downloading GET');
|
|
onError('only downloading GET');
|
|
- return;
|
|
|
|
|
|
+ return deferred.promise;
|
|
}
|
|
}
|
|
|
|
|
|
if (entry.status !== 200) {
|
|
if (entry.status !== 200) {
|
|
onError('only downloading requests with status code 200');
|
|
onError('only downloading requests with status code 200');
|
|
- return;
|
|
|
|
|
|
+ return deferred.promise;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -167,8 +176,10 @@ var WeightChecker = function() {
|
|
debug('%s downloaded correctly', entry.url);
|
|
debug('%s downloaded correctly', entry.url);
|
|
|
|
|
|
entry.weightCheck = result;
|
|
entry.weightCheck = result;
|
|
- callback(null, entry);
|
|
|
|
|
|
+ deferred.resolve(entry);
|
|
});
|
|
});
|
|
|
|
+
|
|
|
|
+ return deferred.promise;
|
|
}
|
|
}
|
|
|
|
|
|
// Inspired by https://github.com/cvan/fastHAR-api/blob/10cec585/app.js
|
|
// Inspired by https://github.com/cvan/fastHAR-api/blob/10cec585/app.js
|
|
@@ -268,11 +279,22 @@ var WeightChecker = function() {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+ function recompressIfImage(entry) {
|
|
|
|
+ var deferred = Q.defer();
|
|
|
|
+
|
|
|
|
+ deferred.resolve(entry);
|
|
|
|
+
|
|
|
|
+ return deferred.promise;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
return {
|
|
return {
|
|
recheckAllFiles: recheckAllFiles,
|
|
recheckAllFiles: recheckAllFiles,
|
|
listRequestWeight: listRequestWeight,
|
|
listRequestWeight: listRequestWeight,
|
|
redownloadEntry: redownloadEntry,
|
|
redownloadEntry: redownloadEntry,
|
|
- download: download
|
|
|
|
|
|
+ download: download,
|
|
|
|
+ recompressIfImage: recompressIfImage
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
|