Skip to content

Commit 73df689

Browse files
committed
Fix: use curl for NIF download instead of :httpc
OTP apps (:inets, :ssl, :public_key) are not fully available during dependency compilation. Use curl which handles HTTPS, redirects, and SSL out of the box.
1 parent 22b1a3c commit 73df689

1 file changed

Lines changed: 12 additions & 48 deletions

File tree

lib/tailwind_compiler/native.ex

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -102,54 +102,18 @@ defmodule TailwindCompiler.Native do
102102
"https://github.com/#{@github_repo}/releases/download/v#{@version}/tailwind_compiler-nif-#{target_triple}.tar.gz"
103103
end
104104

105-
defp download_and_extract(url, dest, redirects_left \\ 5) do
106-
if redirects_left <= 0 do
107-
{:error, :too_many_redirects}
108-
else
109-
Application.ensure_all_started(:inets)
110-
Application.ensure_all_started(:ssl)
111-
112-
http_opts = [ssl: ssl_opts()]
113-
114-
case :httpc.request(:get, {String.to_charlist(url), []}, http_opts, body_format: :binary) do
115-
{:ok, {{_, 200, _}, _headers, body}} ->
116-
:erl_tar.extract({:binary, body}, [:compressed, {:cwd, String.to_charlist(dest)}])
117-
118-
{:ok, {{_, status, _}, headers, _body}} when status in [301, 302, 303, 307, 308] ->
119-
case List.keyfind(headers, ~c"location", 0) do
120-
{_, location} ->
121-
download_and_extract(List.to_string(location), dest, redirects_left - 1)
122-
123-
nil ->
124-
{:error, "HTTP #{status} without Location header"}
125-
end
126-
127-
{:ok, {{_, status, _}, _headers, _body}} ->
128-
{:error, "HTTP #{status}"}
129-
130-
{:error, reason} ->
131-
{:error, reason}
132-
end
133-
end
134-
end
135-
136-
defp ssl_opts do
137-
try do
138-
Application.ensure_all_started(:public_key)
139-
cacerts = :public_key.cacerts_get()
140-
141-
[
142-
verify: :verify_peer,
143-
cacerts: cacerts,
144-
depth: 3,
145-
customize_hostname_check: [
146-
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
147-
]
148-
]
149-
rescue
150-
_ ->
151-
# Fallback when :public_key is unavailable during dep compilation
152-
[verify: :verify_none]
105+
defp download_and_extract(url, dest) do
106+
tar_path = Path.join(dest, "nif.tar.gz")
107+
108+
case System.cmd("curl", ["-fsSL", "-o", tar_path, url], stderr_to_stdout: true) do
109+
{_, 0} ->
110+
result = :erl_tar.extract(String.to_charlist(tar_path), [:compressed, {:cwd, String.to_charlist(dest)}])
111+
File.rm(tar_path)
112+
result
113+
114+
{output, _} ->
115+
File.rm(tar_path)
116+
{:error, String.trim(output)}
153117
end
154118
end
155119

0 commit comments

Comments
 (0)