@live-change/geoip-service 0.8.22
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/definition.js +10 -0
- package/geoip.js +41 -0
- package/index.js +8 -0
- package/package.json +29 -0
package/definition.js
ADDED
package/geoip.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import definition from './definition.js'
|
|
2
|
+
const config = definition.config
|
|
3
|
+
|
|
4
|
+
import maxmind from 'maxmind'
|
|
5
|
+
|
|
6
|
+
const countryDatabase = maxmind.open(config.geoIpCountryPath ?? process.env.GEOIP_COUNTRY_PATH)
|
|
7
|
+
const defaultCountry = config.geoIpDefaultCountry || process.env.GEOIP_DEFAULT_COUNTRY || "unknown"
|
|
8
|
+
countryDatabase.catch(err => console.error('COUNTRY DB ERROR', err))
|
|
9
|
+
|
|
10
|
+
async function getGeoIp(ip) {
|
|
11
|
+
if(!ip) return defaultCountry
|
|
12
|
+
if(ip === '1' || ip.slice(0,4) === '127.') return defaultCountry
|
|
13
|
+
return countryDatabase.then(db => {
|
|
14
|
+
let result = db.get(ip)
|
|
15
|
+
if(!result) return defaultCountry
|
|
16
|
+
if(!result.country) return defaultCountry
|
|
17
|
+
return result.country.iso_code
|
|
18
|
+
}).catch(error => {
|
|
19
|
+
console.error("GEOIP", ip, error);
|
|
20
|
+
return defaultCountry
|
|
21
|
+
})
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
definition.view({
|
|
25
|
+
name: "myCountry",
|
|
26
|
+
properties: {
|
|
27
|
+
ip: {
|
|
28
|
+
type: String
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
returns: {
|
|
32
|
+
type: String
|
|
33
|
+
},
|
|
34
|
+
remote: true,
|
|
35
|
+
async fetch(props, { client, context }) {
|
|
36
|
+
const geoIpResult = await getGeoIp(client.ip)
|
|
37
|
+
console.log('GEOIP', client.ip, '=>', geoIpResult)
|
|
38
|
+
return geoIpResult
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@live-change/geoip-service",
|
|
3
|
+
"version": "0.8.22",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "NODE_ENV=test tape tests/*"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/live-change/live-change-stack.git"
|
|
12
|
+
},
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/live-change/live-change-stack/issues"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/live-change/live-change-stack",
|
|
18
|
+
"author": {
|
|
19
|
+
"email": "michal@laszczewski.pl",
|
|
20
|
+
"name": "Michał Łaszczewski",
|
|
21
|
+
"url": "https://www.viamage.com/"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@live-change/framework": "^0.8.22",
|
|
25
|
+
"maxmind": "^3.0.3"
|
|
26
|
+
},
|
|
27
|
+
"gitHead": "70a56ac3dc54167a2d32ce47a00150e7a94bd04d",
|
|
28
|
+
"type": "module"
|
|
29
|
+
}
|