redjs 0.3.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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
//! `fetch`, `Request` and `Response` (WHATWG Fetch).
//!
//! ## The contract this module is built around
//!
//! **A request either happens, or the promise rejects.** There is no third
//! outcome. What used to be here did have a third outcome: an `http(s)` URL
//! printed
//!
//! ```text
//! [js:fetch] http(s) is not supported in the player
//! ```
//!
//! to stderr and then *resolved* with a `Response` carrying `status: 0` and an
//! empty body. Nothing threw, nothing rejected, and the caller — an application
//! resolving its backend instance, loading an account, reading remote config —
//! saw a well-formed answer that described a request no socket had ever carried.
//! A silent wrong answer, in the runtime's own words.
//!
//! Everything below follows from removing that outcome:
//!
//! - `http:`/`https:` go through a real client ([`super::http`]).
//! - Anything this runtime cannot do is **refused by name**: the rejection says
//!   which option it was and why, so the failure is a sentence, not a mystery.
//! - `Response` never carries a synthetic status. The only `status: 0` this
//!   module can produce is `Response.error()`, which is the spec's own network-
//!   error sentinel and is tagged `type: "error"` so it cannot be confused with
//!   a real reply.
//! - A body that arrives short is an error, not a short body (enforced in the
//!   transport, see [`super::http`]).
//!
//! ## Threading
//!
//! `fetch` returns immediately. The round trip runs on a worker thread that
//! never touches JSC; the result comes back through `web::schedule_native`, the
//! same pump hand-off image decoding uses, and the promise is settled from the
//! pump thread with the VM lock held. Blocking the pump on a socket would stall
//! the whole application for the duration of the request, and settling a promise
//! from the worker thread would touch the VM from off-thread; this is the only
//! shape that does neither.
//!
//! ## Streaming
//!
//! Response bodies are **buffered**, not streamed. `Response.body` is a real
//! `ReadableStream` (so `for await (const chunk of res.body)` works), but it is
//! a stream over bytes already in memory: peak cost is the whole body, and the
//! first chunk is not available until the last one has arrived. That is the
//! wrong trade for a multi-hundred-megabyte download and the right one for the
//! JSON and config payloads this exists to unblock — large assets in this engine
//! come off disk through the content provider, not off the network. Wiring the
//! transport to the streams core incrementally is a follow-up, not a shim: it
//! changes only what `body` is fed from.

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

use super::headers;
use super::http;
use super::request::{self, build_plan, extract_body, Plan};
use super::util::*;
use super::{blob, fsutil};
use crate::capi;
use crate::glue;

/// Simultaneous in-flight network requests. Without `net-h2` each one is a
/// thread; past this a request is refused loudly rather than exhausting the
/// thread pool of the process an app is running in. With `net-h2` a request is
/// a task (it queues for a connection like a browser's does), so the ceiling
/// is only a backstop against a runaway loop.
#[cfg(not(feature = "net-h2"))]
const MAX_INFLIGHT: usize = 64;
#[cfg(feature = "net-h2")]
const MAX_INFLIGHT: usize = 4096;
static INFLIGHT: AtomicUsize = AtomicUsize::new(0);

const CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
const IO_TIMEOUT: Duration = Duration::from_secs(60);

/// `*mut c_void` is not `Send`, but the pump hand-off needs the context key on
/// the worker thread purely to name the queue it posts back to — it is never
/// dereferenced off the JS thread.
#[derive(Clone, Copy)]
struct Ctx(*mut c_void);
unsafe impl Send for Ctx {}

impl Ctx {
    /// A method, not a field read: edition-2021 closures capture individual
    /// fields, and capturing `ctx.0` would capture the bare `*mut c_void`
    /// (not `Send`) instead of the wrapper.
    fn ptr(self) -> *mut c_void {
        self.0
    }
}

// ---------------------------------------------------------------------------
// Response
// ---------------------------------------------------------------------------

pub struct RespData {
    pub bytes: Arc<Vec<u8>>,
    pub headers: Vec<(String, String)>,
    pub url: String,
    pub status: i32,
    pub status_text: String,
    pub redirected: bool,
    /// `"basic"` for a real reply, `"error"` for `Response.error()`.
    pub kind: &'static str,
    /// Spec "null body": `body` is `null` and there is nothing to consume.
    pub has_body: bool,
    /// Set by the first body read; a second one is a TypeError, as in browsers.
    pub body_used: bool,
    /// How the network delivered it, for an embedder's devtools; `None` for a
    /// response that never crossed a network (constructed, local, `data:`).
    /// Not reachable from script.
    pub net: Option<NetInfo>,
}

/// Transport facts about one network response (its final hop), for an
/// embedder's devtools. Never reachable from script.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NetInfo {
    /// `"h2"` or `"http/1.1"` (ALPN spelling, as `nextHopProtocol` reports it).
    pub protocol: &'static str,
    /// The connection had already carried an earlier request.
    pub reused: bool,
    /// Process-unique connection number: equal ids shared one connection.
    pub connection_id: u64,
    /// The address the connection was made to.
    pub remote_addr: Option<std::net::SocketAddr>,
    /// The final hop's `Set-Cookie` lines exactly as received. The jar has
    /// stored them and the `Response` script sees has none (Fetch's forbidden
    /// response-header names); this copy is for devtools only.
    pub set_cookies: Vec<String>,
    /// What the HTTP cache did for the final hop (`Hit`: no connection was
    /// used, and the transport fields above are empty).
    pub cache: http::cache::Outcome,
}

/// The [`NetInfo`] of a `Response` object, if it came off the network. For an
/// embedder's devtools (skathi's Network panel): `response` is the JS
/// `Response` the app received, read on the JS thread — e.g. from a host
/// function the devtools tee calls with it. `None` for a response that never
/// crossed a network (constructed, local file, `data:`).
///
/// # Safety
/// `g` is a live context's global object and `response` a value of its heap;
/// call on that context's thread.
pub unsafe fn net_info(g: *mut c_void, response: u64) -> Option<NetInfo> {
    host_data::<RespData>(g, state(g).response_class, response).and_then(|d| d.net.clone())
}

impl RespData {
    fn mime(&self) -> String {
        http::header(&self.headers, "content-type")
            .map(|s| s.to_string())
            .unwrap_or_default()
    }
}

unsafe fn this_resp<'a>(g: *mut c_void, cf: *mut c_void) -> Option<&'a mut RespData> {
    host_data::<RespData>(g, state(g).response_class, cf_this(cf))
}

pub unsafe fn create_response(g: *mut c_void, data: RespData) -> u64 {
    let st = state(g);
    host_create(g, st.response_class, st.response.proto, data)
}

// ---------------------------------------------------------------------------
// Errors and promises
// ---------------------------------------------------------------------------

pub const ERR: u32 = 0;
pub const TYPE_ERR: u32 = 1;

pub unsafe fn make_error(g: *mut c_void, kind: u32, name: &str, msg: &str) -> u64 {
    let m: Vec<u16> = msg.encode_utf16().collect();
    let mut err = UNDEF;
    glue::rjs_error_create(g, kind, m.as_ptr(), m.len(), &mut err);
    if !name.is_empty() && is_cell(err) {
        define(g, err, "name", js_string(g, name), 0);
    }
    err
}

/// A promise already rejected with `value`.
pub unsafe fn reject_with(g: *mut c_void, value: u64) -> u64 {
    let (mut res, mut rej, mut p) = (0u64, 0u64, 0u64);
    if crate::ffi::redjs_promise_create(g, &mut res, &mut rej, &mut p) != 0 {
        return UNDEF;
    }
    let _ = call(g, rej, UNDEF, &[value]);
    p
}

/// The rejection every refusal goes through. `TypeError` is what a browser
/// rejects `fetch` with, and the message names the thing we would not do.
pub unsafe fn reject_refusal(g: *mut c_void, msg: &str) -> u64 {
    let e = make_error(g, TYPE_ERR, "", msg);
    reject_with(g, e)
}

