voidmerge 0.0.25

VoidMerge: The open-source, developer friendly web services platform.
Documentation
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
//! Javascript execution.

use crate::*;
use bytes::Bytes;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

/// Input to a javascript execution.
#[derive(Clone, serde::Serialize, serde::Deserialize)]
#[serde(
    tag = "type",
    rename_all = "camelCase",
    rename_all_fields = "camelCase"
)]
pub enum JsRequest {
    /// Get the code config.
    CodeConfigReq,
    /// Execute the cron code.
    CronReq,
    /// Validate an object to be stored.
    ObjCheckReq {
        /// The content payload of the object.
        data: Bytes,

        /// The metadata of the object.
        meta: crate::obj::ObjMeta,
    },
    /// Incoming function request.
    FnReq {
        /// The method ("GET" or "PUT").
        method: String,
        /// The request url.
        path: String,
        /// The body content.
        body: Option<Bytes>,
        /// Any sent headers.
        headers: HashMap<String, String>,
    },
}

impl std::fmt::Debug for JsRequest {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::CodeConfigReq => {
                f.debug_struct("JsRequest::CodeConfigReq").finish()
            }
            Self::CronReq => f.debug_struct("JsRequest::CronReq").finish(),
            Self::ObjCheckReq { meta, .. } => f
                .debug_struct("JsRequest::ObjCheckReq")
                .field("meta", meta)
                .finish(),
            Self::FnReq {
                method, path, body, ..
            } => f
                .debug_struct("JsRequest::FnReq")
                .field("method", method)
                .field("path", path)
                .field("body_len", &body.as_ref().map(|b| b.len()).unwrap_or(0))
                .finish(),
        }
    }
}

fn status() -> f64 {
    200.0
}

/// Output from a javascript execution.
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(
    tag = "type",
    rename_all = "camelCase",
    rename_all_fields = "camelCase"
)]
pub enum JsResponse {
    /// Return this for code config requests.
    CodeConfigResOk {
        /// Interval for running cron executions.
        #[serde(default)]
        cron_interval_secs: Option<f64>,
    },

    /// Cron Ok Response.
    CronResOk,

    /// Return this in case of ObjCheck request success.
    ObjCheckResOk,

    /// Outgoing function response.
    FnResOk {
        /// The status code to respond with.
        #[serde(default = "status")]
        status: f64,
        /// The body content.
        #[serde(default)]
        body: Bytes,
        /// Any headers to send.
        #[serde(default)]
        headers: HashMap<String, String>,
    },
}

impl std::fmt::Debug for JsResponse {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::CodeConfigResOk { cron_interval_secs } => f
                .debug_struct("JsResponset::CodeConfigResOk")
                .field("cron_interval_secs", &cron_interval_secs)
                .finish(),
            Self::CronResOk => f.debug_struct("JsResponse::CronResOk").finish(),
            Self::ObjCheckResOk => {
                f.debug_struct("JsRequest::ObjCheckResOk").finish()
            }
            Self::FnResOk { status, body, .. } => f
                .debug_struct("JsRequest::FnResOk")
                .field("status", status)
                .field("body_len", &body.len())
                .finish(),
        }
    }
}

static MAX_THREADS: std::sync::OnceLock<usize> = std::sync::OnceLock::new();

/// Set the max thread count. (Default: 32).
pub fn js_global_set_max_thread(count: usize) -> bool {
    MAX_THREADS.set(count).is_ok()
}

fn js_global_get_max_thread() -> usize {
    *MAX_THREADS.get_or_init(|| 32)
}

static MAX_RAM: std::sync::OnceLock<usize> = std::sync::OnceLock::new();

/// Set max RAM to use. (Default: 768 MiB).
pub fn js_global_set_max_ram(count: usize) -> bool {
    MAX_RAM.set(count).is_ok()
}

fn js_global_get_max_ram() -> usize {
    *MAX_RAM.get_or_init(|| 768 * 1024 * 1024)
}

/// Javascript setup info.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct JsSetup {
    /// The current VoidMerge runtime.
    pub runtime: Runtime,

    /// The current context.
    pub ctx: Arc<str>,

    /// Maximum execution time. Default: 10s.
    pub timeout: std::time::Duration,

    /// Max heap size for the context. Default: 32 MiB.
    pub heap_size: usize,

    /// Javascript code to initialize.
    pub code: Arc<str>,

    /// Javascript env to make available.
    pub env: Arc<serde_json::Value>,
}

impl JsSetup {
    /// Default timeout.
    pub const DEF_TIMEOUT: std::time::Duration =
        std::time::Duration::from_secs(10);

    /// Default heap size.
    pub const DEF_HEAP_SIZE: usize = 1024 * 1024 * 32;
}

static JS: std::sync::OnceLock<Js> = std::sync::OnceLock::new();

/// Javascript executor type.
pub trait JsExec: 'static + Send + Sync {
    /// Execute some javascript code.
    fn exec(
        &self,
        setup: JsSetup,
        request: JsRequest,
    ) -> BoxFut<'_, Result<JsResponse>>;
}

/// Dyn [JsExec] type.
pub type DynJsExec = Arc<dyn JsExec + 'static + Send + Sync>;
type WeakJsExec = std::sync::Weak<dyn JsExec + 'static + Send + Sync>;

/// Default Javascript executor type.
pub struct JsExecDefault(WeakJsExec);

impl JsExecDefault {
    /// Get the default executor instance.
    pub fn create() -> DynJsExec {
        let out: DynJsExec = Arc::new_cyclic(|this: &std::sync::Weak<Self>| {
            JsExecDefault(this.clone())
        });
        out
    }
}

impl JsExec for JsExecDefault {
    fn exec(
        &self,
        setup: JsSetup,
        request: JsRequest,
    ) -> BoxFut<'_, Result<JsResponse>> {
        Box::pin(async move {
            JS.get_or_init(Js::new)
                .exec(setup, request, self.0.clone())
                .await
        })
    }
}

/// Javascript Executor Wrapper Adding Metering.
pub struct JsExecMeter(pub DynJsExec);

impl JsExecMeter {
    /// Create a JsExecMeter wrapper around another javascript executor.
    pub fn create(inner: DynJsExec) -> DynJsExec {
        let out: DynJsExec = Arc::new(Self(inner));
        out
    }
}

impl JsExec for JsExecMeter {
    fn exec(
        &self,
        setup: JsSetup,
        request: JsRequest,
    ) -> BoxFut<'_, Result<JsResponse>> {
        Box::pin(async move {
            let ctx = setup.ctx.clone();
            let mem = setup.heap_size;

            let start = std::time::Instant::now();
            let res = self.0.exec(setup, request).await;
            let mut elapsed_millis = start.elapsed().as_millis();

            if elapsed_millis < 100 {
                elapsed_millis = 100;
            }

            crate::meter::meter_fn_mib_milli(
                &ctx,
                (mem as u128 * elapsed_millis) / 1048576,
            );

            res
        })
    }
}

/// Javascript execution.
struct Js {
    thread_limit: Arc<tokio::sync::Semaphore>,
    ram_mib_limit: Arc<tokio::sync::Semaphore>,
    pool: Arc<Mutex<JsPool>>,
}

impl Js {
    pub fn new() -> Self {
        let max_threads = js_global_get_max_thread();
        let max_ram = js_global_get_max_ram();
        if max_ram < 1024 * 1024 {
            panic!("max ram cannot be less that 1MiB");
        }
        let max_ram_mib = max_ram / (1024 * 1024);
        if max_ram_mib > u32::MAX as usize {
            panic!("max ram is too large in MiB for a u32");
        }
        Self {
            thread_limit: Arc::new(tokio::sync::Semaphore::new(max_threads)),
            ram_mib_limit: Arc::new(tokio::sync::Semaphore::new(max_ram_mib)),
            pool: Arc::new(Mutex::new(JsPool::new(max_threads))),
        }
    }

    pub async fn exec(
        &self,
        setup: JsSetup,
        request: JsRequest,
        weak: WeakJsExec,
    ) -> Result<JsResponse> {
        let avail = self.ram_mib_limit.available_permits() * 1024 * 1024;
        let want = setup.heap_size;
        let clear = want.saturating_sub(avail);
        let mut found = self.pool.lock().unwrap().get_thread(&setup, clear);

        if found.is_none() {
            let t_fut = self.thread_limit.clone().acquire_owned();

            if setup.heap_size < 1024 * 1024 {
                panic!("heap_size cannot be less than 1 MiB");
            }

            let r_fut = self
                .ram_mib_limit
                .clone()
                .acquire_many_owned((setup.heap_size / (1024 * 1024)) as u32);

            let (thread_permit, ram_permit) =
                tokio::try_join!(t_fut, r_fut).expect("permit error");

            found = Some(self.pool.lock().unwrap().get_or_create_thread(
                thread_permit,
                ram_permit,
                &setup,
            ));
        }

        let thread = found.unwrap();

        let out = thread.exec(setup.clone(), request, weak).await;

        // if the thread errored, don't return it
        // if we are out of permits, don't return it
        if thread.is_ready() && self.ram_mib_limit.available_permits() > 0 {
            self.pool.lock().unwrap().put_thread(setup, thread);
        }

        out
    }
}

struct JsPool {
    #[allow(dead_code)]
    max_threads: usize,
    last_prune: std::time::Instant,
    threads: HashMap<JsSetup, Vec<JsThread>>,
}

impl JsPool {
    pub fn new(max_threads: usize) -> Self {
        Self {
            max_threads,
            last_prune: std::time::Instant::now(),
            threads: Default::default(),
        }
    }

    pub fn get_thread(
        &mut self,
        want_setup: &JsSetup,
        clear_heap: usize,
    ) -> Option<JsThread> {
        if self.last_prune.elapsed() > std::time::Duration::from_secs(5) {
            self.last_prune = std::time::Instant::now();
            self.threads.retain(|_, list| !list.is_empty());
        }

        // if we have a matching thread cached, return it
        if let Some(list) = self.threads.get_mut(want_setup) {
            while !list.is_empty() {
                let thread = list.remove(0);
                if thread.is_ready() {
                    return Some(thread);
                }
            }
        }

        // otherwise, try to clear enough space for the request
        let mut clear_amount = 0;
        self.threads.retain(|setup, list| {
            list.retain(|_| {
                if clear_amount < clear_heap {
                    clear_amount += setup.heap_size;
                    false
                } else {
                    true
                }
            });
            !list.is_empty()
        });

        None
    }

    pub fn get_or_create_thread(
        &mut self,
        thread_permit: tokio::sync::OwnedSemaphorePermit,
        ram_permit: tokio::sync::OwnedSemaphorePermit,
        setup: &JsSetup,
    ) -> JsThread {
        // we can set a clear heap size of zero here,
        // since we already got the permit.
        match self.get_thread(setup, 0) {
            Some(thread) => thread,
            None => JsThread::new(thread_permit, ram_permit),
        }
    }

    pub fn put_thread(&mut self, setup: JsSetup, thread: JsThread) {
        self.threads.entry(setup).or_default().push(thread);
    }
}

use deno_core::OpState;
use std::cell::RefCell;
use std::rc::Rc;

struct TState {
    pub setup: JsSetup,
    pub weak: WeakJsExec,
}

impl TState {
    pub fn new(setup: JsSetup, weak: WeakJsExec) -> Self {
        TState { setup, weak }
    }
}

mod deno_ext {
    use super::*;

    #[deno_core::op2]
    #[serde]
    fn op_get_ctx(
        state: Rc<RefCell<OpState>>,
    ) -> std::result::Result<Arc<str>, deno_core::error::CoreError> {
        match state.borrow().try_borrow::<TState>() {
            Some(TState { setup, .. }) => Ok(setup.ctx.clone()),
            _ => Err(deno_core::error::CoreErrorKind::Io(Error::other(
                "bad state",
            ))
            .into()),
        }
    }

    #[deno_core::op2]
    #[serde]
    fn op_get_env(
        state: Rc<RefCell<OpState>>,
    ) -> std::result::Result<Arc<serde_json::Value>, deno_core::error::CoreError>
    {
        match state.borrow().try_borrow::<TState>() {
            Some(TState { setup, .. }) => Ok(setup.env.clone()),
            _ => Err(deno_core::error::CoreErrorKind::Io(Error::other(
                "bad state",
            ))
            .into()),
        }
    }

    #[deno_core::op2]
    #[buffer]
    fn op_to_utf8(#[string] input: &str) -> Vec<u8> {
        input.as_bytes().to_vec()
    }

    #[deno_core::op2]
    #[string]
    fn op_from_utf8(#[buffer] input: &[u8]) -> String {
        String::from_utf8_lossy(input).to_string()
    }

    #[derive(Debug, serde::Serialize)]
    struct MsgNewOutput {
        #[serde(rename = "msgId")]
        msg_id: Arc<str>,
    }

    #[deno_core::op2(async)]
    #[serde]
    async fn op_msg_new(
        state: Rc<RefCell<OpState>>,
    ) -> std::result::Result<MsgNewOutput, deno_core::error::CoreError> {
        let setup = match state.borrow().try_borrow::<TState>() {
            Some(TState { setup, .. }) => setup.clone(),
            _ => {
                return Err(deno_core::error::CoreErrorKind::Io(Error::other(
                    "bad state",
                ))
                .into());
            }
        };

        let msg_id = setup.runtime.msg()?.create(setup.ctx).await?;

        Ok(MsgNewOutput { msg_id })
    }

    #[derive(Debug, serde::Serialize)]
    struct MsgListOutput {
        #[serde(rename = "msgIdList")]
        msg_id_list: Vec<Arc<str>>,
    }

    #[deno_core::op2(async)]
    #[serde]
    async fn op_msg_list(
        state: Rc<RefCell<OpState>>,
    ) -> std::result::Result<MsgListOutput, deno_core::error::CoreError> {
        let setup = match state.borrow().try_borrow::<TState>() {
            Some(TState { setup, .. }) => setup.clone(),
            _ => {
                return Err(deno_core::error::CoreErrorKind::Io(Error::other(
                    "bad state",
                ))
                .into());
            }
        };

        let msg_id_list = setup.runtime.msg()?.list(setup.ctx).await?;

        Ok(MsgListOutput { msg_id_list })
    }

    #[derive(Debug, serde::Deserialize)]
    struct MsgSendInput {
        #[serde(rename = "msgId")]
        msg_id: Arc<str>,

        msg: bytes::Bytes,
    }

    #[deno_core::op2(async)]
    async fn op_msg_send(
        state: Rc<RefCell<OpState>>,
        #[serde] input: MsgSendInput,
    ) -> std::result::Result<(), deno_core::error::CoreError> {
        let setup = match state.borrow().try_borrow::<TState>() {
            Some(TState { setup, .. }) => setup.clone(),
            _ => {
                return Err(deno_core::error::CoreErrorKind::Io(Error::other(
                    "bad state",
                ))
                .into());
            }
        };

        setup
            .runtime
            .msg()?
            .send(
                setup.ctx,
                input.msg_id,
                crate::msg::Message::App { msg: input.msg },
            )
            .await?;

        Ok(())
    }

    #[derive(Debug, serde::Deserialize)]
    struct ObjPutInput {
        #[serde(default)]
        meta: Arc<str>,

        #[serde(default)]
        data: bytes::Bytes,
    }

    #[derive(Debug, serde::Serialize)]
    struct ObjPutOutput {
        meta: Arc<str>,
    }

    #[deno_core::op2(async)]
    #[serde]
    async fn op_obj_put(
        state: Rc<RefCell<OpState>>,
        #[serde] input: ObjPutInput,
    ) -> std::result::Result<ObjPutOutput, deno_core::error::CoreError> {
        let (setup, weak) = match state.borrow().try_borrow::<TState>() {
            Some(TState { setup, weak }) => (setup.clone(), weak.clone()),
            _ => {
                return Err(deno_core::error::CoreErrorKind::Io(Error::other(
                    "bad state",
                ))
                .into());
            }
        };

        let input_meta = crate::obj::ObjMeta(input.meta);

        let meta = crate::obj::ObjMeta::new_context(
            &setup.ctx,
            input_meta.app_path(),
            safe_now(),
            input_meta.expires_secs(),
            input.data.len() as f64,
        );

        if let Some(exec) = weak.upgrade() {
            match exec
                .exec(
                    setup.clone(),
                    JsRequest::ObjCheckReq {
                        data: input.data.clone(),
                        meta: meta.clone(),
                    },
                )
                .await
            {
                Ok(JsResponse::ObjCheckResOk) => (),
                oth => {
                    return Err(deno_core::error::CoreErrorKind::Io(
                        Error::other(format!(
                            "invalid obj check response: {oth:?}"
                        )),
                    )
                    .into());
                }
            }
        } else {
            return Err(deno_core::error::CoreErrorKind::Io(Error::other(
                "aborting obj put due to shutdown",
            ))
            .into());
        }

        setup
            .runtime
            .obj()?
            .put(meta.clone(), input.data)
            .await
            .map_err(|err| {
                deno_core::error::CoreError::from(
                    deno_core::error::CoreErrorKind::Io(err),
                )
            })?;

        Ok(ObjPutOutput { meta: meta.0 })
    }

    #[derive(Debug, serde::Deserialize)]
    struct ObjGetInput {
        #[serde(default)]
        meta: Arc<str>,
    }

    #[derive(Debug, serde::Serialize)]
    struct ObjGetOutput {
        meta: Arc<str>,
        data: Bytes,
    }

    #[deno_core::op2(async)]
    #[serde]
    async fn op_obj_get(
        state: Rc<RefCell<OpState>>,
        #[serde] input: ObjGetInput,
    ) -> std::result::Result<ObjGetOutput, deno_core::error::CoreError> {
        let setup = match state.borrow().try_borrow::<TState>() {
            Some(TState { setup, .. }) => setup.clone(),
            _ => {
                return Err(deno_core::error::CoreErrorKind::Io(Error::other(
                    "bad state",
                ))
                .into());
            }
        };

        let meta = crate::obj::ObjMeta(input.meta);
        if meta.sys_prefix() != crate::obj::ObjMeta::SYS_CTX {
            return Err(deno_core::error::CoreErrorKind::Io(Error::other(
                "invalid sys prefix",
            ))
            .into());
        }
        if meta.ctx() != &*setup.ctx {
            return Err(deno_core::error::CoreErrorKind::Io(Error::other(
                "invalid sys context",
            ))
            .into());
        }
        let (meta, data) =
            setup.runtime.obj()?.get(meta).await.map_err(|err| {
                deno_core::error::CoreError::from(
                    deno_core::error::CoreErrorKind::Io(err),
                )
            })?;

        Ok(ObjGetOutput { meta: meta.0, data })
    }

    #[derive(Debug, serde::Deserialize)]
    struct ObjRmInput {
        #[serde(default)]
        meta: Arc<str>,
    }

    #[deno_core::op2(async)]
    async fn op_obj_rm(
        state: Rc<RefCell<OpState>>,
        #[serde] input: ObjRmInput,
    ) -> std::result::Result<(), deno_core::error::CoreError> {
        let setup = match state.borrow().try_borrow::<TState>() {
            Some(TState { setup, .. }) => setup.clone(),
            _ => {
                return Err(deno_core::error::CoreErrorKind::Io(Error::other(
                    "bad state",
                ))
                .into());
            }
        };

        let meta = crate::obj::ObjMeta(input.meta);
        if meta.sys_prefix() != crate::obj::ObjMeta::SYS_CTX {
            return Err(deno_core::error::CoreErrorKind::Io(Error::other(
                "invalid sys prefix",
            ))
            .into());
        }
        if meta.ctx() != &*setup.ctx {
            return Err(deno_core::error::CoreErrorKind::Io(Error::other(
                "invalid sys context",
            ))
            .into());
        }
        setup.runtime.obj()?.rm(meta).await.map_err(|err| {
            deno_core::error::CoreError::from(
                deno_core::error::CoreErrorKind::Io(err),
            )
        })?;

        Ok(())
    }

    fn f64_1000() -> f64 {
        1000.0
    }

    #[derive(Debug, serde::Deserialize)]
    struct ObjListInput {
        #[serde(rename = "appPathPrefix", default)]
        app_path_prefix: Arc<str>,

        #[serde(rename = "createdGt", default)]
        created_gt: f64,

        #[serde(default = "f64_1000")]
        limit: f64,
    }

    #[derive(Debug, serde::Serialize)]
    struct ObjListOutput {
        #[serde(rename = "metaList")]
        meta_list: Vec<crate::obj::ObjMeta>,
    }

    #[deno_core::op2(async)]
    #[serde]
    async fn op_obj_list(
        state: Rc<RefCell<OpState>>,
        #[serde] input: ObjListInput,
    ) -> std::result::Result<ObjListOutput, deno_core::error::CoreError> {
        let setup = match state.borrow().try_borrow::<TState>() {
            Some(TState { setup, .. }) => setup.clone(),
            _ => {
                return Err(deno_core::error::CoreErrorKind::Io(Error::other(
                    "bad state",
                ))
                .into());
            }
        };

        let path = format!(
            "{}/{}/{}",
            crate::obj::ObjMeta::SYS_CTX,
            setup.ctx,
            input.app_path_prefix,
        );

        let limit = input.limit.clamp(0.0, 1000.0) as u32;

        let result = setup
            .runtime
            .obj()?
            .list(&path, input.created_gt, limit)
            .await
            .map_err(|err| {
                deno_core::error::CoreError::from(
                    deno_core::error::CoreErrorKind::Io(err),
                )
            })?;

        Ok(ObjListOutput { meta_list: result })
    }

    deno_core::extension!(
        vm,
        deps = [deno_console],
        ops = [
            op_get_ctx,
            op_get_env,
            op_to_utf8,
            op_from_utf8,
            op_msg_new,
            op_msg_list,
            op_msg_send,
            op_obj_put,
            op_obj_get,
            op_obj_rm,
            op_obj_list,
        ],
        esm_entry_point = "ext:vm/entry.js",
        esm = [ dir "src/js", "entry.js" ],
    );
}

#[allow(clippy::large_enum_variant)]
enum Cmd {
    Kill,
    Exec {
        setup: JsSetup,
        request: JsRequest,
        weak: WeakJsExec,
        output: tokio::sync::oneshot::Sender<Result<JsResponse>>,
    },
}

struct JsThread {
    _thread_permit: tokio::sync::OwnedSemaphorePermit,
    _ram_permit: tokio::sync::OwnedSemaphorePermit,
    is_ready: Arc<std::sync::atomic::AtomicBool>,
    thread: Option<std::thread::JoinHandle<()>>,
    cmd_send: Option<tokio::sync::mpsc::Sender<Cmd>>,
}

impl Drop for JsThread {
    fn drop(&mut self) {
        let cmd_send = self.cmd_send.take();
        if tokio::runtime::Handle::try_current().is_ok() {
            tokio::task::spawn(async move {
                if let Some(cmd_send) = cmd_send {
                    let _ = cmd_send.send(Cmd::Kill).await;
                }
            });
            if let Some(thread) = self.thread.take() {
                tokio::task::spawn_blocking(move || {
                    let _ = thread.join();
                });
            }
        } else {
            let mut dangle = false;
            if let Some(cmd_send) = cmd_send
                && cmd_send.try_send(Cmd::Kill).is_err()
            {
                eprintln!(
                    "FAILED TO SEND KILL, maybe leaving a thread dangling"
                );
                tracing::error!(
                    "FAILED TO SEND KILL, maybe leaving a thread dangling"
                );
                dangle = true;
            }
            if let Some(thread) = self.thread.take()
                && !dangle
            {
                let _ = thread.join();
            }
        }
    }
}

impl JsThread {
    pub fn is_ready(&self) -> bool {
        self.is_ready.load(std::sync::atomic::Ordering::SeqCst)
    }

    pub async fn exec(
        &self,
        setup: JsSetup,
        request: JsRequest,
        weak: WeakJsExec,
    ) -> Result<JsResponse> {
        let (output, r) = tokio::sync::oneshot::channel();
        self.cmd_send
            .as_ref()
            .unwrap()
            .send(Cmd::Exec {
                setup,
                request,
                weak,
                output,
            })
            .await
            .map_err(|_| std::io::Error::other("thread error"))?;
        r.await.map_err(|_| std::io::Error::other("thread error"))?
    }

