@@ -19,6 +19,7 @@ class UpdateSubscriber extends EventEmitter {
1919 super ( ) ;
2020
2121 this . downloading = false ;
22+ this . checking = false ;
2223
2324 this . _checker = setInterval ( ( ) => {
2425 this . checkUpdates ( ) ;
@@ -34,17 +35,35 @@ class UpdateSubscriber extends EventEmitter {
3435 }
3536
3637 async checkUpdates ( ) {
37- // Leave time for listeners to be up
38- await this . _wait ( 200 ) ;
39-
40- this . emit ( 'checking' ) ;
41- downloadHelper . fetchChecksums ( ) . then ( ( ) => {
42- return downloadHelper . verifyAllChecksums ( downloadPath ) ;
43- } ) . then ( ( ) => {
44- // All checksums match, no update
45- } ) . catch ( ( ) => {
46- this . update ( ) ;
47- } ) ;
38+ if ( this . checking ) {
39+ return ;
40+ }
41+
42+ this . checking = true ;
43+
44+ try {
45+ // Leave time for listeners to be up
46+ await this . _wait ( 200 ) ;
47+
48+ this . emit ( 'checking' ) ;
49+ try {
50+ await downloadHelper . fetchChecksums ( ) ;
51+ return await downloadHelper . verifyAllChecksums ( downloadPath ) ;
52+ }
53+ catch ( ex ) {
54+ this . update ( ) ;
55+ } ;
56+ }
57+ finally {
58+ this . checking = false ;
59+ this . emit ( 'done checking' ) ;
60+ }
61+ }
62+
63+ // Backward compat
64+ triggerUpdate ( ) {
65+ console . warn ( 'geolite2-redist: triggerUpdate() is deprecated' ) ;
66+ return Promise . resolve ( ) ;
4867 }
4968
5069 update ( ) {
@@ -75,18 +94,6 @@ class UpdateSubscriber extends EventEmitter {
7594 } ) ;
7695 }
7796
78- async triggerUpdate ( ) {
79- await this . _wait ( 100 ) ;
80-
81- downloadHelper . fetchChecksums ( ) . then ( ( ) => {
82- return downloadHelper . verifyAllChecksums ( downloadPath ) ;
83- } ) . then ( ( ) => {
84- this . update ( ) ;
85- } ) . catch ( error => {
86- console . warn ( 'geolite2 self-update error:' , error ) ;
87- } ) ;
88- }
89-
9097 close ( ) {
9198 clearInterval ( this . _checker ) ;
9299 }
@@ -99,55 +106,48 @@ class UpdateSubscriber extends EventEmitter {
99106}
100107
101108function wrapReader ( reader , readerBuilder , db ) {
102- const proxyObject = {
103- reader,
104- database : db ,
105- lastUpdateCheck : 0 ,
106- subscriber : null
107- } ;
108-
109- return new Proxy ( proxyObject , {
110- get : ( proxyObject , prop ) => {
111- if ( ! proxyObject . subscriber ) {
112- proxyObject . subscriber = new UpdateSubscriber ( ) ;
113-
114- proxyObject . subscriber . on ( 'update' , async ( ) => {
115- proxyObject . reader = await readerBuilder ( paths [ proxyObject . database ] ) ;
116- } ) ;
109+ const subscriber = new UpdateSubscriber ( ) ;
110+ subscriber . on ( 'update' , async ( ) => {
111+ reader = await readerBuilder ( paths [ db ] ) ;
112+ } ) ;
117113
118- proxyObject . subscriber . on ( 'checking' , ( ) => {
119- proxyObject . lastUpdateCheck = Date . now ( ) ;
120- } ) ;
121- }
114+ return new Proxy ( { } , {
115+ get : ( _ , prop ) => {
116+ switch ( prop ) {
117+ case '_geolite2_triggerUpdateCheck' :
118+ subscriber . checkUpdates ( ) ;
119+ return 'OK' ;
122120
123- if ( Date . now ( ) - proxyObject . lastUpdateCheck > updateTimer || prop === '_geolite2_triggerUpdateCheck' ) {
124- proxyObject . subscriber . checkUpdates ( ) ;
125- if ( prop === '_geolite2_triggerUpdateCheck' ) {
121+ case '_geolite2_triggerUpdate' :
122+ // Keep this in for backward compat
126123 return 'OK' ;
127- }
128- }
129124
130- if ( prop === '_geolite2_triggerUpdate' ) {
131- proxyObject . subscriber . triggerUpdate ( ) ;
132- return 'OK' ;
133- }
125+ case '_geolite2_subscriber' :
126+ return subscriber ;
134127
135- if ( prop === '_geolite2_subscriber' ) {
136- return proxyObject . subscriber ;
137- }
128+ case 'close' :
129+ return ( ) => {
130+ subscriber . close ( ) ;
131+ reader = { } ;
132+ return true ;
133+ } ;
138134
139- if ( prop === 'close' ) {
140- proxyObject . subscriber . close ( ) ;
141- proxyObject . reader = { } ;
142- return ( ) => {
143- return true ;
144- } ;
135+ default :
136+ return reader [ prop ] ;
145137 }
146-
147- return proxyObject . reader [ prop ] ;
148138 } ,
149- set : ( proxyObject , prop , value ) => {
150- proxyObject . reader [ prop ] = value ;
139+ set : ( _ , prop , value ) => {
140+ switch ( prop ) {
141+ case '_geolite2_triggerUpdateCheck' :
142+ case '_geolite2_triggerUpdate' :
143+ case '_geolite2_subscriber' :
144+ case 'close' :
145+ throw new Error ( 'Invalid property setter' ) ;
146+
147+ default :
148+ reader [ prop ] = value ;
149+ break ;
150+ }
151151 }
152152 } ) ;
153153}
0 commit comments