@smpx/koa-request 0.4.2 → 0.5.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/GeoIP.js CHANGED
@@ -9,6 +9,7 @@ const fileName = 'GeoLite2-City';
9
9
  const fileUrl = `https://raw.githubusercontent.com/GitSquared/node-geolite2-redist/master/redist/${fileName}.tar.gz`;
10
10
  const fileDir = __dirname;
11
11
  const filePath = `${fileDir}/${fileName}.mmdb`;
12
+ let geoInitPromise;
12
13
  let geoip;
13
14
 
14
15
  function save(url, outDir) {
@@ -84,13 +85,21 @@ async function updateDb() {
84
85
  setTimeout(updateDb, 3 * 24 * 3600 * 1000);
85
86
  }
86
87
 
88
+ async function geoIpInit() {
89
+ await download();
90
+ geoip = await maxmind.open(filePath);
91
+ if (process.env.NODE_ENV === 'production') {
92
+ updateDb();
93
+ }
94
+ }
95
+
87
96
  async function getGeoIp() {
88
97
  if (!geoip) {
89
- await download();
90
- geoip = await maxmind.open(filePath);
91
- if (process.env.NODE_ENV === 'production') {
92
- updateDb();
98
+ if (!geoInitPromise) {
99
+ geoInitPromise = geoIpInit();
93
100
  }
101
+ await geoInitPromise;
102
+ geoInitPromise = null;
94
103
  }
95
104
  return geoip;
96
105
  }
package/IPRange.js CHANGED
@@ -11,6 +11,7 @@ const PRIVATE_IPS = [
11
11
  ];
12
12
 
13
13
  // Source: https://www.lifewire.com/what-is-the-ip-address-of-google-818153
14
+ // https://developers.google.com/search/apis/ipranges/googlebot.json
14
15
  const GOOGLE_IPS = [
15
16
  '64.233.160.0 - 64.233.191.255',
16
17
  '66.102.0.0 - 66.102.15.255',
@@ -27,11 +28,16 @@ const GOOGLE_IPS = [
27
28
  ];
28
29
 
29
30
  const SEARCH_IPS = [
30
- '178.154.128.0/17', // YANDAX
31
- '87.250.224.0/19', // YANDAX
32
- '157.55.0.0/16', // Microsoft
33
- '207.46.0.0/19', // Microsoft
34
- '17.0.0.0/8', // Apple
31
+ // BingBot (https://www.bing.com/toolbox/bingbot.json)
32
+ '157.55.0.0/16',
33
+ '207.46.0.0/19',
34
+ '52.167.144.0/24',
35
+ '40.77.128.0/17',
36
+ // YANDAX
37
+ '178.154.128.0/17',
38
+ '87.250.224.0/19',
39
+ // Apple
40
+ '17.0.0.0/8',
35
41
  ];
36
42
 
37
43
  let compiledPrivateIps;
package/Request.js CHANGED
@@ -1260,7 +1260,7 @@ class Request {
1260
1260
  let host = refererUri.hostname;
1261
1261
  const baseDomain = this.baseDomain();
1262
1262
 
1263
- if (host.endsWith(baseDomain)) {
1263
+ if (host.endsWith(baseDomain) || /^(?:direct|localhost|127|192|172|10)\b/.test(host)) {
1264
1264
  return {
1265
1265
  name: host,
1266
1266
  source: host,
@@ -1423,9 +1423,16 @@ class Request {
1423
1423
  }
1424
1424
  }
1425
1425
 
1426
+ geoipLoc() {
1427
+ if (this._giploc === undefined) {
1428
+ this._giploc = GeoIP.getSync(this.ip()) || {};
1429
+ }
1430
+ return this._giploc;
1431
+ }
1432
+
1426
1433
  ipLocation() {
1427
1434
  if (this._ipLoc === undefined) {
1428
- const loc = GeoIP.getSync(this.ip()) || {};
1435
+ const loc = this.geoipLoc();
1429
1436
  const country = loc.country;
1430
1437
  const city = loc.city;
1431
1438
  const state = loc.subdivisions && loc.subdivisions[0];
package/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  const Request = require('./Request');
2
2
  const IPRange = require('./IPRange');
3
+ const GeoIP = require('./GeoIP');
3
4
 
4
5
  module.exports = {
5
6
  Request,
6
7
  IPRange,
8
+ GeoIP,
7
9
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smpx/koa-request",
3
- "version": "0.4.2",
3
+ "version": "0.5.0",
4
4
  "description": "Handle basic tasks for koajs",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -30,14 +30,14 @@
30
30
  },
31
31
  "homepage": "https://github.com/smartprix/koa-request#readme",
32
32
  "dependencies": {
33
- "ip": "^1.1.8",
33
+ "ip": "^2.0.1",
34
34
  "koa-basic-auth": "^4.0.0",
35
35
  "koa-body": "^5.0.0",
36
36
  "koa-send": "^5.0.1",
37
- "koa2-ratelimit": "^1.1.1",
38
- "maxmind": "^4.3.6",
39
- "tar": "^6.1.11",
40
- "ua-parser-js": "^1.0.2"
37
+ "koa2-ratelimit": "^1.1.3",
38
+ "maxmind": "^4.3.20",
39
+ "tar": "^6.2.1",
40
+ "ua-parser-js": "^1.0.38"
41
41
  },
42
42
  "devDependencies": {
43
43
  "eslint": "^5.7.0",