feat(daemon): Reorganize and refactor core into client/daemon/shared modules; add IPC protocol and tests
This commit is contained in:
26
ts/shared/protocol/error.codes.ts
Normal file
26
ts/shared/protocol/error.codes.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Standardized error codes for IPC communication
|
||||
* These are used instead of string messages for better error handling
|
||||
*/
|
||||
export enum ErrorCode {
|
||||
// General errors
|
||||
UNKNOWN_ERROR = 'UNKNOWN_ERROR',
|
||||
INVALID_REQUEST = 'INVALID_REQUEST',
|
||||
|
||||
// Process errors
|
||||
PROCESS_NOT_FOUND = 'PROCESS_NOT_FOUND',
|
||||
PROCESS_ALREADY_EXISTS = 'PROCESS_ALREADY_EXISTS',
|
||||
PROCESS_START_FAILED = 'PROCESS_START_FAILED',
|
||||
PROCESS_STOP_FAILED = 'PROCESS_STOP_FAILED',
|
||||
|
||||
// Daemon errors
|
||||
DAEMON_NOT_RUNNING = 'DAEMON_NOT_RUNNING',
|
||||
DAEMON_ALREADY_RUNNING = 'DAEMON_ALREADY_RUNNING',
|
||||
|
||||
// Memory errors
|
||||
MEMORY_LIMIT_EXCEEDED = 'MEMORY_LIMIT_EXCEEDED',
|
||||
|
||||
// Config errors
|
||||
CONFIG_INVALID = 'CONFIG_INVALID',
|
||||
CONFIG_SAVE_FAILED = 'CONFIG_SAVE_FAILED',
|
||||
}
|
198
ts/shared/protocol/ipc.types.ts
Normal file
198
ts/shared/protocol/ipc.types.ts
Normal file
@@ -0,0 +1,198 @@
|
||||
import type { IProcessConfig, IProcessInfo } from './classes.tspm.js';
|
||||
import type { IProcessLog } from './classes.processwrapper.js';
|
||||
|
||||
// Base message types
|
||||
export interface IpcRequest<T = any> {
|
||||
id: string;
|
||||
method: string;
|
||||
params: T;
|
||||
}
|
||||
|
||||
export interface IpcResponse<T = any> {
|
||||
id: string;
|
||||
success: boolean;
|
||||
result?: T;
|
||||
error?: {
|
||||
code: number;
|
||||
message: string;
|
||||
data?: any;
|
||||
};
|
||||
}
|
||||
|
||||
// Request/Response pairs for each operation
|
||||
|
||||
// Start command
|
||||
export interface StartRequest {
|
||||
config: IProcessConfig;
|
||||
}
|
||||
|
||||
export interface StartResponse {
|
||||
processId: string;
|
||||
pid?: number;
|
||||
status: 'online' | 'stopped' | 'errored';
|
||||
}
|
||||
|
||||
// Stop command
|
||||
export interface StopRequest {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface StopResponse {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
// Restart command
|
||||
export interface RestartRequest {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface RestartResponse {
|
||||
processId: string;
|
||||
pid?: number;
|
||||
status: 'online' | 'stopped' | 'errored';
|
||||
}
|
||||
|
||||
// Delete command
|
||||
export interface DeleteRequest {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface DeleteResponse {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
// List command
|
||||
export interface ListRequest {
|
||||
// No parameters needed
|
||||
}
|
||||
|
||||
export interface ListResponse {
|
||||
processes: IProcessInfo[];
|
||||
}
|
||||
|
||||
// Describe command
|
||||
export interface DescribeRequest {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface DescribeResponse {
|
||||
processInfo: IProcessInfo;
|
||||
config: IProcessConfig;
|
||||
}
|
||||
|
||||
// Get logs command
|
||||
export interface GetLogsRequest {
|
||||
id: string;
|
||||
lines?: number;
|
||||
}
|
||||
|
||||
export interface GetLogsResponse {
|
||||
logs: IProcessLog[];
|
||||
}
|
||||
|
||||
// Start all command
|
||||
export interface StartAllRequest {
|
||||
// No parameters needed
|
||||
}
|
||||
|
||||
export interface StartAllResponse {
|
||||
started: string[];
|
||||
failed: Array<{
|
||||
id: string;
|
||||
error: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
// Stop all command
|
||||
export interface StopAllRequest {
|
||||
// No parameters needed
|
||||
}
|
||||
|
||||
export interface StopAllResponse {
|
||||
stopped: string[];
|
||||
failed: Array<{
|
||||
id: string;
|
||||
error: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
// Restart all command
|
||||
export interface RestartAllRequest {
|
||||
// No parameters needed
|
||||
}
|
||||
|
||||
export interface RestartAllResponse {
|
||||
restarted: string[];
|
||||
failed: Array<{
|
||||
id: string;
|
||||
error: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
// Daemon status command
|
||||
export interface DaemonStatusRequest {
|
||||
// No parameters needed
|
||||
}
|
||||
|
||||
export interface DaemonStatusResponse {
|
||||
status: 'running' | 'stopped';
|
||||
pid?: number;
|
||||
uptime?: number;
|
||||
processCount: number;
|
||||
memoryUsage?: number;
|
||||
cpuUsage?: number;
|
||||
}
|
||||
|
||||
// Daemon shutdown command
|
||||
export interface DaemonShutdownRequest {
|
||||
graceful?: boolean;
|
||||
timeout?: number; // milliseconds
|
||||
}
|
||||
|
||||
export interface DaemonShutdownResponse {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
// Heartbeat command
|
||||
export interface HeartbeatRequest {
|
||||
// No parameters needed
|
||||
}
|
||||
|
||||
export interface HeartbeatResponse {
|
||||
timestamp: number;
|
||||
status: 'healthy' | 'degraded';
|
||||
}
|
||||
|
||||
// Type mappings for methods
|
||||
export type IpcMethodMap = {
|
||||
start: { request: StartRequest; response: StartResponse };
|
||||
stop: { request: StopRequest; response: StopResponse };
|
||||
restart: { request: RestartRequest; response: RestartResponse };
|
||||
delete: { request: DeleteRequest; response: DeleteResponse };
|
||||
list: { request: ListRequest; response: ListResponse };
|
||||
describe: { request: DescribeRequest; response: DescribeResponse };
|
||||
getLogs: { request: GetLogsRequest; response: GetLogsResponse };
|
||||
startAll: { request: StartAllRequest; response: StartAllResponse };
|
||||
stopAll: { request: StopAllRequest; response: StopAllResponse };
|
||||
restartAll: { request: RestartAllRequest; response: RestartAllResponse };
|
||||
'daemon:status': {
|
||||
request: DaemonStatusRequest;
|
||||
response: DaemonStatusResponse;
|
||||
};
|
||||
'daemon:shutdown': {
|
||||
request: DaemonShutdownRequest;
|
||||
response: DaemonShutdownResponse;
|
||||
};
|
||||
heartbeat: { request: HeartbeatRequest; response: HeartbeatResponse };
|
||||
};
|
||||
|
||||
// Helper type to extract request type for a method
|
||||
export type RequestForMethod<M extends keyof IpcMethodMap> =
|
||||
IpcMethodMap[M]['request'];
|
||||
|
||||
// Helper type to extract response type for a method
|
||||
export type ResponseForMethod<M extends keyof IpcMethodMap> =
|
||||
IpcMethodMap[M]['response'];
|
5
ts/shared/protocol/protocol.version.ts
Normal file
5
ts/shared/protocol/protocol.version.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* Protocol version for client-daemon communication
|
||||
* This allows for version compatibility checks between client and daemon
|
||||
*/
|
||||
export const PROTOCOL_VERSION = '1.0.0';
|
Reference in New Issue
Block a user