bun_runtime 0.1.2

Bao runtime integration — JS engine + Bun API + event loop
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
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
// @trace REQ-ENG-006
// WebSocket + Performance + TextEncoder/TextDecoder + atob/btoa + queueMicrotask
use ::std::cell::RefCell;
use ::std::ffi::CString;
use ::std::io::{Read, Write};
use ::std::net::TcpStream;
use ::std::net::ToSocketAddrs;
use ::std::ptr::NonNull;
use ::std::sync::atomic::{AtomicU64, Ordering};
use ::std::sync::{Arc, Mutex};
use ::std::time::Duration;
use bun_core::ZBox;

// @trace REQ-ENG-006 [code:bun_uws] — RFC 6455 codec primitives reused for
// both the plain ws:// (via WebSocketClient) and the wss:// (TLS-driven) path.
use bun_uws::ws_codec::apply_mask;

use mozjs::conversions::unsafe_jsstr_to_string;
use mozjs::jsapi::*;
use mozjs::jsval::{BooleanValue, Int32Value, JSVal, NullValue, ObjectValue, StringValue, UndefinedValue};
use mozjs::realm::AutoRealm;
use mozjs::rooted;
use mozjs::rust::wrappers2::{
    CallOriginalPromiseResolve, CallOriginalPromiseThen, JS_DefineFunction, JS_DefineProperty3,
    JS_NewPlainObject, NewArrayObject1,
};

use crate::gc_store::{gc_store_get, gc_store_insert, gc_store_remove};

// @trace REQ-ENG-005 [algorithm:base64] base64 via workspace bun_base64 (SIMD-accelerated)

// ── WebSocket client ──
// @trace REQ-ENG-006 [api:WebSocket] [code:bun_uws] — RFC 6455 framing and the
// plain-text (ws://) client handshake are delegated to `bun_uws::ws_client`
// (WebSocketClient / parse_ws_url / RecvOutcome) and `bun_uws::ws_codec` /
// `ws_handshake`. The wss:// (TLS) variant drives `bao_boringssl_bridge`'s
// TlsConnection over the TCP socket and reuses the same `bun_uws` codec /
// handshake primitives so the two schemes share one wire-format code path.
//
// @trace REQ-STL-001 — the wss:// TLS handshake applies the page's
// StealthProfile through the exact same application path fetch() uses
// (`stealth_http::stealth_profile_to_ssl_config` →
// `bun_http::configure_http_client_with_alpn`), so a page's WebSocket and
// its fetch present an identical JA3/JA4 fingerprint.
//
// Async model (root fix for ScriptThread blocking): `new WebSocket(..)` never
// connects on the JS thread. The constructor captures the thread's stealth
// profile, spawns a background worker that performs the full blocking connect
// (TCP + TLS + RFC 6455 handshake, ≤10s), and returns immediately with
// readyState=CONNECTING. The worker's outcome lands in an
// `Arc<Mutex<Option<..>>>` slot — the ONLY cross-thread channel (no JSObject
// pointers cross threads; BCE-20260621-001 rule). The JS-thread drain pump
// (`ws_pump_all`, wired into `timers::drain_and_check` /
// `timers::drain_one_pass` / the servo node-realm evaluate entry) consumes
// the slot (onopen/onerror) and pumps inbound frames (onmessage/onclose)
// with the sockets in non-blocking mode.

#[derive(Debug)]
#[allow(dead_code)]
enum WsMessage {
    Text(String),
    Binary(Vec<u8>),
    Close,
}

/// A TLS-over-TCP adapter implementing `std::io::{Read, Write}`. It owns the
/// raw `TcpStream` plus a BoringSSL `TlsConnection` and transparently drives
/// the TLS state machine (handshake + record decrypt/encrypt) on every I/O.
///
/// `bun_uws`'s `ws_handshake::client_handshake<S: Read + Write>` and
/// `ws_codec::FrameDecoder` consume this directly, so the wss:// path reuses
/// the exact same RFC 6455 code as the ws:// path.
struct TlsStream {
    tcp: TcpStream,
    tls: bao_boringssl_bridge::connection::TlsConnection,
    /// Decrypted plaintext not yet handed to the reader. `Read` callers (the
    /// WS handshake reads byte-at-a-time) may take less than one TLS record
    /// per read(); the surplus must survive across calls (BCE-20260814-WS-TLS:
    /// the prior adapter dropped it, corrupting the handshake).
    pending_plain: Vec<u8>,
    pending_off: usize,
}

impl TlsStream {
    /// Pump the TLS state machine: flush any pending outgoing ciphertext to the
    /// socket, then process inbound records until the TLS layer has decrypted
    /// data ready (or WouldBlock). Returns the decrypted plaintext bytes.
    fn pump_inbound(&mut self) -> ::std::io::Result<Vec<u8>> {
        loop {
            // Drain any ciphertext BoringSSL wants to send first so a
            // mid-handshake flight isn't stranded in the write BIO.
            self.flush_outgoing()?;
            let res = self.tls.process().map_err(|e| {
                ::std::io::Error::new(::std::io::ErrorKind::InvalidData, e.to_string())
            })?;
            if !res.plaintext.is_empty() {
                let mut joined = Vec::new();
                for chunk in res.plaintext {
                    joined.extend_from_slice(&chunk);
                }
                return Ok(joined);
            }
            // No decrypted data yet — read more ciphertext from the socket.
            let mut buf = [0u8; 16_384];
            match self.tcp.read(&mut buf) {
                Ok(0) => {
                    return Err(::std::io::Error::new(
                        ::std::io::ErrorKind::UnexpectedEof,
                        "tls peer closed",
                    ));
                }
                Ok(n) => self.tls.feed(&buf[..n]),
                Err(ref e)
                    if e.kind() == ::std::io::ErrorKind::WouldBlock
                        || e.kind() == ::std::io::ErrorKind::TimedOut =>
                {
                    return Err(::std::io::Error::from(::std::io::ErrorKind::WouldBlock));
                }
                Err(e) => return Err(e),
            }
        }
    }

    /// Write any pending ciphertext from BoringSSL's write BIO to the socket.
    fn flush_outgoing(&mut self) -> ::std::io::Result<()> {
        let outgoing = self.tls.take_outgoing();
        if outgoing.is_empty() {
            return Ok(());
        }
        self.tcp.write_all(&outgoing)
    }
}

impl ::std::io::Read for TlsStream {
    fn read(&mut self, buf: &mut [u8]) -> ::std::io::Result<usize> {
        // Serve buffered plaintext first; only pump the TLS state machine
        // when the buffer is drained (records can exceed the caller's buf).
        if self.pending_off >= self.pending_plain.len() {
            self.pending_plain = self.pump_inbound()?;
            self.pending_off = 0;
        }
        let avail = &self.pending_plain[self.pending_off..];
        let n = avail.len().min(buf.len());
        buf[..n].copy_from_slice(&avail[..n]);
        self.pending_off += n;
        Ok(n)
    }
}

impl ::std::io::Write for TlsStream {
    fn write(&mut self, buf: &[u8]) -> ::std::io::Result<usize> {
        let written = self
            .tls
            .write(buf)
            .map_err(|e| ::std::io::Error::new(::std::io::ErrorKind::InvalidData, e.to_string()))?;
        self.flush_outgoing()?;
        Ok(written)
    }
    fn flush(&mut self) -> ::std::io::Result<()> {
        self.tcp.flush()
    }
}

/// Connection backend — plain ws:// over TCP, or wss:// over TLS.
enum WsConn {
    /// Plain WebSocket reusing `bun_uws::WebSocketClient` (RFC 6455 codec +
    /// handshake + masked client→server frames, all owned by bun_uws).
    Plain(bun_uws::ws_client::WebSocketClient),
    /// TLS WebSocket: a `TlsStream` driven through `bun_uws`'s codec/handshake.
    Tls {
        stream: TlsStream,
        decoder: bun_uws::ws_codec::FrameDecoder,
        closed: bool,
    },
}

impl WsConn {
    /// Connect to a `ws://` or `wss://` URL, applying the caller's stealth
    /// profile to the wss:// TLS handshake. Runs entirely on the caller's
    /// thread — the JS bridge only invokes this on a background worker.
    fn connect(
        url: &str,
        profile: &::std::option::Option<bao_stealth::StealthProfile>,
    ) -> ::std::result::Result<Self, String> {
        let (scheme, rest) = if let Some(r) = url.strip_prefix("ws://") {
            ("ws", r)
        } else if let Some(r) = url.strip_prefix("wss://") {
            ("wss", r)
        } else {
            // Fall back to ws:// semantics for bare hosts (preserves the prior
            // behavior where a scheme-less URL was treated as ws://).
            ("ws", url)
        };

        let (host, port, path) = split_authority_and_path(rest, scheme);
        if scheme == "wss" {
            Self::connect_tls(&host, port, &path, profile)
        } else {
            // ws:// — delegate to bun_uws::WebSocketClient (reconstructs the
            // canonical URL because bun_uws::parse_ws_url is scheme-strict).
            let canonical = if url.starts_with("ws://") || url.starts_with("wss://") {
                url.to_string()
            } else {
                format!("ws://{}", url)
            };
            let client = bun_uws::ws_client::WebSocketClient::connect(&canonical)
                .map_err(|e| format!("ws connect: {}", e))?;
            Ok(WsConn::Plain(client))
        }
    }

    fn connect_tls(
        host: &str,
        port: u16,
        path: &str,
        profile: &::std::option::Option<bao_stealth::StealthProfile>,
    ) -> ::std::result::Result<Self, String> {
        let addr = format!("{}:{}", host, port);
        let socket_addr = addr
            .to_socket_addrs()
            .map_err(|e| format!("invalid address: {}", e))?
            .next()
            .ok_or_else(|| format!("no address for {}", addr))?;
        let mut tcp = TcpStream::connect_timeout(&socket_addr, Duration::from_secs(10))
            .map_err(|e| format!("connect failed: {}", e))?;
        tcp.set_nonblocking(false).ok();
        tcp.set_read_timeout(Some(Duration::from_secs(10))).ok();

        // Build the BoringSSL client connection and drive the TLS handshake.
        let tls_client = bao_boringssl_bridge::client::TlsClient::new()
            .map_err(|e| format!("tls client init: {}", e))?;
        let mut tls =
            bao_boringssl_bridge::connection::TlsConnection::new_client(&tls_client, host)
                .map_err(|e| format!("tls conn: {}", e))?;

        // STEALTH (REQ-STL-001): apply the page's TLS fingerprint through the
        // same application path fetch() uses — cipher list / TLS 1.3 suites /
        // curves / sigalgs, plus SNI and ALPN(http/1.1) (what a browser
        // offers on a WebSocket TLS connection). Must run BEFORE the first
        // `process()` call so the config lands in the ClientHello.
        let ssl_config = crate::stealth_http::stealth_profile_to_ssl_config(profile);
        let host_c = CString::new(host).map_err(|_| format!("invalid host: {}", host))?;
        {
            let ssl = tls.ssl_ptr();
            if !ssl.is_null() {
                // SAFETY: `ssl_ptr` returns the live SSL handle of this
                // connection; `configure_http_client_with_alpn` only issues
                // SSL_set_* configuration calls on it.
                bun_http::configure_http_client_with_alpn(
                    unsafe { &mut *ssl },
                    host_c.as_ptr(),
                    bun_http::AlpnOffer::H1,
                    Some(&ssl_config),
                );

                // TLS session resumption: offer the cached session for this
                // origin before the handshake starts (same precondition as
                // the stealth config above — the ClientHello has not been
                // serialized yet). Salt semantics match the bun_http fetch
                // path: no stealth profile → salt 0 (the default-profile
                // pool shared across stacks); a profile → the SSLConfig
                // content hash, so sessions that short-circuit parameter
                // negotiation never cross profiles.
                let profile_salt = if profile.is_some() {
                    ssl_config.content_hash()
                } else {
                    0
                };
                bao_boringssl_bridge::session_cache::offer_session(ssl, host, port, profile_salt);
            }
        }

        // Complete the TLS handshake by pumping records until active.
        // BCE-20260814-WS-TLS: the flight produced by `process()` MUST be
        // flushed to the socket BEFORE blocking on read — the prior order
        // (take_outgoing → process → read) left the ClientHello stranded in
        // the write BIO while waiting for a ServerHello that could never
        // arrive (both sides reading → deadlock, surfaced as "handshake
        // stalled" after the 10s timeout).
        loop {
            match tls.process() {
                Ok(res) => {
                    use bao_boringssl_bridge::connection::TlsState;
                    // Flush every flight the state machine just produced
                    // (ClientHello / Finished) before waiting on the peer.
                    loop {
                        let outgoing = tls.take_outgoing();
                        if outgoing.is_empty() {
                            break;
                        }
                        if tcp.write_all(&outgoing).is_err() {
                            return Err("tls handshake write failed".to_string());
                        }
                    }
                    if res.state == TlsState::Active || res.state == TlsState::PeerClosed {
                        break;
                    }
                    // Still handshaking — read more ciphertext from the socket.
                    let mut buf = [0u8; 16_384];
                    match tcp.read(&mut buf) {
                        Ok(n) if n > 0 => tls.feed(&buf[..n]),
                        _ => {
                            return Err("tls handshake stalled".to_string());
                        }
                    }
                }
                Err(e) => return Err(format!("tls handshake: {}", e)),
            }
        }

        let mut stream = TlsStream {
            tcp,
            tls,
            pending_plain: Vec::new(),
            pending_off: 0,
        };
        // RFC 6455 client handshake over the TLS stream (bun_uws-owned).
        bun_uws::ws_handshake::client_handshake(&mut stream, host, &path)
            .map_err(|e| format!("ws handshake: {:?}", e))?;
        Ok(WsConn::Tls {
            stream,
            decoder: bun_uws::ws_codec::FrameDecoder::new(),
            closed: false,
        })
    }

    fn send_text(&mut self, text: &str) -> ::std::result::Result<(), String> {
        match self {
            WsConn::Plain(c) => c.send_text(text).map_err(|e| format!("send failed: {}", e)),
            WsConn::Tls { stream, .. } => {
                let payload = text.as_bytes();
                let key = bun_uws::ws_codec::gen_mask_key();
                let mut frame = Vec::with_capacity(payload.len() + 14);
                frame.push(0x81); // FIN + text opcode
                push_masked_len(&mut frame, payload.len());
                frame.extend_from_slice(&key);
                let mut masked = payload.to_vec();
                apply_mask(&mut masked, &key);
                // BCE-20260814-WS-TLS: the masked payload was never appended
                // to the frame — every wss:// send() transmitted header+key
                // only, dropping the message body (peer then misparsed the
                // next frame's bytes as this frame's payload).
                frame.extend_from_slice(&masked);
                stream
                    .write_all(&frame)
                    .map_err(|e| format!("send failed: {}", e))
            }
        }
    }

    fn read_message(&mut self) -> ::std::result::Result<WsMessage, String> {
        match self {
            WsConn::Plain(c) => match c.recv().map_err(|e| format!("recv: {}", e))? {
                bun_uws::ws_client::RecvOutcome::Message(opcode, payload) => match opcode {
                    bun_uws::ws_codec::Opcode::Text => Ok(WsMessage::Text(
                        String::from_utf8_lossy(&payload).into_owned(),
                    )),
                    bun_uws::ws_codec::Opcode::Binary => Ok(WsMessage::Binary(payload)),
                    _ => Ok(WsMessage::Binary(payload)),
                },
                bun_uws::ws_client::RecvOutcome::Closed => Ok(WsMessage::Close),
                bun_uws::ws_client::RecvOutcome::Timeout => Err("wouldblock".to_string()),
            },
            WsConn::Tls {
                stream,
                decoder,
                closed,
            } => {
                if *closed {
                    return Ok(WsMessage::Close);
                }
                let header = match decoder.decode_frame(stream) {
                    Ok(Some(h)) => h,
                    Ok(None) => return Err("wouldblock".to_string()),
                    Err(ref e)
                        if e.kind() == ::std::io::ErrorKind::WouldBlock
                            || e.kind() == ::std::io::ErrorKind::TimedOut =>
                    {
                        return Err("wouldblock".to_string());
                    }
                    Err(ref e) if e.kind() == ::std::io::ErrorKind::UnexpectedEof => {
                        *closed = true;
                        return Ok(WsMessage::Close);
                    }
                    Err(e) => return Err(format!("recv: {}", e)),
                };
                let mut payload = if header.mask {
                    let mask_key = decoder.take_mask();
                    let mut p = decoder.take_payload(&header);
                    apply_mask(&mut p, &mask_key);
                    p
                } else {
                    decoder.take_payload(&header)
                };
                match header.opcode {
                    bun_uws::ws_codec::Opcode::Text => Ok(WsMessage::Text(
                        String::from_utf8_lossy(&payload).into_owned(),
                    )),
                    bun_uws::ws_codec::Opcode::Binary => Ok(WsMessage::Binary(payload)),
                    bun_uws::ws_codec::Opcode::Close => {
                        *closed = true;
                        Ok(WsMessage::Close)
                    }
                    bun_uws::ws_codec::Opcode::Ping => {
                        // Echo pong (RFC 6455 §5.5.2) using bun_uws codec mask.
                        let key = bun_uws::ws_codec::gen_mask_key();
                        let mut frame = vec![0x8A]; // FIN + pong
                        push_masked_len(&mut frame, payload.len());
                        frame.extend_from_slice(&key);
                        apply_mask(&mut payload, &key);
                        frame.extend_from_slice(&payload);
                        stream
                            .write_all(&frame)
                            .map_err(|e| format!("pong: {}", e))?;
                        self.read_message()
                    }
                    bun_uws::ws_codec::Opcode::Pong | bun_uws::ws_codec::Opcode::Continuation => {
                        self.read_message()
                    }
                }
            }
        }
    }

