Skip to content

Tauri

import { RPCChannel, TauriShellStdio } from "kkrpc/browser"
import { Child, Command } from "@tauri-apps/plugin-shell"
const localAPIImplementation = {
add: (a: number, b: number) => Promise.resolve(a + b)
}
async function spawnCmd(runtime: "deno" | "bun" | "node") {
let cmd: Command<string>
let process = Child | null = null
if (runtime === "deno") {
cmd = Command.create("deno", ["run", "-A", scriptPath])
process = await cmd.spawn()
} else if (runtime === "bun") {
cmd = Command.create("bun", [scriptPath])
process = await cmd.spawn()
} else if (runtime === "node") {
cmd = Command.create("node", [scriptPath])
process = await cmd.spawn()
} else {
throw new Error(`Invalid runtime: ${runtime}, pick either deno or bun`)
}
cmd.stdout.on("data", (data) => {
console.log("stdout", data)
})
cmd.stderr.on("data", (data) => {
console.warn("stderr", data)
})
cmd.on("close", (code) => {
console.log("close", code)
})
cmd.on("error", (err) => {
console.error("error", err)
})
const stdio = new TauriShellStdio(cmd.stdout, process)
const stdioRPC = new RPCChannel<typeof localAPIImplementation, RemoteAPI>(stdio, {
expose: localAPIImplementation
})
const api = stdioRPC.getAPI();
await api
.add(1, 2)
.then((result) => {
console.log("result", result)
})
.catch((err) => {
console.error(err)
})
process?.kill()
}

Sample Tauri App