Skip to content

Commit c2c54a4

Browse files
Merge pull request #256 from permaweb/chore/lua-script-body-or-data
chore: fix httpsig+ans104 compatibility in `lua@5.3a`
2 parents 8ff776e + 4c6b7fc commit c2c54a4

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

src/dev_lua.erl

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,35 @@ find_scripts(Base, Opts) ->
9090
load_scripts(Scripts, Opts) -> load_scripts(Scripts, Opts, []).
9191
load_scripts([], _Opts, Acc) ->
9292
{ok, lists:reverse(Acc)};
93-
load_scripts([ScriptID | Rest], Opts, Acc) when is_binary(ScriptID) ->
93+
load_scripts([ScriptID | Rest], Opts, Acc) when ?IS_ID(ScriptID) ->
9494
case hb_cache:read(ScriptID, Opts) of
95-
{ok, Script} ->
96-
% when loaded by arweave id the result is a map
97-
% with data, so we want to parse the code from the
98-
% data and add to the tuple
99-
Code = case is_map(Script) of
100-
true -> hb_ao:get(<<"data">>, Script, #{});
101-
false -> Script
102-
end,
103-
load_scripts(Rest, Opts, [{ScriptID, Code}|Acc]);
95+
{ok, Script} when is_binary(Script) ->
96+
% The ID referred to a binary script item, so we add it to the list
97+
% as-is.
98+
load_scripts(Rest, Opts, [{ScriptID, Script}|Acc]);
99+
{ok, ScriptMsg} when is_map(ScriptMsg) ->
100+
% We read a message from the store, so we recurse upon the output,
101+
% as if the script message had beeen given directly.
102+
load_scripts([ScriptMsg|Rest], Opts, Acc);
104103
not_found ->
105104
{error, #{
106105
<<"status">> => 404,
107-
<<"body">> =>
108-
<<"Lua script '", ScriptID/binary, "' not available.">>
106+
<<"body">> => <<"Lua script '", ScriptID/binary, "' not found.">>
109107
}}
110108
end;
111109
load_scripts([Script | Rest], Opts, Acc) when is_map(Script) ->
112-
% We have found a message with a direct Lua script. Load it.
113-
case hb_ao:get(<<"body">>, Script, Opts) of
110+
% We have found a message with a Lua script inside. Search for the binary
111+
% of the program in the body and the data.
112+
ScriptBin =
113+
hb_ao:get_first(
114+
[
115+
{Script, <<"body">>},
116+
{Script, <<"data">>}
117+
],
118+
Script,
119+
Opts
120+
),
121+
case ScriptBin of
114122
not_found ->
115123
{error, #{
116124
<<"status">> => 404,
@@ -433,8 +441,10 @@ simple_invocation_test() ->
433441
?assertEqual(2, hb_ao:get(<<"assoctable/b">>, Base, #{})).
434442

435443
load_scripts_by_id_test() ->
444+
% Start a node to ensure the HTTP services are available.
445+
_Node = hb_http_server:start_node(#{}),
436446
Script = <<"DosEHUAqhl_O5FH3vDqPlgGsG92Guxcm6nrwqnjsDKg">>,
437-
{ok, Acc} = load_scripts([Script], #{}, []),
447+
{ok, Acc} = load_scripts([Script], #{}),
438448
[{_,Code}|_] = Acc,
439449
<<Prefix:8/binary, _/binary>> = Code,
440450
?assertEqual(<<"function">>, Prefix).

0 commit comments

Comments
 (0)