forked from microsoft/vscode-node-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURI.ts
More file actions
135 lines (110 loc) · 3.21 KB
/
Copy pathURI.ts
File metadata and controls
135 lines (110 loc) · 3.21 KB
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as URL from 'url';
import * as PathUtils from './pathUtilities';
function isWindows(absPath: string): boolean {
return /^[a-zA-Z]\:\\/.test(absPath);
}
function stripFirst(path: string, c: string) {
return path[0] === c ? path.substr(1) : path;
}
function stripLast(path: string, c: string) {
return path[path.length-1] === c ? path.substr(0, path.length-1) : path;
}
export class URI {
private _uri: string;
private _u: URL.Url;
/**
* Creates a file URI from the given file path.
* If path is relative, an absolute base path must be provided as well.
* If base is missing or if base is not absolute, an exception is thrown.
*/
public static file(path: string, base?: string) {
if (typeof path !== 'string') {
throw new Error('string expected');
}
if (!PathUtils.isAbsolutePath(path)) {
if (base) {
if (PathUtils.isAbsolutePath(base)) {
if (isWindows(base)) {
path = stripLast(base, '\\') + '\\' + stripFirst(path, '\\');
} else {
path = stripLast(base, '/') + '/' + stripFirst(path, '/');
}
} else {
throw new Error('base path not absolute');
}
//path = PathUtils.makePathAbsolute(base, path);
} else {
throw new Error('base path missing');
}
}
if (isWindows(path)) { // is windows
path = path.replace(/\\/g, '/');
}
// simplify '/./' -> '/'
path = path.replace(/\/\.\//g, '/');
if (path[0] !== '/') {
path = '/' + path;
}
path = encodeURI('file://' + path);
const u = new URI();
u._uri = path;
try {
u._u = URL.parse(path);
}
catch (e) {
throw new Error(e);
}
return u;
}
/**
* Creates a URI from the given string.
*/
public static parse(uri: string, base?: string) {
if (uri.indexOf('http:') === 0 || uri.indexOf('https:') === 0 || uri.indexOf('file:') === 0 || uri.indexOf('data:') === 0 ) {
const u = new URI();
u._uri = uri;
try {
u._u = URL.parse(uri);
}
catch (e) {
throw new Error(e);
}
return u;
}
return URI.file(uri, base);
}
constructor() {
}
uri(): string {
return this._uri;
}
isFile(): boolean {
return this._u.protocol === 'file:';
}
filePath(): string {
let path = <string> this._u.path;
path = decodeURI(path);
if (/^\/[a-zA-Z]\:\//.test(path)) {
path = path.substr(1); // remove additional '/'
path = path.replace(/\//g, '\\'); // convert slashes to backslashes
}
return path;
}
isData() {
return this._u.protocol === 'data:' && this._uri.indexOf('application/json') > 0 && this._uri.indexOf('base64') > 0;
}
data(): string | null {
const pos = this._uri.lastIndexOf(',');
if (pos > 0) {
return this._uri.substr(pos+1);
}
return null;
}
isHTTP(): boolean {
return this._u.protocol === 'http:' || this._u.protocol === 'https:';
}
}