forked from runk/node-maxmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (43 loc) · 1.29 KB
/
index.js
File metadata and controls
53 lines (43 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
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;
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) 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 = null;
try {
reader = new Reader(database, opts);
}
catch( readerErr ) {
return cb( readerErr );
}
if( reader == null ) {
return cb( "no_reader" );
}
return cb( null, reader );
});
};
exports.openSync = function(filepath, opts) {
var reader = new Reader(fs.readFileSync(filepath), opts);
if (opts && !!opts.watchForUpdates) {
fs.watch(filepath, function() {
reader.load(fs.readFileSync(filepath));
});
}
return reader;
};
exports.init = function() {
throw new Error(utils.legacyErrorMessage);
};
exports.validate = ip.validate;