SSVM簡介
SSVM是適用于云,AI和區(qū)塊鏈應用程序的高性能糠悯,企業(yè)級WebAssembly(WASM)虛擬機帮坚。包括以下用例:
- Node.js應用程序中Rust函數(shù)的高性能和安全運行時
- 針對ONNX AI模型的硬件優(yōu)化的運行時
- 適用于領(lǐng)先的區(qū)塊鏈平臺的智能合約運行時引擎
一個簡單的Web App
功能:base64的編碼和解碼
首先在rust的lib文件中編寫實際的用來編碼和解碼的代碼:
use base64::{decode, encode};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn base64Encode(s: &str) -> String {
encode(s)
}
#[wasm_bindgen]
pub fn base64Decode(s: &str) -> String {
String::from_utf8(decode(s).unwrap()).unwrap()
}
然后在node中調(diào)用rust代碼:
const {
base64Encode,
base64Decode,
} = require('../pkg/ssvm_nodejs_starter_lib.js')
const http = require('http')
const url = require('url')
const hostname = '0.0.0.0'
const port = 3000
const server = http.createServer((req, res) => {
const queryObject = url.parse(req.url, true).query
if (queryObject['encodeStr']) {
res.end(base64Encode(queryObject['encodeStr']) + '\n')
} else if (queryObject['decodeStr']) {
console.log(queryObject['decodeStr'])
res.end(base64Decode(queryObject['decodeStr']) + '\n')
} else {
res.end(
`Please use command curl http://${hostname}:${port}/?encodeStr=string
or
http://${hostname}:${port}/?decodeStr=string \n`
)
}
})
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`)
})