Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 22, 2019
1 parent fdcbb7d commit b0e683c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
7 changes: 4 additions & 3 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const sendXhr = async (url, options, version) => {
const ip = xhr.responseText.trim();

if (!ip || !isIp[version](ip)) {
return reject();
reject();
return;
}

resolve(ip);
Expand All @@ -42,8 +43,8 @@ const sendXhr = async (url, options, version) => {

const queryHttps = async (version, options) => {
let ip;
const _urls = [].concat.apply(urls[version], options.fallbackUrls || []);
for (const url of _urls) {
const urls_ = [].concat.apply(urls[version], options.fallbackUrls || []);
for (const url of urls_) {
try {
// eslint-disable-next-line no-await-in-loop
ip = await sendXhr(url, options, version);
Expand Down
25 changes: 19 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare namespace publicIp {
interface Options {
/**
Use a HTTPS check using the [icanhazip.com](https://github.com/major/icanhaz) service instead of the DNS query. [ipify.org](https://www.ipify.org) is used as a fallback if `icanhazip.com` fails. This check is much more secure and tamper-proof, but also a lot slower. **This option is only available in the Node.js version**. Default behaviour is to check aginst DNS before using HTTPS fallback, if set as `true` it will *only* check against HTTPS.
Use a HTTPS check using the [icanhazip.com](https://github.com/major/icanhaz) service instead of the DNS query. [ipify.org](https://www.ipify.org) is used as a fallback if `icanhazip.com` fails. This check is much more secure and tamper-proof, but also a lot slower. __This option is only available in the Node.js version__. The default behaviour is to check aginst DNS before using HTTPS fallback. If set to `true`, it will _only_ check against HTTPS.
@default false
*/
Expand All @@ -15,11 +15,24 @@ declare namespace publicIp {
readonly timeout?: number;

/**
In case you want to add your own custom HTTPS endpoints to get public IP from (like [ifconfig.co](https://ifconfig.co), for example), you can set them here. They will only be used if everything else fails. Any service used as fallback *must* return the IP as a plain string.
Add your own custom HTTPS endpoints to get the public IP from. They will only be used if everything else fails. Any service used as fallback _must_ return the IP as a plain string.
@default []
*/
readonly fallbackUrls?: string[];
@example
```
import publicIp = require('public-ip');
(async () => {
await publicIp.v6({
fallbackUrls: [
'https://ifconfig.co/ip'
]
});
})();
```
*/
readonly fallbackUrls?: readonly string[];
}

type CancelablePromise<T> = Promise<T> & {
Expand All @@ -31,7 +44,7 @@ declare const publicIp: {
/**
Get your public IP address - very fast!
In Node.js, it queries the DNS records of OpenDNS, Google DNS and HTTPS services to determine your IP address. In browsers, it uses the excellent [icanhaz](https://github.com/major/icanhaz) and [ipify](https://ipify.org) services through HTTPS.
In Node.js, it queries the DNS records of OpenDNS, Google DNS, and HTTPS services to determine your IP address. In browsers, it uses the excellent [icanhaz](https://github.com/major/icanhaz) and [ipify](https://ipify.org) services through HTTPS.
@returns Your public IPv4 address. A `.cancel()` method is available on the promise, which can be used to cancel the request.
@throws On error or timeout.
Expand All @@ -51,7 +64,7 @@ declare const publicIp: {
/**
Get your public IP address - very fast!
In Node.js, it queries the DNS records of OpenDNS, Google DNS and HTTPS services to determine your IP address. In browsers, it uses the excellent [icanhaz](https://github.com/major/icanhaz) and [ipify](https://ipify.org) services through HTTPS.
In Node.js, it queries the DNS records of OpenDNS, Google DNS, and HTTPS services to determine your IP address. In browsers, it uses the excellent [icanhaz](https://github.com/major/icanhaz) and [ipify](https://ipify.org) services through HTTPS.
@returns Your public IPv6 address. A `.cancel()` method is available on the promise, which can be used to cancel the request.
@throws On error or timeout.
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Get your public IP address - very fast!",
"license": "MIT",
"repository": "sindresorhus/public-ip",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
Expand Down Expand Up @@ -41,8 +42,8 @@
"devDependencies": {
"ava": "^2.2.0",
"sinon": "^7.4.1",
"tsd": "^0.7.4",
"xo": "^0.24.0"
"tsd": "^0.11.0",
"xo": "^0.25.3"
},
"browser": "browser.js",
"xo": {
Expand Down
29 changes: 17 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

> Get your public IP address - very fast!
In Node.js, it queries the DNS records of OpenDNS, Google DNS and HTTPS services to determine your IP address. In browsers, it uses the excellent [icanhaz](https://github.com/major/icanhaz) and [ipify](https://ipify.org) services through HTTPS.

In Node.js, it queries the DNS records of OpenDNS, Google DNS, and HTTPS services to determine your IP address. In browsers, it uses the excellent [icanhaz](https://github.com/major/icanhaz) and [ipify](https://ipify.org) services through HTTPS.

## Install

```
$ npm install public-ip
```


## Usage

```js
Expand All @@ -26,7 +24,6 @@ const publicIp = require('public-ip');
})();
```


## API

### publicIp.v4(options?)
Expand All @@ -40,34 +37,42 @@ Type: `object`

##### onlyHttps

Type: `boolean`<br>
Type: `boolean`\
Default: `false`

Use a HTTPS check using the [icanhazip.com](https://github.com/major/icanhaz) service instead of the DNS query. [ipify.org](https://www.ipify.org) is used as a fallback if `icanhazip.com` fails. This check is much more secure and tamper-proof, but also a lot slower. **This option is only available in the Node.js version**. Default behaviour is to check aginst DNS before using HTTPS fallback, if set as `true` it will *only* check against HTTPS.
Use a HTTPS check using the [icanhazip.com](https://github.com/major/icanhaz) service instead of the DNS query. [ipify.org](https://www.ipify.org) is used as a fallback if `icanhazip.com` fails. This check is much more secure and tamper-proof, but also a lot slower. **This option is only available in the Node.js version**. The default behaviour is to check aginst DNS before using HTTPS fallback. If set to `true`, it will *only* check against HTTPS.

##### fallbackUrls

Type: `string[]`<br>
Type: `string[]`\
Default: `[]`

In case you want to add your own custom HTTPS endpoints to get public IP from (like [ifconfig.co](https://ifconfig.co), for example), you can set them here. They will only be used if everything else fails. Any service used as fallback *must* return the IP as a plain string.
Add your own custom HTTPS endpoints to get the public IP from. They will only be used if everything else fails. Any service used as fallback *must* return the IP as a plain string.

Example: `{ fallbackUrls: [ 'https://ifconfig.co/ip' ] }`
```js
const publicIp = require('public-ip');

(async () => {
await publicIp.v6({
fallbackUrls: [
'https://ifconfig.co/ip'
]
});
})();
```

##### timeout

Type: `number`<br>
Type: `number`\
Default: `5000`

The time in milliseconds until a request is considered timed out.


## Maintainers

- [Sindre Sorhus](https://github.com/sindresorhus)
- [silverwind](https://github.com/silverwind)


## Related

- [public-ip-cli](https://github.com/sindresorhus/public-ip-cli) - CLI for this module
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ test('IPv4 HTTPS cancellation', async t => {
});

// Impossible DNS timeouts seems unreliable to test on a working connection
// because of caches, so we're only testing HTTPS
// because of caches, so we're only testing HTTPS.

test('IPv4 HTTPS impossible timeout', async t => {
await t.throwsAsync(publicIp.v4({onlyHttps: true, timeout: 1}));
Expand Down

0 comments on commit b0e683c

Please sign in to comment.