UNPKG

2.15 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ensureAbsolute = exports.encodeURIComponent_RFC5987 = exports.encodeURIComponent_RFC3986 = exports.isHTTP = void 0;
4var path = require("path");
5var querystring = require("querystring");
6function isHTTP(urlOrPath) {
7 return /^https?:\/\//.test(urlOrPath);
8}
9exports.isHTTP = isHTTP;
10function encodeURIComponent_RFC3986(str) {
11 return encodeURIComponent(str)
12 .replace(/[!'()*]/g, function (c) {
13 return "%" + c.charCodeAt(0).toString(16);
14 });
15}
16exports.encodeURIComponent_RFC3986 = encodeURIComponent_RFC3986;
17function encodeURIComponent_RFC5987(str) {
18 return encodeURIComponent(str).
19 replace(/['()]/g, querystring.escape).
20 replace(/\*/g, "%2A").
21 replace(/%(?:7C|60|5E)/g, querystring.unescape);
22}
23exports.encodeURIComponent_RFC5987 = encodeURIComponent_RFC5987;
24function ensureAbsolute(rootUrl, linkHref) {
25 var url = linkHref;
26 if (!isHTTP(url) && url.indexOf("data:") !== 0) {
27 if (url.indexOf("//") === 0) {
28 if (rootUrl.indexOf("https://") === 0) {
29 url = "https:" + url;
30 }
31 else {
32 url = "http:" + url;
33 }
34 return url;
35 }
36 if (url[0] === "/") {
37 var j = rootUrl.replace(/:\/\//g, ":__").indexOf("/");
38 var rootUrlOrigin = rootUrl.substr(0, j);
39 url = path.join(rootUrlOrigin, url);
40 }
41 else {
42 var i = rootUrl.indexOf("?");
43 var rootUrlWithoutQuery = rootUrl;
44 if (i >= 0) {
45 rootUrlWithoutQuery = rootUrlWithoutQuery.substr(0, i);
46 }
47 if (rootUrlWithoutQuery.substr(-1) === "/") {
48 url = path.join(rootUrlWithoutQuery, url);
49 }
50 else {
51 url = path.join(path.dirname(rootUrlWithoutQuery), url);
52 }
53 }
54 url = url.replace(/\\/g, "/").replace(/^https:\//g, "https:\/\/").replace(/^http:\//g, "http:\/\/");
55 }
56 return url;
57}
58exports.ensureAbsolute = ensureAbsolute;
59//# sourceMappingURL=UrlUtils.js.map
\No newline at end of file