    /// Send a masked close frame (code 1000). Deliberately does NOT delegate
    /// to `WebSocketClient::close()` for the Plain variant — that method
    /// `shutdown(Both)`s the socket immediately, killing the TCP connection
    /// before the peer's Close reply can arrive (no close handshake). Here
    /// only the frame is sent; the socket stays readable so the drain pump
    /// can observe the peer's Close reply and fire onclose.
    fn close(&mut self) -> ::std::result::Result<(), String> {
        let frame = encode_masked_close_frame();
        match self {
            WsConn::Plain(c) => {
                if c.is_closed() {
                    return Ok(());
                }
                c.stream_mut()
                    .write_all(&frame)
                    .map_err(|e| format!("close failed: {}", e))
            }
            WsConn::Tls { stream, .. } => {
                // Send the close frame only. Do NOT set the internal `closed`
                // flag here — `read_message` uses it to short-circuit, which
                // would discard inbound frames still in flight (post-close
                // messages are delivered until the close handshake completes,
                // browser semantics). The flag flips when the peer's Close
                // frame is actually read.
                let _ = stream.write_all(&frame);
                Ok(())
            }
        }
    }

    /// Switch the underlying socket between blocking and non-blocking so the
    /// initial drain loop can poll for buffered frames without hanging.
    fn set_nonblocking(&mut self, nonblocking: bool) {
        match self {
            WsConn::Plain(c) => {
                let _ = c.stream_mut().set_nonblocking(nonblocking);
            }
            WsConn::Tls { stream, .. } => {
                let _ = stream.tcp.set_nonblocking(nonblocking);
            }
        }
    }
}

/// Split `host[:port]/path` from the scheme-stripped remainder. Default port
/// is 80 for ws://, 443 for wss://.
fn split_authority_and_path(rest: &str, scheme: &str) -> (String, u16, String) {
    let default_port = if scheme == "wss" { 443 } else { 80 };
    let (authority, path) = match rest.find('/') {
        Some(i) => (&rest[..i], rest[i..].to_string()),
        None => (rest, "/".to_string()),
    };
    let (host, port) = match authority.rfind(':') {
        Some(i) => (
            authority[..i].to_string(),
            authority[i + 1..].parse::<u16>().unwrap_or(default_port),
        ),
        None => (authority.to_string(), default_port),
    };
    (host, port, path)
}

/// Masked close frame (FIN + Close, code 1000), client→server layout.
fn encode_masked_close_frame() -> Vec<u8> {
    let key = bun_uws::ws_codec::gen_mask_key();
    let mut frame = vec![0x88];
    let payload = 1000u16.to_be_bytes();
    push_masked_len(&mut frame, payload.len());
    frame.extend_from_slice(&key);
    let mut masked = payload.to_vec();
    apply_mask(&mut masked, &key);
    frame.extend_from_slice(&masked);
    frame
}

/// Append the masked-length + (caller-supplied) mask bytes layout for a
/// client→server frame, matching `bun_uws::ws_codec::FrameEncoder::encode_frame`.
fn push_masked_len(frame: &mut Vec<u8>, len: usize) {
    let mask_bit = 0x80u8;
    if len < 126 {
        frame.push((len as u8) | mask_bit);
    } else if len <= u16::MAX as usize {
        frame.push(126u8 | mask_bit);
        frame.extend_from_slice(&(len as u16).to_be_bytes());
    } else {
        frame.push(127u8 | mask_bit);
        frame.extend_from_slice(&(len as u64).to_be_bytes());
    }
}

// ── JS bridge ──

/// Global counter for generating unique GcStore keys for WebSocket objects.
static WS_ID_COUNTER: AtomicU64 = AtomicU64::new(0);

/// Per-connection registry entry. Everything except `connect_slot`'s payload
/// is confined to the thread that constructed the WebSocket (the JS thread);
/// the background connect worker communicates exclusively through the
/// `Arc<Mutex<..>>` slot — no JSObject pointers cross threads
/// (BCE-20260621-001 rule).
struct WsEntry {
    /// Live connection once the background connect completed and onopen
    /// dispatched. Polled non-blocking by `ws_pump_all`.
    client: ::std::option::Option<WsConn>,
    /// Present while the background connect worker is in flight. The worker
    /// writes `Some(Ok(..))` / `Some(Err(..))` exactly once; the JS-thread
    /// drain pump consumes it. `None` + `client: None` = dead entry.
    connect_slot: ::std::option::Option<
        Arc<Mutex<::std::option::Option<::std::result::Result<WsConn, String>>>>,
    >,
    /// JS called close() while still CONNECTING — when the connect lands, the
    /// pump closes it immediately instead of firing onopen.
    close_requested: bool,
    /// JS called close() on an open connection — the close frame was sent;
    /// the pump fires onclose when the peer's Close reply (or transport
    /// error) lands (browser close-handshake semantics).
    close_initiated: bool,
    /// The realm global the WebSocket JS object lives in (captured at
    /// construction). Every dispatch AutoRealms into it (realm-per-context
    /// model, c943b1cc) so the GcStore property lookup and handler call run
    /// in the right compartment.
    realm_global: *mut JSObject,
    js_obj_key: String,
}

impl WsEntry {
    fn is_live(&self) -> bool {
        self.connect_slot.is_some() || self.client.is_some()
    }
}

thread_local! {
    static WS_CONNECTIONS: RefCell<Vec<WsEntry>> = const { RefCell::new(Vec::new()) };
}

/// True while any WebSocket on this thread is connecting or open. Wired into
/// the event-loop liveness checks (`timers::drain_and_check` return value) so
/// the eval loop keeps draining while WS traffic is in flight.
pub fn ws_has_pending() -> bool {
    WS_CONNECTIONS.with(|c| c.borrow().iter().any(|e| e.is_live()))
}

pub fn install_websocket_constructor(
    cx: &mut mozjs::context::JSContext,
    global: mozjs::rust::Handle<*mut JSObject>,
) {
    unsafe {
        let ws_fun = JS_NewFunction(
            cx.raw_cx(),
            Some(websocket_constructor),
            1,
            JSFUN_CONSTRUCTOR,
            c"WebSocket".as_ptr(),
        );
        if !ws_fun.is_null() {
            let ctor_obj = JS_GetFunctionObject(ws_fun);
            if !ctor_obj.is_null() {
                let val = mozjs::jsval::ObjectValue(ctor_obj);
                rooted!(&in(cx) let v = val);
                JS_DefineProperty(
                    cx.raw_cx(),
                    global.into(),
                    c"WebSocket".as_ptr(),
                    v.handle().into(),
                    (JSPROP_ENUMERATE | JSPROP_PERMANENT) as u32,
                );

                rooted!(&in(cx) let ctor_root = ctor_obj);
                for (name, value) in &[
                    ("CONNECTING", 0i32),
                    ("OPEN", 1),
                    ("CLOSING", 2),
                    ("CLOSED", 3),
                ] {
                    let c_name = ZBox::from_bytes(name.as_bytes());
                    rooted!(&in(cx) let iv = Int32Value(*value));
                    JS_DefineProperty(
                        cx.raw_cx(),
                        ctor_root.handle().into(),
                        c_name.as_ptr(),
                        iv.handle().into(),
                        (JSPROP_ENUMERATE | JSPROP_READONLY) as u32,
                    );
                }
            }
        }
    }
}

/// Fire an `onXXX` handler stored on the WebSocket object. `realm_global` is
/// the realm global captured at construction; dispatch AutoRealms into it
/// (the drain pump runs with no realm entered — realm-per-context model).
/// `this` for the call is the WebSocket object (browser semantics).
///
/// # Safety
/// `cx` must be a live JSContext on the current thread; `realm_global` must
/// be the (always-rooted) global of a live realm on that context.
unsafe fn ws_trigger_event(
    cx: *mut JSContext,
    realm_global: *mut JSObject,
    ws_obj_key: &str,
    event_name: &str,
    data_val: Option<JSVal>,
) {
    if realm_global.is_null() {
        return;
    }
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    rooted!(&in(cx_ref) let global_root = realm_global);
    let mut realm = AutoRealm::new_from_handle(cx_ref, global_root.handle());
    let cx_ref: &mut mozjs::context::JSContext = &mut realm;

    // Root the event data FIRST — gc_store_get / JS_GetProperty below can
    // trigger GC and an unrooted JSVal argument would dangle. Always root
    // (undefined placeholder when the event carries no data) so the guard
    // lives for the whole frame.
    let has_data = data_val.is_some();
    let dv_in = data_val.unwrap_or_else(UndefinedValue);
    rooted!(&in(cx_ref) let data_root = dv_in);

    let ws_obj = match gc_store_get(cx, ws_obj_key) {
        Some(obj) => obj,
        None => return,
    };
    rooted!(&in(cx_ref) let ws_obj_root = ws_obj);
    let mut handler_val = UndefinedValue();
    let c_name = ZBox::from_bytes(event_name.as_bytes());
    JS_GetProperty(
        cx,
        ws_obj_root.handle().into(),
        c_name.as_ptr(),
        MutableHandle::<Value> {
            _phantom_0: ::std::marker::PhantomData,
            ptr: &mut handler_val,
        },
    );
    if handler_val.is_object() {
        rooted!(&in(cx_ref) let handler_obj_root = handler_val.to_object());
        if JS_ObjectIsFunction(handler_obj_root.get()) {
            rooted!(&in(cx_ref) let handler_jsval = ObjectValue(handler_obj_root.get()));

            rooted!(&in(cx_ref) let event_obj = mozjs_sys::jsapi::JS_NewPlainObject(cx));
            if !event_obj.get().is_null() {
                if has_data {
                    JS_DefineProperty(
                        cx,
                        event_obj.handle().into(),
                        c"data".as_ptr(),
                        data_root.handle().into(),
                        JSPROP_ENUMERATE as u32,
                    );
                }
                let ev_val = ObjectValue(event_obj.get());
                let call_args = HandleValueArray {
                    length_: 1,
                    elements_: &ev_val,
                };
                let mut rval = UndefinedValue();
                let ok = JS_CallFunctionValue(
                    cx,
                    ws_obj_root.handle().into(),
                    handler_jsval.handle().into(),
                    &call_args,
                    MutableHandle::<Value> {
                        _phantom_0: ::std::marker::PhantomData,
                        ptr: &mut rval,
                    },
                );
                if !ok {
                    // BCE (P0 browser startup panic, servo error.rs:74): the
                    // user handler threw. The old `let _ =` left the pending
                    // exception on the ScriptThread context (browser mode
                    // kills the ScriptThread via servo's
                    // `assert!(!JS_IsExceptionPending)`; node mode silently
                    // swallowed the throw). Capture, clear, and route it —
                    // same contract as timers.rs fire_callback.
                    let mut exn = UndefinedValue();
                    JS_GetPendingException(
                        cx,
                        MutableHandle::<Value> {
                            _phantom_0: ::std::marker::PhantomData,
                            ptr: &mut exn,
                        },
                    );
                    JS_ClearPendingException(cx);
                    rooted!(&in(cx_ref) let reason_root = exn);
                    if !exn.is_undefined() {
                        crate::uncaught::route_uncaught_exception(cx, exn);
                    }
                }
            }
        }
    }
}

unsafe extern "C" fn ws_send(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    if argc == 0 {
        JS_ReportErrorUTF8(cx, c"WebSocket.send() requires a message argument".as_ptr());
        return false;
    }
    let msg_val = *args.get(0).ptr;

    let wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    rooted!(&in(wrapped_cx) let this_obj = args.thisv().to_object());
    let mut idx_val = Int32Value(-1);
    JS_GetProperty(
        cx,
        this_obj.handle().into(),
        c"_wsIdx".as_ptr(),
        MutableHandle::<Value> {
            _phantom_0: ::std::marker::PhantomData,
            ptr: &mut idx_val,
        },
    );
    let idx = idx_val.to_int32() as usize;

    let send_result = WS_CONNECTIONS.with(|c| {
        let mut conns = c.borrow_mut();
        match conns.get_mut(idx) {
            // Browser parity: send() while CONNECTING/CLOSED throws
            // InvalidStateError (never silently drops the message).
            Some(e) if e.client.is_some() => {
                let s = unsafe_jsstr_to_string(cx, NonNull::new_unchecked(msg_val.to_string()));
                e.client.as_mut().unwrap().send_text(&s)
            }
            Some(e) if e.connect_slot.is_some() => {
                Err("InvalidStateError: WebSocket is still connecting".to_string())
            }
            _ => Err("InvalidStateError: WebSocket is already closed".to_string()),
        }
    });

    if let Err(e) = send_result {
        let msg = format!("WebSocket send failed: {}", e);
        let c_msg = ZBox::from_bytes(msg.as_bytes());
        JS_ReportErrorUTF8(cx, c"%s".as_ptr(), c_msg.as_ptr());
        return false;
    }
    args.rval().set(UndefinedValue());
    true
}

unsafe extern "C" fn ws_close_fn(cx: *mut JSContext, _argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, _argc);
    let wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    rooted!(&in(wrapped_cx) let this_obj = args.thisv().to_object());

    let mut idx_val = Int32Value(-1);
    JS_GetProperty(
        cx,
        this_obj.handle().into(),
        c"_wsIdx".as_ptr(),
        MutableHandle::<Value> {
            _phantom_0: ::std::marker::PhantomData,
            ptr: &mut idx_val,
        },
    );
    let idx = idx_val.to_int32() as usize;

    WS_CONNECTIONS.with(|c| {
        let mut conns = c.borrow_mut();
        if let Some(e) = conns.get_mut(idx) {
            if e.client.is_some() && !e.close_initiated {
                // Send the close frame once; keep the socket alive so the
                // pump can see the peer's Close reply and fire onclose
                // (close handshake).
                if let Some(client) = &mut e.client {
                    let _ = client.close();
                }
                e.close_initiated = true;
            } else if e.connect_slot.is_some() {
                // Still CONNECTING — flag it; the pump closes immediately
                // when the background connect lands (no onopen).
                e.close_requested = true;
            }
        }
    });

    rooted!(&in(wrapped_cx) let closing_val = Int32Value(2));
    JS_SetProperty(
        cx,
        this_obj.handle().into(),
        c"readyState".as_ptr(),
        closing_val.handle().into(),
    );
    args.rval().set(UndefinedValue());
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn websocket_constructor(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    if argc == 0 {
        JS_ReportErrorUTF8(cx, c"WebSocket requires a URL argument".as_ptr());
        return false;
    }
    let url_val = *args.get(0).ptr;
    if !url_val.is_string() {
        JS_ReportErrorUTF8(cx, c"WebSocket URL must be a string".as_ptr());
        return false;
    }
    let url = unsafe_jsstr_to_string(cx, NonNull::new_unchecked(url_val.to_string()));

    let wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    rooted!(&in(wrapped_cx) let ws_obj = mozjs_sys::jsapi::JS_NewPlainObject(cx));
    if ws_obj.get().is_null() {
        args.rval().set(UndefinedValue());
        return true;
    }

    {
        let c_url = ZBox::from_bytes(url.as_bytes());
        let js_str = JS_NewStringCopyZ(cx, c_url.as_ptr());
        if !js_str.is_null() {
            rooted!(&in(wrapped_cx) let v = StringValue(&*js_str));
            JS_DefineProperty(
                cx,
                ws_obj.handle().into(),
                c"url".as_ptr(),
                v.handle().into(),
                JSPROP_ENUMERATE as u32,
            );
        }
    }

    rooted!(&in(wrapped_cx) let state_val = Int32Value(0));
    JS_DefineProperty(
        cx,
        ws_obj.handle().into(),
        c"readyState".as_ptr(),
        state_val.handle().into(),
        JSPROP_ENUMERATE as u32,
    );

    rooted!(&in(wrapped_cx) let ba_val = Int32Value(0));
    JS_DefineProperty(
        cx,
        ws_obj.handle().into(),
        c"bufferedAmount".as_ptr(),
        ba_val.handle().into(),
        JSPROP_ENUMERATE as u32,
    );

    for name in &["onopen", "onmessage", "onerror", "onclose"] {
        let c_name = ZBox::from_bytes(name.as_bytes());
        rooted!(&in(wrapped_cx) let ud = UndefinedValue());
        JS_DefineProperty(
            cx,
            ws_obj.handle().into(),
            c_name.as_ptr(),
            ud.handle().into(),
            JSPROP_ENUMERATE as u32,
        );
    }

    mozjs_sys::jsapi::JS_DefineFunction(
        cx,
        ws_obj.handle().into(),
        c"send".as_ptr(),
        Some(ws_send),
        1,
        JSPROP_ENUMERATE as u32,
    );
    mozjs_sys::jsapi::JS_DefineFunction(
        cx,
        ws_obj.handle().into(),
        c"close".as_ptr(),
        Some(ws_close_fn),
        0,
        JSPROP_ENUMERATE as u32,
    );

    // Permission parity with fetch(): the page's net scope governs
    // WebSocket egress too (Permission sandbox).
    {
        let (scheme, rest) = if let Some(r) = url.strip_prefix("ws://") {
            ("ws", r)
        } else if let Some(r) = url.strip_prefix("wss://") {
            ("wss", r)
        } else {
            ("ws", url.as_str())
        };
        let (host, _, _) = split_authority_and_path(rest, scheme);
        if let ::std::result::Result::Err(e) = crate::permission_bridge::check_net(&host) {
            let c_msg = ZBox::from_bytes(e.as_bytes());
            JS_ReportErrorUTF8(cx, c"%s".as_ptr(), c_msg.as_ptr());
            return false;
        }
    }

    // Store the JS WebSocket object in GcStore for GC safety.
    let ws_id = WS_ID_COUNTER.fetch_add(1, Ordering::Relaxed);
    let ws_key = format!("ws_{}", ws_id);
    gc_store_insert(cx, &ws_key, ws_obj.get());

    // Capture the realm global (dispatch AutoRealm target) and the thread's
    // stealth profile HERE, on the JS thread — both are thread-local state.
    let realm_global = CurrentGlobalOrNull(cx);
    let profile = crate::fetch_api::get_fetch_stealth_profile();

    // Register the entry with a pending connect slot; _wsIdx must exist
    // immediately (send()/close() may be called while CONNECTING).
    let slot: Arc<Mutex<::std::option::Option<::std::result::Result<WsConn, String>>>> =
        Arc::new(Mutex::new(None));
    let ws_idx = WS_CONNECTIONS.with(|c| {
        let mut conns = c.borrow_mut();
        conns.push(WsEntry {
            client: None,
            connect_slot: Some(Arc::clone(&slot)),
            close_requested: false,
            close_initiated: false,
            realm_global,
            js_obj_key: ws_key.clone(),
        });
        conns.len() - 1
    });
    rooted!(&in(wrapped_cx) let idx_val = Int32Value(ws_idx as i32));
    JS_DefineProperty(
        cx,
        ws_obj.handle().into(),
        c"_wsIdx".as_ptr(),
        idx_val.handle().into(),
        0,
    );

    // Background connect: the full blocking sequence (TCP + TLS with the
    // page's stealth fingerprint + RFC 6455 handshake, ≤10s) runs OFF the JS
    // thread. The Arc<Mutex> slot is the only cross-thread channel; the
    // JS-thread drain pump (`ws_pump_all`) consumes the outcome and fires
    // onopen / onerror(+onclose). No JSObject pointer crosses threads.
    let url_owned = url.clone();
    ::std::thread::spawn(move || {
        let result = WsConn::connect(&url_owned, &profile);
        if let Ok(mut guard) = slot.lock() {
            *guard = Some(result);
        }
        // If the JS thread is gone (process teardown), the slot leaks —
        // bounded by the 10s connect timeout.
    });

    // readyState stays 0 (CONNECTING). The constructor returns immediately —
    // connect failures surface as onerror + onclose (browser semantics),
    // never a constructor throw, never a silent swallow.
    args.rval().set(mozjs::jsval::ObjectValue(ws_obj.get()));
    true
}

