Skip to content

Commit

Permalink
Require Node.js 14 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Dec 27, 2021
1 parent 4fcb341 commit e53d7fe
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 174 deletions.
2 changes: 2 additions & 0 deletions browser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {default} from './index.js';
export * from './index.js';
32 changes: 21 additions & 11 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const isIp = require('is-ip');
import isIp from 'is-ip';

class CancelError extends Error {
export class CancelError extends Error {
constructor() {
super('Request was cancelled');
this.name = 'CancelError';
Expand All @@ -13,18 +12,18 @@ class CancelError extends Error {
}

const defaults = {
timeout: 5000
timeout: 5000,
};

const urls = {
v4: [
'https://ipv4.icanhazip.com/',
'https://api.ipify.org/'
'https://api.ipify.org/',
],
v6: [
'https://ipv6.icanhazip.com/',
'https://api6.ipify.org/'
]
'https://api6.ipify.org/',
],
};

const sendXhr = (url, options, version) => {
Expand Down Expand Up @@ -63,21 +62,28 @@ const sendXhr = (url, options, version) => {
const queryHttps = (version, options) => {
let request;
const promise = (async function () {
const urls_ = [].concat.apply(urls[version], options.fallbackUrls || []);
const urls_ = [
...urls[version],
...(options.fallbackUrls ?? []),
];

let lastError;
for (const url of urls_) {
try {
request = sendXhr(url, options, version);
// eslint-disable-next-line no-await-in-loop
const ip = await request;
return ip;
} catch (error) {
lastError = error;

if (error instanceof CancelError) {
throw error;
}
}
}

throw new Error('Couldn\'t find your IP');
throw new Error('Could not find your IP address', {cause: lastError});
})();

promise.cancel = () => {
Expand All @@ -87,6 +93,10 @@ const queryHttps = (version, options) => {
return promise;
};

module.exports.v4 = options => queryHttps('v4', {...defaults, ...options});
const publicIp = {};

publicIp.v4 = options => queryHttps('v4', {...defaults, ...options});

publicIp.v6 = options => queryHttps('v6', {...defaults, ...options});

module.exports.v6 = options => queryHttps('v6', {...defaults, ...options});
export default publicIp;
104 changes: 51 additions & 53 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
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__. The default behaviour is to check against DNS before using HTTPS fallback. If set to `true`, it will _only_ check against HTTPS.
@default false
*/
readonly onlyHttps?: boolean;

/**
The time in milliseconds until a request is considered timed out.
@default 5000
*/
readonly timeout?: number;

/**
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 []
@example
```
import publicIp = require('public-ip');
(async () => {
await publicIp.v6({
fallbackUrls: [
'https://ifconfig.co/ip'
]
});
})();
```
*/
readonly fallbackUrls?: readonly string[];
}

type CancelablePromise<T> = Promise<T> & {
cancel(): void;
};
export 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__.
The default behaviour is to check against DNS before using HTTPS fallback. If set to `true`, it will _only_ check against HTTPS.
@default false
*/
readonly onlyHttps?: boolean;

/**
The time in milliseconds until a request is considered timed out.
@default 5000
*/
readonly timeout?: number;

/**
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 []
@example
```
import publicIp from 'public-ip';
await publicIp.v6({
fallbackUrls: [
'https://ifconfig.co/ip'
]
});
```
*/
readonly fallbackUrls?: readonly string[];
}

export type CancelablePromise<T> = Promise<T> & {
cancel(): void;
};

declare const publicIp: {
/**
Get your public IP address - very fast!
Expand All @@ -51,15 +51,13 @@ declare const publicIp: {
@example
```
import publicIp = require('public-ip');
import publicIp from 'public-ip';
(async () => {
console.log(await publicIp.v4());
//=> '46.5.21.123'
})();
console.log(await publicIp.v4());
//=> '46.5.21.123'
```
*/
v4(options?: publicIp.Options): publicIp.CancelablePromise<string>;
v4(options?: Options): CancelablePromise<string>;

/**
Get your public IP address - very fast!
Expand All @@ -71,15 +69,15 @@ declare const publicIp: {
@example
```
import publicIp = require('public-ip');
import publicIp from 'public-ip';
(async () => {
console.log(await publicIp.v6());
//=> 'fe80::200:f8ff:fe21:67cf'
})();
console.log(await publicIp.v6());
//=> 'fe80::200:f8ff:fe21:67cf'
```
*/
v6(options?: publicIp.Options): publicIp.CancelablePromise<string>;
v6(options?: Options): CancelablePromise<string>;
};

export = publicIp;
export default publicIp;

export {CancelError} from 'got';
Loading

0 comments on commit e53d7fe

Please sign in to comment.