redjs 0.4.0

Full-control Rust embedding layer over Bun's JavaScriptCore prebuilt. C ABI designed for zero-alloc C# (NativeAOT) codegen bindings.
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
//! `EventSource` (HTML server-sent events). The stream is parsed on the network thread; the JS
//! thread gets whole events.

use core::ffi::c_void;
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
use std::time::Duration;

use super::util::*;
use super::{blob, cookies, events, http};
use crate::capi;
use crate::glue;

const CONNECTING: u8 = 0;
const OPEN: u8 = 1;
const CLOSED: u8 = 2;

const DEFAULT_RETRY_MS: u64 = 3000;
const CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
const IO_TIMEOUT: Duration = Duration::from_secs(24 * 60 * 60);

#[derive(Debug, PartialEq)]
pub(crate) struct SseEvent {
    pub ty: String,
    pub data: String,
    pub id: String,
}

#[derive(Default)]
pub(crate) struct Parser {
    line: Vec<u8>,
    started: bool,
    after_cr: bool,
    data: String,
    event: String,
    id_buf: String,
    pub last_id: String,
    pub retry: Option<u64>,
}

const BOM: &[u8] = b"\xEF\xBB\xBF";

impl Parser {
    pub fn new(last_id: &str) -> Self {
        Parser { id_buf: last_id.into(), last_id: last_id.into(), ..Default::default() }
    }

    pub fn feed(&mut self, bytes: &[u8], out: &mut Vec<SseEvent>) {
        if !self.started {
            self.line.extend_from_slice(bytes);
            if self.line.len() < BOM.len() && BOM.starts_with(&self.line) {
                return;
            }
            self.started = true;
            let mut pending = std::mem::take(&mut self.line);
            if pending.starts_with(BOM) {
                pending.drain(..BOM.len());
            }
            return self.feed(&pending, out);
        }
        let mut rest = bytes;
        if self.after_cr {
            self.after_cr = false;
            if let Some(stripped) = rest.strip_prefix(b"\n") {
                rest = stripped;
            }
        }
        while let Some(i) = rest.iter().position(|&b| b == b'\r' || b == b'\n') {
            self.line.extend_from_slice(&rest[..i]);
            self.end_line(out);
            let cr = rest[i] == b'\r';
            rest = &rest[i + 1..];
            if cr {
                match rest.first() {
                    Some(b'\n') => rest = &rest[1..],
                    None => self.after_cr = true,
                    _ => {}
                }
            }
        }
        self.line.extend_from_slice(rest);
    }

    fn end_line(&mut self, out: &mut Vec<SseEvent>) {
        let raw = std::mem::take(&mut self.line);
        let line = String::from_utf8_lossy(&raw);
        if line.is_empty() {
            return self.dispatch(out);
        }
        if line.starts_with(':') {
            return;
        }
        let (field, value) = match line.find(':') {
            Some(i) => (&line[..i], line[i + 1..].strip_prefix(' ').unwrap_or(&line[i + 1..])),
            None => (&line[..], ""),
        };
        match field {
            "event" => self.event = value.to_string(),
            "data" => {
                self.data.push_str(value);
                self.data.push('\n');
            }
            "id" if !value.contains('\0') => self.id_buf = value.to_string(),
            "retry" if !value.is_empty() && value.bytes().all(|b| b.is_ascii_digit()) => {
                if let Ok(n) = value.parse() {
                    self.retry = Some(n);
                }
            }
            _ => {}
        }
    }

    fn dispatch(&mut self, out: &mut Vec<SseEvent>) {
        self.last_id.clone_from(&self.id_buf);
        let ty = std::mem::take(&mut self.event);
        if self.data.is_empty() {
            return;
        }
        let mut data = std::mem::take(&mut self.data);
        data.pop();
        let ty = if ty.is_empty() { "message".to_string() } else { ty };
        out.push(SseEvent { ty, data, id: self.last_id.clone() });
    }
}

