UNPKG

4.71 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Jupyter Development Team.
3// Distributed under the terms of the Modified BSD License.
4var __importDefault = (this && this.__importDefault) || function (mod) {
5 return (mod && mod.__esModule) ? mod : { "default": mod };
6};
7Object.defineProperty(exports, "__esModule", { value: true });
8exports.URLExt = void 0;
9const path_1 = require("path");
10const url_parse_1 = __importDefault(require("url-parse"));
11/**
12 * The namespace for URL-related functions.
13 */
14var URLExt;
15(function (URLExt) {
16 /**
17 * Parse a url into a URL object.
18 *
19 * @param url - The URL string to parse.
20 *
21 * @returns A URL object.
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 * Parse URL and retrieve hostname
34 *
35 * @param url - The URL string to parse
36 *
37 * @returns a hostname string value
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 * Join a sequence of url components and normalizes as in node `path.join`.
49 *
50 * @param parts - The url components.
51 *
52 * @returns the joined url.
53 */
54 function join(...parts) {
55 let u = (0, url_parse_1.default)(parts[0], {});
56 // Schema-less URL can be only parsed as relative to a base URL
57 // see https://github.com/unshiftio/url-parse/issues/219#issuecomment-1002219326
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 // If there was a prefix, then the first path must start at the root.
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 * Encode the components of a multi-segment url.
70 *
71 * @param url - The url to encode.
72 *
73 * @returns the encoded url.
74 *
75 * #### Notes
76 * Preserves the `'/'` separators.
77 * Should not include the base url, since all parts are escaped.
78 */
79 function encodeParts(url) {
80 return join(...url.split('/').map(encodeURIComponent));
81 }
82 URLExt.encodeParts = encodeParts;
83 /**
84 * Return a serialized object string suitable for a query.
85 *
86 * @param value The source object.
87 *
88 * @returns an encoded url query.
89 *
90 * #### Notes
91 * Modified version of [stackoverflow](http://stackoverflow.com/a/30707423).
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 * Return a parsed object that represents the values in a query string.
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 * Test whether the url is a local url.
125 *
126 * @param allowRoot - Whether the paths starting at Unix-style filesystem root (`/`) are permitted.
127 *
128 * #### Notes
129 * This function returns `false` for any fully qualified url, including
130 * `data:`, `file:`, and `//` protocol URLs.
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//# sourceMappingURL=url.js.map
\No newline at end of file