Skip to content

Commit

Permalink
fix(typescript-plugin): JSON parsing error when server data length > …
Browse files Browse the repository at this point in the history
…8192 (vuejs#3961)
  • Loading branch information
johnsoncodehk authored and so1ve committed Mar 4, 2024
1 parent 0f958db commit 6a03624
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
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

0 comments on commit 6a03624

Please sign in to comment.