cvm-rs: version 0.2.0

- switch to napi-rs. this mostly affects only the backend side of things, but IMO napi-rs is better (also, way less boilerplate is needed compared to neon).

- jpeg encoding no longer clones the input buffer internally (or wraps it in a Mutex as well), thanks to napi-rs not sucking in this regard. this is *probably* a micro-optimization, but will make it easier to later on do parallel encoding of all rectangles

- guac encoding is weird. This is kind of a painpoint of napi-rs but it's bearable
This commit is contained in:
modeco80
2024-08-20 06:14:08 -04:00
parent 55566fbd3a
commit 17191b0917
6 changed files with 257 additions and 158 deletions

View File

@@ -1,47 +1,20 @@
use crate::guac;
use neon::prelude::*;
fn guac_decode_impl<'a>(cx: &mut FunctionContext<'a>) -> JsResult<'a, JsArray> {
let input = cx.argument::<JsString>(0)?.value(cx);
use napi_derive::napi;
#[napi(js_name = "guacDecode")]
#[allow(unused)]
pub fn guac_decode(input: String) -> napi::anyhow::Result<Vec<String>> {
match guac::decode_instruction(&input) {
Ok(data) => {
let array = JsArray::new(cx, data.len());
Ok(elements) => Ok(elements),
let conv = data
.iter()
.map(|v| cx.string(v))
.collect::<Vec<Handle<JsString>>>();
for (i, str) in conv.iter().enumerate() {
array.set(cx, i as u32, *str)?;
}
return Ok(array);
}
Err(e) => {
return cx.throw_error(format!("{}", e));
}
Err(err) => Err(anyhow::anyhow!("Error decoding Guacamole frame: {}", err)),
}
}
fn guac_encode_impl<'a>(cx: &mut FunctionContext<'a>) -> JsResult<'a, JsString> {
let mut elements: Vec<String> = Vec::with_capacity(cx.len());
// Capture varadic arguments
for i in 0..cx.len() {
let input = cx.argument::<JsString>(i)?.value(cx);
elements.push(input);
}
Ok(cx.string(guac::encode_instruction(&elements)))
}
pub fn guac_decode(mut cx: FunctionContext) -> JsResult<JsArray> {
guac_decode_impl(&mut cx)
}
pub fn guac_encode(mut cx: FunctionContext) -> JsResult<JsString> {
guac_encode_impl(&mut cx)
// ... this is ugly, but works
#[napi(js_name = "guacEncodeImpl")]
#[allow(unused)]
pub fn guac_encode(items: Vec<String>) -> napi::anyhow::Result<String> {
Ok(guac::encode_instruction(&items))
}