react-native-consumet 1.0.0-rc.0 → 1.0.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/generated/java/com/consumet/NativeConsumetSpec.java +4 -0
- package/android/generated/jni/RNConsumetSpec-generated.cpp +6 -0
- package/android/generated/jni/react/renderer/components/RNConsumetSpec/RNConsumetSpecJSI-generated.cpp +8 -0
- package/android/generated/jni/react/renderer/components/RNConsumetSpec/RNConsumetSpecJSI.h +9 -0
- package/android/src/main/java/com/consumet/ConsumetModule.kt +7 -0
- package/android/src/main/java/com/consumet/DdosGuardModule.kt +248 -189
- package/ios/generated/RNConsumetSpec/RNConsumetSpec-generated.mm +7 -0
- package/ios/generated/RNConsumetSpec/RNConsumetSpec.h +2 -0
- package/ios/generated/RNConsumetSpecJSI-generated.cpp +8 -0
- package/ios/generated/RNConsumetSpecJSI.h +9 -0
- package/lib/commonjs/NativeConsumet.ts +28 -26
- package/lib/commonjs/extension-registry.json +13 -13
- package/lib/commonjs/extractors/filemoon.js +2 -2
- package/lib/commonjs/extractors/kwik.js +60 -15
- package/lib/commonjs/extractors/kwik.js.map +1 -1
- package/lib/commonjs/extractors/megacloud.js +6 -6
- package/lib/commonjs/index.js +2 -11
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/models/types.js +4 -4
- package/lib/commonjs/models/video-extractor.js +4 -4
- package/lib/commonjs/providers/anime/animedrive.js +12 -12
- package/lib/commonjs/providers/anime/animekai.js +41 -41
- package/lib/commonjs/providers/anime/animepahe/animepahe.js +16 -15
- package/lib/commonjs/providers/anime/animepahe/animepahe.js.map +1 -1
- package/lib/commonjs/providers/anime/animepahe/create-animepahe.js +12 -9
- package/lib/commonjs/providers/anime/animepahe/create-animepahe.js.map +1 -1
- package/lib/commonjs/providers/anime/animesuge/animesuge.js +78 -0
- package/lib/commonjs/providers/anime/animesuge/animesuge.js.map +1 -0
- package/lib/commonjs/providers/anime/animesuge/create-animesuge.js +312 -0
- package/lib/commonjs/providers/anime/animesuge/create-animesuge.js.map +1 -0
- package/lib/commonjs/providers/anime/animeunity.js +13 -13
- package/lib/commonjs/providers/anime/index.js +4 -4
- package/lib/commonjs/providers/anime/index.js.map +1 -1
- package/lib/commonjs/providers/anime/marin.js +12 -12
- package/lib/commonjs/providers/anime/zoro/create-zoro.js +2 -2
- package/lib/commonjs/providers/anime/zoro/create-zoro.js.map +1 -1
- package/lib/commonjs/providers/anime/zoro/zoro.js +76 -74
- package/lib/commonjs/providers/anime/zoro/zoro.js.map +1 -1
- package/lib/commonjs/providers/manga/brmangas.js +3 -3
- package/lib/commonjs/providers/meta/anilist.js +91 -91
- package/lib/commonjs/providers/meta/mal.js +12 -12
- package/lib/commonjs/providers/meta/tmdb.js +21 -21
- package/lib/commonjs/providers/movies/himovies/create-himovies.js +16 -16
- package/lib/commonjs/providers/movies/himovies/himovies.js +16 -16
- package/lib/commonjs/providers/movies/multimovies.js +15 -15
- package/lib/commonjs/providers/movies/multistream/index.js +21 -20
- package/lib/commonjs/providers/movies/multistream/index.js.map +1 -1
- package/lib/commonjs/providers/movies/multistream/utils.js +142 -130
- package/lib/commonjs/providers/movies/multistream/utils.js.map +1 -1
- package/lib/commonjs/utils/ExtractorManager.js +15 -15
- package/lib/commonjs/utils/ExtractorManager.js.map +1 -1
- package/lib/commonjs/utils/ProviderManager.js +2 -1
- package/lib/commonjs/utils/ProviderManager.js.map +1 -1
- package/lib/commonjs/utils/create-extractor-context.js +9 -1
- package/lib/commonjs/utils/create-extractor-context.js.map +1 -1
- package/lib/commonjs/utils/create-provider-context.js +6 -2
- package/lib/commonjs/utils/create-provider-context.js.map +1 -1
- package/lib/commonjs/utils/extension-utils.js +44 -54
- package/lib/commonjs/utils/extension-utils.js.map +1 -1
- package/lib/commonjs/utils/index.js +2 -2
- package/lib/commonjs/utils/provider-maps.js +31 -0
- package/lib/commonjs/utils/provider-maps.js.map +1 -0
- package/lib/commonjs/utils/providers-list.js +4 -4
- package/lib/commonjs/utils/url-polyfill.js +4 -4
- package/lib/module/NativeConsumet.ts +28 -26
- package/lib/module/extension-registry.json +13 -13
- package/lib/module/extractors/filemoon.js +2 -2
- package/lib/module/extractors/kwik.js +60 -15
- package/lib/module/extractors/kwik.js.map +1 -1
- package/lib/module/extractors/megacloud.js +6 -6
- package/lib/module/index.js +3 -7
- package/lib/module/index.js.map +1 -1
- package/lib/module/models/types.js +4 -4
- package/lib/module/models/video-extractor.js +4 -4
- package/lib/module/providers/anime/animedrive.js +12 -12
- package/lib/module/providers/anime/animekai.js +41 -41
- package/lib/module/providers/anime/animepahe/animepahe.js +14 -14
- package/lib/module/providers/anime/animepahe/animepahe.js.map +1 -1
- package/lib/module/providers/anime/animepahe/create-animepahe.js +13 -9
- package/lib/module/providers/anime/animepahe/create-animepahe.js.map +1 -1
- package/lib/module/providers/anime/animesuge/animesuge.js +73 -0
- package/lib/module/providers/anime/animesuge/animesuge.js.map +1 -0
- package/lib/module/providers/anime/animesuge/create-animesuge.js +309 -0
- package/lib/module/providers/anime/animesuge/create-animesuge.js.map +1 -0
- package/lib/module/providers/anime/animeunity.js +13 -13
- package/lib/module/providers/anime/index.js +2 -2
- package/lib/module/providers/anime/index.js.map +1 -1
- package/lib/module/providers/anime/marin.js +12 -12
- package/lib/module/providers/anime/zoro/create-zoro.js +3 -2
- package/lib/module/providers/anime/zoro/create-zoro.js.map +1 -1
- package/lib/module/providers/anime/zoro/zoro.js +74 -73
- package/lib/module/providers/anime/zoro/zoro.js.map +1 -1
- package/lib/module/providers/manga/brmangas.js +3 -3
- package/lib/module/providers/meta/anilist.js +91 -91
- package/lib/module/providers/meta/mal.js +12 -12
- package/lib/module/providers/meta/tmdb.js +21 -21
- package/lib/module/providers/movies/himovies/create-himovies.js +16 -16
- package/lib/module/providers/movies/himovies/himovies.js +16 -16
- package/lib/module/providers/movies/multimovies.js +15 -15
- package/lib/module/providers/movies/multistream/index.js +21 -20
- package/lib/module/providers/movies/multistream/index.js.map +1 -1
- package/lib/module/providers/movies/multistream/utils.js +141 -129
- package/lib/module/providers/movies/multistream/utils.js.map +1 -1
- package/lib/module/utils/ExtractorManager.js +16 -16
- package/lib/module/utils/ExtractorManager.js.map +1 -1
- package/lib/module/utils/ProviderManager.js +2 -1
- package/lib/module/utils/ProviderManager.js.map +1 -1
- package/lib/module/utils/create-extractor-context.js +10 -1
- package/lib/module/utils/create-extractor-context.js.map +1 -1
- package/lib/module/utils/create-provider-context.js +8 -4
- package/lib/module/utils/create-provider-context.js.map +1 -1
- package/lib/module/utils/extension-utils.js +23 -33
- package/lib/module/utils/extension-utils.js.map +1 -1
- package/lib/module/utils/index.js +1 -1
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/utils/provider-maps.js +27 -0
- package/lib/module/utils/provider-maps.js.map +1 -0
- package/lib/module/utils/providers-list.js +4 -4
- package/lib/module/utils/url-polyfill.js +4 -4
- package/lib/typescript/commonjs/src/NativeConsumet.d.ts +2 -0
- package/lib/typescript/commonjs/src/NativeConsumet.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/extractors/kwik.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/index.d.ts +6 -6
- package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/models/extractor-context.d.ts +8 -0
- package/lib/typescript/commonjs/src/models/extractor-context.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/models/index.d.ts +2 -2
- package/lib/typescript/commonjs/src/models/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/models/provider-context.d.ts +6 -2
- package/lib/typescript/commonjs/src/models/provider-context.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/providers/anime/animepahe/animepahe.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/providers/anime/animepahe/create-animepahe.d.ts +1 -1
- package/lib/typescript/commonjs/src/providers/anime/animepahe/create-animepahe.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/providers/anime/animesuge/animesuge.d.ts +34 -0
- package/lib/typescript/commonjs/src/providers/anime/animesuge/animesuge.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/providers/anime/animesuge/create-animesuge.d.ts +14 -0
- package/lib/typescript/commonjs/src/providers/anime/animesuge/create-animesuge.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/providers/anime/index.d.ts +2 -2
- package/lib/typescript/commonjs/src/providers/anime/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/providers/anime/zoro/create-zoro.d.ts +1 -1
- package/lib/typescript/commonjs/src/providers/anime/zoro/create-zoro.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/providers/anime/zoro/zoro.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/providers/movies/multistream/index.d.ts +1 -1
- package/lib/typescript/commonjs/src/providers/movies/multistream/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/providers/movies/multistream/utils.d.ts +2 -2
- package/lib/typescript/commonjs/src/providers/movies/multistream/utils.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/utils/ProviderManager.d.ts +1 -1
- package/lib/typescript/commonjs/src/utils/ProviderManager.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/utils/create-extractor-context.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/utils/create-provider-context.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/utils/extension-utils.d.ts +1 -20
- package/lib/typescript/commonjs/src/utils/extension-utils.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/utils/index.d.ts +2 -1
- package/lib/typescript/commonjs/src/utils/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/src/utils/provider-maps.d.ts +27 -0
- package/lib/typescript/commonjs/src/utils/provider-maps.d.ts.map +1 -0
- package/lib/typescript/module/src/NativeConsumet.d.ts +2 -0
- package/lib/typescript/module/src/NativeConsumet.d.ts.map +1 -1
- package/lib/typescript/module/src/extractors/kwik.d.ts.map +1 -1
- package/lib/typescript/module/src/index.d.ts +6 -6
- package/lib/typescript/module/src/index.d.ts.map +1 -1
- package/lib/typescript/module/src/models/extractor-context.d.ts +8 -0
- package/lib/typescript/module/src/models/extractor-context.d.ts.map +1 -1
- package/lib/typescript/module/src/models/index.d.ts +2 -2
- package/lib/typescript/module/src/models/index.d.ts.map +1 -1
- package/lib/typescript/module/src/models/provider-context.d.ts +6 -2
- package/lib/typescript/module/src/models/provider-context.d.ts.map +1 -1
- package/lib/typescript/module/src/providers/anime/animepahe/animepahe.d.ts.map +1 -1
- package/lib/typescript/module/src/providers/anime/animepahe/create-animepahe.d.ts +1 -1
- package/lib/typescript/module/src/providers/anime/animepahe/create-animepahe.d.ts.map +1 -1
- package/lib/typescript/module/src/providers/anime/animesuge/animesuge.d.ts +34 -0
- package/lib/typescript/module/src/providers/anime/animesuge/animesuge.d.ts.map +1 -0
- package/lib/typescript/module/src/providers/anime/animesuge/create-animesuge.d.ts +14 -0
- package/lib/typescript/module/src/providers/anime/animesuge/create-animesuge.d.ts.map +1 -0
- package/lib/typescript/module/src/providers/anime/index.d.ts +2 -2
- package/lib/typescript/module/src/providers/anime/index.d.ts.map +1 -1
- package/lib/typescript/module/src/providers/anime/zoro/create-zoro.d.ts +1 -1
- package/lib/typescript/module/src/providers/anime/zoro/create-zoro.d.ts.map +1 -1
- package/lib/typescript/module/src/providers/anime/zoro/zoro.d.ts.map +1 -1
- package/lib/typescript/module/src/providers/movies/multistream/index.d.ts +1 -1
- package/lib/typescript/module/src/providers/movies/multistream/index.d.ts.map +1 -1
- package/lib/typescript/module/src/providers/movies/multistream/utils.d.ts +2 -2
- package/lib/typescript/module/src/providers/movies/multistream/utils.d.ts.map +1 -1
- package/lib/typescript/module/src/utils/ProviderManager.d.ts +1 -1
- package/lib/typescript/module/src/utils/ProviderManager.d.ts.map +1 -1
- package/lib/typescript/module/src/utils/create-extractor-context.d.ts.map +1 -1
- package/lib/typescript/module/src/utils/create-provider-context.d.ts.map +1 -1
- package/lib/typescript/module/src/utils/extension-utils.d.ts +1 -20
- package/lib/typescript/module/src/utils/extension-utils.d.ts.map +1 -1
- package/lib/typescript/module/src/utils/index.d.ts +2 -1
- package/lib/typescript/module/src/utils/index.d.ts.map +1 -1
- package/lib/typescript/module/src/utils/provider-maps.d.ts +27 -0
- package/lib/typescript/module/src/utils/provider-maps.d.ts.map +1 -0
- package/package.json +6 -2
- package/src/NativeConsumet.ts +2 -0
- package/src/extension-registry.json +81 -81
- package/src/extractors/StreamP2P.ts +60 -60
- package/src/extractors/filemoon.ts +58 -58
- package/src/extractors/index.ts +41 -41
- package/src/extractors/kwik.ts +113 -50
- package/src/extractors/megacloud.ts +163 -163
- package/src/extractors/mixdrop.ts +31 -31
- package/src/extractors/mp4upload.ts +31 -31
- package/src/extractors/streamhub.ts +58 -58
- package/src/extractors/streamwish.ts +120 -120
- package/src/extractors/vidhide.ts +59 -59
- package/src/index.tsx +176 -175
- package/src/models/extension-manifest.ts +77 -77
- package/src/models/extractor-context.ts +55 -41
- package/src/models/index.ts +107 -106
- package/src/models/provider-context.ts +192 -182
- package/src/models/types.ts +531 -531
- package/src/models/video-extractor.ts +48 -48
- package/src/providers/anime/animedrive.ts +245 -245
- package/src/providers/anime/animekai.ts +627 -627
- package/src/providers/anime/animepahe/animepahe.ts +76 -76
- package/src/providers/anime/animepahe/create-animepahe.ts +350 -348
- package/src/providers/anime/animesuge/animesuge.ts +77 -0
- package/src/providers/anime/animesuge/create-animesuge.ts +327 -0
- package/src/providers/anime/animeunity.ts +216 -216
- package/src/providers/anime/index.ts +21 -21
- package/src/providers/anime/marin.ts +319 -319
- package/src/providers/anime/zoro/create-zoro.ts +656 -655
- package/src/providers/anime/zoro/zoro.ts +198 -197
- package/src/providers/index.ts +7 -7
- package/src/providers/manga/brmangas.ts +144 -144
- package/src/providers/meta/anilist.ts +2108 -2108
- package/src/providers/meta/mal.ts +635 -635
- package/src/providers/meta/tmdb.ts +470 -470
- package/src/providers/movies/himovies/create-himovies.ts +529 -529
- package/src/providers/movies/himovies/himovies.ts +90 -90
- package/src/providers/movies/index.ts +15 -15
- package/src/providers/movies/multimovies.ts +495 -495
- package/src/providers/movies/multistream/index.ts +232 -231
- package/src/providers/movies/multistream/utils.ts +147 -270
- package/src/utils/ExtractorManager.ts +311 -311
- package/src/utils/ProviderManager.ts +423 -424
- package/src/utils/create-extractor-context.ts +36 -22
- package/src/utils/create-provider-context.ts +117 -107
- package/src/utils/extension-utils.ts +105 -114
- package/src/utils/index.ts +96 -95
- package/src/utils/provider-maps.ts +31 -0
- package/src/utils/providers-list.ts +27 -27
- package/src/utils/url-polyfill.ts +642 -642
- package/src/utils/utils.ts +374 -374
|
@@ -1,642 +1,642 @@
|
|
|
1
|
-
type Nullable<T> = T | null;
|
|
2
|
-
|
|
3
|
-
const isASCII = (ch: string) => ch.charCodeAt(0) < 128;
|
|
4
|
-
|
|
5
|
-
function utf8Encode(str: string): Uint8Array {
|
|
6
|
-
// encode JS string to UTF-8 bytes
|
|
7
|
-
const encoder = typeof TextEncoder !== 'undefined' ? new TextEncoder() : null;
|
|
8
|
-
if (encoder) return encoder.encode(str);
|
|
9
|
-
// fallback
|
|
10
|
-
const bytes: number[] = [];
|
|
11
|
-
for (let i = 0; i < str.length; i++) {
|
|
12
|
-
let code = str.charCodeAt(i);
|
|
13
|
-
if (code < 0x80) bytes.push(code);
|
|
14
|
-
else if (code < 0x800) {
|
|
15
|
-
bytes.push(0xc0 | (code >> 6));
|
|
16
|
-
bytes.push(0x80 | (code & 0x3f));
|
|
17
|
-
} else if (code < 0xd800 || code >= 0xe000) {
|
|
18
|
-
bytes.push(0xe0 | (code >> 12));
|
|
19
|
-
bytes.push(0x80 | ((code >> 6) & 0x3f));
|
|
20
|
-
bytes.push(0x80 | (code & 0x3f));
|
|
21
|
-
} else {
|
|
22
|
-
// surrogate pair
|
|
23
|
-
i++;
|
|
24
|
-
const code2 = str.charCodeAt(i);
|
|
25
|
-
const codePoint = 0x10000 + (((code & 0x3ff) << 10) | (code2 & 0x3ff));
|
|
26
|
-
bytes.push(0xf0 | (codePoint >> 18));
|
|
27
|
-
bytes.push(0x80 | ((codePoint >> 12) & 0x3f));
|
|
28
|
-
bytes.push(0x80 | ((codePoint >> 6) & 0x3f));
|
|
29
|
-
bytes.push(0x80 | (codePoint & 0x3f));
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return new Uint8Array(bytes);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function percentEncodeByte(byte: number) {
|
|
36
|
-
return '%' + byte.toString(16).toUpperCase().padStart(2, '0');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function encodeComponent(str: string, allow = /[A-Za-z0-9\-._~]/) {
|
|
40
|
-
// encode like encodeURIComponent but allow customizing safe characters (RegExp)
|
|
41
|
-
let out = '';
|
|
42
|
-
const bytes = utf8Encode(str);
|
|
43
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
44
|
-
const b = bytes[i]!;
|
|
45
|
-
const ch = String.fromCharCode(b);
|
|
46
|
-
if (allow.test(ch)) out += ch;
|
|
47
|
-
else out += percentEncodeByte(b);
|
|
48
|
-
}
|
|
49
|
-
return out;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function decodePercentEncoded(str: string) {
|
|
53
|
-
// decode percent-encoded UTF-8 sequences
|
|
54
|
-
try {
|
|
55
|
-
// Replace + with %2B so decodeURIComponent doesn't convert + to space (we treat + literally)
|
|
56
|
-
// but in query strings sometimes + is used for space; we keep literal + behavior.
|
|
57
|
-
return decodeURIComponent(str);
|
|
58
|
-
} catch (e) {
|
|
59
|
-
// Be defensive: fallback to manual decode of %XX bytes into utf8 string
|
|
60
|
-
const bytes: number[] = [];
|
|
61
|
-
for (let i = 0; i < str.length; ) {
|
|
62
|
-
const ch = str[i];
|
|
63
|
-
if (ch === '%' && i + 2 < str.length) {
|
|
64
|
-
const hex = str.substr(i + 1, 2);
|
|
65
|
-
const byte = parseInt(hex, 16);
|
|
66
|
-
if (!Number.isNaN(byte)) {
|
|
67
|
-
bytes.push(byte);
|
|
68
|
-
i += 3;
|
|
69
|
-
continue;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
bytes.push(str.charCodeAt(i));
|
|
73
|
-
i++;
|
|
74
|
-
}
|
|
75
|
-
// decode UTF-8 bytes
|
|
76
|
-
let res = '';
|
|
77
|
-
for (let i = 0; i < bytes.length; ) {
|
|
78
|
-
const b = bytes[i]!;
|
|
79
|
-
if (b < 0x80) {
|
|
80
|
-
res += String.fromCharCode(b);
|
|
81
|
-
i++;
|
|
82
|
-
} else if (b >= 0xc0 && b < 0xe0) {
|
|
83
|
-
const b2 = bytes[i + 1] ?? 0x80;
|
|
84
|
-
const code = ((b & 0x1f) << 6) | (b2 & 0x3f);
|
|
85
|
-
res += String.fromCharCode(code);
|
|
86
|
-
i += 2;
|
|
87
|
-
} else if (b >= 0xe0 && b < 0xf0) {
|
|
88
|
-
const b2 = bytes[i + 1] ?? 0x80;
|
|
89
|
-
const b3 = bytes[i + 2] ?? 0x80;
|
|
90
|
-
const code = ((b & 0x0f) << 12) | ((b2 & 0x3f) << 6) | (b3 & 0x3f);
|
|
91
|
-
res += String.fromCharCode(code);
|
|
92
|
-
i += 3;
|
|
93
|
-
} else if (b >= 0xf0) {
|
|
94
|
-
const b2 = bytes[i + 1] ?? 0x80,
|
|
95
|
-
b3 = bytes[i + 2] ?? 0x80,
|
|
96
|
-
b4 = bytes[i + 3] ?? 0x80;
|
|
97
|
-
const codePoint = ((b & 7) << 18) | ((b2 & 0x3f) << 12) | ((b3 & 0x3f) << 6) | (b4 & 0x3f);
|
|
98
|
-
const cp = codePoint - 0x10000;
|
|
99
|
-
res += String.fromCharCode(0xd800 + (cp >> 10), 0xdc00 + (cp & 0x3ff));
|
|
100
|
-
i += 4;
|
|
101
|
-
} else {
|
|
102
|
-
i++;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return res;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function isWindowsDrivePath(input: string) {
|
|
110
|
-
return /^[a-zA-Z]:[\\/]/.test(input);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// Path normalization utility
|
|
114
|
-
function normalizePath(path: string): string {
|
|
115
|
-
if (!path || path === '/') return '/';
|
|
116
|
-
|
|
117
|
-
const segments = path.split('/');
|
|
118
|
-
const normalized: string[] = [];
|
|
119
|
-
|
|
120
|
-
for (const segment of segments) {
|
|
121
|
-
if (segment === '' || segment === '.') {
|
|
122
|
-
continue; // skip empty and current directory
|
|
123
|
-
} else if (segment === '..') {
|
|
124
|
-
if (normalized.length > 0 && normalized[normalized.length - 1] !== '..') {
|
|
125
|
-
normalized.pop(); // go up one directory
|
|
126
|
-
} else if (!path.startsWith('/')) {
|
|
127
|
-
normalized.push('..'); // relative path, keep ..
|
|
128
|
-
}
|
|
129
|
-
} else {
|
|
130
|
-
normalized.push(segment);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
let result = normalized.join('/');
|
|
135
|
-
if (path.startsWith('/')) result = '/' + result;
|
|
136
|
-
if (path.endsWith('/') && result !== '/') result += '/';
|
|
137
|
-
|
|
138
|
-
return result || '/';
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Default ports for common protocols
|
|
142
|
-
const DEFAULT_PORTS: Record<string, string> = {
|
|
143
|
-
'http:': '80',
|
|
144
|
-
'https:': '443',
|
|
145
|
-
'ftp:': '21',
|
|
146
|
-
'ftps:': '990',
|
|
147
|
-
'ssh:': '22',
|
|
148
|
-
'ws:': '80',
|
|
149
|
-
'wss:': '443',
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
/* -----------------------------
|
|
153
|
-
URLSearchParams Implementation
|
|
154
|
-
----------------------------- */
|
|
155
|
-
|
|
156
|
-
export class URLSearchParams implements Iterable<[string, string]> {
|
|
157
|
-
private _map: Map<string, string[]>;
|
|
158
|
-
|
|
159
|
-
constructor(init?: string | Record<string, string> | Array<[string, string]> | URLSearchParams) {
|
|
160
|
-
this._map = new Map();
|
|
161
|
-
if (!init) return;
|
|
162
|
-
if (typeof init === 'string') {
|
|
163
|
-
const s = init.startsWith('?') ? init.slice(1) : init;
|
|
164
|
-
if (s.length === 0) return;
|
|
165
|
-
s.split('&').forEach((pair) => {
|
|
166
|
-
if (pair.length === 0) return;
|
|
167
|
-
const [k, ...rest] = pair.split('=');
|
|
168
|
-
const key = decodePercentEncoded(k!.replace(/\+/g, '%20'))!;
|
|
169
|
-
const val = decodePercentEncoded((rest.join('=') || '').replace(/\+/g, '%20'));
|
|
170
|
-
this.append(key, val);
|
|
171
|
-
});
|
|
172
|
-
} else if (Array.isArray(init)) {
|
|
173
|
-
init.forEach(([k, v]) => this.append(String(k), String(v)));
|
|
174
|
-
} else if (init instanceof URLSearchParams) {
|
|
175
|
-
init.forEach((val, key) => this.append(key, val));
|
|
176
|
-
} else if (typeof init === 'object') {
|
|
177
|
-
Object.keys(init).forEach((k) => this.set(k, init[k]!));
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
append(name: string, value: string) {
|
|
182
|
-
const key = String(name);
|
|
183
|
-
const val = String(value);
|
|
184
|
-
const arr = this._map.get(key) || [];
|
|
185
|
-
arr.push(val);
|
|
186
|
-
this._map.set(key, arr);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
set(name: string, value: string) {
|
|
190
|
-
this._map.set(String(name), [String(value)]);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
get(name: string): Nullable<string> {
|
|
194
|
-
const arr = this._map.get(String(name));
|
|
195
|
-
return arr ? arr[0]! : null;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
getAll(name: string): string[] {
|
|
199
|
-
return (this._map.get(String(name)) || []).slice();
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
has(name: string): boolean {
|
|
203
|
-
return this._map.has(String(name));
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
delete(name: string) {
|
|
207
|
-
this._map.delete(String(name));
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
forEach(cb: (value: string, name: string, self: URLSearchParams) => void, thisArg?: any) {
|
|
211
|
-
for (const [k, vals] of this._map) {
|
|
212
|
-
for (const v of vals) cb.call(thisArg, v, k, this);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
toString(): string {
|
|
217
|
-
const out: string[] = [];
|
|
218
|
-
for (const [k, vals] of this._map) {
|
|
219
|
-
for (const v of vals) {
|
|
220
|
-
out.push(`${encodeComponent(k)}=${encodeComponent(v)}`);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
return out.join('&');
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
sort() {
|
|
227
|
-
// Stable sort by key (and preserve values order per key)
|
|
228
|
-
const entries = Array.from(this._map.entries()).sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
|
|
229
|
-
this._map = new Map(entries);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
toJSON() {
|
|
233
|
-
return this.toString();
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
clone(): URLSearchParams {
|
|
237
|
-
return new URLSearchParams(this);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// Additional utility methods
|
|
241
|
-
get size(): number {
|
|
242
|
-
let count = 0;
|
|
243
|
-
for (const [, vals] of this._map) {
|
|
244
|
-
count += vals.length;
|
|
245
|
-
}
|
|
246
|
-
return count;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
clear(): void {
|
|
250
|
-
this._map.clear();
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
*entries(): IterableIterator<[string, string]> {
|
|
254
|
-
for (const [k, vals] of this._map) for (const v of vals) yield [k, v];
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
*keys(): IterableIterator<string> {
|
|
258
|
-
for (const [k] of this._map) yield k;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
*values(): IterableIterator<string> {
|
|
262
|
-
for (const [, vals] of this._map) for (const v of vals) yield v;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
[Symbol.iterator]() {
|
|
266
|
-
return this.entries();
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/* -----------------------------
|
|
271
|
-
URL Implementation
|
|
272
|
-
----------------------------- */
|
|
273
|
-
|
|
274
|
-
interface ParsedURL {
|
|
275
|
-
protocol: string;
|
|
276
|
-
username: string;
|
|
277
|
-
password: string;
|
|
278
|
-
host: string; // host:port
|
|
279
|
-
hostname: string;
|
|
280
|
-
port: string;
|
|
281
|
-
pathname: string;
|
|
282
|
-
search: string;
|
|
283
|
-
hash: string;
|
|
284
|
-
origin: string;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
const SCHEME_RE = /^([A-Za-z][A-Za-z0-9+.-]*):/;
|
|
288
|
-
|
|
289
|
-
function splitBeforeFirst(haystack: string, needle: string) {
|
|
290
|
-
const idx = haystack.indexOf(needle);
|
|
291
|
-
if (idx === -1) return [haystack, ''];
|
|
292
|
-
return [haystack.slice(0, idx), haystack.slice(idx + needle.length)];
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
function parseURLString(input: string, base?: ParsedURL): ParsedURL {
|
|
296
|
-
let url = input.trim();
|
|
297
|
-
|
|
298
|
-
// Handle data URLs and other special schemes
|
|
299
|
-
if (url.match(/^(data|mailto|tel|sms):/i)) {
|
|
300
|
-
const [scheme, rest] = splitBeforeFirst(url, ':');
|
|
301
|
-
return {
|
|
302
|
-
protocol: scheme!.toLowerCase() + ':',
|
|
303
|
-
username: '',
|
|
304
|
-
password: '',
|
|
305
|
-
host: '',
|
|
306
|
-
hostname: '',
|
|
307
|
-
port: '',
|
|
308
|
-
pathname: rest || '',
|
|
309
|
-
search: '',
|
|
310
|
-
hash: '',
|
|
311
|
-
origin: 'null',
|
|
312
|
-
};
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// If base is provided and url is relative, resolve
|
|
316
|
-
if (base) {
|
|
317
|
-
// handle scheme-relative: //host/path
|
|
318
|
-
if (url.startsWith('//')) {
|
|
319
|
-
url = base.protocol + url;
|
|
320
|
-
} else if (!SCHEME_RE.test(url)) {
|
|
321
|
-
// relative path
|
|
322
|
-
// join base.pathname directory with url
|
|
323
|
-
const basePath = base.pathname;
|
|
324
|
-
let dir = basePath.slice(0, basePath.lastIndexOf('/') + 1);
|
|
325
|
-
if (!dir.endsWith('/')) dir += '/';
|
|
326
|
-
url = `${base.protocol}//${base.host}${dir}${url}`;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
const protocolMatch = url.match(SCHEME_RE);
|
|
331
|
-
const protocol = protocolMatch ? protocolMatch[1]!.toLowerCase() + ':' : '';
|
|
332
|
-
|
|
333
|
-
let rest = protocol ? url.slice(protocol.length) : url;
|
|
334
|
-
|
|
335
|
-
// If protocol present, remove single leading slashes
|
|
336
|
-
if (protocol) {
|
|
337
|
-
if (rest.startsWith('///')) {
|
|
338
|
-
// weird but handle
|
|
339
|
-
rest = '//' + rest.slice(3);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
// Extract hash
|
|
344
|
-
let [withoutHash, hash] = splitBeforeFirst(rest, '#');
|
|
345
|
-
if (hash) hash = '#' + hash;
|
|
346
|
-
|
|
347
|
-
// Extract search
|
|
348
|
-
let [beforeSearch, search] = splitBeforeFirst(withoutHash!, '?');
|
|
349
|
-
if (search) search = '?' + search;
|
|
350
|
-
|
|
351
|
-
// authority and path
|
|
352
|
-
let authority = '';
|
|
353
|
-
let pathname = '';
|
|
354
|
-
if (beforeSearch!.startsWith('//')) {
|
|
355
|
-
const afterSlashes = beforeSearch!.slice(2);
|
|
356
|
-
const idx = afterSlashes.indexOf('/');
|
|
357
|
-
if (idx === -1) {
|
|
358
|
-
authority = afterSlashes;
|
|
359
|
-
pathname = '/';
|
|
360
|
-
} else {
|
|
361
|
-
authority = afterSlashes.slice(0, idx);
|
|
362
|
-
pathname = afterSlashes.slice(idx) || '/';
|
|
363
|
-
}
|
|
364
|
-
} else {
|
|
365
|
-
pathname = beforeSearch || '';
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
// parse username/password and host/port
|
|
369
|
-
let username = '';
|
|
370
|
-
let password = '';
|
|
371
|
-
let host = '';
|
|
372
|
-
let hostname = '';
|
|
373
|
-
let port = '';
|
|
374
|
-
|
|
375
|
-
if (authority) {
|
|
376
|
-
// userinfo@host:port
|
|
377
|
-
const atIdx = authority.lastIndexOf('@');
|
|
378
|
-
let hostport = authority;
|
|
379
|
-
if (atIdx !== -1) {
|
|
380
|
-
const userinfo = authority.slice(0, atIdx);
|
|
381
|
-
hostport = authority.slice(atIdx + 1);
|
|
382
|
-
const [u, p] = splitBeforeFirst(userinfo, ':');
|
|
383
|
-
username = decodePercentEncoded(u!);
|
|
384
|
-
password = p ? decodePercentEncoded(p) : '';
|
|
385
|
-
}
|
|
386
|
-
// IPv6 in [..]:port
|
|
387
|
-
if (hostport.startsWith('[')) {
|
|
388
|
-
const closeIdx = hostport.indexOf(']');
|
|
389
|
-
if (closeIdx !== -1) {
|
|
390
|
-
hostname = hostport.slice(0, closeIdx + 1);
|
|
391
|
-
port = hostport.slice(closeIdx + 1);
|
|
392
|
-
if (port.startsWith(':')) port = port.slice(1);
|
|
393
|
-
} else {
|
|
394
|
-
hostname = hostport; // malformed, but take as hostname
|
|
395
|
-
}
|
|
396
|
-
} else {
|
|
397
|
-
const colonIdx = hostport.lastIndexOf(':');
|
|
398
|
-
if (colonIdx > hostport.indexOf(']')) {
|
|
399
|
-
// normal
|
|
400
|
-
}
|
|
401
|
-
if (colonIdx !== -1 && hostport.indexOf(':') === colonIdx) {
|
|
402
|
-
hostname = hostport.slice(0, colonIdx);
|
|
403
|
-
port = hostport.slice(colonIdx + 1);
|
|
404
|
-
} else {
|
|
405
|
-
hostname = hostport;
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
hostname = hostname || '';
|
|
409
|
-
host = port ? `${hostname}:${port}` : hostname;
|
|
410
|
-
host = host.trim();
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
// Normalization
|
|
414
|
-
if (!pathname) pathname = '/';
|
|
415
|
-
pathname = normalizePath(decodePercentEncoded(pathname));
|
|
416
|
-
|
|
417
|
-
// If hostname present but protocol empty (like relative), try to use base
|
|
418
|
-
let origin = '';
|
|
419
|
-
if (protocol && host) {
|
|
420
|
-
origin = protocol + '//' + host;
|
|
421
|
-
} else if (base && !protocol) {
|
|
422
|
-
origin = base.origin;
|
|
423
|
-
} else if (protocol && !host) {
|
|
424
|
-
origin = protocol + '//';
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
return {
|
|
428
|
-
protocol: protocol,
|
|
429
|
-
username,
|
|
430
|
-
password,
|
|
431
|
-
host,
|
|
432
|
-
hostname,
|
|
433
|
-
port,
|
|
434
|
-
pathname,
|
|
435
|
-
search: search || '',
|
|
436
|
-
hash: hash || '',
|
|
437
|
-
origin,
|
|
438
|
-
};
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
export class URL {
|
|
442
|
-
private _p: ParsedURL;
|
|
443
|
-
public searchParams: URLSearchParams;
|
|
444
|
-
|
|
445
|
-
constructor(input: string, base?: string | URL) {
|
|
446
|
-
let baseParsed: ParsedURL | undefined;
|
|
447
|
-
if (base instanceof URL) baseParsed = base._p;
|
|
448
|
-
else if (typeof base === 'string' && base.length > 0) baseParsed = parseURLString(base);
|
|
449
|
-
|
|
450
|
-
this._p = parseURLString(String(input), baseParsed);
|
|
451
|
-
this.searchParams = new URLSearchParams(this._p.search || '');
|
|
452
|
-
|
|
453
|
-
// Bind searchParams changes to update the URL's search property
|
|
454
|
-
this._bindSearchParamsUpdates();
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
private _bindSearchParamsUpdates() {
|
|
458
|
-
const origToString = this.searchParams.toString.bind(this.searchParams);
|
|
459
|
-
const self = this;
|
|
460
|
-
|
|
461
|
-
// Override methods that change params to update search string
|
|
462
|
-
const mutators = ['append', 'set', 'delete', 'sort', 'clear'];
|
|
463
|
-
for (const m of mutators) {
|
|
464
|
-
const fn = (this.searchParams as any)[m];
|
|
465
|
-
if (typeof fn === 'function') {
|
|
466
|
-
(this.searchParams as any)[m] = function (...args: any[]) {
|
|
467
|
-
const res = fn.apply(this, args);
|
|
468
|
-
const newSearch = origToString();
|
|
469
|
-
self._p.search = newSearch ? '?' + newSearch : '';
|
|
470
|
-
return res;
|
|
471
|
-
};
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
// Static helper methods
|
|
477
|
-
static canParse(url: string, base?: string): boolean {
|
|
478
|
-
try {
|
|
479
|
-
new URL(url, base);
|
|
480
|
-
return true;
|
|
481
|
-
} catch {
|
|
482
|
-
return false;
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
static parse(url: string, base?: string): URL | null {
|
|
487
|
-
try {
|
|
488
|
-
return new URL(url, base);
|
|
489
|
-
} catch {
|
|
490
|
-
return null;
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
get href() {
|
|
495
|
-
return this.toString();
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
set href(val: string) {
|
|
499
|
-
const parsed = parseURLString(String(val));
|
|
500
|
-
this._p = parsed;
|
|
501
|
-
this.searchParams = new URLSearchParams(this._p.search || '');
|
|
502
|
-
this._bindSearchParamsUpdates();
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
get protocol() {
|
|
506
|
-
return this._p.protocol;
|
|
507
|
-
}
|
|
508
|
-
set protocol(val: string) {
|
|
509
|
-
const m = String(val).replace(/:$/, '');
|
|
510
|
-
this._p.protocol = m + ':';
|
|
511
|
-
this._updateOrigin();
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
get username() {
|
|
515
|
-
return this._p.username;
|
|
516
|
-
}
|
|
517
|
-
set username(v: string) {
|
|
518
|
-
this._p.username = String(v);
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
get password() {
|
|
522
|
-
return this._p.password;
|
|
523
|
-
}
|
|
524
|
-
set password(v: string) {
|
|
525
|
-
this._p.password = String(v);
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
get host() {
|
|
529
|
-
return this._p.host;
|
|
530
|
-
}
|
|
531
|
-
set host(v: string) {
|
|
532
|
-
const [hostname, port] = splitBeforeFirst(String(v), ':');
|
|
533
|
-
this._p.hostname = hostname!;
|
|
534
|
-
this._p.port = port!;
|
|
535
|
-
this._p.host = port ? `${hostname}:${port}` : hostname!;
|
|
536
|
-
this._updateOrigin();
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
get hostname() {
|
|
540
|
-
return this._p.hostname;
|
|
541
|
-
}
|
|
542
|
-
set hostname(v: string) {
|
|
543
|
-
this._p.hostname = String(v);
|
|
544
|
-
this._p.host = this._p.port ? `${this._p.hostname}:${this._p.port}` : this._p.hostname;
|
|
545
|
-
this._updateOrigin();
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
get port() {
|
|
549
|
-
return this._p.port;
|
|
550
|
-
}
|
|
551
|
-
set port(v: string) {
|
|
552
|
-
const portStr = String(v);
|
|
553
|
-
// Remove default ports to match browser behavior
|
|
554
|
-
if (DEFAULT_PORTS[this._p.protocol] === portStr) {
|
|
555
|
-
this._p.port = '';
|
|
556
|
-
} else {
|
|
557
|
-
this._p.port = portStr;
|
|
558
|
-
}
|
|
559
|
-
this._p.host = this._p.port ? `${this._p.hostname}:${this._p.port}` : this._p.hostname;
|
|
560
|
-
this._updateOrigin();
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
get pathname() {
|
|
564
|
-
return this._p.pathname;
|
|
565
|
-
}
|
|
566
|
-
set pathname(v: string) {
|
|
567
|
-
let path = String(v);
|
|
568
|
-
if (!path.startsWith('/')) path = '/' + path;
|
|
569
|
-
this._p.pathname = normalizePath(path);
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
get search() {
|
|
573
|
-
return this._p.search;
|
|
574
|
-
}
|
|
575
|
-
set search(v: string) {
|
|
576
|
-
let search = String(v);
|
|
577
|
-
if (search && !search.startsWith('?')) search = '?' + search;
|
|
578
|
-
this._p.search = search;
|
|
579
|
-
this.searchParams = new URLSearchParams(search || '');
|
|
580
|
-
this._bindSearchParamsUpdates();
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
get hash() {
|
|
584
|
-
return this._p.hash;
|
|
585
|
-
}
|
|
586
|
-
set hash(v: string) {
|
|
587
|
-
let hash = String(v);
|
|
588
|
-
if (hash && !hash.startsWith('#')) hash = '#' + hash;
|
|
589
|
-
this._p.hash = hash;
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
get origin() {
|
|
593
|
-
return this._p.origin;
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
private _updateOrigin() {
|
|
597
|
-
if (this._p.protocol && this._p.hostname) {
|
|
598
|
-
this._p.origin = this._p.protocol + '//' + this._p.host;
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
toString() {
|
|
603
|
-
const p = this._p;
|
|
604
|
-
let auth = '';
|
|
605
|
-
if (p.username || p.password) {
|
|
606
|
-
auth = encodeComponent(p.username) + (p.password ? ':' + encodeComponent(p.password) : '') + '@';
|
|
607
|
-
}
|
|
608
|
-
const hostPart = p.host || '';
|
|
609
|
-
const path = p.pathname ? encodeComponentPath(p.pathname) : '/';
|
|
610
|
-
const search =
|
|
611
|
-
p.search || (this.searchParams ? (this.searchParams.toString() ? '?' + this.searchParams.toString() : '') : '');
|
|
612
|
-
const hash = p.hash || '';
|
|
613
|
-
const proto = p.protocol || '';
|
|
614
|
-
|
|
615
|
-
// Handle special schemes
|
|
616
|
-
if (proto.match(/^(data|mailto|tel|sms):/i)) {
|
|
617
|
-
return proto + path + search + hash;
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
// If protocol uses scheme with no host (like mailto), handle simply
|
|
621
|
-
if (!hostPart && proto && (path === '/' || path === '')) {
|
|
622
|
-
return proto + path + search + hash;
|
|
623
|
-
}
|
|
624
|
-
return proto + '//' + auth + hostPart + path + search + hash;
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
toJSON() {
|
|
628
|
-
return this.toString();
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
/* Helper: escape path preserving slashes */
|
|
633
|
-
function encodeComponentPath(path: string) {
|
|
634
|
-
// Split on / and encode segments, preserving empty segments for proper // handling
|
|
635
|
-
return path
|
|
636
|
-
.split('/')
|
|
637
|
-
.map((seg) => encodeComponent(seg))
|
|
638
|
-
.join('/');
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
// Export for Node.js environments that might not have these globals
|
|
642
|
-
export { URL as PolyURL, URLSearchParams as PolyURLSearchParams };
|
|
1
|
+
type Nullable<T> = T | null;
|
|
2
|
+
|
|
3
|
+
const isASCII = (ch: string) => ch.charCodeAt(0) < 128;
|
|
4
|
+
|
|
5
|
+
function utf8Encode(str: string): Uint8Array {
|
|
6
|
+
// encode JS string to UTF-8 bytes
|
|
7
|
+
const encoder = typeof TextEncoder !== 'undefined' ? new TextEncoder() : null;
|
|
8
|
+
if (encoder) return encoder.encode(str);
|
|
9
|
+
// fallback
|
|
10
|
+
const bytes: number[] = [];
|
|
11
|
+
for (let i = 0; i < str.length; i++) {
|
|
12
|
+
let code = str.charCodeAt(i);
|
|
13
|
+
if (code < 0x80) bytes.push(code);
|
|
14
|
+
else if (code < 0x800) {
|
|
15
|
+
bytes.push(0xc0 | (code >> 6));
|
|
16
|
+
bytes.push(0x80 | (code & 0x3f));
|
|
17
|
+
} else if (code < 0xd800 || code >= 0xe000) {
|
|
18
|
+
bytes.push(0xe0 | (code >> 12));
|
|
19
|
+
bytes.push(0x80 | ((code >> 6) & 0x3f));
|
|
20
|
+
bytes.push(0x80 | (code & 0x3f));
|
|
21
|
+
} else {
|
|
22
|
+
// surrogate pair
|
|
23
|
+
i++;
|
|
24
|
+
const code2 = str.charCodeAt(i);
|
|
25
|
+
const codePoint = 0x10000 + (((code & 0x3ff) << 10) | (code2 & 0x3ff));
|
|
26
|
+
bytes.push(0xf0 | (codePoint >> 18));
|
|
27
|
+
bytes.push(0x80 | ((codePoint >> 12) & 0x3f));
|
|
28
|
+
bytes.push(0x80 | ((codePoint >> 6) & 0x3f));
|
|
29
|
+
bytes.push(0x80 | (codePoint & 0x3f));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return new Uint8Array(bytes);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function percentEncodeByte(byte: number) {
|
|
36
|
+
return '%' + byte.toString(16).toUpperCase().padStart(2, '0');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function encodeComponent(str: string, allow = /[A-Za-z0-9\-._~]/) {
|
|
40
|
+
// encode like encodeURIComponent but allow customizing safe characters (RegExp)
|
|
41
|
+
let out = '';
|
|
42
|
+
const bytes = utf8Encode(str);
|
|
43
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
44
|
+
const b = bytes[i]!;
|
|
45
|
+
const ch = String.fromCharCode(b);
|
|
46
|
+
if (allow.test(ch)) out += ch;
|
|
47
|
+
else out += percentEncodeByte(b);
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function decodePercentEncoded(str: string) {
|
|
53
|
+
// decode percent-encoded UTF-8 sequences
|
|
54
|
+
try {
|
|
55
|
+
// Replace + with %2B so decodeURIComponent doesn't convert + to space (we treat + literally)
|
|
56
|
+
// but in query strings sometimes + is used for space; we keep literal + behavior.
|
|
57
|
+
return decodeURIComponent(str);
|
|
58
|
+
} catch (e) {
|
|
59
|
+
// Be defensive: fallback to manual decode of %XX bytes into utf8 string
|
|
60
|
+
const bytes: number[] = [];
|
|
61
|
+
for (let i = 0; i < str.length; ) {
|
|
62
|
+
const ch = str[i];
|
|
63
|
+
if (ch === '%' && i + 2 < str.length) {
|
|
64
|
+
const hex = str.substr(i + 1, 2);
|
|
65
|
+
const byte = parseInt(hex, 16);
|
|
66
|
+
if (!Number.isNaN(byte)) {
|
|
67
|
+
bytes.push(byte);
|
|
68
|
+
i += 3;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
bytes.push(str.charCodeAt(i));
|
|
73
|
+
i++;
|
|
74
|
+
}
|
|
75
|
+
// decode UTF-8 bytes
|
|
76
|
+
let res = '';
|
|
77
|
+
for (let i = 0; i < bytes.length; ) {
|
|
78
|
+
const b = bytes[i]!;
|
|
79
|
+
if (b < 0x80) {
|
|
80
|
+
res += String.fromCharCode(b);
|
|
81
|
+
i++;
|
|
82
|
+
} else if (b >= 0xc0 && b < 0xe0) {
|
|
83
|
+
const b2 = bytes[i + 1] ?? 0x80;
|
|
84
|
+
const code = ((b & 0x1f) << 6) | (b2 & 0x3f);
|
|
85
|
+
res += String.fromCharCode(code);
|
|
86
|
+
i += 2;
|
|
87
|
+
} else if (b >= 0xe0 && b < 0xf0) {
|
|
88
|
+
const b2 = bytes[i + 1] ?? 0x80;
|
|
89
|
+
const b3 = bytes[i + 2] ?? 0x80;
|
|
90
|
+
const code = ((b & 0x0f) << 12) | ((b2 & 0x3f) << 6) | (b3 & 0x3f);
|
|
91
|
+
res += String.fromCharCode(code);
|
|
92
|
+
i += 3;
|
|
93
|
+
} else if (b >= 0xf0) {
|
|
94
|
+
const b2 = bytes[i + 1] ?? 0x80,
|
|
95
|
+
b3 = bytes[i + 2] ?? 0x80,
|
|
96
|
+
b4 = bytes[i + 3] ?? 0x80;
|
|
97
|
+
const codePoint = ((b & 7) << 18) | ((b2 & 0x3f) << 12) | ((b3 & 0x3f) << 6) | (b4 & 0x3f);
|
|
98
|
+
const cp = codePoint - 0x10000;
|
|
99
|
+
res += String.fromCharCode(0xd800 + (cp >> 10), 0xdc00 + (cp & 0x3ff));
|
|
100
|
+
i += 4;
|
|
101
|
+
} else {
|
|
102
|
+
i++;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return res;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function isWindowsDrivePath(input: string) {
|
|
110
|
+
return /^[a-zA-Z]:[\\/]/.test(input);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Path normalization utility
|
|
114
|
+
function normalizePath(path: string): string {
|
|
115
|
+
if (!path || path === '/') return '/';
|
|
116
|
+
|
|
117
|
+
const segments = path.split('/');
|
|
118
|
+
const normalized: string[] = [];
|
|
119
|
+
|
|
120
|
+
for (const segment of segments) {
|
|
121
|
+
if (segment === '' || segment === '.') {
|
|
122
|
+
continue; // skip empty and current directory
|
|
123
|
+
} else if (segment === '..') {
|
|
124
|
+
if (normalized.length > 0 && normalized[normalized.length - 1] !== '..') {
|
|
125
|
+
normalized.pop(); // go up one directory
|
|
126
|
+
} else if (!path.startsWith('/')) {
|
|
127
|
+
normalized.push('..'); // relative path, keep ..
|
|
128
|
+
}
|
|
129
|
+
} else {
|
|
130
|
+
normalized.push(segment);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let result = normalized.join('/');
|
|
135
|
+
if (path.startsWith('/')) result = '/' + result;
|
|
136
|
+
if (path.endsWith('/') && result !== '/') result += '/';
|
|
137
|
+
|
|
138
|
+
return result || '/';
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Default ports for common protocols
|
|
142
|
+
const DEFAULT_PORTS: Record<string, string> = {
|
|
143
|
+
'http:': '80',
|
|
144
|
+
'https:': '443',
|
|
145
|
+
'ftp:': '21',
|
|
146
|
+
'ftps:': '990',
|
|
147
|
+
'ssh:': '22',
|
|
148
|
+
'ws:': '80',
|
|
149
|
+
'wss:': '443',
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/* -----------------------------
|
|
153
|
+
URLSearchParams Implementation
|
|
154
|
+
----------------------------- */
|
|
155
|
+
|
|
156
|
+
export class URLSearchParams implements Iterable<[string, string]> {
|
|
157
|
+
private _map: Map<string, string[]>;
|
|
158
|
+
|
|
159
|
+
constructor(init?: string | Record<string, string> | Array<[string, string]> | URLSearchParams) {
|
|
160
|
+
this._map = new Map();
|
|
161
|
+
if (!init) return;
|
|
162
|
+
if (typeof init === 'string') {
|
|
163
|
+
const s = init.startsWith('?') ? init.slice(1) : init;
|
|
164
|
+
if (s.length === 0) return;
|
|
165
|
+
s.split('&').forEach((pair) => {
|
|
166
|
+
if (pair.length === 0) return;
|
|
167
|
+
const [k, ...rest] = pair.split('=');
|
|
168
|
+
const key = decodePercentEncoded(k!.replace(/\+/g, '%20'))!;
|
|
169
|
+
const val = decodePercentEncoded((rest.join('=') || '').replace(/\+/g, '%20'));
|
|
170
|
+
this.append(key, val);
|
|
171
|
+
});
|
|
172
|
+
} else if (Array.isArray(init)) {
|
|
173
|
+
init.forEach(([k, v]) => this.append(String(k), String(v)));
|
|
174
|
+
} else if (init instanceof URLSearchParams) {
|
|
175
|
+
init.forEach((val, key) => this.append(key, val));
|
|
176
|
+
} else if (typeof init === 'object') {
|
|
177
|
+
Object.keys(init).forEach((k) => this.set(k, init[k]!));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
append(name: string, value: string) {
|
|
182
|
+
const key = String(name);
|
|
183
|
+
const val = String(value);
|
|
184
|
+
const arr = this._map.get(key) || [];
|
|
185
|
+
arr.push(val);
|
|
186
|
+
this._map.set(key, arr);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
set(name: string, value: string) {
|
|
190
|
+
this._map.set(String(name), [String(value)]);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
get(name: string): Nullable<string> {
|
|
194
|
+
const arr = this._map.get(String(name));
|
|
195
|
+
return arr ? arr[0]! : null;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
getAll(name: string): string[] {
|
|
199
|
+
return (this._map.get(String(name)) || []).slice();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
has(name: string): boolean {
|
|
203
|
+
return this._map.has(String(name));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
delete(name: string) {
|
|
207
|
+
this._map.delete(String(name));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
forEach(cb: (value: string, name: string, self: URLSearchParams) => void, thisArg?: any) {
|
|
211
|
+
for (const [k, vals] of this._map) {
|
|
212
|
+
for (const v of vals) cb.call(thisArg, v, k, this);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
toString(): string {
|
|
217
|
+
const out: string[] = [];
|
|
218
|
+
for (const [k, vals] of this._map) {
|
|
219
|
+
for (const v of vals) {
|
|
220
|
+
out.push(`${encodeComponent(k)}=${encodeComponent(v)}`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return out.join('&');
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
sort() {
|
|
227
|
+
// Stable sort by key (and preserve values order per key)
|
|
228
|
+
const entries = Array.from(this._map.entries()).sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
|
|
229
|
+
this._map = new Map(entries);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
toJSON() {
|
|
233
|
+
return this.toString();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
clone(): URLSearchParams {
|
|
237
|
+
return new URLSearchParams(this);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Additional utility methods
|
|
241
|
+
get size(): number {
|
|
242
|
+
let count = 0;
|
|
243
|
+
for (const [, vals] of this._map) {
|
|
244
|
+
count += vals.length;
|
|
245
|
+
}
|
|
246
|
+
return count;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
clear(): void {
|
|
250
|
+
this._map.clear();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
*entries(): IterableIterator<[string, string]> {
|
|
254
|
+
for (const [k, vals] of this._map) for (const v of vals) yield [k, v];
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
*keys(): IterableIterator<string> {
|
|
258
|
+
for (const [k] of this._map) yield k;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
*values(): IterableIterator<string> {
|
|
262
|
+
for (const [, vals] of this._map) for (const v of vals) yield v;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
[Symbol.iterator]() {
|
|
266
|
+
return this.entries();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/* -----------------------------
|
|
271
|
+
URL Implementation
|
|
272
|
+
----------------------------- */
|
|
273
|
+
|
|
274
|
+
interface ParsedURL {
|
|
275
|
+
protocol: string;
|
|
276
|
+
username: string;
|
|
277
|
+
password: string;
|
|
278
|
+
host: string; // host:port
|
|
279
|
+
hostname: string;
|
|
280
|
+
port: string;
|
|
281
|
+
pathname: string;
|
|
282
|
+
search: string;
|
|
283
|
+
hash: string;
|
|
284
|
+
origin: string;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const SCHEME_RE = /^([A-Za-z][A-Za-z0-9+.-]*):/;
|
|
288
|
+
|
|
289
|
+
function splitBeforeFirst(haystack: string, needle: string) {
|
|
290
|
+
const idx = haystack.indexOf(needle);
|
|
291
|
+
if (idx === -1) return [haystack, ''];
|
|
292
|
+
return [haystack.slice(0, idx), haystack.slice(idx + needle.length)];
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function parseURLString(input: string, base?: ParsedURL): ParsedURL {
|
|
296
|
+
let url = input.trim();
|
|
297
|
+
|
|
298
|
+
// Handle data URLs and other special schemes
|
|
299
|
+
if (url.match(/^(data|mailto|tel|sms):/i)) {
|
|
300
|
+
const [scheme, rest] = splitBeforeFirst(url, ':');
|
|
301
|
+
return {
|
|
302
|
+
protocol: scheme!.toLowerCase() + ':',
|
|
303
|
+
username: '',
|
|
304
|
+
password: '',
|
|
305
|
+
host: '',
|
|
306
|
+
hostname: '',
|
|
307
|
+
port: '',
|
|
308
|
+
pathname: rest || '',
|
|
309
|
+
search: '',
|
|
310
|
+
hash: '',
|
|
311
|
+
origin: 'null',
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// If base is provided and url is relative, resolve
|
|
316
|
+
if (base) {
|
|
317
|
+
// handle scheme-relative: //host/path
|
|
318
|
+
if (url.startsWith('//')) {
|
|
319
|
+
url = base.protocol + url;
|
|
320
|
+
} else if (!SCHEME_RE.test(url)) {
|
|
321
|
+
// relative path
|
|
322
|
+
// join base.pathname directory with url
|
|
323
|
+
const basePath = base.pathname;
|
|
324
|
+
let dir = basePath.slice(0, basePath.lastIndexOf('/') + 1);
|
|
325
|
+
if (!dir.endsWith('/')) dir += '/';
|
|
326
|
+
url = `${base.protocol}//${base.host}${dir}${url}`;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const protocolMatch = url.match(SCHEME_RE);
|
|
331
|
+
const protocol = protocolMatch ? protocolMatch[1]!.toLowerCase() + ':' : '';
|
|
332
|
+
|
|
333
|
+
let rest = protocol ? url.slice(protocol.length) : url;
|
|
334
|
+
|
|
335
|
+
// If protocol present, remove single leading slashes
|
|
336
|
+
if (protocol) {
|
|
337
|
+
if (rest.startsWith('///')) {
|
|
338
|
+
// weird but handle
|
|
339
|
+
rest = '//' + rest.slice(3);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Extract hash
|
|
344
|
+
let [withoutHash, hash] = splitBeforeFirst(rest, '#');
|
|
345
|
+
if (hash) hash = '#' + hash;
|
|
346
|
+
|
|
347
|
+
// Extract search
|
|
348
|
+
let [beforeSearch, search] = splitBeforeFirst(withoutHash!, '?');
|
|
349
|
+
if (search) search = '?' + search;
|
|
350
|
+
|
|
351
|
+
// authority and path
|
|
352
|
+
let authority = '';
|
|
353
|
+
let pathname = '';
|
|
354
|
+
if (beforeSearch!.startsWith('//')) {
|
|
355
|
+
const afterSlashes = beforeSearch!.slice(2);
|
|
356
|
+
const idx = afterSlashes.indexOf('/');
|
|
357
|
+
if (idx === -1) {
|
|
358
|
+
authority = afterSlashes;
|
|
359
|
+
pathname = '/';
|
|
360
|
+
} else {
|
|
361
|
+
authority = afterSlashes.slice(0, idx);
|
|
362
|
+
pathname = afterSlashes.slice(idx) || '/';
|
|
363
|
+
}
|
|
364
|
+
} else {
|
|
365
|
+
pathname = beforeSearch || '';
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// parse username/password and host/port
|
|
369
|
+
let username = '';
|
|
370
|
+
let password = '';
|
|
371
|
+
let host = '';
|
|
372
|
+
let hostname = '';
|
|
373
|
+
let port = '';
|
|
374
|
+
|
|
375
|
+
if (authority) {
|
|
376
|
+
// userinfo@host:port
|
|
377
|
+
const atIdx = authority.lastIndexOf('@');
|
|
378
|
+
let hostport = authority;
|
|
379
|
+
if (atIdx !== -1) {
|
|
380
|
+
const userinfo = authority.slice(0, atIdx);
|
|
381
|
+
hostport = authority.slice(atIdx + 1);
|
|
382
|
+
const [u, p] = splitBeforeFirst(userinfo, ':');
|
|
383
|
+
username = decodePercentEncoded(u!);
|
|
384
|
+
password = p ? decodePercentEncoded(p) : '';
|
|
385
|
+
}
|
|
386
|
+
// IPv6 in [..]:port
|
|
387
|
+
if (hostport.startsWith('[')) {
|
|
388
|
+
const closeIdx = hostport.indexOf(']');
|
|
389
|
+
if (closeIdx !== -1) {
|
|
390
|
+
hostname = hostport.slice(0, closeIdx + 1);
|
|
391
|
+
port = hostport.slice(closeIdx + 1);
|
|
392
|
+
if (port.startsWith(':')) port = port.slice(1);
|
|
393
|
+
} else {
|
|
394
|
+
hostname = hostport; // malformed, but take as hostname
|
|
395
|
+
}
|
|
396
|
+
} else {
|
|
397
|
+
const colonIdx = hostport.lastIndexOf(':');
|
|
398
|
+
if (colonIdx > hostport.indexOf(']')) {
|
|
399
|
+
// normal
|
|
400
|
+
}
|
|
401
|
+
if (colonIdx !== -1 && hostport.indexOf(':') === colonIdx) {
|
|
402
|
+
hostname = hostport.slice(0, colonIdx);
|
|
403
|
+
port = hostport.slice(colonIdx + 1);
|
|
404
|
+
} else {
|
|
405
|
+
hostname = hostport;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
hostname = hostname || '';
|
|
409
|
+
host = port ? `${hostname}:${port}` : hostname;
|
|
410
|
+
host = host.trim();
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// Normalization
|
|
414
|
+
if (!pathname) pathname = '/';
|
|
415
|
+
pathname = normalizePath(decodePercentEncoded(pathname));
|
|
416
|
+
|
|
417
|
+
// If hostname present but protocol empty (like relative), try to use base
|
|
418
|
+
let origin = '';
|
|
419
|
+
if (protocol && host) {
|
|
420
|
+
origin = protocol + '//' + host;
|
|
421
|
+
} else if (base && !protocol) {
|
|
422
|
+
origin = base.origin;
|
|
423
|
+
} else if (protocol && !host) {
|
|
424
|
+
origin = protocol + '//';
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
return {
|
|
428
|
+
protocol: protocol,
|
|
429
|
+
username,
|
|
430
|
+
password,
|
|
431
|
+
host,
|
|
432
|
+
hostname,
|
|
433
|
+
port,
|
|
434
|
+
pathname,
|
|
435
|
+
search: search || '',
|
|
436
|
+
hash: hash || '',
|
|
437
|
+
origin,
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
export class URL {
|
|
442
|
+
private _p: ParsedURL;
|
|
443
|
+
public searchParams: URLSearchParams;
|
|
444
|
+
|
|
445
|
+
constructor(input: string, base?: string | URL) {
|
|
446
|
+
let baseParsed: ParsedURL | undefined;
|
|
447
|
+
if (base instanceof URL) baseParsed = base._p;
|
|
448
|
+
else if (typeof base === 'string' && base.length > 0) baseParsed = parseURLString(base);
|
|
449
|
+
|
|
450
|
+
this._p = parseURLString(String(input), baseParsed);
|
|
451
|
+
this.searchParams = new URLSearchParams(this._p.search || '');
|
|
452
|
+
|
|
453
|
+
// Bind searchParams changes to update the URL's search property
|
|
454
|
+
this._bindSearchParamsUpdates();
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
private _bindSearchParamsUpdates() {
|
|
458
|
+
const origToString = this.searchParams.toString.bind(this.searchParams);
|
|
459
|
+
const self = this;
|
|
460
|
+
|
|
461
|
+
// Override methods that change params to update search string
|
|
462
|
+
const mutators = ['append', 'set', 'delete', 'sort', 'clear'];
|
|
463
|
+
for (const m of mutators) {
|
|
464
|
+
const fn = (this.searchParams as any)[m];
|
|
465
|
+
if (typeof fn === 'function') {
|
|
466
|
+
(this.searchParams as any)[m] = function (...args: any[]) {
|
|
467
|
+
const res = fn.apply(this, args);
|
|
468
|
+
const newSearch = origToString();
|
|
469
|
+
self._p.search = newSearch ? '?' + newSearch : '';
|
|
470
|
+
return res;
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// Static helper methods
|
|
477
|
+
static canParse(url: string, base?: string): boolean {
|
|
478
|
+
try {
|
|
479
|
+
new URL(url, base);
|
|
480
|
+
return true;
|
|
481
|
+
} catch {
|
|
482
|
+
return false;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
static parse(url: string, base?: string): URL | null {
|
|
487
|
+
try {
|
|
488
|
+
return new URL(url, base);
|
|
489
|
+
} catch {
|
|
490
|
+
return null;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
get href() {
|
|
495
|
+
return this.toString();
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
set href(val: string) {
|
|
499
|
+
const parsed = parseURLString(String(val));
|
|
500
|
+
this._p = parsed;
|
|
501
|
+
this.searchParams = new URLSearchParams(this._p.search || '');
|
|
502
|
+
this._bindSearchParamsUpdates();
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
get protocol() {
|
|
506
|
+
return this._p.protocol;
|
|
507
|
+
}
|
|
508
|
+
set protocol(val: string) {
|
|
509
|
+
const m = String(val).replace(/:$/, '');
|
|
510
|
+
this._p.protocol = m + ':';
|
|
511
|
+
this._updateOrigin();
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
get username() {
|
|
515
|
+
return this._p.username;
|
|
516
|
+
}
|
|
517
|
+
set username(v: string) {
|
|
518
|
+
this._p.username = String(v);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
get password() {
|
|
522
|
+
return this._p.password;
|
|
523
|
+
}
|
|
524
|
+
set password(v: string) {
|
|
525
|
+
this._p.password = String(v);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
get host() {
|
|
529
|
+
return this._p.host;
|
|
530
|
+
}
|
|
531
|
+
set host(v: string) {
|
|
532
|
+
const [hostname, port] = splitBeforeFirst(String(v), ':');
|
|
533
|
+
this._p.hostname = hostname!;
|
|
534
|
+
this._p.port = port!;
|
|
535
|
+
this._p.host = port ? `${hostname}:${port}` : hostname!;
|
|
536
|
+
this._updateOrigin();
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
get hostname() {
|
|
540
|
+
return this._p.hostname;
|
|
541
|
+
}
|
|
542
|
+
set hostname(v: string) {
|
|
543
|
+
this._p.hostname = String(v);
|
|
544
|
+
this._p.host = this._p.port ? `${this._p.hostname}:${this._p.port}` : this._p.hostname;
|
|
545
|
+
this._updateOrigin();
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
get port() {
|
|
549
|
+
return this._p.port;
|
|
550
|
+
}
|
|
551
|
+
set port(v: string) {
|
|
552
|
+
const portStr = String(v);
|
|
553
|
+
// Remove default ports to match browser behavior
|
|
554
|
+
if (DEFAULT_PORTS[this._p.protocol] === portStr) {
|
|
555
|
+
this._p.port = '';
|
|
556
|
+
} else {
|
|
557
|
+
this._p.port = portStr;
|
|
558
|
+
}
|
|
559
|
+
this._p.host = this._p.port ? `${this._p.hostname}:${this._p.port}` : this._p.hostname;
|
|
560
|
+
this._updateOrigin();
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
get pathname() {
|
|
564
|
+
return this._p.pathname;
|
|
565
|
+
}
|
|
566
|
+
set pathname(v: string) {
|
|
567
|
+
let path = String(v);
|
|
568
|
+
if (!path.startsWith('/')) path = '/' + path;
|
|
569
|
+
this._p.pathname = normalizePath(path);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
get search() {
|
|
573
|
+
return this._p.search;
|
|
574
|
+
}
|
|
575
|
+
set search(v: string) {
|
|
576
|
+
let search = String(v);
|
|
577
|
+
if (search && !search.startsWith('?')) search = '?' + search;
|
|
578
|
+
this._p.search = search;
|
|
579
|
+
this.searchParams = new URLSearchParams(search || '');
|
|
580
|
+
this._bindSearchParamsUpdates();
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
get hash() {
|
|
584
|
+
return this._p.hash;
|
|
585
|
+
}
|
|
586
|
+
set hash(v: string) {
|
|
587
|
+
let hash = String(v);
|
|
588
|
+
if (hash && !hash.startsWith('#')) hash = '#' + hash;
|
|
589
|
+
this._p.hash = hash;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
get origin() {
|
|
593
|
+
return this._p.origin;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
private _updateOrigin() {
|
|
597
|
+
if (this._p.protocol && this._p.hostname) {
|
|
598
|
+
this._p.origin = this._p.protocol + '//' + this._p.host;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
toString() {
|
|
603
|
+
const p = this._p;
|
|
604
|
+
let auth = '';
|
|
605
|
+
if (p.username || p.password) {
|
|
606
|
+
auth = encodeComponent(p.username) + (p.password ? ':' + encodeComponent(p.password) : '') + '@';
|
|
607
|
+
}
|
|
608
|
+
const hostPart = p.host || '';
|
|
609
|
+
const path = p.pathname ? encodeComponentPath(p.pathname) : '/';
|
|
610
|
+
const search =
|
|
611
|
+
p.search || (this.searchParams ? (this.searchParams.toString() ? '?' + this.searchParams.toString() : '') : '');
|
|
612
|
+
const hash = p.hash || '';
|
|
613
|
+
const proto = p.protocol || '';
|
|
614
|
+
|
|
615
|
+
// Handle special schemes
|
|
616
|
+
if (proto.match(/^(data|mailto|tel|sms):/i)) {
|
|
617
|
+
return proto + path + search + hash;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
// If protocol uses scheme with no host (like mailto), handle simply
|
|
621
|
+
if (!hostPart && proto && (path === '/' || path === '')) {
|
|
622
|
+
return proto + path + search + hash;
|
|
623
|
+
}
|
|
624
|
+
return proto + '//' + auth + hostPart + path + search + hash;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
toJSON() {
|
|
628
|
+
return this.toString();
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/* Helper: escape path preserving slashes */
|
|
633
|
+
function encodeComponentPath(path: string) {
|
|
634
|
+
// Split on / and encode segments, preserving empty segments for proper // handling
|
|
635
|
+
return path
|
|
636
|
+
.split('/')
|
|
637
|
+
.map((seg) => encodeComponent(seg))
|
|
638
|
+
.join('/');
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// Export for Node.js environments that might not have these globals
|
|
642
|
+
export { URL as PolyURL, URLSearchParams as PolyURLSearchParams };
|