All files Message.ts

100% Statements 54/54
100% Branches 26/26
100% Functions 13/13
100% Lines 54/54

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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 1941x                         49x   49x 45x         3x         10x             1x 1x                       22x 22x   22x 14x         8x                   11x 11x                   16x 16x                                                         2x           5x           1x           3x           31x 31x   2x     29x 3x     26x   10x 1x   9x     16x 5x 1x   4x 1x   3x           3x 1x   3x     11x 9x 1x   8x       2x           1x 1x 1x 1x 1x   1x 1x 1x 1x 1x    
import { JSONRPC_VERSION } from './Defines';
 
declare type RPCID = string | number | null | undefined;
declare type RPCParams = Array<any> | object;
 
// Base JSON-RPC message structure
class RPCMessage
{
    jsonrpc: string;
    id: RPCID;
 
    constructor(id: RPCID)
    {
        this.jsonrpc = JSONRPC_VERSION;
 
        if (id !== undefined)
            this.id = id;
    }
 
    isRequest(): boolean
    {
        return (this instanceof RPCRequest);
    }
 
    isResponse(): boolean
    {
        return (this instanceof RPCResponseError || this instanceof RPCResponseResult);
    }
}
 
// JSON-RPC error object
class RPCError
{
    code: number = 0;
    message: string = "";
    data?: any;
}
 
// JSON-RPC request object
class RPCRequest extends RPCMessage
{
    method: string;
    params?: RPCParams;
 
    constructor(id: RPCID, method: string, params?: RPCParams)
    {
        super(id);
        this.method = method;
 
        if (params !== undefined)
            this.params = params;
    }
 
    isNotification(): boolean
    {
        return (this.id === undefined || this.id === null);
    }
}
 
class RPCResponseError extends RPCMessage
{
    error: RPCError;
 
    constructor(id: RPCID, error: RPCError)
    {
        super(id);
        this.error = error;
    }
}
 
class RPCResponseResult extends RPCMessage
{
    result: any;
 
    constructor(id: RPCID, result: any)
    {
        super(id);
        this.result = result;
    }
}
 
type RPCResponse = RPCResponseError | RPCResponseResult;
 
// JSON-RPC response object
/*class RPCResponse extends RPCMessage
{
    result?: any;
    error?: RPCError;
 
    constructor(id: RPCID, result?: any, error?: RPCError)
    {
        super(id);
 
        if (result && error)
            throw new Error("Result and error can not coexist in RPCResponse");
        else if (result !== undefined)
            this.result = result;
        else if (error !== undefined)
            this.error = error;
        else
            throw new Error("Result or error must exist in RPCResponse");
    }
}*/
 
class JSONParseError extends Error {
    constructor() {
        super("JSON Parse Error");
    }
}
 
class InvalidMessageError extends Error {
    constructor(e: string) {
        super("Invalid Message: " + e);
    }
}
 
class InvalidRequestError extends Error {
    constructor(e: string) {
        super("Invalid Request: " + e);
    }
}
 
class InvalidResponseError extends Error {
    constructor(e: string) {
        super("Invalid Request: " + e);
    }
}
 
function ParseRPCMessage(msg: string): RPCRequest | RPCResponse
{
    try {
        var data = JSON.parse(msg);
    } catch(e) {
        throw new JSONParseError();
    }
 
    if (data.jsonrpc != JSONRPC_VERSION)
        throw new InvalidMessageError("wrong json rpc version");
 
    // Request object
    if (data.method) {
        // Method must be a string
        if (typeof data.method !== "string")
            throw new InvalidRequestError("method is not a string");
        
        return new RPCRequest(data.id, data.method, data.params);
    }
    // Response error object
    else if (data.error !== undefined) {
        if (typeof data.error.code !== "number")
            throw new InvalidResponseError("error code is not a number");
 
        if (typeof data.error.message !== "string")
            throw new InvalidResponseError("error message is not a string");
 
        let error: RPCError = {
            code: data.error.code,
            message: data.error.message,
        };
 
        // Data field is optional
        if (data.error.data)
            error.data = data.error.data;
 
        return new RPCResponseError(data.id, error);
    }
    // Response success object
    else if (data.result !== undefined) {
        if (data.id === undefined)
            throw new InvalidResponseError("id not found");
 
        return new RPCResponseResult(data.id, data.result);
    }
    // Invalid object
    else
        throw new InvalidMessageError("unknown message type");
}
 
export {
    RPCID,
    RPCParams,
    RPCMessage,
    RPCError,
    RPCResponseError,
    RPCResponseResult,
    RPCRequest,
    RPCResponse,
    JSONParseError,
    InvalidMessageError,
    InvalidRequestError,
    InvalidResponseError,
    ParseRPCMessage
};