rakers 0.1.0

Lightweight headless JS renderer — executes JavaScript and returns the rendered HTML
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
//! JS engine abstraction used by the rendering pipeline.
//!
//! Exposes a single type [`JsRuntime`] backed by whichever engine feature is
//! enabled at compile time (`rquickjs` or `boa`).  Both backends share the same
//! public interface: create a runtime, execute a list of scripts, then read back
//! the accumulated `document.write` output, `document.body.innerHTML`, and
//! `console` messages.

#[cfg(all(feature = "boa", feature = "rquickjs"))]
compile_error!("Enable only one JS engine at a time: 'boa' or 'rquickjs'");

#[cfg(not(any(feature = "boa", feature = "rquickjs")))]
compile_error!("Enable exactly one JS engine feature: 'boa' or 'rquickjs'");

// The JS bootstrap is embedded at compile time; `__HREF__` is substituted at runtime.
const BOOTSTRAP_TEMPLATE: &str = include_str!("bootstrap.js");

// Flush one batch of _r_timers; returns the number of timers remaining after the flush.
// Called in a Rust loop so execute_pending_job() can drain Promise microtasks between passes.
const TIMER_FLUSH_JS: &str = r"
(function() {
    if (_r_timers.length === 0) return 0;
    var batch = _r_timers.splice(0, _r_timers.length);
    for (var i = 0; i < batch.length; i++) {
        try { batch[i](); } catch(e) {
            if (typeof console !== 'undefined') console.error('[rakers timer error]', e && (e.message || String(e)));
        }
    }
    return _r_timers.length;
})()
";

// Read the rendered DOM state after all timers and microtasks have been flushed.
const READBACK_JS: &str = r"
(function() {
    var body = document.body && document.body.innerHTML;
    if (body) return body;
    // If scripts wrote into registry elements but never appended them to body,
    // collect any that have content.
    var parts = [];
    var keys  = Object.keys(_r_reg);
    for (var i = 0; i < keys.length; i++) {
        var el = _r_reg[keys[i]];
        if (el && el.innerHTML) parts.push(_r_serialize(el));
    }
    return parts.join('');
})()
";

/// Produce the browser-globals bootstrap by substituting the page URL into the template.
fn make_bootstrap(page_url: Option<&str>) -> String {
    let href = page_url.unwrap_or("about:blank");
    let escaped = href.replace('\\', "\\\\").replace('"', "\\\"");
    BOOTSTRAP_TEMPLATE.replace("__HREF__", &escaped)
}

// ── boa engine ────────────────────────────────────────────────────────────────

#[cfg(feature = "boa")]
mod boa_rt {
    use std::cell::RefCell;

    use anyhow::anyhow;
    use boa_engine::{
        Context, JsResult, JsValue, NativeFunction, Source, js_string, object::ObjectInitializer,
        property::Attribute,
    };

    thread_local! {
        static WRITTEN:         RefCell<String>      = const { RefCell::new(String::new()) };
        static LOGGED:          RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
        static BODY_INNER_HTML: RefCell<String>      = const { RefCell::new(String::new()) };
    }

    /// A sandboxed JavaScript execution context.
    pub struct JsRuntime;

    impl JsRuntime {
        /// Create a new runtime with a custom per-script timeout.
        ///
        /// Boa has no interrupt-handler API, so the timeout is accepted but not enforced.
        pub fn with_timeout(_timeout: std::time::Duration) -> Self {
            Self::new()
        }

        /// Create a new runtime with no per-script timeout.
        pub fn without_timeout() -> Self {
            Self::new()
        }

        fn new() -> Self {
            WRITTEN.with(|w| w.borrow_mut().clear());
            LOGGED.with(|l| l.borrow_mut().clear());
            BODY_INNER_HTML.with(|b| b.borrow_mut().clear());
            JsRuntime
        }

