From 60ed3cd297c4045b90f4114a74e5baa4ef1c5056 Mon Sep 17 00:00:00 2001 From: Tim van der Meij Date: Sun, 28 Nov 2021 18:35:43 +0100 Subject: [PATCH] Fix compatibility with Node.js 17 in `test/test.js` Node.js 17, which as of writing is the most recent version, contains a breaking change in its DNS resolver, causing Firefox not to start anymore in our test framework. The inline comment together with the following resources provide more background: - https://github.com/nodejs/node/issues/40702 - https://github.com/nodejs/node/pull/39987 - https://github.com/cyrus-and/chrome-remote-interface/issues/467 - https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V17.md#other-notable-changes - https://github.com/DeviceFarmer/adbkit/issues/209 - https://nodejs.org/api/dns.html#dnssetdefaultresultorderorder This commit ensures that versions both older and newer than Node.js 17 work as expected. This is mainly necessary since the bots as of writing run Node.js 14.17.0 which is from before this API got introduced and for example Node.js 12 LTS is only end-of-life in April 2022, so we have to keep support for those older versions unfortunately. --- test/test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/test.js b/test/test.js index 29fab3376133a..a5eb456c08306 100644 --- a/test/test.js +++ b/test/test.js @@ -24,8 +24,24 @@ var os = require("os"); var puppeteer = require("puppeteer"); var url = require("url"); var testUtils = require("./testutils.js"); +const dns = require("dns"); const yargs = require("yargs"); +// Chrome uses host `127.0.0.1` in the browser's websocket endpoint URL while +// Firefox uses `localhost`, which before Node.js 17 also resolved to the IPv4 +// address `127.0.0.1` by Node.js' DNS resolver. However, this behavior changed +// in Node.js 17 where the default is to prefer an IPv6 address if one is +// offered (which varies based on the OS and/or how the `localhost` hostname +// resolution is configured), so it can now also resolve to `::1`. This causes +// Firefox to not start anymore since it doesn't bind on the `::1` interface. +// To avoid this, we switch Node.js' DNS resolver back to preferring IPv4 +// since we connect to a local browser anyway. Only do this for Node.js versions +// that actually have this API since it got introduced in Node.js 14.18.0 and +// it's not relevant for older versions anyway. +if (dns.setDefaultResultOrder !== undefined) { + dns.setDefaultResultOrder("ipv4first"); +} + function parseOptions() { yargs .usage("Usage: $0")