All files / cli/serve JsonRpcHandler.ts

100% Statements 31/31
100% Branches 25/25
100% Functions 3/3
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155                3x 3x 3x 3x 3x                               3x   3x   3x                   45x 45x   2x             43x 2x           41x     41x 2x             39x 39x 1x             38x 38x 1x             37x 37x       1x           36x                                       25x                                       25x       25x 1x   25x                
/**
 * JSON-RPC protocol utilities for the serve command
 */
 
import IJsonRpcRequest from "./types/IJsonRpcRequest";
import IJsonRpcResponse from "./types/IJsonRpcResponse";
 
/** Standard JSON-RPC error codes */
const ERROR_PARSE = -32700;
const ERROR_INVALID_REQUEST = -32600;
const ERROR_METHOD_NOT_FOUND = -32601;
const ERROR_INVALID_PARAMS = -32602;
const ERROR_INTERNAL = -32603;
 
/**
 * Result of parsing a JSON-RPC request
 */
interface IParseResult {
  success: boolean;
  request?: IJsonRpcRequest;
  error?: IJsonRpcResponse;
}
 
/**
 * JSON-RPC protocol handler
 */
class JsonRpcHandler {
  /** Method not found code */
  static readonly ERROR_METHOD_NOT_FOUND = ERROR_METHOD_NOT_FOUND;
  /** Invalid params code */
  static readonly ERROR_INVALID_PARAMS = ERROR_INVALID_PARAMS;
  /** Internal error code */
  static readonly ERROR_INTERNAL = ERROR_INTERNAL;
 
  /**
   * Parse a JSON-RPC request from a line of input
   * @param line - Raw input line
   * @returns Parse result with request or error response
   */
  static parseRequest(line: string): IParseResult {
    // Try to parse JSON
    let parsed: unknown;
    try {
      parsed = JSON.parse(line);
    } catch {
      return {
        success: false,
        error: this.formatError(null, ERROR_PARSE, "Parse error"),
      };
    }
 
    // Validate structure
    if (typeof parsed !== "object" || parsed === null) {
      return {
        success: false,
        error: this.formatError(null, ERROR_INVALID_REQUEST, "Invalid request"),
      };
    }
 
    const obj = parsed as Record<string, unknown>;
 
    // Check for required fields
    if (!("id" in obj) || !("method" in obj)) {
      return {
        success: false,
        error: this.formatError(null, ERROR_INVALID_REQUEST, "Invalid request"),
      };
    }
 
    // Validate id type
    const id = obj.id;
    if (typeof id !== "number" && typeof id !== "string") {
      return {
        success: false,
        error: this.formatError(null, ERROR_INVALID_REQUEST, "Invalid request"),
      };
    }
 
    // Validate method type
    const method = obj.method;
    if (typeof method !== "string") {
      return {
        success: false,
        error: this.formatError(id, ERROR_INVALID_REQUEST, "Invalid request"),
      };
    }
 
    // Validate params if present
    const params = obj.params;
    if (
      params !== undefined &&
      (typeof params !== "object" || params === null)
    ) {
      return {
        success: false,
        error: this.formatError(id, ERROR_INVALID_PARAMS, "Invalid params"),
      };
    }
 
    return {
      success: true,
      request: {
        id,
        method,
        params: params as Record<string, unknown> | undefined,
      },
    };
  }
 
  /**
   * Format a success response
   * @param id - Request identifier
   * @param result - Result value
   * @returns JSON-RPC response object
   */
  static formatResponse(
    id: number | string | null,
    result: unknown,
  ): IJsonRpcResponse {
    return {
      id: id ?? 0,
      result,
    };
  }
 
  /**
   * Format an error response
   * @param id - Request identifier (null if unknown)
   * @param code - Error code
   * @param message - Error message
   * @param data - Optional additional error context
   * @returns JSON-RPC response object
   */
  static formatError(
    id: number | string | null,
    code: number,
    message: string,
    data?: unknown,
  ): IJsonRpcResponse {
    const error: { code: number; message: string; data?: unknown } = {
      code,
      message,
    };
    if (data !== undefined) {
      error.data = data;
    }
    return {
      id: id ?? 0,
      error,
    };
  }
}
 
export default JsonRpcHandler;