Skip to content

Commit 57af0be

Browse files
committed
Fix #5: Download new databases in dbs-tmp instead of overwriting directly
1 parent 869119e commit 57af0be

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ redist/*.mmdb
33

44
# Downloaded databases
55
dbs/
6+
dbs-tmp/
67

78
# Logs
89
logs

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ scripts/update-databases
1010
# ----
1111
# Downloaded databases
1212
dbs/
13+
dbs-tmp/
1314

1415
# Logs
1516
logs

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: node_js
22
node_js:
3-
- 10
43
- 12
4+
- 13
55
jobs:
66
- include:
77
- stage: update mirror

index.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const path = require('path');
2+
const fs = require('fs');
23
const {EventEmitter} = require('events');
34

45
const downloadHelper = require('./scripts/download-helper.js');
@@ -23,6 +24,11 @@ class UpdateSubscriber extends EventEmitter {
2324
}, updateTimer);
2425
this.checkUpdates();
2526

27+
// Clean up failed download files
28+
fs.rmdir(downloadPath+'-tmp', { recursive: true }, e => {
29+
// folder did not exist
30+
});
31+
2632
return this;
2733
}
2834

@@ -48,14 +54,23 @@ class UpdateSubscriber extends EventEmitter {
4854
this.downloading = true;
4955
this.emit('downloading');
5056

51-
downloadHelper.fetchDatabases(downloadPath).then(() => {
52-
return downloadHelper.verifyAllChecksums(downloadPath);
53-
}).then(() => {
54-
this.emit('update', downloadHelper.getEditions());
55-
}).catch(error => {
56-
console.warn('geolite2 self-update error:', error);
57-
}).finally(() => {
58-
this.downloading = false;
57+
fs.mkdir(downloadPath+'-tmp', () => {
58+
downloadHelper.fetchDatabases(downloadPath+'-tmp').then(() => {
59+
return downloadHelper.verifyAllChecksums(downloadPath+'-tmp');
60+
}).then(() => {
61+
fs.rmdir(downloadPath, { recursive: true }, e => {
62+
if (e) throw(e);
63+
fs.rename(downloadPath+'-tmp', downloadPath, e => {
64+
if (e) throw(e);
65+
this.emit('update', downloadHelper.getEditions());
66+
});
67+
});
68+
}).catch(error => {
69+
console.warn('geolite2 self-update error:', error);
70+
fs.rmdir(downloadPath+'-tmp', { recursive: true });
71+
}).finally(() => {
72+
this.downloading = false;
73+
});
5974
});
6075
}
6176

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"types": "index.d.ts",
77
"engines": {
8-
"node": ">= 10.18.1"
8+
"node": ">= 12.10.0"
99
},
1010
"keywords": [
1111
"maxmind",

0 commit comments

Comments
 (0)