Skip to content

Commit

Permalink
Update libraries (#16)
Browse files Browse the repository at this point in the history
* Update local dev env Erlang/Elixir versions

* Add new github actions for testing Elixir 1.15.6 and Erlang 26.1

* Updating ssl_verify_fun for Erlang compatibility

* Updating libraries

* Deactivate flakey test till refactoring
  • Loading branch information
imsoulfly authored Oct 8, 2023
1 parent 2b79ab4 commit 06b7c47
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 56 deletions.
24 changes: 19 additions & 5 deletions lib/local_hex/mirror/hex_api.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
defmodule LocalHex.Mirror.HexApi do
@moduledoc false
require Logger

@callback fetch_hexpm_names(LocalHex.Repository.t()) :: {:ok, binary} | {:error, any}
@callback fetch_hexpm_versions(LocalHex.Repository.t()) :: {:ok, binary} | {:error, any}
@callback fetch_hexpm_package(LocalHex.Repository.t(), binary) :: {:ok, binary} | {:error, any}
@callback fetch_hexpm_tarball(LocalHex.Repository.t(), binary, binary) ::
{:ok, binary} | {:error, any}

def fetch_hexpm_names(repository), do: impl().fetch_hexpm_names(repository)
def fetch_hexpm_versions(repository), do: impl().fetch_hexpm_versions(repository)
def fetch_hexpm_package(repository, name), do: impl().fetch_hexpm_package(repository, name)
def fetch_hexpm_names(repository) do
Logger.debug("#{inspect(impl())} fetch_hexpm_names #{inspect(repository)}")
impl().fetch_hexpm_names(repository)
end

def fetch_hexpm_versions(repository) do
Logger.debug("#{inspect(impl())} fetch_hexpm_versions")
impl().fetch_hexpm_versions(repository)
end

def fetch_hexpm_tarball(repository, name, version),
do: impl().fetch_hexpm_tarball(repository, name, version)
def fetch_hexpm_package(repository, name) do
Logger.debug("#{inspect(impl())} fetch_hexpm_package")
impl().fetch_hexpm_package(repository, name)
end

def fetch_hexpm_tarball(repository, name, version) do
Logger.debug("#{inspect(impl())} fetch_hexpm_tarball")
impl().fetch_hexpm_tarball(repository, name, version)
end

defp impl do
Application.get_env(:local_hex, :hex_api, LocalHex.Mirror.HexPm)
Expand Down
47 changes: 31 additions & 16 deletions lib/local_hex/mirror/sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,12 @@ defmodule LocalHex.Mirror.Sync do

{:ok, package} = sync_package(mirror, name)

stream =
Task.Supervisor.async_stream_nolink(
LocalHex.TaskSupervisor,
package.releases,
fn release ->
:ok = sync_tarball(mirror, name, release.version)
release
end,
mirror_sync_opts
)

releases = for {:ok, release} <- stream, do: release
releases =
for release <- package.releases do
:ok = sync_tarball(mirror, name, release.version)
release
end

{name, releases}
end,
mirror_sync_opts
Expand Down Expand Up @@ -216,57 +210,67 @@ defmodule LocalHex.Mirror.Sync do
end

defp sync_names(mirror) do
Logger.debug("#{inspect(__MODULE__)} sync_names")

with {:ok, signed} <- HexApi.fetch_hexpm_names(mirror),
{:ok, %{packages: names}} <- decode_hexpm_names(mirror, signed),
signed_names <- encode_names(mirror, names),
:ok <- Storage.write_names(mirror, signed_names) do
{:ok, names}
else
other ->
Logger.warn("#{inspect(__MODULE__)} sync_names failed: #{inspect(other)}")
Logger.error("#{inspect(__MODULE__)} sync_names failed: #{inspect(other)}")
other
end
end

defp sync_versions(mirror) do
Logger.debug("#{inspect(__MODULE__)} sync_versions")

with {:ok, signed} <- HexApi.fetch_hexpm_versions(mirror),
{:ok, %{packages: versions}} <- decode_hexpm_versions(mirror, signed),
signed_versions <- encode_versions(mirror, versions),
:ok <- Storage.write_versions(mirror, signed_versions) do
{:ok, versions}
else
other ->
Logger.warn("#{inspect(__MODULE__)} sync_versions failed: #{inspect(other)}")
Logger.error("#{inspect(__MODULE__)} sync_versions failed: #{inspect(other)}")
other
end
end

defp sync_package(mirror, name) do
Logger.debug("#{inspect(__MODULE__)} sync_package")

with {:ok, signed} <- HexApi.fetch_hexpm_package(mirror, name),
{:ok, package} <- decode_hexpm_package(mirror, signed, name),
signed_package <- encode_package(mirror, name, package),
:ok <- Storage.write_package(mirror, name, signed_package) do
{:ok, package}
else
other ->
Logger.warn("#{inspect(__MODULE__)} sync_package failed: #{inspect(other)}")
Logger.error("#{inspect(__MODULE__)} sync_package failed: #{inspect(other)}")
other
end
end

defp sync_tarball(mirror, name, version) do
Logger.debug("#{inspect(__MODULE__)} sync_tarball")

with {:ok, tarball} <- HexApi.fetch_hexpm_tarball(mirror, name, version),
{:ok, package} <- Package.load_from_tarball(tarball),
:ok <- Storage.write_package_tarball(mirror, package) do
:ok
else
other ->
Logger.warn("#{inspect(__MODULE__)} sync_tarball failed: #{inspect(other)}")
Logger.error("#{inspect(__MODULE__)} sync_tarball failed: #{inspect(other)}")
other
end
end

defp encode_names(repository, names) do
Logger.debug("#{inspect(__MODULE__)} encode_names")

protobuf =
:hex_registry.encode_names(%{
repository: repository.name,
Expand All @@ -277,6 +281,8 @@ defmodule LocalHex.Mirror.Sync do
end

defp encode_versions(repository, versions) do
Logger.debug("#{inspect(__MODULE__)} encode_versions")

protobuf =
:hex_registry.encode_versions(%{
repository: repository.name,
Expand All @@ -287,6 +293,8 @@ defmodule LocalHex.Mirror.Sync do
end

defp encode_package(repository, name, package) do
Logger.debug("#{inspect(__MODULE__)} encode_package")

protobuf =
:hex_registry.encode_package(%{
repository: repository.name,
Expand All @@ -298,27 +306,34 @@ defmodule LocalHex.Mirror.Sync do
end

defp sign_and_gzip(repository, protobuf) do
Logger.debug("#{inspect(__MODULE__)} sign_and_gzip")

protobuf
|> :hex_registry.sign_protobuf(repository.private_key)
|> :zlib.gzip()
end

defp decode_hexpm_names(repository, body) do
Logger.debug("#{inspect(__MODULE__)} decode_hexpm_names")
{:ok, payload} = decode_and_verify_signed(body, repository)
:hex_registry.decode_names(payload, repository.options.upstream_name)
end

defp decode_hexpm_versions(repository, body) do
Logger.debug("#{inspect(__MODULE__)} decode_hexpm_versions")
{:ok, payload} = decode_and_verify_signed(body, repository)
:hex_registry.decode_versions(payload, repository.options.upstream_name)
end

defp decode_hexpm_package(repository, body, name) do
Logger.debug("#{inspect(__MODULE__)} decode_hexpm_package")
{:ok, payload} = decode_and_verify_signed(body, repository)
:hex_registry.decode_package(payload, repository.options.upstream_name, name)
end

defp decode_and_verify_signed(body, repository) do
Logger.debug("#{inspect(__MODULE__)} decode_and_verify_signed")

body
|> :zlib.gunzip()
|> :hex_registry.decode_and_verify_signed(repository.options.upstream_public_key)
Expand Down
2 changes: 1 addition & 1 deletion lib/local_hex/package.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ defmodule LocalHex.Package do
end

defp build_dependencies(metadata) do
Logger.warn([
Logger.error([
inspect(__MODULE__),
" loading package ",
metadata["name"] || metadata["app"],
Expand Down
Loading

0 comments on commit 06b7c47

Please sign in to comment.