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 pathlib = require("path");
|
17 | const path_1 = require("path");
|
18 | const url_1 = require("url");
|
19 | const vscode_uri_1 = require("vscode-uri");
|
20 | const utils_1 = require("../core/utils");
|
21 | const url_resolver_1 = require("./url-resolver");
|
22 | const isWindows = process.platform === 'win32';
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | class FsUrlResolver extends url_resolver_1.UrlResolver {
|
33 | constructor(packageDir,
|
34 | // If provided, any URL which matches `host` will attempt to resolve
|
35 | // to a `file` protocol URL regardless of the protocol represented in the
|
36 | // URL to-be-resolved.
|
37 | host,
|
38 | // When attempting to resolve a protocol-relative URL (that is a URL which
|
39 | // begins `//`), the default protocol to resolve to if the resolver can
|
40 | // not produce a `file` URL.
|
41 | protocol = 'https') {
|
42 | super();
|
43 | this.host = host;
|
44 | this.protocol = protocol;
|
45 | this.packageDir =
|
46 | normalizeFsPath(pathlib.resolve(packageDir || process.cwd()));
|
47 | this.packageUrl =
|
48 | this.brandAsResolved(vscode_uri_1.default.file(this.packageDir).toString());
|
49 | if (!this.packageUrl.endsWith('/')) {
|
50 | this.packageUrl = this.brandAsResolved(this.packageUrl + '/');
|
51 | }
|
52 | }
|
53 | resolve(firstHref, secondHref, _import) {
|
54 | const [baseUrl = this.packageUrl, unresolvedHref] = this.getBaseAndUnresolved(firstHref, secondHref);
|
55 | const resolvedHref = this.simpleUrlResolve(baseUrl, unresolvedHref, this.protocol);
|
56 | if (resolvedHref === undefined) {
|
57 | return undefined;
|
58 | }
|
59 | const url = utils_1.parseUrl(resolvedHref);
|
60 | if (this.shouldHandleAsFileUrl(url)) {
|
61 | return this.handleFileUrl(url, unresolvedHref);
|
62 | }
|
63 | return this.brandAsResolved(resolvedHref);
|
64 | }
|
65 | shouldHandleAsFileUrl(url) {
|
66 | const isLocalFileUrl = url.protocol === 'file:' && (!url.host || url.host === 'localhost');
|
67 | const isOurHost = url.host === this.host;
|
68 | return isLocalFileUrl || isOurHost;
|
69 | }
|
70 | |
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | handleFileUrl(url, unresolvedHref) {
|
79 | let pathname;
|
80 | const unresolvedUrl = utils_1.parseUrl(unresolvedHref);
|
81 | if (unresolvedUrl.pathname && unresolvedUrl.pathname.startsWith('/') &&
|
82 | unresolvedUrl.protocol !== 'file:') {
|
83 |
|
84 | let unresolvedPathname;
|
85 | try {
|
86 | unresolvedPathname =
|
87 | path_1.posix.normalize(decodeURIComponent(unresolvedUrl.pathname));
|
88 | }
|
89 | catch (e) {
|
90 | return undefined;
|
91 | }
|
92 | pathname = pathlib.join(this.packageDir, unresolvedPathname);
|
93 | }
|
94 | else {
|
95 |
|
96 |
|
97 | try {
|
98 | pathname = path_1.posix.normalize(decodeURIComponent(url.pathname || ''));
|
99 | }
|
100 | catch (e) {
|
101 | return undefined;
|
102 | }
|
103 | }
|
104 | const path = this.modifyFsPath(this.filesystemPathForPathname(pathname));
|
105 |
|
106 |
|
107 |
|
108 | const resolvedUrl = utils_1.parseUrl(vscode_uri_1.default.file(path).toString());
|
109 | resolvedUrl.search = url.search;
|
110 | resolvedUrl.hash = url.hash;
|
111 | return this.brandAsResolved(url_1.format(resolvedUrl));
|
112 | }
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | modifyFsPath(fsPath) {
|
122 | return fsPath;
|
123 | }
|
124 | relative(fromOrTo, maybeTo, _kind) {
|
125 | const [from, to] = (maybeTo !== undefined) ? [fromOrTo, maybeTo] :
|
126 | [this.packageUrl, fromOrTo];
|
127 | return this.simpleUrlRelative(from, to);
|
128 | }
|
129 | filesystemPathForPathname(decodedPathname) {
|
130 | return normalizeFsPath(vscode_uri_1.default.file(decodedPathname).fsPath);
|
131 | }
|
132 | }
|
133 | exports.FsUrlResolver = FsUrlResolver;
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | function normalizeFsPath(fsPath) {
|
139 | fsPath = pathlib.normalize(fsPath);
|
140 | if (isWindows && /^[a-z]:/.test(fsPath)) {
|
141 |
|
142 | fsPath = fsPath[0].toUpperCase() + fsPath.slice(1);
|
143 | }
|
144 | return fsPath;
|
145 | }
|
146 |
|
\ | No newline at end of file |