-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: Exp support for macOS using a standalone WS server
- Loading branch information
Showing
15 changed files
with
424 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Error thrown when a platform is not supported. | ||
*/ | ||
export class PlatformError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "PlatformError"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { PlatformError } from "./PlatformError"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { fork } from "child_process"; | ||
import net from "net"; | ||
|
||
import { WebSocketServer } from "ws"; | ||
|
||
import { wrapNodeChildProcess } from "./utils/server"; | ||
|
||
import type { ChildProcessWithoutNullStreams } from "child_process"; | ||
|
||
if (!process.argv[2] || !process.argv[3]) { | ||
console.log("Usage: node mac-server.cjs <port> <lsp-node-module-path>"); | ||
process.exit(1); | ||
} | ||
|
||
const port = Number.parseInt(process.argv[2]); | ||
if (Number.isNaN(port)) { | ||
console.log(`Invalid port "${process.argv[2]}"`); | ||
process.exit(1); | ||
} | ||
|
||
console.log("Process PID:", process.pid); | ||
|
||
const server = wrapNodeChildProcess( | ||
fork(process.argv[3], [], { silent: true }) as ChildProcessWithoutNullStreams, | ||
); | ||
console.log("Copilot LSP server started. PID:", server.pid); | ||
|
||
const startWebSocketServer = () => { | ||
const wss = new WebSocketServer({ port: port }); | ||
|
||
wss.on("connection", (ws) => { | ||
console.log(`➕➕ Connection (${wss.clients.size})`); | ||
|
||
ws.once("close", () => { | ||
console.log("🚨 WebSocket Server shutting down..."); | ||
wss.close(); | ||
process.exit(0); | ||
}); | ||
|
||
ws.on("message", (data) => { | ||
// eslint-disable-next-line @typescript-eslint/no-base-to-string | ||
const payload = data.toString("utf-8"); | ||
console.debug("📥", payload); | ||
server.send(payload); | ||
}); | ||
|
||
server.onMessage((message) => { | ||
console.debug("📤", message); | ||
ws.send(message); | ||
}); | ||
}); | ||
|
||
console.log(`✅ WebSocket Server listening on ws://localhost:${port}`); | ||
|
||
const cleanupServer = (() => { | ||
let called = false; | ||
return () => { | ||
if (called) return; | ||
called = true; | ||
console.log("🚨 WebSocket Server shutting down..."); | ||
wss.close((err) => { | ||
if (err) console.error(err); | ||
process.exit(0); | ||
}); | ||
}; | ||
})(); | ||
|
||
process.on("exit", cleanupServer); | ||
process.on("SIGINT", cleanupServer); | ||
process.on("SIGTERM", cleanupServer); | ||
process.on("SIGUSR1", cleanupServer); | ||
process.on("SIGUSR2", cleanupServer); | ||
process.on("uncaughtException", cleanupServer); | ||
}; | ||
|
||
const testServer = net.createServer(); | ||
testServer.once("error", (err) => { | ||
if ((err as unknown as { code: string }).code === "EADDRINUSE") { | ||
console.error(`🚨 Port ${port} is busy`); | ||
process.exit(1); | ||
} | ||
}); | ||
|
||
testServer.once("listening", () => { | ||
testServer.close(startWebSocketServer); | ||
}); | ||
|
||
testServer.listen(port); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.