-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
40 lines (34 loc) · 944 Bytes
/
core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
};
}