Skip to content

Commit 1f0cd8e

Browse files
committed
perf: avoid sending FS requests to the parent directory of the workspace folder
1 parent 4ad286e commit 1f0cd8e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/server.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,42 @@ connection.onInitialize(async params => {
3939
tsdk.diagnosticMessages,
4040
async ({ env, uriConverter, projectHost, sys, configFileName }) => {
4141
const { asFileName, asUri } = uriConverter;
42+
const workspaceFolders = [...server.workspaceFolders.keys()];
4243
uriConverter.asUri = (fileName) => {
4344
if (fileName === '/node_modules') {
4445
return URI.parse('vscode-typescript-web://cdn/');
4546
}
4647
if (fileName.startsWith('/node_modules/')) {
4748
return URI.parse('vscode-typescript-web://cdn/' + fileName.slice('/node_modules/'.length));
4849
}
50+
if (workspaceFolders.length === 1 && fileName.startsWith('/')) {
51+
const basePath = workspaceFolders[0].path.endsWith('/')
52+
? workspaceFolders[0].path
53+
: workspaceFolders[0].path + '/';
54+
return URI.from({
55+
...workspaceFolders[0],
56+
path: basePath.slice(0, -1) + fileName,
57+
});
58+
}
4959
return asUri(fileName);
5060
};
5161
uriConverter.asFileName = (uri) => {
5262
const cdnPath = getCdnPath(uri);
5363
if (cdnPath !== undefined) {
5464
return '/node_modules/' + cdnPath;
5565
}
66+
if (workspaceFolders.length === 1) {
67+
const basePath = workspaceFolders[0].path.endsWith('/')
68+
? workspaceFolders[0].path
69+
: workspaceFolders[0].path + '/';
70+
if (
71+
uri.scheme === workspaceFolders[0].scheme
72+
&& uri.authority === workspaceFolders[0].authority
73+
&& uri.path.startsWith(basePath)
74+
) {
75+
return uri.path.slice(basePath.length - 1);
76+
}
77+
}
5678
return asFileName(uri);
5779
};
5880
const { fs } = env;
@@ -61,18 +83,27 @@ connection.onInitialize(async params => {
6183
if (getCdnPath(uri) !== undefined) {
6284
return ataSys.stat(uri);
6385
}
86+
if (uri.path.endsWith('/node_modules') || uri.path.includes('/node_modules/')) {
87+
return;
88+
}
6489
return fs?.stat(uri);
6590
},
6691
readDirectory(uri) {
6792
if (getCdnPath(uri) !== undefined) {
6893
return ataSys.readDirectory(uri);
6994
}
95+
if (uri.path.endsWith('/node_modules') || uri.path.includes('/node_modules/')) {
96+
return [];
97+
}
7098
return fs?.readDirectory(uri) ?? []
7199
},
72100
readFile(uri) {
73101
if (getCdnPath(uri) !== undefined) {
74102
return ataSys.readFile(uri);
75103
}
104+
if (uri.path.endsWith('/node_modules') || uri.path.includes('/node_modules/')) {
105+
return;
106+
}
76107
return fs?.readFile(uri);
77108
},
78109
}

0 commit comments

Comments
 (0)