        /// Evaluate the browser bootstrap and then each script in `scripts` in order.
        ///
        /// Errors from individual scripts are printed to stderr and skipped; the method
        /// only returns `Err` if the bootstrap itself fails to evaluate.
        pub fn execute(
            &self,
            scripts: &[String],
            page_url: Option<&str>,
            _cfg: &crate::HttpConfig,
        ) -> anyhow::Result<()> {
            let mut ctx = Context::default();
            ctx.runtime_limits_mut().set_stack_size_limit(65536);
            ctx.runtime_limits_mut().set_recursion_limit(65536);
            setup_document(&mut ctx)?;
            setup_console(&mut ctx)?;

            let bootstrap = super::make_bootstrap(page_url);
            ctx.eval(Source::from_bytes(bootstrap.as_bytes()))
                .map_err(|e| anyhow!("bootstrap error: {:?}", e))?;

            for script in scripts {
                if let Err(e) = ctx.eval(Source::from_bytes(script.as_bytes())) {
                    eprintln!("[js error] {:?}", e);
                }
            }

            // Flush timers in passes (boa has no separate microtask drain API).
            for _ in 0..64 {
                let remaining: i32 = ctx
                    .eval(Source::from_bytes(super::TIMER_FLUSH_JS.as_bytes()))
                    .ok()
                    .and_then(|v| v.to_number(&mut ctx).ok())
                    .map(|n| n as i32)
                    .unwrap_or(0);
                if remaining == 0 {
                    break;
                }
            }

            let body_result = ctx.eval(Source::from_bytes(super::READBACK_JS.as_bytes()));
            let body_html = body_result
                .ok()
                .and_then(|v| v.to_string(&mut ctx).ok())
                .map(|s| s.to_std_string_escaped())
                .unwrap_or_default();

            let body_html = match body_html.as_str() {
                "undefined" | "null" | "" => String::new(),
                s => s.to_owned(),
            };
            BODY_INNER_HTML.with(|b| *b.borrow_mut() = body_html);

            Ok(())
        }

        /// Return the accumulated output of all `document.write` / `document.writeln` calls.
        pub fn written_html() -> String {
            WRITTEN.with(|w| w.borrow().clone())
        }

        /// Return the final value of `document.body.innerHTML` (or registry element content).
        pub fn body_inner_html() -> String {
            BODY_INNER_HTML.with(|b| b.borrow().clone())
        }

        /// Return all messages logged via `console.log`, `console.warn`, or `console.error`.
        pub fn logged_messages() -> Vec<String> {
            LOGGED.with(|l| l.borrow().clone())
        }
    }

    /// Register `document.write` and `document.writeln`.
    fn setup_document(ctx: &mut Context) -> anyhow::Result<()> {
        let mut init = ObjectInitializer::new(ctx);
        init.function(
            NativeFunction::from_fn_ptr(doc_write),
            js_string!("write"),
            1,
        );
        init.function(
            NativeFunction::from_fn_ptr(doc_writeln),
            js_string!("writeln"),
            1,
        );
        let obj = init.build();
        ctx.register_global_property(js_string!("document"), obj, Attribute::all())
            .map_err(|e| anyhow!("{:?}", e))?;
        Ok(())
    }

    /// Register `console.log`, `console.warn`, and `console.error`.
    fn setup_console(ctx: &mut Context) -> anyhow::Result<()> {
        let mut init = ObjectInitializer::new(ctx);
        init.function(
            NativeFunction::from_fn_ptr(console_log),
            js_string!("log"),
            0,
        );
        init.function(
            NativeFunction::from_fn_ptr(console_log),
            js_string!("warn"),
            0,
        );
        init.function(
            NativeFunction::from_fn_ptr(console_log),
            js_string!("error"),
            0,
        );
        let obj = init.build();
        ctx.register_global_property(js_string!("console"), obj, Attribute::all())
            .map_err(|e| anyhow!("{:?}", e))?;
        Ok(())
    }

    fn doc_write(_this: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
        let s = js_first_arg_to_string(args, ctx)?;
        WRITTEN.with(|w| w.borrow_mut().push_str(&s));
        Ok(JsValue::undefined())
    }

    fn doc_writeln(_this: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
        let s = js_first_arg_to_string(args, ctx)?;
        WRITTEN.with(|w| {
            let mut w = w.borrow_mut();
            w.push_str(&s);
            w.push('\n');
        });
        Ok(JsValue::undefined())
    }

    fn console_log(_this: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
        let parts: Vec<String> = args
            .iter()
            .map(|a| a.to_string(ctx).map(|s| s.to_std_string_escaped()))
            .collect::<Result<_, _>>()?;
        LOGGED.with(|l| l.borrow_mut().push(parts.join(" ")));
        Ok(JsValue::undefined())
    }

    fn js_first_arg_to_string(args: &[JsValue], ctx: &mut Context) -> JsResult<String> {
        args.first()
            .map(|v| v.to_string(ctx).map(|s| s.to_std_string_escaped()))
            .transpose()
            .map(|o| o.unwrap_or_default())
    }
}

#[cfg(feature = "boa")]
pub use boa_rt::JsRuntime;

// ── QuickJS engine ────────────────────────────────────────────────────────────

#[cfg(feature = "rquickjs")]
mod quickjs_rt {
    use std::cell::RefCell;
    use std::time::{Duration, Instant};

