From f7aa9314acb68a512437ffb85b784592c6af7040 Mon Sep 17 00:00:00 2001 From: Dmitry Shirokov Date: Wed, 1 Jun 2016 23:28:02 +1000 Subject: [PATCH] ipv6 parser simplifications (#41) * ipv6 parser simplifications * fix the problem, customize test script * readme - mention that module is fastest available --- README.md | 2 +- lib/ip.js | 24 +++++++++--------------- package.json | 5 +++-- test/ip.js | 10 ++++++---- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 3f3fe7b4..d80775bc 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/ip.js b/lib/ip.js index d079c770..e5fe062c 100755 --- a/lib/ip.js +++ b/lib/ip.js @@ -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), diff --git a/package.json b/package.json index 6c23461f..eae1f5fe 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "main": "index.js", "engines": { "node": ">=0.8.0", - "npm": "1" + "npm": ">=1" }, "license": "MIT", "scripts": { @@ -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" } } diff --git a/test/ip.js b/test/ip.js index 08a3915f..05ee7683 100755 --- a/test/ip.js +++ b/test/ip.js @@ -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() {