pub struct EsData {
    id: u64,
    state: u8,
    url: String,
    with_credentials: bool,
    attempt: u64,
    cancel: Option<http::Cancel>,
    origin: String,
    last_id: String,
    retry_ms: u64,
}

struct Classes {
    host: capi::JSClassRef,
    source: Class,
    s_handlers: *mut c_void,
}

unsafe impl Send for Classes {}
unsafe impl Sync for Classes {}

fn registry() -> &'static Mutex<HashMap<usize, &'static Classes>> {
    static R: OnceLock<Mutex<HashMap<usize, &'static Classes>>> = OnceLock::new();
    R.get_or_init(|| Mutex::new(HashMap::new()))
}

fn classes(g: *mut c_void) -> &'static Classes {
    registry().lock().unwrap().get(&(g as usize)).copied().expect("webapi::eventsource not installed")
}

/// `(context key, source id)` → the protected `EventSource`, while it is not CLOSED.
fn live() -> &'static Mutex<HashMap<(usize, u64), u64>> {
    static L: OnceLock<Mutex<HashMap<(usize, u64), u64>>> = OnceLock::new();
    L.get_or_init(|| Mutex::new(HashMap::new()))
}

pub(crate) fn forget(gone: crate::context::Gone) {
    registry().lock().unwrap().remove(&gone.key);
    live().lock().unwrap().retain(|(k, _), _| *k != gone.key);
}

unsafe fn es<'a>(g: *mut c_void, obj: u64) -> Option<&'a mut EsData> {
    host_data::<EsData>(g, classes(g).host, obj)
}

#[derive(Clone, Copy)]
struct Ctx(*mut c_void);
unsafe impl Send for Ctx {}
unsafe impl Sync for Ctx {}

#[derive(Default)]
struct Inbox {
    queued: bool,
    head: Option<Result<String, String>>,
    events: Vec<SseEvent>,
    last_id: Option<String>,
    retry: Option<u64>,
    ended: bool,
}

struct Shared {
    id: u64,
    attempt: u64,
    ctx: Ctx,
    cid: Option<crate::context::ContextId>,
    inbox: Mutex<Inbox>,
}

impl Shared {
    fn post(self: &Arc<Self>, f: impl FnOnce(&mut Inbox)) {
        let queue = {
            let mut i = self.inbox.lock().unwrap();
            f(&mut i);
            !std::mem::replace(&mut i.queued, true)
        };
        if queue {
            let me = Arc::clone(self);
            crate::web::schedule_native_from(self.ctx.0, self.cid, 0.0, Box::new(move |g| unsafe { tick(g, &me) }));
        }
    }
}

struct Obs {
    sh: Arc<Shared>,
    cancel: http::Cancel,
    accepted: AtomicBool,
    parser: Mutex<Parser>,
}

fn verdict(h: &http::ProgressHead<'_>) -> Result<String, String> {
    if h.status != 200 {
        return Err(format!("the server answered {} {}", h.status, h.status_text));
    }
    let ct = http::header(h.headers, "content-type").unwrap_or("");
    if !ct.split(';').next().unwrap_or("").trim().eq_ignore_ascii_case("text/event-stream") {
        return Err(format!("the response's MIME type ({ct:?}) is not \"text/event-stream\""));
    }
    let enc = http::header(h.headers, "content-encoding").unwrap_or("").trim();
    if !enc.is_empty() && !enc.eq_ignore_ascii_case("identity") {
        return Err(format!("the response is {enc}-encoded; an event stream must not be"));
    }
    Ok(url::Url::parse(h.url).map(|u| u.origin().ascii_serialization()).unwrap_or_default())
}

impl http::Observe for Obs {
    fn head(&self, h: &http::ProgressHead<'_>) {
        let v = verdict(h);
        if v.is_ok() {
            self.accepted.store(true, Ordering::SeqCst);
        } else {
            self.cancel.abort();
        }
        self.sh.post(|i| i.head = Some(v));
    }

