wasm-rquickjs 0.3.5

Tool for wrapping JavaScript modules as WebAssembly components using the QuickJS engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
import { URL } from '__wasm_rquickjs_builtin/url_native';
import * as querystring from 'node:querystring';
import { ERR_INVALID_ARG_TYPE, ERR_MISSING_ARGS } from '__wasm_rquickjs_builtin/internal/errors';

const __URLSearchParams__ = "__URLSearchParams__";
const __URLSearchParamsURL__ = "__URLSearchParamsURL__";
const __URLSearchParamsUpdating__ = "__URLSearchParamsUpdating__";
const __URLSearchParamsIterator__ = "__URLSearchParamsIterator__";
const customInspectSymbol = Symbol.for('nodejs.util.inspect.custom');

function makeInvalidThisError(type) {
    const err = new TypeError('Value of "this" must be of type ' + type);
    err.code = 'ERR_INVALID_THIS';
    return err;
}

function makeTupleError() {
    const err = new TypeError('Each query pair must be an iterable [name, value] tuple');
    err.code = 'ERR_INVALID_TUPLE';
    return err;
}

function makeNotIterableError() {
    const err = new TypeError('Query pairs must be iterable');
    err.code = 'ERR_ARG_NOT_ITERABLE';
    return err;
}

function requireSearchParams(value) {
    if (!value || !Object.prototype.hasOwnProperty.call(value, __URLSearchParams__)) {
        throw makeInvalidThisError('URLSearchParams');
    }
    const url = value[__URLSearchParamsURL__];
    if (url && !value[__URLSearchParamsUpdating__]) {
        value[__URLSearchParams__] = parseToPairs(url.search);
    }
    return value[__URLSearchParams__];
}

function requireIterator(value) {
    if (!value || !Object.prototype.hasOwnProperty.call(value, __URLSearchParamsIterator__)) {
        throw makeInvalidThisError('URLSearchParamsIterator');
    }
    return value[__URLSearchParamsIterator__];
}

function toUSVString(value) {
    if (typeof value === 'symbol') {
        throw new TypeError('Cannot convert a Symbol value to a string');
    }
    const string = String(value);
    let out = '';
    for (let i = 0; i < string.length; i++) {
        const c = string.charCodeAt(i);
        if (c >= 0xD800 && c <= 0xDBFF) {
            if (i + 1 < string.length) {
                const d = string.charCodeAt(i + 1);
                if (d >= 0xDC00 && d <= 0xDFFF) {
                    out += string[i] + string[i + 1];
                    i++;
                    continue;
                }
            }
            out += '\uFFFD';
        } else if (c >= 0xDC00 && c <= 0xDFFF) {
            out += '\uFFFD';
        } else {
            out += string[i];
        }
    }
    return out;
}

function URLSearchParamsPolyfill(search) {
    if (!(this instanceof URLSearchParamsPolyfill)) {
        throw new TypeError("Class constructor URLSearchParams cannot be invoked without 'new'");
    }

    if (search === undefined || search === null) {
        search = "";
    } else if (search instanceof URLSearchParams || search instanceof URLSearchParamsPolyfill) {
        search = search.toString();
    }

    Object.defineProperty(this, __URLSearchParams__, {
        value: parseToPairs(search),
        writable: true,
        configurable: true,
    });
}

const prototype = URLSearchParamsPolyfill.prototype;

prototype.append = function (name, value) {
    const pairs = requireSearchParams(this);
    if (arguments.length < 2) {
        throw new ERR_MISSING_ARGS('name', 'value');
    }
    pairs.push([toUSVString(name), toUSVString(value)]);
    updateLinkedUrl(this);
};

prototype['delete'] = function (name) {
    const pairs = requireSearchParams(this);
    if (arguments.length < 1) {
        throw new ERR_MISSING_ARGS('name');
    }
    const key = toUSVString(name);
    if (arguments.length > 1) {
        const val = toUSVString(arguments[1]);
        this[__URLSearchParams__] = pairs.filter((pair) => pair[0] !== key || pair[1] !== val);
    } else {
        this[__URLSearchParams__] = pairs.filter((pair) => pair[0] !== key);
    }
    updateLinkedUrl(this);
};

prototype.get = function (name) {
    const pairs = requireSearchParams(this);
    if (arguments.length < 1) {
        throw new ERR_MISSING_ARGS('name');
    }
    const key = toUSVString(name);
    for (let i = 0; i < pairs.length; i++) {
        if (pairs[i][0] === key) return pairs[i][1];
    }
    return null;
};

prototype.getAll = function (name) {
    const pairs = requireSearchParams(this);
    if (arguments.length < 1) {
        throw new ERR_MISSING_ARGS('name');
    }
    const key = toUSVString(name);
    const out = [];
    for (let i = 0; i < pairs.length; i++) {
        if (pairs[i][0] === key) out.push(pairs[i][1]);
    }
    return out;
};

prototype.has = function (name, value) {
    const pairs = requireSearchParams(this);
    if (arguments.length < 1) {
        throw new ERR_MISSING_ARGS('name');
    }
    const key = toUSVString(name);
    const hasValue = value !== undefined;
    const val = hasValue ? toUSVString(value) : undefined;
    for (let i = 0; i < pairs.length; i++) {
        if (pairs[i][0] === key && (!hasValue || pairs[i][1] === val)) return true;
    }
    return false;
};

prototype.set = function set(name, value) {
    const pairs = requireSearchParams(this);
    if (arguments.length < 2) {
        throw new ERR_MISSING_ARGS('name', 'value');
    }
    const key = toUSVString(name);
    const val = toUSVString(value);
    let found = false;
    const out = [];
    for (let i = 0; i < pairs.length; i++) {
        if (pairs[i][0] === key) {
            if (!found) {
                out.push([key, val]);
                found = true;
            }
        } else {
            out.push(pairs[i]);
        }
    }
    if (!found) out.push([key, val]);
    this[__URLSearchParams__] = out;
    updateLinkedUrl(this);
};

