Tauri IPC 桥接的 Typesafe 语言绑定生成器
Note: This project is still under heavy development and may not support all features yet. Feel free to try it out though, create issues, and open PRs!
Generate type-safe bindings for Tauri's IPC bridge. Currently supports Rust Host and Webview frontends (called Guests) in JavaScript, TypeScript, and Rust. Bindings are declared using *.wit files that describe exposed functions and shared types.
Under the current IPC system, Tauri can make no compile-time guarantees on the correctness of your invoke calls and any mistakes will result in nasty runtime errors. For example you might have misspelled a command or parameter name which you will only notice when actually running the app!
tauri-bindgen will generate traits and invoke-handlers for the Host and Guest bindings for JavaScript, TypeScript, Rust and ReScript from a single, shared source of truth using the *.wit format as a Interface Definition Language (IDL). The generated bindings automatically take care of the heavy lifting, such as correctly calling the invoke function and serializing/deserializing parameters and results.
Here are a few reasons why that is cool:
When using strongly typed languages, such as Rust, TypeScript or ReScript the generated code will automatically ensure that you are calling the API correctly, as long as it passes the type checking your golden. This is especially neat when working in a team, so your colleagues can't just change command signatures and pull the rug out from under you.
Sometimes in bigger apps it's easy to loose track of all the commands your backend exposes, there might be an old version left somewhere that still get's exposed to the frontend. This is of course a big security risk. Having all possible commands be declared in a single place makes them easily auditable for you and possibly external security auditing professionals.
tauri-bindgen can also generate easy to read markdown and html descriptions of your interfaces for documentation purposes.
We're planning a big rework of the IPC bridge in the coming months and tauri-bindgen has been designed with some of the early design-work in mind. A big rework of course means breaking changes, but using tauri bindgen isolates from these changes, just update Tauri, update tauri-bindgen, and re-generate your bindings and your code will continue to work!
HostHost refers to the Tauri rust core, so the place where your commands are implemented.
GuestGuest refers to the code running in the Webview, this will usually be written in JavaScript/TypeScript, but can also be written in any other language that runs on the Web (through WASM) like Rust, C, or Swift.
invoke_handler from the interface. This generator can also be used through the tauri-bindgen-host crate (located at crates/host) and, has a generate! macro for generating code.wasm_bindgen that can be used in Rust compile-to-wasm frontend frameworks such as sycamore. You probably want to depend on the tauri-bindgen-guest-rust crate (located at crates/guest-rust) and use the generate! macro to generate code.Declare your interface in a *.wit file:
interface greet {
func greet(name: string) -> string
}then you can generate the host bindings:
…and lastly generate client bindings, this can be done for JavaScript, Typescript or ReScript using the following commands:
tauri-bindgen guest javascript greet.wit --prettier // we can run prettier on the generated code. requires a global prettier install
tauri-bindgen guest typescript greet.wit --prettier
tauri-bindgen guest rescript greet.wit --fmt // run `rescript format` on the generated code. requires a global rescript installor for rust using the provided generate! macro:
tauri_bindgen_guest_rust::generate!({
path: "greet.wit"
});
// now we can call greet
let greeting = greet::greet("Jonas").await;see also the example.
PRs are welcome!
This project has been inspired by and based off of the awesome wit-bindgen tool for WebAssembly by the Bytecode Alliance.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
暂无开放 Issues,或尚未同步最近议题。