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