delphirtl

delphirtlDocs


delphirtl / BaseJsonRpcServer

Class: BaseJsonRpcServer

This is a JSON RPC server that conforms to the JSON RPC 2.0 spec as documented at https://www.jsonrpc.org/specification

BaseJsonRpcServer

Constructors

new BaseJsonRpcServer()

new BaseJsonRpcServer(host?, port?): BaseJsonRpcServer

Constructs a BaseJsonRpcServer server and returns it. Listens on localhost:8080 by default, on both IPv4 and IPv6 To listen only on IPv4, override onBeforeListening and call dns.setDefaultResultOrder(‘ipv4first’); To listen only on IPv6, if both IPv4 and IPv6 is enabled, just pass “localhost” to the host parameter

Parameters

host?: string = "::"

The IP address/hostname to listen on

port?: number = CDefaultPort

The port number to listen on

Returns

BaseJsonRpcServer

BaseJsonRpcServer

Defined in

src/BaseJsonRpcServer.ts:129

Properties

mExpressServer

protected mExpressServer: Express

Defined in

src/BaseJsonRpcServer.ts:102


mHttpTerminator

protected mHttpTerminator: undefined | HttpTerminator

Defined in

src/BaseJsonRpcServer.ts:104


mJsonRpcServer

protected mJsonRpcServer: JSONRPCServer<void>

Defined in

src/BaseJsonRpcServer.ts:103


mListeningHost

protected mListeningHost: string

Defined in

src/BaseJsonRpcServer.ts:105


mListeningPort

protected mListeningPort: number

Defined in

src/BaseJsonRpcServer.ts:106


mRequest

protected mRequest: undefined | Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

Only valid when processing a request

Defined in

src/BaseJsonRpcServer.ts:117


mResponse

protected mResponse: undefined | Response<any, Record<string, any>>

Only valid when processing a request

Defined in

src/BaseJsonRpcServer.ts:112


mServer

protected mServer: undefined | Server

Defined in

src/BaseJsonRpcServer.ts:107

Accessors

listeningPath

Get Signature

get listeningPath(): string

Returns the path on which to listen to requests for. Override to listen on another path.

Returns

string

path on which to listen to requests for

Defined in

src/BaseJsonRpcServer.ts:389

Methods

addRPCMethods()

protected addRPCMethods(): void

Adds all necessary JSON RPC methods to this.mJsonRpcServer

Returns

void

Defined in

src/BaseJsonRpcServer.ts:140


afterConstruction()

protected afterConstruction(): void

Allows descendants to do something after class construction

Returns

void

void

Defined in

src/BaseJsonRpcServer.ts:158


createInvalidParams()

protected createInvalidParams(data?): JSONRPCErrorException

Creates an Invalid Params JSON RPC Error exception, with the message as Invalid Params. The caller must throw the return of this call.

Parameters

data?: any

Returns

JSONRPCErrorException

Invalid Params JSON RPC Error exception

Defined in

src/BaseJsonRpcServer.ts:178


createInvalidParamsMessage()

protected createInvalidParamsMessage(message, data?): JSONRPCErrorException

Creates an Invalid Params JSON RPC Error exception, with the given message. The caller must throw the return of this call.

Parameters

message: string

data?: any

Returns

JSONRPCErrorException

Invalid Params JSON RPC Error exception

Defined in

src/BaseJsonRpcServer.ts:167


createInvalidRequest()

protected createInvalidRequest(data?): JSONRPCErrorException

Creates an Invalid Request JSON RPC Error exception, with the message as Invalid Params. The caller must throw the return of this call.

Parameters

data?: any

Returns

JSONRPCErrorException

Invalid Request JSON RPC Error exception

Defined in

src/BaseJsonRpcServer.ts:216


createInvalidRequestResponse()

protected createInvalidRequestResponse(request): JSONRPCErrorResponse

Creates an Invalid Request JSON RPC Error response.

Parameters

request: any

Returns

JSONRPCErrorResponse

Invalid Params JSON RPC response

Defined in

src/BaseJsonRpcServer.ts:188


createMethodNotFoundResponse()

protected createMethodNotFoundResponse(request): JSONRPCErrorException

Creates a Method not found JSON RPC Error response, with the message as Invalid Params. The caller must throw the return of this call.

Parameters

request: any

Returns

JSONRPCErrorException

Invalid Request JSON RPC Error exception

Defined in

src/BaseJsonRpcServer.ts:203


doParseError()

protected doParseError(err, req, res, next): void

Calls this.onParseError If onParseError doesn’t handle the error, doParseError will send a JSON RPC Error response if the error is a parsing error, or pass the error up to the next middleware to handle

Parameters

err: any

req: Request

res: Response

next: NextFunction