    fn body(&self, chunk: &[u8], _received: u64, _total: Option<u64>) {
        if !self.accepted.load(Ordering::SeqCst) {
            return;
        }
        let mut evs = Vec::new();
        let (last_id, retry) = {
            let mut p = self.parser.lock().unwrap();
            let before = p.last_id.clone();
            p.feed(chunk, &mut evs);
            ((p.last_id != before).then(|| p.last_id.clone()), p.retry.take())
        };
        if evs.is_empty() && last_id.is_none() && retry.is_none() {
            return;
        }
        self.sh.post(|i| {
            i.events.extend(evs);
            if last_id.is_some() {
                i.last_id = last_id;
            }
            if retry.is_some() {
                i.retry = retry;
            }
        });
    }

    fn streams(&self) -> bool {
        true
    }
}

unsafe fn fire(g: *mut c_void, obj: u64, ty: &str) {
    events::dispatch(g, obj, events::new_trusted_event(g, ty));
}

unsafe fn current(g: *mut c_void, id: u64, attempt: u64) -> Option<u64> {
    let obj = live().lock().unwrap().get(&(g as usize, id)).copied()?;
    es(g, obj).is_some_and(|d| d.attempt == attempt && d.state != CLOSED).then_some(obj)
}

unsafe fn close_down(g: *mut c_void, obj: u64) {
    let Some(d) = es(g, obj) else { return };
    d.state = CLOSED;
    d.attempt += 1;
    if let Some(c) = d.cancel.take() {
        c.abort();
    }
    if live().lock().unwrap().remove(&(g as usize, d.id)).is_some() {
        crate::ffi::redjs_unprotect(g, obj);
    }
}

unsafe fn fail(g: *mut c_void, obj: u64, why: &str) {
    if let Some(d) = es(g, obj) {
        eprintln!("[redjs] EventSource {}: {why}; the connection is closed", d.url);
    }
    close_down(g, obj);
    fire(g, obj, "error");
}

unsafe fn reestablish(g: *mut c_void, obj: u64) {
    let Some(d) = es(g, obj) else { return };
    if d.state == CLOSED {
        return;
    }
    d.state = CONNECTING;
    d.attempt += 1;
    d.cancel = None;
    let (id, attempt, delay) = (d.id, d.attempt, d.retry_ms);
    fire(g, obj, "error");
    crate::web::schedule_native(
        g,
        delay as f64,
        Box::new(move |g| unsafe {
            if let Some(obj) = current(g, id, attempt) {
                connect(g, obj);
            }
        }),
    );
}

unsafe fn tick(g: *mut c_void, sh: &Arc<Shared>) {
    let (head, evs, last_id, retry, ended) = {
        let mut i = sh.inbox.lock().unwrap();
        i.queued = false;
        (i.head.take(), std::mem::take(&mut i.events), i.last_id.take(), i.retry.take(), std::mem::take(&mut i.ended))
    };
    let Some(obj) = current(g, sh.id, sh.attempt) else { return };
    if let Some(d) = es(g, obj) {
        if let Some(id) = last_id {
            d.last_id = id;
        }
        if let Some(ms) = retry {
            d.retry_ms = ms;
        }
    }
    match head {
        Some(Ok(origin)) => {
            if let Some(d) = es(g, obj) {
                d.state = OPEN;
                d.origin = origin;
            }
            fire(g, obj, "open");
        }
        Some(Err(why)) => return fail(g, obj, &why),
        None => {}
    }
    for ev in evs {
        let Some(obj) = current(g, sh.id, sh.attempt) else { return };
        let Some(d) = es(g, obj) else { return };
        let origin = d.origin.clone();
        let msg = super::websocket::new_message_event_with_id(g, &ev.ty, js_string(g, &ev.data), &origin, &ev.id);
        events::dispatch(g, obj, msg);
    }
    if ended {
        if let Some(obj) = current(g, sh.id, sh.attempt) {
            reestablish(g, obj);
        }
    }
}

