|
| 1 | +import type { IncomingMessage, Server } from 'node:http'; |
| 2 | +import type { Socket } from 'node:net'; |
| 3 | +import type { IWebSocket } from 'vscode-ws-jsonrpc'; |
| 4 | +import { WebSocketMessageReader, WebSocketMessageWriter } from 'vscode-ws-jsonrpc'; |
| 5 | +import { createConnection, createServerProcess, forward } from 'vscode-ws-jsonrpc/server'; |
| 6 | +import { WebSocketServer } from 'ws'; |
| 7 | + |
| 8 | +export interface BridgeOptions { |
| 9 | + /** Absolute path to the built CLI entry (`dist/cli.js`) to spawn. */ |
| 10 | + readonly cliEntry: string; |
| 11 | + /** WebSocket path the client connects to (e.g. `/psl`). */ |
| 12 | + readonly path: string; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Attaches an LSP WebSocket bridge to an existing HTTP server. |
| 17 | + * |
| 18 | + * The bridge does NOT own a port: it registers an `upgrade` listener on the |
| 19 | + * shared server (the same one Vite serves the editor from) and only claims |
| 20 | + * WebSocket upgrades on {@link BridgeOptions.path}, leaving Vite's own HMR |
| 21 | + * WebSocket and all HTTP requests untouched. On each accepted connection it |
| 22 | + * spawns `node <cliEntry> lsp --stdio` and forwards LSP |
| 23 | + * JSON-RPC traffic between the browser editor and that stdio process. |
| 24 | + * |
| 25 | + * Adapted from the canonical `vscode-ws-jsonrpc` example |
| 26 | + * (TypeFox/monaco-languageclient, MIT): `createServerProcess` spawns the stdio |
| 27 | + * LSP and `forward` pipes the socket <-> process connections. The server does |
| 28 | + * not depend on the client's `processId`, so the example's `initialize` |
| 29 | + * `processId` rewrite is intentionally omitted. |
| 30 | + * |
| 31 | + * Returns a disposer that detaches the bridge. |
| 32 | + */ |
| 33 | +export function attachBridge(server: Server, options: BridgeOptions): () => void { |
| 34 | + const wss = new WebSocketServer({ noServer: true }); |
| 35 | + |
| 36 | + const onUpgrade = (request: IncomingMessage, socket: Socket, head: Buffer): void => { |
| 37 | + const baseUrl = `http://${request.headers.host ?? 'localhost'}/`; |
| 38 | + const pathname = request.url !== undefined ? new URL(request.url, baseUrl).pathname : undefined; |
| 39 | + // Only claim our own path; ignore everything else (e.g. Vite's HMR socket) |
| 40 | + // so other upgrade listeners on the shared server still get a chance. |
| 41 | + if (pathname !== options.path) { |
| 42 | + return; |
| 43 | + } |
| 44 | + wss.handleUpgrade(request, socket, head, (webSocket) => { |
| 45 | + const ws: IWebSocket = { |
| 46 | + send: (content) => |
| 47 | + webSocket.send(content, (error) => { |
| 48 | + // A transient send error (peer gone, socket closing) must not crash |
| 49 | + // the playground process; close the socket and let the LSP |
| 50 | + // connection tear down through the normal close path. |
| 51 | + if (error !== null && error !== undefined) { |
| 52 | + webSocket.close(); |
| 53 | + } |
| 54 | + }), |
| 55 | + onMessage: (cb) => webSocket.on('message', (data) => cb(data)), |
| 56 | + onError: (cb) => webSocket.on('error', cb), |
| 57 | + onClose: (cb) => webSocket.on('close', cb), |
| 58 | + dispose: () => webSocket.close(), |
| 59 | + }; |
| 60 | + if (webSocket.readyState === webSocket.OPEN) { |
| 61 | + launch(ws, options); |
| 62 | + } else { |
| 63 | + webSocket.on('open', () => launch(ws, options)); |
| 64 | + } |
| 65 | + }); |
| 66 | + }; |
| 67 | + |
| 68 | + server.on('upgrade', onUpgrade); |
| 69 | + return () => { |
| 70 | + server.off('upgrade', onUpgrade); |
| 71 | + wss.close(); |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +function launch(socket: IWebSocket, options: BridgeOptions): void { |
| 76 | + const reader = new WebSocketMessageReader(socket); |
| 77 | + const writer = new WebSocketMessageWriter(socket); |
| 78 | + const socketConnection = createConnection(reader, writer, () => socket.dispose()); |
| 79 | + const serverConnection = createServerProcess('PSL', 'node', [options.cliEntry, 'lsp', '--stdio']); |
| 80 | + if (serverConnection === undefined) { |
| 81 | + // The LSP subprocess could not be spawned. Don't leave the client hanging |
| 82 | + // on a backend-less socket: report it and close the connection. |
| 83 | + console.error( |
| 84 | + `Failed to spawn the language server (node ${options.cliEntry} lsp --stdio). Is @prisma-next/cli built?`, |
| 85 | + ); |
| 86 | + socket.dispose(); |
| 87 | + return; |
| 88 | + } |
| 89 | + forward(socketConnection, serverConnection); |
| 90 | +} |
0 commit comments