Returns

void

Invalid Request JSON RPC Error exception

Defined in

src/BaseJsonRpcServer.ts:229


echo()

echo(params): any

Parameters

params: any

Returns

any

Defined in

src/BaseJsonRpcServer.ts:499


handleRequest()

protected handleRequest(request, response): Promise<void>

Handles a single or a batch JSON RPC request.

Parameters

request: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

response: Response<any, Record<string, any>>

Returns

Promise<void>

Defined in

src/BaseJsonRpcServer.ts:258


implementedRPCMethods()

protected implementedRPCMethods(): SimpleJSONRPCMethod<void>[]

Returns RPC methods implemented by the server Override to return an array of RPC methods implemented.

Returns

SimpleJSONRPCMethod<void>[]

array of RPC methods implemented

Defined in

src/BaseJsonRpcServer.ts:297


initExpress()

protected initExpress(): Express

Initializes the Express server and returns it

Returns

Express

Defined in

src/BaseJsonRpcServer.ts:306


initJsonRpcServer()

protected initJsonRpcServer(): JSONRPCServer<void>

Initializes the JSON RPC server and returns it

Returns

JSONRPCServer<void>

Defined in

src/BaseJsonRpcServer.ts:333


initParseErrorHandler()

protected initParseErrorHandler(aExpress): void

Returns a JSON parse error during express.json parsing

Parameters

aExpress: Express

Returns

void

Defined in

src/BaseJsonRpcServer.ts:348


JsonRpcErrorListener()

protected JsonRpcErrorListener(message, data): void

Error listener for JSON RPC, override to handle. Called when an error occurred during the JSON RPC method call. Do not throw an exception within this method

Parameters

message: string

data: unknown

Returns

void

Defined in

src/BaseJsonRpcServer.ts:362


listen()

listen(port?): Promise<void>

Starts listening for RPC requests

Parameters

port?: number

Returns

Promise<void>

Defined in

src/BaseJsonRpcServer.ts:368


log()

protected log(…data): void

Parameters

• …data: any[]

Returns

void

Defined in

src/BaseJsonRpcServer.ts:393


logListeningMethods()

protected logListeningMethods(methods): void

Displays the JSON RPC methods being listened to

Parameters

methods: SimpleJSONRPCMethod<void>[]

Returns

void

Defined in

src/BaseJsonRpcServer.ts:401


onBeforeDispatchRequest()

protected onBeforeDispatchRequest(next, request, serverParams): JSONRPCResponsePromise

Called before every valid JSON body is dispatched, this doesn’t mean that the request is a valid JSON RPC call though. If the request is valid, the method should call next(request, serverParams) and return its result. If the request is invalid, it should throw an appropriate error.

Parameters

next: JSONRPCServerMiddlewareNext<void>

request: JSONRPCRequest

serverParams: undefined | void

Returns

JSONRPCResponsePromise

Defined in

src/BaseJsonRpcServer.ts:415


onBeforeListening()

protected onBeforeListening(): void

This is called before the JSON RPC server starts listening on the port Override this, for example, to listen just on IPv4 by calling dns.setDefaultResultOrder(‘ipv4first’);

Returns

void

Defined in

src/BaseJsonRpcServer.ts:430


onParseError()

protected onParseError(err, request, response): void

Called when there’s a parsing error. The overridden method should create a JSON RPC Error response and send it using response.send(err_response) If the error is not handled, this class will return an error

Parameters

err: any

request: Request

response: TSendHandler

Returns

void

Defined in

src/BaseJsonRpcServer.ts:440


sendInvalidParams()

protected sendInvalidParams(data?): void

Creates an Invalid Params JSON RPC Error exception and sends it.

Parameters

data?: any

Returns

void

Defined in

src/BaseJsonRpcServer.ts:458


sendInvalidRequest()

protected sendInvalidRequest(data?): void

Creates an Invalid Request JSON RPC Error exception and sends it.

Parameters

data?: any

Returns

void

Defined in

src/BaseJsonRpcServer.ts:448


stop()

stop(): Promise<void>

Stops the RPC server. This can be called by a RPC call or by the app itself

Returns

Promise<void>

Defined in

src/BaseJsonRpcServer.ts:466


stopListening()

protected stopListening(): Promise<void>

Returns

Promise<void>

Defined in

src/BaseJsonRpcServer.ts:471


waitForServerListening()

protected waitForServerListening(): Promise<void>

Waits for the server to starts listening before returning. Override to disable this

Returns

Promise<void>

Defined in

src/BaseJsonRpcServer.ts:481


waitForServerStopListening()

protected waitForServerStopListening(): Promise<void>

Returns

Promise<void>

Defined in

src/BaseJsonRpcServer.ts:489