-module(lightspeed@transport@wisp_html).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/lightspeed/transport/wisp_html.gleam").
-export([render_initial/4, status/1, body/1]).
-export_type([html_request/0, html_response/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(" Wisp-style server-rendered initial HTML adapter.\n").
-type html_request() :: {html_request, binary(), binary(), binary(), binary()}.
-type html_response() :: {html_response,
integer(),
list({binary(), binary()}),
binary()}.
-file("src/lightspeed/transport/wisp_html.gleam", 101).
-spec escape_attr(binary()) -> binary().
escape_attr(Value) ->
_pipe = Value,
_pipe@1 = gleam@string:replace(_pipe, <<"&"/utf8>>, <<"&"/utf8>>),
_pipe@2 = gleam@string:replace(_pipe@1, <<"\""/utf8>>, <<"""/utf8>>),
_pipe@3 = gleam@string:replace(_pipe@2, <<"<"/utf8>>, <<"<"/utf8>>),
gleam@string:replace(_pipe@3, <<">"/utf8>>, <<">"/utf8>>).
-file("src/lightspeed/transport/wisp_html.gleam", 63).
-spec build_document(html_request(), binary(), binary(), binary()) -> binary().
build_document(Request, Rendered_html, Websocket_path, Owner) ->
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"<!doctype html>"/utf8,
"<html lang=\"en\">"/utf8>>/binary,
"<head><meta charset=\"utf-8\"><title>Lightspeed</title></head>"/utf8>>/binary,
"<body>"/utf8>>/binary,
"<main id=\"app\""/utf8>>/binary,
" data-ls-session=\""/utf8>>/binary,
(escape_attr(
erlang:element(
2,
Request
)
))/binary>>/binary,
"\""/utf8>>/binary,
" data-ls-owner=\""/utf8>>/binary,
(escape_attr(
Owner
))/binary>>/binary,
"\""/utf8>>/binary,
" data-ls-route=\""/utf8>>/binary,
(escape_attr(
erlang:element(
3,
Request
)
))/binary>>/binary,
"\""/utf8>>/binary,
" data-ls-ws=\""/utf8>>/binary,
(escape_attr(
Websocket_path
))/binary>>/binary,
"\""/utf8>>/binary,
" data-ls-protocol=\""/utf8>>/binary,
(escape_attr(
<<"lightspeed"/utf8>>
))/binary>>/binary,
"\""/utf8>>/binary,
" data-ls-version=\""/utf8>>/binary,
(erlang:integer_to_binary(1))/binary>>/binary,
"\""/utf8>>/binary,
" data-ls-patch-stream-version=\""/utf8>>/binary,
(erlang:integer_to_binary(1))/binary>>/binary,
"\""/utf8>>/binary,
">"/utf8>>/binary,
Rendered_html/binary>>/binary,
"</main>"/utf8>>/binary,
"</body></html>"/utf8>>.
-file("src/lightspeed/transport/wisp_html.gleam", 25).
?DOC(" Render initial HTML with adapter metadata markers.\n").
-spec render_initial(
html_request(),
binary(),
binary(),
lightspeed@transport@contract:auth_hook()
) -> {ok, html_response()} |
{error, lightspeed@transport@contract:adapter_error()}.
render_initial(Request, Rendered_html, Websocket_path, Auth_hook) ->
Context = {auth_context,
erlang:element(2, Request),
erlang:element(3, Request),
erlang:element(4, Request),
erlang:element(5, Request)},
case lightspeed@transport@contract:authenticate(Auth_hook, Context) of
{denied, Reason} ->
{error, {authentication_failed, Reason}};
{authorized, Owner} ->
{ok,
{html_response,
200,
[{<<"content-type"/utf8>>,
<<"text/html; charset=utf-8"/utf8>>}],
build_document(
Request,
Rendered_html,
Websocket_path,
Owner
)}}
end.
-file("src/lightspeed/transport/wisp_html.gleam", 54).
?DOC(" Access response status.\n").
-spec status(html_response()) -> integer().
status(Response) ->
erlang:element(2, Response).
-file("src/lightspeed/transport/wisp_html.gleam", 59).
?DOC(" Access response body.\n").
-spec body(html_response()) -> binary().
body(Response) ->
erlang:element(4, Response).