prototype.toString = function () {
    const pairs = requireSearchParams(this);
    const query = [];
    for (let i = 0; i < pairs.length; i++) {
        query.push(encode(pairs[i][0]) + '=' + encode(pairs[i][1]));
    }
    return query.join('&');
};

export const URLSearchParams = URLSearchParamsPolyfill;

// Define searchParams getter on URL.prototype
Object.defineProperty(URL.prototype, "searchParams", {
    get: function getSearchParams() {
        requireURLReceiver(this, 'searchParams', 'get');
        if (!urlSearchParamsCache.has(this)) {
            const params = new URLSearchParams(this.search);
            Object.defineProperty(params, __URLSearchParamsURL__, {
                value: this,
                writable: true,
                configurable: true,
            });
            Object.defineProperty(params, __URLSearchParamsUpdating__, {
                value: false,
                writable: true,
                configurable: true,
            });
            urlSearchParamsCache.set(this, params);
        }
        return urlSearchParamsCache.get(this);
    },
    enumerable: true,
    configurable: true
});

const urlSearchParamsCache = new WeakMap();

function syncSearchParamsFromUrl(url) {
    const params = urlSearchParamsCache.get(url);
    if (params && !params[__URLSearchParamsUpdating__]) {
        params[__URLSearchParams__] = parseToPairs(url.search);
    }
}

function updateLinkedUrl(params) {
    const url = params[__URLSearchParamsURL__];
    if (!url) return;
    if (!Object.prototype.hasOwnProperty.call(params, __URLSearchParamsUpdating__)) {
        Object.defineProperty(params, __URLSearchParamsUpdating__, {
            value: false,
            writable: true,
            configurable: true,
        });
    }
    params[__URLSearchParamsUpdating__] = true;
    try {
        const query = params.toString();
        url.search = query ? '?' + query : '';
    } finally {
        params[__URLSearchParamsUpdating__] = false;
    }
}

// Wrap createObjectURL to validate Blob argument with proper error code
const _origCreateObjectURL = URL.createObjectURL;
URL.createObjectURL = function createObjectURL(obj) {
    if (!obj || typeof obj !== 'object' ||
        (typeof obj.arrayBuffer !== 'function' && typeof obj.stream !== 'function')) {
        throw new ERR_INVALID_ARG_TYPE('object', 'Blob', obj);
    }
    return _origCreateObjectURL.call(this, obj);
};

// Match Node.js behavior: throw ERR_MISSING_ARGS when no URL was passed.
const _origCanParse = URL.canParse;
URL.canParse = function canParse(url, base) {
    if (arguments.length === 0) {
        throw new ERR_MISSING_ARGS('url');
    }
    if (arguments.length > 1) {
        return _origCanParse.call(this, url, base);
    }
    return _origCanParse.call(this, url);
};

const _origRevokeObjectURL = URL.revokeObjectURL;
URL.revokeObjectURL = function revokeObjectURL(url) {
    if (arguments.length === 0) {
        throw new ERR_MISSING_ARGS('url');
    }
    return _origRevokeObjectURL.call(this, url);
};

const USPProto = URLSearchParams.prototype;

USPProto[Symbol.toStringTag] = 'URLSearchParams';

USPProto.forEach = function (callback, thisArg) {
    requireSearchParams(this);
    if (typeof callback !== 'function') {
        throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback);
    }
    for (let i = 0; i < requireSearchParams(this).length; i++) {
        const pairs = requireSearchParams(this);
        callback.call(thisArg, pairs[i][1], pairs[i][0], this);
    }
};

USPProto.sort = function () {
    const pairs = requireSearchParams(this);
    pairs.sort((a, b) => a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0);
    updateLinkedUrl(this);
};

USPProto.keys = function () {
    requireSearchParams(this);
    return makeIterator(this, 'key');
};

USPProto.values = function () {
    requireSearchParams(this);
    return makeIterator(this, 'value');
};

USPProto.entries = function () {
    requireSearchParams(this);
    return makeIterator(this, 'entry');
};

USPProto[Symbol.iterator] = USPProto.entries;

USPProto[customInspectSymbol] = function inspectURLSearchParams(depth, opts) {
    const pairs = requireSearchParams(this);
    if (depth < 0) return '[Object]';
    if (pairs.length === 0) return 'URLSearchParams {}';
    const entries = pairs.map((pair) => inspectString(pair[0]) + ' => ' + inspectString(pair[1]));
    return formatInspectCollection('URLSearchParams', entries, opts);
};

Object.defineProperty(USPProto, 'size', {
    get: function getSize() {
        return requireSearchParams(this).length;
    }
});

const originalURLSearchParamsMethods = {
    append: USPProto.append,
    delete: USPProto['delete'],
    get: USPProto.get,
    getAll: USPProto.getAll,
    has: USPProto.has,
    set: USPProto.set,
    sort: USPProto.sort,
    entries: USPProto.entries,
    forEach: USPProto.forEach,
    keys: USPProto.keys,
    values: USPProto.values,
    toString: USPProto.toString,
    inspect: USPProto[customInspectSymbol],
};

const urlSearchParamsMethods = {
    append(...args) { return originalURLSearchParamsMethods.append.apply(this, args); },
    ['delete'](...args) { return originalURLSearchParamsMethods.delete.apply(this, args); },
    get(...args) { return originalURLSearchParamsMethods.get.apply(this, args); },
    getAll(...args) { return originalURLSearchParamsMethods.getAll.apply(this, args); },
    has(...args) { return originalURLSearchParamsMethods.has.apply(this, args); },
    set(...args) { return originalURLSearchParamsMethods.set.apply(this, args); },
    sort(...args) { return originalURLSearchParamsMethods.sort.apply(this, args); },
    entries(...args) { return originalURLSearchParamsMethods.entries.apply(this, args); },
    forEach(...args) { return originalURLSearchParamsMethods.forEach.apply(this, args); },
    keys(...args) { return originalURLSearchParamsMethods.keys.apply(this, args); },
    values(...args) { return originalURLSearchParamsMethods.values.apply(this, args); },
    toString(...args) { return originalURLSearchParamsMethods.toString.apply(this, args); },
    [customInspectSymbol](...args) { return originalURLSearchParamsMethods.inspect.apply(this, args); },
};

