7 ms·
Running Zig with WASI on Cloudflare Workers
- ArrayBoundCheck 4y agoI'm uninformed. So cloudflare has workers which run wasm and the article is about zig->wasm? Why not use linux containers?
- oxplot 4y agoThis should give you a good idea: https://blog.cloudflare.com/cloud-computing-without-containers/ https://blog.cloudflare.com/cloud-computing-without-containe...
- syrusakbary 4y agoThis is great. We (Wasmer) have been working with the Zig team (special thanks to Topolarity here!) to get Zig on WASI and WAPM! We also want to have it automatically deployed to WAPM on each new version released. For anyone that wants to try Zig online, this might be of your interest! https://wapm.io/topolarity/zig https://wapm.io/topolarity/zig
- rad_gruchalski 4y agoOh, cool, so tinygo compiled stuff should also work.
- oxplot 4y agoI've only recently started to use TinyGo for my µController development and despite not being as featureful as its C SDK counterparts, I still prefer it by a long shot.
- stavros 4y agoDoes the existence of WASI mean that I can also run Rust programs on Workers, compiled to WASM (without the JS shim)? Does anyone know of any tutorials on this? It would be great if I could access the Request object, with all the headers, payload, etc from Rust.
- qw3rty01 4y agoI'm not sure if there's a JS shim or not, but rust has been a supported language for a long time with that workflow: https://developers.cloudflare.com/workers/tutorials/hello-world-rust/ https://developers.cloudflare.com/workers/tutorials/hello-wo...
- stavros 4y agoThere's a shim there, yeah (worker.js) :/
- brabel 4y agoYeah, that's kind of cheating... it runs Rust after compiling it to WASM and running that inside a JS runtime! Anyone thinking of using that should definitely consider just using WASI instead as Rust, as Zig, can compile to WASM/WASI (for those who don't know, WASI is like a POSIX for WASM, so WASM-compiled programs have access to a POSIX-like API, and therefore don't depend on the Web APIs like the Rust-compiled-to-worker solution does).
- k__ 4y ago"Anyone thinking of using that should definitely consider just using WASI" As far as I know, CFW heavily leans on V8 isolates for scaling. Why reinvent the wheel?
- aseipp 4y agoYes, when they initially announced this feature, they actually demo'd running an unmodified Rust program compiled to wasm32-wasi. It just takes the HTTP body as stdin and writes the response from stdout, though, so it's limited: https://blog.cloudflare.com/announcing-wasi-on-workers/ https://blog.cloudflare.com/announcing-wasi-on-workers/ I also did this recently but with the shim approach: a small Rust program that needs to run a cryptography operation on some input[1], compiled to wasi and operating on stdin/stdout, and then invoked by a larger Typescript program invoking it with the proper stream. Worked great. There is also a thing called workers-rs[2] which will let you write the entire worker in Rust, but it does not use WASI. WASI isn't really enough for anything beyond simple stdin/stdout pipelines without something "wrapping it" to provide the needed fds, env vars, arguments, etc. So instead workers-rs binds to the underlying Worker runtime primitives, the ones normal JavaScript uses, and exposes that -- but it does all of the mangling/bindgen/shim bullshit for you in the background. The shim is unavoidable, and always exists behind the curtain, but this approach lets you completely ignore it. workers-rs doesn't support every API, though (e.g. no R2 support.) In theory, assuming you ported the workers-wasi interface to Rust[3] yourself, you could then write a Worker in Rust using workers-rs that load WASI-conformant WASM programs (written in XYZ) and execute that program on the request object. Sounds confusing but I think you get what I mean. [1] No, this operation wasn't provided by the WebCrypto API, as far as I'm aware, otherwise I probably wouldn't have bothered with the added complexity. [2] https://github.com/cloudflare/workers-rs https://github.com/cloudflare/workers-rs [3] The underlying WebAssembly support comes from V8; WASI, then, is "just" an implementation of some very well known functions/APIs in the surrounding environment that the WASM program expects to exist, and behave in a certain way. So actually the entire workers-wasi framework is here, and if you ported this TypeScript interface to Rust, using workers-rs, you could invoke any WASI program similarly: https://github.com/cloudflare/workers-wasi/blob/main/src/index.ts https://github.com/cloudflare/workers-wasi/blob/main/src/ind...
- MrBuddyCasino 4y agoCool tech - basically CGI for WASM. I can't think of a use case though, usually everything I do server-side requires some form of persistence. Any ideas?
- methyl 4y agoCloudflare Workers have a few ways to persist data, check out KV and D1
- kentonv 4y agoDon't forget Durable Objects, which is a more fundamental building block. https://blog.cloudflare.com/introducing-workers-durable-objects/ https://blog.cloudflare.com/introducing-workers-durable-obje...
- solardev 4y agoYou can persist data with their edge stores (see the sibling comment), or use it to transpose between different API schemas, or as an API gateway that fetches consolidates, consolidates, and caches multiple micro-endpoints, or use it to run Doom (https://blog.cloudflare.com/doom-multiplayer-workers/ https://blog.cloudflare.com/doom-multiplayer-workers/) or IRC (https://github.com/cloudflare/workers-chat-demo https://github.com/cloudflare/workers-chat-demo) a variety of smaller useful things (https://developers.cloudflare.com/workers/examples/ https://developers.cloudflare.com/workers/examples/) I am not affiliated with Cloudflare, but Workers is by far my favorite cloud/serverless platform -- so much simpler and faster than Lambda, etc. (if your use case is simple enough)
- AtNightWeCode 4y agoYou can persist data in the key-value data store, as durable objects or in the R2 buckets (S3 like storage). So that is not a problem. There are other issues with CF workers though.
- pjmlp 4y agoYeah, I have a name for them, lets call them servlets.
- no_wizard 4y agoThis is awesome! Does Zig also have the memory safety guarantees of Rust? It seems like a much nicer language to learn and use IMO
- dralley 4y agoNo, it doesn't make many safety guarantees. The language and tooling is designed to make it easier to write safe code than languages like C, but the compiler doesn't enforce safety. The burden is smaller but it's still the programmer's job.
- azakai 4y agoIt does enforce spatial memory safety, at least, which is a large improvement over C (and to some extent C++). It does not enforce temporal memory safety, which is the harder one. Rust does have that (but it has unsafe blocks due to the limitations), and GC does have that (but it has overhead). Zig might add a temporal memory safety mode in some way eventually, but it will almost certainly add overhead. Example options for that: never release allocated blocks to the OS, or Type-After-Type (different allocation regions per type). Both add enough overhead that it won't be good enough for some use cases, but fine for others.
- eptcyka 4y agoGCs don't enforce temporal safety, no GC will ever prevent a race condition. The fact that the ownership concept in Rust prevents use-after-free bugs is just almost a side-effect of achieving fearless concurrency.
- losvedir 4y agoCould something like this - running in essentially a totally sandboxed environment, like webassembly - be Zig's sweet spot? I think of Zig as a very developer friendly, powerful, fast, fun, low level language, without the strict safety guardrails of rust. For some applications, rust's borrowchecker is not something you're going to want to be without, but is "memory safety" actually a concern when compiling to web assembly? In that case, is Zig all pros and no cons? You get its easy to use interfaces for allocation and working with raw bytes, but without the chance of memory vulnerabilities, like when running in a sandboxed browser environment or Cloudflare's V8 isolates?
- kibwen 4y agoSandboxed environments prevent many of the worst effects of memory unsafety, though at best you'll still crash your program, and at worst you'll still leak or corrupt the contents of your process's memory. They also impose performance overhead relative to unsandboxed code. So it's less "all pros and no cons" and more "fewer cons, but fewer pros". Which can still be an improvement in general, depending on context.
- mustache_kimono 4y ago> I think of Zig as a very developer friendly, powerful, fast, fun, low level language, without the strict safety guardrails of rust. Whenever I read something like this, I think: "Why is it some people will dream up niches for other languages to fill that are about people not liking Rust?" It's interesting if only because it seems lots of people really like writing Rust. Have these people tried Rust? Shouldn't Zig stand on its own -- "Zig is better because it solves X and Y" or even some matter ease or style "Zig makes it easier to X and Y which is important for WASI apps"?
- xtian 4y agoZig does stand on its own https://ziglang.org/learn/why_zig_rust_d_cpp/ https://ziglang.org/learn/why_zig_rust_d_cpp/
- 4y ago
- Integralist 4y agoNice! Good to see Zig adoption picking up. Fastly's Compute@Edge platform (https://developer.fastly.com/learning/compute/ https://developer.fastly.com/learning/compute/) has a section on "Custom SDKs" using languages outside of the supported SDKs (currently: Rust, Go, JavaScript) and in there you'll find Zig and Swift https://developer.fastly.com/learning/compute/custom/ https://developer.fastly.com/learning/compute/custom/
- ColanR 4y agoI'm guessing there'll be a big uptick in adoption once zig passes 1.0. At least for me, I'm just waiting until the syntax stabilizes, so I don't have to change my code later on or relearn how to do things.
- dial9-1 4y agothis is basically the single .jar file deploy reinvented
- baq 4y agojars: the good parts