Skip to content

Commit

Permalink
perf: make downloadDbs try checksums before downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
GitSquared committed Dec 3, 2020
1 parent 24ed68b commit a1b1aee
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ class UpdateSubscriber extends EventEmitter {
try {
await downloadHelper.fetchChecksums();
return await downloadHelper.verifyAllChecksums(downloadPath);
}
catch (ex) {
this.update();
} catch (ex) {
await this.update();
};
}
finally {
Expand All @@ -67,29 +66,32 @@ class UpdateSubscriber extends EventEmitter {
}

update() {
if (this.downloading) {
return false;
}
return new Promise(resolve => {
if (this.downloading) {
resolve(false);
}

this.downloading = true;
this.emit('downloading');
this.downloading = true;
this.emit('downloading');

fs.mkdir(downloadPath+'-tmp', () => {
downloadHelper.fetchDatabases(downloadPath+'-tmp').then(() => {
return downloadHelper.verifyAllChecksums(downloadPath+'-tmp');
}).then(() => {
rimraf(downloadPath, e => {
if (e) throw(e);
fs.rename(downloadPath+'-tmp', downloadPath, e => {
fs.mkdir(downloadPath+'-tmp', () => {
downloadHelper.fetchDatabases(downloadPath+'-tmp').then(() => {
return downloadHelper.verifyAllChecksums(downloadPath+'-tmp');
}).then(() => {
rimraf(downloadPath, e => {
if (e) throw(e);
this.emit('update', downloadHelper.getEditions());
fs.rename(downloadPath+'-tmp', downloadPath, e => {
if (e) throw(e);
this.emit('update', downloadHelper.getEditions());
});
});
}).catch(error => {
console.warn('geolite2 self-update error:', error);
rimraf(downloadPath+'-tmp');
}).finally(() => {
this.downloading = false;
resolve()
});
}).catch(error => {
console.warn('geolite2 self-update error:', error);
rimraf(downloadPath+'-tmp');
}).finally(() => {
this.downloading = false;
});
});
}
Expand Down Expand Up @@ -190,6 +192,10 @@ function downloadDbs(newpath) {

return new Promise(resolve => {
const us = new UpdateSubscriber()
us.once('update', () => {
us.close()
resolve()
})
us.once('update', () => {
us.close()
resolve()
Expand Down

0 comments on commit a1b1aee

Please sign in to comment.