11defmodule Diff.Hex do
22 @ behaviour Diff.Hex.Behaviour
33
4- @ config % {
5- :hex_core . default_config ( )
6- | http_adapter: { Diff.Hex.Adapter , % { } } ,
7- http_user_agent_fragment: "hexpm_diff"
8- }
4+ defp config ( ) do
5+ config = % {
6+ :hex_core . default_config ( )
7+ | http_adapter: { Diff.Hex.Adapter , % { } } ,
8+ http_user_agent_fragment: "hexpm_diff" ,
9+ repo_url: Application . fetch_env! ( :diff , :repo_url )
10+ }
11+
12+ if repo_public_key = Application . get_env ( :diff , :repo_public_key ) do
13+ % { config | repo_public_key: repo_public_key }
14+ else
15+ % { config | repo_verify: false }
16+ end
17+ end
918
1019 @ max_file_size 1024 * 1024
1120
1221 require Logger
1322
1423 def get_versions ( ) do
15- with { :ok , { 200 , _ , results } } <- :hex_repo . get_versions ( @ config ) do
24+ with { :ok , { 200 , _ , results } } <- :hex_repo . get_versions ( config ( ) ) do
1625 { :ok , results }
1726 else
1827 { :ok , { status , _ , _ } } ->
@@ -26,14 +35,17 @@ defmodule Diff.Hex do
2635 end
2736
2837 def get_tarball ( package , version ) do
29- with { :ok , { 200 , _ , tarball } } <- :hex_repo . get_tarball ( @ config , package , version ) do
30- { :ok , tarball }
31- else
32- { :ok , { 403 , _ , _ } } ->
38+ path = Diff.TmpDir . tmp_file ( "tarball" )
39+
40+ case :hex_repo . get_tarball_to_file ( config ( ) , package , version , to_charlist ( path ) ) do
41+ { :ok , { 200 , _headers } } ->
42+ { :ok , path }
43+
44+ { :ok , { 403 , _ } } ->
3345 { :error , :not_found }
3446
35- { :ok , { status , _ , _ } } ->
36- Logger . error ( "Failed to get package versions . Status: #{ status } ." )
47+ { :ok , { status , _ } } ->
48+ Logger . error ( "Failed to get tarball for package: #{ package } . Status: #{ status } ." )
3749 { :error , :not_found }
3850
3951 { :error , reason } ->
@@ -42,16 +54,15 @@ defmodule Diff.Hex do
4254 end
4355 end
4456
45- def unpack_tarball ( tarball , path ) when is_binary ( path ) do
46- path = to_charlist ( path )
47-
48- with { :ok , _ } <- :hex_tarball . unpack ( tarball , path ) do
57+ def unpack_tarball ( tarball_path , output_path ) do
58+ with { :ok , _ } <-
59+ :hex_tarball . unpack ( { :file , to_charlist ( tarball_path ) } , to_charlist ( output_path ) ) do
4960 :ok
5061 end
5162 end
5263
5364 def get_checksums ( package , versions ) do
54- with { :ok , { 200 , _ , releases } } <- :hex_repo . get_package ( @ config , package ) do
65+ with { :ok , { 200 , _ , releases } } <- :hex_repo . get_package ( config ( ) , package ) do
5566 checksums =
5667 for release <- releases . releases , release . version in versions do
5768 release . outer_checksum
@@ -73,12 +84,11 @@ defmodule Diff.Hex do
7384 end
7485
7586 def diff ( package , from , to ) do
76- path_from = tmp_path ( "package-#{ package } -#{ from } -" )
77- path_to = tmp_path ( "package-#{ package } -#{ to } -" )
78-
7987 with { :ok , tarball_from } <- get_tarball ( package , from ) ,
88+ path_from = Diff.TmpDir . tmp_dir ( "package-#{ package } -#{ from } " ) ,
8089 :ok <- unpack_tarball ( tarball_from , path_from ) ,
8190 { :ok , tarball_to } <- get_tarball ( package , to ) ,
91+ path_to = Diff.TmpDir . tmp_dir ( "package-#{ package } -#{ to } " ) ,
8292 :ok <- unpack_tarball ( tarball_to , path_to ) do
8393 from_files = tree_files ( path_from )
8494 to_files = tree_files ( path_to )
@@ -92,8 +102,7 @@ defmodule Diff.Hex do
92102 all_files = ( from_files ++ to_files ) |> Enum . uniq ( ) |> Enum . sort ( )
93103
94104 stream =
95- all_files
96- |> Stream . flat_map ( fn file ->
105+ Stream . flat_map ( all_files , fn file ->
97106 { path_old , path_new } =
98107 cond do
99108 file in new_files -> { "/dev/null" , Path . join ( path_to , file ) }
@@ -120,14 +129,6 @@ defmodule Diff.Hex do
120129 [ { :error , { error , reason } } ]
121130 end
122131 end )
123- |> Stream . transform (
124- fn -> :ok end ,
125- fn elem , :ok -> { [ elem ] , :ok } end ,
126- fn :ok ->
127- File . rm_rf ( path_from )
128- File . rm_rf ( path_to )
129- end
130- )
131132
132133 { :ok , stream }
133134 else
@@ -166,9 +167,4 @@ defmodule Diff.Hex do
166167 |> Enum . filter ( & File . regular? ( & 1 , raw: true ) )
167168 |> Enum . map ( & Path . relative_to ( & 1 , directory ) )
168169 end
169-
170- defp tmp_path ( prefix ) do
171- random_string = Base . encode16 ( :crypto . strong_rand_bytes ( 4 ) )
172- Path . join ( [ System . tmp_dir! ( ) , "diff" , prefix <> random_string ] )
173- end
174170end
0 commit comments