Skip to content

Commit 7d986cd

Browse files
committed
Changed to native BigInt
1 parent 0415eb3 commit 7d986cd

File tree

4 files changed

+52
-60
lines changed

4 files changed

+52
-60
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 IP2Location.com
3+
Copyright (c) 2023 IP2Location.com
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ip2location-nodejs",
3-
"version": "9.4.2",
3+
"version": "9.4.3",
44
"description": "IP2Location geolocation component",
55
"keywords": [
66
"ip2location",
@@ -22,7 +22,6 @@
2222
"types": "src/ip2location.d.ts",
2323
"license": "MIT",
2424
"dependencies": {
25-
"big-integer": "^1.6.47",
2625
"csv-parser": "^3.0.0"
2726
},
2827
"repository": {

src/ip2location.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class IP2Location {
1313
* @param readBytes The number of bytes to read.
1414
* @param position The file offset to start reading.
1515
* @param readType The data type to convert the bytes to. (Valid values: int8|int32|uint32|float|str|int128)
16-
* @param isBigInt Whether to convert to BigInteger object.
16+
* @param isBigInt Whether to convert to BigInt.
1717
* @returns The value of the specified data type.
1818
*/
1919
readBin(readBytes: number, position: number, readType: string, isBigInt: boolean): any;
@@ -36,7 +36,7 @@ export class IP2Location {
3636
* Reads unsigned 32-bit integer from file.
3737
*
3838
* @param position The file offset to start reading.
39-
* @param isBigInt Whether to convert to BigInteger object.
39+
* @param isBigInt Whether to convert to BigInt.
4040
* @returns Unsigned 32-bit integer.
4141
*/
4242
read32(position: number, isBigInt: boolean): number;
@@ -53,7 +53,7 @@ export class IP2Location {
5353
*
5454
* @param position The buffer offset to start reading.
5555
* @param buffer The buffer containing the data.
56-
* @returns BigInteger object.
56+
* @returns BigInt.
5757
*/
5858
read128Row(position: number, buffer: any): any;
5959
/**
@@ -62,22 +62,22 @@ export class IP2Location {
6262
* @param position The buffer offset to start reading.
6363
* @param buffer The buffer containing the data.
6464
* @param len The number of bytes to read.
65-
* @returns BigInteger object or unsigned 32-bit integer.
65+
* @returns BigInt or unsigned 32-bit integer.
6666
*/
6767
read32Or128Row(position: number, buffer: any, len: number): any;
6868
/**
6969
* Reads either unsigned 32-bit or 128-bit integer from file.
7070
*
7171
* @param position The file offset to start reading.
7272
* @param ipType 4 for IPv4 or 6 for IPv6.
73-
* @returns BigInteger object or unsigned 32-bit integer.
73+
* @returns BigInt or unsigned 32-bit integer.
7474
*/
7575
read32Or128(position: number, ipType: number): any;
7676
/**
7777
* Reads unsigned 128-bit integer from file.
7878
*
7979
* @param position The file offset to start reading.
80-
* @returns BigInteger object.
80+
* @returns BigInt.
8181
*/
8282
read128(position: number): any;
8383
/**
@@ -418,7 +418,7 @@ export class IPTools {
418418
* Converts an IPv6 address to IP number.
419419
*
420420
* @param myIP The IP address to convert.
421-
* @returns The IP number in a BigInteger object.
421+
* @returns The IP number in a BigInt.
422422
*/
423423
ipV6ToDecimal(myIP: string): any;
424424
/**

src/ip2location.js

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
var net = require("net");
22
var fs = require("fs");
3-
var bigInt = require("big-integer");
43
var https = require("https");
54
const csv = require("csv-parser");
65

76
// For BIN queries
8-
const VERSION = "9.4.2";
7+
const VERSION = "9.4.3";
98
const MAX_INDEX = 65536;
109
const COUNTRY_POSITION = [
1110
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,
@@ -81,13 +80,13 @@ const ADDRESS_TYPE_POSITION = [
8180
const CATEGORY_POSITION = [
8281
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22,
8382
];
84-
const MAX_IPV4_RANGE = bigInt(4294967295);
85-
const MAX_IPV6_RANGE = bigInt("340282366920938463463374607431768211455");
86-
const FROM_6TO4 = bigInt("42545680458834377588178886921629466624");
87-
const TO_6TO4 = bigInt("42550872755692912415807417417958686719");
88-
const FROM_TEREDO = bigInt("42540488161975842760550356425300246528");
89-
const TO_TEREDO = bigInt("42540488241204005274814694018844196863");
90-
const LAST_32_BITS = bigInt("4294967295");
83+
const MAX_IPV4_RANGE = BigInt(4294967295);
84+
const MAX_IPV6_RANGE = BigInt("340282366920938463463374607431768211455");
85+
const FROM_6TO4 = BigInt("42545680458834377588178886921629466624");
86+
const TO_6TO4 = BigInt("42550872755692912415807417417958686719");
87+
const FROM_TEREDO = BigInt("42540488161975842760550356425300246528");
88+
const TO_TEREDO = BigInt("42540488241204005274814694018844196863");
89+
const LAST_32_BITS = BigInt("4294967295");
9190

9291
const MODES = {
9392
COUNTRY_SHORT: 1,
@@ -239,7 +238,7 @@ class IP2Location {
239238
break;
240239
case "uint32":
241240
return isBigInt
242-
? bigInt(buffer.readUInt32LE(0))
241+
? BigInt(buffer.readUInt32LE(0))
243242
: buffer.readUInt32LE(0);
244243
break;
245244
case "float":
@@ -249,12 +248,10 @@ class IP2Location {
249248
return buffer.toString("utf8");
250249
break;
251250
case "int128":
252-
let myBig = bigInt(); // zero
251+
let myBig = BigInt(0); // zero
253252
let bitShift = 8;
254253
for (let x = 0; x < 16; x++) {
255-
myBig = myBig.add(
256-
bigInt(buffer.readUInt8(x)).shiftLeft(bitShift * x)
257-
);
254+
myBig = myBig + (BigInt(buffer.readUInt8(x)) << (bitShift * x));
258255
}
259256
return myBig;
260257
break;
@@ -288,13 +285,12 @@ class IP2Location {
288285

289286
// Read 128 bits integer in the buffer
290287
read128Row(position, buffer) {
291-
let myBig = bigInt(); // zero
288+
let myBig = BigInt(0); // zero
292289
let bitShift = 8;
293290
for (let x = 0; x < 16; x++) {
294291
let pos = position + x;
295-
myBig = myBig.add(
296-
bigInt(this.read8Row(pos, buffer)).shiftLeft(bitShift * x)
297-
);
292+
myBig =
293+
myBig + (BigInt(this.read8Row(pos, buffer)) << BigInt(bitShift * x));
298294
}
299295
return myBig;
300296
}
@@ -571,19 +567,19 @@ class IP2Location {
571567
ipNumber = ip2No(myIP);
572568

573569
if (
574-
(ipNumber.geq(FROM_6TO4) && ipNumber.leq(TO_6TO4)) ||
575-
(ipNumber.geq(FROM_TEREDO) && ipNumber.leq(TO_TEREDO))
570+
(ipNumber >= FROM_6TO4 && ipNumber <= TO_6TO4) ||
571+
(ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO)
576572
) {
577573
ipType = 4;
578574
MAX_IP_RANGE = MAX_IPV4_RANGE;
579575
high = this.#myDB.dbCount;
580576
baseAddress = this.#myDB.baseAddress;
581577
columnSize = this.#ipV4ColumnSize;
582578

583-
if (ipNumber.geq(FROM_6TO4) && ipNumber.leq(TO_6TO4)) {
584-
ipNumber = ipNumber.shiftRight(80).and(LAST_32_BITS).toJSNumber();
579+
if (ipNumber >= FROM_6TO4 && ipNumber <= TO_6TO4) {
580+
ipNumber = Number((ipNumber >> BigInt(80)) & LAST_32_BITS);
585581
} else {
586-
ipNumber = ipNumber.not().and(LAST_32_BITS).toJSNumber();
582+
ipNumber = Number(~ipNumber & LAST_32_BITS);
587583
}
588584
if (this.#myDB.indexed == 1) {
589585
indexAddress = ipNumber >>> 16;
@@ -593,17 +589,17 @@ class IP2Location {
593589
} else {
594590
firstCol = 16; // IPv6 is 16 bytes
595591
if (this.#myDB.indexedIPV6 == 1) {
596-
indexAddress = ipNumber.shiftRight(112).toJSNumber();
592+
indexAddress = Number(ipNumber >> BigInt(112));
597593
low = this.#indexArrayIPV6[indexAddress][0];
598594
high = this.#indexArrayIPV6[indexAddress][1];
599595
}
600596
}
601597
}
602598
data.ip = myIP;
603-
ipNumber = bigInt(ipNumber);
599+
ipNumber = BigInt(ipNumber);
604600

605-
if (ipNumber.geq(MAX_IP_RANGE)) {
606-
ipNumber = MAX_IP_RANGE.minus(1);
601+
if (ipNumber >= MAX_IP_RANGE) {
602+
ipNumber = MAX_IP_RANGE - BigInt(1);
607603
}
608604

609605
data.ipNo = ipNumber.toString();
@@ -618,10 +614,10 @@ class IP2Location {
618614
ipFrom = this.read32Or128Row(0, fullRow, firstCol);
619615
ipTo = this.read32Or128Row(columnSize, fullRow, firstCol);
620616

621-
ipFrom = bigInt(ipFrom);
622-
ipTo = bigInt(ipTo);
617+
ipFrom = BigInt(ipFrom);
618+
ipTo = BigInt(ipTo);
623619

624-
if (ipFrom.leq(ipNumber) && ipTo.gt(ipNumber)) {
620+
if (ipFrom <= ipNumber && ipTo > ipNumber) {
625621
loadMesg(data, MSG_NOT_SUPPORTED); // load default message
626622

627623
let rowLen = columnSize - firstCol;
@@ -790,7 +786,7 @@ class IP2Location {
790786
}
791787
return;
792788
} else {
793-
if (ipFrom.gt(ipNumber)) {
789+
if (ipFrom > ipNumber) {
794790
high = mid - 1;
795791
} else {
796792
low = mid + 1;
@@ -1032,37 +1028,34 @@ function ip2No(ipV6) {
10321028
let sectionBits = 16; // 16 bits per section
10331029
let m = ipV6.split("::");
10341030

1035-
let total = bigInt(); // zero
1031+
let total = BigInt(0); // zero
10361032

10371033
if (m.length == 2) {
10381034
let myArrLeft = m[0] != "" ? m[0].split(":") : [];
10391035
let myArrRight = m[1] != "" ? m[1].split(":") : [];
10401036
let myArrMid = maxSections - myArrLeft.length - myArrRight.length;
10411037

10421038
for (let x = 0; x < myArrLeft.length; x++) {
1043-
total = total.add(
1044-
bigInt(parseInt("0x" + myArrLeft[x])).shiftLeft(
1045-
(maxSections - (x + 1)) * sectionBits
1046-
)
1047-
);
1039+
total =
1040+
total +
1041+
(BigInt(parseInt("0x" + myArrLeft[x])) <<
1042+
BigInt((maxSections - (x + 1)) * sectionBits));
10481043
}
10491044

10501045
for (let x = 0; x < myArrRight.length; x++) {
1051-
total = total.add(
1052-
bigInt(parseInt("0x" + myArrRight[x])).shiftLeft(
1053-
(myArrRight.length - (x + 1)) * sectionBits
1054-
)
1055-
);
1046+
total =
1047+
total +
1048+
(BigInt(parseInt("0x" + myArrRight[x])) <<
1049+
BigInt((myArrRight.length - (x + 1)) * sectionBits));
10561050
}
10571051
} else if (m.length == 1) {
10581052
let myArr = m[0].split(":");
10591053

10601054
for (let x = 0; x < myArr.length; x++) {
1061-
total = total.add(
1062-
bigInt(parseInt("0x" + myArr[x])).shiftLeft(
1063-
(maxSections - (x + 1)) * sectionBits
1064-
)
1065-
);
1055+
total =
1056+
total +
1057+
(BigInt(parseInt("0x" + myArr[x])) <<
1058+
BigInt((maxSections - (x + 1)) * sectionBits));
10661059
}
10671060
}
10681061

@@ -1231,10 +1224,10 @@ class IPTools {
12311224
// Convert IP number to IPv6 address
12321225
decimalToIPV6(ipNum) {
12331226
if (typeof ipNum == "string" || typeof ipNum == "number") {
1234-
ipNum = bigInt(ipNum);
1227+
ipNum = BigInt(ipNum);
12351228
}
12361229

1237-
if (ipNum.lt(bigInt.zero) || ipNum.gt(MAX_IPV6_RANGE)) {
1230+
if (ipNum < BigInt(0) || ipNum > MAX_IPV6_RANGE) {
12381231
return null;
12391232
}
12401233

@@ -1348,7 +1341,7 @@ class IPTools {
13481341
return null;
13491342
}
13501343

1351-
let ipNum = bigInt(myBin, 2);
1344+
let ipNum = BigInt("0b" + myBin);
13521345
let v6 = this.decimalToIPV6(ipNum);
13531346

13541347
return v6;

0 commit comments

Comments
 (0)