// ---------------------------------------------------------------------------
// Body reading, shared by Request and Response
// ---------------------------------------------------------------------------

/// Marks the body consumed, or returns the rejection for a double read.
unsafe fn take_body(g: *mut c_void, cf: *mut c_void, who: &str) -> Result<Arc<Vec<u8>>, u64> {
    let Some(d) = this_resp(g, cf) else {
        return Err(reject_refusal(g, &format!("Response.{who} called on a non-Response")));
    };
    if d.body_used {
        return Err(reject_refusal(g, &format!("Response.{who}: body has already been consumed")));
    }
    d.body_used = true;
    Ok(Arc::clone(&d.bytes))
}

macro_rules! resp_getter {
    ($name:ident, |$d:ident, $g:ident| $body:expr) => {
        #[crate::host_fn]
        unsafe fn $name($g: *mut c_void, cf: *mut c_void) -> u64 {
            match this_resp($g, cf) {
                Some($d) => $body,
                None => throw_type_error($g, "Response getter called on a non-Response"),
            }
        }
    };
}

resp_getter!(resp_ok, |d, g| {
    let _ = g;
    enc_bool(d.status >= 200 && d.status < 300)
});
resp_getter!(resp_status, |d, g| {
    let _ = g;
    enc_i32(d.status)
});
resp_getter!(resp_status_text, |d, g| {
    let s = d.status_text.clone();
    js_string(g, &s)
});
resp_getter!(resp_url, |d, g| {
    let s = d.url.clone();
    js_string(g, &s)
});
resp_getter!(resp_redirected, |d, g| {
    let _ = g;
    enc_bool(d.redirected)
});
resp_getter!(resp_body_used, |d, g| {
    let _ = g;
    enc_bool(d.body_used)
});
resp_getter!(resp_type, |d, g| {
    let k = d.kind;
    js_string(g, k)
});

/// Cached per spec identity: `res.headers === res.headers`.
#[crate::host_fn]
unsafe fn resp_headers(g: *mut c_void, cf: *mut c_void) -> u64 {
    let this = cf_this(cf);
    let slot = atom_priv(g, "redjs#respHeaders");
    let cached = get_a(g, this, slot);
    if is_cell(cached) {
        glue::rjs_atom_free(g, slot);
        return cached;
    }
    let Some(d) = this_resp(g, cf) else {
        glue::rjs_atom_free(g, slot);
        return throw_type_error(g, "Response.headers on a non-Response");
    };
    let list = d.headers.clone();
    let h = headers::create(g, list);
    glue::rjs_define(g, this, slot, h, 0);
    glue::rjs_atom_free(g, slot);
    h
}

/// `Response.body` — a `ReadableStream` over the buffered bytes (see the module
/// header for why buffered). `null` for a null-body response, per spec.
#[crate::host_fn]
unsafe fn resp_body(g: *mut c_void, cf: *mut c_void) -> u64 {
    let this = cf_this(cf);
    let slot = atom_priv(g, "redjs#bodyStream");
    let cached = get_a(g, this, slot);
    if is_cell(cached) {
        glue::rjs_atom_free(g, slot);
        return cached;
    }
    let (bytes, has_body) = match this_resp(g, cf) {
        Some(d) => (Arc::clone(&d.bytes), d.has_body),
        None => {
            glue::rjs_atom_free(g, slot);
            return throw_type_error(g, "Response.body on a non-Response");
        }
    };
    if !has_body {
        glue::rjs_atom_free(g, slot);
        return NULL;
    }
    let stream = make_body_stream(g, &bytes);
    if is_cell(stream) {
        glue::rjs_define(g, this, slot, stream, 0);
        // Reading the stream is reading the body.
        if let Some(d) = this_resp(g, cf) {
            d.body_used = true;
        }
    }
    glue::rjs_atom_free(g, slot);
    stream
}

/// Lazily-built `bytes => ReadableStream` factory.
///
/// Lazy because `fetch::install` runs before `streams::install_surface` (the
/// install order in `webapi::install` is load-bearing), so `ReadableStream` does
/// not exist yet at install time. Held as a protected function value rather than
/// a global, so nothing is added to `globalThis`.
fn stream_factories() -> &'static Mutex<HashMap<usize, u64>> {
    static F: OnceLock<Mutex<HashMap<usize, u64>>> = OnceLock::new();
    F.get_or_init(|| Mutex::new(HashMap::new()))
}

/// The factory was protected in the gone context; that heap is gone with it.
pub(crate) fn forget(gone: crate::context::Gone) {
    stream_factories().lock().unwrap().remove(&gone.key);
    super::request::forget(gone);
}

unsafe fn make_body_stream(g: *mut c_void, bytes: &[u8]) -> u64 {
    let factory = {
        let mut map = stream_factories().lock().unwrap();
        match map.get(&(g as usize)) {
            Some(&f) => f,
            None => {
                const SRC: &str = "(bytes => new ReadableStream({ \
                     start(c) { c.enqueue(bytes); c.close(); } \
                   }))";
                let url = "redjs/fetch-body-stream";
                let mut out = 0u64;
                let rc = glue::rjs_eval8(g, SRC.as_ptr(), SRC.len(), url.as_ptr(), url.len(), &mut out);
                if rc != 0 || !is_cell(out) {
                    return UNDEF;
                }
                crate::ffi::redjs_protect(g, out);
                map.insert(g as usize, out);
                out
            }
        }
    };
    let u8v = make_u8(g, bytes);
    match call(g, factory, UNDEF, &[u8v]) {
        Ok(v) => v,
        Err(_) => UNDEF,
    }
}

#[crate::host_fn]
unsafe fn resp_array_buffer(g: *mut c_void, cf: *mut c_void) -> u64 {
    let bytes = match take_body(g, cf, "arrayBuffer") {
        Ok(b) => b,
        Err(p) => return p,
    };
    let u8v = make_u8(g, &bytes);
    let buffer = get(g, u8v, "buffer");
    resolved_promise(g, buffer)
}

#[crate::host_fn]
unsafe fn resp_bytes(g: *mut c_void, cf: *mut c_void) -> u64 {
    let bytes = match take_body(g, cf, "bytes") {
        Ok(b) => b,
        Err(p) => return p,
    };
    let u8v = make_u8(g, &bytes);
    resolved_promise(g, u8v)
}

#[crate::host_fn]
unsafe fn resp_text(g: *mut c_void, cf: *mut c_void) -> u64 {
    let bytes = match take_body(g, cf, "text") {
        Ok(b) => b,
        Err(p) => return p,
    };
    let s = String::from_utf8_lossy(&bytes).into_owned();
    resolved_promise(g, js_string(g, &s))
}

#[crate::host_fn]
unsafe fn resp_json(g: *mut c_void, cf: *mut c_void) -> u64 {
    let st = state(g);
    let bytes = match take_body(g, cf, "json") {
        Ok(b) => b,
        Err(p) => return p,
    };
    let s = String::from_utf8_lossy(&bytes).into_owned();
    let sv = js_string(g, &s);
    match call(g, st.json_parse, UNDEF, &[sv]) {
        Ok(v) => resolved_promise(g, v),
        // The parse error itself, not a generic one — a truncated or HTML error
        // page is the usual cause and the position tells you which.
        Err(e) => reject_with(g, e),
    }
}

#[crate::host_fn]
unsafe fn resp_blob(g: *mut c_void, cf: *mut c_void) -> u64 {
    let mime = match this_resp(g, cf) {
        Some(d) => d.mime(),
        None => return reject_refusal(g, "Response.blob called on a non-Response"),
    };
    let bytes = match take_body(g, cf, "blob") {
        Ok(b) => b,
        Err(p) => return p,
    };
    let b = blob::create(g, UNDEF, bytes.as_slice().to_vec(), mime);
    resolved_promise(g, b)
}

