( codeServerSocketPath: string, editorSessionManager: EditorSessionManager, )
| 33 | } |
| 34 | |
| 35 | export async function makeEditorSessionManagerServer( |
| 36 | codeServerSocketPath: string, |
| 37 | editorSessionManager: EditorSessionManager, |
| 38 | ): Promise<http.Server> { |
| 39 | const router = express() |
| 40 | |
| 41 | router.use(express.json()) |
| 42 | |
| 43 | router.get<{}, GetSessionResponse | string | unknown, undefined, { filePath?: string }>( |
| 44 | "/session", |
| 45 | async (req, res) => { |
| 46 | const filePath = req.query.filePath |
| 47 | if (!filePath) { |
| 48 | throw new HttpError("filePath is required", HttpCode.BadRequest) |
| 49 | } |
| 50 | const socketPath = await editorSessionManager.getConnectedSocketPath(filePath) |
| 51 | const response: GetSessionResponse = { socketPath } |
| 52 | res.json(response) |
| 53 | }, |
| 54 | ) |
| 55 | |
| 56 | router.post<{}, string, AddSessionRequest | undefined>("/add-session", async (req, res) => { |
| 57 | const entry = req.body?.entry |
| 58 | if (!entry) { |
| 59 | throw new HttpError("entry is required", HttpCode.BadRequest) |
| 60 | } |
| 61 | editorSessionManager.addSession(entry) |
| 62 | res.status(200).send("session added") |
| 63 | }) |
| 64 | |
| 65 | router.post<{}, string, DeleteSessionRequest | undefined>("/delete-session", async (req, res) => { |
| 66 | const socketPath = req.body?.socketPath |
| 67 | if (!socketPath) { |
| 68 | throw new HttpError("socketPath is required", HttpCode.BadRequest) |
| 69 | } |
| 70 | editorSessionManager.deleteSession(socketPath) |
| 71 | res.status(200).send("session deleted") |
| 72 | }) |
| 73 | |
| 74 | router.use(errorHandler) |
| 75 | |
| 76 | const server = http.createServer(router) |
| 77 | try { |
| 78 | await listen(server, { socket: codeServerSocketPath }) |
| 79 | } catch (e) { |
| 80 | logger.warn(`Could not create socket at ${codeServerSocketPath}`) |
| 81 | } |
| 82 | return server |
| 83 | } |
| 84 | |
| 85 | export class EditorSessionManager { |
| 86 | // Map from socket path to EditorSessionEntry. |
no test coverage detected