1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::time::{Duration, Instant};

use anyhow::anyhow;
use rquickjs::Runtime;
use serde_json::json;

use crate::handler::function::script::Script;
use crate::handler::node::{NodeRequest, NodeResponse, NodeResult};
use crate::model::DecisionNodeKind;

mod js_value;
pub(crate) mod runtime;
mod script;

pub struct FunctionHandler {
    trace: bool,
    runtime: Runtime,
}

static MAX_DURATION: Duration = Duration::from_millis(500);

impl FunctionHandler {
    pub fn new(trace: bool, runtime: Runtime) -> Self {
        Self { trace, runtime }
    }

    pub async fn handle(&self, request: &NodeRequest<'_>) -> NodeResult {
        let content = match &request.node.kind {
            DecisionNodeKind::FunctionNode { content } => Ok(content),
            _ => Err(anyhow!("Unexpected node type")),
        }?;

        let start = Instant::now();
        let interrupt_handler = Box::new(move || start.elapsed() > MAX_DURATION);
        self.runtime.set_interrupt_handler(Some(interrupt_handler));

        let mut script = Script::new(self.runtime.clone());
        let result_response = script.call(content, &request.input).await;

        self.runtime.set_interrupt_handler(None);

        let response = result_response?;
        Ok(NodeResponse {
            output: response.output,
            trace_data: self.trace.then(|| json!({ "log": response.log })),
        })
    }
}