Skip to content

Commit bd858bd

Browse files
committed
Revert "Add support for headers and dynamic base URL (#65) (#77)"
This reverts commit ae78223. This change is temporary, since we need to release a patch first.
1 parent ae78223 commit bd858bd

File tree

3 files changed

+148
-341
lines changed

3 files changed

+148
-341
lines changed

lib/rustler_precompiled.ex

Lines changed: 29 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,7 @@ defmodule RustlerPrecompiled do
2424
2525
* `:crate` - The name of Rust crate if different from the `:otp_app`. This is optional.
2626
27-
* `:base_url` - Location where to find the NIFs from. This should be one of the following:
28-
29-
* A URL to a directory containing the NIFs. The name of the NIF will be appended to it
30-
and a GET request will be made. Works well with public GitHub releases.
31-
32-
* A tuple of `{URL, headers}`. The headers should be a list of key-value pairs.
33-
This is useful when the NIFs are hosted in a private server.
34-
35-
* A tuple of `{module, function}` where the `function` is an atom representing the function
36-
name in that module. It's expected a function of arity 1, where the NIF file name is given,
37-
and it should return a URL or a tuple of `{URL, headers}`.
38-
This should be used for all cases not covered by the above.
39-
For example when multiple requests have to be made, like when using a private GitHub release
40-
through the GitHub API, or when the URLs don't resemble a simple directory.
27+
* `:base_url` - A valid URL that is used as base path for the NIF file.
4128
4229
* `:version` - The version of precompiled assets (it is part of the NIF filename).
4330
@@ -275,19 +262,13 @@ defmodule RustlerPrecompiled do
275262

276263
@native_dir "priv/native"
277264

278-
@doc deprecated: "Use available_nifs/1 instead"
279-
def available_nif_urls(nif_module) when is_atom(nif_module) do
280-
available_nifs(nif_module)
281-
|> Enum.map(fn {_lib_name, {url, _headers}} -> url end)
282-
end
283-
284265
@doc """
285-
Returns URLs for NIFs based on its module name as a list of tuples: `[{lib_name, {url, headers}}]`.
266+
Returns URLs for NIFs based on its module name.
286267
287268
The module name is the one that defined the NIF and this information
288269
is stored in a metadata file.
289270
"""
290-
def available_nifs(nif_module) when is_atom(nif_module) do
271+
def available_nif_urls(nif_module) when is_atom(nif_module) do
291272
nif_module
292273
|> metadata_file()
293274
|> read_map_from_file()
@@ -305,13 +286,6 @@ defmodule RustlerPrecompiled do
305286

