@@ -2,9 +2,10 @@ var net = require("net");
22var fs = require ( "fs" ) ;
33var bigInt = require ( "big-integer" ) ;
44var https = require ( "https" ) ;
5+ const csv = require ( "csv-parser" ) ;
56
67// For BIN queries
7- const VERSION = "9.3 .1" ;
8+ const VERSION = "9.4 .1" ;
89const MAX_INDEX = 65536 ;
910const COUNTRY_POSITION = [
1011 0 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 ,
@@ -1533,8 +1534,121 @@ class IPTools {
15331534 }
15341535}
15351536
1537+ // Country class
1538+ class Country {
1539+ #fields = Array ( ) ;
1540+ #records = { } ;
1541+ #fd;
1542+ #ready = false ;
1543+
1544+ constructor ( csvFile ) {
1545+ if ( ! fs . existsSync ( csvFile ) ) {
1546+ throw new Error ( "The CSV file " + csvFile + " is not found." ) ;
1547+ }
1548+ try {
1549+ fs . createReadStream ( csvFile )
1550+ . pipe ( csv ( true ) )
1551+ . on ( "data" , ( data ) => {
1552+ if ( data . country_code ) {
1553+ this . #records[ data . country_code ] = data ;
1554+ } else {
1555+ throw new Error ( "Invalid country information CSV file." ) ;
1556+ }
1557+ } )
1558+ . on ( "end" , ( ) => {
1559+ this . #ready = true ;
1560+ } ) ;
1561+ } catch ( err ) {
1562+ throw new Error ( "Unable to read " + csvFile + "." ) ;
1563+ }
1564+ }
1565+
1566+ // Get country information
1567+ async getCountryInfo ( countryCode = "" ) {
1568+ while ( ! this . #ready) {
1569+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
1570+ }
1571+ countryCode = countryCode . trim ( ) ;
1572+ let results = Array ( ) ;
1573+ if ( Object . keys ( this . #records) . length === 0 ) {
1574+ throw new Error ( "No record available." ) ;
1575+ }
1576+ if ( countryCode != "" ) {
1577+ if ( this . #records[ countryCode ] ) {
1578+ results . push ( this . #records[ countryCode ] ) ;
1579+ }
1580+ } else {
1581+ for ( const elem in this . #records) {
1582+ results . push ( this . #records[ elem ] ) ;
1583+ }
1584+ }
1585+ return results ;
1586+ }
1587+ }
1588+
1589+ // Region class
1590+ class Region {
1591+ #fields = Array ( ) ;
1592+ #records = { } ;
1593+ #fd;
1594+ #ready = false ;
1595+
1596+ constructor ( csvFile ) {
1597+ if ( ! fs . existsSync ( csvFile ) ) {
1598+ throw new Error ( "The CSV file " + csvFile + " is not found." ) ;
1599+ }
1600+ try {
1601+ fs . createReadStream ( csvFile )
1602+ . pipe ( csv ( true ) )
1603+ . on ( "data" , ( data ) => {
1604+ if ( data . subdivision_name ) {
1605+ if ( ! this . #records[ data . country_code ] ) {
1606+ this . #records[ data . country_code ] = Array ( ) ;
1607+ }
1608+ this . #records[ data . country_code ] . push ( {
1609+ code : data . code ,
1610+ name : data . subdivision_name ,
1611+ } ) ;
1612+ } else {
1613+ throw new Error ( "Invalid region information CSV file." ) ;
1614+ }
1615+ } )
1616+ . on ( "end" , ( ) => {
1617+ this . #ready = true ;
1618+ } ) ;
1619+ } catch ( err ) {
1620+ throw new Error ( "Unable to read " + csvFile + "." ) ;
1621+ }
1622+ }
1623+
1624+ // Get region code
1625+ async getRegionCode ( countryCode = "" , regionName = "" ) {
1626+ while ( ! this . #ready) {
1627+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
1628+ }
1629+ countryCode = countryCode . trim ( ) ;
1630+ regionName = regionName . trim ( ) ;
1631+ if ( Object . keys ( this . #records) . length === 0 ) {
1632+ throw new Error ( "No record available." ) ;
1633+ }
1634+ if ( this . #records[ countryCode ] ) {
1635+ for ( let x = 0 ; x < this . #records[ countryCode ] . length ; x ++ ) {
1636+ let elem = this . #records[ countryCode ] [ x ] ;
1637+ if ( regionName . toUpperCase ( ) == elem . name . toUpperCase ( ) ) {
1638+ return elem . code ;
1639+ }
1640+ }
1641+ return null ;
1642+ } else {
1643+ return null ;
1644+ }
1645+ }
1646+ }
1647+
15361648module . exports = {
15371649 IP2Location : IP2Location ,
15381650 IP2LocationWebService : IP2LocationWebService ,
15391651 IPTools : IPTools ,
1652+ Country : Country ,
1653+ Region : Region ,
15401654} ;
0 commit comments