Skip to content

Commit

Permalink
Make it cross-platform and fix directory problem
Browse files Browse the repository at this point in the history
  • Loading branch information
corollari committed Jul 14, 2020
1 parent a59ed92 commit 621db0a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
16 changes: 9 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs = require('fs')
import path = require('path')
import utils = require('./utils')
const params = require('./params') as {
"LOCATION_RECORD_SIZE": number,
Expand All @@ -8,6 +9,7 @@ const params = require('./params') as {
var cacheEnabled = false;
const ipCache:{[filename:string]:ipBlockRecord[]|indexFile} = {}
var locationCache:Promise<locationRecord[]>;
const DATA_DIR = path.join(path.dirname(__dirname), "data");

function enableCache(){
if(!cacheEnabled){
Expand All @@ -22,12 +24,12 @@ function enableCache(){
type indexFile = number[]
type ipBlockRecord = [number, number|null, number, number, number]

function readFile<format extends (indexFile|ipBlockRecord[]|locationRecord[])>(path:string):Promise<format>{
if(cacheEnabled && ipCache[path] != undefined){
return Promise.resolve(ipCache[path] as format);
function readFile<format extends (indexFile|ipBlockRecord[]|locationRecord[])>(filename:string):Promise<format>{
if(cacheEnabled && ipCache[filename] != undefined){
return Promise.resolve(ipCache[filename] as format);
}
return new Promise(function (resolve, reject){
fs.readFile("data/"+path, function (err, data) {
fs.readFile(path.join(DATA_DIR, filename), function (err, data) {
if(err){
reject(err);
} else if(data==undefined){
Expand All @@ -36,7 +38,7 @@ function readFile<format extends (indexFile|ipBlockRecord[]|locationRecord[])>(p
const content = JSON.parse(data.toString())
resolve(content)
if(cacheEnabled){
ipCache[path] = content;
ipCache[filename] = content;
}
}
})
Expand All @@ -45,9 +47,9 @@ function readFile<format extends (indexFile|ipBlockRecord[]|locationRecord[])>(p

type locationRecord = [string, string, string, number, string, "0" | "1"]

function readFileChunk(path:string, offset:number, length:number): Promise<locationRecord>{
function readFileChunk(filename:string, offset:number, length:number): Promise<locationRecord>{
return new Promise(function (resolve, reject){
fs.open("data/"+path, 'r', function (err, fd){
fs.open(path.join(DATA_DIR, filename), 'r', function (err, fd){
if (err) reject(err);
const buf = Buffer.alloc == undefined? new Buffer(length) : Buffer.alloc(length)
fs.read(fd, buf, 0, length, offset, function(err, _, buffer){
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fast-geoip",
"version": "1.0.1",
"version": "1.0.2",
"description": "A faster & low-memory replacement for geoip-lite, a node library that maps IPs to geographical information",
"directories": {
"test": "tests"
Expand Down
9 changes: 8 additions & 1 deletion processGeoIpCsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@

RAW_DATABASE_DIR = "raw"
DATA_DIR = "data"
CODE_DIR = "build"
PARAMS_FILE = os.path.join(CODE_DIR, "params.js")
BLOCK_SIZE = 2**12 # = 4KB, the storage block size on almost all new OSes
FILE_SIZE = BLOCK_SIZE*12 - 100 # File size is made to be lower than the size of 12 storage blocks (minus a few bytes to account for overheads) in order to make sure that all the file's contents are directly addressed from the file's inode (preventing indirect access to storage blocks)

def removeOldData():
shutil.rmtree(DATA_DIR, ignore_errors=True) # Clean directory
os.mkdir(DATA_DIR)
try:
os.mkdir(CODE_DIR)
os.remove(PARAMS_FILE)
except (FileExistsError, FileNotFoundError):
pass

def jsonify(item):
return json.dumps(item).encode('utf-8')
Expand Down Expand Up @@ -112,7 +119,7 @@ def generateIndexes(ipIndex):
return MID_NODES

def storeDynamicParams(location_record_length, num_mid_nodes):
with open("params.js", "w") as params_file:
with open(PARAMS_FILE, "w") as params_file:
params = {
"LOCATION_RECORD_SIZE": location_record_length,
"NUMBER_NODES_PER_MIDINDEX": num_mid_nodes
Expand Down
11 changes: 5 additions & 6 deletions tests/lookup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const exec = require('child_process').execSync;

// Setup
process.chdir(__dirname + "/mock_data")
exec("python ../../processGeoIpCsv.py")
exec("python ../../processGeoIpCsv.py") // Also generates build/params.js
exec("npm run build")
exec("cp ../../build/index.js ../../build/utils.js .")
exec("cp ../../build/index.js ../../build/utils.js build") // Files on the "files" field of package.json, the files that will be included in the package

const lookup = require("./mock_data/index")
const utils = require("./mock_data/utils")
const lookup = require("./mock_data/build/index")
const utils = require("./mock_data/build/utils")

// ipStr2Num converts ips properly
assert.strictEqual(utils.ipStr2Num("0.0.0.0"), 0)
Expand Down Expand Up @@ -106,8 +106,7 @@ testRandomIps(1e3)

// Tear-down
function tearDown() {
exec("rm -r data")
exec("rm index.js utils.js params.js")
exec("rm -r data build")
}

Promise.all(promises).then(tearDown)
6 changes: 2 additions & 4 deletions tests/testCsvProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def createBlocksFile():
"GeoLite2-City-Blocks-IPv4.csv",
"raw")

DATA_DIRECTORIES = [geoip.RAW_DATABASE_DIR, geoip.DATA_DIR]
DATA_DIRECTORIES = [geoip.RAW_DATABASE_DIR, geoip.DATA_DIR, geoip.CODE_DIR]

class TestDataGenerator(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -122,11 +122,9 @@ def test_storeDynamicParams(self):
params["NUMBER_NODES_PER_MIDINDEX"]
)
self.assertDictEqual(
json.loads(readFile("params.js", jsonParsed=False)[len("module.exports = "):]),
json.loads(readFile(geoip.PARAMS_FILE, jsonParsed=False)[len("module.exports = "):]),
params
)
# Tear-down
os.remove("params.js")

def test_all(self):
createLocationsFile()
Expand Down

0 comments on commit 621db0a

Please sign in to comment.