node-ipdox 1.0.0 → 1.0.1

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 = 1000,
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,32 +14,44 @@ 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);
42
53
  if (cachedResponse) {
54
+ console.log(`Cache hit for IP: ${ip}`);
43
55
  return cachedResponse;
44
56
  }
45
57
  }
@@ -87,16 +99,14 @@ class IPDox {
87
99
  }
88
100
  cacheResponse(ip, response) {
89
101
  this.cache.set(ip, response);
90
- // Remove from cache after timeout
91
- setTimeout(() => this.cache.delete(ip), this.cacheTimeout);
102
+ console.log(`Cached response for IP: ${ip}`);
92
103
  }
93
104
  fetchIPHyphenAPIDotCom(ip) {
94
105
  return __awaiter(this, void 0, void 0, function* () {
95
106
  const requestURL = apis_1.GeoAPIs.IP_HYPHEN_API_DOT_COM + ip + "?fields=24899583";
96
107
  const response = yield axios_1.default.get(requestURL);
97
108
  if (response.data.status === "success") {
98
- this.cacheResponse(ip, response.data);
99
- return Promise.resolve({
109
+ const formattedResponse = {
100
110
  ip: response.data.query,
101
111
  country: response.data.countryCode,
102
112
  city: response.data.city,
@@ -108,7 +118,9 @@ class IPDox {
108
118
  proxy: response.data.proxy,
109
119
  isHosting: response.data.hosting,
110
120
  source: "ip-api.com"
111
- });
121
+ };
122
+ this.cacheResponse(ip, formattedResponse);
123
+ return Promise.resolve(formattedResponse);
112
124
  }
113
125
  else {
114
126
  return Promise.reject();
@@ -120,8 +132,7 @@ class IPDox {
120
132
  const requestURL = apis_1.GeoAPIs.FREE_IP_API_DOT_COM + ip;
121
133
  const response = yield axios_1.default.get(requestURL);
122
134
  if (response.data.ipVersion === 4) {
123
- this.cacheResponse(ip, response.data);
124
- return Promise.resolve({
135
+ const formattedResponse = {
125
136
  ip: response.data.ipAddress,
126
137
  country: response.data.countryCode,
127
138
  city: response.data.cityName,
@@ -133,7 +144,9 @@ class IPDox {
133
144
  proxy: response.data.proxy,
134
145
  isHosting: response.data.hosting,
135
146
  source: "freeipapi.com"
136
- });
147
+ };
148
+ this.cacheResponse(ip, formattedResponse);
149
+ return Promise.resolve(formattedResponse);
137
150
  }
138
151
  else {
139
152
  return Promise.reject();
@@ -145,8 +158,7 @@ class IPDox {
145
158
  const requestURL = apis_1.GeoAPIs.IPWHO_DOT_IS + ip;
146
159
  const response = yield axios_1.default.get(requestURL);
147
160
  if (response.data.success) {
148
- this.cacheResponse(ip, response.data);
149
- return Promise.resolve({
161
+ const formattedResponse = {
150
162
  ip: response.data.ip,
151
163
  country: response.data.country_code,
152
164
  city: response.data.city,
@@ -158,7 +170,9 @@ class IPDox {
158
170
  proxy: false,
159
171
  isHosting: false,
160
172
  source: "ipwho.is"
161
- });
173
+ };
174
+ this.cacheResponse(ip, formattedResponse);
175
+ return Promise.resolve(formattedResponse);
162
176
  }
163
177
  else {
164
178
  return Promise.reject();
@@ -170,8 +184,7 @@ class IPDox {
170
184
  const requestURL = apis_1.GeoAPIs.IPAPI_DOT_CO + ip + "/json";
171
185
  const response = yield axios_1.default.get(requestURL);
172
186
  if (response.data.ip) {
173
- this.cacheResponse(ip, response.data);
174
- return Promise.resolve({
187
+ const formattedResponse = {
175
188
  ip: response.data.ip,
176
189
  country: response.data.country_code,
177
190
  city: response.data.city,
@@ -181,7 +194,9 @@ class IPDox {
181
194
  zip: response.data.postal,
182
195
  isp: response.data.org,
183
196
  source: "ipapi.co"
184
- });
197
+ };
198
+ this.cacheResponse(ip, formattedResponse);
199
+ return Promise.resolve(formattedResponse);
185
200
  }
186
201
  else {
187
202
  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.1",
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,7 +31,8 @@
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
38
  "@types/node": "20.10.1",