Skip to content

Commit

Permalink
update hex_core
Browse files Browse the repository at this point in the history
  • Loading branch information
cgerling committed May 4, 2023
1 parent d353c23 commit 099c74b
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/mix_hex_core.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

%% @doc
%% `hex_core' entrypoint module.
Expand Down
4 changes: 2 additions & 2 deletions src/mix_hex_core.hrl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

-define(HEX_CORE_VERSION, "0.8.2").
-define(HEX_CORE_VERSION, "0.9.0").
2 changes: 1 addition & 1 deletion src/mix_hex_erl_tar.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

%% @private
%% Copied from https://github.com/erlang/otp/blob/OTP-20.0.1/lib/stdlib/src/erl_tar.erl
Expand Down
2 changes: 1 addition & 1 deletion src/mix_hex_erl_tar.hrl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

% Copied from https://github.com/erlang/otp/blob/OTP-20.0.1/lib/stdlib/src/erl_tar.hrl

Expand Down
2 changes: 1 addition & 1 deletion src/mix_hex_filename.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

% @private
% Excerpt from https://github.com/erlang/otp/blob/OTP-20.0.1/lib/stdlib/src/filename.erl#L761-L788
Expand Down
2 changes: 1 addition & 1 deletion src/mix_hex_http.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

%% @doc
%% HTTP contract.
Expand Down
2 changes: 1 addition & 1 deletion src/mix_hex_http_httpc.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

%% @doc
%% httpc-based implementation of {@link mix_hex_http} contract.
Expand Down
4 changes: 3 additions & 1 deletion src/mix_hex_licenses.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

%% @doc
%% Hex Licenses.
%% File generated by spdx.ex. Do not edit manually.

-module(mix_hex_licenses).
Expand Down
2 changes: 1 addition & 1 deletion src/mix_hex_pb_names.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

%% -*- coding: utf-8 -*-
%% @private
Expand Down
2 changes: 1 addition & 1 deletion src/mix_hex_pb_package.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

%% -*- coding: utf-8 -*-
%% @private
Expand Down
2 changes: 1 addition & 1 deletion src/mix_hex_pb_signed.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

%% -*- coding: utf-8 -*-
%% @private
Expand Down
2 changes: 1 addition & 1 deletion src/mix_hex_pb_versions.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

%% -*- coding: utf-8 -*-
%% @private
Expand Down
23 changes: 10 additions & 13 deletions src/mix_hex_registry.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

%% @doc
%% Functions for encoding and decoding Hex registries.
Expand Down Expand Up @@ -51,13 +51,12 @@ encode_names(Names) ->

%% @private
decode_names(Payload, no_verify) ->
#{packages := Packages} = mix_hex_pb_names:decode_msg(Payload, 'Names'),
{ok, Packages};
{ok, mix_hex_pb_names:decode_msg(Payload, 'Names')};

decode_names(Payload, Repository) ->
case mix_hex_pb_names:decode_msg(Payload, 'Names') of
#{repository := Repository, packages := Packages} ->
{ok, Packages};
#{repository := Repository, packages := _Packages} = Result ->
{ok, Result};
_ ->
{error, unverified}
end.
Expand All @@ -82,13 +81,12 @@ encode_versions(Versions) ->

%% @private
decode_versions(Payload, no_verify) ->
#{packages := Packages} = mix_hex_pb_versions:decode_msg(Payload, 'Versions'),
{ok, Packages};
{ok, mix_hex_pb_versions:decode_msg(Payload, 'Versions')};

decode_versions(Payload, Repository) ->
case mix_hex_pb_versions:decode_msg(Payload, 'Versions') of
#{repository := Repository, packages := Packages} ->
{ok, Packages};
#{repository := Repository, packages := _Packages} = Result ->
{ok, Result};
_ ->
{error, unverified}
end.
Expand All @@ -113,13 +111,12 @@ encode_package(Package) ->

%% @private
decode_package(Payload, no_verify, no_verify) ->
#{releases := Releases} = mix_hex_pb_package:decode_msg(Payload, 'Package'),
{ok, Releases};
{ok, mix_hex_pb_package:decode_msg(Payload, 'Package')};

decode_package(Payload, Repository, Package) ->
case mix_hex_pb_package:decode_msg(Payload, 'Package') of
#{repository := Repository, name := Package, releases := Releases} ->
{ok, Releases};
#{repository := Repository, name := Package, releases := _Releases} = Result ->
{ok, Result};
_ ->
{error, unverified}
end.
Expand Down
97 changes: 85 additions & 12 deletions src/mix_hex_repo.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

%% @doc
%% Repo API.
Expand All @@ -7,7 +7,10 @@
get_names/1,
get_versions/1,
get_package/2,
get_tarball/3
get_tarball/3,
get_docs/3,
get_public_key/1,
get_installs/1
]).

%%====================================================================
Expand All @@ -29,12 +32,8 @@
%% '''
%% @end
get_names(Config) when is_map(Config) ->
Verify = maps:get(repo_verify_origin, Config, true),
Decoder = fun(Data) ->
case Verify of
true -> mix_hex_registry:decode_names(Data, repo_name(Config));
false -> mix_hex_registry:decode_names(Data, no_verify)
end
mix_hex_registry:decode_names(Data, verify_repo(Config))
end,
get_protobuf(Config, <<"names">>, Decoder).

Expand All @@ -55,12 +54,8 @@ get_names(Config) when is_map(Config) ->
%% '''
%% @end
get_versions(Config) when is_map(Config) ->
Verify = maps:get(repo_verify_origin, Config, true),
Decoder = fun(Data) ->
case Verify of
true -> mix_hex_registry:decode_versions(Data, repo_name(Config));
false -> mix_hex_registry:decode_versions(Data, no_verify)
end
mix_hex_registry:decode_versions(Data, verify_repo(Config))
end,
get_protobuf(Config, <<"versions">>, Decoder).

Expand Down Expand Up @@ -111,6 +106,68 @@ get_tarball(Config, Name, Version) ->
Other
end.

%% @doc
%% Gets docs tarball from the repository.
%%
%% Examples:
%%
%% ```
%% > {ok, {200, _, Docs}} = mix_hex_repo:get_tarball(mix_hex_core:default_config(), <<"package1">>, <<"1.0.0">>),
%% > {ok, [{"index.html", <<"<!doctype>">>}, ...]} = mix_hex_tarball:unpack_docs(Docs, memory)
%% '''
get_docs(Config, Name, Version) ->
ReqHeaders = make_headers(Config),

case get(Config, docs_url(Config, Name, Version), ReqHeaders) of
{ok, {200, RespHeaders, Docs}} ->
{ok, {200, RespHeaders, Docs}};

Other ->
Other
end.

%% @doc
%% Gets the public key from the repository.
%%
%% Examples:
%%
%% ```
%% > mix_hex_repo:get_public_key(mix_hex_core:default_config())
%% {ok, {200, _, PublicKey}}
%% '''
get_public_key(Config) ->
ReqHeaders = make_headers(Config),
URI = build_url(Config, <<"public_key">>),

