<commit_before>-module(n2o_bullet).
-author('<NAME>').
-include_lib("n2o/include/wf.hrl").

-compile(export_all).
-export([init/4]).
-export([stream/3]).
-export([info/3]).
-export([terminate/2]).

% process spawn

init(_Transport, Req, _Opts, _Active) ->
    put(actions,[]),
    Ctx = wf_context:init_context(Req),
    NewCtx = wf_core:fold(init,Ctx#context.handlers,Ctx),
    wf_context:context(NewCtx),
    Res = ets:update_counter(globals,onlineusers,{2,1}),
    wf:reg(broadcast,wf:peer(Req)),
    wf:send(broadcast,{counter,Res}),
    Req1 = wf:header(<<"Access-Control-Allow-Origin">>, <<"*">>, NewCtx#context.req),
    {ok, Req1, NewCtx}.

% websocket handler

stream(<<"PING">> =Ping, Req, State) -> info(Ping,Req,State);
stream(<<"N2O",Rest/binary>> = Data, Req, State) -> info(Data,Req,State);
stream({text,Data}, Req, State) -> info(Data,Req,State);
stream({binary,Info}, Req, State) -> info(binary_to_term(Info,[safe]),Req,State);
stream(Data, Req, State) when is_binary(Data) -> info(binary_to_term(Data,[safe]),Req,State);
stream(Data, Req, State) -> info(Data,Req,State).

% WebSocketPid ! Message

info({client,Message}, Req, State) ->
    wf_context:clear_actions(),
%    wf:info("Client Message: ~p",[Message]),
    Module = State#context.module,
    try Module:event({client,Message}) catch E:R -> wf:info("Catch: ~p:~p~n~p", [E,R,n2o_error:stack()]) end,
    {reply,wf:json([{eval,iolist_to_binary(render_actions(get(actions)))},
                    {data,binary_to_list(term_to_binary(Message))}]),Req,State};

info({bert,Message}, Req, State) ->
    wf_context:clear_actions(),
    Module = State#context.module,
    Term = try Module:event({bert,Message}) catch E:R -> wf:info("Catch: ~p:~p~n~p", [E,R,n2o_error:stack()]), <<>> end,
    wf:info("Client BERT Binary Message: ~p Result: ~p",[Message,Term]),
    {reply,{binary,term_to_binary(Term)},Req,State};

info({binary,Message}, Req, State) ->
    wf_context:clear_actions(),
    Module = State#context.module,
    Term = try Module:event({binary,Message}) catch E:R -> wf:info("Catch: ~p:~p~n~p", [E,R,n2o_error:stack()]), <<>> end,
    wf:info("Client Raw Binary Message: ~p Result: ~p",[Message,Term]),
    Res = case Term of _ when is_binary(Term) -> Term; _ -> term_to_binary(Term) end,
    {reply,{binary,Res},Req,State};

info({server,Message}, Req, State) ->
    wf_context:clear_actions(),
%    wf:info("Server Message: ~p",[Message]),
    Module = State#context.module,
    try Module:event({server,Message}),[] catch E:R -> wf:info("Catch: ~p:~p~n~p", [E,R,n2o_error:stack()]) end,
    {reply,wf:json([{eval,iolist_to_binary(render_actions(get(actions)))},
                    {data,binary_to_list(term_to_binary(Message))}]),Req,State};

info({pickle,_,_,_}=Event, Req, State) ->
    wf_context:clear_actions(),
%    wf:info("N2O Message: ~p",[Event]),
    Result = try html_events(Event,State) catch E:R -> wf:info("Catch: ~p:~p~n~p", [E,R,n2o_error:stack()]), wf:json([]) end,
    {reply,Result,Req,State};

info({flush,Actions}, Req, State) ->
    wf_context:clear_actions(),
    wf:info("Flush Message: ~p",[Actions]),
    {reply, wf:json([{eval,iolist_to_binary(render_actions(Actions))}]), Req, State};

info(<<"PING">> = Ping, Req, State) ->
%    wf:info("Ping Message: ~p",[Ping]),
    {reply, wf:json([]), Req, State};

info(<<"N2O,",Rest/binary>> = InitMarker, Req, State) ->
    wf:info("N2O INIT: ~p",[Rest]),
    Module = State#context.module,
    InitActions = case Rest of
         <<>> -> Elements = try Module:main() catch X:Y -> wf:error_page(X,Y) end,
                 wf_core:render(Elements),
                 [];
          Binary -> Pid = wf:depickle(Binary),
                    X = Pid ! {'N2O',self()},
                    R = receive A -> render_actions(A) after 100 ->
                        QS = element(14, Req),
                        wf:redirect(case QS of <<>> -> ""; _ -> "?" ++ wf:to_list(QS) end),
                        []
                    end,
                    R end,<commit_msg>TODO fix<commit_after>-module(n2o_bullet).
-author('<NAME>').
-include_lib("n2o/include/wf.hrl").

-compile(export_all).
-export([init/4]).
-export([stream/3]).
-export([info/3]).
-export([terminate/2]).

% process spawn

init(_Transport, Req, _Opts, _Active) ->
    put(actions,[]),
    Ctx = wf_context:init_context(Req),
    NewCtx = wf_core:fold(init,Ctx#context.handlers,Ctx),
    wf_context:context(NewCtx),
    Res = ets:update_counter(globals,onlineusers,{2,1}),
    wf:reg(broadcast,wf:peer(Req)),
    wf:send(broadcast,{counter,Res}),
    Req1 = wf:header(<<"Access-Control-Allow-Origin">>, <<"*">>, NewCtx#context.req),
    {ok, Req1, NewCtx}.

% websocket handler

stream(<<"PING">> =Ping, Req, State) -> info(Ping,Req,State);
stream(<<"N2O",Rest/binary>> = Data, Req, State) -> info(Data,Req,State);
stream({text,Data}, Req, State) -> info(Data,Req,State);
stream({binary,Info}, Req, State) -> info(binary_to_term(Info,[safe]),Req,State);
stream(Data, Req, State) when is_binary(Data) -> info(binary_to_term(Data,[safe]),Req,State);
stream(Data, Req, State) -> info(Data,Req,State).

% WebSocketPid ! Message

info({client,Message}, Req, State) ->
    wf_context:clear_actions(),
%    wf:info("Client Message: ~p",[Message]),
    Module = State#context.module,
    try Module:event({client,Message}) catch E:R -> wf:info("Catch: ~p:~p~n~p", [E,R,n2o_error:stack()]) end,
    {reply,wf:json([{eval,iolist_to_binary(render_actions(get(actions)))},
                    {data,binary_to_list(term_to_binary(Message))}]),Req,State};

info({bert,Message}, Req, State) ->
    wf_context:clear_actions(),
    Module = State#context.module,
    Term = try Module:event({bert,Message}) catch E:R -> wf:info("Catch: ~p:~p~n~p", [E,R,n2o_error:stack()]), <<>> end,
    wf:info("Client BERT Binary Message: ~p Result: ~p",[Message,Term]),
    {reply,{binary,term_to_binary(Term)},Req,State};

info({binary,Message}, Req, State) ->
    wf_context:clear_actions(),
    Module = State#context.module,
    Term = try Module:event({binary,Message}) catch E:R -> wf:info("Catch: ~p:~p~n~p", [E,R,n2o_error:stack()]), <<>> end,
    wf:info("Client Raw Binary Message: ~p Result: ~p",[Message,Term]),
    Res = case Term of _ when is_binary(Term) -> Term; _ -> term_to_binary(Term) end,
    {reply,{binary,Res},Req,State};

info({server,Message}, Req, State) ->
    wf_context:clear_actions(),
%    wf:info("Server Message: ~p",[Message]),
    Module = State#context.module,
    try Module:event({server,Message}),[] catch E:R -> wf:info("Catch: ~p:~p~n~p", [E,R,n2o_error:stack()]) end,
    {reply,wf:json([{eval,iolist_to_binary(render_actions(get(actions)))},
                    {data,binary_to_list(term_to_binary(Message))}]),Req,State};

info({pickle,_,_,_}=Event, Req, State) ->
    wf_context:clear_actions(),
%    wf:info("N2O Message: ~p",[Event]),
    Result = try html_events(Event,State) catch E:R -> wf:info("Catch: ~p:~p~n~p", [E,R,n2o_error:stack()]), wf:json([]) end,
    {reply,Result,Req,State};

info({flush,Actions}, Req, State) ->
    wf_context:clear_actions(),
    wf:info("Flush Message: ~p",[Actions]),
    {reply, wf:json([{eval,iolist_to_binary(render_actions(Actions))}]), Req, State};

info(<<"PING">> = Ping, Req, State) ->
%    wf:info("Ping Message: ~p",[Ping]),
    {reply, wf:json([]), Req, State};

info(<<"N2O,",Rest/binary>> = InitMarker, Req, State) ->
    wf:info("N2O INIT: ~p",[Rest]),
    Module = State#context.module,
    InitActions = case Rest of
         <<>> -> Elements = try Module:main() catch X:Y -> wf:error_page(X,Y) end,
                 wf_core:render(Elements),
                 [];
          Binary -> Pid = wf:depickle(Binary), % FIXME TODO Legacy Nitrogen Compatible Code should be more pretty
                    X = Pid ! {'N2O',self()},
                    R = receive A -> render_actions(A) after 100 ->
                        QS = element(14, Req),
                        wf:redirect(case QS of <<>> -> ""; _ -> "?" ++ wf:to_list(QS) end),
                        []
                    end,
                    R end,
{'fraction_non_alphanumeric': 0.20842599456387448, 'fraction_numerical': 0.006177415369409439, 'mean_word_length': 4.018598884066956, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 103, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1866645', 'n_tokens_mistral': 3065, 'n_tokens_neox': 2970, 'n_words': 488}