Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-ipinfo

Package Overview
Dependencies
Maintainers
2
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-ipinfo - npm Package Compare versions

Comparing version 3.0.2 to 3.1.0

dist/src/errors/apiLimitError.d.ts

6

CHANGELOG.md
# CHANGELOG
## 3.1.0
- Fixed error handling; 429 errors were not properly being propagated to the
user.
- Added a lot more documentation across the board.
## 3.0.2

@@ -4,0 +10,0 @@

6

dist/src/__tests__/ipinfoWrapper.test.js

@@ -108,3 +108,4 @@ "use strict";

relay: false,
hosting: false
hosting: false,
service: ""
});

@@ -225,3 +226,4 @@ expect(data.abuse).toEqual({

relay: false,
hosting: false
hosting: false,
service: ""
},

@@ -228,0 +230,0 @@ abuse: {

import IPinfoWrapper from "./ipinfoWrapper";
import Cache from "./cache/cache";
import LruCache from "./cache/lruCache";
import ApiLimitError from "./errors/apiLimitError";
export { Options } from "lru-cache";
export { Cache, LruCache, IPinfoWrapper };
export { Cache, LruCache, IPinfoWrapper, ApiLimitError };
export { Asn, Company, Carrier, Privacy, Abuse, Domains, IPinfo, Prefix, Prefixes6, AsnResponse, MapResponse, BatchResponse } from "./common";
export default IPinfoWrapper;

@@ -6,3 +6,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.IPinfoWrapper = exports.LruCache = void 0;
exports.ApiLimitError = exports.IPinfoWrapper = exports.LruCache = void 0;
var ipinfoWrapper_1 = __importDefault(require("./ipinfoWrapper"));

@@ -12,2 +12,4 @@ exports.IPinfoWrapper = ipinfoWrapper_1.default;

exports.LruCache = lruCache_1.default;
var apiLimitError_1 = __importDefault(require("./errors/apiLimitError"));
exports.ApiLimitError = apiLimitError_1.default;
exports.default = ipinfoWrapper_1.default;

@@ -8,3 +8,2 @@ import Cache from "./cache/cache";

private timeout;
private limitErrorMessage;
private mapLimitErrorMessage;

@@ -18,2 +17,3 @@ constructor(token: string, cache?: Cache, timeout?: number);

getBatch(urls: string[], batchSize?: number, batchTimeout?: number, timeoutTotal?: number, filter?: boolean): Promise<BatchResponse>;
private _isStatusCodeIn400Range;
}

@@ -45,2 +45,3 @@ "use strict";

var lruCache_1 = __importDefault(require("./cache/lruCache"));
var apiLimitError_1 = __importDefault(require("./errors/apiLimitError"));
var common_1 = require("./common");

@@ -51,4 +52,3 @@ var version_1 = __importDefault(require("./version"));

function IPinfoWrapper(token, cache, timeout) {
this.limitErrorMessage = "You have exceeded 50,000 requests a month. Visit https://ipinfo.io/account to see your API limits.";
this.mapLimitErrorMessage = "You have exceeded maximum IP upload limit i.e 500,000 IPs per request.";
this.mapLimitErrorMessage = "You have exceeded maximum IP upload limit per request.";
this.token = token;

@@ -92,24 +92,37 @@ this.countries = en_US_json_1.default;

});
res.on("close", function () {
var ipinfo = JSON.parse(data);
if (ipinfo.country) {
ipinfo.countryCode = ipinfo.country;
ipinfo.country =
_this.countries[ipinfo.countryCode];
if (!_this._isStatusCodeIn400Range(res.statusCode)) {
res.on("close", function () {
var ipinfo = JSON.parse(data);
if (ipinfo.country) {
ipinfo.countryCode = ipinfo.country;
ipinfo.country =
_this.countries[ipinfo.countryCode];
}
if (ipinfo.abuse && ipinfo.abuse.country) {
ipinfo.abuse.countryCode =
ipinfo.abuse.country;
ipinfo.abuse.country =
_this.countries[ipinfo.abuse.countryCode];
}
_this.cache.set(IPinfoWrapper.cacheKey(ip), ipinfo);
resolve(ipinfo);
});
res.on("error", function (error) {
reject(error);
});
}
else {
if (res.statusCode === 429) {
reject(new apiLimitError_1.default());
}
if (ipinfo.abuse && ipinfo.abuse.country) {
ipinfo.abuse.countryCode = ipinfo.abuse.country;
ipinfo.abuse.country =
_this.countries[ipinfo.abuse.countryCode];
else {
res.on("close", function () {
reject(new Error(data));
});
}
_this.cache.set(IPinfoWrapper.cacheKey(ip), ipinfo);
resolve(ipinfo);
});
res.on("error", function (error) {
if (error.response && error.response.status === 429) {
reject(_this.limitErrorMessage);
}
reject(error);
});
}
});
req.on("error", function (error) {
reject(error);
});
req.end();

