Skip to content

Commit

Permalink
feat: panic handling for circtui input generation
Browse files Browse the repository at this point in the history
  • Loading branch information
DimiDumo committed Nov 8, 2024
1 parent 5a13f3a commit 6423dea
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 45 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ js-sys = "0.3.72"
serde-wasm-bindgen = "0.6.5"
rand = "0.8.5"
base64 = "0.22.1"
console_error_panic_hook = "0.1.7"

[dev-dependencies]
tokio = { version = "1.41", features = ["full"] }
tokio = { version = "1.41", features = ["full"] }
86 changes: 43 additions & 43 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,52 +126,52 @@ pub async fn generateCircuitInputsWithDecomposedRegexesAndExternalInputs(
external_inputs: JsValue,
params: JsValue,
) -> Promise {
// Deserialize decomposed_regexes
let decomposed_regexes: Vec<DecomposedRegex> = match from_value(decomposed_regexes) {
Ok(val) => val,
Err(_) => {
return Promise::reject(&JsValue::from_str("Invalid decomposed_regexes input"));
}
};
console_error_panic_hook::set_once();

// Deserialize external_inputs
let external_inputs: Vec<ExternalInput> = match from_value(external_inputs) {
Ok(val) => val,
Err(_) => {
return Promise::reject(&JsValue::from_str("Invalid external_inputs input"));
}
};
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| async move {
// Deserialize decomposed_regexes
let decomposed_regexes: Vec<DecomposedRegex> = from_value(decomposed_regexes)
.map_err(|_| String::from("Invalid decomposed_regexes input"))?;

// Deserialize params
let params: CircuitInputWithDecomposedRegexesAndExternalInputsParams = match from_value(params)
{
Ok(val) => val,
Err(_) => {
return Promise::reject(&JsValue::from_str("Invalid params input"));
}
};
// Deserialize external_inputs
let external_inputs: Vec<ExternalInput> = from_value(external_inputs)
.map_err(|_| String::from("Invalid external_inputs input"))?;

// Call the async function and await the result
let circuit_inputs = match generate_circuit_inputs_with_decomposed_regexes_and_external_inputs(
&email_addr,
decomposed_regexes,
external_inputs,
params,
)
.await
{
Ok(inputs) => inputs,
Err(err) => {
return Promise::reject(&JsValue::from_str(&format!(
"Failed to generate CircuitInputs: {}",
err
)));
}
};
// Deserialize params
let params: CircuitInputWithDecomposedRegexesAndExternalInputsParams =
from_value(params).map_err(|_| String::from("Invalid params input"))?;

// Call the async function and await the result
let circuit_inputs = generate_circuit_inputs_with_decomposed_regexes_and_external_inputs(
&email_addr,
decomposed_regexes,
external_inputs,
params,
)
.await
.map_err(|err| format!("Failed to generate CircuitInputs: {}", err))?;

// Serialize the output to JsValue
match to_value(&circuit_inputs) {
Ok(serialized_inputs) => Promise::resolve(&serialized_inputs),
Err(_) => Promise::reject(&JsValue::from_str("Failed to serialize CircuitInputs")),
// Serialize the output to JsValue
to_value(&circuit_inputs).map_err(|_| String::from("Failed to serialize CircuitInputs"))
}));

match result {
Ok(future) => match future.await {
Ok(serialized_inputs) => Promise::resolve(&serialized_inputs),
Err(err_msg) => Promise::reject(&JsValue::from_str(&err_msg)),
},
Err(panic) => {
let panic_msg = match panic.downcast::<String>() {
Ok(msg) => *msg,
Err(panic) => match panic.downcast::<&str>() {
Ok(msg) => msg.to_string(),
Err(_) => "Unknown panic occurred".to_string(),
},
};
Promise::reject(&JsValue::from_str(&format!(
"Panic occurred: {}",
panic_msg
)))
}
}
}
47 changes: 47 additions & 0 deletions ts_tests/circuit_intput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { expect, test, describe } from "bun:test";
// import init, { parseEmail } from "../pkg";
import { generateCircuitInputsWithDecomposedRegexesAndExternalInputs } from "../pkg/index.node";
import { readFile } from "fs/promises";

describe("generateCircuitInputsWithDecomposedRegexesAndExternalInputs test suite", async () => {
const helloEml = await readFile("tests/fixtures/test.eml", "utf-8");
console.log("got eml: ", helloEml);

test("Should parse valid email", async () => {
const decomposedRegexes = [
{
parts: [
{
is_public: true,
regex_def: "Hi",
},
{
is_public: true,
regex_def: "!",
},
],
name: "hi",
maxLength: 64,
location: "body",
},
];

const params = {
maxHeaderLength: 2816,
maxBodyLength: 1024,
ignoreBodyHashCheck: false,
removeSoftLinesBreaks: true,
// sha_precompute_selector
};

console.log("calling massive function");
const inputs = await generateCircuitInputsWithDecomposedRegexesAndExternalInputs(
helloEml,
decomposedRegexes,
[],
params
);
console.log("inputs: ", inputs);
// expect(parsedEmail).not.toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion wasm_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ echo "✅ Using wasm-pack version $WASM_PACK_VERSION"

# Build for Node.js
echo "📦 Building Node.js target..."
wasm-pack build --target bundler --out-dir pkg/node --scope @dimidumo
wasm-pack build --target nodejs --out-dir pkg/node --scope @dimidumo

# Build for web
echo "🌐 Building web target..."
Expand Down

0 comments on commit 6423dea

Please sign in to comment.