for (const name of ['append', 'delete', 'get', 'getAll', 'has', 'set', 'sort', 'entries', 'forEach', 'keys', 'values', 'toString']) {
    Object.defineProperty(USPProto, name, {
        value: urlSearchParamsMethods[name],
        writable: true,
        enumerable: true,
        configurable: true,
    });
}
Object.defineProperty(USPProto, Symbol.iterator, {
    value: USPProto.entries,
    writable: true,
    enumerable: false,
    configurable: true,
});
Object.defineProperty(USPProto, customInspectSymbol, {
    value: urlSearchParamsMethods[customInspectSymbol],
    writable: true,
    enumerable: false,
    configurable: true,
});

const ENCODE_REPLACE = {
    '!': '%21',
    "'": '%27',
    '(': '%28',
    ')': '%29',
    '~': '%7E',
    '%20': '+',
    '%00': '\x00'
};

function encode(str) {
    return encodeURIComponent(toUSVString(str)).replace(/[!'\(\)~]|%20|%00/g, function (match) {
        return ENCODE_REPLACE[match];
    });
}

function decode(str) {
    return str
        .replace(/[ +]/g, '%20')
        .replace(/(%[a-f0-9]{2})+/ig, function (match) {
            return decodeURIComponent(match);
        });
}

const URLSearchParamsIteratorPrototype = {};
Object.defineProperty(URLSearchParamsIteratorPrototype, Symbol.toStringTag, {
    value: 'URLSearchParams Iterator',
    writable: false,
    enumerable: false,
    configurable: true,
});

function makeIterator(params, kind) {
    const iterator = {
        next: function next() {
            const state = requireIterator(this);
            const pairs = requireSearchParams(state.params);
            if (state.index >= pairs.length) {
                return {done: true, value: undefined};
            }
            const pair = pairs[state.index++];
            const value = state.kind === 'key' ? pair[0] : state.kind === 'value' ? pair[1] : [pair[0], pair[1]];
            return {done: false, value};
        }
    };
    Object.setPrototypeOf(iterator, URLSearchParamsIteratorPrototype);

    Object.defineProperty(iterator, __URLSearchParamsIterator__, {
        value: { params, kind, index: 0 },
        writable: true,
        configurable: true,
    });

    iterator[Symbol.iterator] = function () {
        return iterator;
    };
    Object.defineProperty(iterator, Symbol.toStringTag, {
        value: 'URLSearchParams Iterator',
        writable: false,
        enumerable: false,
        configurable: true,
    });
    iterator[customInspectSymbol] = function inspectURLSearchParamsIterator(depth, opts) {
        const iteratorState = requireIterator(this);
        if (depth < 0) return '[Object]';
        const pairs = requireSearchParams(iteratorState.params);
        const values = [];
        for (let i = iteratorState.index; i < pairs.length; i++) {
            const pair = pairs[i];
            if (iteratorState.kind === 'key') {
                values.push(inspectString(pair[0]));
            } else if (iteratorState.kind === 'value') {
                values.push(inspectString(pair[1]));
            } else {
                values.push('[ ' + inspectString(pair[0]) + ', ' + inspectString(pair[1]) + ' ]');
            }
        }
        return formatInspectCollection('URLSearchParams Iterator', values, opts);
    };

    return iterator;
}

