@ezetgalaxy/titan 26.12.8 → 26.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -188
- package/package.json +1 -1
- package/templates/common/Dockerfile +1 -1
- package/templates/common/app/titan.d.ts +166 -49
- package/templates/js/app/app.js +1 -1
- package/templates/js/server/src/action_management.rs +1 -0
- package/templates/js/server/src/extensions/builtin.rs +423 -183
- package/templates/js/server/src/extensions/mod.rs +118 -14
- package/templates/js/server/src/extensions/titan_core.js +157 -1
- package/templates/js/server/src/main.rs +123 -54
- package/templates/js/server/src/runtime.rs +232 -76
- package/templates/js/titan/bundle.js +1 -1
- package/templates/js/titan/titan.js +1 -1
- package/templates/rust-js/app/app.js +1 -1
- package/templates/rust-ts/app/app.ts +1 -1
- package/templates/ts/app/app.ts +1 -1
- package/templates/ts/server/src/action_management.rs +1 -0
- package/templates/ts/server/src/extensions/builtin.rs +423 -183
- package/templates/ts/server/src/extensions/mod.rs +118 -14
- package/templates/ts/server/src/extensions/titan_core.js +157 -1
- package/templates/ts/server/src/main.rs +123 -54
- package/templates/ts/server/src/runtime.rs +232 -76
- package/templates/ts/titan/titan.d.ts +167 -47
- package/titanpl-sdk/package.json +1 -1
- package/titanpl-sdk/templates/app/app.js +1 -1
- package/titanpl-sdk/templates/titan/bundle.js +1 -1
- package/titanpl-sdk/templates/titan/titan.js +1 -1
|
@@ -9,16 +9,28 @@ use serde_json::Value;
|
|
|
9
9
|
use jsonwebtoken::{encode, decode, Header, EncodingKey, DecodingKey, Validation};
|
|
10
10
|
use bcrypt::{hash, verify, DEFAULT_COST};
|
|
11
11
|
use postgres::{Client as PgClient, NoTls};
|
|
12
|
-
use std::sync::Mutex;
|
|
13
|
-
use std::collections::HashMap;
|
|
12
|
+
use std::sync::{Mutex, OnceLock};
|
|
13
|
+
use std::collections::{HashMap, BTreeMap};
|
|
14
14
|
|
|
15
|
-
use crate::utils::{blue, gray, parse_expires_in};
|
|
15
|
+
use crate::utils::{blue, gray, red, parse_expires_in};
|
|
16
16
|
use super::{TitanRuntime, v8_str, v8_to_string, throw, ShareContextStore};
|
|
17
17
|
|
|
18
18
|
const TITAN_CORE_JS: &str = include_str!("titan_core.js");
|
|
19
19
|
|
|
20
20
|
// Database connection pool
|
|
21
21
|
static DB_POOL: Mutex<Option<HashMap<String, PgClient>>> = Mutex::new(None);
|
|
22
|
+
static HTTP_CLIENT: OnceLock<reqwest::Client> = OnceLock::new();
|
|
23
|
+
|
|
24
|
+
fn get_http_client() -> &'static reqwest::Client {
|
|
25
|
+
HTTP_CLIENT.get_or_init(|| {
|
|
26
|
+
reqwest::Client::builder()
|
|
27
|
+
.use_rustls_tls()
|
|
28
|
+
.tcp_nodelay(true)
|
|
29
|
+
.user_agent("TitanPL/1.0")
|
|
30
|
+
.build()
|
|
31
|
+
.unwrap_or_else(|_| reqwest::Client::new())
|
|
32
|
+
})
|
|
33
|
+
}
|
|
22
34
|
|
|
23
35
|
|
|
24
36
|
pub fn inject_builtin_extensions(scope: &mut v8::HandleScope, global: v8::Local<v8::Object>, t_obj: v8::Local<v8::Object>) {
|
|
@@ -45,23 +57,39 @@ pub fn inject_builtin_extensions(scope: &mut v8::HandleScope, global: v8::Local<
|
|
|
45
57
|
let log_key = v8_str(scope, "log");
|
|
46
58
|
t_obj.set(scope, log_key.into(), log_fn.into());
|
|
47
59
|
|
|
48
|
-
// t.fetch
|
|
49
|
-
let fetch_fn = v8::Function::new(scope,
|
|
60
|
+
// t.fetch (Metadata version for drift)
|
|
61
|
+
let fetch_fn = v8::Function::new(scope, native_fetch_meta).unwrap();
|
|
50
62
|
let fetch_key = v8_str(scope, "fetch");
|
|
51
63
|
t_obj.set(scope, fetch_key.into(), fetch_fn.into());
|
|
52
64
|
|
|
65
|
+
// t._drift_call
|
|
66
|
+
let drift_fn = v8::Function::new(scope, native_drift_call).unwrap();
|
|
67
|
+
let drift_key = v8_str(scope, "_drift_call");
|
|
68
|
+
t_obj.set(scope, drift_key.into(), drift_fn.into());
|
|
69
|
+
|
|
70
|
+
// t._finish_request
|
|
71
|
+
let finish_fn = v8::Function::new(scope, native_finish_request).unwrap();
|
|
72
|
+
let finish_key = v8_str(scope, "_finish_request");
|
|
73
|
+
t_obj.set(scope, finish_key.into(), finish_fn.into());
|
|
74
|
+
|
|
53
75
|
// t.loadEnv
|
|
54
76
|
let env_fn = v8::Function::new(scope, native_load_env).unwrap();
|
|
55
77
|
let env_key = v8_str(scope, "loadEnv");
|
|
56
78
|
t_obj.set(scope, env_key.into(), env_fn.into());
|
|
57
79
|
|
|
58
|
-
// auth, jwt, password ... (
|
|
80
|
+
// auth, jwt, password, db, core ... (setup native objects BEFORE JS injection)
|
|
59
81
|
setup_native_utils(scope, t_obj);
|
|
60
82
|
|
|
61
83
|
// 2. JS Side Injection (Embedded)
|
|
62
|
-
let
|
|
63
|
-
|
|
64
|
-
|
|
84
|
+
let tc = &mut v8::TryCatch::new(scope);
|
|
85
|
+
let source = v8_str(tc, TITAN_CORE_JS);
|
|
86
|
+
if let Some(script) = v8::Script::compile(tc, source, None) {
|
|
87
|
+
if script.run(tc).is_none() {
|
|
88
|
+
let msg = tc.message().map(|m| m.get(tc).to_rust_string_lossy(tc)).unwrap_or("Unknown".to_string());
|
|
89
|
+
println!("{} {} {}", blue("[Titan]"), red("Core JS Init Failed:"), msg);
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
println!("{} {}", blue("[Titan]"), red("Core JS Compilation Failed"));
|
|
65
93
|
}
|
|
66
94
|
}
|
|
67
95
|
|
|
@@ -116,7 +144,6 @@ fn setup_native_utils(scope: &mut v8::HandleScope, t_obj: v8::Local<v8::Object>)
|
|
|
116
144
|
t_obj.set(scope, sc_key.into(), sc_val);
|
|
117
145
|
|
|
118
146
|
// t.db (Database operations)
|
|
119
|
-
// println!("[DEBUG] Setting up t.db...");
|
|
120
147
|
let db_obj = v8::Object::new(scope);
|
|
121
148
|
let db_connect_fn = v8::Function::new(scope, native_db_connect).unwrap();
|
|
122
149
|
let connect_key = v8_str(scope, "connect");
|
|
@@ -124,7 +151,19 @@ fn setup_native_utils(scope: &mut v8::HandleScope, t_obj: v8::Local<v8::Object>)
|
|
|
124
151
|
|
|
125
152
|
let db_key = v8_str(scope, "db");
|
|
126
153
|
t_obj.set(scope, db_key.into(), db_obj.into());
|
|
127
|
-
|
|
154
|
+
|
|
155
|
+
// t.core (System operations)
|
|
156
|
+
let core_obj = v8::Object::new(scope);
|
|
157
|
+
let fs_obj = v8::Object::new(scope);
|
|
158
|
+
let fs_read_fn = v8::Function::new(scope, native_read).unwrap();
|
|
159
|
+
let read_key = v8_str(scope, "read");
|
|
160
|
+
fs_obj.set(scope, read_key.into(), fs_read_fn.into());
|
|
161
|
+
|
|
162
|
+
let fs_key = v8_str(scope, "fs");
|
|
163
|
+
core_obj.set(scope, fs_key.into(), fs_obj.into());
|
|
164
|
+
|
|
165
|
+
let core_key = v8_str(scope, "core");
|
|
166
|
+
t_obj.set(scope, core_key.into(), core_obj.into());
|
|
128
167
|
}
|
|
129
168
|
|
|
130
169
|
fn native_read(scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, mut retval: v8::ReturnValue) {
|
|
@@ -135,48 +174,25 @@ fn native_read(scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments,
|
|
|
135
174
|
}
|
|
136
175
|
let path_str = v8_to_string(scope, path_val);
|
|
137
176
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
let context = scope.get_current_context();
|
|
144
|
-
let global = context.global(scope);
|
|
145
|
-
let root_key = v8_str(scope, "__titan_root");
|
|
146
|
-
let root_val = global.get(scope, root_key.into()).unwrap();
|
|
177
|
+
// Return Metadata (Non-blocking for drift)
|
|
178
|
+
let obj = v8::Object::new(scope);
|
|
179
|
+
let op_key = v8_str(scope, "__titanAsync");
|
|
180
|
+
let op_val = v8::Boolean::new(scope, true);
|
|
181
|
+
obj.set(scope, op_key.into(), op_val.into());
|
|
147
182
|
|
|
148
|
-
let
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
let
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
Err(_) => {
|
|
162
|
-
throw(scope, &format!("t.read: file not found: {}", path_str));
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
if !target.starts_with(&root_path) {
|
|
168
|
-
throw(scope, "t.read: path escapes allowed root");
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
match std::fs::read_to_string(&target) {
|
|
173
|
-
Ok(content) => {
|
|
174
|
-
retval.set(v8_str(scope, &content).into());
|
|
175
|
-
},
|
|
176
|
-
Err(e) => {
|
|
177
|
-
throw(scope, &format!("t.read failed: {}", e));
|
|
178
|
-
}
|
|
179
|
-
}
|
|
183
|
+
let type_key = v8_str(scope, "type");
|
|
184
|
+
let type_val = v8_str(scope, "fs_read");
|
|
185
|
+
obj.set(scope, type_key.into(), type_val.into());
|
|
186
|
+
|
|
187
|
+
let data_obj = v8::Object::new(scope);
|
|
188
|
+
let path_k = v8_str(scope, "path");
|
|
189
|
+
let path_v = v8_str(scope, &path_str);
|
|
190
|
+
data_obj.set(scope, path_k.into(), path_v.into());
|
|
191
|
+
|
|
192
|
+
let data_key = v8_str(scope, "data");
|
|
193
|
+
obj.set(scope, data_key.into(), data_obj.into());
|
|
194
|
+
|
|
195
|
+
retval.set(obj.into());
|
|
180
196
|
}
|
|
181
197
|
|
|
182
198
|
fn native_decode_utf8(scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, mut retval: v8::ReturnValue) {
|
|
@@ -293,94 +309,7 @@ fn native_log(scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments,
|
|
|
293
309
|
);
|
|
294
310
|
}
|
|
295
311
|
|
|
296
|
-
fn native_fetch(scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, mut retval: v8::ReturnValue) {
|
|
297
|
-
let url = v8_to_string(scope, args.get(0));
|
|
298
|
-
let mut method = "GET".to_string();
|
|
299
|
-
let mut body_str = None;
|
|
300
|
-
let mut headers_vec = Vec::new();
|
|
301
|
-
|
|
302
|
-
let opts_val = args.get(1);
|
|
303
|
-
if opts_val.is_object() {
|
|
304
|
-
let opts_obj = opts_val.to_object(scope).unwrap();
|
|
305
|
-
|
|
306
|
-
let m_key = v8_str(scope, "method");
|
|
307
|
-
if let Some(m_val) = opts_obj.get(scope, m_key.into()) {
|
|
308
|
-
if m_val.is_string() {
|
|
309
|
-
method = v8_to_string(scope, m_val);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
let b_key = v8_str(scope, "body");
|
|
314
|
-
if let Some(b_val) = opts_obj.get(scope, b_key.into()) {
|
|
315
|
-
if b_val.is_string() {
|
|
316
|
-
body_str = Some(v8_to_string(scope, b_val));
|
|
317
|
-
} else if b_val.is_object() {
|
|
318
|
-
let json_obj = v8::json::stringify(scope, b_val).unwrap();
|
|
319
|
-
body_str = Some(json_obj.to_rust_string_lossy(scope));
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
let h_key = v8_str(scope, "headers");
|
|
324
|
-
if let Some(h_val) = opts_obj.get(scope, h_key.into()) {
|
|
325
|
-
if h_val.is_object() {
|
|
326
|
-
let h_obj = h_val.to_object(scope).unwrap();
|
|
327
|
-
if let Some(keys) = h_obj.get_own_property_names(scope, Default::default()) {
|
|
328
|
-
for i in 0..keys.length() {
|
|
329
|
-
let key = keys.get_index(scope, i).unwrap();
|
|
330
|
-
let val = h_obj.get(scope, key).unwrap();
|
|
331
|
-
headers_vec.push((v8_to_string(scope, key), v8_to_string(scope, val)));
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
312
|
|
|
338
|
-
let client = Client::builder().use_rustls_tls().tcp_nodelay(true).build().unwrap_or(Client::new());
|
|
339
|
-
let mut req = client.request(method.parse().unwrap_or(reqwest::Method::GET), &url);
|
|
340
|
-
|
|
341
|
-
for (k, v) in headers_vec {
|
|
342
|
-
if let (Ok(name), Ok(val)) = (HeaderName::from_bytes(k.as_bytes()), HeaderValue::from_str(&v)) {
|
|
343
|
-
let mut map = HeaderMap::new();
|
|
344
|
-
map.insert(name, val);
|
|
345
|
-
req = req.headers(map);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
if let Some(b) = body_str {
|
|
350
|
-
req = req.body(b);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
let res = req.send();
|
|
354
|
-
let obj = v8::Object::new(scope);
|
|
355
|
-
match res {
|
|
356
|
-
Ok(r) => {
|
|
357
|
-
let status = r.status().as_u16();
|
|
358
|
-
let text = r.text().unwrap_or_default();
|
|
359
|
-
|
|
360
|
-
let status_key = v8_str(scope, "status");
|
|
361
|
-
let status_val = v8::Number::new(scope, status as f64);
|
|
362
|
-
obj.set(scope, status_key.into(), status_val.into());
|
|
363
|
-
|
|
364
|
-
let body_key = v8_str(scope, "body");
|
|
365
|
-
let body_val = v8_str(scope, &text);
|
|
366
|
-
obj.set(scope, body_key.into(), body_val.into());
|
|
367
|
-
|
|
368
|
-
let ok_key = v8_str(scope, "ok");
|
|
369
|
-
let ok_val = v8::Boolean::new(scope, true);
|
|
370
|
-
obj.set(scope, ok_key.into(), ok_val.into());
|
|
371
|
-
},
|
|
372
|
-
Err(e) => {
|
|
373
|
-
let ok_key = v8_str(scope, "ok");
|
|
374
|
-
let ok_val = v8::Boolean::new(scope, false);
|
|
375
|
-
obj.set(scope, ok_key.into(), ok_val.into());
|
|
376
|
-
|
|
377
|
-
let err_key = v8_str(scope, "error");
|
|
378
|
-
let err_val = v8_str(scope, &e.to_string());
|
|
379
|
-
obj.set(scope, err_key.into(), err_val.into());
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
retval.set(obj.into());
|
|
383
|
-
}
|
|
384
313
|
|
|
385
314
|
fn native_jwt_sign(scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, mut retval: v8::ReturnValue) {
|
|
386
315
|
let payload_val = args.get(0);
|
|
@@ -529,60 +458,371 @@ fn native_db_query(scope: &mut v8::HandleScope, args: v8::FunctionCallbackArgume
|
|
|
529
458
|
throw(scope, "db.query(): SQL query is required");
|
|
530
459
|
return;
|
|
531
460
|
}
|
|
461
|
+
|
|
462
|
+
// Return Metadata (Non-blocking)
|
|
463
|
+
let obj = v8::Object::new(scope);
|
|
464
|
+
let op_key = v8_str(scope, "__titanAsync");
|
|
465
|
+
let op_val = v8::Boolean::new(scope, true);
|
|
466
|
+
obj.set(scope, op_key.into(), op_val.into());
|
|
532
467
|
|
|
533
|
-
|
|
534
|
-
let
|
|
535
|
-
|
|
468
|
+
let type_key = v8_str(scope, "type");
|
|
469
|
+
let type_val = v8_str(scope, "db_query");
|
|
470
|
+
obj.set(scope, type_key.into(), type_val.into());
|
|
536
471
|
|
|
537
|
-
let
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
472
|
+
let data_obj = v8::Object::new(scope);
|
|
473
|
+
let conn_k = v8_str(scope, "conn");
|
|
474
|
+
let conn_v = v8_str(scope, &conn_string);
|
|
475
|
+
data_obj.set(scope, conn_k.into(), conn_v.into());
|
|
476
|
+
|
|
477
|
+
let q_k = v8_str(scope, "query");
|
|
478
|
+
let q_v = v8_str(scope, &query);
|
|
479
|
+
data_obj.set(scope, q_k.into(), q_v.into());
|
|
480
|
+
|
|
481
|
+
let data_key = v8_str(scope, "data");
|
|
482
|
+
obj.set(scope, data_key.into(), data_obj.into());
|
|
483
|
+
|
|
484
|
+
retval.set(obj.into());
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
fn native_fetch_meta(scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, mut retval: v8::ReturnValue) {
|
|
488
|
+
let url = v8_to_string(scope, args.get(0));
|
|
489
|
+
let opts = args.get(1);
|
|
490
|
+
|
|
491
|
+
let obj = v8::Object::new(scope);
|
|
492
|
+
let op_key = v8_str(scope, "__titanAsync");
|
|
493
|
+
let op_val = v8::Boolean::new(scope, true);
|
|
494
|
+
obj.set(scope, op_key.into(), op_val.into());
|
|
495
|
+
|
|
496
|
+
let type_key = v8_str(scope, "type");
|
|
497
|
+
let type_val = v8_str(scope, "fetch");
|
|
498
|
+
obj.set(scope, type_key.into(), type_val.into());
|
|
499
|
+
|
|
500
|
+
let data_obj = v8::Object::new(scope);
|
|
501
|
+
let url_key = v8_str(scope, "url");
|
|
502
|
+
let url_val = v8_str(scope, &url);
|
|
503
|
+
data_obj.set(scope, url_key.into(), url_val.into());
|
|
504
|
+
|
|
505
|
+
let opts_key = v8_str(scope, "opts");
|
|
506
|
+
data_obj.set(scope, opts_key.into(), opts);
|
|
507
|
+
|
|
508
|
+
let data_key = v8_str(scope, "data");
|
|
509
|
+
obj.set(scope, data_key.into(), data_obj.into());
|
|
510
|
+
|
|
511
|
+
retval.set(obj.into());
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
fn parse_async_op(scope: &mut v8::HandleScope, op_val: v8::Local<v8::Value>) -> Option<super::TitanAsyncOp> {
|
|
515
|
+
if !op_val.is_object() { return None; }
|
|
516
|
+
let op_obj = op_val.to_object(scope).unwrap();
|
|
517
|
+
|
|
518
|
+
let type_key = v8_str(scope, "type");
|
|
519
|
+
let type_obj = op_obj.get(scope, type_key.into())?;
|
|
520
|
+
let op_type = v8_to_string(scope, type_obj);
|
|
521
|
+
|
|
522
|
+
let data_key = v8_str(scope, "data");
|
|
523
|
+
let data_val = op_obj.get(scope, data_key.into())?;
|
|
524
|
+
if !data_val.is_object() { return None; }
|
|
525
|
+
let data_obj = data_val.to_object(scope).unwrap();
|
|
526
|
+
|
|
527
|
+
match op_type.as_str() {
|
|
528
|
+
"fetch" => {
|
|
529
|
+
let url_key = v8_str(scope, "url");
|
|
530
|
+
let url_obj = data_obj.get(scope, url_key.into())?;
|
|
531
|
+
let url = v8_to_string(scope, url_obj);
|
|
532
|
+
|
|
533
|
+
let mut method = "GET".to_string();
|
|
534
|
+
let mut body = None;
|
|
535
|
+
let mut headers = Vec::new();
|
|
536
|
+
|
|
537
|
+
let opts_key = v8_str(scope, "opts");
|
|
538
|
+
if let Some(opts_val) = data_obj.get(scope, opts_key.into()) {
|
|
539
|
+
if opts_val.is_object() {
|
|
540
|
+
let opts_obj = opts_val.to_object(scope).unwrap();
|
|
541
|
+
let m_key = v8_str(scope, "method");
|
|
542
|
+
if let Some(m_val) = opts_obj.get(scope, m_key.into()) {
|
|
543
|
+
if m_val.is_string() { method = v8_to_string(scope, m_val); }
|
|
544
|
+
}
|
|
545
|
+
let b_key = v8_str(scope, "body");
|
|
546
|
+
if let Some(b_val) = opts_obj.get(scope, b_key.into()) {
|
|
547
|
+
if b_val.is_string() {
|
|
548
|
+
body = Some(v8_to_string(scope, b_val));
|
|
549
|
+
} else if b_val.is_object() {
|
|
550
|
+
body = Some(v8::json::stringify(scope, b_val).unwrap().to_rust_string_lossy(scope));
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
let h_key = v8_str(scope, "headers");
|
|
554
|
+
if let Some(h_val) = opts_obj.get(scope, h_key.into()) {
|
|
555
|
+
if h_val.is_object() {
|
|
556
|
+
let h_obj = h_val.to_object(scope).unwrap();
|
|
557
|
+
if let Some(keys) = h_obj.get_own_property_names(scope, Default::default()) {
|
|
558
|
+
for i in 0..keys.length() {
|
|
559
|
+
let key = keys.get_index(scope, i).unwrap();
|
|
560
|
+
let val = h_obj.get(scope, key).unwrap();
|
|
561
|
+
headers.push((v8_to_string(scope, key), v8_to_string(scope, val)));
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
Some(super::TitanAsyncOp::Fetch { url, method, body, headers })
|
|
569
|
+
},
|
|
570
|
+
"db_query" => {
|
|
571
|
+
let conn_key = v8_str(scope, "conn");
|
|
572
|
+
let conn_obj = data_obj.get(scope, conn_key.into())?;
|
|
573
|
+
let conn = v8_to_string(scope, conn_obj);
|
|
574
|
+
let query_key = v8_str(scope, "query");
|
|
575
|
+
let query_obj = data_obj.get(scope, query_key.into())?;
|
|
576
|
+
let query = v8_to_string(scope, query_obj);
|
|
577
|
+
Some(super::TitanAsyncOp::DbQuery { conn, query })
|
|
578
|
+
},
|
|
579
|
+
"fs_read" => {
|
|
580
|
+
let path_key = v8_str(scope, "path");
|
|
581
|
+
let path_obj = data_obj.get(scope, path_key.into())?;
|
|
582
|
+
let path = v8_to_string(scope, path_obj);
|
|
583
|
+
Some(super::TitanAsyncOp::FsRead { path })
|
|
584
|
+
},
|
|
585
|
+
_ => None
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
fn native_drift_call(scope: &mut v8::HandleScope, mut args: v8::FunctionCallbackArguments, mut retval: v8::ReturnValue) {
|
|
590
|
+
let runtime_ptr = unsafe { args.get_isolate() }.get_data(0) as *mut super::TitanRuntime;
|
|
591
|
+
let runtime = unsafe { &mut *runtime_ptr };
|
|
592
|
+
// let isolate_id = runtime.id; // We can use runtime.id directly later
|
|
593
|
+
|
|
594
|
+
let arg0 = args.get(0);
|
|
595
|
+
|
|
596
|
+
let (async_op, op_type) = if arg0.is_array() {
|
|
597
|
+
let arr = v8::Local::<v8::Array>::try_from(arg0).unwrap();
|
|
598
|
+
let mut ops = Vec::new();
|
|
599
|
+
for i in 0..arr.length() {
|
|
600
|
+
let op_val = arr.get_index(scope, i).unwrap();
|
|
601
|
+
if let Some(op) = parse_async_op(scope, op_val) {
|
|
602
|
+
ops.push(op);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
(super::TitanAsyncOp::Batch(ops), "batch".to_string())
|
|
606
|
+
} else {
|
|
607
|
+
match parse_async_op(scope, arg0) {
|
|
608
|
+
Some(op) => {
|
|
609
|
+
let t = match &op {
|
|
610
|
+
super::TitanAsyncOp::Fetch { .. } => "fetch",
|
|
611
|
+
super::TitanAsyncOp::DbQuery { .. } => "db_query",
|
|
612
|
+
super::TitanAsyncOp::FsRead { .. } => "fs_read",
|
|
613
|
+
_ => "unknown"
|
|
614
|
+
};
|
|
615
|
+
(op, t.to_string())
|
|
616
|
+
},
|
|
617
|
+
None => {
|
|
618
|
+
throw(scope, "drift() requires an async operation or array of operations");
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
542
621
|
}
|
|
543
622
|
};
|
|
623
|
+
|
|
624
|
+
let runtime_ptr = unsafe { args.get_isolate() }.get_data(0) as *mut super::TitanRuntime;
|
|
625
|
+
let runtime = unsafe { &mut *runtime_ptr };
|
|
544
626
|
|
|
545
|
-
//
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
627
|
+
// Extract request_id from globalThis.__titan_req.__titan_request_id
|
|
628
|
+
let req_id = {
|
|
629
|
+
let context = scope.get_current_context();
|
|
630
|
+
let global = context.global(scope);
|
|
631
|
+
let req_key = v8_str(scope, "__titan_req");
|
|
632
|
+
if let Some(req_obj_val) = global.get(scope, req_key.into()) {
|
|
633
|
+
if req_obj_val.is_object() {
|
|
634
|
+
let req_obj = req_obj_val.to_object(scope).unwrap();
|
|
635
|
+
let id_key = v8_str(scope, "__titan_request_id");
|
|
636
|
+
req_obj.get(scope, id_key.into()).unwrap().uint32_value(scope).unwrap_or(0)
|
|
637
|
+
} else { 0 }
|
|
638
|
+
} else { 0 }
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
runtime.drift_counter += 1;
|
|
642
|
+
let drift_id = runtime.drift_counter;
|
|
643
|
+
|
|
644
|
+
if req_id != 0 {
|
|
645
|
+
runtime.drift_to_request.insert(drift_id, req_id);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
// --- REPLAY CHECK ---
|
|
649
|
+
// If the result exists, return it immediately (Replay Phase)
|
|
650
|
+
// IMPORTANT: Use .get() not .remove() to allow multiple drifts in the same action
|
|
651
|
+
if let Some(res) = runtime.completed_drifts.get(&drift_id) {
|
|
652
|
+
let json_str = serde_json::to_string(res).unwrap_or_else(|_| "null".to_string());
|
|
653
|
+
let v8_str = v8::String::new(scope, &json_str).unwrap();
|
|
654
|
+
let mut try_catch = v8::TryCatch::new(scope);
|
|
655
|
+
if let Some(val) = v8::json::parse(&mut try_catch, v8_str) {
|
|
656
|
+
retval.set(val);
|
|
657
|
+
} else {
|
|
658
|
+
retval.set(v8::null(&mut try_catch).into());
|
|
659
|
+
}
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
let (tx, rx) = tokio::sync::oneshot::channel::<super::WorkerAsyncResult>();
|
|
664
|
+
|
|
665
|
+
// Send to global async executor
|
|
666
|
+
let req = super::AsyncOpRequest {
|
|
667
|
+
op: async_op,
|
|
668
|
+
drift_id,
|
|
669
|
+
request_id: req_id,
|
|
670
|
+
op_type,
|
|
671
|
+
respond_tx: tx,
|
|
672
|
+
};
|
|
673
|
+
|
|
674
|
+
if let Err(e) = runtime.global_async_tx.try_send(req) {
|
|
675
|
+
println!("[Titan] Drift Call Failed to queue: {}", e);
|
|
676
|
+
retval.set(v8::null(scope).into());
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
// --- SUSPENSION THROW ---
|
|
681
|
+
// We throw a specific exception to halt execution. The Runtime catches this,
|
|
682
|
+
// frees the worker, and waits for the async result.
|
|
683
|
+
|
|
684
|
+
// Trigger Tokio task completion handling in a separate bridge
|
|
685
|
+
let tokio_handle = runtime.tokio_handle.clone();
|
|
686
|
+
let worker_tx = runtime.worker_tx.clone();
|
|
687
|
+
let isolate_id = runtime.id;
|
|
688
|
+
|
|
689
|
+
tokio_handle.spawn(async move {
|
|
690
|
+
if let Ok(res) = rx.await {
|
|
691
|
+
// Signal the pool to RESUME (REPLAY) this specific isolate
|
|
692
|
+
let _ = worker_tx.send(crate::runtime::WorkerCommand::Resume {
|
|
693
|
+
isolate_id,
|
|
694
|
+
drift_id,
|
|
695
|
+
result: res,
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
|
|
700
|
+
throw(scope, "__SUSPEND__");
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
fn native_finish_request(scope: &mut v8::HandleScope, mut args: v8::FunctionCallbackArguments, _retval: v8::ReturnValue) {
|
|
704
|
+
let request_id = args.get(0).uint32_value(scope).unwrap_or(0);
|
|
705
|
+
let result_val = args.get(1);
|
|
706
|
+
|
|
707
|
+
let json = super::v8_to_json(scope, result_val);
|
|
708
|
+
|
|
709
|
+
let runtime_ptr = unsafe { args.get_isolate() }.get_data(0) as *mut super::TitanRuntime;
|
|
710
|
+
let runtime = unsafe { &mut *runtime_ptr };
|
|
711
|
+
|
|
712
|
+
let timings = runtime.request_timings.remove(&request_id).unwrap_or_default();
|
|
713
|
+
|
|
714
|
+
// Cleanup drift mapping for this request
|
|
715
|
+
runtime.drift_to_request.retain(|drift_id, v| {
|
|
716
|
+
if *v == request_id {
|
|
717
|
+
runtime.completed_drifts.remove(drift_id);
|
|
718
|
+
false
|
|
719
|
+
} else {
|
|
720
|
+
true
|
|
721
|
+
}
|
|
722
|
+
});
|
|
723
|
+
|
|
724
|
+
if let Some(tx) = runtime.pending_requests.remove(&request_id) {
|
|
725
|
+
let _ = tx.send(crate::runtime::WorkerResult { json, timings });
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
pub async fn run_single_op(op: super::TitanAsyncOp) -> serde_json::Value {
|
|
730
|
+
match op {
|
|
731
|
+
super::TitanAsyncOp::Fetch { url, method, body, headers } => {
|
|
732
|
+
let client = get_http_client();
|
|
733
|
+
let mut req = client.request(method.parse().unwrap_or(reqwest::Method::GET), &url);
|
|
734
|
+
if let Some(b) = body { req = req.body(b); }
|
|
735
|
+
for (k, v) in headers {
|
|
736
|
+
if let (Ok(name), Ok(val)) = (reqwest::header::HeaderName::from_bytes(k.as_bytes()), reqwest::header::HeaderValue::from_str(&v)) {
|
|
737
|
+
req = req.header(name, val);
|
|
570
738
|
}
|
|
571
|
-
|
|
572
|
-
result.push(serde_json::Value::Object(obj));
|
|
573
739
|
}
|
|
740
|
+
match req.send().await {
|
|
741
|
+
Ok(res) => {
|
|
742
|
+
let status = res.status().as_u16();
|
|
743
|
+
let text = res.text().await.unwrap_or_default();
|
|
744
|
+
serde_json::json!({ "status": status, "body": text, "ok": true })
|
|
745
|
+
},
|
|
746
|
+
Err(e) => serde_json::json!({ "error": e.to_string(), "ok": false })
|
|
747
|
+
}
|
|
748
|
+
},
|
|
749
|
+
super::TitanAsyncOp::FsRead { path } => {
|
|
750
|
+
let root = super::PROJECT_ROOT.get().cloned().unwrap_or_else(|| std::env::current_dir().unwrap_or_default());
|
|
751
|
+
let joined = root.join(&path);
|
|
574
752
|
|
|
575
|
-
|
|
576
|
-
let
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
753
|
+
// Basic security check
|
|
754
|
+
if let Ok(target) = joined.canonicalize() {
|
|
755
|
+
if target.starts_with(&root.canonicalize().unwrap_or(root)) {
|
|
756
|
+
match std::fs::read_to_string(&target) {
|
|
757
|
+
Ok(content) => serde_json::json!(content),
|
|
758
|
+
Err(e) => serde_json::json!({ "error": format!("File read failed: {}", e) })
|
|
759
|
+
}
|
|
760
|
+
} else {
|
|
761
|
+
serde_json::json!({ "error": "Path escapes project root" })
|
|
762
|
+
}
|
|
580
763
|
} else {
|
|
581
|
-
|
|
764
|
+
serde_json::json!({ "error": format!("File not found: {}", path) })
|
|
582
765
|
}
|
|
583
766
|
},
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
767
|
+
super::TitanAsyncOp::DbQuery { conn, query } => {
|
|
768
|
+
tokio::task::spawn_blocking(move || {
|
|
769
|
+
let mut pool = DB_POOL.lock().unwrap();
|
|
770
|
+
if let Some(map) = pool.as_mut() {
|
|
771
|
+
if let Some(client) = map.get_mut(&conn) {
|
|
772
|
+
return match client.query(&query, &[]) {
|
|
773
|
+
Ok(rows) => {
|
|
774
|
+
let mut result = Vec::new();
|
|
775
|
+
for row in rows {
|
|
776
|
+
let mut obj = serde_json::Map::new();
|
|
777
|
+
for (i, column) in row.columns().iter().enumerate() {
|
|
778
|
+
let col_name = column.name();
|
|
779
|
+
let col_value: serde_json::Value = if let Ok(val) = row.try_get::<_, Option<String>>(i) {
|
|
780
|
+
serde_json::json!(val)
|
|
781
|
+
} else if let Ok(val) = row.try_get::<_, Option<i32>>(i) {
|
|
782
|
+
serde_json::json!(val)
|
|
783
|
+
} else if let Ok(val) = row.try_get::<_, Option<i64>>(i) {
|
|
784
|
+
serde_json::json!(val)
|
|
785
|
+
} else if let Ok(val) = row.try_get::<_, Option<f64>>(i) {
|
|
786
|
+
serde_json::json!(val)
|
|
787
|
+
} else if let Ok(val) = row.try_get::<_, Option<bool>>(i) {
|
|
788
|
+
serde_json::json!(val)
|
|
789
|
+
} else {
|
|
790
|
+
serde_json::Value::Null
|
|
791
|
+
};
|
|
792
|
+
obj.insert(col_name.to_string(), col_value);
|
|
793
|
+
}
|
|
794
|
+
result.push(serde_json::Value::Object(obj));
|
|
795
|
+
}
|
|
796
|
+
serde_json::Value::Array(result)
|
|
797
|
+
},
|
|
798
|
+
Err(e) => serde_json::json!({ "error": e.to_string() })
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
serde_json::json!({ "error": "Database connection not found" })
|
|
803
|
+
}).await.unwrap_or_else(|e| serde_json::json!({ "error": e.to_string() }))
|
|
804
|
+
},
|
|
805
|
+
_ => serde_json::json!({ "error": "Invalid operation" })
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
pub async fn run_async_operation(op: super::TitanAsyncOp) -> serde_json::Value {
|
|
810
|
+
match op {
|
|
811
|
+
super::TitanAsyncOp::Batch(ops) => {
|
|
812
|
+
let mut set = tokio::task::JoinSet::new();
|
|
813
|
+
for (i, op) in ops.into_iter().enumerate() {
|
|
814
|
+
set.spawn(async move {
|
|
815
|
+
(i, run_single_op(op).await)
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
let mut results_map = std::collections::BTreeMap::new();
|
|
819
|
+
while let Some(res) = set.join_next().await {
|
|
820
|
+
if let Ok((i, val)) = res {
|
|
821
|
+
results_map.insert(i, val);
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
serde_json::Value::Array(results_map.into_values().collect())
|
|
825
|
+
},
|
|
826
|
+
_ => run_single_op(op).await
|
|
587
827
|
}
|
|
588
828
|
}
|