112 points · by drocer88 · 5y ago · 61 comments
I have been working on something like this for a while too. But it uses GraalVM and it's "Polyglot" runtime to provide support for JS/Ruby/Python/WASM/LLVM languages.
It's packaged as a single binary using graal-native + Quarkus, or if you don't care about that (faster startup time/lower memory use/no JVM needed) but need maximum performance, you can run it as a regular JVM service as well.
It was born out of a need for a polyglot Functions-as-a-Service platform but without the weight of container orchestration -- I needed something I could deploy as a single binary.
Essentially OpenFaaS/OpenWhisk, minus containers.
It doesn't use CGI as a request/response specification though, it uses Vert.x "RoutingContext" interface which has methods like ".getBodyAsJson()", ".params('something')", ".header('foo')", etc.
https://vertx.io/docs/apidocs/io/vertx/ext/web/RoutingContext.html
An open standard like CGI probably would have been better, but Graal has marshalling facilities for sharing object types between languages, so the most user-friendly/ergonomic thing to do was to share the underlying web request object itself across language boundaries, including all the methods you can call on it.---
This has given me motivation to finally finish it so I can publish + open-source it!
And what of all these languages that compile to JS, like Typescript, Reason, Kotlin, et al? Is JS a sound "IR" between the frontend and WebAssembly? Or would it be better to compile things directly to WebAssembly?
Imagine being able to run a complex C dependency (with a high risk of buffer overflows) risk-free because it's running in a WASM sandbox?
Or building features that allow users to provide their own executable code (for plugins and extensions and other customization features) where they code is executed in a sandbox with strictly controlled CPU/memory/network and API access control.
And only if that would be the case the use of WASM would be an advantage. For general extensibility we had Java servlets, Lua modules, PHP modules, etc in webservers for decades.
Maybe it’s interesting for source code obfuscation purposes or in order to allow migration of server logic between different languages. But even that was possible before - and I don’t think the Market need was huge.
Java servlets, Lua and PHP modules cannot be run securely, and there are vulnerabilities for all of them.
That doesn't help if you need to run other people's code.
Yeah, I wonder how it would look like. /s
Like any bytecode (including jvm's), it comes at a performance penalty over bare asm. I personally don't anticipate it will replicate jvm's success as being a runtime when there is no reason for a runtime to exist - since it shouldn't be much harder to compile wasm (and languages that compile to wasm) to native asm ahead of time. On the other hand I wouldn't have predicted jvm's success either.
In places where you want a runtime though, like in shared computation models where it gives you security/performance advantages, it makes perfect sense. It looks like it's probably going to succeed there to me.
Compiling X -> JS -> Wasm doesn't make much sense. Not only is wasm a language with cleaner semantics to compile to, but JS doesn't actually really fit wasm semantics particularly well. In fact, while I could have missed something, I'm not aware of any good JS->wasm compilers. The closest I can think of is https://www.assemblyscript.org/ - and it's not 100% JS compatible.
A runtime like wasmer-python [0] is only 1.5MB.
There have been plenty of similar stacks, JVM is just one among many.
2 - it's the only things that also run on the web
3 - it's probably going to be ubiquitous because of that, meaning it will be a great platform to target by default
4 - it could potentially mean any platform run any code from any other one without having to deal with wrappers: you would write a lib in go or rust, and then run it as is in python
Sandboxing is of little help when external events can cause internal memory corruption that change module's expected outcomes.
It is as good as typical OS process.
WASM has some interesting properties, like sandboxing, a small set of instructions you can fit in your head and some fairly high level constructs (for a compilation target).
However I don't quite know what other benefits it provides. It certainly cannot compete with the JVM/CLR on many fronts. And I don't see quite a lot of use-cases yet that cannot be covered with just integrating V8. Happy to learn more about that (actual rationales).
Yes, see: https://bytecodealliance.org/articles/1-year-update
> Or would it be better to compile things directly to WebAssembly?
It'd be even better to roll out one's own Lambda@Edge: https://krustlet.dev/
Source: https://twitter.com/solomonstre/status/1111004913222324225?l...
[1]: https://github.com/WebAssembly/interface-types/blob/master/p...
"It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma."
So, if the implementation folds the multiple headers into one with the various values as a comma separated list, it should all be compliant. I don't know rust well enough to tell if this one is compliant...it reads like it is not (looks like last header wins): https://github.com/deislabs/wagi/blob/main/src/http_util.rs#...
Edit: It seems RFC7230 keeps most of that language, but makes one exception for Set-Cookie, but that's outbound in this context so it doesn't apply.
It's obviously not a great design, and a more serious standardized version would use interface types. But that still seems quite a ways off.
Wasm is a high-level assembly language that has both a textual and binary format. It can be interpreted or compiled to a native representation, and has tight integration with modern JS engines (though JS is not required to run Wasm).
WASI (WebAssembly System Interface) is a runtime environment standard for Wasm that allows Wasm applications to integrate with 'standard' Unix APIs. These can be forwarded to the host system or emulated.
For more info, honestly just going to the Wasm site, the standard, or one of the runtime implementations and exploring from there is the way to go.
It's best to learn by doing, so if you know a language that can compile to Wasm, it's always a fun project to try to write something using it. Rust, D, C, Zig, and AssemblyScript all have pretty good tooling.
Hope this helps!
This is why we have been experimenting with the outbound HTTP implementation [1], which is available in WAGI. In theory, you could take the same approach as here, with the difference that a WebSockets connection is persistent and long-running.
> Wagi is an implementation of CGI for WebAssembly.
that said, according to the docs this seems to be cgi 1.1 which equates to a process per request, which afaik isnt so scalable.