Skip to content

Commit

Permalink
fix(typescript-plugin): fault tolerance for named pipe servers json f…
Browse files Browse the repository at this point in the history
…ile (#4075)
  • Loading branch information
Simon-He95 authored Mar 20, 2024
1 parent bacea2a commit 618a42c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
18 changes: 6 additions & 12 deletions packages/typescript-plugin/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { collectExtractProps } from './requests/collectExtractProps';
import { getComponentEvents, getComponentNames, getComponentProps, getElementAttrs, getTemplateContextProps } from './requests/componentInfos';
import { getPropertiesAtLocation } from './requests/getPropertiesAtLocation';
import { getQuickInfoAtPosition } from './requests/getQuickInfoAtPosition';
import { NamedPipeServer, connect, pipeTable } from './utils';
import { NamedPipeServer, connect, readPipeTable, updatePipeTable } from './utils';
import type { FileRegistry, VueCompilerOptions } from '@vue/language-core';

export interface Request {
Expand Down Expand Up @@ -103,16 +103,13 @@ export function startNamedPipeServer(

cleanupPipeTable();

if (!fs.existsSync(pipeTable)) {
fs.writeFileSync(pipeTable, JSON.stringify([] satisfies NamedPipeServer[]));
}
const table: NamedPipeServer[] = JSON.parse(fs.readFileSync(pipeTable, 'utf8'));
const table = readPipeTable();
table.push({
path: pipeFile,
serverKind,
currentDirectory,
});
fs.writeFileSync(pipeTable, JSON.stringify(table, undefined, 2));
updatePipeTable(table);

try {
fs.unlinkSync(pipeFile);
Expand All @@ -122,18 +119,15 @@ export function startNamedPipeServer(
}

function cleanupPipeTable() {
if (!fs.existsSync(pipeTable)) {
return;
}
for (const server of JSON.parse(fs.readFileSync(pipeTable, 'utf8'))) {
for (const server of readPipeTable()) {
connect(server.path).then(client => {
if (client) {
client.end();
}
else {
let table: NamedPipeServer[] = JSON.parse(fs.readFileSync(pipeTable, 'utf8'));
let table: NamedPipeServer[] = readPipeTable();
table = table.filter(item => item.path !== server.path);
fs.writeFileSync(pipeTable, JSON.stringify(table, undefined, 2));
updatePipeTable(table);
}
});
}
Expand Down
29 changes: 24 additions & 5 deletions packages/typescript-plugin/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,29 @@ export interface NamedPipeServer {

const { version } = require('../package.json');

export const pipeTable = path.join(os.tmpdir(), `vue-tsp-table-${version}.json`);
const pipeTableFile = path.join(os.tmpdir(), `vue-tsp-table-${version}.json`);

export function readPipeTable() {
if (!fs.existsSync(pipeTableFile)) {
return [];
}
try {
const servers: NamedPipeServer[] = JSON.parse(fs.readFileSync(pipeTableFile, 'utf8'));
return servers;
} catch {
fs.unlinkSync(pipeTableFile);
return [];
}
}

export function updatePipeTable(servers: NamedPipeServer[]) {
if (servers.length === 0) {
fs.unlinkSync(pipeTableFile);
}
else {
fs.writeFileSync(pipeTableFile, JSON.stringify(servers, undefined, 2));
}
}

export function connect(path: string) {
return new Promise<net.Socket | undefined>(resolve => {
Expand All @@ -28,10 +50,7 @@ export function connect(path: string) {
}

export async function searchNamedPipeServerForFile(fileName: string) {
if (!fs.existsSync(pipeTable)) {
return;
}
const servers: NamedPipeServer[] = JSON.parse(fs.readFileSync(pipeTable, 'utf8'));
const servers = readPipeTable();
const configuredServers = servers
.filter(item => item.serverKind === 1 satisfies ts.server.ProjectKind.Configured);
const inferredServers = servers
Expand Down

0 comments on commit 618a42c

Please sign in to comment.