-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use named exports and allow getting either IPv4 or IPv6 (#63)
- Loading branch information
Showing
10 changed files
with
174 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import AggregateError from 'aggregate-error'; // Use built-in when targeting Node.js 16 | ||
|
||
export class IpNotFoundError extends Error { | ||
constructor(options) { | ||
super('Could not get the public IP address', options); | ||
this.name = 'IpNotFoundError'; | ||
} | ||
} | ||
|
||
export function createPublicIp(publicIpv4, publicIpv6) { | ||
return function publicIp(options) { // eslint-disable-line func-names | ||
const ipv4Promise = publicIpv4(options); | ||
const ipv6Promise = publicIpv6(options); | ||
|
||
const promise = (async () => { | ||
try { | ||
const ipv6 = await ipv6Promise; | ||
ipv4Promise.cancel(); | ||
return ipv6; | ||
} catch (ipv6Error) { | ||
if (!(ipv6Error instanceof IpNotFoundError)) { | ||
throw ipv6Error; | ||
} | ||
|
||
try { | ||
return await ipv4Promise; | ||
} catch (ipv4Error) { | ||
throw new AggregateError([ipv4Error, ipv6Error]); | ||
} | ||
} | ||
})(); | ||
|
||
promise.cancel = () => { | ||
ipv4Promise.cancel(); | ||
ipv6Promise.cancel(); | ||
}; | ||
|
||
return promise; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
import {expectType} from 'tsd'; | ||
import publicIp, {CancelablePromise} from './index.js'; | ||
import {publicIp, publicIpv4, publicIpv6, CancelablePromise} from './index.js'; | ||
|
||
expectType<CancelablePromise<string>>(publicIp.v4()); | ||
expectType<CancelablePromise<string>>(publicIp.v4({onlyHttps: true})); | ||
expectType<CancelablePromise<string>>(publicIp.v4({timeout: 10})); | ||
expectType<CancelablePromise<string>>(publicIp.v4({fallbackUrls: ['https://ifconfig.io']})); | ||
publicIp.v4().cancel(); | ||
expectType<CancelablePromise<string>>(publicIpv4()); | ||
expectType<CancelablePromise<string>>(publicIpv4({onlyHttps: true})); | ||
expectType<CancelablePromise<string>>(publicIpv4({timeout: 10})); | ||
expectType<CancelablePromise<string>>(publicIpv4({fallbackUrls: ['https://ifconfig.io']})); | ||
publicIpv4().cancel(); | ||
|
||
expectType<CancelablePromise<string>>(publicIp.v6()); | ||
expectType<CancelablePromise<string>>(publicIp.v6({onlyHttps: true})); | ||
expectType<CancelablePromise<string>>(publicIp.v6({timeout: 10})); | ||
expectType<CancelablePromise<string>>(publicIp.v6({fallbackUrls: ['https://ifconfig.io']})); | ||
publicIp.v6().cancel(); | ||
expectType<CancelablePromise<string>>(publicIpv6()); | ||
expectType<CancelablePromise<string>>(publicIpv6({onlyHttps: true})); | ||
expectType<CancelablePromise<string>>(publicIpv6({timeout: 10})); | ||
expectType<CancelablePromise<string>>(publicIpv6({fallbackUrls: ['https://ifconfig.io']})); | ||
publicIpv6().cancel(); | ||
|
||
expectType<CancelablePromise<string>>(publicIp()); | ||
expectType<CancelablePromise<string>>(publicIp({onlyHttps: true})); | ||
expectType<CancelablePromise<string>>(publicIp({timeout: 10})); | ||
expectType<CancelablePromise<string>>(publicIp({fallbackUrls: ['https://ifconfig.io']})); | ||
publicIp().cancel(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
// Comment out the `is-ip` dependency, launch a local server, and then load the HTMl file. | ||
import publicIp from './browser.js'; | ||
import publicIp, {publicIpv4} from './browser.js'; | ||
|
||
console.log('IP:', await publicIp.v4()); | ||
console.log('IP:', await publicIp.v4({ | ||
console.log('IP:', await publicIpv4()); | ||
console.log('IP:', await publicIpv4({ | ||
fallbackUrls: [ | ||
'https://ifconfig.me', | ||
], | ||
})); | ||
console.log('IP:', await publicIp()); |
Oops, something went wrong.