1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | const path = require("path");
|
17 | const url_1 = require("url");
|
18 | const utils_1 = require("../core/utils");
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | class UrlResolver {
|
28 | getBaseAndUnresolved(url1, url2) {
|
29 | return url2 === undefined ?
|
30 | [undefined, this.brandAsPackageRelative(url1)] :
|
31 | [this.brandAsResolved(url1), this.brandAsFileRelative(url2)];
|
32 | }
|
33 | simpleUrlResolve(baseUrl, url, defaultProtocol) {
|
34 | return this.brandAsResolved(utils_1.resolveUrl(baseUrl, url, defaultProtocol));
|
35 | }
|
36 | simpleUrlRelative(from, to) {
|
37 | const fromUrl = utils_1.parseUrl(from);
|
38 | const toUrl = utils_1.parseUrl(to);
|
39 |
|
40 |
|
41 | if (typeof toUrl.protocol === 'string' &&
|
42 | fromUrl.protocol !== toUrl.protocol ||
|
43 | typeof toUrl.slashes === 'boolean' &&
|
44 | fromUrl.slashes !== toUrl.slashes ||
|
45 | typeof toUrl.host === 'string' && fromUrl.host !== toUrl.host ||
|
46 | typeof toUrl.auth === 'string' && fromUrl.auth !== toUrl.auth) {
|
47 | return this.brandAsFileRelative(to);
|
48 | }
|
49 | let pathname;
|
50 | const { search, hash } = toUrl;
|
51 | if (fromUrl.pathname === toUrl.pathname) {
|
52 | pathname = '';
|
53 | }
|
54 | else {
|
55 | const fromDir = typeof fromUrl.pathname === 'string' ?
|
56 | fromUrl.pathname.replace(/[^/]+$/, '') :
|
57 | '';
|
58 | const toDir = typeof toUrl.pathname === 'string' &&
|
59 | typeof toUrl.pathname === 'string' ?
|
60 | toUrl.pathname :
|
61 | '';
|
62 |
|
63 | const pathlib = path.posix || path;
|
64 |
|
65 |
|
66 | pathname = pathlib.relative(fromDir, toDir + '_').replace(/_$/, '');
|
67 | }
|
68 | return this.brandAsFileRelative(url_1.format({ pathname, search, hash }));
|
69 | }
|
70 | brandAsFileRelative(url) {
|
71 | return url;
|
72 | }
|
73 | brandAsPackageRelative(url) {
|
74 | return url;
|
75 | }
|
76 | brandAsResolved(url) {
|
77 | return url;
|
78 | }
|
79 | }
|
80 | exports.UrlResolver = UrlResolver;
|
81 |
|
\ | No newline at end of file |