ac-geoip 1.4.2 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,41 @@
1
+ <a name="3.0.0"></a>
2
+
3
+ # [3.0.0](https://github.com/admiralcloud/ac-geoip/compare/v2.0.0..v3.0.0) (2022-12-18 08:36:22)
4
+
5
+
6
+ ### Bug Fix
7
+
8
+ * **App:** Package updates | MP | [e3278aa2ee04c529eebdee3c1f37ad1f8fd19425](https://github.com/admiralcloud/ac-geoip/commit/e3278aa2ee04c529eebdee3c1f37ad1f8fd19425)
9
+ Package updates - ac-geoip now requires Node 16+
10
+ Related issues: [/issues#undefined](https://github.com//issues/undefined)
11
+ ## BREAKING CHANGES
12
+ * **App:** Node > 16 is required
13
+ <a name="2.0.0"></a>
14
+
15
+ # [2.0.0](https://github.com/admiralcloud/ac-geoip/compare/v1.4.2..v2.0.0) (2022-01-29 11:56:17)
16
+
17
+
18
+ ### Bug Fix
19
+
20
+ * **App:** Open geolite DB during initialisation | VD | [37b6981913f08798c1b764021d528497f8cc238f](https://github.com/admiralcloud/ac-geoip/commit/37b6981913f08798c1b764021d528497f8cc238f)
21
+ Open geolite DB during initialisation
22
+ Related issues: [undefined/undefined#AC-2500](undefined/browse/AC-2500)
23
+ * **App:** Cache geoip2 Reader object | VD | [54a67bb2d186ccf10ec72e8b370ce9a51c2ab89c](https://github.com/admiralcloud/ac-geoip/commit/54a67bb2d186ccf10ec72e8b370ce9a51c2ab89c)
24
+ Store object to re-use instead of opening DB file each time it's needed.
25
+ Related issues: [undefined/undefined#AC-2500](undefined/browse/AC-2500)
26
+ * **App:** Remove callbacks from async functions | VD | [6c4a6ecd0ce8f8350eb206b41be0d90ddaf09ee5](https://github.com/admiralcloud/ac-geoip/commit/6c4a6ecd0ce8f8350eb206b41be0d90ddaf09ee5)
27
+ Mixing async and callback functions makes code read hard, misunderstand the results and make function behave unpredictably.
28
+ Related issues: [undefined/undefined#AC-2500](undefined/browse/AC-2500)
29
+ ### Style
30
+
31
+ * **App:** Get rid of semicollons, code re-aligning | VD | [e27005f2001a0da06d12fe272a3e007937f1604b](https://github.com/admiralcloud/ac-geoip/commit/e27005f2001a0da06d12fe272a3e007937f1604b)
32
+ Get rid of semicollons, code re-aligning
33
+ ### Chores
34
+
35
+ * **App:** Updated packages | MP | [49f68ab1575163f6b9e9a845a23cd779195d9a41](https://github.com/admiralcloud/ac-geoip/commit/49f68ab1575163f6b9e9a845a23cd779195d9a41)
36
+ Updated packages
37
+ ## BREAKING CHANGES
38
+ * **App:** * lookupLocal, lookup functions fully async and doesn't have callback parameters
1
39
  <a name="1.4.2"></a>
2
40
 
3
41
  ## [1.4.2](https://github.com/admiralcloud/ac-geoip/compare/v1.4.1..v1.4.2) (2021-10-09 10:25:40)
package/README.md CHANGED
@@ -28,7 +28,7 @@ async() {
28
28
  const acgeoip = require('./index')
29
29
 
30
30
  let geoip = {
31
- geolite: {
31
+ geolite: {
32
32
  enabled: true,
33
33
  path: '/path/to/GeoLite2-City.mmdb'
34
34
  }
package/index.js CHANGED
@@ -2,14 +2,14 @@ const _ = require('lodash')
2
2
  const ipPackage = require('ip')
3
3
  const fs = require('fs')
4
4
 
5
- const WebServiceClient = require('@maxmind/geoip2-node').WebServiceClient;
6
- const Reader = require('@maxmind/geoip2-node').Reader;
5
+ const WebServiceClient = require('@maxmind/geoip2-node').WebServiceClient
6
+ const Reader = require('@maxmind/geoip2-node').Reader
7
7
 
8
- const NodeCache = require( "node-cache" );
9
- const geoCache = new NodeCache( { stdTTL: 7*86400, checkperiod: 3600 } );
8
+ const NodeCache = require("node-cache")
9
+ const geoCache = new NodeCache({ stdTTL: 7 * 86400, checkperiod: 3600 })
10
10
 
11
11
  const acgeoip = () => {
12
-
12
+
13
13
  let geoip = {
14
14
  userId: 'userId',
15
15
  licenseKey: 'licenseKey',
@@ -45,22 +45,22 @@ const acgeoip = () => {
45
45
  const dbBuffer = fs.readFileSync(_.get(geoip, 'geolite.path'))
46
46
  geoip.reader = Reader.openBuffer(dbBuffer)
47
47
  }
48
+ else {
49
+ Reader.open(_.get(geoip, 'geolite.path')).then(geoipReader => {
50
+ geoip.geolite.reader = geoipReader
51
+ })
52
+ }
48
53
  }
49
54
 
50
-
51
-
52
- const lookupLocal = async(params, cb) => {
55
+ const lookupLocal = async (params) => {
53
56
  const functionName = 'ac-geoip | lookupLocal'
54
57
  if (!_.get(geoip, 'geolite.enabled')) {
55
- let message = 'acgeoip_geolite_notEnabled'
56
- if (_.isFunction(cb)) return cb({ message })
58
+ const message = 'acgeoip_geolite_notEnabled'
57
59
  throw Error(message)
58
60
  }
61
+
59
62
  const ip = _.get(params, 'ip')
60
- if (ipPackage.isPrivate(ip)) {
61
- if (_.isFunction(cb)) return cb()
62
- return
63
- }
63
+ if (ipPackage.isPrivate(ip)) return
64
64
 
65
65
  const mapping = _.get(params, 'mapping', geoip.mapping)
66
66
  const debug = _.get(params, 'debug')
@@ -73,11 +73,17 @@ const acgeoip = () => {
73
73
  let geoipResponse
74
74
 
75
75
  if (geoip.redis) {
76
- geoipResponse = await checkRedis(params)
76
+ try {
77
+ geoipResponse = await checkRedis(params)
78
+ }
79
+ catch (err) {
80
+ console.error('AC-GEOIP | From Geolite | Failed | %j', err)
81
+ }
77
82
  }
78
83
  else {
79
84
  geoipResponse = getFromMemory({ ip })
80
85
  }
86
+
81
87
  if (debugPerformance) console.log('%s | getFromCache %d', functionName, performanceHelper(start, process.hrtime()))
82
88
 
83
89
  if (!geoipResponse) {
@@ -88,31 +94,34 @@ const acgeoip = () => {
88
94
  else {
89
95
  try {
90
96
  if (_.get(geoip, 'geolite.enabled')) {
91
- geoipResponse = await new Promise((resolve, reject) => {
92
- Reader.open(_.get(geoip, 'geolite.path')).then(reader => {
93
- const response = reader.city(ip)
94
- resolve(response)
95
- }).catch(reject)
96
- })
97
+ let geoipReader = _.get(geoip, 'geolite.reader')
98
+ if (!geoipReader) {
99
+ geoipReader = await Reader.open(_.get(geoip, 'geolite.path'))
100
+ _.set(geoip, 'geolite.reader', geoipReader)
101
+ }
102
+ if (geoipReader) geoipResponse = geoipReader.city(ip)
97
103
  }
104
+
98
105
  if (debugPerformance) console.log('%s | readFromDB %d', functionName, performanceHelper(start, process.hrtime()))
99
106
 
100
107
  if (debug) {
101
108
  console.log('AC-GEOIP | From Geolite | %j', geoipResponse)
102
109
  }
103
110
  }
104
- catch(e) {
111
+ catch (e) {
105
112
  console.error('AC-GEOIP | From Geolite | Failed | %j', e)
106
113
  }
107
114
  }
115
+
108
116
  if (geoipResponse) {
109
117
  _.set(geoipResponse, 'origin', 'db')
110
118
  if (geoip.redis) {
111
- await storeRedis({ ip, geoipResponse })
119
+ await storeRedis({ ip, geoipResponse })
112
120
  }
113
121
  else {
114
- storeInMemory({ ip, geoipResponse })
122
+ storeInMemory({ ip, geoipResponse })
115
123
  }
124
+
116
125
  if (debugPerformance) console.log('%s | storeInCache %d', functionName, performanceHelper(start, process.hrtime()))
117
126
  }
118
127
  }
@@ -126,24 +135,23 @@ const acgeoip = () => {
126
135
  else {
127
136
  response = geoipResponse
128
137
  }
138
+
129
139
  _.set(response, 'origin', _.get(geoipResponse, 'origin'))
130
140
  if (_.get(geoipResponse, 'fromCache')) _.set(response, 'fromCache', true)
131
141
 
132
142
  if (debugPerformance) console.log('%s | Finished %d', functionName, performanceHelper(start, process.hrtime()))
133
- if (_.isFunction(cb)) return cb(null, response)
143
+
134
144
  return response
135
145
  }
136
146
 
137
- const lookup = async(params, cb) => {
147
+ const lookup = async (params) => {
138
148
  const functionName = 'ac-geoip | lookup'
139
149
  if (!_.get(geoip, 'licenseKey') || _.get(geoip, 'licenseKey') === 'licenseKey') {
140
- let message = 'acgeoip_licenseKey_missing'
141
- if (_.isFunction(cb)) return cb({ message })
150
+ const message = 'acgeoip_licenseKey_missing'
142
151
  throw Error(message)
143
152
  }
144
153
  const ip = _.get(params, 'ip')
145
154
  if (ipPackage.isPrivate(ip)) {
146
- if (_.isFunction(cb)) return cb()
147
155
  return
148
156
  }
149
157
 
@@ -151,7 +159,7 @@ const acgeoip = () => {
151
159
  const debug = _.get(params, 'debug')
152
160
  const debugPerformance = _.get(params, 'debugPerforance')
153
161
  const start = process.hrtime()
154
-
162
+
155
163
  let response = {
156
164
  ip
157
165
  }
@@ -169,11 +177,7 @@ const acgeoip = () => {
169
177
  if (!_.get(geoipResponse, 'country')) {
170
178
  try {
171
179
  const client = new WebServiceClient(geoip.userId, geoip.licenseKey)
172
- geoipResponse = await new Promise((resolve, reject) => {
173
- client.city(ip).then(result => {
174
- return resolve(result)
175
- }).catch(reject)
176
- })
180
+ geoipResponse = await client.city(ip)
177
181
  if (debugPerformance) console.log('%s | readFromWebservice %d', functionName, performanceHelper(start, process.hrtime()))
178
182
  if (geoipResponse) {
179
183
  _.set(geoipResponse, 'origin', 'webservice')
@@ -183,17 +187,17 @@ const acgeoip = () => {
183
187
  console.log('AC-GEOIP | From Maxmind | %j', geoipResponse)
184
188
  }
185
189
  }
186
- catch(e) {
190
+ catch (e) {
187
191
  console.error('AC-GEOIP | From Maxmind | Failed | %j', e)
188
192
  }
189
193
  }
190
194
 
191
195
  if (geoipResponse) {
192
196
  if (geoip.redis) {
193
- await storeRedis({ ip, geoipResponse })
197
+ await storeRedis({ ip, geoipResponse })
194
198
  }
195
199
  else {
196
- storeInMemory({ ip, geoipResponse })
200
+ storeInMemory({ ip, geoipResponse })
197
201
  }
198
202
  if (debugPerformance) console.log('%s | storeInCache %d', functionName, performanceHelper(start, process.hrtime()))
199
203
  }
@@ -211,7 +215,6 @@ const acgeoip = () => {
211
215
  _.set(response, 'origin', _.get(geoipResponse, 'origin'))
212
216
  if (_.get(geoipResponse, 'fromCache')) _.set(response, 'fromCache', true)
213
217
 
214
- if (_.isFunction(cb)) return cb(null, response)
215
218
  return response
216
219
  }
217
220
 
@@ -228,13 +231,10 @@ const acgeoip = () => {
228
231
  return geoCache.get(storageKey)
229
232
  }
230
233
 
231
-
232
- const checkRedis = async(params, cb) => {
234
+ const checkRedis = async (params) => {
233
235
  const refresh = _.get(params, 'refresh')
234
- if (!geoip.redis || refresh) {
235
- if (_.isFunction(cb)) return cb()
236
- return
237
- }
236
+ if (!geoip.redis || refresh) return
237
+
238
238
  const ip = _.get(params, 'ip')
239
239
  const redisKey = _.get(geoip, 'environment') + ':geoip:' + ip
240
240
  const debug = _.get(params, 'debug')
@@ -250,14 +250,14 @@ const acgeoip = () => {
250
250
  console.log('AC-GEOIP | From Cache | %j', geoipResponse)
251
251
  }
252
252
  }
253
- catch(e) {
253
+ catch (e) {
254
254
  console.log(e)
255
255
  console.error('AC-GEOIP | From Cache | Failed | %j', e)
256
256
  }
257
257
  return geoipResponse
258
258
  }
259
259
 
260
- const storeRedis = async(params) => {
260
+ const storeRedis = async (params) => {
261
261
  const refresh = _.get(params, 'refresh')
262
262
  if (!geoip.redis || refresh) {
263
263
  return
@@ -273,10 +273,9 @@ const acgeoip = () => {
273
273
  const accuracy = _.get(options, 'accuracy', 1e6)
274
274
  const s = t2[0] - t1[0]
275
275
  const mms = t2[1] - t1[1]
276
- return (s*1e9 + mms)/accuracy
276
+ return (s * 1e9 + mms) / accuracy
277
277
  }
278
278
 
279
-
280
279
  return {
281
280
  init,
282
281
  lookup,
package/package.json CHANGED
@@ -3,24 +3,24 @@
3
3
  "author": "Mark Poepping (https://www.admiralcloud.com)",
4
4
  "license": "MIT",
5
5
  "repository": "admiralcloud/ac-geoip",
6
- "version": "1.4.2",
6
+ "version": "3.0.0",
7
7
  "dependencies": {
8
- "@maxmind/geoip2-node": "^3.2.0",
9
- "ip": "^1.1.5",
8
+ "@maxmind/geoip2-node": "^3.5.0",
9
+ "ip": "^1.1.8",
10
10
  "lodash": "^4.17.21",
11
11
  "node-cache": "^5.1.2"
12
12
  },
13
13
  "devDependencies": {
14
- "ac-semantic-release": "^0.2.6",
15
- "chai": "^4.3.4",
16
- "eslint": "^7.32.0",
17
- "ioredis": "^4.27.10",
18
- "mocha": "^9.1.2"
14
+ "ac-semantic-release": "^0.3.4",
15
+ "chai": "^4.3.7",
16
+ "eslint": "^8.30.0",
17
+ "ioredis": "^5.2.4",
18
+ "mocha": "^10.2.0"
19
19
  },
20
20
  "scripts": {
21
21
  "test": "./node_modules/.bin/mocha --bail --exit --slow 1000 ./test/test.js || :"
22
22
  },
23
23
  "engines": {
24
- "node": ">=8.0.0"
24
+ "node": ">=16.0"
25
25
  }
26
26
  }