-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathbrowserPathResolver.ts
247 lines (220 loc) · 7.92 KB
/
browserPathResolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import { inject, injectable } from 'inversify';
import * as path from 'path';
import { URL } from 'url';
import { IVueFileMapper, VueHandling } from '../../adapter/vueFileMapper';
import { IFsUtils } from '../../common/fsUtils';
import { ILogger } from '../../common/logging';
import {
fixDriveLetterAndSlashes,
isSubpathOrEqualTo,
properRelative,
properResolve,
} from '../../common/pathUtils';
import { SourceMap } from '../../common/sourceMaps/sourceMap';
import {
defaultPathMappingResolver,
getComputedSourceRoot,
getFullSourceEntry,
} from '../../common/sourceMaps/sourceMapResolutionUtils';
import { IUrlResolution } from '../../common/sourcePathResolver';
import * as utils from '../../common/urlUtils';
import { isValidUrlWithProtocol, urlToRegex } from '../../common/urlUtils';
import { PathMapping } from '../../configuration';
import { ISourcePathResolverOptions, SourcePathResolverBase } from '../sourcePathResolver';
export interface IOptions extends ISourcePathResolverOptions {
baseUrl?: string;
pathMapping: PathMapping;
clientID: string | undefined;
remoteFilePrefix: string | undefined;
}
const enum Suffix {
Html = '.html',
Index = 'index.html',
}
const wildcardHostname = 'https?:\\/\\/[^\\/]+\\/';
@injectable()
export class BrowserSourcePathResolver extends SourcePathResolverBase<IOptions> {
constructor(
@inject(IVueFileMapper) private readonly vueMapper: IVueFileMapper,
@inject(IFsUtils) private readonly fsUtils: IFsUtils,
options: IOptions,
logger: ILogger,
) {
super(options, logger);
}
/** @override */
private absolutePathToUrlPath(absolutePath: string): { url: string; needsWildcard: boolean } {
absolutePath = path.normalize(absolutePath);
const { baseUrl, pathMapping } = this.options;
const defaultMapping = ['/', pathMapping['/']] as const;
const bestMatch =
Object.entries(pathMapping)
.sort(
([p1, directoryA], [p2, directoryB]) =>
directoryB.length - directoryA.length || p2.length - p1.length,
)
.find(([, directory]) => isSubpathOrEqualTo(directory, absolutePath)) || defaultMapping;
if (!bestMatch) {
return { url: utils.absolutePathToFileUrl(absolutePath), needsWildcard: false };
}
let urlPath = utils.platformPathToUrlPath(path.relative(bestMatch[1], absolutePath));
const urlPrefix = bestMatch[0].replace(/\/$|^\//g, '');
if (urlPrefix) {
urlPath = urlPrefix + '/' + urlPath;
}
if (!baseUrl && !utils.isValidUrl(urlPath)) {
return { url: urlPath, needsWildcard: true };
}
return { url: utils.completeUrlEscapingRoot(baseUrl, urlPath), needsWildcard: false };
}
public async urlToAbsolutePath({ url, map }: IUrlResolution): Promise<string | undefined> {
const queryCharacter = url.indexOf('?');
// Workaround for vue, see https://github.com/microsoft/vscode-js-debug/issues/239
if (queryCharacter !== -1 && url.slice(queryCharacter - 4, queryCharacter) !== '.vue') {
url = url.slice(0, queryCharacter);
}
return map ? this.sourceMapSourceToAbsolute(url, map) : this.simpleUrlToAbsolute(url);
}
private async simpleUrlToAbsolute(url: string) {
// Simple eval'd code will never have a valid path
if (!url) {
return;
}
// If we have a file URL, we know it's absolute already and points
// to a location on disk.
if (utils.isFileUrl(url)) {
const abs = utils.fileUrlToAbsolutePath(url);
if (await this.fsUtils.exists(abs)) {
return abs;
}
const net = utils.fileUrlToNetworkPath(url);
if (await this.fsUtils.exists(net)) {
return net;
}
}
let pathname: string;
try {
const parsed = new URL(url);
if (!parsed.pathname || parsed.pathname === '/') {
pathname = 'index.html';
} else {
pathname = parsed.pathname;
}
if (parsed.protocol === 'webpack-internal:') {
return undefined;
}
} catch {
pathname = url;
}
const extname = path.extname(pathname);
const pathParts = pathname
.replace(/^\//, '') // Strip leading /
.split(/[\/\\]/);
while (pathParts.length > 0) {
const joinedPath = '/' + pathParts.join('/');
const clientPath = await defaultPathMappingResolver(
joinedPath,
this.options.pathMapping,
this.logger,
);
if (clientPath) {
if (!extname && (await this.fsUtils.exists(clientPath + Suffix.Html))) {
return clientPath + Suffix.Html;
}
if (await this.fsUtils.exists(clientPath)) {
return clientPath;
}
}
pathParts.shift();
}
}
private async sourceMapSourceToAbsolute(url: string, map: SourceMap) {
if (!this.shouldResolveSourceMap(map.metadata)) {
return undefined;
}
switch (this.vueMapper.getVueHandling(url)) {
case VueHandling.Omit:
return undefined;
case VueHandling.Lookup:
const vuePath = await this.vueMapper.lookup(url);
if (vuePath) {
return fixDriveLetterAndSlashes(vuePath);
}
break;
default:
// fall through
}
url = this.normalizeSourceMapUrl(url);
const { pathMapping } = this.options;
const fullSourceEntry = getFullSourceEntry(map.sourceRoot, url);
let mappedFullSourceEntry = this.sourceMapOverrides.apply(fullSourceEntry);
if (mappedFullSourceEntry !== fullSourceEntry) {
mappedFullSourceEntry = fixDriveLetterAndSlashes(mappedFullSourceEntry);
// Prefixing ../ClientApp is a workaround for a bug in ASP.NET debugging in VisualStudio because the wwwroot is not properly configured
const clientAppPath = properResolve(
pathMapping['/'],
'..',
'ClientApp',
properRelative(pathMapping['/'], mappedFullSourceEntry),
);
if (
this.options.clientID === 'visualstudio' &&
fullSourceEntry.startsWith('webpack:///') &&
!(await this.fsUtils.exists(mappedFullSourceEntry)) &&
(await this.fsUtils.exists(clientAppPath))
) {
return clientAppPath;
} else {
return mappedFullSourceEntry;
}
}
if (utils.isFileUrl(url)) {
return utils.fileUrlToAbsolutePath(url);
}
if (!path.isAbsolute(url)) {
const computedSourceRoot = await getComputedSourceRoot(
map.sourceRoot,
map.metadata.compiledPath,
pathMapping,
defaultPathMappingResolver,
this.logger,
);
if (isValidUrlWithProtocol(computedSourceRoot)) {
return new URL(url, computedSourceRoot).href;
}
return properResolve(computedSourceRoot, url);
}
return fixDriveLetterAndSlashes(url);
}
/**
* @override
*/
public absolutePathToUrlRegexp(absolutePath: string) {
const transform = this.absolutePathToUrlPath(absolutePath);
let url = transform.url;
// Make "index" paths optional since some servers, like vercel's serve,
// allow omitting them.
let endRegexEscape = absolutePath.length;
if (url.endsWith(Suffix.Index)) {
endRegexEscape = url.length - Suffix.Index.length - 1;
url = url.slice(0, endRegexEscape) + `\\/?($|index(\\.html)?)`;
} else if (url.endsWith(Suffix.Html)) {
endRegexEscape = url.length - Suffix.Html.length;
url = url.slice(0, endRegexEscape) + `(\\.html)?`;
}
// If there's no base URL, allow the URL to match _any_ protocol
let startRegexEscape = 0;
if (transform.needsWildcard) {
url = wildcardHostname + url;
startRegexEscape = wildcardHostname.length;
endRegexEscape += wildcardHostname.length;
}
const urlRegex = urlToRegex(url, [startRegexEscape, endRegexEscape]);
return transform.needsWildcard
? `${urlToRegex(utils.absolutePathToFileUrl(absolutePath))}|${urlRegex}`
: urlRegex;
}
}