Skip to content

Commit

Permalink
Add types (#17)
Browse files Browse the repository at this point in the history
* Add types

* Add types to package.json

* Make lookups nullable
  • Loading branch information
roim authored May 5, 2021
1 parent c81a16e commit f22ae59
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 26 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.2",
"description": "This is a no-dependency, super lite version of IP2Location LITE lookup.",
"main": "src/ip3country.js",
"types": "types/ip3country.d.ts",
"scripts": {
"test": "node tests/lookupTest.js"
},
Expand Down
58 changes: 32 additions & 26 deletions src/ip3country.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs/promises');
const fsSync = require('fs');
const path = require('path');
const fs = require("fs/promises");
const fsSync = require("fs");
const path = require("path");

const ip3country = {
countryCodes: [],
Expand All @@ -14,38 +14,44 @@ const ip3country = {
n1.c: if n is < 240, c is country code index
242.n2.n3.c: if n >= 240 but < 65536. n2 being lower order byte
243.n2.n3.n4.c: if n >= 65536. n2 being lower order byte
*/
init: async function() {
*/
init: async function () {
if (this._initCalled) {
console.warn("You can call init just once");
return;
}

this._initCalled = true;

const buffer = await fs.readFile(path.resolve(__dirname, '../bin/ip_supalite.bin'));
const buffer = await fs.readFile(
path.resolve(__dirname, "../bin/ip_supalite.bin")
);
this.initWithBuffer(buffer);
},

initSync: function() {
initSync: function () {
if (this._initCalled) {
console.warn("You can call init just once");
return;
}

this._initCalled = true;

const buffer = fsSync.readFileSync(path.resolve(__dirname, '../bin/ip_supalite.bin'));
const buffer = fsSync.readFileSync(
path.resolve(__dirname, "../bin/ip_supalite.bin")
);
this.initWithBuffer(buffer);
},

initWithBuffer: function(buffer) {
initWithBuffer: function (buffer) {
let index = 0;
while (index < buffer.length) {
const c1 = buffer[index++];
const c2 = buffer[index++];
this.countryTable.push('' + String.fromCharCode(c1) + String.fromCharCode(c2));
if (String.fromCharCode(c1) === '*' ) {
this.countryTable.push(
"" + String.fromCharCode(c1) + String.fromCharCode(c2)
);
if (String.fromCharCode(c1) === "*") {
break;
}
}
Expand All @@ -59,12 +65,12 @@ const ip3country = {
} else if (n1 == 242) {
const n2 = buffer[index++];
const n3 = buffer[index++];
count = n2 | n3 << 8;
count = n2 | (n3 << 8);
} else if (n1 == 243) {
const n2 = buffer[index++];
const n3 = buffer[index++];
const n4 = buffer[index++];
count = n2 | n3 << 8 | n4 << 16;
count = n2 | (n3 << 8) | (n4 << 16);
}

lastEndRange += count * 256;
Expand All @@ -74,30 +80,31 @@ const ip3country = {
}
},

lookupStr: function(ipaddrstr) {
const components = ipaddrstr.split('.');
lookupStr: function (ipaddrstr) {
const components = ipaddrstr.split(".");
if (components.length !== 4) {
return null;
}

const ipNumber = parseInt(components[0]) * Math.pow(256, 3) +
(parseInt(components[1]) << 16) +
const ipNumber =
parseInt(components[0]) * Math.pow(256, 3) +
(parseInt(components[1]) << 16) +
(parseInt(components[2]) << 8) +
parseInt(components[3]);
return this.lookupNumeric(ipNumber);
},

lookupNumeric: function(ipNumber) {
lookupNumeric: function (ipNumber) {
if (!this.countryCodes) {
throw new Error('Please call init first');
throw new Error("Please call init first");
}

const index = this.binarySearch(ipNumber);
const index = this.binarySearch(ipNumber);
const cc = this.countryCodes[index];
return (cc === '--' ? null : cc);
return cc === "--" ? null : cc;
},

binarySearch: function(value) {
binarySearch: function (value) {
let min = 0;
let max = this.ipRanges.length - 1;

Expand All @@ -111,8 +118,7 @@ const ip3country = {
}

return min;
}
}

},
};

module.exports = ip3country;
module.exports = ip3country;
36 changes: 36 additions & 0 deletions types/ip3country.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export = ip3country;

declare namespace ip3country {
/**
* Initializes ip3country asynchronously.
* Must be called once before any lookups.
*/
function init(): Promise<void>;

/**
* Initializes ip3country synchronously.
* Must be called once before any lookups.
*
* `init` is preferred. This synchronous version is provided as an utility in
* cases where you can't run asynchronous code. It is not simply a synchronous
* wrapper--it replaces all async calls with sync calls.
*/
function initSync(): void;

/**
* Returns a 2-digit country code for the given IP Address. Returns `null` if
* there is no match. Behaviour is unspecified for invalid IP Address strings
* (either null or a random country will be returned).
*
* @param ip An IP Address in octet dot-decimal notation, e.g. "127.0.0.1".
*/
function lookupStr(ip: string): string | null;

/**
* Returns a 2-digit country code for the given IP Address. Returns `null` if
* there is no match.
*
* @param ip An IP Address represented as a 32-bit signed integer.
*/
function lookupNumeric(ip: number): string | null;
}

0 comments on commit f22ae59

Please sign in to comment.