Skip to content

Commit

Permalink
ipv6 parser simplifications (#41)
Browse files Browse the repository at this point in the history
* ipv6 parser simplifications

* fix the problem, customize test script

* readme - mention that module is fastest available
  • Loading branch information
runk committed Jun 1, 2016
1 parent 43bb966 commit f7aa931
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -2,7 +2,7 @@ node-maxmind [![Build Status](https://travis-ci.org/runk/node-maxmind.png)](http
========

Pure Javascript module for Geo IP lookup using Maxmind binary databases (aka mmdb or geoip2).
Up to [8000% faster](https://github.com/runk/node-maxmind-benchmark) than other Geo lookup libraries. Module natively works with binary Maxmind database format and doesn't require any "CSV - {specific lib format}" conversions as some other modules do. Maxmind binary databases are highly optimized for size and performance so there's no point working with other than that format.
Fastest Maxmind lookup library available - up to [8000% faster](https://github.com/runk/node-maxmind-benchmark) than other libraries. Module has 100% test coverage with comprehensive test suite. It natively works with binary Maxmind database format and doesn't require any "CSV - {specific lib format}" conversions as some other modules do. Maxmind binary databases are highly optimized for size and performance so there's no point working with other than that format.


## GEO databases
Expand Down
24 changes: 9 additions & 15 deletions lib/ip.js
Expand Up @@ -21,22 +21,16 @@ var parseIPv6 = function(ip) {
var parsed;
var chunk;

var hex = function(v) {
v = parseInt(v, 10).toString(16);
return (v.length == 2) ? v : '0' + v;
};

// ipv4 e.g. `::ffff:64.17.254.216`
if (ip.indexOf('::') === 0) {
if (ip.indexOf('.') > -1) {
var ipStartsAt = 2;
if (ip.indexOf('::ffff') > -1) {
ipStartsAt = 7;
addr[10] = addr[11] = 255;
}

ip = ip.substring(ipStartsAt).split('.', 4);
for (i = 0; i < ip.length; i++) {
addr[16 - (4 - i)] = parseInt(ip[i], 10);
}

return addr;
}
if (ip.indexOf('.') > -1) {
ip = ip.replace(/(\d+)\.(\d+)\.(\d+)\.(\d+)/, function(match, a, b, c, d) {
return hex(a) + hex(b) + ':' + hex(c) + hex(d);
});
}

var parts = ip.split('::', 2),
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -42,7 +42,7 @@
"main": "index.js",
"engines": {
"node": ">=0.8.0",
"npm": "1"
"npm": ">=1"
},
"license": "MIT",
"scripts": {
Expand All @@ -54,6 +54,7 @@
"release:major": "npm run lint && npm test && npm version major && npm publish && git push --follow-tags",
"release:minor": "npm run lint && npm test && npm version minor && npm publish && git push --follow-tags",
"release:patch": "npm run lint && npm test && npm version patch && npm publish && git push --follow-tags",
"test": "mocha -R spec --recursive --bail"
"mocha": "mocha -R spec --recursive --bail",
"test": "npm run lint && npm run coverage && npm run coverage:check"
}
}
10 changes: 6 additions & 4 deletions test/ip.js
Expand Up @@ -44,10 +44,12 @@ describe('lib/ip', function() {
});

it('should parse ipv4 with `::ffff`', function() {
var expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 64, 17, 254, 216];
assert.deepEqual(ip.parse('::ffff:64.17.254.216'), expected);
assert.deepEqual(ip.parse('::ffff:4011:fed8'), expected);
assert.deepEqual(ip.parse('0000:0000:0000:0000:0000:ffff:4011:fed8'), expected);
var expected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 1, 2, 254, 216];
assert.deepEqual(ip.parse('::ffff:1.2.254.216'), expected);
assert.deepEqual(ip.parse('::ffff:0102:fed8'), expected);
assert.deepEqual(ip.parse('::ffff:102:fed8'), expected);
assert.deepEqual(ip.parse('0000:0000:0000:0000:0000:ffff:0102:fed8'), expected);
assert.deepEqual(ip.parse('0000:0000:0000:0000:0000:ffff:102:fed8'), expected);
});

it('should parse ipv4 with `::`', function() {
Expand Down

0 comments on commit f7aa931

Please sign in to comment.