Skip to content

Commit a1b1aee

Browse files
committed
perf: make downloadDbs try checksums before downloading
1 parent 24ed68b commit a1b1aee

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

index.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ class UpdateSubscriber extends EventEmitter {
4949
try {
5050
await downloadHelper.fetchChecksums();
5151
return await downloadHelper.verifyAllChecksums(downloadPath);
52-
}
53-
catch (ex) {
54-
this.update();
52+
} catch (ex) {
53+
await this.update();
5554
};
5655
}
5756
finally {
@@ -67,29 +66,32 @@ class UpdateSubscriber extends EventEmitter {
6766
}
6867

6968
update() {
70-
if (this.downloading) {
71-
return false;
72-
}
69+
return new Promise(resolve => {
70+
if (this.downloading) {
71+
resolve(false);
72+
}
7373

74-
this.downloading = true;
75-
this.emit('downloading');
74+
this.downloading = true;
75+
this.emit('downloading');
7676

77-
fs.mkdir(downloadPath+'-tmp', () => {
78-
downloadHelper.fetchDatabases(downloadPath+'-tmp').then(() => {
79-
return downloadHelper.verifyAllChecksums(downloadPath+'-tmp');
80-
}).then(() => {
81-
rimraf(downloadPath, e => {
82-
if (e) throw(e);
83-
fs.rename(downloadPath+'-tmp', downloadPath, e => {
77+
fs.mkdir(downloadPath+'-tmp', () => {
78+
downloadHelper.fetchDatabases(downloadPath+'-tmp').then(() => {
79+
return downloadHelper.verifyAllChecksums(downloadPath+'-tmp');
80+
}).then(() => {
81+
rimraf(downloadPath, e => {
8482
if (e) throw(e);
85-
this.emit('update', downloadHelper.getEditions());
83+
fs.rename(downloadPath+'-tmp', downloadPath, e => {
84+
if (e) throw(e);
85+
this.emit('update', downloadHelper.getEditions());
86+
});
8687
});
88+
}).catch(error => {
89+
console.warn('geolite2 self-update error:', error);
90+
rimraf(downloadPath+'-tmp');
91+
}).finally(() => {
92+
this.downloading = false;
93+
resolve()
8794
});
88-
}).catch(error => {
89-
console.warn('geolite2 self-update error:', error);
90-
rimraf(downloadPath+'-tmp');
91-
}).finally(() => {
92-
this.downloading = false;
9395
});
9496
});
9597
}
@@ -190,6 +192,10 @@ function downloadDbs(newpath) {
190192

191193
return new Promise(resolve => {
192194
const us = new UpdateSubscriber()
195+
us.once('update', () => {
196+
us.close()
197+
resolve()
198+
})
193199
us.once('update', () => {
194200
us.close()
195201
resolve()

0 commit comments

Comments
 (0)