@smpx/koa-request 1.0.4 → 1.1.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.
Files changed (2) hide show
  1. package/GeoIP.js +26 -4
  2. package/package.json +4 -4
package/GeoIP.js CHANGED
@@ -82,7 +82,8 @@ async function updateDb() {
82
82
  console.log(`[Request::GeoIP] Downloaded ${fileName} !`);
83
83
  geoip = await maxmind.open(filePath);
84
84
  }
85
- setTimeout(updateDb, 3 * 24 * 3600 * 1000);
85
+ const timer = setTimeout(updateDb, 3 * 24 * 3600 * 1000);
86
+ timer.unref();
86
87
  }
87
88
 
88
89
  async function geoIpInit() {
@@ -117,11 +118,23 @@ function getGeoIpSync() {
117
118
 
118
119
  class GeoIP {
119
120
  static async get(ip) {
120
- return (await getGeoIp()).get(ip);
121
+ try {
122
+ return await (await getGeoIp()).get(ip);
123
+ }
124
+ catch (e) {
125
+ console.error('Error getting geoip location', e);
126
+ return null;
127
+ }
121
128
  }
122
129
 
123
130
  static getSync(ip) {
124
- return getGeoIpSync().get(ip);
131
+ try {
132
+ return getGeoIpSync().get(ip);
133
+ }
134
+ catch (e) {
135
+ console.error('Error getting geoip location', e);
136
+ return null;
137
+ }
125
138
  }
126
139
 
127
140
  static async init() {
@@ -141,7 +154,7 @@ async function main() {
141
154
  ips.push(`${random(1, 255)}.${random(1, 255)}.${random(1, 255)}.${random(1, 255)}`);
142
155
  }
143
156
 
144
- console.log((await GeoIP.get('1.2.3.4')).subdivisions);
157
+ console.log((await GeoIP.get('1.2.3.4')).location);
145
158
 
146
159
  console.time('ipLookup');
147
160
  for (let i = 0; i < 100000; i++) {
@@ -150,6 +163,15 @@ async function main() {
150
163
  }
151
164
  console.timeEnd('ipLookup');
152
165
  // takes approx ~ 900ms for 100,000 lookups
166
+
167
+ console.log((GeoIP.getSync('1.2.3.4')).location);
168
+
169
+ console.time('ipLookupSync');
170
+ for (let i = 0; i < 100000; i++) {
171
+ GeoIP.getSync(ips[i]);
172
+ }
173
+ console.timeEnd('ipLookupSync');
174
+ // takes approx ~ 900ms for 100,000 lookups
153
175
  }
154
176
 
155
177
  if (require.main === module) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smpx/koa-request",
3
- "version": "1.0.4",
3
+ "version": "1.1.0",
4
4
  "description": "Handle basic tasks for koajs",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -35,9 +35,9 @@
35
35
  "koa-body": "^5.0.0",
36
36
  "koa-send": "^5.0.1",
37
37
  "koa2-ratelimit": "^1.1.3",
38
- "maxmind": "^4.3.20",
39
- "tar": "^6.2.1",
40
- "ua-parser-js": "^1.0.38"
38
+ "maxmind": "^5.0.0",
39
+ "tar": "^7.4.3",
40
+ "ua-parser-js": "^1.0.40"
41
41
  },
42
42
  "devDependencies": {
43
43
  "eslint": "^5.7.0",