Comparing version 1.3.0 to 2.0.0
17
index.js
'use strict'; | ||
var assert = require('assert'); | ||
var fs = require('fs'); | ||
var Reader = require('./lib/reader'); | ||
@@ -7,6 +9,17 @@ var ip = require('./lib/ip'); | ||
exports.open = function(database, opts) { | ||
return new Reader(database, opts); | ||
exports.open = function(filepath, opts, cb) { | ||
if (!cb) cb = opts; | ||
assert.equal(typeof cb, 'function', 'Callback function must be provided. \ | ||
If you want to open library synchronously, use maxmind.openSync function.'); | ||
fs.readFile(filepath, function(err, database) { | ||
if (err) cb(err); | ||
else cb(null, new Reader(database, opts)); | ||
}); | ||
}; | ||
exports.openSync = function(filepath, opts) { | ||
return new Reader(fs.readFileSync(filepath), opts); | ||
}; | ||
exports.init = function() { | ||
@@ -13,0 +26,0 @@ throw new Error(utils.legacyErrorMessage); |
'use strict'; | ||
var fs = require('fs'); | ||
var LRU = require('lru-cache'); | ||
@@ -15,3 +14,3 @@ var Metadata = require('./metadata'); | ||
function Reader(database, opts) { | ||
function Reader(db, opts) { | ||
opts = opts || {}; | ||
@@ -24,3 +23,3 @@ opts.cache = opts.cache || {}; | ||
this.db = fs.readFileSync(database); | ||
this.db = db; | ||
@@ -64,2 +63,3 @@ this.metadata = new Metadata(this.db); | ||
// occupy the first 32-bits of the address space (from 0 to 2**32 - 1). | ||
// Which means they're padded with zeros. | ||
var ipStartBit = (this.metadata.ipVersion === 6 && rawAddress.length === 4) ? 128 - 32 : 0; | ||
@@ -77,5 +77,2 @@ | ||
// When storing IPv4 addresses in an IPv6 tree, they are stored as-is, so they | ||
// occupy the first 32-bits of the address space (from 0 to 2**32 - 1). | ||
// Which means they're padded with zeros. | ||
for (var i = ipStartBit; i < this.metadata.treeDepth; i++) { | ||
@@ -82,0 +79,0 @@ bit = ipUtil.bitAt(rawAddress, i - ipStartBit); |
{ | ||
"name": "maxmind", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/runk/node-maxmind", | ||
@@ -5,0 +5,0 @@ "description": "IP lookup using Maxmind databases", |
@@ -79,5 +79,9 @@ node-maxmind [![Build Status](https://travis-ci.org/runk/node-maxmind.png)](https://travis-ci.org/runk/node-maxmind) | ||
- GeoLite2 Free Downloadable Databases http://dev.maxmind.com/geoip/geoip2/geolite2/ | ||
- Great talk about V8 performance https://www.youtube.com/watch?v=UJPdhx5zTaw | ||
- V8 Optimization killers https://github.com/petkaantonov/bluebird/wiki/Optimization-killers | ||
- More V8 performance tips http://www.html5rocks.com/en/tutorials/speed/v8/ | ||
## License | ||
MIT |
@@ -34,7 +34,7 @@ 'use strict'; | ||
it('should successfully handle database', function() { | ||
assert(maxmind.open(path.join(dataDir, 'GeoIP2-City-Test.mmdb'))); | ||
assert(maxmind.openSync(path.join(dataDir, 'GeoIP2-City-Test.mmdb'))); | ||
}); | ||
it('should fetch geo ip', function() { | ||
var geoIp = maxmind.open(path.join(dataDir, 'GeoIP2-City-Test.mmdb')); | ||
var geoIp = maxmind.openSync(path.join(dataDir, 'GeoIP2-City-Test.mmdb')); | ||
var data = actual('GeoIP2-City-Test.json'); | ||
@@ -56,3 +56,3 @@ assert.deepEqual(geoIp.get('1.1.1.1'), null); | ||
assert.throws(function verify() { | ||
maxmind.open('./data/README.md'); | ||
maxmind.openSync('./data/README.md'); | ||
}); | ||
@@ -62,3 +62,3 @@ }); | ||
it('should accept cache options', function() { | ||
assert(maxmind.open(path.join(dataDir, 'GeoIP2-City-Test.mmdb'), { | ||
assert(maxmind.openSync(path.join(dataDir, 'GeoIP2-City-Test.mmdb'), { | ||
cache: { max: 1000 } | ||
@@ -71,3 +71,3 @@ })); | ||
it('should decode all possible types - complex', function() { | ||
var geoIp = maxmind.open(path.join(dataDir, 'MaxMind-DB-test-decoder.mmdb')); | ||
var geoIp = maxmind.openSync(path.join(dataDir, 'MaxMind-DB-test-decoder.mmdb')); | ||
assert.deepEqual(geoIp.get('::1.1.1.1'), { | ||
@@ -91,3 +91,3 @@ array: [1, 2, 3], | ||
it('should decode all possible types - zero/empty values', function() { | ||
var geoIp = maxmind.open(path.join(dataDir, 'MaxMind-DB-test-decoder.mmdb')); | ||
var geoIp = maxmind.openSync(path.join(dataDir, 'MaxMind-DB-test-decoder.mmdb')); | ||
assert.deepEqual(geoIp.get('::0.0.0.0'), { | ||
@@ -110,3 +110,3 @@ array: [], | ||
it('should return correct value: string entries', function() { | ||
var geoIp = maxmind.open(path.join(dataDir, 'MaxMind-DB-string-value-entries.mmdb')); | ||
var geoIp = maxmind.openSync(path.join(dataDir, 'MaxMind-DB-string-value-entries.mmdb')); | ||
assert.equal(geoIp.get('1.1.1.1'), '1.1.1.1/32'); | ||
@@ -145,3 +145,3 @@ assert.equal(geoIp.get('1.1.1.2'), '1.1.1.2/31'); | ||
it('should test everything: ' + file, function() { | ||
var geoIp = maxmind.open(path.join(dataDir, '/' + file + '.mmdb')); | ||
var geoIp = maxmind.openSync(path.join(dataDir, '/' + file + '.mmdb')); | ||
var data = actual(file + '.json'); | ||
@@ -148,0 +148,0 @@ tester(geoIp, data); |
'use strict'; | ||
var assert = require('assert'); | ||
var path = require('path'); | ||
var maxmind = require('../index'); | ||
@@ -8,2 +9,5 @@ | ||
describe('index', function() { | ||
var dataDir = path.join(__dirname, 'data/test-data'); | ||
var dbPath = path.join(dataDir, 'GeoIP2-City-Test.mmdb'); | ||
describe('validate()', function() { | ||
@@ -24,2 +28,40 @@ it('should work fine for both IPv4 and IPv6', function() { | ||
}); | ||
describe('open()', function() { | ||
it('should successfully handle database', function(done) { | ||
maxmind.open(dbPath, function(err, lookup) { | ||
if (err) return done(err); | ||
assert(lookup.get('2001:230::')); | ||
done(); | ||
}); | ||
}); | ||
it('should successfully handle database, with opts', function(done) { | ||
maxmind.open(dbPath, { cache: { max: 1000 } }, function(err, lookup) { | ||
if (err) return done(err); | ||
assert(lookup.get('2001:230::')); | ||
done(); | ||
}); | ||
}); | ||
it('should successfully handle errors while opening a db', function(done) { | ||
maxmind.open('/foo/bar', function(err) { | ||
assert.equal(err.code, 'ENOENT'); | ||
done(); | ||
}); | ||
}); | ||
it('should throw an error when no callback provided', function() { | ||
assert.throws(function() { | ||
maxmind.open(dbPath); | ||
}, /Callback function must be provided/); | ||
}); | ||
}); | ||
describe('openSync()', function() { | ||
it('should successfully handle database', function() { | ||
var lookup = maxmind.openSync(dbPath); | ||
assert(lookup.get('2001:230::')); | ||
}); | ||
}); | ||
}); |
'use strict'; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
@@ -11,2 +12,5 @@ var assert = require('assert'); | ||
var dataDir = path.join(__dirname, 'data/test-data'); | ||
var read = function(dir, filepath) { | ||
return fs.readFileSync(path.join(dir, filepath)); | ||
}; | ||
@@ -16,3 +20,3 @@ describe('findAddressInTree()', function() { | ||
it('should work for most basic case', function() { | ||
var reader = new Reader(path.join(dataDir, 'GeoIP2-City-Test.mmdb')); | ||
var reader = new Reader(read(dataDir, 'GeoIP2-City-Test.mmdb')); | ||
assert.equal(reader.findAddressInTree('1.1.1.1'), null); | ||
@@ -22,3 +26,3 @@ }); | ||
it('should return correct value: city database', function() { | ||
var reader = new Reader(path.join(dataDir, 'GeoIP2-City-Test.mmdb')); | ||
var reader = new Reader(read(dataDir, 'GeoIP2-City-Test.mmdb')); | ||
assert.equal(reader.findAddressInTree('1.1.1.1'), null); | ||
@@ -36,3 +40,3 @@ assert.equal(reader.findAddressInTree('175.16.199.1'), 3042); | ||
it('should return correct value: string entries', function() { | ||
var reader = new Reader(path.join(dataDir, 'MaxMind-DB-string-value-entries.mmdb')); | ||
var reader = new Reader(read(dataDir, 'MaxMind-DB-string-value-entries.mmdb')); | ||
assert.equal(reader.findAddressInTree('1.1.1.1'), 98); | ||
@@ -84,3 +88,3 @@ assert.equal(reader.findAddressInTree('1.1.1.2'), 87); | ||
it('should return correct value: ' + file, function() { | ||
var reader = new Reader(path.join(dataDir, '' + file)); | ||
var reader = new Reader(read(dataDir, '' + file)); | ||
for (var ip in ips) { | ||
@@ -96,3 +100,3 @@ assert.equal(reader.findAddressInTree(ip), ips[ip], 'IP: ' + ip); | ||
it('should behave fine when there is no ipv4 search tree', function() { | ||
var reader = new Reader(path.join(dataDir, 'MaxMind-DB-no-ipv4-search-tree.mmdb')); | ||
var reader = new Reader(read(dataDir, 'MaxMind-DB-no-ipv4-search-tree.mmdb')); | ||
assert.equal(reader.findAddressInTree('::1:ffff:ffff'), 80); | ||
@@ -105,3 +109,3 @@ // TODO: perhaps null should be returned here, note that pointer is larger than file itself | ||
// TODO: find out in what way the file is broken | ||
var reader = new Reader(path.join(dataDir, 'MaxMind-DB-test-broken-search-tree-24.mmdb')); | ||
var reader = new Reader(read(dataDir, 'MaxMind-DB-test-broken-search-tree-24.mmdb')); | ||
assert.equal(reader.findAddressInTree('1.1.1.1'), 102); | ||
@@ -115,3 +119,3 @@ assert.equal(reader.findAddressInTree('1.1.1.2'), 90); | ||
assert.throws(function() { | ||
new Reader(path.join(__dirname, 'databases/legacy.dat')); | ||
new Reader(read(path.join(__dirname, 'databases'), 'legacy.dat')); | ||
}, /Maxmind v1 module has changed API/); | ||
@@ -122,3 +126,3 @@ }); | ||
assert.throws(function() { | ||
new Reader(path.join(__dirname, 'databases/broken.dat')); | ||
new Reader(read(path.join(__dirname, 'databases'), 'broken.dat')); | ||
}, /Cannot parse binary database/); | ||
@@ -125,0 +129,0 @@ }); |
2016765
62592
87
4