Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

maxmind-db-reader

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

maxmind-db-reader - npm Package Compare versions

Comparing version 0.2.0 to 0.2.1

.editorconfig

25

index.js
'use strict';
var Reader = require('./lib/Reader.js'),
MaxmindDBReader;
var Reader = require('./lib/Reader.js');
MaxmindDBReader = module.exports = function () {
module.exports = MaxmindDBReader;
function MaxmindDBReader() {
// allow creation without 'new' keyword
if (!(this instanceof MaxmindDBReader))
return new MaxmindDBReader();
};
}
MaxmindDBReader.open = function(database,callback){
Reader.open(database,function(err, reader){
MaxmindDBReader.open = function openAsync(database, callback) {
Reader.open(database, function(err, reader){
if(err){

@@ -21,12 +22,12 @@ return callback(err);

});
}
};
MaxmindDBReader.openSync = function(database){
MaxmindDBReader.openSync = function openSync(database) {
var mmdbreader = MaxmindDBReader();
mmdbreader.reader = Reader.openSync(database);
return mmdbreader
}
return mmdbreader;
};
MaxmindDBReader.prototype.getGeoData = function getGeoData(ipAddress,callback) {
this.reader.get(ipAddress,callback);
MaxmindDBReader.prototype.getGeoData = function getGeoData(ipAddress, callback) {
this.reader.get(ipAddress, callback);
};

@@ -33,0 +34,0 @@

'use strict';
var bigInt = require('big-integer'),
Decoder;
var bigInt = require('big-integer');
Decoder = module.exports = function (fileStream, pointerBase) {
module.exports = Decoder;
function Decoder(fileStream, pointerBase) {
this.fileStream = fileStream;

@@ -28,5 +29,5 @@ this.pointerBase = pointerBase || 0;

this.pointerValueOffset = [0, 0, 2048, 526336, 0];
};
}
Decoder.prototype.decode = function decode(offset,callback,nexttick) {
Decoder.prototype.decode = function decode(offset, callback, nexttick) {
var tmp,

@@ -36,10 +37,10 @@ that = this,

type = this.types[ctrlByte >> 5]
;
;
if (type === 'pointer') {
tmp = this.decodePointer(ctrlByte, offset);
this.decode(tmp[0],function(err,data){
if(err) return callback(err);
this.decode(tmp[0], function (err, data) {
if (err) return callback(err);
callback(null, [data[0], tmp[1]]);
},nexttick);
}, nexttick);
return;

@@ -60,40 +61,40 @@ }

tmp = this.sizeFromCtrlByte(ctrlByte, offset);
if(nexttick === false)
this.decodeByType(type, tmp[1], tmp[0],callback);
if (nexttick === false)
this.decodeByType(type, tmp[1], tmp[0], callback);
else
process.nextTick(function(){
that.decodeByType(type, tmp[1], tmp[0],callback);
process.nextTick(function () {
that.decodeByType(type, tmp[1], tmp[0], callback);
});
};
Decoder.prototype.decodeByType = function decodeByType(type, offset, size,callback) {
Decoder.prototype.decodeByType = function decodeByType(type, offset, size, callback) {
var newOffset = offset + size,
bytes = this.read(offset, size)
;
;
switch (type) {
case 'map':
return this.decodeMap(size, offset,callback);
case 'array':
return this.decodeArray(size, offset,callback);
case 'boolean':
return callback(null,[this.decodeBoolean(size), offset]);
case 'utf8_string':
return callback(null,[this.decodeString(bytes), newOffset]);
case 'double':
return callback(null,[this.decodeDouble(bytes), newOffset]);
case 'float':
return callback(null,[this.decodeFloat(bytes), newOffset]);
case 'bytes':
return callback(null,[bytes, newOffset]);
case 'uint16':
return callback(null,[this.decodeUint16(bytes), newOffset]);
case 'uint32':
return callback(null,[this.decodeUint32(bytes), newOffset]);
case 'int32':
return callback(null,[this.decodeInt32(bytes), newOffset]);
case 'uint64':
return callback(null,[this.decodeUint64(bytes), newOffset]);
case 'uint128':
return callback(null,[this.decodeUint128(bytes), newOffset]);
case 'map':
return this.decodeMap(size, offset, callback);
case 'array':
return this.decodeArray(size, offset, callback);
case 'boolean':
return callback(null, [this.decodeBoolean(size), offset]);
case 'utf8_string':
return callback(null, [this.decodeString(bytes), newOffset]);
case 'double':
return callback(null, [this.decodeDouble(bytes), newOffset]);
case 'float':
return callback(null, [this.decodeFloat(bytes), newOffset]);
case 'bytes':
return callback(null, [bytes, newOffset]);
case 'uint16':
return callback(null, [this.decodeUint16(bytes), newOffset]);
case 'uint32':
return callback(null, [this.decodeUint32(bytes), newOffset]);
case 'int32':
return callback(null, [this.decodeInt32(bytes), newOffset]);
case 'uint64':
return callback(null, [this.decodeUint64(bytes), newOffset]);
case 'uint128':
return callback(null, [this.decodeUint128(bytes), newOffset]);
}

@@ -107,3 +108,3 @@ callback(new Error("MaxmindDBReader: Unknown or unexpected type: " + type + ' at offset:' + offset));

type = this.types[ctrlByte >> 5]
;
;

@@ -134,18 +135,31 @@ if (type === 'pointer') {

bytes = this.read(offset, size)
;
;
switch (type) {
case 'map': return this.decodeMapSync(size, offset);
case 'array': return this.decodeArraySync(size, offset);
case 'boolean': return [this.decodeBoolean(size), offset];
case 'utf8_string': return [this.decodeString(bytes), newOffset];
case 'double': return [this.decodeDouble(bytes), newOffset];
case 'float': return [this.decodeFloat(bytes), newOffset];
case 'bytes': return [bytes, newOffset];
case 'uint16': return [this.decodeUint16(bytes), newOffset];
case 'uint32': return [this.decodeUint32(bytes), newOffset];
case 'int32': return [this.decodeInt32(bytes), newOffset];
case 'uint64': return [this.decodeUint64(bytes), newOffset];
case 'uint128': return [this.decodeUint128(bytes), newOffset];
default: throw new Error("MaxmindDBReader: Unknown or unexpected type: " + type + ' at offset:' + offset);
case 'map':
return this.decodeMapSync(size, offset);
case 'array':
return this.decodeArraySync(size, offset);
case 'boolean':
return [this.decodeBoolean(size), offset];
case 'utf8_string':
return [this.decodeString(bytes), newOffset];
case 'double':
return [this.decodeDouble(bytes), newOffset];
case 'float':
return [this.decodeFloat(bytes), newOffset];
case 'bytes':
return [bytes, newOffset];
case 'uint16':
return [this.decodeUint16(bytes), newOffset];
case 'uint32':
return [this.decodeUint32(bytes), newOffset];
case 'int32':
return [this.decodeInt32(bytes), newOffset];
case 'uint64':
return [this.decodeUint64(bytes), newOffset];
case 'uint128':
return [this.decodeUint128(bytes), newOffset];
default:
throw new Error("MaxmindDBReader: Unknown or unexpected type: " + type + ' at offset:' + offset);
}

@@ -178,3 +192,3 @@ };

decoded = this.decodeUint32(bytes)
;
;

@@ -196,3 +210,3 @@ if (size === 29) {

buffer = this.read(offset, pointerSize)
;
;

@@ -208,10 +222,10 @@ offset += pointerSize;

Decoder.prototype.decodeArray = function decodeArray(size, offset,callback) {
Decoder.prototype.decodeArray = function decodeArray(size, offset, callback) {
var that = this;
process.nextTick(function(){
process.nextTick(function () {
var tmp,
i = 1,
array = [],
cb = function(err,tmp){
if(err){
cb = function (err, tmp) {
if (err) {
return callback(err);

@@ -221,10 +235,10 @@ }

array.push(tmp[0]);
if(i++ < size){
that.decode(offset,cb,i%20===0);
}else{
callback(null, [array,offset]);
if (i++ < size) {
that.decode(offset, cb, i % 20 === 0);
} else {
callback(null, [array, offset]);
}
};
if(size === 0) callback(null,[[],offset]);
that.decode(offset,cb,false);
if (size === 0) callback(null, [[], offset]);
that.decode(offset, cb, false);
});

@@ -237,5 +251,5 @@ };

array = []
;
;
for(i; i < size; i++) {
for (i; i < size; i++) {
tmp = this.decodeSync(offset);

@@ -261,10 +275,10 @@ offset = tmp[1];

Decoder.prototype.decodeMap = function decodeMap(size, offset,callback) {
Decoder.prototype.decodeMap = function decodeMap(size, offset, callback) {
var that = this;
process.nextTick(function(){
process.nextTick(function () {
var tmp, key,
map = {},
i = 1,
cb = function(err,tmp){
if(err){
cb = function (err, tmp) {
if (err) {
return callback(err);

@@ -274,14 +288,14 @@ }

offset = tmp[1];
if(i++ < size){
if (i++ < size) {
tmp = that.decodeSync(offset);
key = tmp[0].toString();
that.decode(tmp[1],cb,i%20===0);
}else{
callback(null, [map,offset]);
that.decode(tmp[1], cb, i % 20 === 0);
} else {
callback(null, [map, offset]);
}
};
if(size === 0) return callback(null,[{},offset]);
if (size === 0) return callback(null, [{}, offset]);
tmp = that.decodeSync(offset);
key = tmp[0].toString();
that.decode(tmp[1],cb,false);
that.decode(tmp[1], cb, false);
});

@@ -294,3 +308,3 @@ };

i = 0
;
;

@@ -338,3 +352,3 @@ for (i; i < size; i++) {

numberOfLongs = size / 4
;
;

@@ -341,0 +355,0 @@ buffer = new Buffer(size);

@@ -1,44 +0,49 @@

var ipp = module.exports = function(ip){
if(ip.indexOf('.')!==-1){
return ipp.parseIPv4(ip);
}else{
return ipp.parseIPv6(ip);
}
'use strict';
var IPAddress = require('./IPAddress');
module.exports = IPParser;
function IPParser(ip) {
if (ip.indexOf('.') !== -1) {
return IPParser.parseIPv4(ip);
} else {
return IPParser.parseIPv6(ip);
}
}
IPParser.parseIPv4 = function parseIPv4(ip) {
return ipv4Buffer(IPAddress.parseIPv4(ip));
};
ipp.invalidIPv4 = /[^0-9\.]/i
ipp.parseIPv4 = function(ipstr){
if(ipp.invalidIPv4.test(ipstr)){
throw new Error("Invalid IPv4 address");
}
var arr = new Buffer(4);
arr.fill(0);
ipstr.split('.').forEach(function(nr,i){
arr[i] = parseInt(nr);
});
return arr;
IPParser.parseIPv6 = function parseIPv6(ip) {
return ipv6Buffer(IPAddress.parseIPv6(ip));
};
var repeat = function(c,l){
var str="",i=0;
while(i++ < l)str+=c;
return str;
function ipv4Buffer(groups) {
var arr = new Buffer(4);
arr.fill(0);
groups.forEach(function part(nr, i) {
arr[i] = parseInt(nr);
});
return arr;
}
ipp.invalidIPv6 = /[^0-9a-f\:]/i;
ipp.parseIPv6 = function(ipstr){
if(ipp.invalidIPv6.test(ipstr)){
throw new Error("Invalid IPv6 address");
}
var arr = new Buffer(16);
arr.fill(0);
var l = ipstr.split(':').length;
if(l < 8) ipstr = ipstr.replace('::',repeat(':',10 - l));
ipstr.split(':').forEach(function(hex,i){
if(hex=="")return;
if(hex.length < 4){
hex = repeat('0',4 - hex.length) + hex;
}
arr.write(hex,i*2,'hex');
});
return arr;
};
function ipv6Buffer(groups) {
var arr = new Buffer(16);
arr.fill(0);
groups.forEach(function part(hex, i) {
if (hex == "") return;
if (hex.length < 4) {
hex = repeat('0', 4 - hex.length) + hex;
}
arr.write(hex, i * 2, 'hex');
});
return arr;
}
function repeat(c, l) {
var str = "", i = 0;
while (i++ < l)str += c;
return str;
}
'use strict';
var Metadata;
module.exports = Metadata;
Metadata = module.exports = function (metadata) {
function Metadata(metadata) {
this.binaryFormatMajorVersion = metadata.binary_format_major_version;

@@ -17,3 +17,3 @@ this.binaryFormatMinorVersion = metadata.binary_format_minor_version;

this.searchTreeSize = this.nodeCount * this.nodeByteSize;
};
}

@@ -20,0 +20,0 @@ Metadata.prototype.getBinaryFormatMajorVersion = function getBinaryFormatMajorVersion() {

'use strict';
var DATA_SECTION_SEPARATOR_SIZE = 16,
METADATA_START_MARKER = new Buffer('ABCDEF4D61784D696E642E636F6D','hex'),
fs = require('fs'),
Metadata = require('./Metadata.js'),
Decoder = require('./Decoder.js'),
IPParser = require('./IPParser.js'),
Reader;
METADATA_START_MARKER = new Buffer('ABCDEF4D61784D696E642E636F6D', 'hex'),
fs = require('fs'),
Metadata = require('./Metadata.js'),
Decoder = require('./Decoder.js'),
IPParser = require('./IPParser.js');
Reader = module.exports = function () {};
module.exports = Reader;
Reader.open = function(database, callback){
function Reader() {
}
Reader.open = function openAsync(database, callback) {
var reader = new Reader();
var start, metadataDecoder, metadataArray;
fs.readFile(database,function(err, data){
if(err){
callback && callback(err);
return;
var start, metadataDecoder;
fs.readFile(database, function db(err, data) {
if (err) {
return (callback && callback(err));
}
reader.fileHandle =data;
reader.fileHandle = data;
start = reader.findMetadataStart(reader.fileHandle);
metadataDecoder = new Decoder(reader.fileHandle, 0);
metadataDecoder.decode(start,function(err,metadata){
metadataDecoder.decode(start, function decoded(err, metadata) {
if (err) {
return (callback && callback(err, null));
}
reader.metadata = new Metadata(metadata[0]);
reader.decoder = new Decoder(reader.fileHandle, reader.metadata.getSearchTreeSize() + DATA_SECTION_SEPARATOR_SIZE);
callback && callback(null,reader);
reader.decoder = new Decoder(reader.fileHandle, reader.metadata.getSearchTreeSize() + DATA_SECTION_SEPARATOR_SIZE);
callback && callback(null, reader);
});
});
}
};
Reader.openSync = function (database) {
Reader.openSync = function openSync(database) {
var reader = new Reader();

@@ -42,3 +46,3 @@ var start, metadataDecoder, metadataArray;

reader.metadata = new Metadata(metadataArray[0]);
reader.decoder = new Decoder(reader.fileHandle, reader.metadata.getSearchTreeSize() + DATA_SECTION_SEPARATOR_SIZE);
reader.decoder = new Decoder(reader.fileHandle, reader.metadata.getSearchTreeSize() + DATA_SECTION_SEPARATOR_SIZE);
return reader;

@@ -51,7 +55,6 @@ };

fsize = file.length - 1
;
while (found <= mlen && fsize--) {
;
while (found <= mlen && fsize-- > 0) {
found += (file[fsize] === METADATA_START_MARKER[mlen - found]) ? 1 : -found;
}
return fsize + found;

@@ -65,11 +68,11 @@ };

Reader.prototype.get = function get(ipAddress,callback) {
Reader.prototype.get = function get(ipAddress, callback) {
var pointer = this.findAddressInTree(ipAddress);
if(pointer === 0){
process.nextTick(function(){
callback(null,null);
if (pointer === 0) {
process.nextTick(function () {
callback(null, null);
});
}else{
this.resolveDataPointer(pointer,callback);
};
} else {
this.resolveDataPointer(pointer, callback);
}
};

@@ -86,3 +89,3 @@

len = countRaw * 8 + ipStartBit
;
;

@@ -117,3 +120,3 @@ for (i; i < len; i++) {

baseOffset = nodeNumber * this.metadata.getNodeByteSize()
;
;

@@ -135,3 +138,3 @@ buffer.fill(0);

case 32:
return bytes.readUInt32BE(baseOffset + index * 4, true);
return this.fileHandle.readUInt32BE(baseOffset + index * 4, true);
default:

@@ -148,8 +151,8 @@ throw new Error("MaxmindDBReader: Unknown Recordsize in DB");

Reader.prototype.resolveDataPointer = function resolveDataPointer(pointer,callback) {
Reader.prototype.resolveDataPointer = function resolveDataPointer(pointer, callback) {
var resolved = pointer - this.metadata.getNodeCount() + this.metadata.getSearchTreeSize();
this.decoder.decode(resolved,function(err,data){
if(err) return callback(err);
callback(null,data[0]);
this.decoder.decode(resolved, function (err, data) {
if (err) return callback(err);
callback(null, data[0]);
});

@@ -156,0 +159,0 @@ };

{
"name": "maxmind-db-reader",
"version": "0.2.0",
"description": "This is the pure Node API for reading MaxMind DB files. MaxMind DB is a binary file format that stores data indexed by IP address subnets (IPv4 or IPv6).",
"main": "./index.js",
"contributors": [
{
"name": "Patrick Klös",
"email": "pkloes@web.de"
"name": "maxmind-db-reader",
"version": "0.2.1",
"description": "This is the pure Node API for reading MaxMind DB files. MaxMind DB is a binary file format that stores data indexed by IP address subnets (IPv4 or IPv6).",
"main": "./index.js",
"contributors": [
{
"name": "Patrick Klös",
"email": "pkloes@web.de"
},
{
"name": "Corné 'EaterOfCode' Oppelaar",
"email": "hello@eaterofco.de"
},
{
"name": "Rajesh Segu",
"email": "rajesh.segu@gmail.com"
}
],
"repository": {
"type": "git",
"url": "https://github.com/PaddeK/node-maxmind-db"
},
{
"name": "Corné 'EaterOfCode' Oppelaar",
"email": "hello@eaterofco.de"
"bin": {
"mmdb-geoip": "./repl"
},
"keywords": [
"Maxmind",
"Maxmind DB",
"Maxmind DB Reader",
"GeoIP2",
"GeoIP2 Lite"
],
"author": {
"name": "Patrick Klös",
"email": "pkloes@web.de"
},
"dependencies": {
"big-integer": ">=1.1.5",
"ip-address": "4.0.0"
},
"devDependencies": {
"istanbul": "^0.3.13",
"jshint": "2.4.2",
"path": "^0.11.14",
"scotch-tape": "0.2.0"
},
"scripts": {
"cover": "istanbul cover --report cobertura --print detail tape -- test/test.js",
"cover-html": "istanbul report html",
"fast-test": "tape test/test.js",
"test": "npm run cover -s"
},
"licenses": [
{
"type": "LGPL 2.1",
"url": "https://github.com/PaddeK/node-maxmind-db/blob/master/LICENSE"
}
],
"engine": {
"node": ">=0.8.0"
}
],
"repository": {
"type": "git",
"url": "https://github.com/PaddeK/node-maxmind-db"
},
"bin": {
"mmdb-geoip": "./repl"
},
"keywords": [
"Maxmind",
"Maxmind DB",
"Maxmind DB Reader",
"GeoIP2",
"GeoIP2 Lite"
],
"author": {
"name": "Patrick Klös",
"email": "pkloes@web.de"
},
"dependencies": {
"big-integer": ">=1.1.5"
},
"licenses": [
{
"type": "LGPL 2.1",
"url": "https://github.com/PaddeK/node-maxmind-db/blob/master/LICENSE"
}
],
"engine": {
"node": ">=0.8.0"
}
}

@@ -13,3 +13,3 @@ # node-maxmind-db

npm i https://github.com/EaterOfCode/node-maxmind-db/archive/master.tar.gz
npm i maxmind-db-reader

@@ -146,5 +146,6 @@ # Example

* `.openSync (databasePath) maxmind-db-reader`
`maxmind-db-reader()`
`maxmind-db-reader()`
* `.getGeoData (ip, callback(err,geodata)) void`
* `.getGeoDataSync (ip) geodata`
* `.getDatabaseMetadata() metadata`

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc