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
//! A server manages multiple contexts.

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

fn p_no(s: &Arc<str>) -> bool {
    s.is_empty()
}

fn timeout_secs() -> f64 {
    10.0
}

fn max_heap_bytes() -> usize {
    1024 * 1024 * 32
}

fn is_false(b: &bool) -> bool {
    !b
}

/// System setup information.
#[derive(Default, Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SysSetup {
    /// System admin tokens.
    #[serde(rename = "x", default, skip_serializing_if = "Vec::is_empty")]
    pub sys_admin: Vec<Arc<str>>,
}

/// Context setup information.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CtxSetup {
    /// The context identifier.
    #[serde(rename = "c", default, skip_serializing_if = "p_no")]
    pub ctx: Arc<str>,

    /// If this boolean is true, other properties will be ignored,
    /// and the context will be deleted.
    #[serde(rename = "d", default, skip_serializing_if = "is_false")]
    pub delete: bool,

    /// Context admin tokens.
    #[serde(rename = "x", default, skip_serializing_if = "Vec::is_empty")]
    pub ctx_admin: Vec<Arc<str>>,

    /// Timeout for function invocations.
    #[serde(rename = "t", default = "timeout_secs")]
    pub timeout_secs: f64,

    /// Max memory allowed for function invocations.
    #[serde(rename = "h", default = "max_heap_bytes")]
    pub max_heap_bytes: usize,
}

impl Default for CtxSetup {
    fn default() -> Self {
        Self {
            ctx: Default::default(),
            delete: false,
            ctx_admin: Default::default(),
            timeout_secs: timeout_secs(),
            max_heap_bytes: max_heap_bytes(),
        }
    }
}

impl CtxSetup {
    fn check(&self) -> Result<()> {
        safe_str(&self.ctx)?;
        for token in self.ctx_admin.iter() {
            safe_str(token)?;
        }
        if self.max_heap_bytes < 1024 * 1024
            || self.max_heap_bytes / (1024 * 1024) > u32::MAX as usize
        {
            return Err(Error::other("invalid max heap bytes"));
        }
        Ok(())
    }
}

/// Context config information.
#[derive(Default, Clone, serde::Serialize, serde::Deserialize)]
pub struct CtxConfig {
    /// The context identifier.
    #[serde(rename = "c", default, skip_serializing_if = "p_no")]
    pub ctx: Arc<str>,

    /// Context admin tokens.
    #[serde(rename = "x", default, skip_serializing_if = "Vec::is_empty")]
    pub ctx_admin: Vec<Arc<str>>,

    /// Javascript code for the context.
    #[serde(rename = "l", default, skip_serializing_if = "p_no")]
    pub code: Arc<str>,

    /// Javascript code env metadata for the context.
    #[serde(
        rename = "e",
        default,
        skip_serializing_if = "serde_json::Value::is_null"
    )]
    pub code_env: Arc<serde_json::Value>,
}

impl std::fmt::Debug for CtxConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CtxConfig")
            .field("ctx", &self.ctx)
            .field("ctx_admin", &self.ctx_admin)
            .field("code_bytes", &self.code.len())
            .field("code_env", &self.code_env)
            .finish()
    }
}

impl CtxConfig {
    fn check(&self) -> Result<()> {
        safe_str(&self.ctx)?;
        for token in self.ctx_admin.iter() {
            safe_str(token)?;
        }
        Ok(())
    }
}

/// A server manages multiple contexts.
pub struct Server {
    runtime: RuntimeHandle,
    sys_setup: Mutex<SysSetup>,
    ctx_setup: Mutex<HashMap<Arc<str>, (CtxSetup, CtxConfig)>>,
    ctx_map: Mutex<HashMap<Arc<str>, Arc<crate::ctx::Ctx>>>,
}

