delphirtl
Version:
RTL functions from Delphi
238 lines (237 loc) • 9 kB
TypeScript
import { createJSONRPCErrorResponse, isJSONRPCID, JSONRPC, JSONRPCClient, JSONRPCErrorCode, JSONRPCErrorException, JSONRPCErrorResponse, JSONRPCParams, JSONRPCRequest, JSONRPCResponsePromise, JSONRPCServer, JSONRPCServerMiddlewareNext, SimpleJSONRPCMethod } from "json-rpc-2.0";
import express, { Express, NextFunction } from 'express';
import * as http from 'http';
import { HttpTerminator } from 'http-terminator';
type Request = express.Request;
type Response = express.Response;
type JSONRPCParameters = JSONRPCParams | undefined;
export interface TSendHandler {
send: (body: string | number | boolean | object | Buffer) => void;
}
/**
* Invalid params
*
* @type {"Invalid params"}
* @category Constants
*/
declare const SInvalidParams = "Invalid params";
/**
* Invalid request
*
* @type {"Invalid Request"}
* @category Constants
*/
declare const SInvalidRequest = "Invalid Request";
/**
* Method not found
*
* @type {"Method not found"}
* @category Constants
*/
declare const SMethodNotFound = "Method not found";
/**
* Parse error
*
* @type {"Parse error"}
* @category Constants
*/
declare const SParseError = "Parse error";
/**
* Entity parse failed
*
* @type {"entity.parse.failed"}
* @category Constants
*/
declare const SEntityParseFailed = "entity.parse.failed";
/**
* No content
*
* @type {204}
* @category Constants
*/
declare const CNoContent = 204;
/**
* This is a JSON RPC server that conforms to the JSON RPC 2.0 spec as documented at https://www.jsonrpc.org/specification
*
* @class BaseJsonRpcServer
* @typedef {BaseJsonRpcServer}
* @category JSON RPC
*/
declare class BaseJsonRpcServer {
protected mExpressServer: Express;
protected mJsonRpcServer: JSONRPCServer;
protected mHttpTerminator: HttpTerminator | undefined;
protected mListeningHost: string;
protected mListeningPort: number;
protected mServer: http.Server | undefined;
/**
* Only valid when processing a request
*/
protected mResponse: express.Response | undefined;
/**
* Only valid when processing a request
*/
protected mRequest: express.Request | undefined;
/**
* 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
*
* @param {string} [host="::"] The IP address/hostname to listen on
* @param {number} [port=CDefaultPort] The port number to listen on
* @returns {BaseJsonRpcServer} BaseJsonRpcServer
*/
constructor(host?: string, port?: number);
/**
* Adds all necessary JSON RPC methods to this.mJsonRpcServer
*/
protected addRPCMethods(): void;
/**
* Allows descendants to do something after class construction
*
* @returns {void} void
*/
protected afterConstruction(): void;
/**
* Creates an Invalid Params JSON RPC Error exception, with the given message.
* The caller must throw the return of this call.
*
* @returns {string} Invalid Params JSON RPC Error exception
*/
protected createInvalidParamsMessage(message: string, data?: any): JSONRPCErrorException;
/**
* Creates an Invalid Params JSON RPC Error exception, with the message as Invalid Params.
* The caller must throw the return of this call.
*
* @returns {string} Invalid Params JSON RPC Error exception
*/
protected createInvalidParams(data?: any): JSONRPCErrorException;
/**
* Creates an Invalid Request JSON RPC Error response.
*
* @returns {string} Invalid Params JSON RPC response
*/
protected createInvalidRequestResponse(request: any): JSONRPCErrorResponse;
/**
* Creates a Method not found JSON RPC Error response, with the message as Invalid Params.
* The caller must throw the return of this call.
*
* @returns {string} Invalid Request JSON RPC Error exception
*/
protected createMethodNotFoundResponse(request: any): JSONRPCErrorException;
/**
* Creates an Invalid Request JSON RPC Error exception, with the message as Invalid Params.
* The caller must throw the return of this call.
*
* @returns {string} Invalid Request JSON RPC Error exception
*/
protected createInvalidRequest(data?: any): JSONRPCErrorException;
/**
* 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
*
* @returns {string} Invalid Request JSON RPC Error exception
*/
protected doParseError(err: any, req: Request, res: Response, next: NextFunction): void;
/**
* Handles a single or a batch JSON RPC request.
*
* @returns {void}
*/
protected handleRequest(request: express.Request, response: express.Response): Promise<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
*/
protected implementedRPCMethods(): SimpleJSONRPCMethod<void>[];
/**
* Initializes the Express server and returns it
*
* @returns {Express}
*/
protected initExpress(): import("express-serve-static-core").Express;
/**
* Initializes the JSON RPC server and returns it
*
* @returns {JSONRPCServer}
*/
protected initJsonRpcServer(): JSONRPCServer<void>;
/**
* Returns a JSON parse error during express.json parsing
*
* @returns {void}
*/
protected initParseErrorHandler(aExpress: Express): 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
*
* @returns {void}
*/
protected JsonRpcErrorListener(message: string, data: unknown): void;
/**
* Starts listening for RPC requests
*/
listen(port?: number): Promise<void>;
/**
* 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
*/
get listeningPath(): string;
protected log(...data: any[]): void;
/**
* Displays the JSON RPC methods being listened to
*/
protected logListeningMethods(methods: SimpleJSONRPCMethod<void>[]): void;
/**
* 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.
*
* @returns {JSONRPCResponsePromise}
*/
protected onBeforeDispatchRequest(next: JSONRPCServerMiddlewareNext<void>, request: JSONRPCRequest, serverParams: void | undefined): JSONRPCResponsePromise;
/**
* 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');
*/
protected onBeforeListening(): 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
*
* @returns {void}
*/
protected onParseError(err: any, request: Request, response: TSendHandler): void;
/**
* Creates an Invalid Request JSON RPC Error exception and sends it.
*
* @returns {}
*/
protected sendInvalidRequest(data?: any): void;
/**
* Creates an Invalid Params JSON RPC Error exception and sends it.
*
* @returns {}
*/
protected sendInvalidParams(data?: any): void;
/**
* Stops the RPC server. This can be called by a RPC call or by the app itself
*/
stop(): Promise<void>;
protected stopListening(): Promise<void>;
/**
* Waits for the server to starts listening before returning.
* Override to disable this
*/
protected waitForServerListening(): Promise<void>;
protected waitForServerStopListening(): Promise<void>;
echo(params: JSONRPCParameters): any;
}
export { BaseJsonRpcServer, SimpleJSONRPCMethod, Request, Response, createJSONRPCErrorResponse, isJSONRPCID, JSONRPCErrorCode, JSONRPCErrorException, JSONRPC, JSONRPCClient, JSONRPCParams, JSONRPCRequest, JSONRPCResponsePromise, JSONRPCServer, JSONRPCServerMiddlewareNext, JSONRPCParameters, SEntityParseFailed, SInvalidParams, SInvalidRequest, SMethodNotFound, SParseError, CNoContent };