ip2location-nodejs
Advanced tools
Comparing version 9.3.1 to 9.4.0
{ | ||
"name": "ip2location-nodejs", | ||
"version": "9.3.1", | ||
"version": "9.4.0", | ||
"description": "IP2Location geolocation component", | ||
@@ -25,3 +25,4 @@ "keywords": [ | ||
"dependencies": { | ||
"big-integer": "^1.6.47" | ||
"big-integer": "^1.6.47", | ||
"csv-parser": "^3.0.0" | ||
}, | ||
@@ -28,0 +29,0 @@ "repository": { |
@@ -496,2 +496,35 @@ export class IP2Location { | ||
cidrToIPV6(cidr: string): string[]; | ||
} | ||
} | ||
declare class Country { | ||
/** | ||
* Read the country information CSV file and parse the data. | ||
* | ||
* @param csvFile The full path to the country information CSV file. | ||
*/ | ||
constructor(csvFile: any); | ||
/** | ||
* Retrieves the country information. | ||
* | ||
* @param countryCode The country code to get the country information. | ||
* @returns The country information. | ||
*/ | ||
getCountryInfo(countryCode?: string): Promise<any[]>; | ||
#private; | ||
} | ||
declare class Region { | ||
/** | ||
* Read the region information CSV file and parse the data. | ||
* | ||
* @param csvFile The full path to the region information CSV file. | ||
*/ | ||
constructor(csvFile: any); | ||
/** | ||
* Retrieves the region code for the country code and region name. | ||
* | ||
* @param countryCode The country code to get the region code. | ||
* @param regionName The region name to get the region code. | ||
* @returns The region code. | ||
*/ | ||
getRegionCode(countryCode?: string, regionName?: string): Promise<any>; | ||
#private; | ||
} |
@@ -5,5 +5,6 @@ var net = require("net"); | ||
var https = require("https"); | ||
const csv = require("csv-parser"); | ||
// For BIN queries | ||
const VERSION = "9.3.1"; | ||
const VERSION = "9.4.0"; | ||
const MAX_INDEX = 65536; | ||
@@ -1537,2 +1538,113 @@ const COUNTRY_POSITION = [ | ||
// Country class | ||
class Country { | ||
#fields = Array(); | ||
#records = {}; | ||
#fd; | ||
#ready = false; | ||
constructor(csvFile) { | ||
if (!fs.existsSync(csvFile)) { | ||
throw new Error("The CSV file " + csvFile + " is not found."); | ||
} | ||
try { | ||
fs.createReadStream(csvFile) | ||
.pipe(csv(true)) | ||
.on("data", (data) => { | ||
if (data.country_code) { | ||
this.#records[data.country_code] = data; | ||
} else { | ||
throw new Error("Invalid country information CSV file."); | ||
} | ||
}) | ||
.on("end", () => { | ||
this.#ready = true; | ||
}); | ||
} catch (err) { | ||
throw new Error("Unable to read " + csvFile + "."); | ||
} | ||
} | ||
// Get country information | ||
async getCountryInfo(countryCode = "") { | ||
while (!this.#ready) { | ||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
} | ||
countryCode = countryCode.trim(); | ||
let results = Array(); | ||
if (Object.keys(this.#records).length === 0) { | ||
throw new Error("No record available."); | ||
} | ||
if (countryCode != "") { | ||
if (this.#records[countryCode]) { | ||
results.push(this.#records[countryCode]); | ||
} | ||
} else { | ||
for (const elem in this.#records) { | ||
results.push(this.#records[elem]); | ||
} | ||
} | ||
return results; | ||
} | ||
} | ||
// Region class | ||
class Region { | ||
#fields = Array(); | ||
#records = {}; | ||
#fd; | ||
#ready = false; | ||
constructor(csvFile) { | ||
if (!fs.existsSync(csvFile)) { | ||
throw new Error("The CSV file " + csvFile + " is not found."); | ||
} | ||
try { | ||
fs.createReadStream(csvFile) | ||
.pipe(csv(true)) | ||
.on("data", (data) => { | ||
if (data.subdivision_name) { | ||
if (!this.#records[data.country_code]) { | ||
this.#records[data.country_code] = Array(); | ||
} | ||
this.#records[data.country_code].push({ | ||
code: data.code, | ||
name: data.subdivision_name, | ||
}); | ||
} else { | ||
throw new Error("Invalid region information CSV file."); | ||
} | ||
}) | ||
.on("end", () => { | ||
this.#ready = true; | ||
}); | ||
} catch (err) { | ||
throw new Error("Unable to read " + csvFile + "."); | ||
} | ||
} | ||
// Get region code | ||
async getRegionCode(countryCode = "", regionName = "") { | ||
while (!this.#ready) { | ||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
} | ||
countryCode = countryCode.trim(); | ||
regionName = regionName.trim(); | ||
if (Object.keys(this.#records).length === 0) { | ||
throw new Error("No record available."); | ||
} | ||
if (this.#records[countryCode]) { | ||
for (let x = 0; x < this.#records[countryCode].length; x++) { | ||
let elem = this.#records[countryCode][x]; | ||
if (regionName.toUpperCase() == elem.name.toUpperCase()) { | ||
return elem.code; | ||
} | ||
} | ||
return null; | ||
} else { | ||
return null; | ||
} | ||
} | ||
} | ||
module.exports = { | ||
@@ -1542,2 +1654,4 @@ IP2Location: IP2Location, | ||
IPTools: IPTools, | ||
Country: Country, | ||
Region: Region, | ||
}; |
@@ -1,2 +0,2 @@ | ||
const {IP2Location, IP2LocationWebService, IPTools} = require("ip2location-nodejs"); | ||
const {IP2Location, IP2LocationWebService, IPTools, Country, Region} = require("ip2location-nodejs"); | ||
@@ -87,1 +87,17 @@ let ip2location = new IP2Location(); | ||
console.log(tools.cidrToIPV6("2002:1234::abcd:ffff:c0a8:101/62")); | ||
let country = new Country("./IP2LOCATION-COUNTRY-INFORMATION-BASIC.CSV"); | ||
country.getCountryInfo("US").then(country_info => { | ||
console.log(country_info); | ||
}); | ||
country.getCountryInfo("").then(country_info => { | ||
console.log(country_info); | ||
}); | ||
let region = new Region("./IP2LOCATION-ISO3166-2.CSV"); | ||
region.getRegionCode("US", "California").then(region_code => { | ||
console.log(region_code); | ||
}); |
76542
2073
2
+ Addedcsv-parser@^3.0.0
+ Addedcsv-parser@3.2.0(transitive)