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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//! Make a flow function working as a lambda service in [Flows.network](https://flows.network)
//!
//! # Quick Start
//!
//! To get started, let's write a very tiny flow function.
//!
//! ```rust
//! use lambda_flows::{request_received, send_response};
//!
//! #[no_mangle]
//! #[tokio::main(flavor = "current_thread")]
//! pub async fn run() {
//!     request_received(handler).await;
//! }
//!
//! async fn handler(_headers: Vec<(String, String)>, _qry: HashMap<String, Value>, _body: Vec<u8>) {
//!     send_response(
//!         200,
//!         vec![(String::from("content-type"), String::from("text/html"))],
//!         "ok".as_bytes().to_vec(),
//!     );
//! }
//! ```
//!
//! When a new request is received the callback closure of function [request_received()] will be called and [send_response()] is used to make the response.

use http_req::request;
use lazy_static::lazy_static;
use serde_json::Value;
use std::collections::HashMap;
use std::future::Future;

lazy_static! {
    static ref LAMBDA_API_PREFIX: String = String::from(
        std::option_env!("LAMBDA_API_PREFIX")
            .unwrap_or("https://lambda-flows-extension.vercel.app/api")
    );
}
const LAMBDA_ENTRY_URL: &str = "https://code.flows.network/lambda";

extern "C" {
    fn is_listening() -> i32;
    fn get_flows_user(p: *mut u8) -> i32;
    fn get_flow_id(p: *mut u8) -> i32;
    fn get_event_headers_length() -> i32;
    fn get_event_headers(p: *mut u8) -> i32;
    fn get_event_query_length() -> i32;
    fn get_event_query(p: *mut u8) -> i32;
    fn get_event_body_length() -> i32;
    fn get_event_body(p: *mut u8) -> i32;
    fn set_error_log(p: *const u8, len: i32);
    fn set_output(p: *const u8, len: i32);
    fn set_response(p: *const u8, len: i32);
    fn set_response_headers(p: *const u8, len: i32);
    fn set_response_status(status: i32);
    // fn redirect_to(p: *const u8, len: i32);
}

/// Register a callback closure with the query and body of the request for the lambda service.
///
/// The query is formed as a [HashMap]. For example, say the entrypoint of the lambda service is
/// `https://code.flows.network/lambda/6rtSi9SEsC?param=hello`
/// then the query will look like `HashMap("param", Value::String("hello"))`
///
/// The body is the raw bytes of the request body.
pub async fn request_received<F, Fut>(cb: F)
where
    F: FnOnce(Vec<(String, String)>, HashMap<String, Value>, Vec<u8>) -> Fut,
    Fut: Future<Output = ()>,
{
    unsafe {
        match is_listening() {
            // Calling register
            1 => {
                let mut flows_user = Vec::<u8>::with_capacity(100);
                let c = get_flows_user(flows_user.as_mut_ptr());
                flows_user.set_len(c as usize);
                let flows_user = String::from_utf8(flows_user).unwrap();

                let mut flow_id = Vec::<u8>::with_capacity(100);
                let c = get_flow_id(flow_id.as_mut_ptr());
                if c == 0 {
                    panic!("Failed to get flow id");
                }
                flow_id.set_len(c as usize);
                let flow_id = String::from_utf8(flow_id).unwrap();

                let mut writer = Vec::new();
                let res = request::get(
                    format!(
                        "{}/{}/{}/listen",
                        LAMBDA_API_PREFIX.as_str(),
                        flows_user,
                        flow_id
                    ),
                    &mut writer,
                )
                .unwrap();

                match res.status_code().is_success() {
                    true => {
                        let listener: Value = serde_json::from_slice(&writer).unwrap();
                        let l_key = listener["l_key"].as_str().unwrap();
                        let output = format!("Lambda endpoint: {}/{}", LAMBDA_ENTRY_URL, l_key);
                        set_output(output.as_ptr(), output.len() as i32);
                    }
                    false => {
                        set_error_log(writer.as_ptr(), writer.len() as i32);
                    }
                }
            }
            _ => {
                if let Some((header, qry, body)) = message_from_request() {
                    cb(header, qry, body).await;
                }
            }
        }
    }
}

/// Set the response for the lambda service.
pub fn send_response(status: u16, headers: Vec<(String, String)>, body: Vec<u8>) {
    let headers = serde_json::to_string(&headers).unwrap_or_default();
    unsafe {
        set_response_status(status as i32);
        set_response_headers(headers.as_ptr(), headers.len() as i32);
        set_response(body.as_ptr(), body.len() as i32);
    }
}

fn message_from_request() -> Option<(Vec<(String, String)>, HashMap<String, Value>, Vec<u8>)> {
    unsafe {
        let l = get_event_headers_length();
        let mut event_headers = Vec::<u8>::with_capacity(l as usize);
        let c = get_event_headers(event_headers.as_mut_ptr());
        assert!(c == l);
        event_headers.set_len(c as usize);
        let event_headers = serde_json::from_slice(&event_headers).unwrap();

        let l = get_event_query_length();
        let mut event_query = Vec::<u8>::with_capacity(l as usize);
        let c = get_event_query(event_query.as_mut_ptr());
        assert!(c == l);
        event_query.set_len(c as usize);
        let event_query = serde_json::from_slice(&event_query).unwrap();

        let l = get_event_body_length();
        let mut event_body = Vec::<u8>::with_capacity(l as usize);
        let c = get_event_body(event_body.as_mut_ptr());
        assert!(c == l);
        event_body.set_len(c as usize);

        Some((event_headers, event_query, event_body))
    }
}