Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ and you can override the default version.
Available configurations:
- `:version` the binary release version to use (see [numscript-wasm releases](https://github.com/PagoPlus/numscript-wasm/releases/)).
- `:retries` number of times to retry the download in case of a network failure.
- `:binary_path` where the WASM binary file will be downloaded

Ex:
```elixir
config :numscriptex,
binary_path: :numscriptex |> :code.priv_dir() |> Path.join("numscript.wasm")
version: "0.0.2",
retries: 3
```
Expand Down
4 changes: 4 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Config

config :numscriptex,
binary_path: System.get_env("NUMSCRIPTEX_BINARY_PATH")
29 changes: 19 additions & 10 deletions lib/numscriptex/assets_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,35 @@ defmodule Numscriptex.AssetsManager do
@retries Application.compile_env(:numscriptex, :retries, 3)
@numscript_checksums_url "https://github.com/PagoPlus/numscript-wasm/releases/download/v#{@numscript_wasm_version}/numscript_checksums.txt"
@numscript_wasm_url "https://github.com/PagoPlus/numscript-wasm/releases/download/v#{@numscript_wasm_version}/numscript.wasm"
@binary_path :numscriptex
|> :code.priv_dir()
|> Path.join("numscript.wasm")
|> to_charlist()

@default_binary_path :numscriptex
|> :code.priv_dir()
|> Path.join("numscript.wasm")
|> to_charlist()

defmacro ensure_wasm_binary_is_valid do
quote do
require Logger

File.mkdir_p!(:code.priv_dir(:numscriptex))

if File.exists?(unquote(@binary_path)) do
if File.exists?(unquote(binary_path())) do
unquote(maybe_retry_download(compare_checksums()))
else
unquote(maybe_retry_download(download_and_compare_binary()))
end
end
end

def binary_path, do: @binary_path
def binary_path do
path = Application.get_env(:numscriptex, :binary_path, @default_binary_path)

if !is_nil(path) && path != "" do
to_charlist(path)
else
@default_binary_path
end
end

def hash_wasm_binary do
# Logic explanation:
Expand All @@ -42,7 +51,7 @@ defmodule Numscriptex.AssetsManager do
# 3. Uses `:crypto.hash_final/1` to finalize the streaming hash calculation;
# 4. Encode the hash to the hexadecimal base (also the same as the checksums).
quote do
File.stream!(unquote(@binary_path), 1024)
File.stream!(unquote(binary_path()), 1024)
|> Enum.reduce(:crypto.hash_init(:sha256), fn line, acc ->
:crypto.hash_update(acc, line)
end)
Expand Down Expand Up @@ -124,7 +133,7 @@ defmodule Numscriptex.AssetsManager do
:get,
{unquote(@numscript_wasm_url), []},
[],
stream: unquote(@binary_path)
stream: unquote(binary_path())
)

case request do
Expand All @@ -138,7 +147,7 @@ defmodule Numscriptex.AssetsManager do

{:error, reason} ->
Logger.error(
"Failed to download Numscript-WASM binary. Reason: #{inspect(reason)}. Binary path: #{unquote(@binary_path)}"
"Failed to download Numscript-WASM binary. Reason: #{inspect(reason)}. Binary path: #{unquote(binary_path())}"
)

raise CompileError
Expand All @@ -148,7 +157,7 @@ defmodule Numscriptex.AssetsManager do

defp maybe_delete_wasm_binary do
quote do
case File.rm(unquote(@binary_path)) do
case File.rm(unquote(binary_path())) do
:ok ->
:ok

Expand Down
12 changes: 12 additions & 0 deletions test/numscriptex/assets_manager_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,17 @@ defmodule Numscriptex.AssetsManagerTest do

File.copy!(dest_path, binary_path)
end

test "uses custom binary path when configured" do
new_binary_path = System.tmp_dir() |> Path.join("test_binary.wasm") |> to_charlist()

refute AssetsManager.binary_path() == new_binary_path

Application.put_env(:numscriptex, :binary_path, new_binary_path)

assert AssetsManager.binary_path() == new_binary_path

on_exit(fn -> Application.delete_env(:numscriptex, :binary_path) end)
end
end
end