Comparing version 0.1.0 to 0.1.1
@@ -14,3 +14,3 @@ | ||
ls.init('./test/dbs/GeoLiteCity.dat'); | ||
ls.init('./test/dbs/GeoLiteCity.dat', { memoryCache: true }); | ||
var ips = fs.readFileSync('./test/dbs/ips.txt'); | ||
@@ -17,0 +17,0 @@ ips = ips.toString().split("\n").map(function(line) { |
@@ -15,2 +15,5 @@ function DynBuffer(source, offset, length) { | ||
}; | ||
DynBuffer.prototype.pointer = function(start) { | ||
return this.offset + start; | ||
}; | ||
DynBuffer.prototype.at = function(start) { | ||
@@ -17,0 +20,0 @@ return this.source[this.offset + start]; |
var Buff = require('./buff'), | ||
var Buff = require('./buff'), | ||
DatabaseInfo = require('./database_info'), | ||
Country = require('./country'), | ||
Location = require('./location'), | ||
Region = require('./region'), | ||
DynBuffer = require('./dyn_buffer'); | ||
Country = require('./country'), | ||
Location = require('./location'), | ||
Region = require('./region'), | ||
DynBuffer = require('./dyn_buffer'); | ||
const US_OFFSET = 1; | ||
@@ -344,3 +345,2 @@ const CANADA_OFFSET = 677; | ||
for (var depth = 31; depth >= 0; depth--) { | ||
var cache = _cache[offset]; | ||
@@ -353,29 +353,24 @@ | ||
// read from memory | ||
if ((dboptions & GEOIP_MEMORY_CACHE) != 0) { | ||
//read from memory | ||
dbbuffer.copy(buf, 0, 2 * recordLength * offset, (2 * recordLength * offset) + (2 * MAX_RECORD_LENGTH)); | ||
buf = new DynBuffer(dbbuffer, 2 * recordLength * offset, 2 * MAX_RECORD_LENGTH); | ||
// read from index cache | ||
} else if ((dboptions & GEOIP_INDEX_CACHE) != 0) { | ||
//read from index cache | ||
index_cache.copy(buf, 0, 2 * recordLength * offset, (2 * recordLength * offset) + (2 * MAX_RECORD_LENGTH)); | ||
buf = new DynBuffer(index_cache, 2 * recordLength * offset, 2 * MAX_RECORD_LENGTH); | ||
// read from disk | ||
} else { | ||
//read from disk | ||
try { | ||
file.seek(2 * recordLength * offset); | ||
file.readFully(buf); | ||
} catch (e) { | ||
console.log("IO Exception"); | ||
} | ||
buf = new DynBuffer(new Buffer(2 * MAX_RECORD_LENGTH), 0, 2 * MAX_RECORD_LENGTH); | ||
file.seek(2 * recordLength * offset); | ||
file.readFully(buf.source); | ||
} | ||
x0 = 0; | ||
x1 = 0; | ||
x0 = 0, x1 = 0; | ||
for (var j = 0; j<recordLength; j++) { | ||
y = buf[0*recordLength+j]; | ||
// y = dbbuffer[(2 * recordLength * offset) + (0*recordLength+j)] | ||
y = buf.at(0*recordLength+j); | ||
x0 += (y << (j * 8)); | ||
y = buf[1*recordLength+j]; | ||
// y = dbbuffer[(2 * recordLength * offset) + (1*recordLength+j)] | ||
y = buf.at(1*recordLength+j); | ||
x1 += (y << (j * 8)); | ||
@@ -385,4 +380,5 @@ } | ||
if ((ipAddress & (1 << depth)) > 0) { | ||
// unfortunately we cannot perform bitwise operations | ||
// on integers lager than max 2^32 | ||
if (Math.abs(ipAddress & (1 << depth)) > 0) { | ||
if (x1 >= databaseSegment) { | ||
@@ -421,2 +417,4 @@ last_netmask = 32 - depth; | ||
var ip = ipnum; | ||
if (typeof ipnum == "string") | ||
@@ -423,0 +421,0 @@ ipnum = this.ip2Long(ipnum); |
{ | ||
"name": "maxmind", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/runk/node-maxmind", | ||
@@ -8,3 +8,5 @@ "description": "IP lookup using Maxmind databases", | ||
"author": "Shirokov Dmitry <deadrunk@gmail.com>", | ||
"dependencies": {}, | ||
"dependencies": { | ||
}, | ||
"repository": { | ||
@@ -11,0 +13,0 @@ "type":"git", |
@@ -5,3 +5,3 @@ | ||
IP geo lookup using Maxmind databases, written in pure javascript, with no dependencies. | ||
IP geo lookup using Maxmind databases, written in pure javascript with no dependencies. | ||
@@ -64,4 +64,4 @@ ## GEO databases | ||
- default: 18,000 lookups / second | ||
- `indexCache`: 60,000 lookups / second | ||
- `memodyCache`: 100,000 lookups / second | ||
- `indexCache`: 80,000 lookups / second | ||
- `memodyCache`: 130,000 lookups / second | ||
@@ -68,0 +68,0 @@ ## Tests |
@@ -10,2 +10,3 @@ | ||
const GEO_CITY = __dirname + '/dbs/GeoLiteCity.dat'; | ||
const GEO_CITY_FULL = __dirname + '/dbs/GeoIPCity_FULL.dat'; | ||
const GEO_COUNTRY = __dirname + '/dbs/GeoIP.dat'; | ||
@@ -40,2 +41,6 @@ | ||
assert.equal(result, 1474659864); | ||
var result = ls.ip2Long("195.68.137.18"); | ||
assert.equal(result, 3276048658); | ||
}); | ||
@@ -70,2 +75,10 @@ }); | ||
assert.equal(c.getCode(), 'RU'); | ||
var c = ls.getCountry('210.250.100.200'); | ||
assert.equal(c.getName(), 'Japan'); | ||
assert.equal(c.getCode(), 'JP'); | ||
var c = ls.getCountry('1.2.1.1'); | ||
assert.equal(c.getName(), 'China'); | ||
assert.equal(c.getCode(), 'CN'); | ||
}); | ||
@@ -80,5 +93,16 @@ | ||
describe("#seekCountry", function() { | ||
it("should perform binary search", function() { | ||
assert.equal(ls.init(GEO_CITY_FULL), true); | ||
var iplong = ls.ip2Long('195.68.137.18'); | ||
assert.equal(ls.seekCountry(iplong), 9150727); | ||
iplong = ls.ip2Long('210.250.100.200'); | ||
assert.equal(ls.seekCountry(iplong), 8067695); | ||
}); | ||
}); | ||
describe('#getLocation', function() { | ||
it('should init with country db', function() { | ||
assert.equal(ls.init(GEO_CITY), true); | ||
assert.equal(ls.init(GEO_CITY_FULL), true); | ||
assert.equal(ls.inited, true); | ||
@@ -92,6 +116,6 @@ }); | ||
assert.equal(l.countryName, 'Russian Federation'); | ||
assert.equal(l.region, '48'); | ||
assert.equal(l.city, 'Moscow'); | ||
assert.equal(l.latitude, 55.75219999999999); | ||
assert.equal(l.longitude, 37.6156); | ||
assert.equal(l.region, '21'); | ||
assert.equal(l.city, 'Ivanovo'); | ||
assert.equal(l.latitude, 56.99719999999999); | ||
assert.equal(l.longitude, 40.97139999999999); | ||
assert.equal(l.metro_code, 0); | ||
@@ -101,4 +125,30 @@ assert.equal(l.dma_code, 0); | ||
}); | ||
it('should return location by ip (2)', function() { | ||
var l = ls.getLocation('195.68.137.18'); | ||
assert.equal(l.countryCode, 'RU'); | ||
assert.equal(l.countryName, 'Russian Federation'); | ||
assert.equal(l.region, '47'); | ||
assert.equal(l.city, 'Marfino'); | ||
assert.equal(l.latitude, 55.70269999999999); | ||
assert.equal(l.longitude, 37.38319999999999); | ||
assert.equal(l.metro_code, 0); | ||
assert.equal(l.dma_code, 0); | ||
assert.equal(l.area_code, 0); | ||
}); | ||
it('should return location by ip (3)', function() { | ||
var l = ls.getLocation('2.2.3.29'); | ||
assert.equal(l.countryCode, 'FR'); | ||
assert.equal(l.countryName, 'France'); | ||
assert.equal(l.region, 'A2'); | ||
assert.equal(l.city, 'Rennes'); | ||
assert.equal(l.latitude, 48.111999999999995); | ||
assert.equal(l.longitude, -1.6742999999999881); | ||
assert.equal(l.metro_code, 0); | ||
assert.equal(l.dma_code, 0); | ||
assert.equal(l.area_code, 0); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
166633
6655