Modernize the calculateMD5
test helper function
The test helper code largely predates the introduction of modern JavaScript features and should be refactored to improve readability. In particular callbacks and recursive function calls make the code harder to understand and maintain. This commit: - replaces the callback argument with returning a promise; - uses `const` instead of `var`; - uses arrow functions for shorter code; - uses template strings for shorter string formatting code; - improves the error messages to have more details.
This commit is contained in:
parent
5111b6d371
commit
c9a923066b
@ -110,23 +110,18 @@ function downloadManifestFiles(manifest, callback) {
|
|||||||
downloadNext();
|
downloadNext();
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateMD5(file, callback) {
|
function calculateMD5(file) {
|
||||||
var hash = crypto.createHash("md5");
|
return new Promise((resolve, reject) => {
|
||||||
var stream = fs.createReadStream(file);
|
const hash = crypto.createHash("md5");
|
||||||
stream.on("data", function (data) {
|
const stream = fs.createReadStream(file);
|
||||||
hash.update(data);
|
stream.on("data", data => hash.update(data));
|
||||||
});
|
stream.on("error", error => reject(error));
|
||||||
stream.on("error", function (err) {
|
stream.on("end", () => resolve(hash.digest("hex")));
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
stream.on("end", function () {
|
|
||||||
var result = hash.digest("hex");
|
|
||||||
callback(null, result);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function verifyManifestFiles(manifest, callback) {
|
function verifyManifestFiles(manifest, callback) {
|
||||||
function verifyNext() {
|
async function verifyNext() {
|
||||||
if (i >= manifest.length) {
|
if (i >= manifest.length) {
|
||||||
callback(error);
|
callback(error);
|
||||||
return;
|
return;
|
||||||
@ -150,35 +145,29 @@ function verifyManifestFiles(manifest, callback) {
|
|||||||
verifyNext();
|
verifyNext();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
calculateMD5(item.file, function (err, md5) {
|
|
||||||
if (err) {
|
try {
|
||||||
console.log('WARNING: Unable to open file for reading "' + err + '".');
|
const md5 = await calculateMD5(item.file);
|
||||||
error = true;
|
if (!item.md5) {
|
||||||
} else if (!item.md5) {
|
|
||||||
console.error(
|
console.error(
|
||||||
'WARNING: Missing md5 for file "' +
|
`WARNING: MD5 hash missing for "${item.file}" (computed "${md5}").`
|
||||||
item.file +
|
|
||||||
'". ' +
|
|
||||||
'Hash for current file is "' +
|
|
||||||
md5 +
|
|
||||||
'"'
|
|
||||||
);
|
);
|
||||||
error = true;
|
error = true;
|
||||||
} else if (md5 !== item.md5) {
|
} else if (md5 !== item.md5) {
|
||||||
console.error(
|
console.error(
|
||||||
'WARNING: MD5 of file "' +
|
`WARNING: MD5 hash mismatch for "${item.file}" (expected "${item.md5}", computed "${md5}").`
|
||||||
item.file +
|
|
||||||
'" does not match file. Expected "' +
|
|
||||||
item.md5 +
|
|
||||||
'" computed "' +
|
|
||||||
md5 +
|
|
||||||
'"'
|
|
||||||
);
|
);
|
||||||
error = true;
|
error = true;
|
||||||
}
|
}
|
||||||
i++;
|
} catch (ex) {
|
||||||
verifyNext();
|
console.log(
|
||||||
});
|
`WARNING: MD5 hash calculation failed for "${item.file}" ("${ex}").`
|
||||||
|
);
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
verifyNext();
|
||||||
}
|
}
|
||||||
var i = 0;
|
var i = 0;
|
||||||
var error = false;
|
var error = false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user