quickjs-jit-stdlib 0.14.0

LLRT's standard modules for quickjs-jit, vendored into one crate
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
// Vendored from LLRT d9fd873e793c (modules/llrt_fetch/src/request.rs); module paths and
// feature names adapted by scripts/import-stdlib.py, with local patches.
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use std::sync::RwLock;

use crate::abort::AbortSignal;
use crate::http::Agent;
use crate::json::parse::json_parse;
use crate::stream_web::ReadableStream;
use crate::url::{url_class::URL, url_search_params::URLSearchParams};
use crate::utils::{bytes::ObjectBytes, object::ObjectExt, result::ResultExt};
use hyper::{header::CONTENT_TYPE, Method};
use rquickjs::{
    atom::PredefinedAtom, class::Trace, function::Opt, prelude::This, ArrayBuffer, Class, Coerced,
    Ctx, Exception, FromJs, IntoJs, Null, Object, Promise, Result, TypedArray, Value,
};

use crate::fetch::body_helpers::{bytes_to_utf8_simd_lossy, strip_bom};

use super::{
    headers::{Headers, HeadersGuard},
    Blob, FormData, MIME_TYPE_FORM_DATA, MIME_TYPE_FORM_URLENCODED, MIME_TYPE_OCTET_STREAM,
    MIME_TYPE_TEXT,
};

#[derive(Clone, Default, PartialEq)]
pub enum RequestMode {
    #[default]
    Cors,
    NoCors,
    SameOrigin,
    Navigate,
}

impl TryFrom<String> for RequestMode {
    type Error = String;

    fn try_from(s: String) -> std::result::Result<Self, Self::Error> {
        Ok(match s.as_str() {
            "cors" => RequestMode::Cors,
            "no-cors" => RequestMode::NoCors,
            "same-origin" => RequestMode::SameOrigin,
            "navigate" => RequestMode::Navigate,
            _ => return Err(["Invalid request mode: ", s.as_str()].concat()),
        })
    }
}

impl RequestMode {
    pub fn as_str(&self) -> &str {
        match self {
            Self::Cors => "cors",
            Self::NoCors => "no-cors",
            Self::SameOrigin => "same-origin",
            Self::Navigate => "navigate",
        }
    }
}

#[allow(dead_code)]
#[derive(rquickjs::JsLifetime)]
enum BodyVariant<'js> {
    Provided(Option<Value<'js>>),
    Empty,
}

pub(crate) enum BodyTaken<'js> {
    Bytes(Vec<u8>),
    Stream(Class<'js, ReadableStream<'js>>),
}

#[rquickjs::class]
#[derive(rquickjs::JsLifetime)]
pub struct Request<'js> {
    url: String,
    method: Method,
    headers: Option<Class<'js, Headers>>,
    body: RwLock<BodyVariant<'js>>,
    body_stream: RwLock<Option<Value<'js>>>,
    signal: Option<Class<'js, AbortSignal<'js>>>,
    mode: RequestMode,
    keepalive: bool,
    agent: Option<Class<'js, Agent>>,
}

impl<'js> Trace<'js> for Request<'js> {
    fn trace<'a>(&self, tracer: rquickjs::class::Tracer<'a, 'js>) {
        if let Some(headers) = &self.headers {
            headers.trace(tracer);
        }
        let body = self.body.read().unwrap();
        if let BodyVariant::Provided(Some(body)) = &*body {
            body.trace(tracer);
        }
        if let Some(stream) = self.body_stream.read().unwrap().as_ref() {
            stream.trace(tracer);
        }
    }
}