    use anyhow::anyhow;
    use rquickjs::{
        Context, Ctx, Function, Module, Object, Runtime, Value,
        context::EvalOptions,
        loader::{Loader, Resolver},
    };

    struct StubModuleSystem;

    impl Resolver for StubModuleSystem {
        fn resolve(&mut self, _ctx: &Ctx<'_>, _base: &str, name: &str) -> rquickjs::Result<String> {
            Ok(name.to_string())
        }
    }

    impl Loader for StubModuleSystem {
        fn load<'js>(&mut self, ctx: &Ctx<'js>, name: &str) -> rquickjs::Result<Module<'js>> {
            Module::declare(ctx.clone(), name, "export default {};")
        }
    }

    thread_local! {
        static WRITTEN:         RefCell<String>                        = const { RefCell::new(String::new()) };
        static LOGGED:          RefCell<Vec<String>>                   = const { RefCell::new(Vec::new()) };
        static BODY_INNER_HTML: RefCell<String>                        = const { RefCell::new(String::new()) };
        // Deadline for the currently-executing script; None means no limit active.
        static SCRIPT_DEADLINE: RefCell<Option<Instant>>               = const { RefCell::new(None) };
        // HttpConfig fields stored so the _r_fetch_sync native function can use them.
        static XHR_UA:          RefCell<Option<String>>                = const { RefCell::new(None) };
        static XHR_HEADERS:     RefCell<Vec<(String, String)>>         = const { RefCell::new(Vec::new()) };
        static XHR_PROXY:       RefCell<Option<String>>                = const { RefCell::new(None) };
        // Per-request timeout applied to XHR fetches; mirrors the script timeout.
        static XHR_TIMEOUT:          RefCell<Option<Duration>>         = const { RefCell::new(None) };
        // Whether to forward custom -H headers on XHR requests (off by default).
        static XHR_FORWARD_HEADERS:  std::cell::Cell<bool>             = const { std::cell::Cell::new(false) };
    }

    fn set_deadline(timeout: Duration) {
        SCRIPT_DEADLINE.with(|d| *d.borrow_mut() = Some(Instant::now() + timeout));
    }

    fn clear_deadline() {
        SCRIPT_DEADLINE.with(|d| *d.borrow_mut() = None);
    }

    /// A sandboxed JavaScript execution context.
    pub struct JsRuntime {
        timeout: Option<Duration>,
    }

    impl JsRuntime {
        /// Create a new runtime with a custom per-script timeout (useful in tests).
        pub fn with_timeout(timeout: Duration) -> Self {
            WRITTEN.with(|w| w.borrow_mut().clear());
            LOGGED.with(|l| l.borrow_mut().clear());
            BODY_INNER_HTML.with(|b| b.borrow_mut().clear());
            JsRuntime {
                timeout: Some(timeout),
            }
        }

        /// Create a new runtime with no per-script timeout.
        pub fn without_timeout() -> Self {
            WRITTEN.with(|w| w.borrow_mut().clear());
            LOGGED.with(|l| l.borrow_mut().clear());
            BODY_INNER_HTML.with(|b| b.borrow_mut().clear());
            JsRuntime { timeout: None }
        }

        /// Evaluate the browser bootstrap and then each script in `scripts` in order.
        ///
        /// Scripts are evaluated in sloppy (non-strict) mode to match browser behaviour —
        /// assignments to undeclared globals are allowed, as used by `SvelteKit` and webpack.
        /// Errors from individual scripts are printed to stderr and skipped; the method
        /// only returns `Err` if the bootstrap itself fails to evaluate.
        pub fn execute(
            &self,
            scripts: &[String],
            page_url: Option<&str>,
            cfg: &crate::HttpConfig,
        ) -> anyhow::Result<()> {
            XHR_UA.with(|u| u.borrow_mut().clone_from(&cfg.user_agent));
            XHR_HEADERS.with(|h| h.borrow_mut().clone_from(&cfg.headers));
            XHR_PROXY.with(|p| p.borrow_mut().clone_from(&cfg.proxy));
            XHR_TIMEOUT.with(|t| *t.borrow_mut() = self.timeout);
            XHR_FORWARD_HEADERS.with(|f| f.set(cfg.forward_headers));

            let rt = Runtime::new().map_err(|e| anyhow!("quickjs runtime: {e:?}"))?;
            rt.set_loader(StubModuleSystem, StubModuleSystem);

            // Check the per-script deadline every 10 000 opcodes to keep overhead near zero.
            rt.set_interrupt_handler(Some(Box::new({
                let mut counter = 0u32;
                move || {
                    counter = counter.wrapping_add(1);
                    if !counter.is_multiple_of(10_000) {
                        return false;
                    }
                    SCRIPT_DEADLINE.with(|d| d.borrow().is_some_and(|dl| Instant::now() > dl))
                }
            })));

            let ctx = Context::full(&rt).map_err(|e| anyhow!("quickjs context: {e:?}"))?;

            ctx.with(|ctx| -> anyhow::Result<()> {
                setup_document(&ctx)?;
                setup_console(&ctx)?;
                setup_xhr_fetch(&ctx)?;

                let sloppy = || {
                    let mut o = EvalOptions::default();
                    o.strict = false;
                    o
                };

                let bootstrap = super::make_bootstrap(page_url);
                ctx.eval_with_options::<Value, _>(bootstrap, sloppy())
                    .map_err(|e| anyhow!("bootstrap error: {e:?}"))?;

                for script in scripts {
                    if let Some(t) = self.timeout {
                        set_deadline(t);
                    }
                    let result = ctx.eval_with_options::<Value, _>(script.as_str(), sloppy());
                    clear_deadline();
                    if result.is_err() {
                        let exc = ctx.catch();
                        if let Some(e) = exc.as_exception() {
                            let msg = e.message().unwrap_or_else(|| "unknown exception".into());
                            eprintln!("[js error] {msg}");
                            if crate::is_verbose()
                                && let Some(stack) = e.stack()
                            {
                                eprintln!("[js stack] {stack}");
                            }
                        }
                    }
                    // Drain Promise microtasks after each script so .then() chains fire
                    // before the next script runs.
                    while ctx.execute_pending_job() {}
                }

                // Flush _r_timers in a Rust loop, draining Promise microtasks between passes.
                // Ember/Glimmer's Backburner run loop schedules rendering via Promise chains,
                // so both queues must be drained together until both are empty.
                let mut consecutive_empty = 0u32;
                for _ in 0..128u32 {
                    if let Some(t) = self.timeout {
                        set_deadline(t);
                    }
                    let remaining: i32 = ctx
                        .eval_with_options::<Value, _>(super::TIMER_FLUSH_JS, sloppy())
                        .ok()
                        .and_then(|v| v.as_int())
                        .unwrap_or(0);
                    clear_deadline();
                    let mut had_jobs = false;
                    while ctx.execute_pending_job() {
                        had_jobs = true;
                    }
                    if remaining == 0 && !had_jobs {
                        consecutive_empty += 1;
                        if consecutive_empty >= 3 {
                            break;
                        }
                    } else {
                        consecutive_empty = 0;
                    }
                }

                if let Some(t) = self.timeout {
                    set_deadline(t);
                }
                let body_html: String = ctx
                    .eval_with_options::<Value, _>(super::READBACK_JS, sloppy())
                    .ok()
                    .and_then(|v| v.as_string().and_then(|s| s.to_string().ok()))
                    .unwrap_or_default();
                clear_deadline();

                let body_html = match body_html.as_str() {
                    "undefined" | "null" | "" => String::new(),
                    s => s.to_owned(),
                };
                BODY_INNER_HTML.with(|b| *b.borrow_mut() = body_html);

                Ok(())
            })?;

            Ok(())
        }

        /// Return the accumulated output of all `document.write` / `document.writeln` calls.
        pub fn written_html() -> String {
            WRITTEN.with(|w| w.borrow().clone())
        }

        /// Return the final value of `document.body.innerHTML` (or registry element content).
        pub fn body_inner_html() -> String {
            BODY_INNER_HTML.with(|b| b.borrow().clone())
        }

        /// Return all messages logged via `console.log`, `console.warn`, or `console.error`.
        pub fn logged_messages() -> Vec<String> {
            LOGGED.with(|l| l.borrow().clone())
        }
    }

    /// Register `document.write` and `document.writeln`.
    fn setup_document(ctx: &Ctx<'_>) -> anyhow::Result<()> {
        let doc = Object::new(ctx.clone()).map_err(|e| anyhow!("{e:?}"))?;

        doc.set(
            "write",
            Function::new(ctx.clone(), |s: String| {
                WRITTEN.with(|w| w.borrow_mut().push_str(&s));
                Ok::<(), rquickjs::Error>(())
            })
            .map_err(|e| anyhow!("{e:?}"))?,
        )
        .map_err(|e| anyhow!("{e:?}"))?;

        doc.set(
            "writeln",
            Function::new(ctx.clone(), |s: String| {
                WRITTEN.with(|w| {
                    let mut w = w.borrow_mut();
                    w.push_str(&s);
                    w.push('\n');
                });
                Ok::<(), rquickjs::Error>(())
            })
            .map_err(|e| anyhow!("{e:?}"))?,
        )
        .map_err(|e| anyhow!("{e:?}"))?;

        ctx.globals()
            .set("document", doc)
            .map_err(|e| anyhow!("{e:?}"))?;
        Ok(())
    }

    /// Register `console.log`, `console.warn`, and `console.error`.
    fn setup_console(ctx: &Ctx<'_>) -> anyhow::Result<()> {
        use rquickjs::function::Rest;

        let console = Object::new(ctx.clone()).map_err(|e| anyhow!("{e:?}"))?;

        let log_fn = Function::new(ctx.clone(), |args: Rest<rquickjs::Coerced<String>>| {
            let parts: Vec<String> = args.0.into_iter().map(|s| s.0).collect();
            LOGGED.with(|l| l.borrow_mut().push(parts.join(" ")));
            Ok::<(), rquickjs::Error>(())
        })
        .map_err(|e| anyhow!("{e:?}"))?;

        let noop_fn = Function::new(ctx.clone(), || Ok::<(), rquickjs::Error>(()))
            .map_err(|e| anyhow!("{e:?}"))?;

        console
            .set("log", log_fn.clone())
            .map_err(|e| anyhow!("{e:?}"))?;
        console
            .set("warn", log_fn.clone())
            .map_err(|e| anyhow!("{e:?}"))?;
        console
            .set("error", log_fn.clone())
            .map_err(|e| anyhow!("{e:?}"))?;
        console
            .set("info", log_fn.clone())
            .map_err(|e| anyhow!("{e:?}"))?;
        console.set("debug", log_fn).map_err(|e| anyhow!("{e:?}"))?;
        console
            .set("table", noop_fn.clone())
            .map_err(|e| anyhow!("{e:?}"))?;
        console
            .set("group", noop_fn.clone())
            .map_err(|e| anyhow!("{e:?}"))?;
        console
            .set("groupEnd", noop_fn.clone())
            .map_err(|e| anyhow!("{e:?}"))?;
        console
            .set("groupCollapsed", noop_fn.clone())
            .map_err(|e| anyhow!("{e:?}"))?;
        console
            .set("time", noop_fn.clone())
            .map_err(|e| anyhow!("{e:?}"))?;
        console
            .set("timeEnd", noop_fn.clone())
            .map_err(|e| anyhow!("{e:?}"))?;
        console
            .set("assert", noop_fn)
            .map_err(|e| anyhow!("{e:?}"))?;

        ctx.globals()
            .set("console", console)
            .map_err(|e| anyhow!("{e:?}"))?;
        Ok(())
    }

    /// Register `_r_fetch_sync(url)` — a synchronous HTTP GET used by the XHR stub
    /// so frameworks that XHR-load templates (e.g. `RiotJS`) get real response bodies.
    fn setup_xhr_fetch(ctx: &Ctx<'_>) -> anyhow::Result<()> {
        let fetch_fn = Function::new(ctx.clone(), |url: String| -> String {
            let ua = XHR_UA.with(|u| u.borrow().clone());
            let headers = XHR_HEADERS.with(|h| h.borrow().clone());
            let proxy = XHR_PROXY.with(|p| p.borrow().clone());
            let timeout = XHR_TIMEOUT.with(|t| *t.borrow());
            let forward_hdrs = XHR_FORWARD_HEADERS.with(std::cell::Cell::get);
            if !url.starts_with("http://") && !url.starts_with("https://") {
                return String::new();
            }
            let mut builder = ureq::AgentBuilder::new();
            if let Some(ref proxy_url) = proxy
                && let Ok(p) = ureq::Proxy::new(proxy_url)
            {
                builder = builder.proxy(p);
            }
            if let Some(dur) = timeout {
                builder = builder.timeout(dur);
            }
            let agent = builder.build();
            let mut req = agent.get(&url);
            if let Some(ref ua_str) = ua {
                req = req.set("User-Agent", ua_str);
            }
            if forward_hdrs {
                for (name, value) in &headers {
                    req = req.set(name, value);
                }
            }
            match req.call() {
                Ok(resp) => resp.into_string().unwrap_or_default(),
                Err(_) => String::new(),
            }
        })
        .map_err(|e| anyhow!("{e:?}"))?;

        ctx.globals()
            .set("_r_fetch_sync", fetch_fn)
            .map_err(|e| anyhow!("{e:?}"))?;
        Ok(())
    }
}

#[cfg(feature = "rquickjs")]
pub use quickjs_rt::JsRuntime;