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
use http_req::request;
use lazy_static::lazy_static;
use serde_json::Value;
use std::collections::HashMap;
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_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);
}
pub fn request_received<F>(cb: F)
where
F: Fn(HashMap<String, Value>, Vec<u8>),
{
unsafe {
match is_listening() {
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((qry, body)) = message_from_request() {
cb(qry, body);
}
}
}
}
}
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<(HashMap<String, Value>, Vec<u8>)> {
unsafe {
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_query, event_body))
}
}