// ── WebSocket drain pump ──
// Runs on the JS thread from the event-loop drain paths. Consumes completed
// background connects and pumps inbound frames. Never blocks (try_lock +
// non-blocking sockets); JS handlers run OUTSIDE the WS_CONNECTIONS borrow so
// they can call send()/close() reentrantly.

/// One action peeled off the registry per iteration (JS calls happen after
/// the borrow is dropped).
enum PumpAction {
    /// Background connect finished (slot outcome consumed).
    ConnectDone(usize, ::std::result::Result<WsConn, String>),
    /// Text frame received on an open connection.
    TextMessage(usize, String),
    /// Binary frame received on an open connection.
    BinaryMessage(usize, Vec<u8>),
    /// Close frame received / transport error — connection is dead.
    /// `msg` is Some for a transport error (fires onerror first), None for a
    /// clean close handshake.
    Closed(usize, ::std::option::Option<String>),
}

/// Pump all WebSockets on this thread. Called from `timers::drain_and_check`,
/// `timers::drain_one_pass`, and the servo node-realm evaluate entry.
pub fn ws_pump_all(raw_cx: *mut JSContext) {
    loop {
        let action = WS_CONNECTIONS.with(|c| {
            let mut conns = c.borrow_mut();
            for (idx, e) in conns.iter_mut().enumerate() {
                // 1. Completed background connects (try_lock — the worker
                //    may still hold the lock writing its outcome). Take the
                //    slot out first so the MutexGuard borrow ends before the
                //    assignment below.
                if e.connect_slot.is_some() {
                    let taken = e
                        .connect_slot
                        .as_ref()
                        .unwrap()
                        .try_lock()
                        .ok()
                        .and_then(|mut guard| guard.take());
                    if let ::std::option::Option::Some(res) = taken {
                        e.connect_slot = ::std::option::Option::None;
                        return ::std::option::Option::Some(PumpAction::ConnectDone(idx, res));
                    }
                    continue;
                }
                // 2. Inbound frames on open connections (non-blocking).
                //    Frames received after a locally-initiated close are
                //    still delivered (browser semantics: messages queue
                //    until the close handshake completes).
                let ::std::option::Option::Some(client) = &mut e.client else {
                    continue;
                };
                match client.read_message() {
                    Ok(WsMessage::Text(t)) => {
                        return ::std::option::Option::Some(PumpAction::TextMessage(idx, t))
                    }
                    Ok(WsMessage::Binary(b)) => {
                        return ::std::option::Option::Some(PumpAction::BinaryMessage(idx, b))
                    }
                    Ok(WsMessage::Close) => {
                        // Echo the close handshake unless we already sent
                        // our close frame (close_initiated).
                        if !e.close_initiated {
                            let _ = client.close();
                        }
                        e.client = ::std::option::Option::None;
                        return ::std::option::Option::Some(PumpAction::Closed(idx, None));
                    }
                    Err(err) if err == "wouldblock" => continue,
                    Err(err) => {
                        // Transport error — explicit surface, never silent.
                        let _ = client.close();
                        e.client = ::std::option::Option::None;
                        return ::std::option::Option::Some(PumpAction::Closed(
                            idx,
                            ::std::option::Option::Some(err),
                        ));
                    }
                }
            }
            ::std::option::Option::None
        });

        match action {
            ::std::option::Option::Some(PumpAction::ConnectDone(idx, res)) => unsafe {
                ws_connect_dispatch(raw_cx, idx, res);
            },
            ::std::option::Option::Some(PumpAction::TextMessage(idx, text)) => unsafe {
                ws_message_dispatch(raw_cx, idx, ::std::option::Option::Some(text), None);
            },
            ::std::option::Option::Some(PumpAction::BinaryMessage(idx, bytes)) => unsafe {
                ws_message_dispatch(raw_cx, idx, None, ::std::option::Option::Some(bytes));
            },
            ::std::option::Option::Some(PumpAction::Closed(idx, err)) => unsafe {
                ws_closed_dispatch(raw_cx, idx, err);
            },
            ::std::option::Option::None => break,
        }
    }
}

/// Snapshot of the dispatch-relevant fields of one entry.
struct WsDispatchInfo {
    realm_global: *mut JSObject,
    js_obj_key: String,
    is_open: bool,
}

fn ws_entry_info(idx: usize) -> ::std::option::Option<WsDispatchInfo> {
    WS_CONNECTIONS.with(|c| {
        c.borrow().get(idx).map(|e| WsDispatchInfo {
            realm_global: e.realm_global,
            js_obj_key: e.js_obj_key.clone(),
            is_open: e.client.is_some(),
        })
    })
}

/// Set `readyState` on the stored WebSocket object (inside its realm).
///
/// # Safety
/// `raw_cx` must be a live JSContext on the current thread.
unsafe fn ws_set_ready_state(
    raw_cx: *mut JSContext,
    realm_global: *mut JSObject,
    ws_key: &str,
    state: i32,
) {
    if realm_global.is_null() {
        return;
    }
    // Enter the realm FIRST — gc_store_get resolves through
    // CurrentGlobalOrNull, which is null while the drain pump runs.
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(raw_cx));
    let cx_ref = &mut wrapped_cx;
    rooted!(&in(cx_ref) let global_root = realm_global);
    let mut realm = AutoRealm::new_from_handle(cx_ref, global_root.handle());
    let cx_ref: &mut mozjs::context::JSContext = &mut realm;
    let ws_obj = match gc_store_get(raw_cx, ws_key) {
        Some(o) => o,
        None => return,
    };
    rooted!(&in(cx_ref) let ws_obj_root = ws_obj);
    rooted!(&in(cx_ref) let state_val = Int32Value(state));
    JS_SetProperty(
        raw_cx,
        ws_obj_root.handle().into(),
        c"readyState".as_ptr(),
        state_val.handle().into(),
    );
}

/// Background connect completed: install the connection and fire onopen, or
/// surface the failure as onerror + onclose (explicit, never silent).
///
/// # Safety
/// `raw_cx` must be a live JSContext on the current thread.
unsafe fn ws_connect_dispatch(
    raw_cx: *mut JSContext,
    idx: usize,
    res: ::std::result::Result<WsConn, String>,
) {
    let info = match ws_entry_info(idx) {
        Some(i) => i,
        None => return,
    };
    match res {
        Ok(mut client) => {
            // Non-blocking from here on — the drain pump polls this socket.
            client.set_nonblocking(true);
            enum Landed {
                Open,
                CloseNow,
            }
            let landed = WS_CONNECTIONS.with(|c| {
                let mut conns = c.borrow_mut();
                match conns.get_mut(idx) {
                    // close() was called while CONNECTING: never open it.
                    ::std::option::Option::Some(e) if e.close_requested => {
                        e.close_requested = false;
                        Landed::CloseNow
                    }
                    ::std::option::Option::Some(e) => {
                        e.client = ::std::option::Option::Some(client);
                        Landed::Open
                    }
                    ::std::option::Option::None => Landed::CloseNow,
                }
            });
            match landed {
                Landed::Open => {
                    ws_set_ready_state(raw_cx, info.realm_global, &info.js_obj_key, 1);
                    ws_trigger_event(raw_cx, info.realm_global, &info.js_obj_key, "onopen", None);
                }
                Landed::CloseNow => {
                    // `client` was not installed; dropping it closes the
                    // socket (TcpStream Drop).
                    ws_set_ready_state(raw_cx, info.realm_global, &info.js_obj_key, 3);
                    ws_trigger_event(raw_cx, info.realm_global, &info.js_obj_key, "onclose", None);
                    gc_store_remove(raw_cx, &info.js_obj_key);
                }
            }
        }
        Err(msg) => {
            // Connect failed: CLOSED + onerror(with the reason) + onclose.
            ws_set_ready_state(raw_cx, info.realm_global, &info.js_obj_key, 3);
            // Enter the realm before creating the error string — allocation
            // needs a valid zone (the pump runs with no realm entered).
            let data = if !info.realm_global.is_null() {
                let mut wrapped_cx =
                    mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(raw_cx));
                let cx_ref = &mut wrapped_cx;
                rooted!(&in(cx_ref) let global_root = info.realm_global);
                let mut realm = AutoRealm::new_from_handle(cx_ref, global_root.handle());
                let _cx_in_realm: &mut mozjs::context::JSContext = &mut realm;
                let c_msg = ZBox::from_bytes(msg.as_bytes());
                let js_str = JS_NewStringCopyZ(raw_cx, c_msg.as_ptr());
                if !js_str.is_null() {
                    ::std::option::Option::Some(StringValue(&*js_str))
                } else {
                    ::std::option::Option::None
                }
            } else {
                ::std::option::Option::None
            };
            ws_trigger_event(raw_cx, info.realm_global, &info.js_obj_key, "onerror", data);
            ws_trigger_event(raw_cx, info.realm_global, &info.js_obj_key, "onclose", None);
            gc_store_remove(raw_cx, &info.js_obj_key);
        }
    }
}

/// Inbound frame on an open connection: fire onmessage. Text frames arrive
/// as strings; binary frames as an Array of byte values (explicit — binary
/// was previously dropped silently).
///
/// # Safety
/// `raw_cx` must be a live JSContext on the current thread.
unsafe fn ws_message_dispatch(
    raw_cx: *mut JSContext,
    idx: usize,
    text: ::std::option::Option<String>,
    binary: ::std::option::Option<Vec<u8>>,
) {
    let info = match ws_entry_info(idx) {
        Some(i) => i,
        None => return,
    };
    if !info.is_open {
        return;
    }
    if info.realm_global.is_null() {
        return;
    }
    // Enter the realm BEFORE any JS value creation — JS_NewStringCopyZ /
    // JS_NewUint8Array allocate in the current zone, and the drain pump runs
    // with no realm entered (invalid zone → SIGSEGV in the allocator).
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(raw_cx));
    let cx_ref = &mut wrapped_cx;
    rooted!(&in(cx_ref) let global_root = info.realm_global);
    let mut realm = AutoRealm::new_from_handle(cx_ref, global_root.handle());
    let cx_ref: &mut mozjs::context::JSContext = &mut realm;
    let data = if let ::std::option::Option::Some(t) = text {
        let c_text = ZBox::from_bytes(t.as_bytes());
        let js_str = JS_NewStringCopyZ(raw_cx, c_text.as_ptr());
        if js_str.is_null() {
            return;
        }
        StringValue(&*js_str)
    } else if let ::std::option::Option::Some(bytes) = binary {
        let arr = mozjs_sys::jsapi::JS_NewUint8Array(raw_cx, bytes.len());
        if arr.is_null() {
            return;
        }
        rooted!(&in(cx_ref) let arr_root = arr);
        if !bytes.is_empty() {
            let mut is_shared = false;
            // SAFETY: same pattern as bun_api.rs — data pointer of the
            // just-created, rooted Uint8Array; copied before any GC point.
            let data_ptr = mozjs_sys::jsapi::JS_GetUint8ArrayData(
                arr_root.get(),
                &mut is_shared,
                ::std::ptr::null(),
            );
            if !data_ptr.is_null() {
                ::std::ptr::copy_nonoverlapping(bytes.as_ptr(), data_ptr, bytes.len());
            }
        }
        ObjectValue(arr_root.get())
    } else {
        return;
    };
    ws_trigger_event(
        raw_cx,
        info.realm_global,
        &info.js_obj_key,
        "onmessage",
        ::std::option::Option::Some(data),
    );
}

/// Connection is dead (close handshake finished or transport error): CLOSED +
/// onclose (+ onerror first for a transport error). Terminal cleanup of the
/// GcStore root.
///
/// # Safety
/// `raw_cx` must be a live JSContext on the current thread.
unsafe fn ws_closed_dispatch(
    raw_cx: *mut JSContext,
    idx: usize,
    err: ::std::option::Option<String>,
) {
    let info = match ws_entry_info(idx) {
        Some(i) => i,
        None => return,
    };
    ws_set_ready_state(raw_cx, info.realm_global, &info.js_obj_key, 3);
    if let ::std::option::Option::Some(msg) = err {
        // Enter the realm before creating the error string (valid zone for
        // allocation — the pump runs with no realm entered).
        let data = if !info.realm_global.is_null() {
            let mut wrapped_cx =
                mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(raw_cx));
            let cx_ref = &mut wrapped_cx;
            rooted!(&in(cx_ref) let global_root = info.realm_global);
            let mut realm = AutoRealm::new_from_handle(cx_ref, global_root.handle());
            let _cx_in_realm: &mut mozjs::context::JSContext = &mut realm;
            let c_msg = ZBox::from_bytes(msg.as_bytes());
            let js_str = JS_NewStringCopyZ(raw_cx, c_msg.as_ptr());
            if !js_str.is_null() {
                ::std::option::Option::Some(StringValue(&*js_str))
            } else {
                ::std::option::Option::None
            }
        } else {
            ::std::option::Option::None
        };
        ws_trigger_event(raw_cx, info.realm_global, &info.js_obj_key, "onerror", data);
    }
    ws_trigger_event(raw_cx, info.realm_global, &info.js_obj_key, "onclose", None);
    gc_store_remove(raw_cx, &info.js_obj_key);
}

// ── Performance ──

pub fn install_performance(
    cx: &mut mozjs::context::JSContext,
    global: mozjs::rust::Handle<*mut JSObject>,
) {
    unsafe {
        rooted!(&in(cx) let perf_obj = JS_NewPlainObject(cx));
        if perf_obj.get().is_null() {
            return;
        }
        JS_DefineFunction(
            cx,
            perf_obj.handle(),
            c"now".as_ptr(),
            Some(performance_now),
            0,
            JSPROP_ENUMERATE as u32,
        );
        JS_DefineProperty3(
            cx,
            global,
            c"performance".as_ptr(),
            perf_obj.handle(),
            JSPROP_ENUMERATE as u32,
        );
    }
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn performance_now(_cx: *mut JSContext, _argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, _argc);
    let now = ::std::time::SystemTime::now()
        .duration_since(::std::time::UNIX_EPOCH)
        .unwrap_or_default();
    let ms = now.as_secs_f64() * 1000.0;
    args.rval().set(mozjs::jsval::DoubleValue(ms));
    true
}

// ── TextEncoder / TextDecoder ──

pub fn install_web_encodings(
    cx: &mut mozjs::context::JSContext,
    global: mozjs::rust::Handle<*mut JSObject>,
) {
    unsafe {
        let te_fun = JS_NewFunction(
            cx.raw_cx(),
            Some(text_encoder_constructor),
            0,
            JSFUN_CONSTRUCTOR,
            c"TextEncoder".as_ptr(),
        );
        if !te_fun.is_null() {
            let te_obj = JS_GetFunctionObject(te_fun);
            if !te_obj.is_null() {
                rooted!(&in(cx) let te_obj_r = te_obj);
                rooted!(&in(cx) let proto = JS_NewPlainObject(cx));
                if !proto.get().is_null() {
                    JS_DefineFunction(
                        cx,
                        proto.handle(),
                        c"encode".as_ptr(),
                        Some(text_encoder_encode),
                        1,
                        JSPROP_ENUMERATE as u32,
                    );
                    JS_DefineFunction(
                        cx,
                        proto.handle(),
                        c"encodeInto".as_ptr(),
                        Some(text_encoder_encode_into),
                        2,
                        JSPROP_ENUMERATE as u32,
                    );
                    JS_DefineProperty3(
                        cx,
                        te_obj_r.handle(),
                        c"prototype".as_ptr(),
                        proto.handle(),
                        JSPROP_PERMANENT as u32,
                    );
                }
                JS_DefineProperty3(
                    cx,
                    global,
                    c"TextEncoder".as_ptr(),
                    te_obj_r.handle(),
                    (JSPROP_ENUMERATE | JSPROP_PERMANENT) as u32,
                );
            }
        }

        let td_fun = JS_NewFunction(
            cx.raw_cx(),
            Some(text_decoder_constructor),
            1,
            JSFUN_CONSTRUCTOR,
            c"TextDecoder".as_ptr(),
        );
        if !td_fun.is_null() {
            let td_obj = JS_GetFunctionObject(td_fun);
            if !td_obj.is_null() {
                rooted!(&in(cx) let td_obj_r = td_obj);
                rooted!(&in(cx) let proto = JS_NewPlainObject(cx));
                if !proto.get().is_null() {
                    JS_DefineFunction(
                        cx,
                        proto.handle(),
                        c"decode".as_ptr(),
                        Some(text_decoder_decode),
                        1,
                        JSPROP_ENUMERATE as u32,
                    );
                    JS_DefineProperty3(
                        cx,
                        td_obj_r.handle(),
                        c"prototype".as_ptr(),
                        proto.handle(),
                        JSPROP_PERMANENT as u32,
                    );
                }
                JS_DefineProperty3(
                    cx,
                    global,
                    c"TextDecoder".as_ptr(),
                    td_obj_r.handle(),
                    (JSPROP_ENUMERATE | JSPROP_PERMANENT) as u32,
                );
            }
        }
    }
}