impl Server {
    /// Construct a new server.
    pub async fn new(runtime: RuntimeHandle) -> Result<Self> {
        let sys_setup = runtime.runtime().obj()?.get_sys_setup().await?;

        let ctx_setup = runtime.runtime().obj()?.list_ctx_all().await?;

        let this = Self {
            runtime,
            sys_setup: Mutex::new(sys_setup),
            ctx_setup: Mutex::new(ctx_setup.clone()),
            ctx_map: Mutex::new(HashMap::new()),
        };

        for (ctx, (setup, config)) in ctx_setup {
            this.setup_context(ctx, setup, config).await?;
        }

        Ok(this)
    }

    async fn setup_context(
        &self,
        ctx: Arc<str>,
        setup: CtxSetup,
        config: CtxConfig,
    ) -> Result<()> {
        let sub = crate::ctx::Ctx::new(
            ctx.clone(),
            setup,
            config,
            self.runtime.runtime(),
        )
        .await?;
        self.ctx_map.lock().unwrap().insert(ctx, sub);
        Ok(())
    }

    fn get_sys_setup(&self) -> SysSetup {
        self.sys_setup.lock().unwrap().clone()
    }

    fn get_ctx_setup(&self, ctx: &str) -> Result<(CtxSetup, CtxConfig)> {
        self.ctx_setup
            .lock()
            .unwrap()
            .get(ctx)
            .cloned()
            .ok_or_else(|| Error::not_found(format!("no context: {ctx}")))
    }

    fn check_sysadmin(&self, token: &Arc<str>) -> Result<()> {
        if !self.get_sys_setup().sys_admin.contains(token) {
            return Err(Error::unauthorized(
                "action requires sysadmin permissions",
            ));
        }
        Ok(())
    }

    fn check_ctxadmin(
        &self,
        token: &Arc<str>,
        ctx: &Arc<str>,
    ) -> Result<(CtxSetup, CtxConfig)> {
        let (cur_setup, cur_config) = self.get_ctx_setup(ctx)?;

        if !self.get_sys_setup().sys_admin.contains(token) {
            // If we are not a sys admin, we must be a ctx admin
            if !cur_setup.ctx_admin.contains(token)
                && !cur_config.ctx_admin.contains(token)
            {
                return Err(Error::unauthorized(
                    "action requires ctxadmin permissions",
                ));
            }
        }

        Ok((cur_setup, cur_config))
    }

    /// Set sysadmin tokens.
    pub async fn set_sys_admin(&self, sys_admin: Vec<Arc<str>>) -> Result<()> {
        for token in sys_admin.iter() {
            safe_str(token)?;
        }
        let mut sys_setup = self.get_sys_setup();
        sys_setup.sys_admin = sys_admin;
        self.runtime
            .runtime()
            .obj()?
            .set_sys_setup(sys_setup.clone())
            .await?;
        *self.sys_setup.lock().unwrap() = sys_setup;
        Ok(())
    }

    /// A general health check that is not context-specific.
    pub async fn health_get(&self) -> Result<()> {
        tracing::trace!(request = "health_get");
        Ok(())
    }

    /// Setup a context.
    pub async fn ctx_setup_put(
        &self,
        token: Arc<str>,
        setup: CtxSetup,
    ) -> Result<()> {
        self.check_sysadmin(&token)?;

        setup.check()?;

        self.runtime
            .runtime()
            .obj()?
            .set_ctx_setup(setup.clone())
            .await?;

        let (ctx, (ctx_setup, ctx_config)) = {
            let ctx = setup.ctx.clone();
            let mut lock = self.ctx_setup.lock().unwrap();
            let r = lock.entry(ctx.clone()).or_default();
            r.0 = setup;
            (ctx, r.clone())
        };

        tracing::trace!(request = "ctx_setup", ?ctx_setup, ?ctx_config);

        self.setup_context(ctx, ctx_setup, ctx_config).await?;

        Ok(())
    }

