-
Notifications
You must be signed in to change notification settings - Fork 41
Description
Zig is a simple and general-purpose programming language. Recently, it's gaining more traction thanks to its benefits. Zig supports Wasm + WASI compilation natively and they use it extensively.
Since it can be compiled to WebAssembly, so there's no requirement of a language runtime like Python, Ruby or JavaScript. The goal for this task is to create the wrapper code to interact with Wasm Workers Server. The same approach we took for the Rust and Go support.
Technical requirements
These are the steps to add Zig support:
-
Read the JSON metadata that comes via STDIN. Here you have the example from the Rust kit:
wasm-workers-server/kits/rust/src/io.rs
Lines 15 to 23 in 5631fb4
pub struct Input { url: String, method: String, headers: HashMap<String, String>, body: String, kv: HashMap<String, String>, #[serde(default)] params: HashMap<String, String>, } -
Define the
Request,ResponseandCacheentities. If it's available, it's better to use primitives from the Golang standard library or common usage libraries. -
Initialize a
Requestobject from the JSON metadata. -
Initialize the Cache store.
-
Call the worker function with the
Requestinstance. -
Detect the content in the response. If it's a valid UTF-8 string, we can return. If it uses a different encoding, you need to encode it in base64.
-
Transform the response and the Cache status into the expected JSON output. Note that the
base64flag is only enabled when the content is encoded.wasm-workers-server/kits/rust/src/io.rs
Lines 59 to 65 in 5631fb4
pub struct Output { data: String, headers: HashMap<String, String>, status: u16, kv: HashMap<String, String>, base64: bool, } -
Print the JSON output via STDOUT
Additional notes
- The library should be defined in the
kits/zigfolder. - The documentation is available in the site: https://workers.wasmlabs.dev/docs/languages/introduction.
- Currently, we're not publishing the libraries to language registries. Instead, we document how to get it from the GitHub repository.