text
stringlengths
12
786k
type event = t Web_event . t
type event_cb = t Web_event . cb
let style n = n ## style
let getStyle n key = getStyle n ## style key
let setStyle n key value = setStyle n ## style key value
let setStyleProperty n ( ? priority = false ) key value = let style = n ## style in match Js . Undefined . toOption style ## setProperty with | None -> setStyle n key value | Some _valid -> style ## setProperty__ key value ( if priority then ( Js . Null . return " important " ) else Js...
let childNodes n = n ## childNodes
let firstChild n = n ## firstChild
let appendChild n child = n ## appendChild child
let removeChild n child = n ## removeChild child
let insertBefore n child refNode = n ## insertBefore child refNode
let remove n child = n ## remove child
let setAttributeNS n namespace key value = n ## setAttributeNS namespace key value
let setAttribute n key value = n ## setAttribute key value
let setAttributeNsOptional n namespace key value = match namespace with | " " -> n ## setAttribute key value | ns -> n ## setAttributeNS ns key value
let removeAttributeNS n namespace key = n ## removeAttributeNS namespace key
let removeAttribute n key = n ## removeAttribute key
let removeAttributeNsOptional n namespace key = match namespace with | " " -> n ## removeAttribute key | ns -> n ## removeAttributeNS ns key
let addEventListener n typ listener options = n ## addEventListener typ listener options
let removeEventListener n typ listener options = n ## removeEventListener typ listener options
let focus n = n ## focus ( )
let set_nodeValue n text = n ## nodeValue #= text
let get_nodeValue n = n ## nodeValue
let remove_polyfill : unit -> unit = fun ( ) -> [ % bs . raw { | // remove polyfill ( function ( ) { if ( ( ' ! remove ' in Element . prototype ) ) { Element . prototype . remove = function ( ) { if ( this . parentNode ) { this . parentNode . removeChild ...
let singularize str = Option . value ~ default : str ( CCString . chop_suffix ~ suf " : s " str ) ; ;
module Form = struct type t = ( string * string option * string option ) list [ @@ deriving yojson , show ] let set ( ? key = " _form " ) ( errors : Conformist . error list ) ( urlencoded : ( string * string list ) list ) resp = let t = List . map ( fun ( k , v ) ...
module type SERVICE = sig type t val find : string -> t option Lwt . t val search : ? filter : string -> ? sort [ : ` Desc | ` Asc ] -> ? limit : int -> ? offset : int -> unit -> ( t list * int ) Lwt . t val insert : t -> ( t , string ) Result . t Lwt . t val update : str...
module Query = struct type sort = [ ` Desc | ` Asc ] type t = { filter : string option ; limit : int option ; offset : int option ; sort : sort option } let default_limit = 50 let sort_of_string ( str : string ) : sort option = match str with | " asc " -> Some ` Asc | " des...
module type VIEW = sig type t val skip_index_fetch : bool val index : Rock . Request . t -> string -> t list * int -> Query . t -> [ > Html_types . html ] Tyxml . Html . elt Lwt . t val new ' : Rock . Request . t -> string -> Form . t -> [ > Html_types . html ] Tyxml . Ht...
module type CONTROLLER = sig type t val index : string -> Rock . Request . t -> Rock . Response . t Lwt . t val new ' : ? key : string -> string -> Rock . Request . t -> Rock . Response . t Lwt . t val create : string -> ( ' a , ' b , t ) Conformist . t -> Rock . Reque...
module MakeController ( Service : SERVICE ) ( View : VIEW with type t = Service . t ) = struct exception Exception of string type t = Service . t let fetch_csrf name req = match Web_csrf . find req with | None -> Logs . err ( fun m -> m " CSRF middleware not installed for resource ' % s ...
type action = [ ` Index | ` Create | ` New | ` Edit | ` Show | ` Update | ` Destroy ]
let router_of_action ( type a ) ( module Controller : CONTROLLER with type t = a ) name schema ( action : action ) = match action with | ` Index -> Web . get ( Format . sprintf " /% s " name ) ( Controller . index name ) | ` Create -> Web . post ( Format . sprintf " /...
let routers_of_actions ( type a ) name schema ( module Controller : CONTROLLER with type t = a ) ( actions : action list ) = List . map ( router_of_action ( module Controller ) name schema ) actions ; ;
let resource_of_controller ( type a ) ? only name schema ( module Controller : CONTROLLER with type t = a ) = match only with | None -> routers_of_actions name schema ( module Controller ) [ ` Index ; ` Create ; ` New ; ` Edit ; ` Show ; ` Update ; ` Destroy ] | Some act...
let resource_of_service ( type a ) ? only name schema ~ view ( : module View : VIEW with type t = a ) ( module Service : SERVICE with type t = a ) = let module Controller = MakeController ( Service ) ( View ) in resource_of_controller ? only name schema ( module Controller ) ; ;
let no_cookie_set_without_session _ ( ) = let req = Opium . Request . get " " |> Opium . Request . add_cookie ( " _session " , " { } " ) in let handler _ = Opium . Response . of_plain_text " " |> Lwt . return in let % lwt response = handler req in Alcotest . ( c...
let unsigned_session_cookie _ ( ) = let req = Opium . Request . get " " |> Opium . Request . add_cookie ( " _session " , { { " | foo " " : bar " } } ) | in let handler req = let value = Sihl . Web . Session . find " foo " req in Alcotest . ( check ( op...
let invalid_session_cookie_signature _ ( ) = let req = Opium . Request . get " " |> Opium . Request . add_cookie ( " _session " , { { " | foo " " : bar " } . aE75kXj9sbZp6tP7oJLhrp9c /+ w } ) =| in let handler req = let value = Sihl . Web . Session . find " ...
let invalid_session_cookie_value _ ( ) = let req = Opium . Request . get " " |> Opium . Request . add_cookie ( " _session " , " foobar . jE75kXj9sbZp6tP7oJLhrp9c /+ w " ) = in let handler req = let value = Sihl . Web . Session . find " foo " req in Alcotest . ( che...
let cookie_set _ ( ) = let req = Opium . Request . get " " in let handler _ = let resp = Opium . Response . of_plain_text " " in Lwt . return @@ Sihl . Web . Session . set [ " foo " , " bar " ] resp in let % lwt response = handler req in let cookie = Opium . Respons...
let session_persisted_across_requests _ ( ) = let req = Opium . Request . get " " in let handler _ = let resp = Opium . Response . of_plain_text " " in Lwt . return @@ Sihl . Web . Session . set [ " foo " , " bar " ] resp in let % lwt response = handler req in let cook...
let fetch_all_kv_pairs _ ( ) = let open Sihl . Web in let session = [ " foo " , " bar " ; " baz " , " qux " ; " foo " , " quux " ] in let req = Request . get " " |> Sihl . Test . Session . set_value_req session in let handler req = let all = req |> Session...
let update_value _ ( ) = let open Sihl . Web in let target1 = " baz " , " qux " in let target2 = " waldo " , " grault " in let con = " able " in let session = [ " foo " , " bar " ; target1 ; " foo " , " quux " ] in let req = Request . get " " in let...
let delete_value _ ( ) = let open Sihl . Web in let target1 = " baz " , " qux " in let target2 = " waldo " , " grault " in let session = [ " foo " , " bar " ; target1 ; " foo " , " quux " ] in let req = Request . get " " in let handler _ = Response . ...
let set_value _ ( ) = let open Sihl . Web in let target1 = " baz " , " qux " in let target2 = " waldo " in let updated = " grault " in let session = [ " foo " , " bar " ; target1 ; " foo " , " quux " ] in let req = Request . get " " in let handler _ = R...
let suite = [ ( " session " , [ test_case " no cookie set without session " ` Quick no_cookie_set_without_session ; test_case " unsigned session cookie " ` Quick unsigned_session_cookie ; test_case " invalid session cookie signature " ` Quick invalid_session_cookie_signature ; test_...
let ( ) = Logs . set_level ( Sihl . Log . get_log_level ( ) ) ; Logs . set_reporter ( Sihl . Log . cli_reporter ( ) ) ; Lwt_main . run ( Alcotest_lwt . run " session " suite ) ; ;
let remove_trailing_slash _ ( ) = let middleware = Sihl . Web . Middleware . trailing_slash ( ) in let req = Opium . Request . get " / foo / bar " / in let handler req = Alcotest . ( check string " without trailing slash " " / foo / bar " req . Opium . Request . target ...
let remove_trailing_slash_on_root _ ( ) = let middleware = Sihl . Web . Middleware . trailing_slash ( ) in let req = Opium . Request . get " " / in let handler req = Alcotest . ( check string " does not remove trailing slash " " " / req . Opium . Request . target ) ; L...
let remove_trailing_slash_on_root_with_prefix _ ( ) = Unix . putenv " PREFIX_PATH " " path " ; let middleware = Sihl . Web . Middleware . trailing_slash ( ) in let req = Opium . Request . get " / path " //// in let handler req = Alcotest . ( check string " does not remove ...
let suite = [ ( " trailing slash " , [ test_case " remove trailing slash " ` Quick remove_trailing_slash ; test_case " remove trailing slash on root " ` Quick remove_trailing_slash_on_root ; test_case " remove trailing slash on root with prefix " ` Quick remove_trailing_slash_on_root_...
let ( ) = Logs . set_level ( Sihl . Log . get_log_level ( ) ) ; Logs . set_reporter ( Sihl . Log . cli_reporter ( ) ) ; Lwt_main . run ( Alcotest_lwt . run " trailing slash " suite ) ; ;
type t = < history : History . t Js . Undefined . t [ @ bs . get ] ; location : Web_location . t [ @ bs . get ] ; clearTimeout : timeoutHandlerID -> unit [ @ bs . meth ] ; requestAnimationFrame : ( float -> unit ) -> int [ @ bs . meth ] ; cancelAnimationFrame : ...
let history ( ) = window ## history
let localStorage ( ) = window ## localStorage
let location ( ) = window ## location
let requestAnimationFrame callback = window ## requestAnimationFrame callback
let cancelAnimationFrame id = window ## cancelAnimationFrame id
let clearTimeout id = window ## clearTimeout id
let setInterval cb msTime = window ## setInterval cb msTime
let setTimeout cb msTime = window ## setTimeout cb msTime
let addEventListener typ listener options = window ## addEventListener typ listener options
let removeEventListener typ listener options = window ## removeEventListener typ listener options
let requestAnimationFrame_polyfill : unit -> unit = fun ( ) -> [ % bs . raw { | // requestAnimationFrame polyfill ( function ( ) { var lastTime = 0 ; var vendors = [ ' ms ' , ' moz ' , ' webkit ' , ' o ' ] ; for ( var x = 0 ; x < vendors . length && !...
let decode_callback ( err : unit -> string ) ( cb : Value . t ) : Value . t -> unit = match Decode . _function cb with | None -> Main . raise_js ( err ( ) ) | Some cb -> fun v -> cb [ | v ] | |> ignore
let send_message ( err : unit -> string ) ( msg : Value . t ) ( dec : ' msg Decode . t ) ( cb : ' msg -> unit ) : unit = match dec msg with | None -> let open Main in log_string ( err ( ) ) ; log_value msg | Some msg -> cb msg
let do_async ( f : unit -> unit ) : unit = Timer . set f 0 |> ignore object method postMessage : ' msg -> unit Js . meth method terminate : unit Js . meth end
type t = worker Js . t
let start ( url : string ) ( dec : ' msg Decode . t ) ( cb : ' msg -> unit ) : t = let open Main in let w = new_global " Worker " [ | Value . string url ] | and cb event = send_message ( fun _ -> " cannot decode message from " ^ url ^ " " " ) \ ( Event . valu...
let post_message ( msg : Value . t ) ( w : t ) : unit = w ## postMessage msg
let terminate ( w : t ) : unit = w ## terminate
type ' msg worker_function = ( Value . t -> unit ) -> ' msg -> unit
let make ( decode : ' msg Decode . t ) ( f : ' msg worker_function ) : unit = let open Main in let post = match get_global " postMessage " with | None -> raise_js " webworker : < postMessage > function not available " | Some post -> post in let post = decode_callback ( fun _ -> " ...
module Simulate = struct type t = ( Value . t -> unit ) option ref let start ( dec : ' rcv Decode . t ) ( cb : ' rcv -> unit ) ( worker_decode : ' msg Decode . t ) ( worker : ' msg worker_function ) : t = let post_to_creator v = send_message ( fun _ -> " main : cannot...
let simulate_js ( name : string ) ( decode : ' msg Decode . t ) ( wfun : ' msg worker_function ) : unit = let open Main in ( fun post -> let post_to_creator = decode_callback ( fun _ -> " webworker : < postMessage > is not a funtion " ) post in let w = Simulate . start ( fun...
let kasa_worker = Worker . create " KaSaWorker . js " in let kasa_mailbox = Kasa_client . new_mailbox ( ) in let kamoha_worker = Worker . create " KaMoHaWorker . js " in let kamoha_mailbox = Kamoha_client . new_mailbox ( ) in let kastor_worker = Worker . create " KaStorWorker . js...
type event_loadend = Web_json . t method abort : unit -> unit method getAllResponseHeaders : unit -> string Js . null method getResponseHeader : string -> string Js . null method _open : string -> string -> bool -> string -> string -> unit method overrideMimeType : string -> unit method send : un...
type t = _xmlhttprequest Js . t
type errors = | IncompleteResponse | NetworkError
type body = | EmptyBody | EmptyStringBody | StringBody of string | FormDataBody of Web_formdata . t | FormListBody of ( string * string ) list | DocumentBody of Web_document . t
let abort x = x ## abort ( )
let getAllResponseHeaders x = let open ! Tea_result in match Js . Null . toOption ( x ## getAllResponseHeaders ( ) ) with | None -> Error IncompleteResponse | Some " " -> Error NetworkError | Some s -> Ok s
let getAllResponseHeadersAsList x = let open ! Tea_result in match getAllResponseHeaders x with | Error _ as err -> err | Ok s -> Ok ( s |> Js . String . split " \ r \ n " |> Array . map ( Js . String . splitAtMost " : " ~ limit : 2 ) |> Array . to_list |> List . filter ( ...
let getAllResponseHeadersAsDict x = let module StringMap = Map . Make ( String ) in match getAllResponseHeadersAsList x with | Tea_result . Error _ as err -> err | Tea_result . Ok l -> let insert d ( k , v ) = StringMap . add k v d in Tea_result . Ok ( List . fold_left insert StringMap ....
let getResponseHeader key x = Js . Null . toOption ( x ## getResponse key )
let open_ method ' url ( ? async = true ) ( ? user " " ) = ( ? password " " ) = x = x ## _open method ' url async user password
let overrideMimeType mimetype x = x ## overrideMimeType mimetype
let send body x = match body with | EmptyBody -> x ## send ( ) | EmptyStringBody -> x ## send__string Js . Null . empty | StringBody s -> x ## send__string ( Js . Null . return s ) | FormDataBody f -> x ## send__formdata f | FormListBody l -> let form = List . fold_left ( fun f ( k...
let setRequestHeader header value x = x ## setRequestHeader header value
type state = | Unsent | Opened | HeadersReceived | Loading | Done
type responseType = | StringResponseType | ArrayBufferResponseType | BlobResponseType | DocumentResponseType | JsonResponseType | TextResponseType | RawResponseType of string
type responseBody = | NoResponse | StringResponse of string | ArrayBufferResponse of unit | BlobResponse of unit | DocumentResponse of Web_document . t | JsonResponse of Web_json . t | TextResponse of string | RawResponse of string * unit
let set_onreadystatechange cb x = x ## onreadystatechange #= cb
let get_onreadystatechange x = x ## onreadystatechange
let readyState x = match x ## readystate with | 0 -> Unsent | 1 -> Opened | 2 -> HeadersReceived | 3 -> Loading | 4 -> Done | i -> failwith ( " Invalid return from ' readystate ' of : " ^ string_of_int i )
let set_responseType typ x = match typ with | StringResponseType -> x ## responseType #= " " | ArrayBufferResponseType -> x ## responseType #= " arraybuffer " | BlobResponseType -> x ## responseType #= " blob " | DocumentResponseType -> x ## responseType #= " document " | JsonResponseT...
let get_responseType x = match x ## responseType with | " " -> StringResponseType | " arraybuffer " -> ArrayBufferResponseType | " blob " -> BlobResponseType | " document " -> DocumentResponseType | " json " -> JsonResponseType | " text " -> TextResponseType | s -> RawResponseTyp...
let get_response x = match Js . Null . toOption x ## response with | None -> NoResponse | Some resp -> match get_responseType x with | StringResponseType -> StringResponse ( Obj . magic resp ) | ArrayBufferResponseType -> ArrayBufferResponse ( Obj . magic resp ) | BlobResponseType -> BlobRes...
let get_responseText x = x ## responseText