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(`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: ${config}\n`);
 43    process.stdin.resume();
 44    handleBuffer(new Prettier(prettierPath, prettier, config));
 45})()
 46
 47async function handleBuffer(prettier) {
 48    for await (let 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        handleMessage(message, prettier).catch(e => {
 57            sendResponse({ id: message.id, ...makeError(`error during message handling: ${e}`) });
 58        });
 59    }
 60}
 61
 62const headerSeparator = "\r\n";
 63const contentLengthHeaderName = 'Content-Length';
 64
 65async function* readStdin() {
 66    let buffer = Buffer.alloc(0);
 67    let streamEnded = false;
 68    process.stdin.on('end', () => {
 69        streamEnded = true;
 70    });
 71    process.stdin.on('data', (data) => {
 72        buffer = Buffer.concat([buffer, data]);
 73    });
 74
 75    async function handleStreamEnded(errorMessage) {
 76        sendResponse(makeError(errorMessage));
 77        buffer = Buffer.alloc(0);
 78        messageLength = null;
 79        await once(process.stdin, 'readable');
 80        streamEnded = false;
 81    }
 82
 83    try {
 84        let headersLength = null;
 85        let messageLength = null;
 86        main_loop: while (true) {
 87            if (messageLength === null) {
 88                while (buffer.indexOf(`${headerSeparator}${headerSeparator}`) === -1) {
 89                    if (streamEnded) {
 90                        await handleStreamEnded('Unexpected end of stream: headers not found');
 91                        continue main_loop;
 92                    } else if (buffer.length > contentLengthHeaderName.length * 10) {
 93                        await handleStreamEnded(`Unexpected stream of bytes: no headers end found after ${buffer.length} bytes of input`);
 94                        continue main_loop;
 95                    }
 96                    await once(process.stdin, 'readable');
 97                }
 98                const headers = buffer.subarray(0, buffer.indexOf(`${headerSeparator}${headerSeparator}`)).toString('ascii');
 99                const contentLengthHeader = headers.split(headerSeparator).map(header => header.split(':'))
100                    .filter(header => header[2] === undefined)
101                    .filter(header => (header[1] || '').length > 0)
102                    .find(header => header[0].trim() === contentLengthHeaderName);
103                if (contentLengthHeader === undefined) {
104                    await handleStreamEnded(`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`);
105                    continue main_loop;
106                }
107                headersLength = headers.length + headerSeparator.length * 2;
108                messageLength = parseInt(contentLengthHeader[1], 10);
109            }
110
111            while (buffer.length < (headersLength + messageLength)) {
112                if (streamEnded) {
113                    await handleStreamEnded(
114                        `Unexpected end of stream: buffer length ${buffer.length} does not match expected header length ${headersLength} + body length ${messageLength}`);
115                    continue main_loop;
116                }
117                await once(process.stdin, 'readable');
118            }
119
120            const messageEnd = headersLength + messageLength;
121            const message = buffer.subarray(headersLength, messageEnd);
122            buffer = buffer.subarray(messageEnd);
123            messageLength = null;
124            yield message.toString('utf8');
125        }
126    } catch (e) {
127        sendResponse(makeError(`Error reading stdin: ${e}`));
128    } finally {
129        process.stdin.off('data', () => { });
130    }
131}
132
133// TODO kb, more methods?
134// shutdown
135// error
136async function handleMessage(message, prettier) {
137    const { method, id, params } = message;
138    if (method === undefined) {
139        throw new Error(`Message method is undefined: ${JSON.stringify(message)}`);
140    }
141    if (id === undefined) {
142        throw new Error(`Message id is undefined: ${JSON.stringify(message)}`);
143    }
144
145    if (method === 'prettier/format') {
146        if (params === undefined || params.text === undefined) {
147            throw new Error(`Message params.text is undefined: ${JSON.stringify(message)}`);
148        }
149        if (params.options === undefined) {
150            throw new Error(`Message params.options is undefined: ${JSON.stringify(message)}`);
151        }
152
153        let resolvedConfig = {};
154        if (params.options.filepath !== undefined) {
155            resolvedConfig = await prettier.prettier.resolveConfig(params.options.filepath) || {};
156        }
157
158        const options = {
159            ...(params.options.prettierOptions || prettier.config),
160            ...resolvedConfig,
161            parser: params.options.parser,
162            plugins: params.options.plugins,
163            path: params.options.filepath
164        };
165        process.stderr.write(`Resolved config: ${JSON.stringify(resolvedConfig)}, will format file '${params.options.filepath || ''}' with options: ${JSON.stringify(options)}\n`);
166        const formattedText = await prettier.prettier.format(params.text, options);
167        sendResponse({ id, result: { text: formattedText } });
168    } else if (method === 'prettier/clear_cache') {
169        prettier.prettier.clearConfigCache();
170        prettier.config = await prettier.prettier.resolveConfig(prettier.path) || {};
171        sendResponse({ id, result: null });
172    } else if (method === 'initialize') {
173        sendResponse({
174            id,
175            result: {
176                "capabilities": {}
177            }
178        });
179    } else {
180        throw new Error(`Unknown method: ${method}`);
181    }
182}
183
184function makeError(message) {
185    return {
186        error: {
187            "code": -32600, // invalid request code
188            message,
189        }
190    };
191}
192
193function sendResponse(response) {
194    let responsePayloadString = JSON.stringify({
195        jsonrpc: "2.0",
196        ...response
197    });
198    let headers = `${contentLengthHeaderName}: ${Buffer.byteLength(responsePayloadString)}${headerSeparator}${headerSeparator}`;
199    let dataToSend = headers + responsePayloadString;
200    process.stdout.write(dataToSend);
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}