diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d50ada6..6a82b18 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,7 +12,6 @@ jobs: node-version: - 18 - 16 - - 14 steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 diff --git a/core.js b/core.js index fde3053..16c7c91 100644 --- a/core.js +++ b/core.js @@ -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) { diff --git a/package.json b/package.json index c9f3df9..59a0e47 100644 --- a/package.json +++ b/package.json @@ -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", @@ -64,6 +65,9 @@ "serial": true, "files": [ "test.js" + ], + "nodeArguments": [ + "--loader=esmock" ] } } diff --git a/test.js b/test.js index cc83be2..44755ae 100644 --- a/test.js +++ b/test.js @@ -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'; @@ -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 => {