Skip to content

Commit

Permalink
chore: add --var-max flag to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Mar 12, 2024
1 parent a2c3f82 commit fc7ea67
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
21 changes: 17 additions & 4 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ let print_timing ~time name f =
out

let main file func_name input loop timeout_ms allowed_paths allowed_hosts config
memory_max http_max log_level log_file wasi stdin time =
memory_max http_max var_max log_level log_file wasi stdin time _manifest =
let input = if stdin then read_stdin () else input in
let allowed_paths = split_allowed_paths allowed_paths in
let config = split_config config in
let memory =
Manifest.{ max_pages = memory_max; max_http_response_bytes = http_max }
Manifest.
{
max_pages = memory_max;
max_http_response_bytes = http_max;
max_var_bytes = var_max;
}
in
let manifest =
print_timing ~time "loaded manifest" @@ fun () ->
Expand Down Expand Up @@ -101,6 +106,10 @@ let http_max =
& opt (some int) None
& info [ "http-response-max" ] ~docv:"BYTES" ~doc)

let var_max =
let doc = "Max number of bytes allowed in the Extism var store" in
Arg.(value & opt (some int) None & info [ "var-max" ] ~docv:"BYTES" ~doc)

let timeout =
let doc = "Plugin timeout in milliseconds." in
Arg.(value & opt int 30000 & info [ "timeout"; "t" ] ~docv:"MILLIS" ~doc)
Expand Down Expand Up @@ -145,6 +154,10 @@ let wasi =
let doc = "Enable WASI." in
Arg.(value & flag & info [ "wasi" ] ~doc)

let manifest =
let doc = "Unused, exists for compatibility with Go CLI" in
Arg.(value & flag & info [ "manifest" ] ~doc)

let stdin =
let doc = "Read function input from stdin." in
Arg.(value & flag & info [ "stdin" ] ~doc)
Expand All @@ -156,8 +169,8 @@ let time =
let main_t =
Term.(
const main $ file $ func_name $ input $ loop $ timeout $ allowed_paths
$ allowed_hosts $ config $ memory_max $ http_max $ log_level $ log_file
$ wasi $ stdin $ time)
$ allowed_hosts $ config $ memory_max $ http_max $ var_max $ log_level
$ log_file $ wasi $ stdin $ time $ manifest)

let cmd = Cmd.v (Cmd.info "extism-call") main_t
let () = exit (Cmd.eval cmd)
41 changes: 38 additions & 3 deletions manifest/extism_manifest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let base64_of_yojson j = Yojson.Safe.Util.to_string j
type memory_options = {
max_pages : int option; [@yojson.option]
max_http_response_bytes : int option; [@yojson.option]
max_var_bytes : int option; [@yojson.option]
}
[@@deriving yojson]

Expand Down Expand Up @@ -107,7 +108,13 @@ let with_memory_max max t =
| None ->
{
t with
memory = Some { max_http_response_bytes = None; max_pages = Some max };
memory =
Some
{
max_http_response_bytes = None;
max_pages = Some max;
max_var_bytes = None;
};
}
| Some m -> { t with memory = Some { m with max_pages = Some max } }

Expand All @@ -116,14 +123,42 @@ let with_http_response_max_bytes max t =
| None ->
{
t with
memory = Some { max_http_response_bytes = Some max; max_pages = None };
memory =
Some
{
max_http_response_bytes = Some max;
max_pages = None;
max_var_bytes = None;
};
}
| Some m ->
{ t with memory = Some { m with max_http_response_bytes = Some max } }

let with_var_max_bytes max t =
match t.memory with
| None ->
{
t with
memory =
Some
{
max_var_bytes = Some max;
max_pages = None;
max_http_response_bytes = None;
};
}
| Some m ->
{ t with memory = Some { m with max_http_response_bytes = Some max } }

let%test "rountrip" =
let config = [ ("a", Some "b"); ("b", Some "c") ] in
let memory = { max_pages = Some 5; max_http_response_bytes = Some 9999 } in
let memory =
{
max_pages = Some 5;
max_http_response_bytes = Some 9999;
max_var_bytes = Some 12300;
}
in
let t =
create ~config ~memory ~allowed_hosts:[ "example.com" ]
~allowed_paths:[ ("a", "b") ]
Expand Down
5 changes: 5 additions & 0 deletions manifest/extism_manifest.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type memory_options = {
max_http_response_bytes: int option;
(** [max_http_response_bytes] can be used to limit the size of the response returned by
[extism_http_request] *)
max_var_bytes: int option;
(** [max_var_bytes] can be used to limit the size of the Extism var store *)
}
[@@deriving yojson]
(** Memory options *)
Expand Down Expand Up @@ -135,3 +137,6 @@ val with_memory_max : int -> t -> t

val with_http_response_max_bytes : int -> t -> t
(** Returns a new {!t} with [memory.max_http_response_bytes] updated *)

val with_var_max_bytes : int -> t -> t
(** Returns a new {!t} with [memory.max_var_bytes] updated *)

0 comments on commit fc7ea67

Please sign in to comment.