    /// Configure a context.
    pub async fn ctx_config_put(
        &self,
        token: Arc<str>,
        config: CtxConfig,
    ) -> Result<()> {
        self.check_ctxadmin(&token, &config.ctx)?;

        config.check()?;

        self.runtime
            .runtime()
            .obj()?
            .set_ctx_config(config.clone())
            .await?;

        let (ctx, (ctx_setup, ctx_config)) = {
            let ctx = config.ctx.clone();
            let mut lock = self.ctx_setup.lock().unwrap();
            let r = lock.entry(ctx.clone()).or_default();
            r.1 = config;
            (ctx, r.clone())
        };

        tracing::trace!(request = "ctx_config", ?ctx_setup, ?ctx_config);

        self.setup_context(ctx, ctx_setup, ctx_config).await?;

        Ok(())
    }

    /// Handle a msg listen request.
    pub async fn msg_listen(
        &self,
        ctx: Arc<str>,
        msg_id: Arc<str>,
    ) -> Option<crate::msg::DynMsgRecv> {
        tracing::trace!(request = "msg_listen", ?ctx, ?msg_id);

        self.runtime
            .runtime()
            .msg()
            .ok()?
            .get_recv(ctx, msg_id)
            .await
    }

    /// Generate a full backup file on the local system.
    pub async fn obj_backup_full(&self, token: Arc<str>) -> Result<()> {
        self.check_sysadmin(&token)?;

        let mut zip = tokio::task::spawn_blocking(|| {
            let zip = std::fs::OpenOptions::new()
                .write(true)
                .create_new(true)
                .open("backup.zip")?;
            std::io::Result::Ok(zip::ZipWriter::new(zip))
        })
        .await??;

        let mut created_gt = 0.0;
        let mut file_no = 1;

        loop {
            let meta_list = self
                .runtime
                .runtime()
                .obj()?
                .list("", created_gt, 200)
                .await?;

            if meta_list.is_empty() {
                return Ok(());
            }

            for meta in meta_list {
                created_gt = meta.created_secs();

                let (meta, data) =
                    self.runtime.runtime().obj()?.get(meta).await?;

                let meta2 = meta.clone();
                zip = tokio::task::spawn_blocking(move || {
                    use std::io::Write;
                    let enc = rmp_serde::to_vec(&(meta2, data))
                        .map_err(std::io::Error::other)?;
                    zip.start_file(
                        file_no.to_string(),
                        zip::write::SimpleFileOptions::default(),
                    )?;
                    zip.write_all(&enc)?;
                    std::io::Result::Ok(zip)
                })
                .await??;

                file_no += 1;

                tracing::info!(%meta, "backup file");
            }
        }
    }

    /// Restore a full backup file from the local system.
    pub async fn obj_restore_full(&self, token: Arc<str>) -> Result<()> {
        self.check_sysadmin(&token)?;

        let (mut zip, count) = tokio::task::spawn_blocking(|| {
            let zip =
                std::fs::OpenOptions::new().read(true).open("backup.zip")?;
            let zip = zip::ZipArchive::new(zip)?;
            let count = zip.len();
            std::io::Result::Ok((zip, count))
        })
        .await??;

        for idx in 0..count {
            let (tmp, meta, data) = tokio::task::spawn_blocking(move || {
                let mut out = Vec::new();
                {
                    let mut read = zip.by_index(idx)?;
                    use std::io::Read;
                    read.read_to_end(&mut out)?;
                }
                let (meta, data): (crate::obj::ObjMeta, bytes::Bytes) =
                    rmp_serde::from_slice(&out)
                        .map_err(std::io::Error::other)?;
                std::io::Result::Ok((zip, meta, data))
            })
            .await??;
            zip = tmp;

            self.runtime
                .runtime()
                .obj()?
                .put(meta.clone(), data)
                .await?;

            tracing::info!(%meta, "restore file");
        }

        Ok(())
    }

    /// List metadata from the object store.
    pub async fn obj_list(
        &self,
        token: Arc<str>,
        ctx: Arc<str>,
        prefix: Arc<str>,
        created_gt: f64,
        limit: u32,
    ) -> Result<Vec<crate::obj::ObjMeta>> {
        self.check_ctxadmin(&token, &ctx)?;

        let prefix =
            format!("{}/{}/{prefix}", crate::obj::ObjMeta::SYS_CTX, ctx);

        tracing::trace!(
            request = "obj_list",
            ?ctx,
            ?prefix,
            ?created_gt,
            ?limit
        );

        let res = self
            .runtime
            .runtime()
            .obj()?
            .list(&prefix, created_gt, limit)
            .await;

        if let Ok(meta_list) = &res {
            let sum: usize = meta_list.iter().map(|m| m.len()).sum();

            crate::meter::meter_egress_byte(&ctx, sum as u128);
        }

        res
    }

    /// Get an item from the object store.
    pub async fn obj_get(
        &self,
        token: Arc<str>,
        ctx: Arc<str>,
        app_path: String,
    ) -> Result<(crate::obj::ObjMeta, bytes::Bytes)> {
        self.check_ctxadmin(&token, &ctx)?;

        let meta =
            crate::obj::ObjMeta::new_context(&ctx, &app_path, 0.0, 0.0, 0.0);

        tracing::trace!(request = "obj_get", ?ctx, ?meta);

        let res = self.runtime.runtime().obj()?.get(meta).await;

        if let Ok((meta, data)) = &res {
            crate::meter::meter_egress_byte(
                &ctx,
                (meta.len() + data.len()) as u128,
            );
        }

        res
    }

    /// Put an item into the object store.
    pub async fn obj_put(
        &self,
        token: Arc<str>,
        meta: crate::obj::ObjMeta,
        data: bytes::Bytes,
    ) -> Result<crate::obj::ObjMeta> {
        let ctx: Arc<str> = meta.ctx().into();
        self.check_ctxadmin(&token, &ctx)?;

        let cs = meta.created_secs();
        let cs = if cs < 1.0 {
            safe_now().to_string()
        } else {
            meta.0.split('/').nth(3).unwrap_or("").to_string()
        };

        let meta = crate::obj::ObjMeta(
            format!(
                "c/{ctx}/{}/{cs}/{}/{}",
                meta.app_path(),
                meta.expires_secs(),
                data.len(),
            )
            .into(),
        );

        tracing::trace!(request = "obj_put", ?ctx, ?meta);

        let c = match self.ctx_map.lock().unwrap().get(&ctx) {
            None => {
                return Err(Error::not_found(format!(
                    "invalid context: {ctx}"
                )));
            }
            Some(c) => c.clone(),
        };
        c.obj_check_req(meta.clone(), data.clone()).await?;

        self.runtime
            .runtime()
            .obj()?
            .put(meta.clone(), data)
            .await?;

        Ok(meta)
    }

    /// Process a function request.
    pub async fn fn_req(
        &self,
        ctx: Arc<str>,
        req: crate::js::JsRequest,
    ) -> Result<crate::js::JsResponse> {
        let req_id = rid();

        tracing::trace!(request = "fn_req", %req_id, ?ctx, ?req);

        let c = match self.ctx_map.lock().unwrap().get(&ctx) {
            None => {
                tracing::trace!(request = "fn_req", ?ctx, "invalid context");
                return Err(Error::not_found(format!(
                    "invalid context: {ctx}"
                )));
            }
            Some(c) => c.clone(),
        };

        let res = c.fn_req(req).await;

        tracing::trace!(request = "fn_req", %req_id, ?ctx, ?res);

        use crate::js::JsResponse::FnResOk;
        if let Ok(FnResOk { body, headers, .. }) = &res {
            let mut egress_gib = body.len();
            for (k, v) in headers {
                egress_gib += k.len();
                egress_gib += v.len();
            }

            crate::meter::meter_egress_byte(&ctx, egress_gib as u128);
        }

        res
    }
}

fn rid() -> u64 {
    static I: std::sync::atomic::AtomicU64 =
        std::sync::atomic::AtomicU64::new(1);
    I.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}