case get(Config, URI, ReqHeaders) of
{ok, {200, RespHeaders, PublicKey}} ->
{ok, {200, RespHeaders, PublicKey}};

Other ->
Other
end.

%% @doc
%% Gets a CSV with the installs for Hex 1.x.
%%
%% Examples:
%%
%% ```
%% > mix_hex_repo:get_installs(mix_hex_core:default_config())
%% {ok, {200, _, Installs}}
%% '''
get_installs(Config) ->
ReqHeaders = make_headers(Config),
URI = build_url(Config, <<"installs/hex-1.x.csv">>),

case get(Config, URI, ReqHeaders) of
{ok, {200, RespHeaders, Installs}} ->
{ok, {200, RespHeaders, Installs}};

Other ->
Other
end.

%%====================================================================
%% Internal functions
%%====================================================================
Expand Down Expand Up @@ -156,6 +213,13 @@ decode(Signed, PublicKey, Decoder, Config) ->
Decoder(Payload)
end.

%% @private
verify_repo(Config) ->
case maps:get(repo_verify_origin, Config, true) of
true -> repo_name(Config);
false -> no_verify
end.

%% @private
repo_name(#{repo_organization := Name}) when is_binary(Name) -> Name;
repo_name(#{repo_name := Name}) when is_binary(Name) -> Name.
Expand All @@ -165,6 +229,11 @@ tarball_url(Config, Name, Version) ->
Filename = tarball_filename(Name, Version),
build_url(Config, <<"tarballs/", Filename/binary>>).

%% @private
docs_url(Config, Name, Version) ->
Filename = docs_filename(Name, Version),
build_url(Config, <<"docs/", Filename/binary>>).

%% @private
build_url(#{repo_url := URI, repo_organization := Org}, Path) when is_binary(Org) ->
<<URI/binary, "/repos/", Org/binary, "/", Path/binary>>;
Expand All @@ -177,6 +246,10 @@ build_url(Config, Path) ->
tarball_filename(Name, Version) ->
<<Name/binary, "-", Version/binary, ".tar">>.

%% @private
docs_filename(Name, Version) ->
<<Name/binary, "-", Version/binary, ".tar.gz">>.

%% @private
make_headers(Config) ->
maps:fold(fun set_header/3, #{}, Config).
Expand Down
13 changes: 8 additions & 5 deletions src/mix_hex_tarball.erl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

%% @doc
%% Functions for creating and unpacking Hex tarballs.
Expand Down Expand Up @@ -103,11 +103,14 @@ create_docs(Files, #{tarball_max_size := TarballMaxSize, tarball_max_uncompresse
Tarball = gzip(UncompressedTarball),
Size = byte_size(Tarball),

case(Size > TarballMaxSize) or (UncompressedSize > TarballMaxUncompressedSize) of
true ->
{error, {tarball, too_big}};
case {(Size > TarballMaxSize), (UncompressedSize > TarballMaxUncompressedSize)} of
{_, true} ->
{error, {tarball, {too_big_uncompressed, TarballMaxUncompressedSize}}};

false ->
{true, _} ->
{error, {tarball, {too_big_compressed, TarballMaxSize}}};

{false, false} ->
{ok, Tarball}
end.

Expand Down
2 changes: 1 addition & 1 deletion src/mix_safe_erl_term.xrl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Vendored from hex_core v0.8.2, do not edit manually
%% Vendored from hex_core v0.9.0 (130c1a7), do not edit manually

%%% Author : Robert Virding
%%% Purpose : Token definitions for Erlang.
Expand Down

0 comments on commit 099c74b

Please sign in to comment.