Skip to content

Commit

Permalink
fallback to ipv4/ipv6 in the event of an unreachable host error (#74)
Browse files Browse the repository at this point in the history
If there is a request failure and the reason implies that the error is
due to the usage being ipv4 or ipv6, the alternative is set and the
request retries. A variable is used to determine if a retry should be
attempted or not. If it is false, this means that a retry is already
being attempted, and the request should raise.

This is based on:

https://github.com/elixir-lang/elixir/blob/9c42348f6407fa315bae00acffe433e9417e2c2e/lib/mix/lib/mix/utils.ex#L687
  • Loading branch information
Gazler authored Oct 18, 2024
1 parent ac23b69 commit c7d7bb8
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/esbuild/npm_registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ defmodule Esbuild.NpmRegistry do
tar
end

defp fetch_file!(url) do
case do_fetch(url) do
{:ok, {{_, 200, _}, _headers, body}} ->
defp fetch_file!(url, retry \\ true) do
case {retry, do_fetch(url)} do
{_, {:ok, {{_, 200, _}, _headers, body}}} ->
body

{true, {:error, {:failed_connect, [{:to_address, _}, {inet, _, reason}]}}}
when inet in [:inet, :inet6] and
reason in [:ehostunreach, :enetunreach, :eprotonosupport, :nxdomain] ->
:httpc.set_options(ipfamily: fallback(inet))
fetch_file!(url, false)

other ->
raise """
couldn't fetch #{url}: #{inspect(other)}
Expand All @@ -71,6 +77,9 @@ defmodule Esbuild.NpmRegistry do
end
end

defp fallback(:inet), do: :inet6
defp fallback(:inet6), do: :inet

defp do_fetch(url) do
scheme = URI.parse(url).scheme
url = String.to_charlist(url)
Expand Down

0 comments on commit c7d7bb8

Please sign in to comment.