#[crate::host_fn]
unsafe fn resp_clone(g: *mut c_void, cf: *mut c_void) -> u64 {
    let Some(d) = this_resp(g, cf) else {
        return throw_type_error(g, "Response.clone called on a non-Response");
    };
    if d.body_used {
        return throw_type_error(g, "Response.clone: body has already been consumed");
    }
    let data = RespData {
        bytes: Arc::clone(&d.bytes),
        headers: d.headers.clone(),
        url: d.url.clone(),
        status: d.status,
        status_text: d.status_text.clone(),
        redirected: d.redirected,
        kind: d.kind,
        has_body: d.has_body,
        body_used: false,
        net: d.net.clone(),
    };
    create_response(g, data)
}

/// Reads a `ResponseInit` / `RequestInit` `headers` + `status` + `statusText`.
unsafe fn response_init(g: *mut c_void, init: u64, default_mime: Option<String>) -> Result<(i32, String, Vec<(String, String)>), String> {
    let mut list = Vec::new();
    let mut status = 200i32;
    let mut status_text = String::new();
    if is_cell(init) {
        let h = get(g, init, "headers");
        list = headers::from_init(g, h)?;
        let s = get(g, init, "status");
        if s != UNDEF {
            let n = read_number(g, s);
            if !(200.0..=599.0).contains(&n) {
                return Err(format!("status {n} is outside the range 200-599"));
            }
            status = n as i32;
        }
        let t = get(g, init, "statusText");
        if t != UNDEF {
            status_text = read_string(g, t).unwrap_or_default();
        }
    }
    if let Some(mime) = default_mime {
        if !list.iter().any(|(k, _)| k == "content-type") && !mime.is_empty() {
            list.push(("content-type".into(), mime));
        }
    }
    Ok((status, status_text, list))
}

#[crate::host_fn]
unsafe fn response_construct(g: *mut c_void, cf: *mut c_void) -> u64 {
    let body_v = cf_arg(cf, 0);
    let (bytes, implied, has_body) = match extract_body(g, body_v) {
        Ok(Some((b, m))) => (b, m, true),
        Ok(None) => (Vec::new(), None, false),
        Err(m) => return throw_type_error(g, &format!("new Response: {m}")),
    };
    let (status, status_text, list) = match response_init(g, cf_arg(cf, 1), implied) {
        Ok(v) => v,
        Err(m) => return throw_type_error(g, &format!("new Response: {m}")),
    };
    create_response(g, RespData {
        bytes: Arc::new(bytes),
        headers: list,
        url: String::new(),
        status,
        status_text,
        redirected: false,
        kind: "default",
        has_body,
        body_used: false,
        net: None,
    })
}

/// `Response.json(data, init)`.
#[crate::host_fn]
unsafe fn response_static_json(g: *mut c_void, cf: *mut c_void) -> u64 {
    let global = glue::rjs_global(g);
    let stringify = get(g, get(g, global, "JSON"), "stringify");
    let text = match call(g, stringify, UNDEF, &[cf_arg(cf, 0)]) {
        Ok(v) => read_string(g, v).unwrap_or_default(),
        Err(e) => {
            glue::rjs_throw(g, e);
            return UNDEF;
        }
    };
    let (status, status_text, list) =
        match response_init(g, cf_arg(cf, 1), Some("application/json".into())) {
            Ok(v) => v,
            Err(m) => return throw_type_error(g, &format!("Response.json: {m}")),
        };
    create_response(g, RespData {
        bytes: Arc::new(text.into_bytes()),
        headers: list,
        url: String::new(),
        status,
        status_text,
        redirected: false,
        kind: "default",
        has_body: true,
        body_used: false,
        net: None,
    })
}

/// `Response.error()` — the spec's network-error sentinel. This is the ONLY
/// legitimate `status: 0` in this module, and it is tagged `type: "error"` so
/// it can never be mistaken for the fake one that used to come back from
/// `fetch('https://…')`.
#[crate::host_fn]
unsafe fn response_static_error(g: *mut c_void, _cf: *mut c_void) -> u64 {
    create_response(g, RespData {
        bytes: Arc::new(Vec::new()),
        headers: Vec::new(),
        url: String::new(),
        status: 0,
        status_text: String::new(),
        redirected: false,
        kind: "error",
        has_body: false,
        body_used: false,
        net: None,
    })
}

// ---------------------------------------------------------------------------
// AbortSignal bridge
// ---------------------------------------------------------------------------

struct BridgeData {
    cancel: Arc<http::Cancel>,
}

fn bridge_class() -> capi::JSClassRef {
    static CLS: OnceLock<usize> = OnceLock::new();
    *CLS.get_or_init(|| unsafe { make_host_class::<BridgeData>("FetchAbortBridge") as usize })
        as capi::JSClassRef
}

/// The `abort` listener. A listener OBJECT rather than a function, because a
/// native function value cannot carry the `Arc<Cancel>`; DOM listeners may be
/// objects with `handleEvent` (and `events::call_listener` honours that).
#[crate::host_fn]
unsafe fn bridge_handle_event(g: *mut c_void, cf: *mut c_void) -> u64 {
    if let Some(d) = host_data::<BridgeData>(g, bridge_class(), cf_this(cf)) {
        d.cancel.abort();
    }
    UNDEF
}

unsafe fn make_bridge(g: *mut c_void, cancel: Arc<http::Cancel>) -> u64 {
    let obj = host_create(g, bridge_class(), UNDEF, BridgeData { cancel });
    define(g, obj, "handleEvent", make_fn(g, "handleEvent", 1, bridge_handle_event), 2);
    obj
}

unsafe fn signal_is_aborted(g: *mut c_void, signal: u64) -> bool {
    is_cell(signal) && truthy_flag(get(g, signal, "aborted"))
}

unsafe fn signal_reason(g: *mut c_void, signal: u64) -> u64 {
    let r = if is_cell(signal) { get(g, signal, "reason") } else { UNDEF };
    if r == UNDEF || r == NULL {
        super::domexception::create(g, "AbortError", "The operation was aborted")
    } else {
        r
    }
}

unsafe fn listen(g: *mut c_void, signal: u64, bridge: u64, add: bool) {
    let name = if add { "addEventListener" } else { "removeEventListener" };
    let f = get(g, signal, name);
    if is_cell(f) {
        let ev = js_string(g, "abort");
        let _ = call(g, f, signal, &[ev, bridge]);
    }
}

// ---------------------------------------------------------------------------
// fetch
// ---------------------------------------------------------------------------

/// Everything the delivery task needs, all of it `Send`.
struct Delivery {
    resolve: u64,
    reject: u64,
    signal: u64,
    bridge: u64,
    cancel: Arc<http::Cancel>,
    result: Result<http::Response, http::HttpError>,
}
unsafe impl Send for Delivery {}

