zed/crates/prettier/src/prettier_server.js

239 lines
8.6 KiB
JavaScript
Raw Normal View History

2023-11-03 09:02:38 +00:00
const { Buffer } = require("buffer");
const fs = require("fs");
const path = require("path");
2023-11-03 09:02:38 +00:00
const { once } = require("events");
2023-09-15 14:14:22 +00:00
const prettierContainerPath = process.argv[2];
if (prettierContainerPath == null || prettierContainerPath.length == 0) {
2023-11-03 09:02:38 +00:00
process.stderr.write(
`Prettier path argument was not specified or empty.\nUsage: ${process.argv[0]} ${process.argv[1]} prettier/path\n`,
);
process.exit(1);
}
fs.stat(prettierContainerPath, (err, stats) => {
if (err) {
process.stderr.write(`Path '${prettierContainerPath}' does not exist\n`);
process.exit(1);
}
if (!stats.isDirectory()) {
process.stderr.write(`Path '${prettierContainerPath}' exists but is not a directory\n`);
process.exit(1);
}
});
2023-11-03 09:02:38 +00:00
const prettierPath = path.join(prettierContainerPath, "node_modules/prettier");
2023-09-15 14:14:22 +00:00
2023-09-22 12:52:34 +00:00
class Prettier {
constructor(path, prettier, config) {
this.path = path;
this.prettier = prettier;
this.config = config;
}
}
(async () => {
let prettier;
2023-09-22 12:52:34 +00:00
let config;
try {
prettier = await loadPrettier(prettierPath);
2023-11-03 09:02:38 +00:00
config = (await prettier.resolveConfig(prettierPath)) || {};
2023-09-18 11:56:40 +00:00
} catch (e) {
process.stderr.write(`Failed to load prettier: ${e}\n`);
process.exit(1);
}
2023-10-12 09:31:30 +00:00
process.stderr.write(`Prettier at path '${prettierPath}' loaded successfully, config: ${JSON.stringify(config)}\n`);
2023-09-15 14:14:22 +00:00
process.stdin.resume();
2023-09-22 12:52:34 +00:00
handleBuffer(new Prettier(prettierPath, prettier, config));
2023-11-03 09:02:38 +00:00
})();
2023-09-15 14:14:22 +00:00
async function handleBuffer(prettier) {
2023-10-12 09:31:30 +00:00
for await (const messageText of readStdin()) {
let message;
try {
message = JSON.parse(messageText);
} catch (e) {
sendResponse(makeError(`Failed to parse message '${messageText}': ${e}`));
continue;
}
2023-10-12 09:31:30 +00:00
// allow concurrent request handling by not `await`ing the message handling promise (async function)
2023-11-03 09:02:38 +00:00
handleMessage(message, prettier).catch((e) => {
const errorMessage = message;
if ((errorMessage.params || {}).text !== undefined) {
errorMessage.params.text = "..snip..";
}
2023-11-03 09:02:38 +00:00
sendResponse({
id: message.id,
...makeError(`error during message '${JSON.stringify(errorMessage)}' handling: ${e}`),
});
});
}
2023-09-15 14:14:22 +00:00
}
const headerSeparator = "\r\n";
2023-11-03 09:02:38 +00:00
const contentLengthHeaderName = "Content-Length";
2023-09-15 14:14:22 +00:00
async function* readStdin() {
let buffer = Buffer.alloc(0);
let streamEnded = false;
2023-11-03 09:02:38 +00:00
process.stdin.on("end", () => {
2023-09-15 14:14:22 +00:00
streamEnded = true;
});
2023-11-03 09:02:38 +00:00
process.stdin.on("data", (data) => {
2023-09-15 14:14:22 +00:00
buffer = Buffer.concat([buffer, data]);
});
async function handleStreamEnded(errorMessage) {
sendResponse(makeError(errorMessage));
buffer = Buffer.alloc(0);
messageLength = null;
2023-11-03 09:02:38 +00:00
await once(process.stdin, "readable");
streamEnded = false;
}
2023-09-15 14:14:22 +00:00
try {
let headersLength = null;
let messageLength = null;
2023-09-15 14:14:22 +00:00
main_loop: while (true) {
if (messageLength === null) {
while (buffer.indexOf(`${headerSeparator}${headerSeparator}`) === -1) {
if (streamEnded) {
2023-11-03 09:02:38 +00:00
await handleStreamEnded("Unexpected end of stream: headers not found");
continue main_loop;
} else if (buffer.length > contentLengthHeaderName.length * 10) {
2023-11-03 09:02:38 +00:00
await handleStreamEnded(
`Unexpected stream of bytes: no headers end found after ${buffer.length} bytes of input`,
);
continue main_loop;
}
2023-11-03 09:02:38 +00:00
await once(process.stdin, "readable");
}
2023-11-03 09:02:38 +00:00
const headers = buffer
.subarray(0, buffer.indexOf(`${headerSeparator}${headerSeparator}`))
.toString("ascii");
const contentLengthHeader = headers
.split(headerSeparator)
.map((header) => header.split(":"))
.filter((header) => header[2] === undefined)
.filter((header) => (header[1] || "").length > 0)
.find((header) => (header[0] || "").trim() === contentLengthHeaderName);
2023-10-12 09:31:30 +00:00
const contentLength = (contentLengthHeader || [])[1];
if (contentLength === undefined) {
await handleStreamEnded(`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`);
2023-09-15 14:14:22 +00:00
continue main_loop;
}
headersLength = headers.length + headerSeparator.length * 2;
2023-10-12 09:31:30 +00:00
messageLength = parseInt(contentLength, 10);
2023-09-15 14:14:22 +00:00
}
2023-11-03 09:02:38 +00:00
while (buffer.length < headersLength + messageLength) {
2023-09-15 14:14:22 +00:00
if (streamEnded) {
await handleStreamEnded(
2023-11-03 09:02:38 +00:00
`Unexpected end of stream: buffer length ${buffer.length} does not match expected header length ${headersLength} + body length ${messageLength}`,
);
2023-09-15 14:14:22 +00:00
continue main_loop;
}
2023-11-03 09:02:38 +00:00
await once(process.stdin, "readable");
2023-09-15 14:14:22 +00:00
}
const messageEnd = headersLength + messageLength;
const message = buffer.subarray(headersLength, messageEnd);
buffer = buffer.subarray(messageEnd);
2023-10-12 09:31:30 +00:00
headersLength = null;
messageLength = null;
2023-11-03 09:02:38 +00:00
yield message.toString("utf8");
2023-09-15 14:14:22 +00:00
}
} catch (e) {
2023-09-18 11:56:40 +00:00
sendResponse(makeError(`Error reading stdin: ${e}`));
2023-09-15 14:14:22 +00:00
} finally {
2023-11-03 09:02:38 +00:00
process.stdin.off("data", () => {});
2023-09-15 14:14:22 +00:00
}
}
async function handleMessage(message, prettier) {
2023-09-18 11:56:40 +00:00
const { method, id, params } = message;
if (method === undefined) {
throw new Error(`Message method is undefined: ${JSON.stringify(message)}`);
2023-09-18 11:56:40 +00:00
}
if (id === undefined) {
throw new Error(`Message id is undefined: ${JSON.stringify(message)}`);
2023-09-18 11:56:40 +00:00
}
2023-11-03 09:02:38 +00:00
if (method === "prettier/format") {
2023-09-18 11:56:40 +00:00
if (params === undefined || params.text === undefined) {
throw new Error(`Message params.text is undefined: ${JSON.stringify(message)}`);
2023-09-18 11:56:40 +00:00
}
2023-09-18 15:08:55 +00:00
if (params.options === undefined) {
throw new Error(`Message params.options is undefined: ${JSON.stringify(message)}`);
2023-09-18 15:08:55 +00:00
}
2023-09-22 15:40:12 +00:00
let resolvedConfig = {};
if (params.options.filepath !== undefined) {
2023-11-03 09:02:38 +00:00
resolvedConfig = (await prettier.prettier.resolveConfig(params.options.filepath)) || {};
}
2023-09-22 15:40:12 +00:00
const options = {
...(params.options.prettierOptions || prettier.config),
...resolvedConfig,
2023-09-22 15:40:12 +00:00
parser: params.options.parser,
2023-09-22 20:49:03 +00:00
plugins: params.options.plugins,
2023-11-03 09:02:38 +00:00
path: params.options.filepath,
2023-09-22 15:40:12 +00:00
};
2023-11-03 09:02:38 +00:00
process.stderr.write(
`Resolved config: ${JSON.stringify(resolvedConfig)}, will format file '${
params.options.filepath || ""
}' with options: ${JSON.stringify(options)}\n`,
);
2023-09-22 15:40:12 +00:00
const formattedText = await prettier.prettier.format(params.text, options);
2023-09-18 11:56:40 +00:00
sendResponse({ id, result: { text: formattedText } });
2023-11-03 09:02:38 +00:00
} else if (method === "prettier/clear_cache") {
2023-09-22 12:52:34 +00:00
prettier.prettier.clearConfigCache();
2023-11-03 09:02:38 +00:00
prettier.config = (await prettier.prettier.resolveConfig(prettier.path)) || {};
2023-09-18 11:56:40 +00:00
sendResponse({ id, result: null });
2023-11-03 09:02:38 +00:00
} else if (method === "initialize") {
2023-09-18 11:56:40 +00:00
sendResponse({
2023-11-03 09:02:38 +00:00
id,
2023-09-18 11:56:40 +00:00
result: {
2023-11-03 09:02:38 +00:00
capabilities: {},
},
2023-09-18 11:56:40 +00:00
});
} else {
throw new Error(`Unknown method: ${method}`);
}
}
function makeError(message) {
2023-09-18 11:56:40 +00:00
return {
error: {
2023-11-03 09:02:38 +00:00
code: -32600, // invalid request code
2023-09-18 11:56:40 +00:00
message,
2023-11-03 09:02:38 +00:00
},
2023-09-18 11:56:40 +00:00
};
}
function sendResponse(response) {
2023-10-12 09:31:30 +00:00
const responsePayloadString = JSON.stringify({
2023-09-18 11:56:40 +00:00
jsonrpc: "2.0",
2023-11-03 09:02:38 +00:00
...response,
2023-09-18 11:56:40 +00:00
});
2023-11-03 09:02:38 +00:00
const headers = `${contentLengthHeaderName}: ${Buffer.byteLength(
responsePayloadString,
)}${headerSeparator}${headerSeparator}`;
2023-10-12 09:31:30 +00:00
process.stdout.write(headers + responsePayloadString);
}
function loadPrettier(prettierPath) {
return new Promise((resolve, reject) => {
fs.access(prettierPath, fs.constants.F_OK, (err) => {
if (err) {
reject(`Path '${prettierPath}' does not exist.Error: ${err}`);
} else {
try {
resolve(require(prettierPath));
} catch (err) {
reject(`Error requiring prettier module from path '${prettierPath}'.Error: ${err}`);
}
}
});
});
}