node-ipdox 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -15,7 +15,11 @@ npm install node-ipdox --save
15
15
  ```javascript
16
16
  import { IPDox } from "node-ipdox";
17
17
 
18
- const ipdox = new IPDox({ cacheTimeout: 43200000, maxRetries: 10 });
18
+ const ipdox = new IPDox({
19
+ cacheMaxItems: 5000,
20
+ cacheMaxAge: 43200000,
21
+ maxRetries: 10,
22
+ });
19
23
 
20
24
  ipdox
21
25
  .doxIP({ ip: "8.8.8.8" })
@@ -25,11 +29,12 @@ ipdox
25
29
 
26
30
  ## API
27
31
 
28
- ### `new IPDox({ cacheTimeout, maxRetries })`
32
+ ### `new IPDox({ cacheMaxItems, cacheMaxAge, maxRetries })`
29
33
 
30
34
  Creates a new instance of IPDox.
31
35
 
32
- - `cacheTimeout` - The cache timeout in milliseconds (default: 43200000 (12 hours))
36
+ - `cacheMaxItems` - The maximum number of items to store in the cache (default: 1000)
37
+ - `cacheMaxAge` - The cache timeout in milliseconds (default: 43200000 (12 hours))
33
38
  - `maxRetries` - Maximum number of retries if an API request fails (default: 10)
34
39
 
35
40
  ### `ipdox.doxIP({ ip })`
@@ -40,6 +45,8 @@ Fetches geolocation data for the specified IP address.
40
45
 
41
46
  Returns a Promise that resolves to an `IPDOXResponse` object.
42
47
 
48
+ If no response is found, undefined is returned.
49
+
43
50
  ## IPDOXResponse
44
51
 
45
52
  The `IPDOXResponse` object includes the following properties:
package/dist/ipdox.d.ts CHANGED
@@ -3,21 +3,20 @@ import { IPDOXResponse } from "./types/IPDOXResponse";
3
3
  import { IPDOXConstructor } from "./types/IPDOXConstructor";
4
4
  declare class IPDox {
5
5
  private cache;
6
- private cacheTimeout;
7
6
  private maxRetries;
8
7
  /**
9
- * @description Creates an instance of ListSubscribers.
10
- * @param {IPDOXConstructor} params - Params of the constructor
11
- * @param {number} params.cacheTimeout - The cache timeout in milliseconds (default: 43200000 (12 hours))
12
- *
13
- *
8
+ * @description Creates an instance of IPDox.
9
+ * @param {IPDOXConstructor} params
10
+ * @param {number} params.cacheMaxItems - The maximum number of items in the cache (default: 1000)
11
+ * @param {number} params.cacheMaxAge - The cache timeout in milliseconds (default: 43200000 (12 hours))
12
+ * @param {number} params.maxRetries - The maximum number of retries (default: 10)
14
13
  */
15
- constructor({ cacheTimeout, maxRetries }: IPDOXConstructor);
14
+ constructor({ cacheMaxItems, cacheMaxAge, maxRetries }?: IPDOXConstructor);
16
15
  /**
17
- * @description Get all the subscribers of a list
16
+ * @description Get information about an IP address
18
17
  * @param {IPDOXRequest} params - Params of the request
19
18
  * @param {string} params.ip - IP address
20
- * @returns {Promise<IPDOXResponse>} - Promise of the response
19
+ * @returns {Promise<IPDOXResponse | undefined>} - Promise of the response or undefined
21
20
  * @memberof IPDox
22
21
  */
23
22
  doxIP({ ip }: IPDOXRequest): Promise<IPDOXResponse | undefined>;
package/dist/ipdox.js CHANGED
@@ -14,28 +14,39 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const axios_1 = __importDefault(require("axios"));
16
16
  const apis_1 = require("./utils/apis");
17
+ const lru_cache_1 = require("lru-cache");
17
18
  class IPDox {
18
19
  /**
19
- * @description Creates an instance of ListSubscribers.
20
- * @param {IPDOXConstructor} params - Params of the constructor
21
- * @param {number} params.cacheTimeout - The cache timeout in milliseconds (default: 43200000 (12 hours))
22
- *
23
- *
20
+ * @description Creates an instance of IPDox.
21
+ * @param {IPDOXConstructor} params
22
+ * @param {number} params.cacheMaxItems - The maximum number of items in the cache (default: 1000)
23
+ * @param {number} params.cacheMaxAge - The cache timeout in milliseconds (default: 43200000 (12 hours))
24
+ * @param {number} params.maxRetries - The maximum number of retries (default: 10)
24
25
  */
25
- constructor({ cacheTimeout = 43200000, maxRetries = 10 }) {
26
- this.cache = new Map();
27
- this.cacheTimeout = cacheTimeout;
26
+ constructor({ cacheMaxItems = 1000, cacheMaxAge = 43200000, maxRetries = 10 } = {
27
+ cacheMaxItems: 1000,
28
+ cacheMaxAge: 43200000,
29
+ maxRetries: 10
30
+ }) {
31
+ this.cache = new lru_cache_1.LRUCache({
32
+ max: cacheMaxItems,
33
+ ttl: cacheMaxAge
34
+ });
28
35
  this.maxRetries = maxRetries;
29
36
  }
30
37
  /**
31
- * @description Get all the subscribers of a list
38
+ * @description Get information about an IP address
32
39
  * @param {IPDOXRequest} params - Params of the request
33
40
  * @param {string} params.ip - IP address
34
- * @returns {Promise<IPDOXResponse>} - Promise of the response
41
+ * @returns {Promise<IPDOXResponse | undefined>} - Promise of the response or undefined
35
42
  * @memberof IPDox
36
43
  */
37
44
  doxIP({ ip }) {
38
45
  return __awaiter(this, void 0, void 0, function* () {
46
+ // Check that the ip is valid
47
+ if (!ip || ip === "") {
48
+ return undefined;
49
+ }
39
50
  // Check if the IP is already in the cache
40
51
  if (this.cache.has(ip)) {
41
52
  const cachedResponse = this.cache.get(ip);
@@ -87,16 +98,13 @@ class IPDox {
87
98
  }
88
99
  cacheResponse(ip, response) {
89
100
  this.cache.set(ip, response);
90
- // Remove from cache after timeout
91
- setTimeout(() => this.cache.delete(ip), this.cacheTimeout);
92
101
  }
93
102
  fetchIPHyphenAPIDotCom(ip) {
94
103
  return __awaiter(this, void 0, void 0, function* () {
95
104
  const requestURL = apis_1.GeoAPIs.IP_HYPHEN_API_DOT_COM + ip + "?fields=24899583";
96
105
  const response = yield axios_1.default.get(requestURL);
97
106
  if (response.data.status === "success") {
98
- this.cacheResponse(ip, response.data);
99
- return Promise.resolve({
107
+ const formattedResponse = {
100
108
  ip: response.data.query,
101
109
  country: response.data.countryCode,
102
110
  city: response.data.city,
@@ -108,7 +116,9 @@ class IPDox {
108
116
  proxy: response.data.proxy,
109
117
  isHosting: response.data.hosting,
110
118
  source: "ip-api.com"
111
- });
119
+ };
120
+ this.cacheResponse(ip, formattedResponse);
121
+ return Promise.resolve(formattedResponse);
112
122
  }
113
123
  else {
114
124
  return Promise.reject();
@@ -120,8 +130,7 @@ class IPDox {
120
130
  const requestURL = apis_1.GeoAPIs.FREE_IP_API_DOT_COM + ip;
121
131
  const response = yield axios_1.default.get(requestURL);
122
132
  if (response.data.ipVersion === 4) {
123
- this.cacheResponse(ip, response.data);
124
- return Promise.resolve({
133
+ const formattedResponse = {
125
134
  ip: response.data.ipAddress,
126
135
  country: response.data.countryCode,
127
136
  city: response.data.cityName,
@@ -133,7 +142,9 @@ class IPDox {
133
142
  proxy: response.data.proxy,
134
143
  isHosting: response.data.hosting,
135
144
  source: "freeipapi.com"
136
- });
145
+ };
146
+ this.cacheResponse(ip, formattedResponse);
147
+ return Promise.resolve(formattedResponse);
137
148
  }
138
149
  else {
139
150
  return Promise.reject();
@@ -145,8 +156,7 @@ class IPDox {
145
156
  const requestURL = apis_1.GeoAPIs.IPWHO_DOT_IS + ip;
146
157
  const response = yield axios_1.default.get(requestURL);
147
158
  if (response.data.success) {
148
- this.cacheResponse(ip, response.data);
149
- return Promise.resolve({
159
+ const formattedResponse = {
150
160
  ip: response.data.ip,
151
161
  country: response.data.country_code,
152
162
  city: response.data.city,
@@ -158,7 +168,9 @@ class IPDox {
158
168
  proxy: false,
159
169
  isHosting: false,
160
170
  source: "ipwho.is"
161
- });
171
+ };
172
+ this.cacheResponse(ip, formattedResponse);
173
+ return Promise.resolve(formattedResponse);
162
174
  }
163
175
  else {
164
176
  return Promise.reject();
@@ -170,8 +182,7 @@ class IPDox {
170
182
  const requestURL = apis_1.GeoAPIs.IPAPI_DOT_CO + ip + "/json";
171
183
  const response = yield axios_1.default.get(requestURL);
172
184
  if (response.data.ip) {
173
- this.cacheResponse(ip, response.data);
174
- return Promise.resolve({
185
+ const formattedResponse = {
175
186
  ip: response.data.ip,
176
187
  country: response.data.country_code,
177
188
  city: response.data.city,
@@ -181,7 +192,9 @@ class IPDox {
181
192
  zip: response.data.postal,
182
193
  isp: response.data.org,
183
194
  source: "ipapi.co"
184
- });
195
+ };
196
+ this.cacheResponse(ip, formattedResponse);
197
+ return Promise.resolve(formattedResponse);
185
198
  }
186
199
  else {
187
200
  return Promise.reject();
@@ -2,10 +2,12 @@
2
2
  * Interface for the constructor
3
3
  *
4
4
  * @interface IPDOXConstructor
5
- * @property {number} cacheTimeout The cache timeout in milliseconds (default: 43200000 (12 hours))
5
+ * @property {number} cacheMaxItems The maximum number of items in the cache (default: 1000)
6
+ * @property {number} cacheMaxAge The cache timeout in milliseconds (default: 43200000 (12 hours))
6
7
  * @property {number} maxRetries The maximum number of retries (default: 10)
7
8
  */
8
9
  export interface IPDOXConstructor {
9
- cacheTimeout?: number;
10
+ cacheMaxItems?: number;
11
+ cacheMaxAge?: number;
10
12
  maxRetries?: number;
11
13
  }
package/package.json CHANGED
@@ -1,9 +1,17 @@
1
1
  {
2
2
  "name": "node-ipdox",
3
- "version": "1.0.0",
4
- "description": "",
3
+ "version": "1.0.2",
4
+ "description": "node-ipdox is a Node.js library for GeoIP lookup, leveraging various free GeoIP APIs. It provides a unified response format and incorporates local caching to minimize duplicate requests.",
5
5
  "author": "Mikel Calvo <contact@mikecalvo.net> (www.mikelcalvo.net)",
6
- "keywords": ["geoip", "ip lookup", "ipdoxxing"],
6
+ "keywords": [
7
+ "geoip",
8
+ "ip lookup",
9
+ "ipdoxxing",
10
+ "ipdox",
11
+ "ip",
12
+ "ip address",
13
+ "ip address lookup"
14
+ ],
7
15
  "license": "ISC",
8
16
  "readmeFilename": "README.md",
9
17
  "homepage": "https://github.com/mikelcalvo/ipdox#readme",
@@ -23,11 +31,12 @@
23
31
  "url": "git+https://github.com/mikelcalvo/node-ipdox.git"
24
32
  },
25
33
  "dependencies": {
26
- "axios": "1.6.2"
34
+ "axios": "1.6.2",
35
+ "lru-cache": "^10.1.0"
27
36
  },
28
37
  "devDependencies": {
29
- "@types/node": "20.10.1",
30
- "prettier": "3.1.0",
31
- "typescript": "^5.3.2"
38
+ "@types/node": "20.10.5",
39
+ "prettier": "3.1.1",
40
+ "typescript": "^5.3.3"
32
41
  }
33
42
  }