Skip to content

Commit

Permalink
A check for gzip format
Browse files Browse the repository at this point in the history
  • Loading branch information
runk committed Mar 7, 2017
1 parent 9fc2ff7 commit a59aa88
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Expand Up @@ -4,6 +4,7 @@ var assert = require('assert');
var fs = require('fs');
var Reader = require('./lib/reader');
var ip = require('./lib/ip');
var isGzip = require('./lib/is-gzip');
var utils = require('./lib/utils');

exports.Reader = Reader;
Expand All @@ -15,6 +16,9 @@ exports.open = function(filepath, opts, cb) {

fs.readFile(filepath, function(err, database) {
if (err) return cb(err);
if (isGzip(database)) {
return cb(new Error('Looks like you are passing in a file in gzip format, please use mmdb database instead.'));
}
var reader = new Reader(database, opts);
if (opts && !!opts.watchForUpdates) {
fs.watch(filepath, function() {
Expand Down
9 changes: 9 additions & 0 deletions lib/is-gzip.js
@@ -0,0 +1,9 @@
'use strict';

module.exports = function(buf) {
if (!buf || buf.length < 3) {
return false;
}

return buf[0] === 0x1f && buf[1] === 0x8b && buf[2] === 0x08;
};
Binary file added test/databases/GeoIP2-City-Test.mmdb.gz
Binary file not shown.
9 changes: 9 additions & 0 deletions test/index.js
Expand Up @@ -84,6 +84,15 @@ describe('index', function() {
}, /Callback function must be provided/);
});

it('should return an error when gzip file attempted', function(done) {
var dbPath = path.join(__dirname, 'databases/GeoIP2-City-Test.mmdb.gz');
maxmind.open(dbPath, function(err) {
assert.equal(err.message,
'Looks like you are passing in a file in gzip format, please use mmdb database instead.');
done();
});
});

it('should check for an error when cannot read database on update', function(done) {
var counter = 0;
var cb = function(err, reader) {
Expand Down
21 changes: 21 additions & 0 deletions test/is-gzip.js
@@ -0,0 +1,21 @@
'use strict';

var assert = require('assert');
var isGzip = require('../lib/is-gzip');

describe('lib/is-gzip', function() {
it('should return false for short buffers', function() {
assert.equal(isGzip(new Buffer([1, 2])), false);
});

it('should return false for string buffer', function() {
assert.equal(isGzip(new Buffer('kraken')), false);
});

it('should return false for string buffer', function() {
// gzipped "kraken" string
// shell: `echo "kraken" | gzip | base64`
var buffer = new Buffer('H4sIAGBDv1gAA8suSsxOzeMCAKjj9U8HAAAA', 'base64');
assert.equal(isGzip(buffer), true);
});
});

0 comments on commit a59aa88

Please sign in to comment.