306287
@doc false
307288
def nif_urls_from_metadata(metadata) when is_map(metadata) do
308-
with {:ok, nifs} <- nifs_from_metadata(metadata) do
309-
{:ok, Enum.map(nifs, fn {_lib_name, {url, _headers}} -> url end)}
310-
end
311-
end
312-
313-
@doc false
314-
def nifs_from_metadata(metadata) when is_map(metadata) do
315289
case metadata do
316290
%{
317291
targets: targets,
@@ -346,27 +320,22 @@ defmodule RustlerPrecompiled do
346320
variants = Map.fetch!(variants, target_triple)
347321

348322
for variant <- variants do
349-
lib_name = lib_name_with_ext(target_triple, lib_name <> "--" <> Atom.to_string(variant))
350-
{lib_name, tar_gz_file_url(base_url, lib_name)}
323+
tar_gz_file_url(
324+
base_url,
325+
lib_name_with_ext(target_triple, lib_name <> "--" <> Atom.to_string(variant))
326+
)
351327
end
352328
end
353329

354330
defp maybe_variants_tar_gz_urls(_, _, _, _), do: []
355331

356-
@doc deprecated: "Use current_target_nifs/1 instead"
357-
def current_target_nif_urls(nif_module) when is_atom(nif_module) do
358-
nif_module
359-
|> current_target_nifs()
360-
|> Enum.map(fn {_lib_name, {url, _headers}} -> url end)
361-
end
362-
363332
@doc """
364-
Returns the file URLs to be downloaded for current target as a list of tuples: `[{lib_name, {url, headers}}]`.
333+
Returns the file URLs to be downloaded for current target.
365334
366335
It is in the plural because a target may have some variants for it.
367336
It receives the NIF module.
368337
"""
369-
def current_target_nifs(nif_module) when is_atom(nif_module) do
338+
def current_target_nif_urls(nif_module) when is_atom(nif_module) do
370339
metadata =
371340
nif_module
372341
|> metadata_file()
@@ -393,10 +362,9 @@ defmodule RustlerPrecompiled do
393362

394363
defp tar_gz_urls(base_url, basename, version, nif_version, target_triple, variants) do
395364
lib_name = lib_name(basename, version, nif_version, target_triple)
396-
lib_name_with_ext = lib_name_with_ext(target_triple, lib_name)
397365

398366
[
399-
{lib_name_with_ext, tar_gz_file_url(base_url, lib_name_with_ext(target_triple, lib_name))}
367+
tar_gz_file_url(base_url, lib_name_with_ext(target_triple, lib_name))
400368
| maybe_variants_tar_gz_urls(variants, base_url, target_triple, lib_name)
401369
]
402370
end
@@ -648,7 +616,7 @@ defmodule RustlerPrecompiled do
648616

649617
# `cache_base_dir` is a "private" option used only in tests.
650618
cache_dir = cache_dir(config.base_cache_dir, "precompiled_nifs")
651-
cached_tar_gz = Path.join(cache_dir, file_name)
619+
cached_tar_gz = Path.join(cache_dir, "#{file_name}.tar.gz")
652620

653621
{:ok,
654622
Map.merge(basic_metadata, %{
@@ -873,34 +841,21 @@ defmodule RustlerPrecompiled do
873841
"so"
874842
end
875843

876-
"#{lib_name}.#{ext}.tar.gz"
877-
end
878-
879-
defp tar_gz_file_url({module, function_name}, file_name)
880-
when is_atom(module) and is_atom(function_name) do
881-
apply(module, function_name, [file_name])
844+
"#{lib_name}.#{ext}"
882845
end
883846

884-
defp tar_gz_file_url({base_url, request_headers}, file_name) do
847+
defp tar_gz_file_url(base_url, file_name) do
885848
uri = URI.parse(base_url)
886849

887850
uri =
888851
Map.update!(uri, :path, fn path ->
889-
Path.join(path || "", file_name)
852+
Path.join(path || "", "#{file_name}.tar.gz")
890853
end)
891854

892-
{to_string(uri), request_headers}
893-
end
894-
895-
defp tar_gz_file_url(base_url, file_name) do
896-
tar_gz_file_url({base_url, []}, file_name)
897-
end
898-
899-
defp download_nif_artifact(url) when is_binary(url) do
900-
download_nif_artifact({url, []})
855+
to_string(uri)
901856
end
902857

903-
defp download_nif_artifact({url, request_headers}) do
858+
defp download_nif_artifact(url) do
904859
url = String.to_charlist(url)
905860
Logger.debug("Downloading NIF from #{url}")
906861

@@ -941,10 +896,7 @@ defmodule RustlerPrecompiled do
941896

942897
options = [body_format: :binary]
943898

944-
request_headers =
945-
Enum.map(request_headers, fn {k, v} when is_binary(k) -> {String.to_charlist(k), v} end)
946-
947-
case :httpc.request(:get, {url, request_headers}, http_options, options) do
899+
case :httpc.request(:get, {url, []}, http_options, options) do
948900
{:ok, {{_, 200, _}, _headers, body}} ->
949901
{:ok, body}
950902

@@ -961,17 +913,16 @@ defmodule RustlerPrecompiled do
961913
attempts = max_retries(options)
962914

963915
download_results =
964-
for {lib_name, url} <- urls,
965-
do: {lib_name, with_retry(fn -> download_nif_artifact(url) end, attempts)}
916+
for url <- urls, do: {url, with_retry(fn -> download_nif_artifact(url) end, attempts)}
966917

967918
cache_dir = cache_dir("precompiled_nifs")
968919
:ok = File.mkdir_p(cache_dir)
969920

970921
Enum.flat_map(download_results, fn result ->
971-
with {:download, {lib_name, download_result}} <- {:download, result},
922+
with {:download, {url, download_result}} <- {:download, result},
972923
{:download_result, {:ok, body}} <- {:download_result, download_result},
973924
hash <- :crypto.hash(@checksum_algo, body),
974-
path <- Path.join(cache_dir, lib_name),
925+
path <- Path.join(cache_dir, basename_from_url(url)),
975926
{:file, :ok} <- {:file, File.write(path, body)} do
976927
checksum = Base.encode16(hash, case: :lower)
977928

@@ -981,7 +932,7 @@ defmodule RustlerPrecompiled do
981932

982933
[
983934
%{
984-
lib_name: lib_name,
935+
url: url,
985936
path: path,
986937
checksum: checksum,
987938
checksum_algo: @checksum_algo
@@ -1035,6 +986,14 @@ defmodule RustlerPrecompiled do
1035986
end)
1036987
end
1037988

989+
defp basename_from_url(url) do
990+
uri = URI.parse(url)
991+
992+
uri.path
993+
|> String.split("/")
994+
|> List.last()
995+
end
996+
1038997
defp read_map_from_file(file) do
1039998
with {:ok, contents} <- File.read(file),
1040999
{%{} = contents, _} <- Code.eval_string(contents) do

lib/rustler_precompiled/config.ex

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,34 +83,16 @@ defmodule RustlerPrecompiled.Config do
8383

8484
defp validate_base_url!(nil), do: raise_for_nil_field_value(:base_url)
8585

86-
defp validate_base_url!(base_url) when is_binary(base_url) do
87-
validate_base_url!({base_url, []})
88-
end
89-
90-
defp validate_base_url!({base_url, headers}) when is_binary(base_url) and is_list(headers) do
86+
defp validate_base_url!(base_url) do
9187
case :uri_string.parse(base_url) do
9288
%{} ->
93-
if Enum.all?(headers, &match?({key, value} when is_binary(key) and is_binary(value), &1)) do
94-
{base_url, headers}
95-
else
96-
raise "`:base_url` headers for `RustlerPrecompiled` must be a list of `{binary(),binary()}`"
97-
end
89+
base_url
9890

9991
{:error, :invalid_uri, error} ->
10092
raise "`:base_url` for `RustlerPrecompiled` is invalid: #{inspect(to_string(error))}"
10193
end
10294
end
10395

104-
defp validate_base_url!({module, function}) when is_atom(module) and is_atom(function) do
105-
Code.ensure_compiled!(module)
106-
107-
if Kernel.function_exported?(module, function, 1) do
108-
{module, function}
109-
else
110-
raise "`:base_url` for `RustlerPrecompiled` is a function that does not exist: `#{inspect(module)}.#{function}/1`"
111-
end
112-
end
113-
11496
defp validate_list!(nil, option, _valid_values), do: raise_for_nil_field_value(option)
11597

11698
defp validate_list!([_ | _] = values, option, valid_values) do

0 commit comments

Comments
 (0)