prettier_server.js

  1const { Buffer } = require("buffer");
  2const fs = require("fs");
  3const path = require("path");
  4const { once } = require("events");
  5
  6const prettierContainerPath = process.argv[2];
  7if (prettierContainerPath == null || prettierContainerPath.length == 0) {
  8    process.stderr.write(
  9        `Prettier path argument was not specified or empty.\nUsage: ${process.argv[0]} ${process.argv[1]} prettier/path\n`,
 10    );
 11    process.exit(1);
 12}
 13fs.stat(prettierContainerPath, (err, stats) => {
 14    if (err) {
 15        process.stderr.write(`Path '${prettierContainerPath}' does not exist\n`);
 16        process.exit(1);
 17    }
 18
 19    if (!stats.isDirectory()) {
 20        process.stderr.write(`Path '${prettierContainerPath}' exists but is not a directory\n`);
 21        process.exit(1);
 22    }
 23});
 24const prettierPath = path.join(prettierContainerPath, "node_modules/prettier");
 25
 26class Prettier {
 27    constructor(path, prettier, config) {
 28        this.path = path;
 29        this.prettier = prettier;
 30        this.config = config;
 31    }
 32}
 33
 34(async () => {
 35    let prettier;
 36    let config;
 37    try {
 38        prettier = await loadPrettier(prettierPath);
 39        config = (await prettier.resolveConfig(prettierPath)) || {};
 40    } catch (e) {
 41        process.stderr.write(`Failed to load prettier: ${e}\n`);
 42        process.exit(1);
 43    }
 44    process.stderr.write(`Prettier at path '${prettierPath}' loaded successfully, config: ${JSON.stringify(config)}\n`);
 45    process.stdin.resume();
 46    handleBuffer(new Prettier(prettierPath, prettier, config));
 47})();
 48
 49async function handleBuffer(prettier) {
 50    for await (const messageText of readStdin()) {
 51        let message;
 52        try {
 53            message = JSON.parse(messageText);
 54        } catch (e) {
 55            sendResponse(makeError(`Failed to parse message '${messageText}': ${e}`));
 56            continue;
 57        }
 58        // allow concurrent request handling by not `await`ing the message handling promise (async function)
 59        handleMessage(message, prettier).catch((e) => {
 60            const errorMessage = message;
 61            if ((errorMessage.params || {}).text !== undefined) {
 62                errorMessage.params.text = "..snip..";
 63            }
 64            sendResponse({
 65                id: message.id,
 66                ...makeError(`error during message '${JSON.stringify(errorMessage)}' handling: ${e}`),
 67            });
 68        });
 69    }
 70}
 71
 72const headerSeparator = "\r\n";
 73const contentLengthHeaderName = "Content-Length";
 74
 75async function* readStdin() {
 76    let buffer = Buffer.alloc(0);
 77    let streamEnded = false;
 78    process.stdin.on("end", () => {
 79        streamEnded = true;
 80    });
 81    process.stdin.on("data", (data) => {
 82        buffer = Buffer.concat([buffer, data]);
 83    });
 84
 85    async function handleStreamEnded(errorMessage) {
 86        sendResponse(makeError(errorMessage));
 87        buffer = Buffer.alloc(0);
 88        messageLength = null;
 89        await once(process.stdin, "readable");
 90        streamEnded = false;
 91    }
 92
 93    try {
 94        let headersLength = null;
 95        let messageLength = null;
 96        main_loop: while (true) {
 97            if (messageLength === null) {
 98                while (buffer.indexOf(`${headerSeparator}${headerSeparator}`) === -1) {
 99                    if (streamEnded) {
100                        await handleStreamEnded("Unexpected end of stream: headers not found");
101                        continue main_loop;
102                    } else if (buffer.length > contentLengthHeaderName.length * 10) {
103                        await handleStreamEnded(
104                            `Unexpected stream of bytes: no headers end found after ${buffer.length} bytes of input`,
105                        );
106                        continue main_loop;
107                    }
108                    await once(process.stdin, "readable");
109                }
110                const headers = buffer
111                    .subarray(0, buffer.indexOf(`${headerSeparator}${headerSeparator}`))
112                    .toString("ascii");
113                const contentLengthHeader = headers
114                    .split(headerSeparator)
115                    .map((header) => header.split(":"))
116                    .filter((header) => header[2] === undefined)
117                    .filter((header) => (header[1] || "").length > 0)
118                    .find((header) => (header[0] || "").trim() === contentLengthHeaderName);
119                const contentLength = (contentLengthHeader || [])[1];
120                if (contentLength === undefined) {
121                    await handleStreamEnded(`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`);
122                    continue main_loop;
123                }
124                headersLength = headers.length + headerSeparator.length * 2;
125                messageLength = parseInt(contentLength, 10);
126            }
127
128            while (buffer.length < headersLength + messageLength) {
129                if (streamEnded) {
130                    await handleStreamEnded(
131                        `Unexpected end of stream: buffer length ${buffer.length} does not match expected header length ${headersLength} + body length ${messageLength}`,
132                    );
133                    continue main_loop;
134                }
135                await once(process.stdin, "readable");
136            }
137
138            const messageEnd = headersLength + messageLength;
139            const message = buffer.subarray(headersLength, messageEnd);
140            buffer = buffer.subarray(messageEnd);
141            headersLength = null;
142            messageLength = null;
143            yield message.toString("utf8");
144        }
145    } catch (e) {
146        sendResponse(makeError(`Error reading stdin: ${e}`));
147    } finally {
148        process.stdin.off("data", () => {});
149    }
150}
151
152async function handleMessage(message, prettier) {
153    const { method, id, params } = message;
154    if (method === undefined) {
155        throw new Error(`Message method is undefined: ${JSON.stringify(message)}`);
156    } else if (method == "initialized") {
157        return;
158    }
159
160    if (id === undefined) {
161        throw new Error(`Message id is undefined: ${JSON.stringify(message)}`);
162    }
163
164    if (method === "prettier/format") {
165        if (params === undefined || params.text === undefined) {
166            throw new Error(`Message params.text is undefined: ${JSON.stringify(message)}`);
167        }
168        if (params.options === undefined) {
169            throw new Error(`Message params.options is undefined: ${JSON.stringify(message)}`);
170        }
171
172        let resolvedConfig = {};
173        if (params.options.filepath !== undefined) {
174            resolvedConfig = (await prettier.prettier.resolveConfig(params.options.filepath)) || {};
175        }
176        
177        const plugins = Array.isArray(resolvedConfig?.plugins) && resolvedConfig.plugins.length > 0 ? 
178          resolvedConfig.plugins : 
179          params.options.plugins;
180
181        const options = {
182            ...(params.options.prettierOptions || prettier.config),
183            ...resolvedConfig,
184            plugins,
185            parser: params.options.parser,
186            filepath: params.options.filepath,
187        };
188        process.stderr.write(
189            `Resolved config: ${JSON.stringify(resolvedConfig)}, will format file '${
190                params.options.filepath || ""
191            }' with options: ${JSON.stringify(options)}\n`,
192        );
193        const formattedText = await prettier.prettier.format(params.text, options);
194        sendResponse({ id, result: { text: formattedText } });
195    } else if (method === "prettier/clear_cache") {
196        prettier.prettier.clearConfigCache();
197        prettier.config = (await prettier.prettier.resolveConfig(prettier.path)) || {};
198        sendResponse({ id, result: null });
199    } else if (method === "initialize") {
200        sendResponse({
201            id,
202            result: {
203                capabilities: {},
204            },
205        });
206    } else {
207        throw new Error(`Unknown method: ${method}`);
208    }
209}
210
211function makeError(message) {
212    return {
213        error: {
214            code: -32600, // invalid request code
215            message,
216        },
217    };
218}
219
220function sendResponse(response) {
221    const responsePayloadString = JSON.stringify({
222        jsonrpc: "2.0",
223        ...response,
224    });
225    const headers = `${contentLengthHeaderName}: ${Buffer.byteLength(
226        responsePayloadString,
227    )}${headerSeparator}${headerSeparator}`;
228    process.stdout.write(headers + responsePayloadString);
229}
230
231function loadPrettier(prettierPath) {
232    return new Promise((resolve, reject) => {
233        fs.access(prettierPath, fs.constants.F_OK, (err) => {
234            if (err) {
235                reject(`Path '${prettierPath}' does not exist.Error: ${err}`);
236            } else {
237                try {
238                    resolve(require(prettierPath));
239                } catch (err) {
240                    reject(`Error requiring prettier module from path '${prettierPath}'.Error: ${err}`);
241                }
242            }
243        });
244    });
245}