Skip to content

Commit

Permalink
Don't throw CancelError if ipv6() resolves first (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb committed Mar 17, 2024
1 parent 8c467ed commit 5490c81
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
node-version:
- 18
- 16
- 14
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
1 change: 1 addition & 0 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function createPublicIp(publicIpv4, publicIpv6) {
const promise = (async () => {
try {
const ipv6 = await ipv6Promise;
ipv4Promise.catch(() => {}); // Don't throw when cancelling
ipv4Promise.cancel();
return ipv6;
} catch (ipv6Error) {
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
},
"devDependencies": {
"ava": "^4.3.0",
"esmock": "^2.6.4",
"sinon": "^14.0.0",
"time-span": "^5.0.0",
"tsd": "^0.21.0",
Expand All @@ -64,6 +65,9 @@
"serial": true,
"files": [
"test.js"
],
"nodeArguments": [
"--loader=esmock"
]
}
}
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import process from 'node:process';
import test from 'ava';
import esmock from 'esmock';
import {isIPv6, isIPv4} from 'is-ip';
import timeSpan from 'time-span';
import dnsStub from './mocks/dns-socket.js';
Expand Down Expand Up @@ -90,6 +91,21 @@ if (!process.env.CI) {
test('IPv6 HTTPS', async t => {
t.true(isIPv6(await publicIpv6({onlyHttps: true})));
});

test('IPv4 call cancels after IPv6 call succeeds first', async t => {
const {publicIp} = await esmock('./index.js', {
publicIpv4(...arguments_) {
const promise = publicIpv4(...arguments_);

const returnValue = new Promise(() => {});
returnValue.cancel = promise.cancel;

return returnValue;
},
});

t.true(isIPv6(await publicIp()));
});
}

test.serial('IPv4 or IPv6', async t => {
Expand Down

0 comments on commit 5490c81

Please sign in to comment.