@@ -24,20 +24,7 @@ defmodule RustlerPrecompiled do
24
24
25
25
* `:crate` - The name of Rust crate if different from the `:otp_app`. This is optional.
26
26
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.
41
28
42
29
* `:version` - The version of precompiled assets (it is part of the NIF filename).
43
30
@@ -275,19 +262,13 @@ defmodule RustlerPrecompiled do
275
262
276
263
@ native_dir "priv/native"
277
264
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
-
284
265
@ 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.
286
267
287
268
The module name is the one that defined the NIF and this information
288
269
is stored in a metadata file.
289
270
"""
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
291
272
nif_module
292
273
|> metadata_file ( )
293
274
|> read_map_from_file ( )
@@ -305,13 +286,6 @@ defmodule RustlerPrecompiled do
305
286
306
287
@ doc false
307
288
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
315
289
case metadata do
316
290
% {
317
291
targets: targets ,
@@ -346,27 +320,22 @@ defmodule RustlerPrecompiled do
346
320
variants = Map . fetch! ( variants , target_triple )
347
321
348
322
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
+ )
351
327
end
352
328
end
353
329
354
330
defp maybe_variants_tar_gz_urls ( _ , _ , _ , _ ) , do: [ ]
355
331
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
-
363
332
@ 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.
365
334
366
335
It is in the plural because a target may have some variants for it.
367
336
It receives the NIF module.
368
337
"""
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
370
339
metadata =
371
340
nif_module
372
341
|> metadata_file ( )
@@ -393,10 +362,9 @@ defmodule RustlerPrecompiled do
393
362
394
363
defp tar_gz_urls ( base_url , basename , version , nif_version , target_triple , variants ) do
395
364
lib_name = lib_name ( basename , version , nif_version , target_triple )
396
- lib_name_with_ext = lib_name_with_ext ( target_triple , lib_name )
397
365
398
366
[
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 ) )
400
368
| maybe_variants_tar_gz_urls ( variants , base_url , target_triple , lib_name )
401
369
]
402
370
end
@@ -648,7 +616,7 @@ defmodule RustlerPrecompiled do
648
616
649
617
# `cache_base_dir` is a "private" option used only in tests.
650
618
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" )
652
620
653
621
{ :ok ,
654
622
Map . merge ( basic_metadata , % {
@@ -873,34 +841,21 @@ defmodule RustlerPrecompiled do
873
841
"so"
874
842
end
875
843
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 } "
882
845
end
883
846
884
- defp tar_gz_file_url ( { base_url , request_headers } , file_name ) do
847
+ defp tar_gz_file_url ( base_url , file_name ) do
885
848
uri = URI . parse ( base_url )
886
849
887
850
uri =
888
851
Map . update! ( uri , :path , fn path ->
889
- Path . join ( path || "" , file_name )
852
+ Path . join ( path || "" , " #{ file_name } .tar.gz" )
890
853
end )
891
854
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 )
901
856
end
902
857
903
- defp download_nif_artifact ( { url , request_headers } ) do
858
+ defp download_nif_artifact ( url ) do
904
859
url = String . to_charlist ( url )
905
860
Logger . debug ( "Downloading NIF from #{ url } " )
906
861
@@ -941,10 +896,7 @@ defmodule RustlerPrecompiled do
941
896
942
897
options = [ body_format: :binary ]
943
898
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
948
900
{ :ok , { { _ , 200 , _ } , _headers , body } } ->
949
901
{ :ok , body }
950
902
@@ -961,17 +913,16 @@ defmodule RustlerPrecompiled do
961
913
attempts = max_retries ( options )
962
914
963
915
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 ) }
966
917
967
918
cache_dir = cache_dir ( "precompiled_nifs" )
968
919
:ok = File . mkdir_p ( cache_dir )
969
920
970
921
Enum . flat_map ( download_results , fn result ->
971
- with { :download , { lib_name , download_result } } <- { :download , result } ,
922
+ with { :download , { url , download_result } } <- { :download , result } ,
972
923
{ :download_result , { :ok , body } } <- { :download_result , download_result } ,
973
924
hash <- :crypto . hash ( @ checksum_algo , body ) ,
974
- path <- Path . join ( cache_dir , lib_name ) ,
925
+ path <- Path . join ( cache_dir , basename_from_url ( url ) ) ,
975
926
{ :file , :ok } <- { :file , File . write ( path , body ) } do
976
927
checksum = Base . encode16 ( hash , case: :lower )
977
928
@@ -981,7 +932,7 @@ defmodule RustlerPrecompiled do
981
932
982
933
[
983
934
% {
984
- lib_name: lib_name ,
935
+ url: url ,
985
936
path: path ,
986
937
checksum: checksum ,
987
938
checksum_algo: @ checksum_algo
@@ -1035,6 +986,14 @@ defmodule RustlerPrecompiled do
1035
986
end )
1036
987
end
1037
988
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
+
1038
997
defp read_map_from_file ( file ) do
1039
998
with { :ok , contents } <- File . read ( file ) ,
1040
999
{ % { } = contents , _ } <- Code . eval_string ( contents ) do
0 commit comments