    pub fn new(
        thread_permit: tokio::sync::OwnedSemaphorePermit,
        ram_permit: tokio::sync::OwnedSemaphorePermit,
    ) -> Self {
        let is_ready = Arc::new(std::sync::atomic::AtomicBool::new(true));

        struct D(Arc<std::sync::atomic::AtomicBool>);

        impl Drop for D {
            fn drop(&mut self) {
                self.not_ready();
            }
        }

        impl D {
            pub fn not_ready(&self) {
                self.0.store(false, std::sync::atomic::Ordering::SeqCst);
            }
        }

        let on_drop = D(is_ready.clone());

        let (cmd_send, mut cmd_recv) = tokio::sync::mpsc::channel(32);
        let thread = std::thread::spawn(move || {
            let on_drop = on_drop;

            let mut cur_setup;
            let mut cur_request;
            let mut cur_weak;
            let mut cur_output;

            match cmd_recv.blocking_recv() {
                None => return,
                Some(Cmd::Kill) => return,
                Some(Cmd::Exec {
                    setup,
                    request,
                    weak,
                    output,
                }) => {
                    cur_setup = setup;
                    cur_request = request;
                    cur_weak = weak;
                    cur_output = output;
                }
            }

            loop {
                let extensions = vec![deno_ext::vm::init()];

                let opts = rustyscript::RuntimeOptions {
                    extensions,
                    timeout: cur_setup.timeout,
                    max_heap_size: Some(cur_setup.heap_size),
                    ..Default::default()
                };

                let mut rust = rustyscript::Runtime::new(opts).unwrap();

                rust.put(TState::new(cur_setup.clone(), cur_weak.clone()))
                    .unwrap();

                if let Err(err) = rust.eval::<()>(&cur_setup.code) {
                    on_drop.not_ready();
                    let _ = cur_output.send(Err(std::io::Error::other(err)));
                    return;
                }

                loop {
                    tracing::trace!(js_request = ?cur_request);

                    let res: Result<JsResponse> = match rust
                        .tokio_runtime()
                        .block_on(async {
                            tokio::time::timeout(
                                cur_setup.timeout,
                                rust.call_function_async(
                                    None,
                                    "vm",
                                    rustyscript::json_args!(cur_request),
                                ),
                            )
                            .await
                        }) {
                        Ok(Ok(r)) => Ok(r),
                        Ok(Err(err @ rustyscript::Error::JsError(_))) => {
                            Err(std::io::Error::other(err))
                        }
                        Ok(Err(err)) => {
                            let err = if matches!(
                                err,
                                rustyscript::Error::Runtime(_)
                                    | rustyscript::Error::HeapExhausted
                            ) {
                                std::io::Error::other(format!(
                                    "MemoryError({err:?})"
                                ))
                            } else {
                                std::io::Error::other(err)
                            };
                            tracing::debug!(
                                ?err,
                                "JS Processing Error, Aborting v8 isolate"
                            );
                            on_drop.not_ready();
                            let _ = cur_output.send(Err(err));
                            return;
                        }
                        Err(_) => {
                            tracing::debug!(
                                "JS Timeout Error, Aborting v8 isolate"
                            );
                            on_drop.not_ready();
                            let _ = cur_output
                                .send(Err(std::io::Error::other("Timeout")));
                            return;
                        }
                    };
                    let _ = cur_output.send(res);

                    match cmd_recv.blocking_recv() {
                        None => return,
                        Some(Cmd::Kill) => return,
                        Some(Cmd::Exec {
                            setup,
                            request,
                            weak,
                            output,
                        }) => {
                            let reset = cur_setup != setup;
                            cur_setup = setup;
                            cur_request = request;
                            cur_weak = weak;
                            cur_output = output;
                            if reset {
                                break;
                            }
                        }
                    };
                }
            }
        });
        Self {
            is_ready,
            _thread_permit: thread_permit,
            _ram_permit: ram_permit,
            thread: Some(thread),
            cmd_send: Some(cmd_send),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[ignore = "Run this test in isolation via `cargo test -- --ignored js_stress`"]
    #[tokio::test(flavor = "multi_thread")]
    async fn js_stress() {
        let rth = RuntimeHandle::default();
        let obj = obj::obj_file::ObjFile::create(None).await.unwrap();
        rth.set_obj(obj);

        fn setup(id: usize, runtime: Runtime) -> JsSetup {
            JsSetup {
                runtime,
                ctx: format!("ctx-{id}").into(),
                env: Arc::new(serde_json::Value::Null),
                code: format!(
                    "
async function vm(req) {{
    if (req.type === 'fnReq') {{
        const body = (new TextEncoder()).encode('{id}')
        return {{ type: 'fnResOk', body }};
    }}
    throw new Error('unhandled');
}}
"
                )
                .into(),
                timeout: JsSetup::DEF_TIMEOUT,
                heap_size: JsSetup::DEF_HEAP_SIZE * 5,
            }
        }

        const COUNT: usize = 64;

        let mut setups = Vec::with_capacity(COUNT);
        for id in 0..COUNT {
            setups.push(setup(id, rth.runtime()));
        }

        let js = JsExecDefault::create();

        let req = JsRequest::FnReq {
            method: "GET".into(),
            path: "".into(),
            body: None,
            headers: Default::default(),
        };

        for r in 1..=10 {
            println!("round {r}/10");
            let mut all = Vec::with_capacity(COUNT);
            for id in 0..COUNT {
                all.push(js.exec(setups[id].clone(), req.clone()));
            }
            let res = futures::future::try_join_all(all).await.unwrap();
            assert_eq!(COUNT, res.len());
            for id in 0..COUNT {
                match &res[id] {
                    JsResponse::FnResOk { body, .. } => {
                        let body = String::from_utf8_lossy(body);
                        assert_eq!(id.to_string(), body);
                    }
                    oth => panic!("unexpected result: {oth:?}"),
                }
            }
        }
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn js_simple() {
        let rth = RuntimeHandle::default();
        let obj = obj::obj_file::ObjFile::create(None).await.unwrap();
        rth.set_obj(obj);

        let setup = JsSetup {
            runtime: rth.runtime(),
            ctx: "bobbo".into(),
            env: Arc::new(serde_json::Value::Null),
            code: "
async function vm(req) {
    if (req.type === 'objCheckReq') {
        return { type: 'objCheckResOk' };
    } else if (req.type === 'fnReq') {
        const b = (new TextEncoder()).encode('hello');
        console.log('encode', b, b instanceof Uint8Array);
        const s = (new TextDecoder()).decode(b);
        console.log('decode', s);

        const { meta } = await VM.objPut({
            meta: 'c/A/test',
            data: new TextEncoder().encode('hello'),
        });
        console.log(`put returned meta: ${meta}`);

        const { data } = await VM.objGet({ meta });
        const res = new TextDecoder().decode(data);
        console.log(`fetched: ${res}`);

        const { metaList } = await VM.objList({
            appPathPrefix: 't',
            createdGt: 0.0,
            limit: 42,
        });
        console.log(`list result: ${JSON.stringify(metaList)}`);
        let count = metaList.length;

        if (count !== 1) {
            throw new Error(`failed to list the item`);
        }

        if (res !== 'hello') {
            throw new Error(`bad response, expected 'hello', got: ${res}`);
        }

        return { type: 'fnResOk' };
    } else {
        throw new Error(`invalid type: ${req.type}`);
    }
}
"
            .into(),
            timeout: JsSetup::DEF_TIMEOUT,
            heap_size: JsSetup::DEF_HEAP_SIZE,
        };

        let req = JsRequest::FnReq {
            method: "GET".into(),
            path: "foo/bar".into(),
            body: None,
            headers: Default::default(),
        };

        let js = JsExecDefault::create();

        let res = js.exec(setup.clone(), req.clone()).await.unwrap();
        println!("got: {res:#?}");
        let res = js.exec(setup, req).await.unwrap();
        println!("got: {res:#?}");

        let prefix = format!("{}/bobbo/", crate::obj::ObjMeta::SYS_CTX);
        let p = rth
            .runtime()
            .obj()
            .unwrap()
            .list(&prefix, 0.0, u32::MAX)
            .await
            .unwrap();
        for meta in p {
            println!("GOT: {meta:?}");
        }
    }
}