function inspectString(value) {
    return "'" + String(value).replace(/\\/g, '\\\\').replace(/'/g, "\\'") + "'";
}

function formatInspectCollection(name, entries, opts) {
    if (entries.length === 0) return name + ' {  }';
    if (opts && opts.breakLength === 1) {
        return name + ' {\n  ' + entries.join(',\n  ') + ' }';
    }
    return name + ' { ' + entries.join(', ') + ' }';
}

function parseToPairs(search) {
    const pairs = [];

    if (typeof search === 'symbol') {
        toUSVString(search);
    }

    if (typeof search === "object" || typeof search === 'function') {
        const iterator = search && search[Symbol.iterator];
        if (iterator !== undefined) {
            if (typeof iterator !== 'function') {
                throw makeNotIterableError();
            }
            const iterable = iterator.call(search);
            if (!iterable || typeof iterable.next !== 'function') {
                throw makeNotIterableError();
            }
            let step;
            while (!(step = iterable.next()).done) {
                const item = step.value;
                if (!item || typeof item[Symbol.iterator] !== 'function') {
                    throw makeTupleError();
                }
                const itemIterator = item[Symbol.iterator]();
                const first = itemIterator.next();
                const second = itemIterator.next();
                const third = itemIterator.next();
                if (first.done || second.done || !third.done) {
                    throw makeTupleError();
                }
                pairs.push([toUSVString(first.value), toUSVString(second.value)]);
            }
        } else {
            const symbols = Object.getOwnPropertySymbols(search || {});
            if (symbols.length > 0) {
                toUSVString(symbols[0]);
            }
            const keys = Object.keys(search || {});
            for (let i = 0; i < keys.length; i++) {
                const key = keys[i];
                pairs.push([toUSVString(key), toUSVString(search[key])]);
            }
        }
    } else {
        search = String(search);
        if (search.startsWith("?")) {
            search = search.slice(1);
        }

        for (const value of search.split("&")) {
            const index = value.indexOf('=');
            if (index !== -1) {
                pairs.push([decode(value.slice(0, index)), decode(value.slice(index + 1))]);
            } else if (value) {
                pairs.push([decode(value), '']);
            }
        }
    }

    return pairs;
}

const originalURLDescriptors = {};
for (const name of ['href', 'origin', 'protocol', 'username', 'password', 'host', 'hostname', 'port', 'pathname', 'search', 'hash']) {
    originalURLDescriptors[name] = Object.getOwnPropertyDescriptor(URL.prototype, name);
}
originalURLDescriptors.searchParams = Object.getOwnPropertyDescriptor(URL.prototype, 'searchParams');
originalURLDescriptors.toString = Object.getOwnPropertyDescriptor(URL.prototype, 'toString');
originalURLDescriptors.toJSON = Object.getOwnPropertyDescriptor(URL.prototype, 'toJSON');

function makeURLReceiverError(property, operation) {
    if (operation === 'method' || (operation === 'get' && (property === 'href' || property === 'search'))) {
        return new TypeError('Receiver must be an instance of class URL');
    }
    return new TypeError('Cannot read private member from an object whose class did not declare it');
}

function requireURLReceiver(receiver, property, operation) {
    if (!(receiver instanceof URL)) {
        throw makeURLReceiverError(property, operation);
    }
}

function getURLProperty(receiver, property) {
    requireURLReceiver(receiver, property, 'get');
    return originalURLDescriptors[property].get.call(receiver);
}

function setURLProperty(receiver, property, value) {
    requireURLReceiver(receiver, property, 'set');
    try {
        return originalURLDescriptors[property].set.call(receiver, value);
    } catch (error) {
        if (error && error.name !== 'TypeError') {
            throw new TypeError(error.message);
        }
        throw error;
    } finally {
        syncSearchParamsFromUrl(receiver);
    }
}

function callURLMethod(receiver, method, args) {
    requireURLReceiver(receiver, method, 'method');
    return originalURLDescriptors[method].value.apply(receiver, args);
}

const urlPrototypeMethods = {
    toString(...args) { return callURLMethod(this, 'toString', args); },
    toJSON(...args) { return callURLMethod(this, 'toJSON', args); },
    [customInspectSymbol]() { return this.href; },
};

const urlAccessors = {
    get href() { return getURLProperty(this, 'href'); },
    set href(value) { setURLProperty(this, 'href', value); },
    get origin() { return getURLProperty(this, 'origin'); },
    get protocol() { return getURLProperty(this, 'protocol'); },
    set protocol(value) { setURLProperty(this, 'protocol', value); },
    get username() { return getURLProperty(this, 'username'); },
    set username(value) { setURLProperty(this, 'username', value); },
    get password() { return getURLProperty(this, 'password'); },
    set password(value) { setURLProperty(this, 'password', value); },
    get host() { return getURLProperty(this, 'host'); },
    set host(value) { setURLProperty(this, 'host', value); },
    get hostname() { return getURLProperty(this, 'hostname'); },
    set hostname(value) { setURLProperty(this, 'hostname', value); },
    get port() { return getURLProperty(this, 'port'); },
    set port(value) { setURLProperty(this, 'port', value); },
    get pathname() { return getURLProperty(this, 'pathname'); },
    set pathname(value) { setURLProperty(this, 'pathname', value); },
    get search() { return getURLProperty(this, 'search'); },
    set search(value) { setURLProperty(this, 'search', value); },
    get searchParams() { return originalURLDescriptors.searchParams.get.call(this); },
    get hash() { return getURLProperty(this, 'hash'); },
    set hash(value) { setURLProperty(this, 'hash', value); },
};

try {
    Object.defineProperty(URL.prototype, 'toString', {
        value: urlPrototypeMethods.toString,
        writable: true,
        enumerable: true,
        configurable: true,
    });
    for (const property of ['href', 'origin', 'protocol', 'username', 'password', 'host', 'hostname', 'port', 'pathname', 'search', 'searchParams', 'hash']) {
        const descriptor = Object.getOwnPropertyDescriptor(urlAccessors, property);
        descriptor.enumerable = true;
        descriptor.configurable = true;
        Object.defineProperty(URL.prototype, property, descriptor);
    }
    Object.defineProperty(URL.prototype, 'toJSON', {
        value: urlPrototypeMethods.toJSON,
        writable: true,
        enumerable: true,
        configurable: true,
    });
    Object.defineProperty(URL.prototype, customInspectSymbol, {
        value: urlPrototypeMethods[customInspectSymbol],
        writable: true,
        enumerable: false,
        configurable: true,
    });
} catch (_) {
    // rquickjs currently exposes native class properties as non-configurable.
    // Keep the native descriptors when they cannot be redefined; behavioral
    // compatibility is still provided by the native URL implementation and the
    // JS URLSearchParams wrapper above.
}
try {
    Object.defineProperty(URL.prototype, 'toJSON', {
        value: urlPrototypeMethods.toJSON,
        writable: true,
        enumerable: true,
        configurable: true,
    });
} catch (_) {}
try {
    Object.defineProperty(URL.prototype, customInspectSymbol, {
        value: urlPrototypeMethods[customInspectSymbol],
        writable: true,
        enumerable: false,
        configurable: true,
    });
} catch (_) {}
Object.defineProperty(URL.prototype, Symbol.toStringTag, {
    value: 'URL',
    writable: false,
    enumerable: false,
    configurable: true,
});

// --- node:url module APIs ---

export { URL };

function makeNodeError(code, Type, message) {
    const err = new Type(message);
    err.code = code;
    return err;
}

function makeInvalidUrlError(input) {
    const err = makeNodeError('ERR_INVALID_URL', TypeError, 'Invalid URL');
    err.input = input;
    return err;
}

const FORBIDDEN_HOST_CHARS = /[\0\t\n\r #%/<>?@\\^|]/;
const WARN_INVALID_HOSTNAME_KEY = '__wasm_rquickjs_url_warned_invalid_hostname';

if (globalThis[WARN_INVALID_HOSTNAME_KEY] === undefined) {
    globalThis[WARN_INVALID_HOSTNAME_KEY] = false;
}

function validateHostName(hostname, ipv6Host, input) {
    if (!hostname) {
        return;
    }
    if (FORBIDDEN_HOST_CHARS.test(hostname)) {
        throw makeInvalidUrlError(input);
    }
}

function emitInvalidUrlDeprecation(input) {
    if (globalThis[WARN_INVALID_HOSTNAME_KEY]) {
        return;
    }
    globalThis[WARN_INVALID_HOSTNAME_KEY] = true;

    const warningMessage = `The URL ${input} is invalid. Future versions of Node.js will throw an error.`;

    if (typeof process !== 'undefined' && typeof process.emitWarning === 'function') {
        process.emitWarning(warningMessage, {
            type: 'DeprecationWarning',
            code: 'DEP0170',
        });
    }

    if (typeof process !== 'undefined' &&
        process.stderr &&
        typeof process.stderr.write === 'function') {
        process.stderr.write(`[DEP0170] DeprecationWarning: ${warningMessage}\n`);
    }
}

const ENCODE_CHARS_RE = /[\x00-\x1F\x20"#%?<>{}|\\^`~\[\]\x7F]/g;

function percentEncode(char) {
    const code = char.charCodeAt(0);
    if (code < 0x10) return '%0' + code.toString(16).toUpperCase();
    return '%' + code.toString(16).toUpperCase();
}

export function fileURLToPath(url, options) {
    const windows = options && options.windows === true;

    if (typeof url === 'string') {
        url = new URL(url);
    } else if (!(url instanceof URL)) {
        throw makeNodeError(
            'ERR_INVALID_ARG_TYPE',
            TypeError,
            'The "url" argument must be of type string or an instance of URL'
        );
    }

    if (url.protocol !== 'file:') {
        throw makeNodeError(
            'ERR_INVALID_URL_SCHEME',
            TypeError,
            'The URL must be of scheme file'
        );
    }

    if (windows) {
        const hostname = url.hostname;
        let pathname = url.pathname;

        if (hostname) {
            pathname = '\\\\' + hostname + pathname.replace(/\//g, '\\');
            return decodeURIComponent(pathname);
        }

        const match = pathname.match(/^\/([A-Za-z])(?:[:|])(\/.*)?$/);
        if (match) {
            const drive = match[1].toUpperCase();
            const rest = match[2] || '\\';
            return drive + ':' + decodeURIComponent(rest).replace(/\//g, '\\');
        }

        return decodeURIComponent(pathname).replace(/\//g, '\\');
    }

    if (url.hostname) {
        throw makeNodeError(
            'ERR_INVALID_FILE_URL_HOST',
            TypeError,
            'File URL host must be "localhost" or empty on the current platform'
        );
    }

    const pathname = url.pathname;
    if (pathname.indexOf('%2F') !== -1 || pathname.indexOf('%2f') !== -1) {
        throw makeNodeError(
            'ERR_INVALID_FILE_URL_PATH',
            TypeError,
            'File URL path must not include encoded / characters'
        );
    }

    return decodeURIComponent(pathname);
}

export function pathToFileURL(path, options) {
    if (typeof path !== 'string') {
        throw makeNodeError(
            'ERR_INVALID_ARG_TYPE',
            TypeError,
            'The "path" argument must be of type string'
        );
    }

    const windows = options && options.windows === true;

    if (windows) {
        let resolved = path.replace(/\\/g, '/');

        const extLocalMatch = resolved.match(/^\/\/\?\/([A-Za-z]:)(\/.*)?$/);
        if (extLocalMatch) {
            const drive = extLocalMatch[1][0].toUpperCase();
            const rest = extLocalMatch[2] || '/';
            return new URL('file:///' + drive + ':' + rest.replace(ENCODE_CHARS_RE, percentEncode));
        }

        const extUncMatch = resolved.match(/^\/\/\?\/UNC\/([^/]+)(\/.*)?$/);
        if (extUncMatch) {
            const host = extUncMatch[1];
            const rest = extUncMatch[2] || '/';
            return new URL('file://' + host + rest.replace(ENCODE_CHARS_RE, percentEncode));
        }

        if (resolved.startsWith('//')) {
            const rest = resolved.slice(2);
            if (rest.startsWith('/') || rest === '') {
                throw makeNodeError(
                    'ERR_INVALID_ARG_VALUE',
                    TypeError,
                    'The argument \'path\' must be a string. Received ' + JSON.stringify(path)
                );
            }
            const slashIdx = rest.indexOf('/');
            if (slashIdx === -1) {
                throw makeNodeError(
                    'ERR_INVALID_ARG_VALUE',
                    TypeError,
                    'The argument \'path\' must be a string. Received ' + JSON.stringify(path)
                );
            }
            const host = rest.slice(0, slashIdx);
            const pathPart = rest.slice(slashIdx);
            return new URL('file://' + host + pathPart.replace(ENCODE_CHARS_RE, percentEncode));
        }

        const driveMatch = resolved.match(/^([A-Za-z]):(\/.*)?$/);
        if (driveMatch) {
            const drive = driveMatch[1].toUpperCase();
            const rest = driveMatch[2] || '/';
            return new URL('file:///' + drive + ':' + rest.replace(ENCODE_CHARS_RE, percentEncode));
        }

        if (!resolved.startsWith('/')) {
            resolved = '/' + resolved;
        }
        return new URL('file://' + resolved.replace(ENCODE_CHARS_RE, percentEncode));
    }

    if (!path.startsWith('/')) {
        path = '/' + path;
    }

    const encoded = path.replace(ENCODE_CHARS_RE, percentEncode);
    return new URL('file://' + encoded);
}

export function urlToHttpOptions(url) {
    const options = {
        protocol: url.protocol,
        hostname: typeof url.hostname === 'string' && url.hostname.startsWith('[')
            ? url.hostname.slice(1, -1)
            : url.hostname,
    };

    if (url.port !== '') {
        options.port = Number(url.port);
    }

    if (url.username || url.password) {
        options.auth = url.password
            ? url.username + ':' + url.password
            : url.username;
    }

    options.pathname = url.pathname;
    options.search = url.search;
    options.hash = url.hash;
    options.href = url.href;
    options.path = (url.pathname || '') + (url.search || '');

    return options;
}

export function format(urlObject, options) {
    if (typeof urlObject === 'string') {
        return formatLegacy(legacyParse(urlObject));
    }

    if (typeof urlObject !== 'object' || urlObject === null) {
        throw makeNodeError(
            'ERR_INVALID_ARG_TYPE',
            TypeError,
            'The "urlObject" argument must be one of type object or string'
        );
    }

    if (urlObject instanceof URL ||
        (typeof urlObject.href === 'string' &&
         typeof urlObject.protocol === 'string' &&
         typeof urlObject.hostname === 'string' &&
         !('slashes' in urlObject))) {
        if (options !== undefined && options !== null && typeof options !== 'object') {
            throw makeNodeError(
                'ERR_INVALID_ARG_TYPE',
                TypeError,
                'The "options" argument must be of type object'
            );
        }

        const opts = options || {};
        const auth = opts.auth !== undefined ? opts.auth : true;
        const fragment = opts.fragment !== undefined ? opts.fragment : true;
        const search = opts.search !== undefined ? opts.search : true;

        let result = urlObject.protocol;

        if (urlObject.host !== undefined && urlObject.host !== '') {
            result += '//';
            if (auth && (urlObject.username || urlObject.password)) {
                result += urlObject.username;
                if (urlObject.password) {
                    result += ':' + urlObject.password;
                }
                result += '@';
            }
            result += urlObject.host;
        } else if (urlObject.protocol === 'file:') {
            result += '//';
        }

        result += urlObject.pathname || '';

        if (search && urlObject.search) {
            result += urlObject.search;
        }

        if (fragment && urlObject.hash) {
            result += urlObject.hash;
        }

        return result;
    }

    return formatLegacy(urlObject);
}

function formatLegacy(urlObject) {
    let result = '';
    const protocol = urlObject.protocol || '';
    let pathname = urlObject.pathname || '';
    let host = '';

    if (urlObject.host) {
        host = urlObject.host;
    } else if (urlObject.hostname) {
        host = urlObject.hostname.indexOf(':') !== -1
            ? '[' + urlObject.hostname + ']'
            : urlObject.hostname;
        if (urlObject.port) {
            host += ':' + urlObject.port;
        }
    }

    if (urlObject.auth) {
        host = urlObject.auth + '@' + host;
    }

    if (protocol) {
        result += protocol;
    }

    if (urlObject.slashes || isSlashedProtocol(protocol)) {
        if (urlObject.slashes || host) {
            if (pathname && pathname.charAt(0) !== '/') {
                pathname = '/' + pathname;
            }
            result += '//';
        } else if (protocol.toLowerCase().startsWith('file')) {
            result += '//';
        }
    }

    result += host;
    result += pathname;

    if (urlObject.search) {
        result += urlObject.search;
    } else if (urlObject.query && typeof urlObject.query === 'object') {
        result += '?' + querystring.stringify(urlObject.query);
    }

    if (urlObject.hash) {
        result += urlObject.hash;
    }

    return result;
}

const SLASHED_PROTOCOLS = {
    'http:': true, 'https:': true, 'ftp:': true, 'gopher:': true, 'file:': true,
    'http': true, 'https': true, 'ftp': true, 'gopher': true, 'file': true,
    'ws:': true, 'wss:': true,
    'ws': true, 'wss': true,
};

const HOSTLESS_PROTOCOLS = {
    'javascript:': true,
    'javascript': true,
};

function isSlashedProtocol(protocol) {
    return typeof protocol === 'string' && SLASHED_PROTOCOLS[protocol.toLowerCase()] === true;
}

export function Url() {
    this.protocol = null;
    this.slashes = null;
    this.auth = null;
    this.host = null;
    this.port = null;
    this.hostname = null;
    this.hash = null;
    this.search = null;
    this.query = null;
    this.pathname = null;
    this.path = null;
    this.href = null;
}

Url.prototype.parse = function parseUrl(urlString, parseQueryString, slashesDenoteHost) {
    const parsed = parse(urlString, parseQueryString, slashesDenoteHost);
    Object.assign(this, parsed);
    return this;
};

Url.prototype.format = function formatUrl() {
    return formatLegacy(this);
};

Url.prototype.resolve = function resolveUrl(relative) {
    return this.resolveObject(relative).format();
};

Url.prototype.resolveObject = function resolveUrlObject(relative) {
    const rel = typeof relative === 'string' ? parse(relative, false, true) : relative;

    const result = new Url();
    Object.assign(result, this);

    // Hash is always overridden, even for empty relatives.
    result.hash = rel.hash;

    if (rel.href === '') {
        result.href = result.format();
        return result;
    }

    if (rel.slashes && !rel.protocol) {
        Object.keys(rel).forEach((key) => {
            if (key !== 'protocol') {
                result[key] = rel[key];
            }
        });

        if (isSlashedProtocol(result.protocol) && result.hostname && !result.pathname) {
            result.path = result.pathname = '/';
        }

        result.href = result.format();
        return result;
    }

    if (rel.protocol && rel.protocol !== result.protocol) {
        if (!isSlashedProtocol(rel.protocol)) {
            Object.assign(result, rel);
            result.href = result.format();
            return result;
        }

        result.protocol = rel.protocol;
        if (!rel.host && !/^file:?$/.test(rel.protocol) && !HOSTLESS_PROTOCOLS[rel.protocol]) {
            const relPath = (rel.pathname || '').split('/');
            while (relPath.length && !(rel.host = relPath.shift()));

            rel.host = rel.host || '';
            rel.hostname = rel.hostname || '';

            if (relPath[0] !== '') {
                relPath.unshift('');
            }
            if (relPath.length < 2) {
                relPath.unshift('');
            }

            result.pathname = relPath.join('/');
        } else {
            result.pathname = rel.pathname;
        }

        result.search = rel.search;
        result.query = rel.query;
        result.host = rel.host || '';
        result.auth = rel.auth;
        result.hostname = rel.hostname || rel.host;
        result.port = rel.port;

        if (result.pathname || result.search) {
            result.path = (result.pathname || '') + (result.search || '');
        }

        result.slashes = result.slashes || rel.slashes;
        result.href = result.format();
        return result;
    }

    const isSourceAbs = result.pathname && result.pathname.charAt(0) === '/';
    const isRelAbs = rel.host || (rel.pathname && rel.pathname.charAt(0) === '/');
    let mustEndAbs = isRelAbs || isSourceAbs || (result.host && rel.pathname);
    const removeAllDots = mustEndAbs;
    let srcPath = (result.pathname && result.pathname.split('/')) || [];
    const relPath = (rel.pathname && rel.pathname.split('/')) || [];
    const noLeadingSlashes = result.protocol && !isSlashedProtocol(result.protocol);

    if (noLeadingSlashes) {
        result.hostname = '';
        result.port = null;

        if (result.host) {
            if (srcPath[0] === '') {
                srcPath[0] = result.host;
            } else {
                srcPath.unshift(result.host);
            }
        }

        result.host = '';

        if (rel.protocol) {
            rel.hostname = null;
            rel.port = null;
            result.auth = null;

            if (rel.host) {
                if (relPath[0] === '') {
                    relPath[0] = rel.host;
                } else {
                    relPath.unshift(rel.host);
                }
            }

            rel.host = null;
        }

        mustEndAbs = mustEndAbs && (relPath[0] === '' || srcPath[0] === '');
    }

    if (isRelAbs) {
        if (rel.host || rel.host === '') {
            if (result.host !== rel.host) {
                result.auth = null;
            }
            result.host = rel.host;
            result.port = rel.port;
        }

        if (rel.hostname || rel.hostname === '') {
            if (result.hostname !== rel.hostname) {
                result.auth = null;
            }
            result.hostname = rel.hostname;
        }

        result.search = rel.search;
        result.query = rel.query;
        srcPath = relPath;
    } else if (relPath.length) {
        srcPath = srcPath || [];
        srcPath.pop();
        srcPath = srcPath.concat(relPath);
        result.search = rel.search;
        result.query = rel.query;
    } else if (rel.search !== null && rel.search !== undefined) {
        if (noLeadingSlashes) {
            result.hostname = result.host = srcPath.shift();

            const authInHost =
                result.host && result.host.indexOf('@') > 0 ? result.host.split('@') : false;
            if (authInHost) {
                result.auth = authInHost.shift();
                result.host = result.hostname = authInHost.shift();
            }
        }

        result.search = rel.search;
        result.query = rel.query;

        if (result.pathname !== null || result.search !== null) {
            result.path = (result.pathname ? result.pathname : '') +
                (result.search ? result.search : '');
        }

        result.href = result.format();
        return result;
    }

    if (!srcPath.length) {
        result.pathname = null;
        if (result.search) {
            result.path = '/' + result.search;
        } else {
            result.path = null;
        }

        result.href = result.format();
        return result;
    }

    let last = srcPath[srcPath.length - 1];
    const hasTrailingSlash = (
        ((result.host || rel.host || srcPath.length > 1) &&
            (last === '.' || last === '..')) ||
        last === ''
    );

    let up = 0;
    for (let i = srcPath.length - 1; i >= 0; i--) {
        last = srcPath[i];
        if (last === '.') {
            srcPath.splice(i, 1);
        } else if (last === '..') {
            srcPath.splice(i, 1);
            up++;
        } else if (up) {
            srcPath.splice(i, 1);
            up--;
        }
    }

    if (!mustEndAbs && !removeAllDots) {
        while (up--) {
            srcPath.unshift('..');
        }
    }

    if (mustEndAbs && srcPath[0] !== '' && (!srcPath[0] || srcPath[0].charAt(0) !== '/')) {
        srcPath.unshift('');
    }

    if (hasTrailingSlash && srcPath.join('/').slice(-1) !== '/') {
        srcPath.push('');
    }

    const isAbsolute = srcPath[0] === '' || (srcPath[0] && srcPath[0].charAt(0) === '/');

    if (noLeadingSlashes) {
        result.hostname = result.host = isAbsolute ? '' : srcPath.length ? srcPath.shift() : '';

        const authInHost = result.host && result.host.indexOf('@') > 0 ?
            result.host.split('@') : false;
        if (authInHost) {
            result.auth = authInHost.shift();
            result.host = result.hostname = authInHost.shift();
        }
    }

    mustEndAbs = mustEndAbs || (result.host && srcPath.length);

    if (mustEndAbs && !isAbsolute) {
        srcPath.unshift('');
    }

    if (!srcPath.length) {
        result.pathname = null;
        result.path = null;
    } else {
        result.pathname = srcPath.join('/');
    }

    if (result.pathname !== null || result.search !== null) {
        result.path = (result.pathname ? result.pathname : '') +
            (result.search ? result.search : '');
    }

    result.auth = rel.auth || result.auth;
    result.slashes = result.slashes || rel.slashes;
    result.href = result.format();
    return result;
};

function legacyParse(urlString, parseState) {
    const u = new Url();
    let shouldWarnInvalidHost = false;

    if (typeof urlString !== 'string') {
        return u;
    }

    let rest = urlString.trim();
    u.href = rest;

    const hashIdx = rest.indexOf('#');
    if (hashIdx !== -1) {
        u.hash = rest.slice(hashIdx);
        rest = rest.slice(0, hashIdx);
    }

    const qIdx = rest.indexOf('?');
    if (qIdx !== -1) {
        u.search = rest.slice(qIdx);
        u.query = rest.slice(qIdx + 1);
        rest = rest.slice(0, qIdx);
    }

    const protoMatch = rest.match(/^([a-zA-Z][a-zA-Z0-9.+\-]*:)/);
    if (protoMatch) {
        u.protocol = protoMatch[1].toLowerCase();
        rest = rest.slice(protoMatch[1].length);
    }

    if (rest.startsWith('//')) {
        u.slashes = true;
        rest = rest.slice(2);

        const authHostPath = rest;
        let pathStart = authHostPath.indexOf('/');
        if (pathStart === -1) pathStart = authHostPath.length;

        const authHost = authHostPath.slice(0, pathStart);
        rest = authHostPath.slice(pathStart);

        const atIdx = authHost.lastIndexOf('@');
        if (atIdx !== -1) {
            u.auth = decodeURIComponent(authHost.slice(0, atIdx));
            const hostPart = authHost.slice(atIdx + 1);
            shouldWarnInvalidHost = parseHostPort(u, hostPart, urlString) || shouldWarnInvalidHost;
        } else {
            shouldWarnInvalidHost = parseHostPort(u, authHost, urlString) || shouldWarnInvalidHost;
        }
    } else if (u.protocol && !isSlashedProtocol(u.protocol) && !HOSTLESS_PROTOCOLS[u.protocol]) {
        let pathStart = rest.indexOf('/');
        if (pathStart === -1) {
            pathStart = rest.length;
        }

        const authHost = rest.slice(0, pathStart);
        rest = rest.slice(pathStart);

        const atIdx = authHost.lastIndexOf('@');
        if (atIdx !== -1) {
            u.auth = decodeURIComponent(authHost.slice(0, atIdx));
            const hostPart = authHost.slice(atIdx + 1);
            shouldWarnInvalidHost = parseHostPort(u, hostPart, urlString) || shouldWarnInvalidHost;
        } else {
            shouldWarnInvalidHost = parseHostPort(u, authHost, urlString) || shouldWarnInvalidHost;
        }
    }

    if (rest) {
        u.pathname = rest;
    } else if (u.slashes && u.protocol && isSlashedProtocol(u.protocol) && u.host !== '') {
        u.pathname = '/';
    }

    if (u.pathname !== null || u.search !== null) {
        u.path = (u.pathname || '') + (u.search || '');
    } else {
        u.path = null;
    }
    u.href = formatLegacy(u);

    if (parseState && shouldWarnInvalidHost) {
        parseState.shouldWarnInvalidHost = true;
    }

    return u;
}

function parseHostPort(u, hostStr, input) {
    if (!hostStr) {
        u.host = '';
        u.hostname = '';
        return false;
    }

    let isIpv6Host = false;
    let shouldWarnInvalidHost = false;
    if (hostStr.startsWith('[')) {
        const bracketEnd = hostStr.indexOf(']');
        if (bracketEnd !== -1) {
            isIpv6Host = true;
            u.hostname = hostStr.slice(1, bracketEnd);
            const remaining = hostStr.slice(bracketEnd + 1);
            if (remaining.startsWith(':')) {
                u.port = remaining.slice(1) || null;
            }
        } else {
            u.hostname = hostStr;
        }
    } else {
        const colonIdx = hostStr.lastIndexOf(':');
        if (colonIdx !== -1) {
            const maybPort = hostStr.slice(colonIdx + 1);
            if (/^\d+$/.test(maybPort)) {
                u.hostname = hostStr.slice(0, colonIdx);
                u.port = maybPort;
            } else {
                u.hostname = hostStr;
                shouldWarnInvalidHost = true;
            }
        } else {
            u.hostname = hostStr;
        }
    }

    validateHostName(u.hostname, isIpv6Host, input);

    u.host = u.hostname;
    if (u.port) {
        u.host += ':' + u.port;
    }

    return shouldWarnInvalidHost;
}

export function parse(urlString, parseQueryString, slashesDenoteHost) {
    if (urlString instanceof Url) {
        return urlString;
    }

    if (typeof urlString !== 'string') {
        throw new ERR_INVALID_ARG_TYPE('url', 'string', urlString);
    }

    const parseState = { shouldWarnInvalidHost: false };
    const u = legacyParse(urlString, parseState);
    let shouldWarnInvalidHost = parseState.shouldWarnInvalidHost;

    if (slashesDenoteHost && !u.protocol) {
        let rest = urlString.trim();
        u.href = rest;

        const hashIdx = rest.indexOf('#');
        if (hashIdx !== -1) {
            u.hash = rest.slice(hashIdx);
            rest = rest.slice(0, hashIdx);
        }

        const qIdx = rest.indexOf('?');
        if (qIdx !== -1) {
            u.search = rest.slice(qIdx);
            u.query = rest.slice(qIdx + 1);
            rest = rest.slice(0, qIdx);
        }

        if (rest.startsWith('//')) {
            u.slashes = true;
            rest = rest.slice(2);
            const pathStart = rest.indexOf('/');
            if (pathStart === -1) {
                shouldWarnInvalidHost = parseHostPort(u, rest, urlString) || shouldWarnInvalidHost;
                rest = '';
            } else {
                shouldWarnInvalidHost = parseHostPort(u, rest.slice(0, pathStart), urlString) || shouldWarnInvalidHost;
                rest = rest.slice(pathStart);
            }
        }

        u.pathname = rest || null;
        if (u.pathname !== null || u.search !== null) {
            u.path = (u.pathname || '') + (u.search || '');
        } else {
            u.path = null;
        }
        u.href = formatLegacy(u);
    }

    if (shouldWarnInvalidHost) {
        emitInvalidUrlDeprecation(urlString);
    }

    if (parseQueryString) {
        const qs = u.query || '';
        const parsed = querystring.parse(qs);
        Object.setPrototypeOf(parsed, null);
        u.query = parsed;
    }

    return u;
}

export function resolve(from, to) {
    return parse(from, false, true).resolve(to);
}

export function resolveObject(source, relative) {
    if (!source) {
        return relative;
    }

    return parse(source, false, true).resolveObject(relative);
}

export function domainToASCII(domain) {
    return domain;
}

export function domainToUnicode(domain) {
    return domain;
}

export default {
    URL,
    URLSearchParams,
    fileURLToPath,
    pathToFileURL,
    urlToHttpOptions,
    format,
    parse,
    resolve,
    resolveObject,
    Url,
    domainToASCII,
    domainToUnicode,
};