Comparing version 0.0.2 to 0.0.3
@@ -1,2 +0,2 @@ | ||
var util = require('util'), | ||
var fs = require('fs'), | ||
Buffer = require('buffer').Buffer; | ||
@@ -6,4 +6,4 @@ | ||
function Buff(ba) { | ||
this.ba = ba; | ||
/*function Buff(path) { | ||
this.ba = fs.readFileSync(path); | ||
}; | ||
@@ -36,6 +36,35 @@ Buff.prototype.ba = null; | ||
// do nothing atm | ||
};*/ | ||
function Buff(path) { | ||
this.meta = fs.statSync(path); | ||
this.ba = fs.openSync(path, 'r'); | ||
}; | ||
Buff.prototype.ba = null; | ||
Buff.prototype.meta = null; | ||
Buff.prototype.pointer = 0; | ||
Buff.prototype.length = function() { | ||
return this.meta.size; | ||
}; | ||
Buff.prototype.seek = function(p) { | ||
this.pointer = p; | ||
}; | ||
Buff.prototype.readByte = function() { | ||
var b = new Buffer(1); | ||
fs.readSync(this.ba, b, 0, 1, this.pointer++); | ||
return b.readUInt8(0); | ||
}; | ||
Buff.prototype.readFully = function(dest) { | ||
var len = fs.readSync(this.ba, dest, 0, dest.length, this.pointer); | ||
this.pointer += len; | ||
return len; | ||
}; | ||
Buff.prototype.getFilePointer = function() { | ||
return this.pointer; | ||
}; | ||
Buff.prototype.close = function() { | ||
fs.closeSync(this.ba); | ||
}; | ||
module.exports = Buff; |
var fs = require('fs'), | ||
Buff = require('./buff'), | ||
var Buff = require('./buff'), | ||
DatabaseInfo = require('./database_info'), | ||
@@ -107,19 +106,4 @@ Country = require('./country'), | ||
function _getDatabaseInfo() { | ||
module.exports.ip2Long = function(ip) { | ||
ip = ip.split('.'); | ||
var c = ip.length; | ||
ip[0] = parseInt(ip[0]) || 0; | ||
ip[1] = parseInt(ip[1]) || 0; | ||
ip[2] = parseInt(ip[2]) || 0; | ||
ip[3] = parseInt(ip[3]) || 0; | ||
return ip[0] * (c === 1 || 16777216) | ||
+ ip[1] * (c <= 2 || 65536) | ||
+ ip[2] * (c <= 3 || 256) | ||
+ ip[3] * 1; | ||
}; | ||
module.exports.getDatabaseInfo = function() { | ||
var hasStructureInfo = false; | ||
@@ -131,3 +115,3 @@ var delim = new Buffer(3); | ||
for (var i = 0; i < STRUCTURE_INFO_MAX_SIZE; i++) { | ||
var read = file.read(delim); | ||
var read = file.readFully(delim); | ||
if (read == 3 && (delim[0]&0xFF) == 255 && (delim[1]&0xFF) == 255 && (delim[2]&0xFF) == 255) { | ||
@@ -158,29 +142,32 @@ hasStructureInfo = true; | ||
return null; | ||
} | ||
}; | ||
function _check_mtime() { | ||
// try { | ||
// if ((dboptions & GEOIP_CHECK_CACHE) != 0){ | ||
// long t = databaseFile.lastModified(); | ||
// if (t != mtime){ | ||
// /* GeoIP Database file updated */ | ||
// /* refresh filehandle */ | ||
// close(); | ||
// file = new RandomAccessFile(databaseFile,"r"); | ||
// databaseInfo = null; | ||
// init(); | ||
// } | ||
// } | ||
// } catch (IOException e) { | ||
// System.out.println("file not found"); | ||
// } | ||
} | ||
// do noting atm | ||
}; | ||
module.exports.ip2Long = function(ip) { | ||
ip = ip.split('.'); | ||
var c = ip.length; | ||
ip[0] = parseInt(ip[0]) || 0; | ||
ip[1] = parseInt(ip[1]) || 0; | ||
ip[2] = parseInt(ip[2]) || 0; | ||
ip[3] = parseInt(ip[3]) || 0; | ||
return ip[0] * (c === 1 || 16777216) | ||
+ ip[1] * (c <= 2 || 65536) | ||
+ ip[2] * (c <= 3 || 256) | ||
+ ip[3] * 1; | ||
}; | ||
module.exports.databaseInfo = null; | ||
module.exports.inited = false; | ||
module.exports.path = null; | ||
module.exports.init = function(path) { | ||
file = new Buff( | ||
fs.readFileSync(path) | ||
); | ||
if (path == this.path && this.inited) | ||
return true; | ||
file = new Buff(path); | ||
var i, j; | ||
@@ -253,3 +240,2 @@ var delim = new Buffer(3); | ||
for (j = 0; j < SEGMENT_RECORD_LENGTH; j++) { | ||
// databaseSegments[0] += (unsignedByteToInt(buf[j]) << (j * 8)); | ||
databaseSegments[0] += buf.readUInt8(j) << (j * 8); | ||
@@ -278,3 +264,3 @@ } | ||
file.readFully(dbbuffer,0,l); | ||
databaseInfo = this.getDatabaseInfo(); | ||
this.databaseInfo = _getDatabaseInfo(); | ||
file.close(); | ||
@@ -295,2 +281,3 @@ } | ||
this.inited = true; | ||
this.path = path; | ||
@@ -326,2 +313,5 @@ return true; | ||
//read from disk | ||
// fs.readSync(fd, buffer, offset, length, position) | ||
try { | ||
@@ -340,2 +330,3 @@ file.seek(2 * recordLength * offset); | ||
y = buf[0*recordLength+j]; | ||
// y = dbbuffer[(2 * recordLength * offset) + (0*recordLength+j)] | ||
if (y < 0) { | ||
@@ -347,2 +338,3 @@ y+= 256; | ||
y = buf[1*recordLength+j]; | ||
// y = dbbuffer[(2 * recordLength * offset) + (1*recordLength+j)] | ||
if (y < 0) { | ||
@@ -352,3 +344,2 @@ y+= 256; | ||
x1 += (y << (j * 8)); | ||
} | ||
@@ -355,0 +346,0 @@ |
{ | ||
"name": "maxmind", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"homepage": "https://github.com/runk/maxmind", | ||
"description": "IP lookup module using Maxmind databases", | ||
"description": "IP lookup using Maxmind databases", | ||
"keywords": ["maxmind", "geo", "geobase", "geo lookup", "ip base", "geocode"], | ||
@@ -7,0 +7,0 @@ "author": "Shirokov Dmitry <deadrunk@gmail.com>", |
@@ -5,3 +5,3 @@ | ||
IP lookup using Maxmind databases, written in pure javascript | ||
IP geo lookup using Maxmind databases, written in pure javascript, with no dependencies. | ||
@@ -43,5 +43,5 @@ ## GEO databases | ||
Module is quite fresh and some sirius bugs are possible. Feel free to | ||
Module is quite young and some serious bugs are possible. Feel free to | ||
send pull request / bug reports. | ||
Module currently work only in `MEMORY_CACHE` mode. |
@@ -42,3 +42,3 @@ | ||
describe('#getDatabaseInfo', function() { | ||
describe('.databaseInfo', function() { | ||
@@ -50,3 +50,3 @@ it('inited prop should be true', function() { | ||
it('should return proper DatabaseInfo', function() { | ||
var info = ls.getDatabaseInfo(); | ||
var info = ls.databaseInfo; | ||
assert.ok(info instanceof DatabaseInfo); | ||
@@ -53,0 +53,0 @@ assert.equal(info.getType(), 428); |
181092
27
7009