pub fn install_atob_btoa(
    cx: &mut mozjs::context::JSContext,
    global: mozjs::rust::Handle<*mut JSObject>,
) {
    unsafe {
        JS_DefineFunction(
            cx,
            global,
            c"atob".as_ptr(),
            Some(atob_fn),
            1,
            JSPROP_ENUMERATE as u32,
        );
        JS_DefineFunction(
            cx,
            global,
            c"btoa".as_ptr(),
            Some(btoa_fn),
            1,
            JSPROP_ENUMERATE as u32,
        );
    }
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn atob_fn(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    if argc == 0 || !(*args.get(0).ptr).is_string() {
        args.rval().set(UndefinedValue());
        return true;
    }
    let s = unsafe_jsstr_to_string(
        cx,
        ::std::ptr::NonNull::new_unchecked((*args.get(0).ptr).to_string()),
    );
    // HTML spec forgivable-base64 decode preamble (mirrors servo
    // base64_atob): strip HTML space, drop trailing padding on a %4==0
    // input, reject len%4==1, reject non-alphabet characters.
    let is_html_space = |c: char| matches!(c, ' ' | '\t' | '\n' | '\r' | '\x0C');
    let cleaned: String = s.chars().filter(|&c| !is_html_space(c)).collect();
    let mut input: &str = &cleaned;
    if input.len() % 4 == 0 {
        if input.ends_with("==") {
            input = &input[..input.len() - 2];
        } else if input.ends_with('=') {
            input = &input[..input.len() - 1];
        }
    }
    if input.len() % 4 == 1 || input.chars().any(|c| !c.is_ascii_alphanumeric() && c != '+' && c != '/')
    {
        JS_ReportErrorUTF8(
            cx,
            c"Failed to decode base64: InvalidCharacterError".as_ptr(),
        );
        return false;
    }
    match bun_base64::decode_alloc(input.as_bytes()) {
        Ok(bytes) => {
            // BCE (atob binary truncation, 2026-08-18): two stacked defects
            // made any NUL-bearing payload truncate to its first zero byte
            // (a 117k-char WAV base64 decoded to 8-9 bytes — "RIFF" plus a
            // couple of size-field bytes, cut at the 0x00s):
            //   1. `String::from_utf8_lossy(&bytes)` treated raw binary as
            //      UTF-8, corrupting every non-ASCII byte into U+FFFD.
            //   2. `JS_NewStringCopyZ` copies up to the first NUL.
            // HTML spec / servo's own base64_atob semantics: each decoded
            // octet maps to ONE code unit (latin-1) — same contract as
            // bun_api::js_string_from_child_bytes. Copy via explicit-length
            // JS_NewUCStringCopyN so 0x00 survives as a plain code unit.
            let units: Vec<u16> = bytes.iter().map(|&b| b as u16).collect();
            let js_str = if units.is_empty() {
                JS_NewStringCopyN(cx, c"".as_ptr(), 0)
            } else {
                JS_NewUCStringCopyN(cx, units.as_ptr(), units.len())
            };
            if js_str.is_null() {
                args.rval().set(UndefinedValue());
            } else {
                args.rval().set(StringValue(&*js_str));
            }
        }
        Err(_) => {
            JS_ReportErrorUTF8(
                cx,
                c"Failed to decode base64: InvalidCharacterError".as_ptr(),
            );
            return false;
        }
    }
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn btoa_fn(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    if argc == 0 || !(*args.get(0).ptr).is_string() {
        args.rval().set(UndefinedValue());
        return true;
    }
    // BCE (btoa latin1 corruption, found by the atob round-trip regression):
    // the input's UTF-8 bytes used to be encoded directly, so a code unit
    // like 0xE0 encoded as the TWO bytes C0 A0 instead of the single octet
    // E0 — every >0x7F character corrupted the encoding. HTML spec / servo
    // base64_btoa: throw InvalidCharacterError on any code point > U+00FF;
    // otherwise each code point encodes as ONE octet. `chars()` iterates
    // code points (== code units for the ≤0xFF domain we keep here).
    let s = unsafe_jsstr_to_string(
        cx,
        ::std::ptr::NonNull::new_unchecked((*args.get(0).ptr).to_string()),
    );
    if s.chars().any(|c| c > '\u{FF}') {
        JS_ReportErrorUTF8(
            cx,
            c"Failed to encode base64: InvalidCharacterError".as_ptr(),
        );
        return false;
    }
    let octets: Vec<u8> = s.chars().map(|c| c as u8).collect();
    let encoded_bytes = bun_base64::encode_alloc(&octets);
    let encoded = ::std::str::from_utf8(&encoded_bytes).unwrap_or("");
    let c_str = ZBox::from_bytes(encoded.as_bytes());
    let js_str = JS_NewStringCopyZ(cx, c_str.as_ptr());
    if js_str.is_null() {
        args.rval().set(UndefinedValue());
    } else {
        args.rval().set(StringValue(&*js_str));
    }
    true
}

pub fn install_queue_microtask(
    cx: &mut mozjs::context::JSContext,
    global: mozjs::rust::Handle<*mut JSObject>,
) {
    unsafe {
        JS_DefineFunction(
            cx,
            global,
            c"queueMicrotask".as_ptr(),
            Some(queue_microtask_fn),
            1,
            JSPROP_ENUMERATE as u32,
        );
    }
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn text_encoder_constructor(
    cx: *mut JSContext,
    argc: u32,
    vp: *mut JSVal,
) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    let obj = mozjs_sys::jsapi::JS_NewPlainObject(cx);
    if obj.is_null() {
        args.rval().set(UndefinedValue());
        return true;
    }
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    rooted!(&in(wrapped_cx) let obj_r = obj);
    let encoding_str = JS_NewStringCopyZ(cx, c"utf-8".as_ptr());
    if !encoding_str.is_null() {
        let val = StringValue(&*encoding_str);
        rooted!(&in(wrapped_cx) let val_root = val);
        JS_DefineProperty(
            cx,
            obj_r.handle().into(),
            c"encoding".as_ptr(),
            val_root.handle().into(),
            (JSPROP_ENUMERATE | JSPROP_READONLY) as u32,
        );
    }

    JS_DefineFunction(
        &mut wrapped_cx,
        obj_r.handle(),
        c"encode".as_ptr(),
        Some(text_encoder_encode),
        1,
        JSPROP_ENUMERATE as u32,
    );
    JS_DefineFunction(
        &mut wrapped_cx,
        obj_r.handle(),
        c"encodeInto".as_ptr(),
        Some(text_encoder_encode_into),
        2,
        JSPROP_ENUMERATE as u32,
    );

    args.rval().set(ObjectValue(obj));
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn text_encoder_encode(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    let input = if argc > 0 {
        let v = *args.get(0).ptr;
        if v.is_string() {
            crate::js_to_rust_string(cx, v)
        } else {
            String::new()
        }
    } else {
        String::new()
    };

    let bytes = input.as_bytes();
    let wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));

    // @trace REQ-ENG-005 [api:TextEncoder.encode] — Return a real SM
    // Uint8Array (not a plain Array) so callers see .byteLength/.buffer and
    // pass `instanceof Uint8Array`. Buffer.test.js drives this via
    // `new TextEncoder().encode(str).byteLength`.
    let u8_obj = mozjs_sys::jsapi::JS_NewUint8Array(cx, bytes.len());
    if u8_obj.is_null() {
        args.rval().set(UndefinedValue());
        return true;
    }
    if !bytes.is_empty() {
        rooted!(&in(wrapped_cx) let arr = u8_obj);
        let mut is_shared = false;
        let data_ptr =
            mozjs_sys::jsapi::JS_GetUint8ArrayData(arr.get(), &mut is_shared, ::std::ptr::null());
        if !data_ptr.is_null() {
            ::std::ptr::copy_nonoverlapping(bytes.as_ptr(), data_ptr, bytes.len());
        }
        args.rval().set(ObjectValue(arr.get()));
    } else {
        args.rval().set(ObjectValue(u8_obj));
    }
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn text_encoder_encode_into(
    _cx: *mut JSContext,
    _argc: u32,
    vp: *mut JSVal,
) -> bool {
    let args = CallArgs::from_vp(vp, _argc);
    args.rval().set(UndefinedValue());
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn text_decoder_constructor(
    cx: *mut JSContext,
    argc: u32,
    vp: *mut JSVal,
) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    let obj = mozjs_sys::jsapi::JS_NewPlainObject(cx);
    if obj.is_null() {
        args.rval().set(UndefinedValue());
        return true;
    }
    let encoding = if argc > 0 {
        let v = *args.get(0).ptr;
        if v.is_string() {
            crate::js_to_rust_string(cx, v)
        } else {
            "utf-8".to_string()
        }
    } else {
        "utf-8".to_string()
    };
    let encoding_lower = encoding.to_lowercase();
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    rooted!(&in(wrapped_cx) let obj_r = obj);
    let encoding_str = JS_NewStringCopyZ(cx, ZBox::from_bytes(encoding_lower.as_bytes()).as_ptr());
    if !encoding_str.is_null() {
        let val = StringValue(&*encoding_str);
        rooted!(&in(wrapped_cx) let val_root = val);
        JS_DefineProperty(
            cx,
            obj_r.handle().into(),
            c"encoding".as_ptr(),
            val_root.handle().into(),
            (JSPROP_ENUMERATE | JSPROP_READONLY) as u32,
        );
    }
    rooted!(&in(wrapped_cx) let fatal_val = BooleanValue(false));
    JS_DefineProperty(
        cx,
        obj_r.handle().into(),
        c"fatal".as_ptr(),
        fatal_val.handle().into(),
        (JSPROP_ENUMERATE | JSPROP_READONLY) as u32,
    );
    rooted!(&in(wrapped_cx) let bom_val = BooleanValue(false));
    JS_DefineProperty(
        cx,
        obj_r.handle().into(),
        c"ignoreBOM".as_ptr(),
        bom_val.handle().into(),
        (JSPROP_ENUMERATE | JSPROP_READONLY) as u32,
    );

    JS_DefineFunction(
        &mut wrapped_cx,
        obj_r.handle(),
        c"decode".as_ptr(),
        Some(text_decoder_decode),
        1,
        JSPROP_ENUMERATE as u32,
    );

    args.rval().set(ObjectValue(obj));
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn text_decoder_decode(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    if argc == 0 {
        let empty = JS_NewStringCopyZ(cx, c"".as_ptr());
        args.rval().set(if empty.is_null() {
            UndefinedValue()
        } else {
            StringValue(&*empty)
        });
        return true;
    }

    let input = *args.get(0).ptr;
    // Primitive input (number/string/null) is not a BufferSource — spec
    // TypeError. Guard BEFORE to_object(): to_object() on a non-object
    // asserts.
    if !input.is_object() {
        mozjs::error::throw_type_error(
            cx,
            c"The provided value is not an instance of ArrayBuffer or ArrayBufferView".as_ref(),
        );
        return false;
    }

    // WHATWG BufferSource extraction: ArrayBufferView (Uint8Array & every
    // other view, byteOffset-adjusted) OR ArrayBuffer. Both take the direct
    // data-pointer path — the previous generic length+GetElement loop read
    // view ELEMENTS one by one and produced "" for a bare ArrayBuffer (it
    // has no `length` property). Anything that is not a BufferSource is a
    // TypeError per spec (decode() with no args returns "" above).
    let bytes: Vec<u8> = {
        let wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
        rooted!(&in(wrapped_cx) let obj = input.to_object());
        let mut len: usize = 0;
        let mut is_shared = false;
        let mut data: *mut u8 = ::std::ptr::null_mut();
        let unwrapped =
            mozjs_sys::jsapi::JS_GetObjectAsArrayBufferView(obj.get(), &mut len, &mut is_shared, &mut data);
        if !unwrapped.is_null() && !data.is_null() {
            // Detached views surface as length 0 + null data (handled below);
            // a live view yields the byteOffset-adjusted pointer directly.
            ::std::slice::from_raw_parts(data, len).to_vec()
        } else if !unwrapped.is_null() {
            Vec::new() // detached view: empty byte sequence per spec
        } else {
            let ab_unwrapped =
                mozjs_sys::jsapi::JS::GetObjectAsArrayBuffer(obj.get(), &mut len, &mut data);
            if !ab_unwrapped.is_null() && !data.is_null() {
                ::std::slice::from_raw_parts(data, len).to_vec()
            } else if !ab_unwrapped.is_null() {
                Vec::new() // detached ArrayBuffer
            } else {
                mozjs::error::throw_type_error(
                    cx,
                    c"The provided value is not an instance of ArrayBuffer or ArrayBufferView"
                        .as_ref(),
                );
                return false;
            }
        }
    };

    // TextDecoder defaults to fatal:false — invalid sequences become U+FFFD
    // replacement characters instead of throwing.
    let decoded = String::from_utf8_lossy(&bytes).into_owned();

    let utf16: Vec<u16> = decoded.encode_utf16().collect();
    let js_str = JS_NewUCStringCopyN(cx, utf16.as_ptr(), utf16.len());
    args.rval().set(if js_str.is_null() {
        UndefinedValue()
    } else {
        StringValue(&*js_str)
    });
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn queue_microtask_fn(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    if argc == 0 || !(*args.get(0).ptr).is_object() {
        return true;
    }
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx = &mut wrapped_cx;

    rooted!(&in(cx) let callback = (*args.get(0).ptr).to_object());
    rooted!(&in(cx) let undef_val = UndefinedValue());
    let resolved = CallOriginalPromiseResolve(cx, undef_val.handle());
    if resolved.is_null() {
        args.rval().set(UndefinedValue());
        return true;
    }
    rooted!(&in(cx) let promise = resolved);
    rooted!(&in(cx) let null_reject = ::std::ptr::null_mut::<JSObject>());
    CallOriginalPromiseThen(
        cx,
        promise.handle(),
        callback.handle(),
        null_reject.handle(),
    );
    args.rval().set(UndefinedValue());
    true
}

// ═══════════════════════════════════════════════════════════════════════════
// crypto.subtle — WebCrypto surface bridged onto the REAL primitives the
// node:crypto layer already uses (bao_crypto + bun_sha_hmac + bun_base64).
// @trace REQ-ENG-006 [api:crypto.subtle]
//
// The subtle object is the SAME object globals::install_crypto_global put on
// globalThis.crypto (so require("crypto").subtle — aliased by node_crypto —
// upgrades with it). Installed at the tail of the web-API phase; defining the
// methods on the existing object preserves every alias.
// ═══════════════════════════════════════════════════════════════════════════

/// Define the full WebCrypto method set on globalThis.crypto.subtle.
pub fn install_crypto_subtle(cx: &mut mozjs::context::JSContext, global: mozjs::rust::Handle<*mut JSObject>) {
    unsafe {
        rooted!(&in(cx) let global_root = global.get());
        let mut crypto_val = UndefinedValue();
        // BCE (P0 browser startup panic, servo error.rs:74): a capability
        // probe that hits a throwing getter returns false WITH the exception
        // pending; an unconsumed pending exception detonates servo's
        // `assert!(!JS_IsExceptionPending)` in `throw_dom_exception` on the
        // next error path, killing the ScriptThread at page init. Read as
        // "not available" and consume the exception.
        let crypto_found = JS_GetProperty(
            cx.raw_cx(),
            global_root.handle().into(),
            c"crypto".as_ptr(),
            MutableHandle::<Value> {
                _phantom_0: ::std::marker::PhantomData,
                ptr: &mut crypto_val,
            },
        );
        if !crypto_found {
            JS_ClearPendingException(cx.raw_cx());
            return;
        }
        if !crypto_val.is_object() {
            return;
        }
        rooted!(&in(cx) let crypto_obj = crypto_val.to_object());
        let mut subtle_val = UndefinedValue();
        // BCE (error.rs:74): same probe contract as `crypto` above.
        let subtle_found = JS_GetProperty(
            cx.raw_cx(),
            crypto_obj.handle().into(),
            c"subtle".as_ptr(),
            MutableHandle::<Value> {
                _phantom_0: ::std::marker::PhantomData,
                ptr: &mut subtle_val,
            },
        );
        if !subtle_found {
            JS_ClearPendingException(cx.raw_cx());
            return;
        }
        if !subtle_val.is_object() {
            return;
        }
        rooted!(&in(cx) let subtle = subtle_val.to_object());
        for (name, op, nargs) in [
            ("encrypt", subtle_encrypt as unsafe extern "C" fn(*mut JSContext, u32, *mut JSVal) -> bool, 3),
            ("decrypt", subtle_decrypt, 3),
            ("generateKey", subtle_generate_key, 3),
            ("importKey", subtle_import_key, 5),
            ("sign", subtle_sign, 3),
            ("verify", subtle_verify, 4),
            // digest: the pre-existing globals.rs implementation returned the
            // raw bytes instead of a Promise (spec violation — every
            // `subtle.digest().then` threw). Redefined here on the SAME subtle
            // object with Promise semantics over the real BoringSSL hashers.
            ("digest", subtle_digest, 2),
        ] {
            JS_DefineFunction(
                cx,
                subtle.handle(),
                ZBox::from_bytes(name.as_bytes()).as_ptr(),
                Some(op),
                nargs,
                JSPROP_ENUMERATE as u32,
            );
        }
    }
}

// ── subtle helpers ──────────────────────────────────────────────────────────

/// Constant-time byte equality (signature verification must not leak).
fn ct_eq(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }
    let mut diff: u8 = 0;
    for (x, y) in a.iter().zip(b.iter()) {
        diff |= x ^ y;
    }
    diff == 0
}

/// Extract BufferSource bytes: TypedArray/DataView fast path, then BARE
/// ArrayBuffer (subtle results are bare ArrayBuffers — the node_crypto
/// extractor misses those), then empty.
#[allow(unsafe_op_in_unsafe_fn)]
unsafe fn subtle_bytes(cx: *mut JSContext, val: JSVal) -> Vec<u8> {
    if !val.is_object() {
        return Vec::new();
    }
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    rooted!(&in(cx_ref) let obj_root = val.to_object());
    let mut length: usize = 0;
    let mut is_shared = false;
    let mut data_ptr: *mut u8 = ::std::ptr::null_mut();
    let u8_unwrapped = mozjs_sys::jsapi::JS_GetObjectAsUint8Array(
        obj_root.get(),
        &mut length,
        &mut is_shared,
        &mut data_ptr,
    );
    if !u8_unwrapped.is_null() && !data_ptr.is_null() && length > 0 {
        return ::std::slice::from_raw_parts(data_ptr, length).to_vec();
    }
    let mut view_length: usize = 0;
    let mut view_shared = false;
    let mut view_data: *mut u8 = ::std::ptr::null_mut();
    let view_unwrapped = mozjs_sys::jsapi::JS_GetObjectAsArrayBufferView(
        obj_root.get(),
        &mut view_length,
        &mut view_shared,
        &mut view_data,
    );
    if !view_unwrapped.is_null() && !view_data.is_null() && view_length > 0 {
        return ::std::slice::from_raw_parts(view_data, view_length).to_vec();
    }
    // Bare ArrayBuffer (length via ByteLength; data ptr valid while rooted,
    // copied before any further JSAPI call).
    let ab_len = mozjs_sys::jsapi::JS::GetArrayBufferByteLength(obj_root.get());
    if ab_len > 0 {
        let mut ab_shared = false;
        let ab_data = mozjs_sys::jsapi::JS::GetArrayBufferData(
            obj_root.get(),
            &mut ab_shared,
            ::std::ptr::null(),
        );
        if !ab_data.is_null() {
            return ::std::slice::from_raw_parts(ab_data, ab_len).to_vec();
        }
    }
    Vec::new()
}

/// Read a string-valued property off a JS object.
#[allow(unsafe_op_in_unsafe_fn)]
unsafe fn subtle_str_prop(cx: *mut JSContext, obj: *mut JSObject, name: &str) -> Option<String> {
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    rooted!(&in(cx_ref) let obj_r = obj);
    let c_name = ZBox::from_bytes(name.as_bytes());
    let mut v = UndefinedValue();
    // BCE (error.rs:74): caller-supplied algorithm objects can carry
    // throwing getters; a failed probe must consume its pending exception
    // (browser mode runs this on the servo ScriptThread context).
    bao_stealth::engine_props::get_property_clearing(cx, obj_r.handle().into(), c_name.as_cstr(), &mut v);
    if v.is_string() {
        Some(crate::js_to_rust_string(cx, v))
    } else {
        None
    }
}

/// Read a numeric property as u32.
#[allow(unsafe_op_in_unsafe_fn)]
unsafe fn subtle_u32_prop(cx: *mut JSContext, obj: *mut JSObject, name: &str) -> Option<u32> {
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    rooted!(&in(cx_ref) let obj_r = obj);
    let c_name = ZBox::from_bytes(name.as_bytes());
    let mut v = UndefinedValue();
    // BCE (error.rs:74): clearing probe — see subtle_str_prop.
    bao_stealth::engine_props::get_property_clearing(cx, obj_r.handle().into(), c_name.as_cstr(), &mut v);
    if v.is_int32() && v.to_int32() >= 0 {
        Some(v.to_int32() as u32)
    } else if v.is_double() && v.to_double() >= 0.0 {
        Some(v.to_double() as u32)
    } else {
        None
    }
}

/// Read a BufferSource-valued property (iv / additionalData / data).
#[allow(unsafe_op_in_unsafe_fn)]
unsafe fn subtle_bytes_prop(cx: *mut JSContext, obj: *mut JSObject, name: &str) -> Option<Vec<u8>> {
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    rooted!(&in(cx_ref) let obj_r = obj);
    let c_name = ZBox::from_bytes(name.as_bytes());
    let mut v = UndefinedValue();
    // BCE (error.rs:74): clearing probe — see subtle_str_prop.
    bao_stealth::engine_props::get_property_clearing(cx, obj_r.handle().into(), c_name.as_cstr(), &mut v);
    if v.is_object() {
        Some(subtle_bytes(cx, v))
    } else {
        None
    }
}

/// Copy bytes into a fresh ArrayBuffer value.
#[allow(unsafe_op_in_unsafe_fn)]
unsafe fn bytes_to_arraybuffer_val(cx: *mut JSContext, bytes: &[u8]) -> JSVal {
    // glue::NewArrayBufferWithContents takes ownership of a malloc'd buffer
    // (JS frees it) — copy into a fresh malloc block, zero-copy from there.
    if bytes.is_empty() {
        let ab = mozjs_sys::jsapi::JS::NewArrayBuffer(cx, 0);
        return if ab.is_null() { UndefinedValue() } else { ObjectValue(ab) };
    }
    let buf = libc::malloc(bytes.len()) as *mut u8;
    if buf.is_null() {
        return UndefinedValue();
    }
    ::std::ptr::copy_nonoverlapping(bytes.as_ptr(), buf, bytes.len());
    let ab = mozjs_sys::jsapi::glue::NewArrayBufferWithContents(cx, bytes.len(), buf as *mut core::ffi::c_void);
    if ab.is_null() {
        libc::free(buf as *mut core::ffi::c_void);
        return UndefinedValue();
    }
    ObjectValue(ab)
}

/// Build a CryptoKey JS object. `material` names the hidden bytes slot:
/// `_raw` for symmetric keys, `_der` (pkcs8 for private, spki for public)
/// for asymmetric.
#[allow(unsafe_op_in_unsafe_fn)]
unsafe fn make_crypto_key(
    cx: *mut JSContext,
    ktype: &str,
    alg_name: &str,
    extra_alg: &[(&str, String)],
    extractable: bool,
    usages: *mut JSObject,
    material_slot: &str,
    material: &[u8],
) -> *mut JSObject {
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    rooted!(&in(cx_ref) let key = JS_NewPlainObject(cx_ref));
    if key.get().is_null() {
        return ::std::ptr::null_mut();
    }
    let kh = key.handle().into();

    let c_t = ZBox::from_bytes(ktype.as_bytes());
    let t_js = JS_NewStringCopyZ(cx, c_t.as_ptr());
    if !t_js.is_null() {
        rooted!(&in(cx_ref) let tv = StringValue(&*t_js));
        JS_DefineProperty(cx, kh, c"type".as_ptr(), tv.handle().into(), (JSPROP_ENUMERATE | JSPROP_READONLY) as u32);
    }
    rooted!(&in(cx_ref) let ev = BooleanValue(extractable));
    JS_DefineProperty(cx, kh, c"extractable".as_ptr(), ev.handle().into(), (JSPROP_ENUMERATE | JSPROP_READONLY) as u32);
    rooted!(&in(cx_ref) let uv = ObjectValue(usages));
    JS_DefineProperty(cx, kh, c"usages".as_ptr(), uv.handle().into(), (JSPROP_ENUMERATE | JSPROP_READONLY) as u32);

    // algorithm: { name, ...extras }
    rooted!(&in(cx_ref) let alg_obj = JS_NewPlainObject(cx_ref));
    if !alg_obj.get().is_null() {
        let ah = alg_obj.handle().into();
        let c_n = ZBox::from_bytes(alg_name.as_bytes());
        let n_js = JS_NewStringCopyZ(cx, c_n.as_ptr());
        if !n_js.is_null() {
            rooted!(&in(cx_ref) let nv = StringValue(&*n_js));
            JS_DefineProperty(cx, ah, c"name".as_ptr(), nv.handle().into(), (JSPROP_ENUMERATE | JSPROP_READONLY) as u32);
        }
        for (k, v) in extra_alg {
            if *k == "length" {
                // Numeric algorithm member (AES key length) per spec.
                if let Ok(n) = v.parse::<i32>() {
                    rooted!(&in(cx_ref) let nv = Int32Value(n));
                    JS_DefineProperty(
                        cx,
                        ah,
                        ZBox::from_bytes(k.as_bytes()).as_ptr(),
                        nv.handle().into(),
                        (JSPROP_ENUMERATE | JSPROP_READONLY) as u32,
                    );
                }
                continue;
            }
            let c_v = ZBox::from_bytes(v.as_bytes());
            let v_js = JS_NewStringCopyZ(cx, c_v.as_ptr());
            if !v_js.is_null() {
                rooted!(&in(cx_ref) let vv = StringValue(&*v_js));
                JS_DefineProperty(
                    cx,
                    ah,
                    ZBox::from_bytes(k.as_bytes()).as_ptr(),
                    vv.handle().into(),
                    (JSPROP_ENUMERATE | JSPROP_READONLY) as u32,
                );
            }
        }
        rooted!(&in(cx_ref) let av = ObjectValue(alg_obj.get()));
        JS_DefineProperty(cx, kh, c"algorithm".as_ptr(), av.handle().into(), (JSPROP_ENUMERATE | JSPROP_READONLY) as u32);
    }

    // Hidden material slots (Uint8Array over copied bytes).
    let stash = |slot: &str, bytes: &[u8]| {
        let u8v = mozjs_sys::jsapi::JS_NewUint8Array(cx, bytes.len());
        if u8v.is_null() {
            return;
        }
        let mut len: usize = 0;
        let mut shared = false;
        let mut data: *mut u8 = ::std::ptr::null_mut();
        let unwrapped = mozjs_sys::jsapi::JS_GetObjectAsUint8Array(u8v, &mut len, &mut shared, &mut data);
        if unwrapped.is_null() || data.is_null() || len < bytes.len() {
            return;
        }
        ::std::ptr::copy_nonoverlapping(bytes.as_ptr(), data, bytes.len());
        rooted!(&in(cx_ref) let mv = ObjectValue(u8v));
        JS_DefineProperty(
            cx,
            kh,
            ZBox::from_bytes(slot.as_bytes()).as_ptr(),
            mv.handle().into(),
            0,
        );
    };
    stash(material_slot, material);
    key.get()
}

/// Build the WebCrypto CryptoKeyPair `{ privateKey, publicKey }` that
/// generateKey resolves with for asymmetric algorithms. Each half carries
/// its own `_der` material: pkcs8 on the private key, spki on the public.
#[allow(unsafe_op_in_unsafe_fn)]
unsafe fn make_crypto_key_pair(
    cx: *mut JSContext,
    alg_name: &str,
    extra_alg: &[(&str, String)],
    extractable: bool,
    usages: *mut JSObject,
    private_der: &[u8],
    public_der: &[u8],
) -> *mut JSObject {
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    rooted!(&in(cx_ref) let pair = JS_NewPlainObject(cx_ref));
    if pair.get().is_null() {
        return ::std::ptr::null_mut();
    }
    let ph = pair.handle().into();
    let private_key = make_crypto_key(cx, "private", alg_name, extra_alg, extractable, usages, "_der", private_der);
    let public_key = make_crypto_key(cx, "public", alg_name, extra_alg, extractable, usages, "_der", public_der);
    if private_key.is_null() || public_key.is_null() {
        return ::std::ptr::null_mut();
    }
    rooted!(&in(cx_ref) let pv = ObjectValue(private_key));
    JS_DefineProperty(cx, ph, c"privateKey".as_ptr(), pv.handle().into(), (JSPROP_ENUMERATE | JSPROP_READONLY) as u32);
    rooted!(&in(cx_ref) let uv = ObjectValue(public_key));
    JS_DefineProperty(cx, ph, c"publicKey".as_ptr(), uv.handle().into(), (JSPROP_ENUMERATE | JSPROP_READONLY) as u32);
    pair.get()
}

/// Caller-supplied JSVal coerced to an object — None on non-object input
/// (a rejected promise) instead of the JSVal::to_object debug assert abort
/// that a stray `undefined` key argument used to trigger.
unsafe fn subtle_arg_object(val: JSVal) -> Option<*mut JSObject> {
    if val.is_object() {
        Some(val.to_object())
    } else {
        None
    }
}

/// Read a hidden bytes slot off a CryptoKey.
#[allow(unsafe_op_in_unsafe_fn)]
unsafe fn key_material(cx: *mut JSContext, key: *mut JSObject, slot: &str) -> Option<Vec<u8>> {
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    rooted!(&in(cx_ref) let key_r = key);
    let mut v = UndefinedValue();
    JS_GetProperty(
        cx,
        key_r.handle().into(),
        ZBox::from_bytes(slot.as_bytes()).as_ptr(),
        MutableHandle::<Value> {
            _phantom_0: ::std::marker::PhantomData,
            ptr: &mut v,
        },
    );
    if v.is_object() {
        Some(subtle_bytes(cx, v))
    } else {
        None
    }
}

/// Fresh pending Promise, set as the method's rval. Expands to a block
/// evaluating to the rooted promise object (null on allocation failure).
macro_rules! subtle_promise {
    ($cx:expr, $cx_ref:expr, $args:expr) => {{
        rooted!(&in($cx_ref) let null_global = ::std::ptr::null_mut::<JSObject>());
        let promise = mozjs_sys::jsapi::JS::NewPromiseObject($cx, null_global.handle().into());
        if promise.is_null() {
            $args.rval().set(UndefinedValue());
            ::std::ptr::null_mut::<JSObject>()
        } else {
            $args.rval().set(ObjectValue(promise));
            promise
        }
    }};
}

// ── subtle.digest (Promise-returning redefinition over real hashers) ───────

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn subtle_digest(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    let promise = subtle_promise!(cx, cx_ref, &args);
    if promise.is_null() {
        return true;
    }
    rooted!(&in(cx_ref) let promise_root = promise);

    let result: ::std::result::Result<Vec<u8>, String> = (|| {
        use bun_sha_hmac::sha::hashers;
        if argc < 2 {
            return Err("digest(algorithm, data) requires 2 arguments".to_string());
        }
        let alg_val = *args.get(0).ptr;
        let name = if alg_val.is_string() {
            crate::js_to_rust_string(cx, alg_val).to_uppercase()
        } else if alg_val.is_object() {
            let obj = alg_val.to_object();
            subtle_str_prop(cx, obj, "name").unwrap_or_default().to_uppercase()
        } else {
            return Err("digest algorithm must be a string or {name}".to_string());
        };
        let data = subtle_bytes(cx, *args.get(1).ptr);
        match name.as_str() {
            "SHA-1" | "SHA1" => {
                let mut out = [0u8; hashers::SHA1::DIGEST];
                hashers::SHA1::hash(&data, &mut out);
                Ok(out.to_vec())
            }
            "SHA-256" | "SHA256" => {
                let mut out = [0u8; hashers::SHA256::DIGEST];
                hashers::SHA256::hash(&data, &mut out);
                Ok(out.to_vec())
            }
            "SHA-384" | "SHA384" => {
                let mut out = [0u8; hashers::SHA384::DIGEST];
                hashers::SHA384::hash(&data, &mut out);
                Ok(out.to_vec())
            }
            "SHA-512" | "SHA512" => {
                let mut out = [0u8; hashers::SHA512::DIGEST];
                hashers::SHA512::hash(&data, &mut out);
                Ok(out.to_vec())
            }
            other => Err(format!("subtle.digest: unsupported algorithm {}", other)),
        }
    })();

    match result {
        Ok(bytes) => {
            let v = bytes_to_arraybuffer_val(cx, &bytes);
            subtle_resolve(cx, promise_root.get(), v);
        }
        Err(msg) => subtle_reject(cx, promise_root.get(), &format!("subtle.digest: {}", msg)),
    }
    true
}

/// Reject the promise with a REAL TypeError from the realm's constructor.
#[allow(unsafe_op_in_unsafe_fn)]
unsafe fn subtle_reject(cx: *mut JSContext, promise: *mut JSObject, msg: &str) {
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    rooted!(&in(cx_ref) let promise_root = promise);
    let global = CurrentGlobalOrNull(cx);
    let err_obj = if !global.is_null() {
        rooted!(&in(cx_ref) let global_root = global);
        let c_msg = ZBox::from_bytes(msg.as_bytes());
        let msg_js = JS_NewStringCopyZ(cx, c_msg.as_ptr());
        let mut err = UndefinedValue();
        if !msg_js.is_null() {
            rooted!(&in(cx_ref) let mv = StringValue(&*msg_js));
            let elems = [*mv.handle()];
            let call_args = HandleValueArray {
                length_: 1,
                elements_: elems.as_ptr(),
            };
            let mut type_error_fn = UndefinedValue();
            JS_GetProperty(
                cx,
                global_root.handle().into(),
                c"TypeError".as_ptr(),
                MutableHandle::<Value> {
                    _phantom_0: ::std::marker::PhantomData,
                    ptr: &mut type_error_fn,
                },
            );
            if type_error_fn.is_object() {
                rooted!(&in(cx_ref) let fn_val = type_error_fn);
                rooted!(&in(cx_ref) let undef_this = ::std::ptr::null_mut::<JSObject>());
                if JS_CallFunctionValue(
                    cx,
                    undef_this.handle().into(),
                    fn_val.handle().into(),
                    &call_args,
                    MutableHandle::<Value> {
                        _phantom_0: ::std::marker::PhantomData,
                        ptr: &mut err,
                    },
                ) && err.is_object() {
                    err.to_object()
                } else {
                    JS_ClearPendingException(cx);
                    ::std::ptr::null_mut()
                }
            } else {
                ::std::ptr::null_mut()
            }
        } else {
            ::std::ptr::null_mut()
        }
    } else {
        ::std::ptr::null_mut()
    };
    if err_obj.is_null() {
        // Degraded shape only when the realm has no TypeError at all — the
        // message still reaches the rejection.
        rooted!(&in(cx_ref) let obj = JS_NewPlainObject(cx_ref));
        if !obj.get().is_null() {
            let c_msg = ZBox::from_bytes(msg.as_bytes());
            let m_js = JS_NewStringCopyZ(cx, c_msg.as_ptr());
            if !m_js.is_null() {
                rooted!(&in(cx_ref) let mv = StringValue(&*m_js));
                JS_DefineProperty(cx, obj.handle().into(), c"message".as_ptr(), mv.handle().into(), JSPROP_ENUMERATE as u32);
            }
            let c_n = ZBox::from_bytes("TypeError".as_bytes());
            let n_js = JS_NewStringCopyZ(cx, c_n.as_ptr());
            if !n_js.is_null() {
                rooted!(&in(cx_ref) let nv = StringValue(&*n_js));
                JS_DefineProperty(cx, obj.handle().into(), c"name".as_ptr(), nv.handle().into(), JSPROP_ENUMERATE as u32);
            }
            rooted!(&in(cx_ref) let ev = ObjectValue(obj.get()));
            mozjs_sys::jsapi::JS::RejectPromise(cx, promise_root.handle().into(), ev.handle().into());
            return;
        }
        return;
    }
    rooted!(&in(cx_ref) let ev = ObjectValue(err_obj));
    mozjs_sys::jsapi::JS::RejectPromise(cx, promise_root.handle().into(), ev.handle().into());
}

/// Resolve the promise with a value.
#[allow(unsafe_op_in_unsafe_fn)]
unsafe fn subtle_resolve(cx: *mut JSContext, promise: *mut JSObject, val: JSVal) {
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    rooted!(&in(cx_ref) let promise_root = promise);
    rooted!(&in(cx_ref) let v = val);
    mozjs_sys::jsapi::JS::ResolvePromise(cx, promise_root.handle().into(), v.handle().into());
}

/// algo.name string from the first argument (algorithm identifier object).
#[allow(unsafe_op_in_unsafe_fn)]
unsafe fn subtle_algo_name(cx: *mut JSContext, val: JSVal) -> ::std::result::Result<(String, *mut JSObject), String> {
    // WebCrypto AlgorithmIdentifier is `(Algorithm or DOMString)`: a bare
    // string like 'HMAC' is the exact equivalent of {name: 'HMAC'} with no
    // extra params (browsers and Node webcrypto both coerce). Synthesize the
    // object so every downstream subtle_*_prop(alg_obj, …) lookup behaves
    // exactly as if the caller passed the object form.
    if val.is_string() {
        let name = crate::js_to_rust_string(cx, val);
        if name.is_empty() {
            return Err("algorithm name is required".to_string());
        }
        let mut wrapped_cx =
            mozjs::context::JSContext::from_ptr(::std::ptr::NonNull::new_unchecked(cx));
        let cx_ref = &mut wrapped_cx;
        let obj = JS_NewPlainObject(cx_ref);
        if obj.is_null() {
            return Err("algorithm object allocation failed".to_string());
        }
        rooted!(&in(cx_ref) let obj_root = obj);
        let js_name = JS_NewStringCopyN(
            cx,
            name.as_ptr() as *const ::std::os::raw::c_char,
            name.len(),
        );
        if js_name.is_null() {
            return Err("algorithm name string allocation failed".to_string());
        }
        rooted!(&in(cx_ref) let name_val = mozjs::jsval::StringValue(&*js_name));
        JS_DefineProperty(
            cx,
            obj_root.handle().into(),
            c"name".as_ptr(),
            name_val.handle().into(),
            JSPROP_ENUMERATE as u32,
        );
        return Ok((name, obj_root.get()));
    }
    if !val.is_object() {
        return Err("algorithm identifier must be a string or an object".to_string());
    }
    let obj = val.to_object();
    let name = subtle_str_prop(cx, obj, "name")
        .ok_or_else(|| "algorithm.name is required".to_string())?;
    Ok((name, obj))
}

/// Map an algorithm name + symmetric key length to the cipher algorithm.
fn aes_cipher_algo(name: &str, key_len: usize) -> ::std::result::Result<bao_crypto::cipher::CipherAlgorithm, String> {
    let bits = key_len * 8;
    let qualified = match name {
        "AES-GCM" => match bits {
            128 => "aes-128-gcm",
            192 => "aes-192-gcm",
            256 => "aes-256-gcm",
            _ => return Err(format!("invalid AES-GCM key length: {} bits", bits)),
        },
        "AES-CBC" => match bits {
            128 => "aes-128-cbc",
            192 => "aes-192-cbc",
            256 => "aes-256-cbc",
            _ => return Err(format!("invalid AES-CBC key length: {} bits", bits)),
        },
        other => return Err(format!("unsupported cipher algorithm: {}", other)),
    };
    bao_crypto::cipher::parse_algorithm(qualified).map_err(|e| e.to_string())
}

// ── subtle.encrypt / subtle.decrypt ─────────────────────────────────────────

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn subtle_encrypt(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    let promise = subtle_promise!(cx, cx_ref, &args);
    if promise.is_null() {
        return true;
    }
    rooted!(&in(cx_ref) let promise_root = promise);

    let result: ::std::result::Result<Vec<u8>, String> = (|| {
        if argc < 3 {
            return Err("encrypt(algorithm, key, data) requires 3 arguments".to_string());
        }
        let (name, alg_obj) = subtle_algo_name(cx, *args.get(0).ptr)?;
        let key_obj = subtle_arg_object(*args.get(1).ptr)
            .ok_or("encrypt: key must be a CryptoKey".to_string())?;
        let data = subtle_bytes(cx, *args.get(2).ptr);
        let raw = key_material(cx, key_obj, "_raw")
            .ok_or("encrypt: not a symmetric CryptoKey".to_string())?;

        match name.as_str() {
            "AES-GCM" => {
                let iv = subtle_bytes_prop(cx, alg_obj, "iv")
                    .ok_or("AES-GCM requires an iv".to_string())?;
                let aad = subtle_bytes_prop(cx, alg_obj, "additionalData");
                if let Some(tl) = subtle_u32_prop(cx, alg_obj, "tagLength") {
                    if tl != 128 {
                        return Err(format!("AES-GCM tagLength {} is not supported (128 only)", tl));
                    }
                }
                let algo = aes_cipher_algo("AES-GCM", raw.len())?;
                let out = bao_crypto::cipher::encrypt(algo, &raw, &iv, aad.as_deref(), &data)
                    .map_err(|e| e.to_string())?;
                let mut combined = out.ciphertext;
                combined.extend_from_slice(&out.auth_tag);
                Ok(combined)
            }
            "AES-CBC" => {
                let iv = subtle_bytes_prop(cx, alg_obj, "iv")
                    .ok_or("AES-CBC requires an iv".to_string())?;
                let algo = aes_cipher_algo("AES-CBC", raw.len())?;
                let mut ctx = bao_crypto::cipher::CipherCtx::new(
                    algo,
                    &raw,
                    &iv,
                    bao_crypto::cipher::Direction::Encrypt,
                )
                .map_err(|e| e.to_string())?;
                let mut out = ctx.update(&data).map_err(|e| e.to_string())?;
                out.extend_from_slice(&ctx.final_ex().map_err(|e| e.to_string())?);
                Ok(out)
            }
            other => Err(format!("subtle.encrypt: unsupported algorithm {}", other)),
        }
    })();

    match result {
        Ok(bytes) => {
            let v = bytes_to_arraybuffer_val(cx, &bytes);
            subtle_resolve(cx, promise_root.get(), v);
        }
        Err(msg) => subtle_reject(cx, promise_root.get(), &format!("subtle.encrypt: {}", msg)),
    }
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn subtle_decrypt(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    let promise = subtle_promise!(cx, cx_ref, &args);
    if promise.is_null() {
        return true;
    }
    rooted!(&in(cx_ref) let promise_root = promise);

    let result: ::std::result::Result<Vec<u8>, String> = (|| {
        if argc < 3 {
            return Err("decrypt(algorithm, key, data) requires 3 arguments".to_string());
        }
        let (name, alg_obj) = subtle_algo_name(cx, *args.get(0).ptr)?;
        let key_obj = subtle_arg_object(*args.get(1).ptr)
            .ok_or("decrypt: key must be a CryptoKey".to_string())?;
        let data = subtle_bytes(cx, *args.get(2).ptr);
        let raw = key_material(cx, key_obj, "_raw")
            .ok_or("decrypt: not a symmetric CryptoKey".to_string())?;

        match name.as_str() {
            "AES-GCM" => {
                let iv = subtle_bytes_prop(cx, alg_obj, "iv")
                    .ok_or("AES-GCM requires an iv".to_string())?;
                let aad = subtle_bytes_prop(cx, alg_obj, "additionalData");
                let tag_len = subtle_u32_prop(cx, alg_obj, "tagLength")
                    .map_or(16usize, |bits| bits as usize / 8);
                if tag_len != 16 {
                    return Err(format!("AES-GCM tagLength {} bits is not supported (128 only)", tag_len * 8));
                }
                if data.len() < tag_len {
                    return Err("AES-GCM ciphertext shorter than the auth tag".to_string());
                }
                let split = data.len() - tag_len;
                let algo = aes_cipher_algo("AES-GCM", raw.len())?;
                bao_crypto::cipher::decrypt(
                    algo,
                    &raw,
                    &iv,
                    aad.as_deref(),
                    &data[..split],
                    &data[split..],
                )
                .map_err(|_| "decryption failed (authentication or parameters)".to_string())
            }
            "AES-CBC" => {
                let iv = subtle_bytes_prop(cx, alg_obj, "iv")
                    .ok_or("AES-CBC requires an iv".to_string())?;
                let algo = aes_cipher_algo("AES-CBC", raw.len())?;
                let mut ctx = bao_crypto::cipher::CipherCtx::new(
                    algo,
                    &raw,
                    &iv,
                    bao_crypto::cipher::Direction::Decrypt,
                )
                .map_err(|e| e.to_string())?;
                let mut out = ctx.update(&data).map_err(|e| e.to_string())?;
                out.extend_from_slice(&ctx.final_ex().map_err(|e| e.to_string())?);
                Ok(out)
            }
            other => Err(format!("subtle.decrypt: unsupported algorithm {}", other)),
        }
    })();

    match result {
        Ok(bytes) => {
            let v = bytes_to_arraybuffer_val(cx, &bytes);
            subtle_resolve(cx, promise_root.get(), v);
        }
        Err(msg) => subtle_reject(cx, promise_root.get(), &format!("subtle.decrypt: {}", msg)),
    }
    true
}

// ── subtle.generateKey ──────────────────────────────────────────────────────

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn subtle_generate_key(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    let promise = subtle_promise!(cx, cx_ref, &args);
    if promise.is_null() {
        return true;
    }
    rooted!(&in(cx_ref) let promise_root = promise);

    let result: ::std::result::Result<*mut JSObject, String> = (|| {
        if argc < 3 {
            return Err("generateKey(algorithm, extractable, usages) requires 3 arguments".to_string());
        }
        let (name, alg_obj) = subtle_algo_name(cx, *args.get(0).ptr)?;
        let extractable = (*args.get(1).ptr).to_boolean();
        let usages = subtle_arg_object(*args.get(2).ptr)
            .ok_or("generateKey: usages must be an array".to_string())?;

        match name.as_str() {
            "AES-GCM" | "AES-CBC" => {
                let bits = subtle_u32_prop(cx, alg_obj, "length")
                    .ok_or("AES generateKey requires algorithm.length".to_string())?;
                if !matches!(bits, 128 | 192 | 256) {
                    return Err(format!("invalid AES key length {} (128/192/256)", bits));
                }
                let mut raw = vec![0u8; bits as usize / 8];
                bao_crypto::random::rand_bytes(&mut raw).map_err(|e| e.to_string())?;
                Ok(make_crypto_key(
                    cx,
                    "secret",
                    &name,
                    &[("length", bits.to_string())],
                    extractable,
                    usages,
                    "_raw",
                    &raw,
                ))
            }
            "RSA-RSASSA-PKCS1-v1_5" => {
                let bits = subtle_u32_prop(cx, alg_obj, "modulusLength")
                    .ok_or("RSA generateKey requires modulusLength".to_string())? as usize;
                let hash = subtle_str_prop(cx, alg_obj, "hash")
                    .or_else(|| subtle_str_prop(cx, alg_obj, "hash.name"))
                    .unwrap_or_else(|| "SHA-256".to_string());
                let kp = bao_crypto::keypair::generate_key_pair(&bao_crypto::keypair::KeyPairType::Rsa { bits })
                    .map_err(|e| e.to_string())?;
                Ok(make_crypto_key_pair(
                    cx,
                    &name,
                    &[("hash", hash)],
                    extractable,
                    usages,
                    &kp.private_key_der,
                    &kp.public_key_der,
                ))
            }
            "ECDSA" => {
                let curve = subtle_str_prop(cx, alg_obj, "namedCurve")
                    .or_else(|| subtle_str_prop(cx, alg_obj, "namedCurve.name"))
                    .ok_or("ECDSA generateKey requires namedCurve".to_string())?;
                let ec_curve = match curve.as_str() {
                    "P-256" => bao_crypto::keypair::EcCurve::P256,
                    "P-384" => bao_crypto::keypair::EcCurve::P384,
                    other => return Err(format!("unsupported ECDSA curve {}", other)),
                };
                let kp = bao_crypto::keypair::generate_key_pair(&bao_crypto::keypair::KeyPairType::Ec { curve: ec_curve })
                    .map_err(|e| e.to_string())?;
                Ok(make_crypto_key_pair(
                    cx,
                    &name,
                    &[("namedCurve", curve)],
                    extractable,
                    usages,
                    &kp.private_key_der,
                    &kp.public_key_der,
                ))
            }
            other => Err(format!("subtle.generateKey: unsupported algorithm {}", other)),
        }
    })();

    match result {
        Ok(key) if !key.is_null() => {
            let v = ObjectValue(key);
            subtle_resolve(cx, promise_root.get(), v);
        }
        Ok(_) => subtle_reject(cx, promise_root.get(), "subtle.generateKey: key construction failed"),
        Err(msg) => subtle_reject(cx, promise_root.get(), &format!("subtle.generateKey: {}", msg)),
    }
    true
}

// ── subtle.importKey ────────────────────────────────────────────────────────

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn subtle_import_key(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    let promise = subtle_promise!(cx, cx_ref, &args);
    if promise.is_null() {
        return true;
    }
    rooted!(&in(cx_ref) let promise_root = promise);

    let result: ::std::result::Result<*mut JSObject, String> = (|| {
        if argc < 5 {
            return Err("importKey(format, keyData, algorithm, extractable, usages) requires 5 arguments".to_string());
        }
        let format = crate::js_to_rust_string(cx, *args.get(0).ptr);
        let (name, _alg_obj) = subtle_algo_name(cx, *args.get(2).ptr)?;
        let extractable = (*args.get(3).ptr).to_boolean();
        let usages = subtle_arg_object(*args.get(4).ptr)
            .ok_or("importKey: usages must be an array".to_string())?;

        match format.as_str() {
            "raw" => {
                let raw = subtle_bytes(cx, *args.get(1).ptr);
                if raw.is_empty() {
                    return Err("raw import requires key bytes".to_string());
                }
                match name.as_str() {
                    "AES-GCM" | "AES-CBC" => {
                        if !matches!(raw.len() * 8, 128 | 192 | 256) {
                            return Err(format!("invalid AES key length {} bits", raw.len() * 8));
                        }
                        Ok(make_crypto_key(cx, "secret", &name, &[("length", (raw.len() * 8).to_string())], extractable, usages, "_raw", &raw))
                    }
                    "HMAC" => {
                        let hash = subtle_str_prop(cx, _alg_obj, "hash")
                            .or_else(|| subtle_str_prop(cx, _alg_obj, "hash.name"))
                            .ok_or("HMAC import requires algorithm.hash".to_string())?;
                        Ok(make_crypto_key(cx, "secret", "HMAC", &[("hash", hash)], extractable, usages, "_raw", &raw))
                    }
                    other => Err(format!("subtle.importKey raw: unsupported algorithm {}", other)),
                }
            }
            "pkcs8" => {
                let der = subtle_bytes(cx, *args.get(1).ptr);
                if der.is_empty() {
                    return Err("pkcs8 import requires DER bytes".to_string());
                }
                let extras: Vec<(String, String)> = match name.as_str() {
                    "RSA-RSASSA-PKCS1-v1_5" => {
                        let hash = subtle_str_prop(cx, _alg_obj, "hash")
                            .or_else(|| subtle_str_prop(cx, _alg_obj, "hash.name"))
                            .unwrap_or_else(|| "SHA-256".to_string());
                        vec![("hash".to_string(), hash)]
                    }
                    "ECDSA" => {
                        let curve = subtle_str_prop(cx, _alg_obj, "namedCurve")
                            .or_else(|| subtle_str_prop(cx, _alg_obj, "namedCurve.name"))
                            .unwrap_or_else(|| "P-256".to_string());
                        vec![("namedCurve".to_string(), curve)]
                    }
                    other => return Err(format!("subtle.importKey pkcs8: unsupported algorithm {}", other)),
                };
                let extras_ref: Vec<(&str, String)> = extras.iter().map(|(k, v)| (k.as_str(), v.clone())).collect();
                Ok(make_crypto_key(cx, "private", &name, &extras_ref, extractable, usages, "_der", &der))
            }
            "spki" => {
                let der = subtle_bytes(cx, *args.get(1).ptr);
                if der.is_empty() {
                    return Err("spki import requires DER bytes".to_string());
                }
                let extras: Vec<(String, String)> = match name.as_str() {
                    "RSA-RSASSA-PKCS1-v1_5" => {
                        let hash = subtle_str_prop(cx, _alg_obj, "hash")
                            .or_else(|| subtle_str_prop(cx, _alg_obj, "hash.name"))
                            .unwrap_or_else(|| "SHA-256".to_string());
                        vec![("hash".to_string(), hash)]
                    }
                    "ECDSA" => {
                        let curve = subtle_str_prop(cx, _alg_obj, "namedCurve")
                            .or_else(|| subtle_str_prop(cx, _alg_obj, "namedCurve.name"))
                            .unwrap_or_else(|| "P-256".to_string());
                        vec![("namedCurve".to_string(), curve)]
                    }
                    other => return Err(format!("subtle.importKey spki: unsupported algorithm {}", other)),
                };
                let extras_ref: Vec<(&str, String)> = extras.iter().map(|(k, v)| (k.as_str(), v.clone())).collect();
                Ok(make_crypto_key(cx, "public", &name, &extras_ref, extractable, usages, "_der", &der))
            }
            "jwk" => {
                // Symmetric oct keys: { kty: "oct", k: <base64url> }. Asymmetric
                // JWK import requires JWK→DER assembly which is NOT wired —
                // explicit NotSupported error, never a silent fallback.
                if !(*args.get(1).ptr).is_object() {
                    return Err("jwk import requires a JWK object".to_string());
                }
                let jwk = (*args.get(1).ptr).to_object();
                let kty = subtle_str_prop(cx, jwk, "kty").unwrap_or_default();
                if kty != "oct" {
                    return Err(format!("jwk import for kty \"{}\" is not supported (oct only); use pkcs8/spki DER import", kty));
                }
                let k = subtle_str_prop(cx, jwk, "k").ok_or("oct JWK requires the k field".to_string())?;
                let src = k.as_bytes();
                let upper = bun_base64::decode_lenient_len(src.len());
                let mut out = vec![0u8; upper];
                let n = bun_base64::decode_lenient(&mut out, src, true);
                out.truncate(n);
                if out.is_empty() {
                    return Err("oct JWK k field decoded to zero bytes".to_string());
                }
                match name.as_str() {
                    "AES-GCM" | "AES-CBC" => {
                        if !matches!(out.len() * 8, 128 | 192 | 256) {
                            return Err(format!("invalid AES key length {} bits from JWK", out.len() * 8));
                        }
                        Ok(make_crypto_key(cx, "secret", &name, &[("length", (out.len() * 8).to_string())], extractable, usages, "_raw", &out))
                    }
                    "HMAC" => {
                        let hash = subtle_str_prop(cx, _alg_obj, "hash")
                            .or_else(|| subtle_str_prop(cx, _alg_obj, "hash.name"))
                            .ok_or("HMAC import requires algorithm.hash".to_string())?;
                        Ok(make_crypto_key(cx, "secret", "HMAC", &[("hash", hash)], extractable, usages, "_raw", &out))
                    }
                    other => Err(format!("subtle.importKey jwk: unsupported algorithm {}", other)),
                }
            }
            other => Err(format!("subtle.importKey: unsupported format {}", other)),
        }
    })();

    match result {
        Ok(key) if !key.is_null() => {
            let v = ObjectValue(key);
            subtle_resolve(cx, promise_root.get(), v);
        }
        Ok(_) => subtle_reject(cx, promise_root.get(), "subtle.importKey: key construction failed"),
        Err(msg) => subtle_reject(cx, promise_root.get(), &format!("subtle.importKey: {}", msg)),
    }
    true
}

// ── subtle.sign / subtle.verify ─────────────────────────────────────────────

fn subtle_hmac_algorithm(hash: &str) -> ::std::result::Result<bun_sha_hmac::sha::evp::Algorithm, String> {
    use bun_sha_hmac::sha::evp::Algorithm;
    Ok(match hash.to_uppercase().as_str() {
        "SHA-1" => Algorithm::Sha1,
        "SHA-224" => Algorithm::Sha224,
        "SHA-256" => Algorithm::Sha256,
        "SHA-384" => Algorithm::Sha384,
        "SHA-512" => Algorithm::Sha512,
        other => return Err(format!("unsupported HMAC hash {}", other)),
    })
}

fn subtle_rsa_hash(hash: &str) -> ::std::result::Result<bao_crypto::sign::RsaHash, String> {
    Ok(match hash.to_uppercase().as_str() {
        "SHA-256" => bao_crypto::sign::RsaHash::Sha256,
        "SHA-384" => bao_crypto::sign::RsaHash::Sha384,
        "SHA-512" => bao_crypto::sign::RsaHash::Sha512,
        other => return Err(format!("unsupported RSA hash {}", other)),
    })
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn subtle_sign(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    let promise = subtle_promise!(cx, cx_ref, &args);
    if promise.is_null() {
        return true;
    }
    rooted!(&in(cx_ref) let promise_root = promise);

    let result: ::std::result::Result<Vec<u8>, String> = (|| {
        if argc < 3 {
            return Err("sign(algorithm, key, data) requires 3 arguments".to_string());
        }
        let (name, alg_obj) = subtle_algo_name(cx, *args.get(0).ptr)?;
        let key_obj = subtle_arg_object(*args.get(1).ptr)
            .ok_or("sign: key must be a CryptoKey".to_string())?;
        // BCE-root discipline: the handle must outlive the GetProperty call —
        // a rooted! binding inside a block expression un-roots at the block's
        // closing brace, leaving JS_GetProperty with a dangling Handle.
        rooted!(&in(cx_ref) let key_obj_root = key_obj);
        let data = subtle_bytes(cx, *args.get(2).ptr);

        let key_alg = {
            let mut v = UndefinedValue();
            JS_GetProperty(
                cx,
                key_obj_root.handle().into(),
                c"algorithm".as_ptr(),
                MutableHandle::<Value> {
                    _phantom_0: ::std::marker::PhantomData,
                    ptr: &mut v,
                },
            );
            if !v.is_object() {
                return Err("sign: not a CryptoKey".to_string());
            }
            v.to_object()
        };
        let ktype = subtle_str_prop(cx, key_obj, "type").unwrap_or_default();

        match name.as_str() {
            "HMAC" => {
                let raw = key_material(cx, key_obj, "_raw")
                    .ok_or("sign: not a symmetric CryptoKey".to_string())?;
                // The hash is a property of the KEY's algorithm (what the key
                // was imported with); an explicit hash on the algorithm
                // argument (object form only — the string form carries no
                // params) wins, mirroring the object-form behavior.
                let hash = subtle_str_prop(cx, alg_obj, "hash")
                    .or_else(|| subtle_str_prop(cx, key_alg, "hash"))
                    .unwrap_or_else(|| "SHA-256".to_string());
                let algo = subtle_hmac_algorithm(&hash)?;
                let mut out = [0u8; bun_sha_hmac::hmac::EVP_MAX_MD_SIZE];
                let mac = bun_sha_hmac::hmac::generate(&raw, &data, algo, &mut out)
                    .ok_or("HMAC computation failed".to_string())?;
                Ok(mac.to_vec())
            }
            "RSA-RSASSA-PKCS1-v1_5" | "RSASSA-PKCS1-v1_5" => {
                if ktype != "private" {
                    return Err("sign: not a private CryptoKey".to_string());
                }
                let der = key_material(cx, key_obj, "_der")
                    .ok_or("sign: not a private CryptoKey".to_string())?;
                let hash = subtle_str_prop(cx, key_alg, "hash")
                    .unwrap_or_else(|| "SHA-256".to_string());
                let signer = bao_crypto::sign::Signer::from_pkcs8_der(
                    &bao_crypto::sign::SignAlgorithm::RsaPkcs1v15 { hash: subtle_rsa_hash(&hash)? },
                    &der,
                )
                .map_err(|e| e.to_string())?;
                signer
                    .sign(&data, bao_crypto::sign::SignatureFormat::Der)
                    .map_err(|e| e.to_string())
            }
            "ECDSA" => {
                if ktype != "private" {
                    return Err("sign: not a private CryptoKey".to_string());
                }
                let der = key_material(cx, key_obj, "_der")
                    .ok_or("sign: not a private CryptoKey".to_string())?;
                let curve = subtle_str_prop(cx, key_alg, "namedCurve").unwrap_or_else(|| "P-256".to_string());
                let algo = match curve.as_str() {
                    "P-256" => bao_crypto::sign::SignAlgorithm::EcdsaP256,
                    "P-384" => bao_crypto::sign::SignAlgorithm::EcdsaP384,
                    other => return Err(format!("unsupported ECDSA curve {}", other)),
                };
                let signer = bao_crypto::sign::Signer::from_pkcs8_der(&algo, &der)
                    .map_err(|e| e.to_string())?;
                // WebCrypto ECDSA signatures are raw r||s.
                signer
                    .sign(&data, bao_crypto::sign::SignatureFormat::Raw)
                    .map_err(|e| e.to_string())
            }
            other => Err(format!("subtle.sign: unsupported algorithm {}", other)),
        }
    })();

    match result {
        Ok(bytes) => {
            let v = bytes_to_arraybuffer_val(cx, &bytes);
            subtle_resolve(cx, promise_root.get(), v);
        }
        Err(msg) => subtle_reject(cx, promise_root.get(), &format!("subtle.sign: {}", msg)),
    }
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn subtle_verify(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    let mut wrapped_cx = mozjs::context::JSContext::from_ptr(NonNull::new_unchecked(cx));
    let cx_ref = &mut wrapped_cx;
    let promise = subtle_promise!(cx, cx_ref, &args);
    if promise.is_null() {
        return true;
    }
    rooted!(&in(cx_ref) let promise_root = promise);

    let result: ::std::result::Result<bool, String> = (|| {
        if argc < 4 {
            return Err("verify(algorithm, key, signature, data) requires 4 arguments".to_string());
        }
        let (name, _alg_obj) = subtle_algo_name(cx, *args.get(0).ptr)?;
        let key_obj = subtle_arg_object(*args.get(1).ptr)
            .ok_or("verify: key must be a CryptoKey".to_string())?;
        // Same root discipline as subtle_sign — no block-scoped roots in args.
        rooted!(&in(cx_ref) let key_obj_root = key_obj);
        let signature = subtle_bytes(cx, *args.get(2).ptr);
        let data = subtle_bytes(cx, *args.get(3).ptr);

        let key_alg = {
            let mut v = UndefinedValue();
            JS_GetProperty(
                cx,
                key_obj_root.handle().into(),
                c"algorithm".as_ptr(),
                MutableHandle::<Value> {
                    _phantom_0: ::std::marker::PhantomData,
                    ptr: &mut v,
                },
            );
            if !v.is_object() {
                return Err("verify: not a CryptoKey".to_string());
            }
            v.to_object()
        };
        let ktype = subtle_str_prop(cx, key_obj, "type").unwrap_or_default();

        match name.as_str() {
            "HMAC" => {
                let raw = key_material(cx, key_obj, "_raw")
                    .ok_or("verify: not a symmetric CryptoKey".to_string())?;
                let hash = subtle_str_prop(cx, key_alg, "hash").unwrap_or_else(|| "SHA-256".to_string());
                let algo = subtle_hmac_algorithm(&hash)?;
                let mut out = [0u8; bun_sha_hmac::hmac::EVP_MAX_MD_SIZE];
                let Some(mac) = bun_sha_hmac::hmac::generate(&raw, &data, algo, &mut out) else {
                    return Err("HMAC computation failed".to_string());
                };
                Ok(ct_eq(mac, &signature))
            }
            "RSA-RSASSA-PKCS1-v1_5" | "RSASSA-PKCS1-v1_5" => {
                let hash = subtle_str_prop(cx, key_alg, "hash").unwrap_or_else(|| "SHA-256".to_string());
                let algo = bao_crypto::sign::SignAlgorithm::RsaPkcs1v15 { hash: subtle_rsa_hash(&hash)? };
                let verifier = if ktype == "private" {
                    let der = key_material(cx, key_obj, "_der")
                        .ok_or("verify: key carries no DER material".to_string())?;
                    bao_crypto::verify::Verifier::from_pkcs8_der(&algo, &der)
                } else {
                    let der = key_material(cx, key_obj, "_der")
                        .ok_or("verify: key carries no DER material".to_string())?;
                    bao_crypto::verify::Verifier::from_public_der(&algo, &der)
                }
                .map_err(|e| e.to_string())?;
                verifier
                    .verify(&data, &signature, bao_crypto::sign::SignatureFormat::Der)
                    .map_err(|e| e.to_string())
            }
            "ECDSA" => {
                let curve = subtle_str_prop(cx, key_alg, "namedCurve").unwrap_or_else(|| "P-256".to_string());
                let algo = match curve.as_str() {
                    "P-256" => bao_crypto::sign::SignAlgorithm::EcdsaP256,
                    "P-384" => bao_crypto::sign::SignAlgorithm::EcdsaP384,
                    other => return Err(format!("unsupported ECDSA curve {}", other)),
                };
                let verifier = if ktype == "private" {
                    let der = key_material(cx, key_obj, "_der")
                        .ok_or("verify: key carries no DER material".to_string())?;
                    bao_crypto::verify::Verifier::from_pkcs8_der(&algo, &der)
                } else {
                    let der = key_material(cx, key_obj, "_der")
                        .ok_or("verify: key carries no DER material".to_string())?;
                    bao_crypto::verify::Verifier::from_public_der(&algo, &der)
                }
                .map_err(|e| e.to_string())?;
                // WebCrypto ECDSA signatures are raw r||s.
                verifier
                    .verify(&data, &signature, bao_crypto::sign::SignatureFormat::Raw)
                    .map_err(|e| e.to_string())
            }
            other => Err(format!("subtle.verify: unsupported algorithm {}", other)),
        }
    })();

    match result {
        Ok(ok) => {
            let v = BooleanValue(ok);
            subtle_resolve(cx, promise_root.get(), v);
        }
        Err(msg) => subtle_reject(cx, promise_root.get(), &format!("subtle.verify: {}", msg)),
    }
    true
}

// ═══════════════════════════════════════════════════════════════════════════
// localStorage — CLI mode. Persisted store at ~/.bao/localstorage.json
// (browser pages keep servo's own localStorage; this is the CLI surface).
// @trace REQ-ENG-006 [api:localStorage]
// ═══════════════════════════════════════════════════════════════════════════

fn localstorage_path() -> ::std::path::PathBuf {
    let home = ::std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
    let dir = ::std::path::Path::new(&home).join(".bao");
    let _ = ::std::fs::create_dir_all(&dir);
    dir.join("localstorage.json")
}

::std::thread_local! {
    static LS_STORE: RefCell<Option<::std::collections::BTreeMap<String, String>>> =
        const { RefCell::new(None) };
}

/// Lazy-load the persisted store (missing/corrupt file → empty map; a corrupt
/// file is reported to stderr but never breaks the API — next mutation
/// rewrites the file with valid JSON).
fn ls_with_store<R>(f: impl FnOnce(&mut ::std::collections::BTreeMap<String, String>) -> R) -> R {
    LS_STORE.with(|cell| {
        let mut guard = cell.borrow_mut();
        if guard.is_none() {
            let map = ::std::fs::read_to_string(localstorage_path())
                .ok()
                .and_then(|text| serde_json::from_str::<serde_json::Value>(&text).ok())
                .and_then(|v| {
                    let obj = v.as_object()?;
                    let mut m = ::std::collections::BTreeMap::new();
                    for (k, val) in obj {
                        if let Some(s) = val.as_str() {
                            m.insert(k.clone(), s.to_string());
                        }
                    }
                    Some(m)
                })
                .unwrap_or_default();
            *guard = Some(map);
        }
        f(guard.as_mut().unwrap())
    })
}

fn ls_persist() {
    LS_STORE.with(|cell| {
        if let Some(map) = cell.borrow().as_ref() {
            if let Ok(text) = serde_json::to_string_pretty(map) {
                let tmp = localstorage_path().with_extension("json.tmp");
                if ::std::fs::write(&tmp, text).is_ok() {
                    let _ = ::std::fs::rename(&tmp, localstorage_path());
                }
            }
        }
    });
}

/// Install localStorage on the global (CLI surface; browser pages keep
/// servo's own implementation, so installation only fills a missing slot).
pub fn install_local_storage(cx: &mut mozjs::context::JSContext, global: mozjs::rust::Handle<*mut JSObject>) {
    unsafe {
        rooted!(&in(cx) let global_root = global.get());
        let mut existing = UndefinedValue();
        let found = JS_GetProperty(
            cx.raw_cx(),
            global_root.handle().into(),
            c"localStorage".as_ptr(),
            MutableHandle::<Value> {
                _phantom_0: ::std::marker::PhantomData,
                ptr: &mut existing,
            },
        );
        // BCE (browser startup panic, servo error.rs:74): the probe's failure
        // return MUST be consumed. On an opaque-origin page (data:/about:
        // blank) servo's `window.localStorage` getter THROWS SecurityError —
        // JS_GetProperty returns false with the exception pending. The old
        // code ignored the return, leaving the pending exception on the
        // context; the next servo error path (`throw_dom_exception`) then
        // tripped `assert!(!JS_IsExceptionPending)` and killed Script#1 at
        // startup (pipeline never ready, CDP never listening). This is a
        // Rust-side capability probe, so a throwing getter means "servo owns
        // storage here, possibly origin-blocked" — clear the exception and
        // leave the page's storage semantics to servo.
        if !found {
            JS_ClearPendingException(cx.raw_cx());
            return; // browser page context — servo's storage stays
        }
        if existing.is_object() {
            return; // browser page context — servo's storage stays
        }
        rooted!(&in(cx) let ls = JS_NewPlainObject(cx));
        if ls.get().is_null() {
            return;
        }
        let lh = ls.handle();
        for (name, op, nargs) in [
            ("getItem", ls_get_item as unsafe extern "C" fn(*mut JSContext, u32, *mut JSVal) -> bool, 1),
            ("setItem", ls_set_item, 2),
            ("removeItem", ls_remove_item, 1),
            ("clear", ls_clear, 0),
            ("key", ls_key, 1),
        ] {
            JS_DefineFunction(
                cx,
                lh,
                ZBox::from_bytes(name.as_bytes()).as_ptr(),
                Some(op),
                nargs,
                JSPROP_ENUMERATE as u32,
            );
        }
        // length getter (JS_DefineProperty1: getter/setter native variant)
        JS_DefineProperty1(
            cx.raw_cx(),
            ls.handle().into(),
            c"length".as_ptr(),
            Some(ls_length_getter),
            None,
            (JSPROP_ENUMERATE | JSPROP_READONLY) as u32,
        );
        rooted!(&in(cx) let ls_root = ls.get());
        JS_DefineProperty3(
            cx,
            global,
            c"localStorage".as_ptr(),
            ls_root.handle(),
            (JSPROP_ENUMERATE | JSPROP_PERMANENT) as u32,
        );
    }
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn ls_get_item(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    if argc < 1 || !(*args.get(0).ptr).is_string() {
        args.rval().set(UndefinedValue());
        return true;
    }
    let key = crate::js_to_rust_string(cx, *args.get(0).ptr);
    ls_with_store(|m| {
        let v = m.get(&key).cloned();
        match v {
            Some(s) => {
                let c_s = ZBox::from_bytes(s.as_bytes());
                let js = JS_NewStringCopyZ(cx, c_s.as_ptr());
                args.rval().set(if js.is_null() {
                    UndefinedValue()
                } else {
                    StringValue(&*js)
                });
            }
            None => args.rval().set(NullValue()),
        }
    });
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn ls_set_item(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    if argc < 2 {
        let c_m = ZBox::from_bytes("localStorage.setItem(key, value) requires 2 arguments".as_bytes());
        JS_ReportErrorUTF8(cx, c"%s".as_ptr(), c_m.as_ptr());
        return false;
    }
    let key = crate::js_to_rust_string(cx, *args.get(0).ptr);
    let val = crate::js_to_rust_string(cx, *args.get(1).ptr);
    ls_with_store(|m| {
        m.insert(key, val);
    });
    ls_persist();
    args.rval().set(UndefinedValue());
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn ls_remove_item(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    if argc < 1 {
        args.rval().set(UndefinedValue());
        return true;
    }
    let key = crate::js_to_rust_string(cx, *args.get(0).ptr);
    ls_with_store(|m| {
        m.remove(&key);
    });
    ls_persist();
    args.rval().set(UndefinedValue());
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn ls_clear(_cx: *mut JSContext, _argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, _argc);
    ls_with_store(|m| {
        m.clear();
    });
    ls_persist();
    args.rval().set(UndefinedValue());
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn ls_key(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, argc);
    if argc < 1 || !(*args.get(0).ptr).is_int32() {
        args.rval().set(NullValue());
        return true;
    }
    let idx = (*args.get(0).ptr).to_int32();
    if idx < 0 {
        args.rval().set(NullValue());
        return true;
    }
    let val = ls_with_store(|m| {
        m.keys().nth(idx as usize).cloned()
    });
    match val {
        Some(k) => {
            let c_k = ZBox::from_bytes(k.as_bytes());
            let js = JS_NewStringCopyZ(cx, c_k.as_ptr());
            args.rval().set(if js.is_null() {
                NullValue()
            } else {
                StringValue(&*js)
            });
        }
        None => args.rval().set(NullValue()),
    }
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn ls_length(_cx: *mut JSContext, _argc: u32, vp: *mut JSVal) -> bool {
    let args = CallArgs::from_vp(vp, _argc);
    let n = ls_with_store(|m| m.len());
    args.rval().set(Int32Value(n as i32));
    true
}

#[allow(unsafe_op_in_unsafe_fn)]
unsafe extern "C" fn ls_length_getter(cx: *mut JSContext, _argc: u32, vp: *mut JSVal) -> bool {
    ls_length(cx, _argc, vp)
}

// ═══════════════════════════════════════════════════════════════════════════
// EventSource — SSE client over fetch(). Pure-JS class: fetch + TextDecoder
// + timers are all real runtime surfaces; the SSE framing/retry state
// machine follows the WHATWG spec's event-stream parsing algorithm.
//
// Streaming note: fetch() currently materialises the full body, so events
// arrive in one batch when the response completes (then auto-reconnect fires
// per the retry interval). The parser itself is the real spec algorithm and
// upgrades unchanged once fetch lands response streaming.
// @trace REQ-ENG-006 [api:EventSource]
//
// Also registers the CLI-mode window/document explicit gates (DOMParser
// style): referencing/typeof works, every METHOD call throws the honest
// "browser context required" error — no silent empty-DOM fakes.
// ═══════════════════════════════════════════════════════════════════════════

pub fn install_event_source(cx: &mut mozjs::context::JSContext, global: mozjs::rust::Handle<*mut JSObject>) {
    let src = r#"
(function() {
  var _g = globalThis;

  // ── EventSource ──
  var CONNECTING = 0, OPEN = 1, CLOSED = 2;

  function EventSource(url) {
    if (!(this instanceof EventSource)) return new EventSource(url);
    this.url = String(url);
    this.readyState = CONNECTING;
    this._retry = 3000;
    this._lastEventId = '';
    this._timer = null;
    this._listeners = {};
    this.onopen = null;
    this.onmessage = null;
    this.onerror = null;
    var self = this;
    this._connect();
  }
  EventSource.CONNECTING = CONNECTING;
  EventSource.OPEN = OPEN;
  EventSource.CLOSED = CLOSED;

  EventSource.prototype._fire = function(type, ev) {
    ev = ev || {};
    ev.type = type;
    var handler = this['on' + type];
    if (typeof handler === 'function') handler.call(this, ev);
    var list = this._listeners[type];
    if (list) {
      for (var i = 0; i < list.length; i++) list[i].call(this, ev);
    }
  };

  EventSource.prototype._connect = function() {
    var self = this;
    if (this.readyState === CLOSED) return;
    this.readyState = CONNECTING;
    var headers = { 'Accept': 'text/event-stream' };
    if (this._lastEventId) headers['Last-Event-ID'] = this._lastEventId;
    fetch(this.url, { headers: headers })
      .then(function(resp) {
        if (!resp.ok) {
          self._fail('EventSource: HTTP ' + resp.status + ' for ' + self.url);
          return null;
        }
        // Spec: the response MIME type must be text/event-stream.
        var ct = resp.headers && resp.headers.get && resp.headers.get('content-type');
        if (ct && String(ct).indexOf('text/event-stream') === -1) {
          self._fail('EventSource: response Content-Type "' + ct + '" is not text/event-stream');
          return null;
        }
        self.readyState = OPEN;
        self._fire('open');
        return resp.text();
      })
      .then(function(text) {
        if (text === null || text === undefined) return;
        self._parse(text);
        // Server closed the stream — spec: schedule reconnect.
        self._scheduleReconnect();
      })
      .catch(function(err) {
        self._fail(err && err.message ? err.message : String(err));
      });
  };

  EventSource.prototype._fail = function(msg) {
    if (this.readyState === CLOSED) return;
    this._fire('error', { message: msg });
    this._scheduleReconnect();
  };

  EventSource.prototype._scheduleReconnect = function() {
    var self = this;
    if (this.readyState === CLOSED) return;
    this.readyState = CONNECTING;
    if (this._timer) clearTimeout(this._timer);
    this._timer = setTimeout(function() { self._connect(); }, this._retry);
  };

  // WHATWG event-stream parsing: lines split on CR/LF/CRLF, fields
  // event/data/id/retry, blank line dispatches the accumulated event.
  EventSource.prototype._parse = function(text) {
    var lines = text.split(/\r\n|\r|\n/);
    var dataLines = [], eventName = '', lastId = this._lastEventId;
    for (var i = 0; i < lines.length; i++) {
      var line = lines[i];
      if (line === '') {
        if (dataLines.length > 0) {
          var ev = { data: dataLines.join('\n'), lastEventId: lastId };
          if (eventName !== '' && eventName !== 'message') {
            ev.lastEventId = lastId;
            this._fire(eventName, ev);
          } else {
            this._fire('message', ev);
          }
        }
        dataLines = [];
        eventName = '';
        continue;
      }
      if (line.charCodeAt(0) === 0x3a /* ':' */) continue; // comment
      var colon = line.indexOf(':');
      var field, value;
      if (colon === -1) { field = line; value = ''; }
      else {
        field = line.slice(0, colon);
        value = line.slice(colon + 1);
        if (value.charCodeAt(0) === 0x20 /* ' ' */) value = value.slice(1);
      }
      if (field === 'event') eventName = value;
      else if (field === 'data') dataLines.push(value);
      else if (field === 'id') { if (value.indexOf('\0') === -1) lastId = value; }
      else if (field === 'retry') {
        var n = parseInt(value, 10);
        if (!isNaN(n)) this._retry = n;
      }
    }
    this._lastEventId = lastId;
    // A trailing non-blank data block without the final blank line is NOT
    // dispatched (spec: incomplete event at stream end).
  };

  EventSource.prototype.close = function() {
    if (this._timer) { clearTimeout(this._timer); this._timer = null; }
    this.readyState = CLOSED;
  };

  EventSource.prototype.addEventListener = function(type, cb) {
    if (typeof cb !== 'function') return;
    if (!this._listeners[type]) this._listeners[type] = [];
    this._listeners[type].push(cb);
  };
  EventSource.prototype.removeEventListener = function(type, cb) {
    var list = this._listeners[type];
    if (!list) return;
    var i = list.indexOf(cb);
    if (i !== -1) list.splice(i, 1);
  };

  _g.EventSource = EventSource;

  // ── window/document: CLI leaves them UNDEFINED (team-lead ruling, Node
  // parity). An "exists but every property is a throwing placeholder" gate
  // is itself the silent-fake shape (feature detection sees a window that
  // lies about existing); honest MISSING — typeof window === 'undefined' —
  // matches Node and the DOMParser philosophy. Browser pages keep servo's
  // real window/document; bare references in CLI throw ReferenceError, which
  // IS the explicit signal.
})();
"#;
    unsafe {
        let raw = cx.raw_cx();
        let mut rval = UndefinedValue();
        let opts = mozjs::glue::NewCompileOptions(raw, c"event_source".as_ptr(), 1);
        if !opts.is_null() {
            let mut src_text = mozjs::rust::transform_str_to_source_text(src);
            mozjs_sys::jsapi::JS::Evaluate2(
                raw,
                opts,
                &mut src_text,
                MutableHandle::<Value> {
                    _phantom_0: ::std::marker::PhantomData,
                    ptr: &mut rval,
                },
            );
            libc::free(opts as *mut _);
        }
        let _ = global;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_ws_url_ws() {
        let (host, port, path) = split_authority_and_path("example.com/chat", "ws");
        assert_eq!(host, "example.com");
        assert_eq!(port, 80);
        assert_eq!(path, "/chat");
    }

    // @trace REQ-ENG-006 [api:WebSocket wss://] — wss:// is now supported
    // (default port 443); the prior behaviour rejected it outright.
    #[test]
    fn parse_ws_url_wss_default_port() {
        let (host, port, path) = split_authority_and_path("example.com/secure", "wss");
        assert_eq!(host, "example.com");
        assert_eq!(port, 443);
        assert_eq!(path, "/secure");
    }

    #[test]
    fn parse_ws_url_with_port() {
        let (host, port, path) = split_authority_and_path("localhost:8080/ws", "ws");
        assert_eq!(host, "localhost");
        assert_eq!(port, 8080);
        assert_eq!(path, "/ws");
    }

    #[test]
    fn parse_ws_url_default_path() {
        let (_, _, path) = split_authority_and_path("host/", "ws");
        assert_eq!(path, "/");
    }

    #[test]
    fn parse_ws_url_no_path_defaults_to_slash() {
        let (_, _, path) = split_authority_and_path("host", "ws");
        assert_eq!(path, "/");
    }

    #[test]
    fn parse_ws_url_empty_string() {
        let (host, port, path) = split_authority_and_path("", "ws");
        assert_eq!(host, "");
        assert_eq!(port, 80);
        assert_eq!(path, "/");
    }

    #[test]
    fn parse_ws_url_ipv4_with_port() {
        let (host, port, path) = split_authority_and_path("127.0.0.1:9222/json", "ws");
        assert_eq!(host, "127.0.0.1");
        assert_eq!(port, 9222);
        assert_eq!(path, "/json");
    }

    #[test]
    fn parse_ws_url_query_string() {
        let (host, port, path) = split_authority_and_path("example.com/ws?token=abc", "ws");
        assert_eq!(host, "example.com");
        assert_eq!(port, 80);
        assert!(path.starts_with("/ws"));
    }

    #[test]
    fn parse_ws_url_deep_path() {
        let (host, _, path) = split_authority_and_path("host/a/b/c/d", "ws");
        assert_eq!(host, "host");
        assert_eq!(path, "/a/b/c/d");
    }

    // @trace REQ-ENG-006 [code:bun_uws] — frame length encoding now shares
    // bun_uws::ws_codec's layout via push_masked_len (the client→server masked
    // length bytes). The masking key itself is per-frame random in the live
    // path, so these unit tests verify only the length-byte shape.
    #[test]
    fn push_masked_len_empty() {
        let mut frame = Vec::new();
        push_masked_len(&mut frame, 0);
        assert_eq!(frame.len(), 1);
        assert_eq!(frame[0] & 0x7F, 0); // length = 0
    }

    #[test]
    fn push_masked_len_short() {
        let mut frame = Vec::new();
        push_masked_len(&mut frame, 5);
        assert_eq!(frame.len(), 1);
        assert_eq!(frame[0] & 0x7F, 5); // length = 5
    }

    #[test]
    fn push_masked_len_medium() {
        let mut frame = Vec::new();
        let payload = vec![0u8; 200];
        push_masked_len(&mut frame, payload.len());
        // 1 byte + 2 bytes extended length
        assert_eq!(frame.len(), 3);
        assert_eq!(frame[0] & 0x7F, 126); // 126 signals 16-bit length
        let ext_len = u16::from_be_bytes([frame[1], frame[2]]);
        assert_eq!(ext_len, 200);
    }

    #[test]
    fn push_masked_len_large() {
        let mut frame = Vec::new();
        let payload = vec![0u8; 70000];
        push_masked_len(&mut frame, payload.len());
        // 1 byte + 8 bytes extended length
        assert_eq!(frame.len(), 9);
        assert_eq!(frame[0] & 0x7F, 127); // 127 signals 64-bit length
    }

    #[test]
    fn ws_message_debug_variants() {
        let text = WsMessage::Text("hello".to_string());
        let binary = WsMessage::Binary(vec![1, 2, 3]);
        let close = WsMessage::Close;
        assert!(format!("{:?}", text).contains("Text"));
        assert!(format!("{:?}", binary).contains("Binary"));
        assert!(format!("{:?}", close).contains("Close"));
    }

    // Async WS state machine: liveness transitions of a registry entry.
    // dead (no client, no slot) → connecting (slot) → open (client) → dead.
    #[test]
    fn ws_entry_liveness_transitions() {
        let connecting = WsEntry {
            client: None,
            connect_slot: Some(Arc::new(Mutex::new(None))),
            close_requested: false,
            close_initiated: false,
            realm_global: ::std::ptr::null_mut(),
            js_obj_key: "ws_test".to_string(),
        };
        assert!(connecting.is_live(), "connecting entry is live");

        let dead = WsEntry {
            client: None,
            connect_slot: None,
            close_requested: false,
            close_initiated: false,
            realm_global: ::std::ptr::null_mut(),
            js_obj_key: "ws_test".to_string(),
        };
        assert!(
            !dead.is_live(),
            "dead entry (post-close/failure) is not live"
        );
    }

    // Stealth plumbing: the wss path consumes the exact same profile source
    // fetch() uses. `get_fetch_stealth_profile` must round-trip what
    // `set_fetch_stealth_profile` stored (default-None when unset).
    #[test]
    fn fetch_stealth_profile_getter_roundtrip() {
        let saved = crate::fetch_api::get_fetch_stealth_profile();
        crate::fetch_api::set_fetch_stealth_profile(Some(
            bao_stealth::StealthProfile::firefox_default(),
        ));
        let got = crate::fetch_api::get_fetch_stealth_profile();
        crate::fetch_api::set_fetch_stealth_profile(saved);
        let p = got.expect("profile must round-trip");
        assert!(
            !p.tls.cipher_suites.is_empty(),
            "firefox profile must carry cipher suites (wss stealth source)"
        );
    }
}