deislabs / wagi

Write HTTP handlers in WebAssembly with a minimal amount of work
Apache License 2.0
889 stars 45 forks source link

add 'argv' argument to modules.toml and bindle #166

Closed technosophos closed 2 years ago

technosophos commented 2 years ago

This PR does the following:

Why do we need this?

Scripting engines read files off of the filesystem and then interpret them. Examples:

However, they all make some assumptions about the shape of the argv array. Specifically, they assume that the array is:

0: script engine
1: top-level script
2...: the args that should be passed to the script

So, for example, a ruby invocation might be ruby env.rb --some args.

In the CGI days, we basically worked around this expectation with shell scripts. But we can't do that in Wagi. So the proposed solution is to allow the argv to be rewritten according to a user-specified pattern.

Example:

[[module]]
route = "/"
module = "ruby.wasm"
volumes = { "/" = "lib", "/usr" = "ruby-wasm32-wasi/usr" }
argv = "ruby /env.rb ${SCRIPT_NAME} ${ARGS}"

Take an example URL like this: http://localhost:3000/?p1=v1&p2=v2.

The above argv translates that to: ruby /env.rb / p1=v1 p2=v2. And ruby.wasm (which is ruby compiled to Wasm) skips arg[0], reads the /env.rb into the engine, and then passes that script the argv ["/", "p1=v1", "p2=v2"].

Signed-off-by: Matt Butcher matt.butcher@fermyon.com