unsafe fn settle(g: *mut c_void, d: Delivery) {
    if is_cell(d.signal) && is_cell(d.bridge) {
        listen(g, d.signal, d.bridge, false);
    }

    // An abort that landed while the result was queued still wins: the caller
    // asked for the request to stop before it observed an answer.
    let aborted = d.cancel.is_aborted() || signal_is_aborted(g, d.signal);

    match d.result {
        _ if aborted => {
            let reason = signal_reason(g, d.signal);
            let _ = call(g, d.reject, UNDEF, &[reason]);
        }
        Ok(resp) => {
            let has_body = !matches!(resp.status, 204 | 205 | 304);
            // Fetch's "forbidden response-header names": the jar has already
            // seen them (http::send, per hop); script never does, so
            // `headers.get("set-cookie")` is null and `getSetCookie()` is [].
            let mut headers = resp.headers;
            let set_cookies: Vec<String> = headers
                .iter()
                .filter(|(k, _)| k.eq_ignore_ascii_case("set-cookie"))
                .map(|(_, v)| v.clone())
                .collect();
            headers.retain(|(k, _)| !k.eq_ignore_ascii_case("set-cookie") && !k.eq_ignore_ascii_case("set-cookie2"));
            let obj = create_response(g, RespData {
                bytes: Arc::new(resp.body),
                headers,
                url: resp.url,
                status: resp.status as i32,
                status_text: resp.status_text,
                redirected: resp.redirected,
                kind: "basic",
                has_body,
                body_used: false,
                net: Some(NetInfo {
                    protocol: resp.protocol,
                    reused: resp.reused,
                    connection_id: resp.connection_id,
                    remote_addr: resp.remote_addr,
                    set_cookies,
                    cache: resp.cache,
                }),
            });
            let _ = call(g, d.resolve, UNDEF, &[obj]);
        }
        Err(e) => {
            // Browsers reject `fetch` with a TypeError for every network
            // failure; the message carries what actually went wrong.
            let err = match e.kind {
                http::ErrorKind::Aborted => super::domexception::create(g, "AbortError", "The operation was aborted"),
                _ => make_error(g, TYPE_ERR, "", &format!("Failed to fetch {}", e.message)),
            };
            let _ = call(g, d.reject, UNDEF, &[err]);
        }
    }

    crate::ffi::redjs_unprotect(g, d.resolve);
    crate::ffi::redjs_unprotect(g, d.reject);
    if is_cell(d.bridge) {
        crate::ffi::redjs_unprotect(g, d.bridge);
    }
    if is_cell(d.signal) {
        crate::ffi::redjs_unprotect(g, d.signal);
    }
}

unsafe fn fetch_network(g: *mut c_void, plan: Plan) -> u64 {
    let (mut resolve, mut reject, mut promise) = (0u64, 0u64, 0u64);
    if crate::ffi::redjs_promise_create(g, &mut resolve, &mut reject, &mut promise) != 0 {
        return reject_refusal(g, "fetch: could not create a promise");
    }

    // A signal that is already aborted never opens a socket (spec, and it is
    // also the cheapest correct answer).
    if signal_is_aborted(g, plan.signal) {
        let reason = signal_reason(g, plan.signal);
        let _ = call(g, reject, UNDEF, &[reason]);
        return promise;
    }

    if INFLIGHT.load(Ordering::SeqCst) >= MAX_INFLIGHT {
        let e = make_error(
            g,
            TYPE_ERR,
            "",
            &format!("Failed to fetch: more than {MAX_INFLIGHT} requests are already in flight"),
        );
        let _ = call(g, reject, UNDEF, &[e]);
        return promise;
    }

    let cancel = http::Cancel::new();
    let bridge = if is_cell(plan.signal) {
        let b = make_bridge(g, Arc::clone(&cancel));
        listen(g, plan.signal, b, true);
        crate::ffi::redjs_protect(g, b);
        crate::ffi::redjs_protect(g, plan.signal);
        b
    } else {
        UNDEF
    };

    crate::ffi::redjs_protect(g, resolve);
    crate::ffi::redjs_protect(g, reject);

    let req = http::Request {
        method: plan.method,
        url: plan.url,
        headers: plan.headers,
        body: plan.body,
        redirect: if plan.redirect == "error" { http::Redirect::Error } else { http::Redirect::Follow },
        // `check_supported` already refused anything else.
        credentials: super::cookies::Credentials::parse(&plan.credentials)
            .unwrap_or(super::cookies::Credentials::SameOrigin),
        connect_timeout: CONNECT_TIMEOUT,
        io_timeout: IO_TIMEOUT,
        observer: None,
        // `check_supported` already refused anything else.
        cache: http::cache::CacheMode::parse(&plan.cache).unwrap_or_default(),
    };

    let ctx = Ctx(g);
    let id = crate::context::id_of(g);
    let signal = plan.signal;
    let worker_cancel = Arc::clone(&cancel);
    INFLIGHT.fetch_add(1, Ordering::SeqCst);
    // The transport calls this once, on a network thread (a task on the
    // pooled runtime with `net-h2`, a worker thread without).
    let started = http::start(
        req,
        (*cancel).clone(),
        Box::new(move |result| {
            let delivery = Delivery { resolve, reject, signal, bridge, cancel: worker_cancel, result };
            INFLIGHT.fetch_sub(1, Ordering::SeqCst);
            // Back onto the JS thread: the pump runs this with the VM lock
            // held, and only in the context that asked. `resolve`/`reject`
            // are values of THAT heap; a newer context at the same address
            // must never call them, and a torn-down one must not get its
            // queue back.
            crate::web::schedule_native_from(ctx.ptr(), id, 0.0, Box::new(move |g| unsafe { settle(g, delivery) }));
        }),
    );

    if let Err(e) = started {
        INFLIGHT.fetch_sub(1, Ordering::SeqCst);
        let err = make_error(g, TYPE_ERR, "", &format!("Failed to fetch: {}", e.message));
        let _ = call(g, reject, UNDEF, &[err]);
        crate::ffi::redjs_unprotect(g, resolve);
        crate::ffi::redjs_unprotect(g, reject);
        if is_cell(bridge) {
            listen(g, signal, bridge, false);
            crate::ffi::redjs_unprotect(g, bridge);
            crate::ffi::redjs_unprotect(g, signal);
        }
    }
    promise
}

/// Local files and `data:` URIs — the paths that were already here, with the
/// one hole closed: a `data:` URI that does not parse now REJECTS instead of
/// resolving with a status-0 response.
unsafe fn fetch_local(g: *mut c_void, plan: &Plan) -> u64 {
    if plan.method != "GET" && plan.method != "HEAD" {
        return reject_refusal(g, &format!(
            "fetch: {} is not supported for local and data: URLs — only GET and HEAD are",
            plan.method
        ));
    }
    let fetched = fsutil::fetch_url(g, &plan.url);
    if fetched.status == 0 {
        // fsutil signals "could not even be interpreted" with status 0. That is
        // a failure, and the whole point of this change is that it must not
        // reach JS wearing a Response.
        return reject_refusal(g, &format!("Failed to fetch {}: the URL could not be read", plan.url));
    }
    let mime = fetched.mime.clone();
    let has_body = plan.method == "GET";
    let resp = create_response(g, RespData {
        bytes: Arc::new(if has_body { fetched.bytes } else { Vec::new() }),
        headers: vec![("content-type".into(), mime)],
        url: plan.url.clone(),
        status: fetched.status,
        status_text: fetched.status_text.to_string(),
        redirected: false,
        kind: "basic",
        has_body,
        body_used: false,
        net: None,
    });
    resolved_promise(g, resp)
}

#[crate::host_fn]
unsafe fn fetch_fn(g: *mut c_void, cf: *mut c_void) -> u64 {
    // Spec: `fetch` reports argument problems by REJECTING, never by throwing.
    let plan = match build_plan(g, cf_arg(cf, 0), cf_arg(cf, 1)) {
        Ok(p) => p,
        Err(m) => return reject_refusal(g, &format!("Failed to fetch: {m}")),
    };

    let lower = plan.url.trim_start().to_ascii_lowercase();
    if lower.starts_with("http://") || lower.starts_with("https://") {
        return fetch_network(g, plan);
    }
    if lower.starts_with("blob:") {
        return fetch_blob(g, &plan);
    }
    if let Some(scheme) = unsupported_scheme(&lower) {
        return reject_refusal(g, &format!(
            "Failed to fetch: the {scheme}: scheme is not supported by this runtime"
        ));
    }
    fetch_local(g, &plan)
}

