Skip to content

Commit

Permalink
wip: json RPC message parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes committed Jan 5, 2023
1 parent c580850 commit a39fa93
Show file tree
Hide file tree
Showing 5 changed files with 397 additions and 127 deletions.
5 changes: 1 addition & 4 deletions src/rpc/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@ class ErrorRpcParse<T> extends ErrorRpc<T> {
exitCode = sysexits.SOFTWARE;
}

export {
ErrorRpc,
ErrorRpcParse,
}
export { ErrorRpc, ErrorRpcParse };
71 changes: 42 additions & 29 deletions src/rpc/types.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,67 @@
import {POJO} from "../types";
import type { POJO } from '../types';

/**
* This is the JSON RPC request object. this is the generic message type used for the RPC.
*/
type JsonRpcRequest<T extends POJO> = {
type: 'JsonRpcRequest';
// A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0"
jsonrpc: '2.0',
jsonrpc: '2.0';
// A String containing the name of the method to be invoked. Method names that begin with the word rpc followed by a
// period character (U+002E or ASCII 46) are reserved for rpc-internal methods and extensions and MUST NOT be used
// for anything else.
method: string,
method: string;
// A Structured value that holds the parameter values to be used during the invocation of the method.
// This member MAY be omitted.
params?: T,
params?: T;
// An identifier established by the Client that MUST contain a String, Number, or NULL value if included.
// If it is not included it is assumed to be a notification. The value SHOULD normally not be Null [1] and Numbers
// SHOULD NOT contain fractional parts [2]
id: string | number | null,
id: string | number | null;
};

type JsonRpcNotification<T extends POJO> = {
type: 'JsonRpcNotification';
// A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0"
jsonrpc: '2.0',
jsonrpc: '2.0';
// A String containing the name of the method to be invoked. Method names that begin with the word rpc followed by a
// period character (U+002E or ASCII 46) are reserved for rpc-internal methods and extensions and MUST NOT be used
// for anything else.
method: string,
method: string;
// A Structured value that holds the parameter values to be used during the invocation of the method.
// This member MAY be omitted.
params?: T,
}
params?: T;
};

type JsonRpcReponseResult<T extends POJO> = {
type JsonRpcResponseResult<T extends POJO> = {
type: 'JsonRpcResponseResult';
// A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
jsonrpc: '2.0',
jsonrpc: '2.0';
// This member is REQUIRED on success.
// This member MUST NOT exist if there was an error invoking the method.
// The value of this member is determined by the method invoked on the Server.
result: T,
result: T;
// This member is REQUIRED.
// It MUST be the same as the value of the id member in the Request Object.
// If there was an error in detecting the id in the Request object (e.g. Parse error/Invalid Request),
// it MUST be Null.
id: string | number | null,
}
id: string | number | null;
};

type JsonRpcReponseError<T extends POJO> = {
type JsonRpcResponseError<T extends POJO> = {
type: 'JsonRpcResponseError';
// A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".
jsonrpc: '2.0',
jsonrpc: '2.0';
// This member is REQUIRED on error.
// This member MUST NOT exist if there was no error triggered during invocation.
// The value for this member MUST be an Object as defined in section 5.1.
error: JsonRpcError<T>,
error: JsonRpcError<T>;
// This member is REQUIRED.
// It MUST be the same as the value of the id member in the Request Object.
// If there was an error in detecting the id in the Request object (e.g. Parse error/Invalid Request),
// it MUST be Null.
id: string | number | null,
}
id: string | number | null;
};

// The error codes from and including -32768 to -32000 are reserved for pre-defined errors. Any code within this range,
// but not defined explicitly below is reserved for future use. The error codes are nearly the same as those suggested
Expand All @@ -71,26 +75,35 @@ type JsonRpcReponseError<T extends POJO> = {
// -32603 Internal error Internal JSON-RPC error.
// -32000 to -32099

type JsonRpcError<T extends POJO = {}> = {
type JsonRpcError<T extends POJO> = {
// A Number that indicates the error type that occurred.
// This MUST be an integer.
code: number,
code: number;
// A String providing a short description of the error.
// The message SHOULD be limited to a concise single sentence.
message: string,
message: string;
// A Primitive or Structured value that contains additional information about the error.
// This may be omitted.
// The value of this member is defined by the Server (e.g. detailed error information, nested errors etc.).
data?: T,
}
data?: T;
};

type JsonRpcResponse<T extends POJO, K extends POJO> =
| JsonRpcResponseResult<T>
| JsonRpcResponseError<K>;

type JsonRpcResponse<T extends POJO, K extends POJO> = JsonRpcReponseResult<T> | JsonRpcReponseError<K>;
type jsonRpcMessage<T extends POJO> =
| JsonRpcRequest<T>
| JsonRpcNotification<T>
| JsonRpcResponseResult<T>
| JsonRpcResponseError<T>;

export {
export type {
JsonRpcRequest,
JsonRpcNotification,
JsonRpcReponseResult,
JsonRpcReponseError,
JsonRpcResponseResult,
JsonRpcResponseError,
JsonRpcError,
JsonRpcResponse,
}
jsonRpcMessage,
};
Loading

0 comments on commit a39fa93

Please sign in to comment.