@@ -149,19 +162,31 @@ }

});
res.on("close", function () {
var asnResp = JSON.parse(data);
if (asnResp.country) {
asnResp.countryCode = asnResp.country;
asnResp.country =
_this.countries[asnResp.countryCode];
if (!_this._isStatusCodeIn400Range(res.statusCode)) {
res.on("close", function () {
var asnResp = JSON.parse(data);
if (asnResp.country) {
asnResp.countryCode = asnResp.country;
asnResp.country =
_this.countries[asnResp.countryCode];
}
_this.cache.set(IPinfoWrapper.cacheKey(asn), asnResp);
resolve(asnResp);
});
res.on("error", function (error) {
reject(error);
});
}
else {
if (res.statusCode === 429) {
reject(new apiLimitError_1.default());
}
_this.cache.set(IPinfoWrapper.cacheKey(asn), asnResp);
resolve(asnResp);
});
res.on("error", function (error) {
if (error.response && error.response.status === 429) {
reject(_this.limitErrorMessage);
else {
res.on("close", function () {
reject(new Error(data));
});
}
reject(error);
});
}
});
req.on("error", function (error) {
reject(error);
});
req.end();

@@ -178,3 +203,3 @@ }

return new Promise(function (_resolve, reject) {
reject(_this.mapLimitErrorMessage);
reject(new Error(_this.mapLimitErrorMessage));
});

@@ -203,11 +228,20 @@ }

});
res.on("close", function () {
resolve(JSON.parse(data));
});
res.on("error", function (error) {
if (error.response && error.response.status === 429) {
reject(_this.limitErrorMessage);
if (!_this._isStatusCodeIn400Range(res.statusCode)) {
res.on("close", function () {
resolve(JSON.parse(data));
});
res.on("error", function (error) {
reject(error);
});
}
else {
if (res.statusCode === 429) {
reject(new apiLimitError_1.default());
}
reject(error);
});
else {
res.on("close", function () {
reject(new Error(data));
});
}
}
});

@@ -248,14 +282,23 @@ req.on("error", function (error) {

});
res.on("close", function () {
resolve(data);
});
res.on("error", function (error) {
if (error.response && error.response.status === 429) {
reject(_this.limitErrorMessage);
if (!_this._isStatusCodeIn400Range(res.statusCode)) {
res.on("close", function () {
resolve(data);
});
res.on("error", function (error) {
reject(error);
});
}
else {
if (res.statusCode === 429) {
reject(new apiLimitError_1.default());
}
reject(error);
});
else {
res.on("close", function () {
reject(new Error(data));
});
}
}
});
req.on("timeout", function () {
reject("batch timeout reached");
reject(new Error("batch timeout reached"));
});

@@ -354,3 +397,3 @@ req.on("error", function (error) {

if (value === totalTimeoutReached) {
reject("Total timeout has been exceeded.");
reject(new Error("Total timeout has been exceeded."));
}

@@ -370,4 +413,12 @@ else {

};
IPinfoWrapper.prototype._isStatusCodeIn400Range = function (statusCode) {
if (statusCode && statusCode >= 400 && statusCode < 500) {
return true;
}
else {
return false;
}
};
return IPinfoWrapper;
}());
exports.default = IPinfoWrapper;