/// Fetch §"parse a single range header value" with allowWhitespace = true,
/// which is how the blob scheme fetch calls it: `bytes`, optional tabs or
/// spaces, `=`, then `a-b`, `a-` or `-n` with optional tabs or spaces around
/// the numbers (`bytes= 9 - 21`, as Chromium and WPT `fetch/range/blob.any.js`
/// accept), and nothing after. `(start, end)`; `None` for anything else,
/// including a list of ranges. A number too large for `u64` is `u64::MAX`:
/// the spec reads the digits as a mathematical value, so an end past the
/// Blob is clamped by the caller and a start past it is a network error.
fn parse_single_range(v: &str) -> Option<(Option<u64>, Option<u64>)> {
    let b = v.as_bytes();
    let mut i = "bytes".len();
    if !b.starts_with(b"bytes") {
        return None;
    }
    let ws = |i: &mut usize| {
        while *i < b.len() && (b[*i] == b' ' || b[*i] == b'\t') {
            *i += 1;
        }
    };
    let digits = |i: &mut usize| -> Option<u64> {
        let from = *i;
        let mut n = 0u64;
        while *i < b.len() && b[*i].is_ascii_digit() {
            n = n.saturating_mul(10).saturating_add((b[*i] - b'0') as u64);
            *i += 1;
        }
        (*i > from).then_some(n)
    };
    ws(&mut i);
    if b.get(i) != Some(&b'=') {
        return None;
    }
    i += 1;
    ws(&mut i);
    let start = digits(&mut i);
    ws(&mut i);
    if b.get(i) != Some(&b'-') {
        return None;
    }
    i += 1;
    ws(&mut i);
    let end = digits(&mut i);
    if i != b.len() {
        return None;
    }
    match (start, end) {
        (None, None) => None,
        (Some(s), Some(e)) if s > e => None,
        r => Some(r),
    }
}

/// The window the blob scheme serves of a `full`-byte Blob, given the request
/// `headers`: `(start, len, Content-Range)`, the last `None` when no `Range`
/// was asked for. `Err` is why the request is a network error. Shared by
/// `fetch` and `XMLHttpRequest` (super::xhr).
pub(crate) fn blob_range(full: u64, headers: &[(String, String)]) -> Result<(u64, u64, Option<String>), String> {
    // "Get `Range`" combines every value with ", ", so two Range headers are
    // a list of ranges, which is a network error.
    let values: Vec<&str> =
        headers.iter().filter(|(k, _)| k.eq_ignore_ascii_case("range")).map(|(_, v)| v.as_str()).collect();
    if values.is_empty() {
        return Ok((0, full, None));
    }
    let v = values.join(", ");
    let Some(r) = parse_single_range(&v) else {
        return Err(format!("the Range header {v:?} is not a single byte range"));
    };
    let (s, e) = match r {
        // A suffix: the last n bytes (all of them when n is larger).
        (None, Some(n)) => (full.saturating_sub(n), full.saturating_sub(1)),
        (Some(s), e) => {
            if s >= full {
                return Err(format!("the Range {v:?} starts past the end of a {full}-byte Blob"));
            }
            (s, e.map_or(full - 1, |e| e.min(full - 1)))
        }
        (None, None) => unreachable!(),
    };
    if full == 0 || e < s {
        return Err(format!("the Range {v:?} selects nothing in an empty Blob"));
    }
    Ok((s, e - s + 1, Some(format!("bytes {s}-{e}/{full}"))))
}

/// What a blob fetch settles with, all of it `Send`.
struct BlobDelivery {
    resolve: u64,
    reject: u64,
    signal: u64,
    url: String,
    status: i32,
    headers: Vec<(String, String)>,
}
unsafe impl Send for BlobDelivery {}

impl BlobDelivery {
    unsafe fn settle(self, g: *mut c_void, body: Result<Arc<Vec<u8>>, super::blob::ReadError>) {
        // An abort that landed while the read ran still wins, as for http.
        if signal_is_aborted(g, self.signal) {
            let reason = signal_reason(g, self.signal);
            let _ = call(g, self.reject, UNDEF, &[reason]);
        } else {
            match body {
                Ok(bytes) => {
                    let resp = create_response(g, RespData {
                        bytes,
                        headers: self.headers,
                        url: self.url,
                        status: self.status,
                        status_text: if self.status == 206 { "Partial Content".into() } else { "OK".into() },
                        redirected: false,
                        kind: "basic",
                        has_body: true,
                        body_used: false,
                        net: None,
                    });
                    let _ = call(g, self.resolve, UNDEF, &[resp]);
                }
                Err(e) => {
                    // A network error, as for any blob whose read fails.
                    let err = make_error(g, TYPE_ERR, "", &format!("Failed to fetch: {}: {}", e.name(), e.message()));
                    let _ = call(g, self.reject, UNDEF, &[err]);
                }
            }
        }
        crate::ffi::redjs_unprotect(g, self.resolve);
        crate::ffi::redjs_unprotect(g, self.reject);
        if is_cell(self.signal) {
            crate::ffi::redjs_unprotect(g, self.signal);
        }
    }
}

/// Fetch §"scheme fetch" for `blob:`: GET only; the entry in this context's
/// store or a network error; `200` with `Content-Length` and `Content-Type`
/// (the Blob's type, even when it is empty), or `206` with `Content-Range`
/// for one valid `Range`. A memory Blob answers at once (its `Arc` shared
/// when the whole of it is asked for); a path-backed one is read on a read
/// thread under the snapshot rule, like `text()`.
unsafe fn fetch_blob(g: *mut c_void, plan: &Plan) -> u64 {
    if plan.method != "GET" {
        return reject_refusal(g, &format!("Failed to fetch: a blob: URL can only be fetched with GET, not {}", plan.method));
    }
    let Some(entry) = super::url::lookup(g, &plan.url) else {
        return reject_refusal(g, "Failed to fetch: this blob: URL is not in the blob URL store (it was revoked, or never created in this context)");
    };
    let (start, len, content_range) = match blob_range(entry.range.len, &plan.headers) {
        Ok(r) => r,
        Err(m) => return reject_refusal(g, &format!("Failed to fetch: {m}")),
    };
    let mut headers = vec![("content-length".to_string(), len.to_string()), ("content-type".to_string(), entry.mime.clone())];
    if let Some(cr) = &content_range {
        headers.push(("content-range".to_string(), cr.clone()));
    }
    let status = if content_range.is_some() { 206 } else { 200 };
    if signal_is_aborted(g, plan.signal) {
        let reason = signal_reason(g, plan.signal);
        return reject_with(g, reason);
    }
    let window = super::blob::Range { source: entry.range.source.clone(), offset: entry.range.offset + start, len };

    let (mut resolve, mut reject, mut promise) = (0u64, 0u64, 0u64);
    if crate::ffi::redjs_promise_create(g, &mut resolve, &mut reject, &mut promise) != 0 {
        return reject_refusal(g, "fetch: could not create a promise");
    }
    crate::ffi::redjs_protect(g, resolve);
    crate::ffi::redjs_protect(g, reject);
    if is_cell(plan.signal) {
        crate::ffi::redjs_protect(g, plan.signal);
    }
    // `Response.url` is serialized with "exclude fragment" (Fetch).
    let url = plan.url.split('#').next().unwrap_or_default().to_string();
    let d = BlobDelivery { resolve, reject, signal: plan.signal, url, status, headers };
    if let super::blob::Source::Memory(arc) = &window.source {
        let body = if window.offset == 0 && window.len as usize == arc.len() {
            Ok(Arc::clone(arc))
        } else {
            window.in_memory().map(|b| Arc::new(b.to_vec())).ok_or(super::blob::ReadError::NotReadable)
        };
        d.settle(g, body);
        return promise;
    }
    super::blob::read_async(g, window, Box::new(move |g, r| unsafe { d.settle(g, r.map(Arc::new)) }));
    promise
}

/// A scheme we recognise as a scheme but will not serve. Anything without a
/// scheme is a local path and goes to `fetch_local`.
pub(crate) fn unsupported_scheme(lower_url: &str) -> Option<&'static str> {
    for s in ["ws", "wss", "ftp", "ftps", "about", "chrome", "javascript"] {
        if lower_url.starts_with(s) && lower_url[s.len()..].starts_with(':') {
            return Some(s);
        }
    }
    None
}

// ---------------------------------------------------------------------------
// install
// ---------------------------------------------------------------------------