unsafe fn connect(g: *mut c_void, obj: u64) {
    let Some(d) = es(g, obj) else { return };
    let cancel = http::Cancel::default();
    d.cancel = Some(cancel.clone());
    let sh = Arc::new(Shared {
        id: d.id,
        attempt: d.attempt,
        ctx: Ctx(g),
        cid: crate::context::id_of(g),
        inbox: Mutex::new(Inbox::default()),
    });
    let mut headers = vec![
        ("Accept".to_string(), "text/event-stream".to_string()),
        ("Accept-Encoding".to_string(), "identity".to_string()),
        ("Cache-Control".to_string(), "no-cache".to_string()),
    ];
    if !d.last_id.is_empty() {
        headers.push(("Last-Event-ID".to_string(), d.last_id.clone()));
    }
    let obs = Obs { sh: Arc::clone(&sh), cancel: cancel.clone(), accepted: AtomicBool::new(false), parser: Mutex::new(Parser::new(&d.last_id)) };
    let req = http::Request {
        method: "GET".into(),
        url: d.url.clone(),
        headers,
        body: None,
        redirect: http::Redirect::Follow,
        credentials: if d.with_credentials { cookies::Credentials::Include } else { cookies::Credentials::SameOrigin },
        connect_timeout: CONNECT_TIMEOUT,
        io_timeout: IO_TIMEOUT,
        observer: Some(http::Observer(Arc::new(obs))),
        cache: http::cache::CacheMode::NoStore,
    };
    let worker = Arc::clone(&sh);
    if http::start(req, cancel, Box::new(move |_| worker.post(|i| i.ended = true))).is_err() {
        sh.post(|i| i.ended = true);
    }
}

#[crate::host_fn]
unsafe fn construct(g: *mut c_void, cf: *mut c_void) -> u64 {
    if cf_argc(cf) < 1 {
        return throw_type_error(g, "Failed to construct 'EventSource': 1 argument required, but only 0 present.");
    }
    let Ok(s) = to_js_string(g, cf_arg(cf, 0)) else { return UNDEF };
    let raw = read_string(g, s).unwrap_or_default();
    let init = cf_arg(cf, 1);
    let with_credentials = if blob::is_object(g, init) {
        match try_get(g, init, "withCredentials") {
            Ok(v) => v != UNDEF && glue::rjs_to_boolean(g, v) != 0,
            Err(()) => return UNDEF,
        }
    } else {
        false
    };
    let base = super::xhr::base_url(g);
    let Ok(url) = url::Url::options().base_url(base.as_ref()).parse(&raw) else {
        return super::domexception::throw(g, "SyntaxError", &format!("Failed to construct 'EventSource': The URL '{raw}' is invalid."));
    };
    let c = classes(g);
    let nt = cf_new_target(cf);
    let mut proto = if is_cell(nt) { get(g, nt, "prototype") } else { UNDEF };
    if !is_cell(proto) {
        proto = c.source.proto;
    }
    static NEXT_ID: AtomicU64 = AtomicU64::new(1);
    let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
    let network = matches!(url.scheme(), "http" | "https");
    let obj = host_create(
        g,
        c.host,
        proto,
        EsData {
            id,
            state: CONNECTING,
            url: url.to_string(),
            with_credentials,
            attempt: 1,
            cancel: None,
            origin: String::new(),
            last_id: String::new(),
            retry_ms: DEFAULT_RETRY_MS,
        },
    );
    live().lock().unwrap().insert((g as usize, id), obj);
    crate::ffi::redjs_protect(g, obj);
    if network {
        connect(g, obj);
    } else {
        let scheme = url.scheme().to_string();
        crate::web::schedule_native(
            g,
            0.0,
            Box::new(move |g| unsafe {
                if let Some(obj) = current(g, id, 1) {
                    fail(g, obj, &format!("the {scheme}: scheme is not supported"));
                }
            }),
        );
    }
    obj
}

unsafe fn this_es<'a>(g: *mut c_void, cf: *mut c_void, what: &str) -> Result<&'a mut EsData, u64> {
    es(g, cf_this(cf)).ok_or_else(|| throw_type_error(g, &format!("Illegal invocation: EventSource.{what} on a non-EventSource")))
}

#[crate::host_fn]
unsafe fn close(g: *mut c_void, cf: *mut c_void) -> u64 {
    if let Err(e) = this_es(g, cf, "close") {
        return e;
    }
    close_down(g, cf_this(cf));
    UNDEF
}

#[crate::host_fn]
unsafe fn get_url(g: *mut c_void, cf: *mut c_void) -> u64 {
    match this_es(g, cf, "url") {
        Ok(d) => js_string(g, &d.url),
        Err(e) => e,
    }
}

#[crate::host_fn]
unsafe fn get_with_credentials(g: *mut c_void, cf: *mut c_void) -> u64 {
    match this_es(g, cf, "withCredentials") {
        Ok(d) => enc_bool(d.with_credentials),
        Err(e) => e,
    }
}

#[crate::host_fn]
unsafe fn get_ready_state(g: *mut c_void, cf: *mut c_void) -> u64 {
    match this_es(g, cf, "readyState") {
        Ok(d) => enc_i32(d.state as i32),
        Err(e) => e,
    }
}

unsafe fn handler_bag(g: *mut c_void, this: u64, create: bool) -> u64 {
    let slot = classes(g).s_handlers;
    let bag = get_a(g, this, slot);
    if is_cell(bag) || !create || !is_cell(this) {
        return bag;
    }
    let fresh = new_object(g, UNDEF);
    glue::rjs_define(g, this, slot, fresh, 0);
    fresh
}

macro_rules! handler_accessor {
    ($get_fn:ident, $set_fn:ident, $name:literal) => {
        #[crate::host_fn]
        unsafe fn $get_fn(g: *mut c_void, cf: *mut c_void) -> u64 {
            let bag = handler_bag(g, cf_this(cf), false);
            let v = if is_cell(bag) { get(g, bag, $name) } else { NULL };
            if blob::is_object(g, v) {
                v
            } else {
                NULL
            }
        }
        #[crate::host_fn]
        unsafe fn $set_fn(g: *mut c_void, cf: *mut c_void) -> u64 {
            let bag = handler_bag(g, cf_this(cf), true);
            if is_cell(bag) {
                let v = cf_arg(cf, 0);
                define(g, bag, $name, if blob::is_object(g, v) { v } else { NULL }, 0);
            }
            UNDEF
        }
    };
}

handler_accessor!(get_onopen, set_onopen, "onopen");
handler_accessor!(get_onmessage, set_onmessage, "onmessage");
handler_accessor!(get_onerror, set_onerror, "onerror");

pub unsafe fn install(g: *mut c_void, event_target_proto: u64) {
    let host = make_host_class::<EsData>("EventSource");
    let source = define_class(g, &ClassSpec {
        name: "EventSource",
        arity: 1,
        construct: Some(construct),
        parent_proto: event_target_proto,
        methods: &[("close", 0, close)],
        getters: &[("url", get_url), ("withCredentials", get_with_credentials), ("readyState", get_ready_state)],
        accessors: &[
            ("onopen", get_onopen, set_onopen),
            ("onmessage", get_onmessage, set_onmessage),
            ("onerror", get_onerror, set_onerror),
        ],
    });
    for (name, v) in [("CONNECTING", CONNECTING), ("OPEN", OPEN), ("CLOSED", CLOSED)] {
        define(g, source.ctor, name, enc_i32(v as i32), 1 | 2 | 4);
        define(g, source.proto, name, enc_i32(v as i32), 1 | 2 | 4);
    }
    let c = Classes { host, source, s_handlers: atom_priv(g, "redjs#esHandlers") };
    registry().lock().unwrap().insert(g as usize, Box::leak(Box::new(c)));
}

#[cfg(test)]
mod tests;