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
  • Loading branch information
johnsoncodehk committed Mar 4, 2024
1 parent fc1e288 commit cd56398
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
27 changes: 17 additions & 10 deletions packages/typescript-plugin/lib/client.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 cd56398

Please sign in to comment.