pub unsafe fn install(g: *mut c_void) -> (crate::capi::JSClassRef, Class) {
    // Order matters only in that `Request` and `Response` both want `Headers`.
    headers::install(g);
    request::install(g);

    let resp_cls = make_host_class::<RespData>("Response");
    let response = define_class(g, &ClassSpec {
        name: "Response",
        arity: 0,
        construct: Some(response_construct),
        parent_proto: 0,
        methods: &[
            ("arrayBuffer", 0, resp_array_buffer),
            ("bytes", 0, resp_bytes),
            ("text", 0, resp_text),
            ("json", 0, resp_json),
            ("blob", 0, resp_blob),
            ("clone", 0, resp_clone),
        ],
        getters: &[
            ("ok", resp_ok),
            ("status", resp_status),
            ("statusText", resp_status_text),
            ("url", resp_url),
            ("redirected", resp_redirected),
            ("bodyUsed", resp_body_used),
            ("body", resp_body),
            ("type", resp_type),
            ("headers", resp_headers),
        ],
        accessors: &[],
    });
    define(g, response.ctor, "json", make_fn(g, "json", 2, response_static_json), 2);
    define(g, response.ctor, "error", make_fn(g, "error", 0, response_static_error), 2);

    let n16: Vec<u16> = "fetch".encode_utf16().collect();
    let mut f = UNDEF;
    glue::rjs_function_create(g, n16.as_ptr(), n16.len(), 2, fetch_fn as *const c_void, &mut f);
    define(g, glue::rjs_global(g), "fetch", f, 2);

    (resp_cls, response)
}

// ---------------------------------------------------------------------------
// Tests — through real JS, against a real socket
// ---------------------------------------------------------------------------

#[cfg(test)]
mod jsc_tests {
    use crate::api::Context;
    use crate::webapi::testing::*;
    use std::io::{Read, Write};
    use std::time::{Duration, Instant};

    /// The shared `testing::ctx` installs the web classes but not the scheduler
    /// (`web::install`), which normally comes from the host. `fetch` needs it:
    /// results are delivered as native tasks on the pump, and the tests below
    /// also use `setTimeout`.
    fn fetch_ctx() -> Context {
        let c = ctx();
        unsafe {
            crate::web::install(c.raw());
        }
        c
    }

    /// Runs an async body to completion, pumping the scheduler (which is where
    /// a fetch result comes back through) and the microtask queue.
    fn run(c: &Context, body: &str) -> String {
        s(
            c,
            &format!(
                "globalThis.__out = '<pending>'; (async () => {{ {body} }})().then(\
                   v => {{ globalThis.__out = String(v); }}, \
                   e => {{ globalThis.__out = 'ERROR: ' + ((e && e.name ? e.name + ': ' : '') + ((e && e.message) || e)); }}); 'started'"
            ),
        );
        let deadline = Instant::now() + Duration::from_secs(20);
        loop {
            unsafe {
                crate::web::pump(c.raw(), crate::web::PUMP_TIMERS | crate::web::PUMP_MICROTASKS, core::ptr::null_mut());
            }
            c.drain_microtasks();
            let out = s(c, "globalThis.__out");
            if out != "<pending>" {
                return out;
            }
            if Instant::now() > deadline {
                return "<timed out>".into();
            }
            std::thread::sleep(Duration::from_millis(2));
        }
    }

    /// A loopback listener answering each connection with the next canned reply.
    fn serve(replies: Vec<Vec<u8>>) -> u16 {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        std::thread::spawn(move || {
            for reply in replies {
                let Ok((mut sock, _)) = listener.accept() else { return };
                let _ = sock.set_read_timeout(Some(Duration::from_secs(5)));
                let mut got = Vec::new();
                let mut buf = [0u8; 4096];
                loop {
                    match sock.read(&mut buf) {
                        Ok(0) => break,
                        Ok(n) => {
                            got.extend_from_slice(&buf[..n]);
                            if let Some(i) = got.windows(4).position(|w| w == b"\r\n\r\n") {
                                let len = String::from_utf8_lossy(&got[..i])
                                    .lines()
                                    .find_map(|l| {
                                        l.to_ascii_lowercase()
                                            .strip_prefix("content-length:")
                                            .map(|v| v.trim().parse::<usize>().unwrap_or(0))
                                    })
                                    .unwrap_or(0);
                                if got.len() >= i + 4 + len {
                                    break;
                                }
                            }
                        }
                        Err(_) => break,
                    }
                }
                let _ = sock.write_all(&reply);
                let _ = sock.flush();
                let _ = sock.shutdown(std::net::Shutdown::Write);
            }
        });
        port
    }

    // -- the defect this epic exists for -------------------------------------

