Files
zen/bindings/nodejs/src/expression.rs
T
stefan-gorules c8692c401d fix: threadless wasm (#543)
* feat(nodejs): replace the threaded wasm build with threadless wasm32-wasip1

The browser binding no longer needs SharedArrayBuffer or cross-origin isolation. napi moves to 3.14 / cli 3.10.8 with emnapi 2.0.0-alpha.6; the wasm build registers napi-async-runtime's current-thread backend and spawns each evaluation as a runtime task. console.sleep goes through a sleep provider so the wasm build waits on the host timer instead of tokio. CI and the Makefile build and publish @gorules/zen-engine-wasm32-wasip1 in place of the threaded package.

* ci(nodejs): test the wasm32-wasip1 binding and load test binaries in place

cli 3.10 rejects napi artifacts when any configured target is missing, so test jobs download their binary next to index.js instead of building npm dirs. A wasm test job runs the suite against the threadless binding and gates the release.

* chore(nodejs): stop tracking generated wasip1 loaders

napi build writes them alongside the wasm and CI ships them through the wasm artifact, like index.d.ts.

* ci(nodejs): build the glibc legs with cargo zigbuild directly

cli 3.10 runs cargo metadata with the requested target, which rejects the zigbuild glibc suffix. Matches the go and uniffi workflows.

* build(nodejs): keep dev dependencies installable on node 20

@octokit/request 10.0.16 (via @napi-rs/cli) pulls content-type 3, which requires node 22; pin the cli's copy to 10.0.14.
2026-10-13 12:30:29 +02:00

122 lines
4.0 KiB
Rust

use napi::anyhow::anyhow;
use napi_derive::napi;
use serde_json::Value;
#[napi]
pub fn evaluate_expression_sync(expression: String, context: Option<Value>) -> napi::Result<Value> {
let ctx = zen_expression::Variable::try_from_value(context.unwrap_or(Value::Null))
.map_err(|e| anyhow!(e))?;
Ok(
zen_expression::evaluate_expression(expression.as_str(), ctx)
.map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?
.to_value(),
)
}
#[allow(dead_code)]
#[napi]
pub fn evaluate_unary_expression_sync(expression: String, context: Value) -> napi::Result<bool> {
let context = zen_expression::Variable::try_from_value(context).map_err(|e| anyhow!(e))?;
Ok(
zen_expression::evaluate_unary_expression(expression.as_str(), context)
.map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?,
)
}
#[allow(dead_code)]
#[napi]
pub fn render_template_sync(template: String, context: Value) -> napi::Result<Value> {
let context = zen_expression::Variable::try_from_value(context).map_err(|e| anyhow!(e))?;
Ok(zen_tmpl::render(template.as_str(), context)
.map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?
.to_value())
}
#[allow(dead_code)]
#[napi]
pub async fn evaluate_expression(
expression: String,
context: Option<Value>,
) -> napi::Result<Value> {
crate::mt::run_task(move || evaluate_expression_sync(expression, context))
.await
.map_err(|_| anyhow!("Hook timed out"))?
}
#[allow(dead_code)]
#[napi]
pub async fn evaluate_unary_expression(expression: String, context: Value) -> napi::Result<bool> {
crate::mt::run_task(move || evaluate_unary_expression_sync(expression, context))
.await
.map_err(|_| anyhow!("Hook timed out"))?
}
#[allow(dead_code)]
#[napi]
pub async fn render_template(template: String, context: Value) -> napi::Result<Value> {
crate::mt::run_task(move || render_template_sync(template, context))
.await
.map_err(|_| anyhow!("Hook timed out"))?
}
pub(crate) fn json_to_variable_type(value: &Value) -> zen_expression::variable::VariableType {
use std::rc::Rc;
use zen_expression::variable::VariableType as VT;
let Some(tag) = value.get("type").and_then(Value::as_str) else {
return VT::Any;
};
match tag {
"any" => VT::Any,
"null" => VT::Null,
"bool" => VT::Bool,
"string" => VT::String,
"number" => VT::Number,
"date" => VT::Date,
"interval" => VT::Interval,
"const" => value
.get("value")
.and_then(Value::as_str)
.map(|s| VT::Const(Rc::from(s)))
.unwrap_or(VT::Any),
"enum" => {
let name = value.get("name").and_then(Value::as_str).map(Rc::from);
let values = value
.get("values")
.and_then(Value::as_array)
.map(|arr| arr.iter().filter_map(Value::as_str).map(Rc::from).collect())
.unwrap_or_default();
VT::Enum(name, values)
}
"array" => {
let items = value
.get("items")
.map(json_to_variable_type)
.unwrap_or(VT::Any);
VT::Array(Rc::new(items))
}
"object" => {
let object = VT::empty_object();
if let (VT::Object(map), Some(fields)) =
(&object, value.get("fields").and_then(Value::as_object))
{
for (key, field) in fields {
map.borrow_mut()
.insert(Rc::from(key.as_str()), json_to_variable_type(field));
}
}
object
}
"nullable" => {
let inner = value
.get("inner")
.map(json_to_variable_type)
.unwrap_or(VT::Any);
VT::Nullable(Rc::new(inner))
}
_ => VT::Any,
}
}