From 1d8d2fd636a40c107d47d0a87869930fdd0f82d0 Mon Sep 17 00:00:00 2001 From: Leandro Pereira Date: Wed, 11 Oct 2023 11:02:30 -0400 Subject: [PATCH] Replace brotli with ex_brotli (#360) ex_brotli is distributed as a precompiled rust binary so it avoids issues compiling the C source code required by brotli --- lib/beacon/loader.ex | 4 ++-- lib/beacon/runtime_css.ex | 4 ++-- lib/beacon/runtime_js.ex | 4 ++-- mix.exs | 2 +- mix.lock | 2 +- test/beacon/runtime_css_test.exs | 17 +++++++++++++++++ test/beacon/runtime_js_test.exs | 17 +++++++++++++++++ 7 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 test/beacon/runtime_css_test.exs create mode 100644 test/beacon/runtime_js_test.exs diff --git a/lib/beacon/loader.ex b/lib/beacon/loader.ex index 63015b69..4d5b286c 100644 --- a/lib/beacon/loader.ex +++ b/lib/beacon/loader.ex @@ -68,7 +68,7 @@ defmodule Beacon.Loader do end defp load_site_from_db(site) do - with :ok <- Beacon.RuntimeJS.load(), + with :ok <- Beacon.RuntimeJS.load!(), :ok <- load_runtime_css(site), :ok <- load_stylesheets(site), :ok <- load_components(site), @@ -135,7 +135,7 @@ defmodule Beacon.Loader do if Code.ensure_loaded?(Mix.Project) and Mix.env() == :test do defp load_runtime_css(_site), do: :ok else - defp load_runtime_css(site), do: Beacon.RuntimeCSS.load(site) + defp load_runtime_css(site), do: Beacon.RuntimeCSS.load!(site) end defp load_stylesheets(site) do diff --git a/lib/beacon/runtime_css.ex b/lib/beacon/runtime_css.ex index 99cae7e2..28d64ae1 100644 --- a/lib/beacon/runtime_css.ex +++ b/lib/beacon/runtime_css.ex @@ -19,10 +19,10 @@ defmodule Beacon.RuntimeCSS do end @doc false - def load(site) do + def load!(site) do {:ok, css} = compile(site) - case :brotli.encode(css) do + case ExBrotli.compress(css) do {:ok, compressed} -> hash = Base.encode16(:crypto.hash(:md5, css), case: :lower) true = :ets.insert(:beacon_assets, {{site, :css}, {hash, css, compressed}}) diff --git a/lib/beacon/runtime_js.ex b/lib/beacon/runtime_js.ex index 45be182f..45ebb8cd 100644 --- a/lib/beacon/runtime_js.ex +++ b/lib/beacon/runtime_js.ex @@ -40,10 +40,10 @@ defmodule Beacon.RuntimeJS do end end - def load do + def load! do js = build() - case :brotli.encode(js) do + case ExBrotli.compress(js) do {:ok, compressed} -> hash = Base.encode16(:crypto.hash(:md5, js), case: :lower) true = :ets.insert(:beacon_assets, {:js, {hash, js, compressed}}) diff --git a/mix.exs b/mix.exs index 71e13b47..1f3c700a 100644 --- a/mix.exs +++ b/mix.exs @@ -35,12 +35,12 @@ defmodule Beacon.MixProject do defp deps do [ {:accent, "~> 1.1"}, - {:brotli, "~> 0.3.2"}, {:bypass, "~> 2.1", only: :test}, {:credo, "~> 1.6", only: [:dev, :test], runtime: false}, {:dialyxir, "~> 1.2", only: :dev, runtime: false}, {:ecto_sql, "~> 3.6"}, {:esbuild, "~> 0.5", only: :dev}, + {:ex_brotli, "~> 0.3"}, {:ex_doc, "~> 0.29", only: :docs}, {:ex_aws, "~> 2.4"}, {:ex_aws_s3, "~> 2.4"}, diff --git a/mix.lock b/mix.lock index 719ae81a..dcee9a17 100644 --- a/mix.lock +++ b/mix.lock @@ -1,6 +1,5 @@ %{ "accent": {:hex, :accent, "1.1.1", "20257356446d45078b19b91608f74669b407b39af891ee3db9ee6824d1cae19d", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:plug, "~> 1.3", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "6d5afa50d4886e3370e04fa501468cbaa6c4b5fe926f72ccfa844ad9e259adae"}, - "brotli": {:hex, :brotli, "0.3.2", "59cf45a399098516f1d34f70d8e010e5c9bf326659d3ef34c7cc56793339002b", [:rebar3], [], "hexpm", "9ec3ef9c753f80d0c657b4905193c55e5198f169fa1d1c044d8601d4d931a2ad"}, "bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"}, "bypass": {:hex, :bypass, "2.1.0", "909782781bf8e20ee86a9cabde36b259d44af8b9f38756173e8f5e2e1fabb9b1", [:mix], [{:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "d9b5df8fa5b7a6efa08384e9bbecfe4ce61c77d28a4282f79e02f1ef78d96b80"}, "castore": {:hex, :castore, "1.0.3", "7130ba6d24c8424014194676d608cb989f62ef8039efd50ff4b3f33286d06db8", [:mix], [], "hexpm", "680ab01ef5d15b161ed6a95449fac5c6b8f60055677a8e79acf01b27baa4390b"}, @@ -21,6 +20,7 @@ "esbuild": {:hex, :esbuild, "0.7.0", "ce3afb13cd2c5fd63e13c0e2d0e0831487a97a7696cfa563707342bb825d122a", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "4ae9f4f237c5ebcb001390b8ada65a12fb2bb04f3fe3d1f1692b7a06fbfe8752"}, "ex_aws": {:hex, :ex_aws, "2.4.3", "6c6d88ba7b9c07e3b0f4b70406d5fccb9f5358f5ef18138f7bd396f7863e8255", [:mix], [{:configparser_ex, "~> 4.0", [hex: :configparser_ex, repo: "hexpm", optional: true]}, {:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:jsx, "~> 2.8 or ~> 3.0", [hex: :jsx, repo: "hexpm", optional: true]}, {:mime, "~> 1.2 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:sweet_xml, "~> 0.7", [hex: :sweet_xml, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "67f61f8b6aec740150d483a21f551fabce26a481d9917305ed2bb47717007519"}, "ex_aws_s3": {:hex, :ex_aws_s3, "2.4.0", "ce8decb6b523381812798396bc0e3aaa62282e1b40520125d1f4eff4abdff0f4", [:mix], [{:ex_aws, "~> 2.0", [hex: :ex_aws, repo: "hexpm", optional: false]}, {:sweet_xml, ">= 0.0.0", [hex: :sweet_xml, repo: "hexpm", optional: true]}], "hexpm", "85dda6e27754d94582869d39cba3241d9ea60b6aa4167f9c88e309dc687e56bb"}, + "ex_brotli": {:hex, :ex_brotli, "0.3.0", "69d5f3720df70d5c89d1395d8fbe49ba37466b626834aaf6d77c72e0c93cf975", [:mix], [{:phoenix, ">= 0.0.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:rustler, "~> 0.29", [hex: :rustler, repo: "hexpm", optional: true]}, {:rustler_precompiled, "~> 0.6", [hex: :rustler_precompiled, repo: "hexpm", optional: false]}], "hexpm", "8e46982f7d20069419ca8c8c54f9f3ebd9fa0e1d094c54cbf8ce3d636d84dfa7"}, "ex_doc": {:hex, :ex_doc, "0.29.4", "6257ecbb20c7396b1fe5accd55b7b0d23f44b6aa18017b415cb4c2b91d997729", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "2c6699a737ae46cb61e4ed012af931b57b699643b24dabe2400a8168414bc4f5"}, "expo": {:hex, :expo, "0.4.1", "1c61d18a5df197dfda38861673d392e642649a9cef7694d2f97a587b2cfb319b", [:mix], [], "hexpm", "2ff7ba7a798c8c543c12550fa0e2cbc81b95d4974c65855d8d15ba7b37a1ce47"}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, diff --git a/test/beacon/runtime_css_test.exs b/test/beacon/runtime_css_test.exs new file mode 100644 index 00000000..c873fa7e --- /dev/null +++ b/test/beacon/runtime_css_test.exs @@ -0,0 +1,17 @@ +defmodule Beacon.RuntimeCSSTest do + use BeaconWeb.ConnCase, async: false + + alias Beacon.RuntimeCSS + + @site :my_site + + setup_all do + start_supervised!({Beacon.Loader, Beacon.Config.fetch!(@site)}) + :ok + end + + test "load!" do + assert RuntimeCSS.load!(@site) == :ok + assert @site |> RuntimeCSS.fetch() |> :erlang.size() > 100 + end +end diff --git a/test/beacon/runtime_js_test.exs b/test/beacon/runtime_js_test.exs new file mode 100644 index 00000000..3edab9d9 --- /dev/null +++ b/test/beacon/runtime_js_test.exs @@ -0,0 +1,17 @@ +defmodule Beacon.RuntimeJSTest do + use BeaconWeb.ConnCase, async: false + + alias Beacon.RuntimeJS + + @site :my_site + + setup_all do + start_supervised!({Beacon.Loader, Beacon.Config.fetch!(@site)}) + :ok + end + + test "load" do + assert RuntimeJS.load!() == :ok + assert RuntimeJS.fetch() |> :erlang.size() > 100 + end +end