#[rquickjs::methods(rename_all = "camelCase")]
impl<'js> Request<'js> {
    #[qjs(constructor)]
    pub fn new(
        ctx: Ctx<'js>,
        this: This<Value<'js>>,
        input: Value<'js>,
        options: Opt<Value<'js>>,
    ) -> Result<Self> {
        // When called with `new`, rquickjs passes the constructor function as
        // `this`. When called without `new`, `this` is undefined (strict) or
        // the global object (sloppy). WPT's request-error.any.js requires
        // `Request("...")` to throw TypeError.
        if this.as_function().is_none() {
            return Err(Exception::throw_type(
                &ctx,
                "Failed to construct 'Request': Please use the 'new' operator",
            ));
        }
        let mut request = Self {
            url: "".into(),
            method: Method::GET,
            headers: None,
            body: RwLock::new(BodyVariant::Empty),
            body_stream: RwLock::new(None),
            signal: None,
            mode: RequestMode::Cors,
            keepalive: false,
            agent: None,
        };

        // If the input is a Request, we may need to tee its body at the very
        // end so construction failures (e.g. GET with body) don't leave the
        // input disturbed. This holds the input request and whether init.body
        // overrides the body.
        let mut input_request_to_disturb: Option<(Class<'js, Request<'js>>, bool)> = None;

        if input.is_string() {
            let s: String = input.get()?;
            // Validate as a URL; accept relative URLs against a generic base.
            if s.is_empty() {
                return Err(Exception::throw_type(&ctx, "Invalid URL"));
            }
            let base = url::Url::parse("http://llrt.local/").expect("static base URL");
            let parsed = base
                .join(&s)
                .map_err(|_| Exception::throw_type(&ctx, "Invalid URL"))?;
            if !parsed.username().is_empty() || parsed.password().is_some() {
                return Err(Exception::throw_type(&ctx, "URL must not have credentials"));
            }
            request.url = s;
        } else if let Ok(url) = URL::from_js(&ctx, input.clone()) {
            request.url = url.to_string();
        } else if input.is_object() {
            let obj = input.as_object().expect("input is an object");
            // Check if input is a Request - if so, transfer body (mark original as used)
            if let Some(input_request) = Class::<Request>::from_object(obj) {
                let input_req = input_request.borrow();
                let init_overrides_body = options
                    .0
                    .as_ref()
                    .and_then(|v| v.as_object())
                    .and_then(|o| o.get::<_, Value>("body").ok())
                    .is_some_and(|v| !v.is_undefined() && !v.is_null());
                if !init_overrides_body && is_unusable_body(&input_req) {
                    return Err(Exception::throw_type(
                        &ctx,
                        "Cannot construct a Request from a disturbed or locked body",
                    ));
                }
                request.url = input_req.url.clone();
                request.method = input_req.method.clone();
                request.mode = input_req.mode.clone();
                request.keepalive = input_req.keepalive;
                request.signal = input_req.signal.clone();
                request.agent = input_req.agent.clone();
                if let Some(headers) = &input_req.headers {
                    request.headers = Some(Class::<Headers>::instance(
                        ctx.clone(),
                        headers.borrow().clone(),
                    )?);
                }
                drop(input_req);
                input_request_to_disturb = Some((input_request, init_overrides_body));
            } else {
                assign_request(&mut request, ctx.clone(), obj)?;
            }
        }
        if let Some(options) = options.0 {
            if let Some(obj) = options.as_object() {
                assign_request(&mut request, ctx.clone(), obj)?;
            }
        }
        // Validate: GET/HEAD requests cannot have a body (even when the body
        // was inherited from an input Request).
        if matches!(request.method, Method::GET | Method::HEAD) {
            let has_body = matches!(
                &*request.body.read().unwrap(),
                BodyVariant::Provided(Some(_))
            ) || input_request_to_disturb.as_ref().is_some_and(
                |(r, override_body)| {
                    !override_body
                        && matches!(
                            &*r.borrow().body.read().unwrap(),
                            BodyVariant::Provided(Some(_))
                        )
                },
            );
            if has_body {
                return Err(Exception::throw_type(
                    &ctx,
                    "Failed to construct 'Request': Request with GET/HEAD method cannot have body.",
                ));
            }
        }
        // Now that all validation has passed, disturb the input's body (if it
        // was a Request). The input's body field is cleared (marking bodyUsed
        // = true) while its cached body_stream is preserved so that the input's
        // `body` getter keeps returning the same object it did before.
        if let Some((input_request, init_overrides_body)) = input_request_to_disturb {
            let input_req = input_request.borrow();
            if let Ok(body_value) = input_req.body(ctx.clone()) {
                if let Some(stream) = body_value
                    .as_object()
                    .and_then(Class::<ReadableStream>::from_object)
                {
                    let is_unusable =
                        stream.borrow().is_readable_stream_locked() || stream.borrow().disturbed;
                    if !is_unusable {
                        let (_b1, branch2) =
                            crate::stream_web::tee_readable_stream(ctx.clone(), stream)?;
                        if !init_overrides_body {
                            *request.body.write().unwrap() =
                                BodyVariant::Provided(Some(branch2.into_value()));
                        }
                    }
                }
            }
            // Mark input's body as consumed without clearing the cached stream
            // reference, so `bodyRequest.body === originalBody` still holds.
            *input_req.body.write().unwrap() = BodyVariant::Provided(None);
            drop(input_req);
        }
        if request.headers.is_none() {
            let guard = if request.mode == RequestMode::NoCors {
                HeadersGuard::RequestNoCors
            } else {
                HeadersGuard::Request
            };
            let mut default_headers = Headers::default();
            default_headers.guard = guard;
            let headers = Class::instance(ctx, default_headers)?;
            request.headers = Some(headers);
        }

        Ok(request)
    }

