-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathurlUtils.ts
590 lines (506 loc) · 17.7 KB
/
urlUtils.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import { promises as dns } from 'dns';
import * as path from 'path';
import { parse as urlParse, URL } from 'url';
import Cdp from '../cdp/api';
import { AnyChromiumConfiguration } from '../configuration';
import { BrowserTargetType } from '../targets/browser/browserTargets';
import { MapUsingProjection } from './datastructure/mapUsingProjection';
import { IFsUtils } from './fsUtils';
import { memoize } from './objUtils';
import { fixDriveLetterAndSlashes, forceForwardSlashes, isUncPath } from './pathUtils';
import { escapeRegexSpecialChars, isRegexSpecialChar } from './stringUtils';
let isCaseSensitive = process.platform !== 'win32';
export function resetCaseSensitivePaths() {
isCaseSensitive = process.platform !== 'win32';
}
export function setCaseSensitivePaths(sensitive: boolean) {
isCaseSensitive = sensitive;
}
export function getCaseSensitivePaths() {
return isCaseSensitive;
}
/**
* Lowercases the path if the filesystem is case-insensitive. Warning: this
* should only be done for the purposes of comparing paths. Paths returned
* through DAP and other protocols should be correctly-cased to avoid incorrect
* disambiguation.
*/
export function lowerCaseInsensitivePath(path: string) {
return isCaseSensitive ? path : path.toLowerCase();
}
/**
* Compares the paths, case-insensitively based on the platform.
*/
export function comparePathsWithoutCasing(a: string, b: string) {
return isCaseSensitive ? a === b : a.toLowerCase() === b.toLowerCase();
}
/**
* Compares the paths, case-insensitively based on the platform, and
* normalizing back- and forward-slashes.
*/
export function comparePathsWithoutCasingOrSlashes(a: string, b: string) {
return comparePathsWithoutCasing(forceForwardSlashes(a), forceForwardSlashes(b));
}
export function caseNormalizedMap<V>(): Map<string, V> {
return getCaseSensitivePaths() ? new Map() : new MapUsingProjection(lowerCaseInsensitivePath);
}
const win32PathExt =
process.platform === 'win32' ? process.env.PATHEXT?.toLowerCase().split(';') : undefined;
/**
* Gets a case-normalized binary name suitable for comparison. On Windows,
* removes any executable extension.
*/
export const getNormalizedBinaryName = (binaryPath: string) => {
const filename = lowerCaseInsensitivePath(path.basename(binaryPath));
if (win32PathExt) {
for (const ext of win32PathExt) {
if (filename.endsWith(ext)) {
return filename.slice(0, -ext.length);
}
}
}
return filename;
};
/**
* Returns the closest parent directory where the predicate returns true.
*/
export const nearestDirectoryWhere = async (
rootDir: string,
predicate: (dir: string) => Promise<boolean>,
): Promise<string | undefined> => {
while (true) {
if (await predicate(rootDir)) {
return rootDir;
}
const parent = path.dirname(rootDir);
if (parent === rootDir) {
return undefined;
}
rootDir = parent;
}
};
/**
* Returns the closest parent directory that contains a file with the given name.
*/
export const nearestDirectoryContaining = (fsUtils: IFsUtils, rootDir: string, file: string) =>
nearestDirectoryWhere(rootDir, p => fsUtils.exists(path.join(p, file)));
// todo: not super correct, and most node libraries don't handle this accurately
const knownLoopbacks = new Set<string>(['localhost', '127.0.0.1', '::1']);
const knownMetaAddresses = new Set<string>([
'0.0.0.0',
'::',
'0000:0000:0000:0000:0000:0000:0000:0000',
]);
/**
* Checks if the given address, well-formed loopback IPs. We don't need exotic
* variations like `127.1` because `dns.lookup()` will resolve the proper
* version for us. The "right" way would be to parse the IP to an integer
* like Go does (https://golang.org/pkg/net/#IP.IsLoopback).
*/
export const isLoopbackIp = (ipOrLocalhost: string) =>
knownLoopbacks.has(ipOrLocalhost.toLowerCase());
/**
* If given a URL, returns its hostname.
*/
const getHostnameFromMaybeUrl = (maybeUrl: string) => {
try {
const url = new URL(maybeUrl);
// replace brackets in ipv6 addresses:
return url.hostname.replace(/^\[|\]$/g, '');
} catch {
return maybeUrl;
}
};
/**
* Gets whether the IP address is a meta-address like 0.0.0.0.
*/
export const isMetaAddress = (address: string) =>
knownMetaAddresses.has(getHostnameFromMaybeUrl(address));
/**
* Gets whether the IP is a loopback address.
*/
export const isLoopback = memoize(async (address: string) => {
const ipOrHostname = getHostnameFromMaybeUrl(address);
if (isLoopbackIp(ipOrHostname)) {
return true;
}
try {
const resolved = await dns.lookup(ipOrHostname);
return isLoopbackIp(resolved.address);
} catch {
return false;
}
});
export function completeUrl(base: string | undefined, relative: string): string | undefined {
try {
return new URL(relative, base).href;
} catch (e) {}
}
export function removeQueryString(url: string) {
try {
const parsed = new URL(url);
parsed.search = '';
return parsed.toString();
} catch {
return url;
}
}
// This function allows relative path to escape the root:
// "http://example.com/foo/bar.js" + "../../baz/qux.js" => "http://example.com/../baz/qux.js"
// This allows relative source map sources to reference outside of webRoot.
export function completeUrlEscapingRoot(base: string | undefined, relative: string): string {
try {
new URL(relative);
return relative;
} catch (e) {}
let url: URL;
try {
url = new URL(base || '');
} catch (e) {
return relative;
}
let s = url.protocol + '//';
if (url.username) s += url.username + ':' + url.password + '@';
s += url.host;
s += path.dirname(url.pathname);
if (s[s.length - 1] !== '/') s += '/';
s += relative;
return s;
}
export function isValidUrl(url: string): boolean {
try {
new URL(url);
return true;
} catch (e) {
return false;
}
}
export function isValidUrlWithProtocol(url: string): boolean {
try {
const parsed = new URL(url);
return parsed.protocol.toLowerCase() == 'http:' || parsed.protocol.toLowerCase() == 'https:';
} catch (e) {
return false;
}
}
export function escapeForRegExp(s: string): string {
const chars = '^[]{}()\\.^$*+?|-,';
let foundChar = false;
for (let i = 0; i < chars.length; ++i) {
if (s.indexOf(chars.charAt(i)) !== -1) {
foundChar = true;
break;
}
}
if (!foundChar) return s;
let result = '';
for (let i = 0; i < s.length; ++i) {
if (chars.indexOf(s.charAt(i)) !== -1) result += '\\';
result += s.charAt(i);
}
return result;
}
/**
* Remove a slash of any flavor from the end of the path
*/
export function stripTrailingSlash(aPath: string): string {
return aPath.replace(/\/$/, '').replace(/\\$/, '');
}
const vscodeWebviewResourceSchemeRe =
/^https:\/\/([a-z0-9\-]+)\+\.vscode-resource\.vscode-(?:webview|cdn)\.net\/(.+)/i;
/**
* If urlOrPath is a file URL, removes the 'file:///', adjusting for platform differences
*/
export function fileUrlToAbsolutePath(urlOrPath: FileUrl): string;
export function fileUrlToAbsolutePath(urlOrPath: string): string | undefined;
export function fileUrlToAbsolutePath(urlOrPath: string): string | undefined {
const webviewResource = vscodeWebviewResourceSchemeRe.exec(urlOrPath);
if (webviewResource) {
urlOrPath = `${webviewResource[1]}:///${webviewResource[2]}`;
} else if (urlOrPath.startsWith('vscode-webview-resource://')) {
// todo@connor4312: is this still in use?
const url = new URL(urlOrPath);
// Strip off vscode webview url part: vscode-webview-resource://<36-char-guid>/file...
urlOrPath = url.pathname
.replace(/%2F/gi, '/')
.replace(/^\/([a-z0-9\-]+)(\/{1,2})/i, (_: string, scheme: string, sep: string) => {
if (sep.length === 1) {
return `${scheme}:///`; // Add empty authority.
} else {
return `${scheme}://`; // Url has own authority.
}
});
} else if (!isFileUrl(urlOrPath)) {
return undefined;
}
urlOrPath = urlOrPath.replace('file:///', '');
urlOrPath = decodeURIComponent(urlOrPath);
// UNC paths are returned from Chrome in the form `file:////shared/folder`,
// rather than `file:///`. This is not _entirely_ prescriptive since some
// applications can use four slashes for posix paths as well (even though V8
// doesn't seem to), so only do this if the debugger is currently running on Windows.
if (urlOrPath.startsWith('/') && process.platform === 'win32') {
if (urlOrPath[1] !== '/') {
urlOrPath = '/' + urlOrPath; // restore extra slash
}
}
if (urlOrPath[0] !== '/' && !urlOrPath.match(/^[A-Za-z]:/)) {
// If it has a : before the first /, assume it's a windows path or url.
// Ensure unix-style path starts with /, it can be removed when file:/// was stripped.
// Don't add if the url still has a protocol
urlOrPath = '/' + urlOrPath;
}
return fixDriveLetterAndSlashes(urlOrPath);
}
/**
* Converts a file URL to a windows network path, if possible.
*/
export function fileUrlToNetworkPath(urlOrPath: string): string {
if (isFileUrl(urlOrPath)) {
urlOrPath = urlOrPath.replace('file:///', '\\\\');
urlOrPath = urlOrPath.replace(/\//g, '\\');
urlOrPath = decodeURIComponent(urlOrPath);
}
return urlOrPath;
}
// TODO: this does not escape/unescape special characters, but it should.
export function absolutePathToFileUrl(absolutePath: string): string {
if (!isAbsolute(absolutePath)) {
throw new Error(
`You are using the 'absolutePathToFileUrl()' on a string which is not absolute: ${absolutePath}`,
);
}
if (platform === 'win32') {
return 'file:///' + platformPathToUrlPath(absolutePath);
}
return 'file://' + platformPathToUrlPath(absolutePath);
}
/**
* Returns whether the path is a Windows or posix path.
*/
export function isAbsolute(_path: string): boolean {
return path.posix.isAbsolute(_path) || path.win32.isAbsolute(_path);
}
/**
* Returns whether the uri looks like a data URI.
*/
export function isDataUri(uri: string | undefined | null): uri is string {
return !!uri && uri.startsWith('data:');
}
const urlToRegexChar = (char: string, arr: Set<string>, escapeRegex: boolean) => {
if (!escapeRegex || char === ':') {
arr.add(char);
return;
}
if (char === '/') {
arr.add(`\\${char}`);
return;
}
if (isRegexSpecialChar(char)) {
arr.add(`\\${char}`);
} else {
arr.add(char);
}
const encoded = encodeURIComponent(char);
if (char !== '\\' && encoded !== char) {
arr.add(encoded); // will never have any regex special chars
}
};
const createReGroup = (patterns: ReadonlySet<string>): string => {
switch (patterns.size) {
case 0:
return '';
case 1:
return patterns.values().next().value;
default:
// Prefer the more compacy [aA] form if we're only matching single
// characters, produce a non-capturing group otherwise.
const arr = [...patterns];
return arr.some(p => p.length > 1) ? `(?:${arr.join('|')})` : `[${arr.join('')}]`;
}
};
const charToUrlReGroupSet = new Set<string>();
const charRangeToUrlReGroup = (str: string, start: number, end: number, escapeRegex: boolean) => {
let re = '';
// Loop through each character of the string. Convert the char to a regex,
// creating a group, and then append that to the match.
// Note that using "for..of" is important here to loop over UTF code points.
for (const char of str.slice(start, end)) {
if (isCaseSensitive) {
urlToRegexChar(char, charToUrlReGroupSet, escapeRegex);
} else {
urlToRegexChar(char.toLowerCase(), charToUrlReGroupSet, escapeRegex);
urlToRegexChar(char.toUpperCase(), charToUrlReGroupSet, escapeRegex);
}
re += createReGroup(charToUrlReGroupSet);
charToUrlReGroupSet.clear();
}
return re;
};
/**
* Converts and escape the file URL to a regular expression.
*/
export function urlToRegex(
aPath: string,
[escapeReStart, escapeReEnd]: [number, number] = [0, aPath.length],
) {
if (escapeReEnd <= escapeReStart) {
return aPath;
}
const patterns: string[] = [];
// Split out the portion of the path that has already been converted to a regex pattern
const rePrefix = charRangeToUrlReGroup(aPath, 0, escapeReStart, false);
const reSuffix = charRangeToUrlReGroup(aPath, escapeReEnd, aPath.length, false);
const unescapedPath = aPath.slice(escapeReStart, escapeReEnd);
// aPath will often (always?) be provided as a file URI, or URL. Decode it
// --we'll reencode it as we go--and also create a match for its absolute
// path.
//
// This de- and re-encoding is important for special characters, since:
// - It comes in like "file:///c:/foo/%F0%9F%92%A9.js"
// - We decode it to file:///c:/foo/💩.js
// - For case insensitive systems, we generate a regex like [fF][oO][oO]/(?:💩|%F0%9F%92%A9).[jJ][sS]
// - If we didn't de-encode it, the percent would be case-insensitized as
// well and we would not include the original character in the regex
for (const str of [decodeURIComponent(unescapedPath), fileUrlToAbsolutePath(unescapedPath)]) {
if (!str) {
continue;
}
const re = charRangeToUrlReGroup(str, 0, str.length, true);
// If we're on windows but not case sensitive (i.e. we didn't expand a
// fancy regex above), replace `file:///c:/` or simple `c:/` patterns with
// an insensitive drive letter.
patterns.push(
makeDriveLetterReCaseInsensitive(`${rePrefix}${re}${reSuffix}`).concat('($|\\?)'),
);
}
return patterns.join('|');
}
export const makeDriveLetterReCaseInsensitive = (re: string) =>
re.replace(
/^(file:\\\/\\\/\\\/)?([a-z]):/i,
(_, file = '', letter) => `${file}[${letter.toUpperCase()}${letter.toLowerCase()}]:`,
);
/**
* Opaque typed used to indicate strings that are file URLs.
*/
export type FileUrl = string & { __opaque_file_url: true };
/**
* Returns whether the string is a file URL
*/
export function isFileUrl(candidate: string): candidate is FileUrl {
return candidate.startsWith('file:///');
}
export function maybeAbsolutePathToFileUrl(
rootPath: string | undefined,
sourceUrl: string,
): string {
if (
rootPath &&
platformPathToPreferredCase(sourceUrl).startsWith(rootPath) &&
!isValidUrl(sourceUrl)
)
return absolutePathToFileUrl(sourceUrl);
return sourceUrl;
}
let platform = process.platform;
export const overridePlatform = (newPlatform: NodeJS.Platform) => {
platform = newPlatform;
};
export const resetPlatform = () => {
platform = process.platform;
};
export function urlPathToPlatformPath(p: string): string {
if (platform === 'win32') {
p = p.replace(/\//g, '\\');
}
return decodeURI(p);
}
export function platformPathToUrlPath(p: string): string {
p = platformPathToPreferredCase(p);
if (platform === 'win32') {
if (isUncPath(p)) {
p = p.slice(1); // emit "file:////" and not "file://///" to match what V8 expects
}
return p
.split(/[\\//]/g)
.map((p, i) => (i > 0 ? encodeURIComponent(p) : p))
.join('/');
} else {
return p.split('/').map(encodeURIComponent).join('/');
}
}
export function platformPathToPreferredCase(p: string): string;
export function platformPathToPreferredCase(p: string | undefined): string | undefined;
export function platformPathToPreferredCase(p: string | undefined): string | undefined {
if (p && platform === 'win32' && p[1] === ':') return p[0].toUpperCase() + p.substring(1);
return p;
}
export type TargetFilter = (info: Cdp.Target.TargetInfo) => boolean;
/**
* Creates a target filter function for the given Chrome configuration.
*/
export const createTargetFilterForConfig = (
config: AnyChromiumConfiguration,
additonalMatches: ReadonlyArray<string> = [],
): ((t: { url: string }) => boolean) => {
const filter = config.urlFilter || config.url || ('file' in config && config.file);
if (!filter) {
return () => true;
}
const tester = createTargetFilter(filter, ...additonalMatches);
return t => tester(t.url);
};
/**
* Requires that the target is also a 'page'.
*/
export const requirePageTarget =
<T>(filter: (t: T) => boolean): ((t: T & { type: string }) => boolean) =>
t =>
t.type === BrowserTargetType.Page && filter(t);
/**
* The "isURL" from chrome-debug-core. In js-debug we use `new URL()` to see
* if a string is a URL, but this is slightly different from url.parse.
* @see https://github.com/microsoft/vscode-chrome-debug-core/blob/456318b2a4b2d3394ce8daae1e70d898f55393ea/src/utils.ts#L310
*/
function isURLCompat(urlOrPath: string): boolean {
return !!urlOrPath && !path.isAbsolute(urlOrPath) && !!urlParse(urlOrPath).protocol;
}
/**
* Creates a function to filter a target URL.
*/
export const createTargetFilter = (
...targetUrls: ReadonlyArray<string>
): ((testUrl: string) => boolean) => {
const standardizeMatch = (aUrl: string) => {
aUrl = aUrl.toLowerCase();
const fileUrl = fileUrlToAbsolutePath(aUrl);
if (fileUrl) {
// Strip file:///, if present
aUrl = fileUrl;
} else if (isURLCompat(aUrl) && aUrl.includes('://')) {
// Strip the protocol, if present
aUrl = aUrl.substr(aUrl.indexOf('://') + 3);
}
// Remove optional trailing /
if (aUrl.endsWith('/')) {
aUrl = aUrl.substr(0, aUrl.length - 1);
}
const hashIndex = aUrl.indexOf('#');
if (hashIndex !== -1) {
aUrl = aUrl.slice(0, aUrl[hashIndex - 1] === '/' ? hashIndex - 1 : hashIndex);
}
return aUrl;
};
const escaped = targetUrls.map(url =>
escapeRegexSpecialChars(standardizeMatch(url), '/*').replace(/(\/\*$)|\*/g, '.*'),
);
const targetUrlRegex = new RegExp('^(' + escaped.join('|') + ')$', 'g');
return testUrl => {
targetUrlRegex.lastIndex = 0;
return targetUrlRegex.test(standardizeMatch(testUrl));
};
};