Files
opencode-manager/backend/src/ipc/ipcServer.ts
T
Chris Scott 5de419ee28 fix issues (#136)
* fix: prevent delete button click when card is not swiped open

* refactor: improve CreateRepoInput type safety

* security: require AUTH_SECRET and use secure random generation

* fix: SSH key path validation, async passphrase stripping, and port handling

* fix: use correct console methods for logger levels

* Add error handling to DB update functions and improve migration logging

* Add error handling for session lookup in auth middleware

* Add structured git error handling with user-friendly messages

- Parse git errors with specific error codes (AUTH_FAILED, REPO_NOT_FOUND,
  PUSH_REJECTED, MERGE_CONFLICT, etc.) and appropriate HTTP status codes
- Return structured error responses with summary and detail from backend
- Add GitErrorBanner component for better error display in UI
- Add optional onError callback to useGit hook for external error handling

* Make IPC server initialization/disposal async and fix SSH host key handler

* add try-catch error handling in frontend lib utilities

* remove unresolved promise handlers in speech synthesizer callbacks

* Extract error types to shared package

* Import GitErrorCode from shared package

* Refactor API layer to use shared FetchError and fetchWrapper

* Update prompt parser and source control components

* Extract MCP server list to separate component with improved badge menu UX

* merge useSettingsDialogUrl into useSettingsDialog and extract getRepoDisplayName utility

* add separate loading state for passkey sign in

* refactor hooks: use fetchWrapper, improve error handling, add proper types

* replace browser confirm() with DeleteDialog component in settings

* fix: add missing variables parameter in React Query onError callbacks

* Refactor message parts to use separate Zustand store

* Update QuestionPrompt styling for light/dark mode
2026-02-15 19:48:03 -05:00

115 lines
3.3 KiB
TypeScript

import * as http from 'http'
import * as crypto from 'crypto'
import * as path from 'path'
import * as os from 'os'
import * as fs from 'fs/promises'
import { logger } from '../utils/logger'
function getIPCHandlePath(id: string): string {
if (process.platform === 'win32') {
return `\\\\.\\pipe\\opencode-git-${id}-sock`
}
if (process.platform !== 'darwin' && process.env['XDG_RUNTIME_DIR']) {
return path.join(process.env['XDG_RUNTIME_DIR'], `opencode-git-${id}.sock`)
}
return path.join(os.tmpdir(), `opencode-git-${id}.sock`)
}
export interface IPCHandler {
handle(request: unknown): Promise<unknown>
}
export class IPCServer {
private handlers = new Map<string, IPCHandler>()
constructor(
private server: http.Server,
public readonly ipcHandlePath: string
) {
server.on('request', (req, res) => this.onRequest(req, res))
}
registerHandler(name: string, handler: IPCHandler): void {
this.handlers.set(`/${name}`, handler)
}
getHandler(name: string): IPCHandler | undefined {
return this.handlers.get(`/${name}`)
}
getEnv(): Record<string, string> {
return { VSCODE_GIT_IPC_HANDLE: this.ipcHandlePath }
}
private onRequest(req: http.IncomingMessage, res: http.ServerResponse): void {
logger.info(`IPC request received: url=${req.url}, method=${req.method}`)
const handler = this.handlers.get(req.url || '')
if (!handler) {
logger.warn(`IPC handler not found for path: ${req.url}, registered paths: ${Array.from(this.handlers.keys()).join(', ')}`)
res.writeHead(404)
res.end(JSON.stringify({ error: 'Handler not found' }))
return
}
const chunks: Buffer[] = []
req.on('data', (d: Buffer) => chunks.push(d))
req.on('end', async () => {
try {
const body = Buffer.concat(chunks).toString('utf8')
logger.info(`IPC request body: "${body}"`)
if (!body) {
logger.warn(`Empty request body for path: ${req.url}`)
res.writeHead(400)
res.end(JSON.stringify({ error: 'Empty request body' }))
return
}
const request = JSON.parse(body)
logger.info(`IPC calling handler for ${req.url} with:`, request)
const result = await handler.handle(request)
logger.info(`IPC handler result for ${req.url}:`, result)
res.writeHead(200)
res.end(JSON.stringify(result))
} catch (error) {
logger.error(`IPC request error for path ${req.url}:`, error)
res.writeHead(500)
res.end(JSON.stringify({ error: 'Internal server error' }))
}
})
}
dispose(): Promise<void> {
return new Promise((resolve) => {
this.server.close(() => resolve())
})
}
}
export async function createIPCServer(context?: string): Promise<IPCServer> {
const server = http.createServer()
const hash = crypto.createHash('sha256')
if (!context) {
const buffer = crypto.randomBytes(20)
hash.update(buffer)
} else {
hash.update(context)
}
const ipcHandlePath = getIPCHandlePath(hash.digest('hex').substring(0, 10))
if (process.platform !== 'win32') {
try {
await fs.unlink(ipcHandlePath)
} catch {
/* socket file may not exist */
}
}
return new Promise((resolve, reject) => {
server.on('error', reject)
server.listen(ipcHandlePath, () => {
resolve(new IPCServer(server, ipcHandlePath))
})
})
}