    #[test]
    fn an_http_url_no_longer_resolves_with_a_synthetic_status_zero() {
        // THE regression test. The old implementation logged to stderr and
        // resolved a Response with status 0 and an empty body, so a caller saw
        // a success-shaped answer to a request that never happened. Nothing
        // here may resolve with a fabricated status again: either a real
        // response comes back, or the promise rejects.
        let c = fetch_ctx();
        let port = serve(vec![b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi".to_vec()]);
        let out = run(
            &c,
            &format!(
                "const r = await fetch('http://127.0.0.1:{port}/x'); \
                 if (r.status === 0) return 'FABRICATED STATUS 0'; \
                 return r.status + ':' + (await r.text());"
            ),
        );
        assert_eq!(out, "200:hi");
    }

    #[test]
    fn a_failed_request_rejects_rather_than_resolving() {
        // Nothing is listening on port 1. A resolved promise here — with any
        // status, any body — is the bug.
        let c = fetch_ctx();
        let out = run(
            &c,
            "try { const r = await fetch('http://127.0.0.1:1/nope'); \
               return 'RESOLVED ' + r.status; } \
             catch (e) { return 'rejected'; }",
        );
        assert_eq!(out, "rejected", "a failed fetch must reject, not resolve");
    }

    #[test]
    fn a_failed_request_rejects_with_a_typeerror_like_a_browser() {
        let c = fetch_ctx();
        let out = run(
            &c,
            "try { await fetch('http://127.0.0.1:1/nope'); return 'RESOLVED'; } \
             catch (e) { return (e instanceof TypeError) + ':' + /Failed to fetch/.test(e.message); }",
        );
        assert_eq!(out, "true:true");
    }

    #[test]
    fn an_unsupported_scheme_rejects_by_name_instead_of_resolving() {
        let c = fetch_ctx();
        for (url, needle) in [("ws://example.com/socket", "ws"), ("ftp://example.com/f", "ftp")] {
            let out = run(
                &c,
                &format!(
                    "try {{ const r = await fetch('{url}'); return 'RESOLVED ' + r.status; }} \
                     catch (e) {{ return e.message; }}"
                ),
            );
            assert!(out.contains(needle), "expected {needle}: to be named in the rejection, got {out}");
            assert!(!out.starts_with("RESOLVED"), "{url} resolved instead of rejecting: {out}");
        }
    }

    #[test]
    fn every_unsupported_option_is_refused_by_its_own_name() {
        let c = fetch_ctx();
        let cases = [
            ("{ redirect: 'manual' }", "manual"),
            ("{ credentials: 'bogus' }", "bogus"),
            ("{ mode: 'no-cors' }", "no-cors"),
            ("{ cache: 'only-if-cached' }", "only-if-cached"),
            ("{ integrity: 'sha256-abc' }", "integrity"),
            ("{ method: 'CONNECT' }", "CONNECT"),
        ];
        for (init, needle) in cases {
            let out = run(
                &c,
                &format!(
                    "try {{ const r = await fetch('http://127.0.0.1:1/x', {init}); \
                       return 'RESOLVED ' + r.status; }} \
                     catch (e) {{ return e.message; }}"
                ),
            );
            assert!(!out.starts_with("RESOLVED"), "{init} resolved instead of rejecting: {out}");
            assert!(out.contains(needle), "{init} did not name {needle:?} in its refusal: {out}");
        }
    }

    // -- the working path ------------------------------------------------------

    #[test]
    fn json_headers_and_status_come_back_from_a_real_server() {
        let c = fetch_ctx();
        let port = serve(vec![
            b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nX-Trace: abc\r\nContent-Length: 27\r\n\r\n{\"instance\":\"eu\",\"ok\":true}".to_vec(),
        ]);
        let out = run(
            &c,
            &format!(
                "const r = await fetch('http://127.0.0.1:{port}/resolve'); \
                 const j = await r.json(); \
                 return [r.ok, r.status, r.statusText, r.type, r.headers.get('x-trace'), \
                         r.headers.get('content-type'), j.instance, j.ok].join('|');"
            ),
        );
        assert_eq!(out, "true|200|OK|basic|abc|application/json|eu|true");
    }

    // -- credentials and the jar (webapi::cookies has the rules) --------------

    /// A loopback server that answers each of `n` connections with
    /// `reply(request head)` — so a reply can echo the Cookie it received.
    fn serve_fn(n: usize, reply: impl Fn(&str) -> String + Send + 'static) -> u16 {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        std::thread::spawn(move || {
            for _ in 0..n {
                let Ok((mut sock, _)) = listener.accept() else { return };
                let _ = sock.set_read_timeout(Some(Duration::from_secs(5)));
                let mut got = Vec::new();
                let mut buf = [0u8; 4096];
                while !got.windows(4).any(|w| w == b"\r\n\r\n") {
                    match sock.read(&mut buf) {
                        Ok(0) | Err(_) => break,
                        Ok(k) => got.extend_from_slice(&buf[..k]),
                    }
                }
                let head = String::from_utf8_lossy(&got).into_owned();
                let _ = sock.write_all(reply(&head).as_bytes());
                let _ = sock.flush();
                let _ = sock.shutdown(std::net::Shutdown::Write);
            }
        });
        port
    }

    /// The reply: echo the request's Cookie header as the body, plus `extra`
    /// response headers.
    fn echo_cookie(head: &str, extra: &str) -> String {
        let cookie = head
            .lines()
            .find_map(|l| l.strip_prefix("cookie: ").or_else(|| l.strip_prefix("Cookie: ")))
            .unwrap_or("-")
            .to_string();
        format!("HTTP/1.1 200 OK\r\n{extra}Content-Length: {}\r\n\r\n{cookie}", cookie.len())
    }

    #[test]
    fn include_stores_set_cookie_and_sends_it_back_and_omit_does_neither() {
        let _g = super::super::cookies::test_lock();
        super::super::cookies::reset_for_tests();
        let c = fetch_ctx();
        let port = serve_fn(4, |head| {
            if head.starts_with("GET /login") {
                echo_cookie(head, "Set-Cookie: sid=abc; Path=/; HttpOnly\r\nSet-Cookie: theme=dark; Path=/\r\n")
            } else {
                echo_cookie(head, "")
            }
        });
        let out = run(
            &c,
            &format!(
                "const base = 'http://127.0.0.1:{port}'; \
                 const a = await fetch(base + '/login', {{ credentials: 'include' }}); \
                 const first = await a.text(); \
                 const hidden = [String(a.headers.get('set-cookie')), a.headers.getSetCookie().length, \
                                 [...a.headers.keys()].includes('set-cookie')].join(','); \
                 const b = await (await fetch(base + '/me', {{ credentials: 'include' }})).text(); \
                 const c = await (await fetch(base + '/me', {{ credentials: 'omit' }})).text(); \
                 const d = await (await fetch(base + '/me')).text(); \
                 return [first, hidden, b, c, d].join('|');"
            ),
        );
        assert_eq!(out, "-|null,0,false|sid=abc; theme=dark|-|-");
        super::super::cookies::reset_for_tests();
    }

    #[test]
    fn document_cookie_is_empty_and_ignores_writes() {
        let _g = super::super::cookies::test_lock();
        super::super::cookies::reset_for_tests();
        super::super::cookies::set_from_header(
            &url::Url::parse("https://example.com/").unwrap(),
            "visible=1",
            super::super::cookies::Api::Http,
        )
        .unwrap();
        let c = fetch_ctx();
        let out = s(&c, "document.cookie = 'x=1'; JSON.stringify([typeof document.cookie, document.cookie])");
        assert_eq!(out, "[\"string\",\"\"]");
        assert!(super::super::cookies::get(None).iter().all(|k| k.name != "x"), "the write went nowhere");
        super::super::cookies::reset_for_tests();
    }

    #[test]
    fn a_post_sends_its_method_headers_and_body() {
        let c = fetch_ctx();
        let port = serve(vec![b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok".to_vec()]);
        let out = run(
            &c,
            &format!(
                "const r = await fetch('http://127.0.0.1:{port}/api/envelope', {{ \
                    method: 'POST', \
                    headers: {{ 'Content-Type': 'application/json', 'X-Token': 'abc' }}, \
                    body: JSON.stringify({{ hello: 'world' }}) }}); \
                 return r.status + ':' + (await r.text());"
            ),
        );
        assert_eq!(out, "200:ok");
    }

    #[test]
    fn an_error_status_is_a_resolved_response_not_a_rejection() {
        let c = fetch_ctx();
        let port = serve(vec![b"HTTP/1.1 503 Service Unavailable\r\nContent-Length: 4\r\n\r\ndown".to_vec()]);
        let out = run(
            &c,
            &format!(
                "const r = await fetch('http://127.0.0.1:{port}/x'); \
                 return [r.ok, r.status, r.statusText, await r.text()].join('|');"
            ),
        );
        assert_eq!(out, "false|503|Service Unavailable|down");
    }

    #[test]
    fn redirects_are_followed_and_the_final_url_is_reported() {
        let c = fetch_ctx();
        let port = serve(vec![
            b"HTTP/1.1 302 Found\r\nLocation: /final\r\nContent-Length: 0\r\n\r\n".to_vec(),
            b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\ndone".to_vec(),
        ]);
        let out = run(
            &c,
            &format!(
                "const r = await fetch('http://127.0.0.1:{port}/start'); \
                 return [r.status, r.redirected, r.url.endsWith('/final'), await r.text()].join('|');"
            ),
        );
        assert_eq!(out, "200|true|true|done");
    }

    #[test]
    fn net_info_reports_the_transport_and_keeps_set_cookie_for_devtools_only() {
        let c = fetch_ctx();
        let port = serve(vec![
            b"HTTP/1.1 200 OK\r\nSet-Cookie: a=1; Path=/\r\nSet-Cookie: b=2\r\nContent-Length: 2\r\n\r\nok".to_vec(),
        ]);
        let out = run(
            &c,
            &format!(
                "const r = await fetch('http://127.0.0.1:{port}/x'); globalThis.__resp = r; \
                 return [String(r.headers.get('set-cookie')), await r.text()].join('|');"
            ),
        );
        // Script never sees Set-Cookie (Fetch's forbidden response-header names)...
        assert_eq!(out, "null|ok");
        // ...the embedder's devtools do, with the rest of the transport facts.
        let v = c.global_get("__resp").unwrap();
        let info = unsafe { super::net_info(c.raw(), v.0) }.expect("a network response carries NetInfo");
        assert_eq!(info.protocol, "http/1.1");
        assert!(!info.reused);
        assert!(info.connection_id > 0);
        assert_eq!(info.remote_addr.map(|a| a.port()), Some(port));
        assert_eq!(info.set_cookies, vec!["a=1; Path=/".to_string(), "b=2".to_string()]);
        let constructed = c.eval("new Response('x')").unwrap();
        assert!(unsafe { super::net_info(c.raw(), constructed.0) }.is_none());
    }

    #[test]
    fn the_response_body_is_a_readable_stream() {
        let c = fetch_ctx();
        let port = serve(vec![b"HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhello world".to_vec()]);
        let out = run(
            &c,
            &format!(
                "const r = await fetch('http://127.0.0.1:{port}/x'); \
                 if (!(r.body instanceof ReadableStream)) return 'NOT A STREAM'; \
                 const rd = r.body.getReader(); let text = ''; \
                 for (;;) {{ const {{ value, done }} = await rd.read(); if (done) break; \
                   text += new TextDecoder().decode(value); }} \
                 return text;"
            ),
        );
        assert_eq!(out, "hello world");
    }

    #[test]
    fn a_204_has_a_null_body() {
        let c = fetch_ctx();
        let port = serve(vec![b"HTTP/1.1 204 No Content\r\n\r\n".to_vec()]);
        let out = run(
            &c,
            &format!(
                "const r = await fetch('http://127.0.0.1:{port}/x'); \
                 return [r.status, r.body === null, (await r.text()).length].join('|');"
            ),
        );
        assert_eq!(out, "204|true|0");
    }

    #[test]
    fn a_body_cannot_be_read_twice() {
        let c = fetch_ctx();
        let port = serve(vec![b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi".to_vec()]);
        let out = run(
            &c,
            &format!(
                "const r = await fetch('http://127.0.0.1:{port}/x'); \
                 await r.text(); \
                 try {{ await r.text(); return 'READ TWICE'; }} catch (e) {{ return 'rejected:' + r.bodyUsed; }}"
            ),
        );
        assert_eq!(out, "rejected:true");
    }

    // -- AbortSignal -----------------------------------------------------------

    #[test]
    fn a_pre_aborted_signal_rejects_with_its_own_reason() {
        let c = fetch_ctx();
        let out = run(
            &c,
            "const ac = new AbortController(); ac.abort(new Error('nope')); \
             try { await fetch('http://127.0.0.1:1/x', { signal: ac.signal }); return 'RESOLVED'; } \
             catch (e) { return e.message; }",
        );
        assert_eq!(out, "nope");
    }

    #[test]
    fn aborting_mid_flight_rejects_the_pending_fetch() {
        // A server that accepts and then never answers. Without the socket
        // shutdown behind AbortController this sits on the 60s read timeout.
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        std::thread::spawn(move || {
            let held = listener.accept();
            std::thread::sleep(Duration::from_secs(30));
            drop(held);
        });

        let c = fetch_ctx();
        let started = Instant::now();
        let out = run(
            &c,
            &format!(
                "const ac = new AbortController(); \
                 const p = fetch('http://127.0.0.1:{port}/silent', {{ signal: ac.signal }}); \
                 setTimeout(() => ac.abort(), 200); \
                 try {{ await p; return 'RESOLVED'; }} catch (e) {{ \
                   return e instanceof DOMException ? e.name + '/' + e.code : 'not a DOMException: ' + e.name; }}"
            ),
        );
        assert_eq!(out, "AbortError/20");
        assert!(started.elapsed() < Duration::from_secs(15), "abort did not cut the connection short");
    }

    // -- Headers, which used to be a trap ---------------------------------------

    #[test]
    fn headers_set_append_and_delete_actually_do_something() {
        // These three were bound to a no-op host function: `h.set(...)` returned
        // undefined and changed nothing, so an Authorization header assembled
        // this way was never sent and never reported missing.
        let c = fetch_ctx();
        let out = run(
            &c,
            "const h = new Headers(); \
             h.set('Authorization', 'Bearer abc'); \
             h.append('X-Multi', '1'); h.append('X-Multi', '2'); \
             h.set('X-Gone', 'x'); h.delete('X-Gone'); \
             return [h.get('authorization'), h.get('x-multi'), h.has('x-gone'), String(h.get('x-gone'))].join('|');",
        );
        assert_eq!(out, "Bearer abc|1, 2|false|null");
    }

    #[test]
    fn a_headers_record_initializer_keeps_every_key() {
        // The old constructor read exactly one key — `content-type` — and
        // dropped the rest without a word.
        let c = fetch_ctx();
        let out = run(
            &c,
            "const h = new Headers({ 'Content-Type': 'application/json', \
                                     'X-Token': 'abc', 'Accept': 'text/plain' }); \
             return [h.get('content-type'), h.get('x-token'), h.get('accept')].join('|');",
        );
        assert_eq!(out, "application/json|abc|text/plain");
    }

    #[test]
    fn headers_iterate_sorted_and_from_pairs() {
        let c = fetch_ctx();
        let out = run(
            &c,
            "const h = new Headers([['x-b', '2'], ['x-a', '1']]); \
             return [...h].map(([k, v]) => k + '=' + v).join(',');",
        );
        assert_eq!(out, "x-a=1,x-b=2");
    }

    #[test]
    fn an_injected_header_value_is_refused() {
        let c = fetch_ctx();
        let out = run(
            &c,
            "const h = new Headers(); \
             try { h.set('X-Evil', 'a\\r\\nX-Injected: 1'); return 'ACCEPTED'; } \
             catch (e) { return 'refused'; }",
        );
        assert_eq!(out, "refused");
    }

    // -- Request, which used to drop its own arguments ---------------------------

    #[test]
    fn a_request_keeps_the_method_headers_and_body_it_was_given() {
        // The bootstrap stub built `{ url, method, headers: new Headers() }` and
        // discarded init.headers and init.body outright, so
        // `fetch(new Request(url, {method:'POST', body}))` sent an empty GET.
        let c = fetch_ctx();
        let out = run(
            &c,
            "const r = new Request('http://example.com/x', { \
               method: 'post', headers: { 'X-Token': 'abc' }, body: 'payload' }); \
             return [r.method, r.url, r.headers.get('x-token'), await r.text()].join('|');",
        );
        assert_eq!(out, "POST|http://example.com/x|abc|payload");
    }

    #[test]
    fn fetch_accepts_a_request_object() {
        let c = fetch_ctx();
        let port = serve(vec![b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok".to_vec()]);
        let out = run(
            &c,
            &format!(
                "const req = new Request('http://127.0.0.1:{port}/x', {{ method: 'POST', body: 'abc' }}); \
                 const r = await fetch(req); return r.status + ':' + (await r.text());"
            ),
        );
        assert_eq!(out, "200:ok");
    }

    #[test]
    fn a_readable_stream_body_is_refused_by_name() {
        let c = fetch_ctx();
        let out = run(
            &c,
            "const body = new ReadableStream({ start(c) { c.enqueue(new Uint8Array([1])); c.close(); } }); \
             try { await fetch('http://127.0.0.1:1/x', { method: 'POST', body }); return 'ACCEPTED'; } \
             catch (e) { return /ReadableStream/.test(e.message) ? 'named' : 'unnamed: ' + e.message; }",
        );
        assert_eq!(out, "named");
    }

    // -- Response ----------------------------------------------------------------

    #[test]
    fn response_error_is_the_only_status_zero_and_says_so() {
        let c = fetch_ctx();
        let out = run(&c, "const r = Response.error(); return r.status + '|' + r.type + '|' + r.ok;");
        assert_eq!(out, "0|error|false");
    }

    #[test]
    fn response_json_builds_a_json_response() {
        let c = fetch_ctx();
        let out = run(
            &c,
            "const r = Response.json({ a: 1 }, { status: 201 }); \
             return [r.status, r.headers.get('content-type'), await r.text()].join('|');",
        );
        assert_eq!(out, "201|application/json|{\"a\":1}");
    }

    #[test]
    fn a_data_uri_still_works_and_a_broken_one_rejects() {
        let c = fetch_ctx();
        let ok = run(&c, "const r = await fetch('data:text/plain,hello'); return await r.text();");
        assert_eq!(ok, "hello");

        let bad = run(
            &c,
            "try { const r = await fetch('data:text/plain;base64'); return 'RESOLVED ' + r.status; } \
             catch (e) { return 'rejected'; }",
        );
        assert_eq!(bad, "rejected", "a malformed data: URI must not resolve with a fake status");
    }
}