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