Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript-plugin): JSON parsing error when server data length > 8192 #3961

Merged
merged 2 commits into from Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/language-server/node.ts
Expand Up @@ -124,9 +124,9 @@ connection.onRequest(GetConvertAttrCasingEditsRequest.type, async params => {
});

connection.onRequest(GetConnectedNamedPipeServerRequest.type, async fileName => {
const connected = await tsPluginClient.connectForFile(fileName);
if (connected) {
return connected[1];
const server = await tsPluginClient.searchNamedPipeServerForFile(fileName);
if (server) {
return server;
}
});

Expand Down
27 changes: 17 additions & 10 deletions packages/typescript-plugin/lib/client.ts
Expand Up @@ -81,18 +81,20 @@ export function getElementAttrs(
}

async function sendRequest<T>(request: Request) {
const connected = await connectForFile(request.args[0]);
if (!connected) {
const server = await searchNamedPipeServerForFile(request.args[0]);
if (!server) {
console.warn('[Vue Named Pipe Client] No server found for', request.args[0]);
return;
}
const [client] = connected;
const result = await sendRequestWorker<T>(request, client);
client.end();
return result;
const client = await connect(server.path);
if (!client) {
console.warn('[Vue Named Pipe Client] Failed to connect to', server.path);
return;
}
return await sendRequestWorker<T>(request, client);
}

export async function connectForFile(fileName: string) {
export async function searchNamedPipeServerForFile(fileName: string) {
if (!fs.existsSync(pipeTable)) {
return;
}
Expand All @@ -107,23 +109,28 @@ export async function connectForFile(fileName: string) {
if (client) {
const response = await sendRequestWorker<boolean>({ type: 'containsFile', args: [fileName] }, client);
if (response) {
return [client, server] as const;
return server;
}
}
}
for (const server of inferredServers) {
if (!path.relative(server.currentDirectory, fileName).startsWith('..')) {
const client = await connect(server.path);
if (client) {
return [client, server] as const;
return server;
}
}
}
}

function sendRequestWorker<T>(request: Request, client: net.Socket) {
return new Promise<T | undefined | null>(resolve => {
client.once('data', data => {
let dataChunks: Buffer[] = [];
client.on('data', chunk => {
dataChunks.push(chunk);
});
client.on('end', () => {
const data = Buffer.concat(dataChunks);
const text = data.toString();
resolve(JSON.parse(text));
});
Expand Down
1 change: 1 addition & 0 deletions packages/typescript-plugin/lib/server.ts
Expand Up @@ -77,6 +77,7 @@ export function startNamedPipeServer(serverKind: ts.server.ProjectKind, currentD
console.warn('[Vue Named Pipe Server] Unknown request type:', request.type);
connection.write(JSON.stringify(null));
}
connection.end();
});
connection.on('error', err => console.error('[Vue Named Pipe Server]', err.message));
});
Expand Down