-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathschemaRequestHandler.ts
More file actions
116 lines (104 loc) · 4.39 KB
/
schemaRequestHandler.ts
File metadata and controls
116 lines (104 loc) · 4.39 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
import { join } from 'path';
import { getErrorStatusDescription, xhr, XHRResponse } from 'request-light';
import * as URL from 'url';
import { Connection, RequestType, WorkspaceFolder } from 'vscode-languageserver';
import { URI } from 'vscode-uri';
import { CustomSchemaContentRequest, VSCodeContentRequest } from '../../requestTypes';
import { isRelativePath, relativeToAbsolutePath } from '../utils/paths';
import { WorkspaceContextService } from '../yamlLanguageService';
export interface FileSystem {
readFile(fsPath: string, encoding?: string): Promise<string>;
}
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace FSReadUri {
export const type: RequestType<string, string, unknown> = new RequestType('fs/readUri');
}
/**
* Handles schema content requests given the schema URI
* @param uri can be a local file, vscode request, http(s) request or a custom request
*/
export const schemaRequestHandler = async (
connection: Connection,
uri: string,
workspaceFolders: WorkspaceFolder[],
workspaceRoot: URI,
useVSCodeContentRequest: boolean,
fs: FileSystem,
isWeb: boolean
): Promise<string> => {
if (!uri) {
return Promise.reject('No schema specified');
}
// If the requested schema URI is a relative file path
// Convert it into a proper absolute path URI
if (isRelativePath(uri)) {
// HACK: the fs/readUri extension is only available with vscode-yaml,
// and this fix is specific to vscode-yaml on web, so don't use it in other cases
if (workspaceFolders.length === 1 && isWeb) {
const wsUri = URI.parse(workspaceFolders[0].uri);
const wsDirname = wsUri.path;
const modifiedUri = wsUri.with({ path: join(wsDirname, uri) });
try {
return connection.sendRequest(FSReadUri.type, modifiedUri.toString());
} catch (e) {
connection.window.showErrorMessage(`failed to get content of '${modifiedUri}': ${e}`);
}
} else {
uri = relativeToAbsolutePath(workspaceFolders, workspaceRoot, uri);
}
}
let scheme = URI.parse(uri).scheme.toLowerCase();
// test if uri is windows path, ie starts with 'c:\'
if (/^[a-z]:[\\/]/i.test(uri)) {
const winUri = URI.file(uri);
scheme = winUri.scheme.toLowerCase();
uri = winUri.toString();
}
// If the requested schema is a local file, read and return the file contents
if (scheme === 'file') {
const fsPath = URI.parse(uri).fsPath;
return fs.readFile(fsPath, 'UTF-8').catch(() => {
// If there was an error reading the file, return empty error message
// Otherwise return the file contents as a string
return '';
});
}
// HTTP(S) requests are sent and the response result is either the schema content or an error
if (scheme === 'http' || scheme === 'https') {
// If we are running inside of VSCode we need to make a content request. This content request
// will make it so that schemas behind VPN's will resolve correctly
if (useVSCodeContentRequest) {
return connection.sendRequest(VSCodeContentRequest.type, uri).then(
(responseText) => {
return responseText;
},
(error) => {
return Promise.reject(error.message);
}
) as Promise<string>;
}
// Send the HTTP(S) schema content request and return the result
const version = (typeof process !== 'undefined' && process.env.YAML_LANGUAGE_SERVER_VERSION) || 'unknown';
const nodeVersion = typeof process !== 'undefined' && process.versions?.node ? ` node/${process.versions.node}` : '';
const platform = typeof process !== 'undefined' && process.platform ? ` (${process.platform})` : '';
const headers = {
'Accept-Encoding': 'gzip, deflate',
'User-Agent': `yaml-language-server/${version} (RedHat)${nodeVersion}${platform}`,
};
return xhr({ url: uri, followRedirects: 5, headers }).then(
(response) => {
return response.responseText;
},
(error: XHRResponse) => {
return Promise.reject(error.responseText || getErrorStatusDescription(error.status) || error.toString());
}
);
}
// Neither local file nor vscode, nor HTTP(S) schema request, so send it off as a custom request
return connection.sendRequest(CustomSchemaContentRequest.type, uri) as Promise<string>;
};
export const workspaceContext: WorkspaceContextService = {
resolveRelativePath: (relativePath: string, resource: string) => {
return URL.resolve(resource, relativePath);
},
};