1 | "use strict";
|
2 |
|
3 |
|
4 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
5 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
6 | };
|
7 | Object.defineProperty(exports, "__esModule", { value: true });
|
8 | exports.URLExt = void 0;
|
9 | const path_1 = require("path");
|
10 | const url_parse_1 = __importDefault(require("url-parse"));
|
11 |
|
12 |
|
13 |
|
14 | var URLExt;
|
15 | (function (URLExt) {
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function parse(url) {
|
24 | if (typeof document !== 'undefined' && document) {
|
25 | const a = document.createElement('a');
|
26 | a.href = url;
|
27 | return a;
|
28 | }
|
29 | return (0, url_parse_1.default)(url);
|
30 | }
|
31 | URLExt.parse = parse;
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | function getHostName(url) {
|
40 | return (0, url_parse_1.default)(url).hostname;
|
41 | }
|
42 | URLExt.getHostName = getHostName;
|
43 | function normalize(url) {
|
44 | return url && parse(url).toString();
|
45 | }
|
46 | URLExt.normalize = normalize;
|
47 | |
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function join(...parts) {
|
55 | let u = (0, url_parse_1.default)(parts[0], {});
|
56 |
|
57 |
|
58 | const isSchemaLess = u.protocol === '' && u.slashes;
|
59 | if (isSchemaLess) {
|
60 | u = (0, url_parse_1.default)(parts[0], 'https:' + parts[0]);
|
61 | }
|
62 | const prefix = `${isSchemaLess ? '' : u.protocol}${u.slashes ? '//' : ''}${u.auth}${u.auth ? '@' : ''}${u.host}`;
|
63 |
|
64 | const path = path_1.posix.join(`${!!prefix && u.pathname[0] !== '/' ? '/' : ''}${u.pathname}`, ...parts.slice(1));
|
65 | return `${prefix}${path === '.' ? '' : path}`;
|
66 | }
|
67 | URLExt.join = join;
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function encodeParts(url) {
|
80 | return join(...url.split('/').map(encodeURIComponent));
|
81 | }
|
82 | URLExt.encodeParts = encodeParts;
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function objectToQueryString(value) {
|
94 | const keys = Object.keys(value).filter(key => key.length > 0);
|
95 | if (!keys.length) {
|
96 | return '';
|
97 | }
|
98 | return ('?' +
|
99 | keys
|
100 | .map(key => {
|
101 | const content = encodeURIComponent(String(value[key]));
|
102 | return key + (content ? '=' + content : '');
|
103 | })
|
104 | .join('&'));
|
105 | }
|
106 | URLExt.objectToQueryString = objectToQueryString;
|
107 | |
108 |
|
109 |
|
110 | function queryStringToObject(value) {
|
111 | return value
|
112 | .replace(/^\?/, '')
|
113 | .split('&')
|
114 | .reduce((acc, val) => {
|
115 | const [key, value] = val.split('=');
|
116 | if (key.length > 0) {
|
117 | acc[key] = decodeURIComponent(value || '');
|
118 | }
|
119 | return acc;
|
120 | }, {});
|
121 | }
|
122 | URLExt.queryStringToObject = queryStringToObject;
|
123 | |
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | function isLocal(url, allowRoot = false) {
|
133 | const { protocol } = parse(url);
|
134 | return ((!protocol || url.toLowerCase().indexOf(protocol) !== 0) &&
|
135 | (allowRoot ? url.indexOf('//') !== 0 : url.indexOf('/') !== 0));
|
136 | }
|
137 | URLExt.isLocal = isLocal;
|
138 | })(URLExt || (exports.URLExt = URLExt = {}));
|
139 |
|
\ | No newline at end of file |