    #[qjs(get)]
    fn url(&self) -> &str {
        &self.url
    }

    #[qjs(get)]
    fn method(&self) -> &str {
        self.method.as_str()
    }

    #[qjs(get)]
    fn headers(&self) -> Option<Class<'js, Headers>> {
        self.headers.clone()
    }

    #[qjs(prop, rename = PredefinedAtom::SymbolToStringTag, configurable)]
    pub fn to_string_tag() -> &'static str {
        stringify!(Request)
    }

    #[qjs(get)]
    fn body(&self, ctx: Ctx<'js>) -> Result<Value<'js>> {
        // Return cached stream if available
        if let Some(stream) = self.body_stream.read().unwrap().as_ref() {
            return Ok(stream.clone());
        }

        let body_guard = self.body.read().unwrap();
        match &*body_guard {
            BodyVariant::Empty => Ok(Value::new_null(ctx)),
            BodyVariant::Provided(None) => Ok(Value::new_null(ctx)),
            BodyVariant::Provided(Some(value)) => {
                // If already a ReadableStream, return it directly
                if let Some(stream) = value
                    .as_object()
                    .and_then(Class::<ReadableStream>::from_object)
                {
                    return Ok(stream.into_value());
                }

                // Create a ReadableStream that yields the body data once
                let body_value = value.clone();
                drop(body_guard);

                let stream =
                    crate::fetch::body_helpers::create_body_value_stream(&ctx, body_value)?;
                *self.body_stream.write().unwrap() = Some(stream.clone());
                Ok(stream)
            }
        }
    }

    #[qjs(get)]
    fn keepalive(&self) -> bool {
        self.keepalive
    }

    #[qjs(get)]
    fn signal(&self) -> Option<Class<'js, AbortSignal<'js>>> {
        self.signal.clone()
    }

    #[qjs(get)]
    fn body_used(&self) -> bool {
        if crate::fetch::body_helpers::is_body_stream_disturbed(&self.body_stream) {
            return true;
        }

        let body = self.body.read().unwrap();
        match &*body {
            BodyVariant::Provided(value) => value.is_none(),
            BodyVariant::Empty => false,
        }
    }

    #[qjs(get)]
    fn mode(&self) -> &str {
        self.mode.as_str()
    }

    #[qjs(get)]
    fn destination(&self) -> &'static str {
        ""
    }

    #[qjs(get)]
    fn referrer(&self) -> &'static str {
        "about:client"
    }

    #[qjs(get, rename = "referrerPolicy")]
    fn referrer_policy(&self) -> &'static str {
        ""
    }

    #[qjs(get)]
    fn credentials(&self) -> &'static str {
        "same-origin"
    }

    #[qjs(get)]
    fn integrity(&self) -> &'static str {
        ""
    }

    #[qjs(get)]
    fn redirect(&self) -> &'static str {
        "follow"
    }

    #[qjs(get, rename = "isReloadNavigation")]
    fn is_reload_navigation(&self) -> bool {
        false
    }

    #[qjs(get, rename = "isHistoryNavigation")]
    fn is_history_navigation(&self) -> bool {
        false
    }

    #[qjs(get)]
    fn duplex(&self) -> &'static str {
        "half"
    }

    #[qjs(get)]
    fn cache(&self) -> &'static str {
        "default"
    }

    #[qjs(get)]
    fn agent(&self) -> Option<Class<'js, Agent>> {
        self.agent.clone()
    }

    pub fn text(&self, ctx: Ctx<'js>) -> Result<Promise<'js>> {
        let body = match self.take_body_sync(&ctx) {
            Ok(b) => b,
            Err(e) => return reject_with_error(&ctx, e),
        };
        let ctx_clone = ctx.clone();
        Promise::wrap_future(&ctx, async move {
            let bytes_opt = resolve_body_taken(&ctx_clone, body).await?;
            if let Some(bytes) = bytes_opt {
                let bytes = strip_bom(bytes);
                return Result::<String>::Ok(bytes_to_utf8_simd_lossy(bytes));
            }
            Ok(String::new())
        })
    }

    pub fn text_stream(&self, ctx: Ctx<'js>) -> Result<Value<'js>> {
        let body = match self.take_body_sync(&ctx) {
            Ok(Some(body)) => Some(body),
            Ok(None) => None,
            Err(err) => return Err(err),
        };
        let body = match body {
            None => return crate::fetch::body_helpers::create_text_stream(&ctx, None),
            Some(BodyTaken::Bytes(bytes)) => Some(
                crate::fetch::body_helpers::create_body_value_stream(&ctx, bytes.into_js(&ctx)?)?,
            ),
            Some(BodyTaken::Stream(stream)) => Some(stream.into_value()),
        };
        crate::fetch::body_helpers::create_text_stream(&ctx, body)
    }

    pub fn json(&self, ctx: Ctx<'js>) -> Result<Promise<'js>> {
        let body = match self.take_body_sync(&ctx) {
            Ok(b) => b,
            Err(e) => return reject_with_error(&ctx, e),
        };
        let ctx_clone = ctx.clone();
        Promise::wrap_future(&ctx, async move {
            let bytes_opt = resolve_body_taken(&ctx_clone, body).await?;
            if let Some(bytes) = bytes_opt {
                return json_parse(&ctx_clone, strip_bom(bytes));
            }
            Err(Exception::throw_syntax(&ctx_clone, "JSON input is empty"))
        })
    }

    fn array_buffer(&self, ctx: Ctx<'js>) -> Result<Promise<'js>> {
        let body = match self.take_body_sync(&ctx) {
            Ok(b) => b,
            Err(e) => return reject_with_error(&ctx, e),
        };
        let ctx_clone = ctx.clone();
        Promise::wrap_future(&ctx, async move {
            let bytes_opt = resolve_body_taken(&ctx_clone, body).await?;
            if let Some(bytes) = bytes_opt {
                return ArrayBuffer::new_copy(ctx_clone, bytes);
            }
            ArrayBuffer::new_copy(ctx_clone, Vec::<u8>::new())
        })
    }

    fn bytes(&self, ctx: Ctx<'js>) -> Result<Promise<'js>> {
        let body = match self.take_body_sync(&ctx) {
            Ok(b) => b,
            Err(e) => return reject_with_error(&ctx, e),
        };
        let ctx_clone = ctx.clone();
        Promise::wrap_future(&ctx, async move {
            let bytes_opt = resolve_body_taken(&ctx_clone, body).await?;
            if let Some(bytes) = bytes_opt {
                return TypedArray::new(ctx_clone, bytes).map(|m| m.into_value());
            }
            TypedArray::new(ctx_clone, Vec::<u8>::new()).map(|m| m.into_value())
        })
    }

    fn blob(&self, ctx: Ctx<'js>) -> Result<Promise<'js>> {
        let mime_type = self.get_header_value(&ctx, CONTENT_TYPE.as_str())?;
        let body = match self.take_body_sync(&ctx) {
            Ok(b) => b,
            Err(e) => return reject_with_error(&ctx, e),
        };
        let ctx_clone = ctx.clone();
        Promise::wrap_future(&ctx, async move {
            let bytes_opt = resolve_body_taken(&ctx_clone, body).await?;
            let bytes = bytes_opt.unwrap_or_default();
            Blob::from_bytes(&ctx_clone, bytes, mime_type)
        })
    }

    async fn form_data(&self, ctx: Ctx<'js>) -> Result<FormData<'js>> {
        let mime_type = self
            .get_header_value(&ctx, CONTENT_TYPE.as_str())?
            .unwrap_or(MIME_TYPE_OCTET_STREAM.into());

        let is_multipart = mime_type.starts_with("multipart/form-data");
        let is_urlencoded =
            mime_type.starts_with(MIME_TYPE_FORM_URLENCODED.split(';').next().unwrap_or(""));
        if !is_multipart && !is_urlencoded {
            return Err(Exception::throw_type(
                &ctx,
                "formData: invalid Content-Type",
            ));
        }

        if let Some(bytes) = resolve_body_taken(&ctx, self.take_body_sync(&ctx)?).await? {
            let form_data = FormData::from_multipart_bytes(&ctx, &mime_type, bytes)?;
            return Ok(form_data);
        }
        // Empty body: reject only for multipart (parser requires boundary).
        if is_multipart {
            return Err(Exception::throw_type(&ctx, "formData: body is empty"));
        }
        Ok(FormData::default())
    }

    fn clone(&self, ctx: Ctx<'js>) -> Result<Self> {
        let headers = if let Some(headers) = &self.headers {
            Some(Class::<Headers>::instance(
                ctx.clone(),
                headers.borrow().clone(),
            )?)
        } else {
            None
        };

        let body_guard = self.body.read().unwrap();
        let cloned_body = match &*body_guard {
            BodyVariant::Provided(provided) => {
                let Some(provided) = provided else {
                    return Err(Exception::throw_type(
                        &ctx,
                        "Cannot clone request after body has been used",
                    ));
                };
                // For ReadableStream bodies, use tee() to create two branches
                if let Some(stream) = provided
                    .as_object()
                    .and_then(Class::<ReadableStream>::from_object)
                {
                    let (branch1, branch2) =
                        crate::stream_web::tee_readable_stream(ctx.clone(), stream.clone())?;

                    // Update original body to use branch1
                    drop(body_guard);
                    let mut body_write = self.body.write().unwrap();
                    *body_write = BodyVariant::Provided(Some(branch1.into_value()));
                    *self.body_stream.write().unwrap() = None;

                    return Ok(Self {
                        url: self.url.clone(),
                        method: self.method.clone(),
                        headers,
                        body: RwLock::new(BodyVariant::Provided(Some(branch2.into_value()))),
                        body_stream: RwLock::new(None),
                        signal: self.signal.clone(),
                        mode: self.mode.clone(),
                        keepalive: self.keepalive,
                        agent: self.agent.clone(),
                    });
                }
                BodyVariant::Provided(Some(provided.clone()))
            }
            BodyVariant::Empty => BodyVariant::Empty,
        };
        drop(body_guard);

        Ok(Self {
            url: self.url.clone(),
            method: self.method.clone(),
            headers,
            body: RwLock::new(cloned_body),
            body_stream: RwLock::new(None),
            signal: self.signal.clone(),
            mode: self.mode.clone(),
            keepalive: self.keepalive,
            agent: self.agent.clone(),
        })
    }
}

impl<'js> Request<'js> {
    /// Synchronously takes the body (marking bodyUsed) and returns either
    /// ready bytes or a ReadableStream to be consumed asynchronously.
    pub(crate) fn take_body_sync(&self, ctx: &Ctx<'js>) -> Result<Option<BodyTaken<'js>>> {
        if let Some(stream_val) = self.body_stream.read().unwrap().as_ref() {
            if let Some(stream) = stream_val
                .as_object()
                .and_then(Class::<ReadableStream>::from_object)
            {
                crate::fetch::body_helpers::validate_stream_usable(ctx, &stream, "read body")?;
            }
        }

        let mut body_guard = self.body.write().unwrap();
        match &mut *body_guard {
            BodyVariant::Provided(provided) => {
                let provided = provided
                    .take()
                    .ok_or(Exception::throw_type(ctx, "Body is already read"))?;
                drop(body_guard);

                if let Some(stream) = provided
                    .as_object()
                    .and_then(Class::<ReadableStream>::from_object)
                {
                    return Ok(Some(BodyTaken::Stream(stream)));
                }

                let bytes =
                    if let Some(blob) = provided.as_object().and_then(Class::<Blob>::from_object) {
                        blob.borrow().get_bytes()
                    } else {
                        let bytes = ObjectBytes::from(ctx, &provided)?;
                        bytes.as_bytes(ctx)?.to_vec()
                    };
                Ok(Some(BodyTaken::Bytes(bytes)))
            }
            BodyVariant::Empty => Ok(None),
        }
    }

    fn get_header_value(&self, _ctx: &Ctx<'js>, key: &str) -> Result<Option<String>> {
        let Some(headers) = self.headers.as_ref() else {
            return Ok(None);
        };
        Ok(headers
            .borrow()
            .iter()
            .find_map(|(k, v)| (k == key).then(|| v.to_string())))
    }
}

/// Collects all data from a ReadableStream into a Vec<u8>
async fn collect_readable_stream<'js>(
    _ctx: &Ctx<'js>,
    stream: &Class<'js, ReadableStream<'js>>,
) -> Result<Vec<u8>> {
    crate::fetch::body_helpers::collect_readable_stream(stream).await
}

fn reject_with_error<'js>(ctx: &Ctx<'js>, err: rquickjs::Error) -> Result<Promise<'js>> {
    // Turn a synchronous throw into a rejected Promise. Extract the actual
    // JS exception value from the Ctx's pending-exception slot so that the
    // caller sees a real TypeError (or whatever) object — not an uninitialized
    // value (which `promise_rejects_js` checks for in WPT).
    use crate::stream_web::utils::promise::{promise_rejected_with, PromisePrimordials};
    use crate::utils::primordials::Primordial;
    let _ = err; // err is `Error::Exception`; the real value is in ctx.catch().
    let exception_value = ctx.catch();
    let primordials = PromisePrimordials::get(ctx)?;
    promise_rejected_with(&primordials, exception_value)
}

async fn resolve_body_taken<'js>(
    ctx: &Ctx<'js>,
    body: Option<BodyTaken<'js>>,
) -> Result<Option<Vec<u8>>> {
    match body {
        None => Ok(None),
        Some(BodyTaken::Bytes(bytes)) => Ok(Some(bytes)),
        Some(BodyTaken::Stream(stream)) => collect_readable_stream(ctx, &stream).await.map(Some),
    }
}

/// True if the request's body has been disturbed (read from) or locked.
/// Covers both cases that disqualify `new Request(existingReq)`.
fn is_unusable_body(req: &Request<'_>) -> bool {
    if req.body_used() {
        return true;
    }
    if let Some(stream_val) = req.body_stream.read().unwrap().as_ref() {
        if let Some(stream) = stream_val
            .as_object()
            .and_then(Class::<ReadableStream>::from_object)
        {
            return stream.borrow().is_readable_stream_locked();
        }
    }
    false
}

fn assign_request<'js>(request: &mut Request<'js>, ctx: Ctx<'js>, obj: &Object<'js>) -> Result<()> {
    if let Some(url) = obj.get_optional("url")? {
        request.url = url;
    }
    if let Some(method) = obj.get_optional::<_, String>("method")? {
        let method = method.to_ascii_uppercase();
        let method = Method::from_bytes(method.as_bytes())
            .map_err(|_| Exception::throw_type(&ctx, "Invalid HTTP method name"))?;
        if matches!(method.as_str(), "CONNECT" | "TRACE" | "TRACK") {
            return Err(Exception::throw_type(&ctx, "This method is not allowed."));
        }
        request.method = method;
    }
    if let Some(mode) = obj.get_optional::<_, String>("mode")? {
        if mode == "navigate" {
            return Err(Exception::throw_type(
                &ctx,
                "Cannot construct a Request with a RequestInit whose mode member is set as 'navigate'.",
            ));
        }
        if mode == "no-cors" && !matches!(request.method, Method::GET | Method::HEAD | Method::POST)
        {
            return Err(Exception::throw_type(
                &ctx,
                "'no-cors' mode requires the method to be GET, HEAD, or POST",
            ));
        }
        if let Some(cache) = obj.get_optional::<_, String>("cache")? {
            if cache == "only-if-cached" && mode != "same-origin" {
                return Err(Exception::throw_type(
                    &ctx,
                    "Cache mode 'only-if-cached' requires mode to be 'same-origin'.",
                ));
            }
        }
        request.mode = mode
            .try_into()
            .or_throw_type(&ctx, "Invalid request mode")?;
    }
    if let Some(referrer) = obj.get_optional::<_, String>("referrer")? {
        if !referrer.is_empty() && referrer != "about:client" {
            // Must be a valid URL
            if !crate::url::url_class::URL::is_valid(&referrer) {
                return Err(Exception::throw_type(&ctx, "Referrer is not a valid URL."));
            }
        }
    }
    if let Some(rp) = obj.get_optional::<_, String>("referrerPolicy")? {
        if !matches!(
            rp.as_str(),
            "" | "no-referrer"
                | "no-referrer-when-downgrade"
                | "same-origin"
                | "origin"
                | "strict-origin"
                | "origin-when-cross-origin"
                | "strict-origin-when-cross-origin"
                | "unsafe-url"
        ) {
            return Err(Exception::throw_type(&ctx, "Invalid referrerPolicy"));
        }
    }
    if let Some(credentials) = obj.get_optional::<_, String>("credentials")? {
        if !matches!(credentials.as_str(), "omit" | "same-origin" | "include") {
            return Err(Exception::throw_type(&ctx, "Invalid credentials"));
        }
    }
    if let Some(cache) = obj.get_optional::<_, String>("cache")? {
        if !matches!(
            cache.as_str(),
            "default" | "no-store" | "reload" | "no-cache" | "force-cache" | "only-if-cached"
        ) {
            return Err(Exception::throw_type(&ctx, "Invalid cache mode"));
        }
    }
    if let Some(redirect) = obj.get_optional::<_, String>("redirect")? {
        if !matches!(redirect.as_str(), "follow" | "error" | "manual") {
            return Err(Exception::throw_type(&ctx, "Invalid redirect mode"));
        }
    }
    if let Some(duplex) = obj.get_optional::<_, Value>("duplex")? {
        if !duplex.is_undefined() && !duplex.is_null() {
            let d: String = duplex.get()?;
            if !matches!(d.as_str(), "half" | "full") {
                return Err(Exception::throw_type(&ctx, "Invalid duplex"));
            }
            if d == "full" {
                return Err(Exception::throw_type(
                    &ctx,
                    "duplex 'full' is not supported",
                ));
            }
        }
    }
    if let Some(priority) = obj.get_optional::<_, Value>("priority")? {
        if !priority.is_undefined() && !priority.is_null() {
            let p: String = priority.get()?;
            if !matches!(p.as_str(), "high" | "low" | "auto") {
                return Err(Exception::throw_type(&ctx, "Invalid priority"));
            }
        }
    }
    if let Some(window) = obj.get_optional::<_, Value>("window")? {
        if !window.is_null() {
            return Err(Exception::throw_type(&ctx, "window must be null"));
        }
    }
    if let Some(keepalive) = obj.get_optional::<_, Value>("keepalive")? {
        request.keepalive = if let Some(b) = keepalive.as_bool() {
            b
        } else if let Some(n) = keepalive.as_number() {
            n != 0.0
        } else {
            false
        }
    }

    if let Some(signal) = obj.get_optional::<_, Value>("signal")? {
        if !signal.is_undefined() && !signal.is_null() {
            let signal = AbortSignal::from_js(&ctx, signal).map_err(|_| {
                Exception::throw_type(
                    &ctx,
                    "Failed to construct 'Request': 'signal' property is not an AbortSignal",
                )
            })?;
            request.signal = Some(Class::instance(ctx.clone(), signal)?);
        }
    }

    let mut content_type: Option<String> = None;

    if let Some(body) = obj.get_optional::<_, Value>("body")? {
        if !body.is_undefined() && !body.is_null() {
            if matches!(request.method, Method::GET | Method::HEAD) {
                return Err(Exception::throw_type(
                    &ctx,
                    "Failed to construct 'Request': Request with GET/HEAD method cannot have body.",
                ));
            }

            let body = if body.is_string() {
                content_type = Some(MIME_TYPE_TEXT.into());
                BodyVariant::Provided(Some(body))
            } else if let Some(body_obj) = body.as_object() {
                // Check if it's a ReadableStream
                if let Some(stream) = Class::<ReadableStream>::from_object(body_obj) {
                    // Per spec, `duplex` must be "half" when body is a stream.
                    // Skip when the init object is itself a Request (body comes
                    // from the Request's body getter, not a user-supplied stream).
                    let is_request_init = Class::<Request>::from_object(obj).is_some();
                    if !is_request_init {
                        let duplex: Option<String> = obj.get_optional("duplex")?;
                        if duplex.as_deref() != Some("half") {
                            return Err(Exception::throw_type(
                                &ctx,
                                "Request with stream body requires duplex: 'half'",
                            ));
                        }
                    }
                    if request.keepalive {
                        return Err(Exception::throw_type(
                            &ctx,
                            "keepalive requests cannot have a streaming body",
                        ));
                    }
                    crate::fetch::body_helpers::validate_stream_usable(
                        &ctx,
                        &stream,
                        "construct Request",
                    )?;
                    *request.body_stream.write().unwrap() = Some(body.clone());
                    BodyVariant::Provided(Some(body))
                } else if let Some(blob) = Class::<Blob>::from_object(body_obj) {
                    let blob = blob.borrow();
                    if !blob.mime_type().is_empty() {
                        content_type = Some(blob.mime_type());
                    }
                    BodyVariant::Provided(Some(body))
                } else if let Some(fd) = Class::<FormData>::from_object(body_obj) {
                    let fd = fd.borrow();
                    let (multipart_body, boundary) = fd.to_multipart_bytes(&ctx)?;
                    content_type = Some([MIME_TYPE_FORM_DATA, &boundary].concat());
                    BodyVariant::Provided(Some(multipart_body.into_js(&ctx)?))
                } else if body_obj.instance_of::<URLSearchParams>() {
                    content_type = Some(MIME_TYPE_FORM_URLENCODED.into());
                    BodyVariant::Provided(Some(body))
                } else if ArrayBuffer::from_value(body.clone()).is_some()
                    || body_obj.get::<_, Value>("buffer").is_ok_and(|b| {
                        b.as_object()
                            .is_some_and(|o| ArrayBuffer::from_object(o.clone()).is_some())
                    })
                {
                    BodyVariant::Provided(Some(body))
                } else {
                    // WebIDL: fall back to stringifying the value (USVString body).
                    let s: String = Coerced::from_js(&ctx, body.clone())?.0;
                    content_type = Some(MIME_TYPE_TEXT.into());
                    BodyVariant::Provided(Some(s.into_js(&ctx)?))
                }
            } else {
                BodyVariant::Provided(Some(body))
            };
            request.body = RwLock::new(body);
        }
    }

    let headers = {
        let guard = if request.mode == RequestMode::NoCors {
            HeadersGuard::RequestNoCors
        } else {
            HeadersGuard::Request
        };
        let mut headers = Headers::from_value(
            &ctx,
            obj.get_optional("headers")?
                .unwrap_or_else(|| Null.into_js(&ctx).unwrap()),
            guard,
        )?;
        if !headers.contains_lower(CONTENT_TYPE.as_str()) {
            if let Some(value) = content_type {
                headers.set(
                    ctx.clone(),
                    CONTENT_TYPE.as_str().into(),
                    value.into_js(&ctx)?,
                )?;
            }
        }
        Class::instance(ctx, headers)?
    };
    request.headers = Some(headers);

    if let Some(agent_opt) = obj.get_optional("agent")? {
        request.agent = Some(agent_opt);
    }

    Ok(())
}