@@ -1,2 +0,2 @@

declare const VERSION = "3.0.2";
declare const VERSION = "3.1.0";
export default VERSION;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var VERSION = "3.0.2";
var VERSION = "3.1.0";
exports.default = VERSION;
{
"name": "node-ipinfo",
"version": "3.0.2",
"version": "3.1.0",
"description": "Official Node client library for IPinfo",

@@ -33,3 +33,4 @@ "main": "dist/src/index.js",

"dependencies": {
"lru-cache": "^6.0.0"
"lru-cache": "^6.0.0",
"@types/lru-cache": "^5.1.1"
},

@@ -39,3 +40,2 @@ "devDependencies": {

"@types/jest": "^26.0.24",
"@types/lru-cache": "^5.1.1",
"@types/node": "^16.10.3",

@@ -42,0 +42,0 @@ "dotenv": "^8.6.0",

@@ -20,3 +20,3 @@ # [<img src="https://ipinfo.io/static/ipinfo-small.svg" alt="IPinfo" width="24"/>](https://ipinfo.io/) IPinfo NodeJS Client Library

## Installation
### Installation

@@ -27,5 +27,5 @@ ```sh

## Usage
### Usage
### TypeScript
##### TypeScript

@@ -46,8 +46,8 @@ ```typescript

### JavaScript
##### JavaScript
```javascript
let { IPinfoWrapper } = require("node-ipinfo");
const { IPinfoWrapper } = require("node-ipinfo");
let ipinfo = new IPinfoWrapper("MY_TOKEN");
const ipinfo = new IPinfoWrapper("MY_TOKEN");

@@ -80,4 +80,4 @@ ipinfo.lookupIp("1.1.1.1").then((response) => {

};
let cache = new LruCache(cacheOptions);
let ipinfoWrapper = new IPinfoWrapper("MY_TOKEN", cache);
const cache = new LruCache(cacheOptions);
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN", cache);
```

@@ -88,10 +88,10 @@

```javascript
let { IPinfoWrapper, LruCache } = require("node-ipinfo");
const { IPinfoWrapper, LruCache } = require("node-ipinfo");
let cacheOptions = {
const cacheOptions = {
max: 5000,
maxAge: 24 * 1000 * 60 * 60,
};
let cache = new LruCache(cacheOptions);
let ipinfo = new IPinfoWrapper("MY_TOKEN", cache);
const cache = new LruCache(cacheOptions);
const ipinfo = new IPinfoWrapper("MY_TOKEN", cache);
```

@@ -112,3 +112,3 @@

// 10 second timeout.
let ipinfoWrapper = new IPinfoWrapper("MY_TOKEN", null, 10000);
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN", null, 10000);
```

@@ -119,6 +119,6 @@

```javascript
let { IPinfoWrapper } = require("node-ipinfo");
const { IPinfoWrapper } = require("node-ipinfo");
// 10 second timeout.
let ipinfo = new IPinfoWrapper("MY_TOKEN", null, 10000);
const ipinfo = new IPinfoWrapper("MY_TOKEN", null, 10000);
```

@@ -128,4 +128,40 @@

##### TypeScript
```typescript
import IPinfoWrapper, { IPinfo, ApiLimitError } from "node-ipinfo";
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN");
ipinfoWrapper.lookupIp("1.1.1.1").then((response: IPinfo) => {
console.log(response);
})
.catch((error) => {
console.log(error);
if (error instanceof ApiLimitError) {
// handle api limit exceed error
} else {
// handle other errors
}
});
```
##### JavaScript
```javascript
// example coming soon
const { IPinfoWrapper, ApiLimitError } = require("node-ipinfo");
const ipinfo = new IPinfoWrapper("MY_TOKEN");
ipinfo.lookupIp("1.1.1.1").then((response) => {
console.log(response);
},
(error) => {
console.log(error);
if (error instanceof ApiLimitError){
// handle api limit exceed error
} else {
// handle other errors
}
});
```

@@ -135,4 +171,34 @@

`response.country` will return the country name, whereas `response.countryCode` can be used to fetch country code.
##### TypeScript
```typescript
import IPinfoWrapper, { IPinfo } from "node-ipinfo";
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN");
ipinfoWrapper.lookupIp("1.1.1.1").then((response: IPinfo) => {
// country code, e.g. 'US'
console.log(response.countryCode);
// country name, e.g. 'United States'
console.log(response.country);
});
```
##### JavaScript
```javascript
// example coming soon
const { IPinfoWrapper } = require("node-ipinfo");
const ipinfo = new IPinfoWrapper("MY_TOKEN");
ipinfo.lookupIp("1.1.1.1").then((response) => {
// country code, e.g. 'US'
console.log(response.countryCode);
// country name, e.g. 'United States'
console.log(response.country);
});
```

@@ -142,6 +208,96 @@

`response.loc` will return a composite string of latitude and longitude values in the `"latitude,longitude"` format.
##### TypeScript
```typescript
import IPinfoWrapper, { IPinfo } from "node-ipinfo";
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN");
ipinfoWrapper.lookupIp("1.1.1.1").then((response: IPinfo) => {
// '34.0522,-118.2437'
console.log(response.loc);
});
```
##### JavaScript
```javascript
// example coming soon
const { IPinfoWrapper } = require("node-ipinfo");
const ipinfo = new IPinfoWrapper("MY_TOKEN");
ipinfo.lookupIp("1.1.1.1").then((response) => {
// '34.0522,-118.2437'
console.log(response.loc);
});
```
### Locate IPs on a World Map
A world map can be generated with locations of all input IPs using `getMap`. It returns the URL of the map in the response.
##### TypeScript
```typescript
import IPinfoWrapper, { MapResponse } from "node-ipinfo";
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN");
const ips = ["1.1.1.1", "8.8.8.8", "1.2.3.4"];
ipinfoWrapper.getMap(ips).then((response: MapResponse) => {
console.log(response);
});
```
##### JavaScript
```javascript
const { IPinfoWrapper } = require("node-ipinfo");
const ipinfo = new IPinfoWrapper("MY_TOKEN");
const ips = ["1.1.1.1", "8.8.8.8", "1.2.3.4"];
ipinfo.getMap(ips).then((response) => {
console.log(response);
});
```
### Batch Operations
Looking up a single IP at a time can be slow. It could be done concurrently from the client side, but IPinfo supports a batch endpoint to allow you to group together IPs and let us handle retrieving details for them in bulk for you.
##### TypeScript
```typescript
import IPinfoWrapper, { BatchResponse } from "node-ipinfo";
const ipinfoWrapper = new IPinfoWrapper("MY_TOKEN");
const ips = ["1.1.1.1", "8.8.8.8", "1.2.3.4/country"];
ipinfoWrapper.getBatch(ips).then((response: BatchResponse) => {
console.log(response);
});
```
##### JavaScript
```javascript
const { IPinfoWrapper } = require("node-ipinfo");
const ipinfo = new IPinfoWrapper("MY_TOKEN");
const ips = ["1.1.1.1", "8.8.8.8", "1.2.3.4/country"];
ipinfo.getBatch(ips).then((response) => {
console.log(response);
});
```
The input size is not limited, as the interface will chunk operations for you
behind the scenes.
Please see [the official documentation](https://ipinfo.io/developers/batch) for
more information and limitations.
## Integrated Typescript Typings

@@ -161,3 +317,3 @@

### Other Libraries
## Other Libraries

@@ -168,3 +324,3 @@ There are official IPinfo client libraries available for many languages including PHP, Python, Go, Java, Ruby, and many popular frameworks such as Django, Rails and Laravel. There are also many third party libraries and integrations available for our API.

### About IPinfo
## About IPinfo

@@ -171,0 +327,0 @@ Founded in 2013, IPinfo prides itself on being the most reliable, accurate, and in-depth source of IP address data available anywhere. We process terabytes of data to produce our custom IP geolocation, company, carrier, privacy detection (VPN, proxie, Tor), hosted domains, and IP type data sets. Our API handles over 40 billion requests a month for 100,000 businesses and developers.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc