1 | import net = require('net');
|
2 | import tls = require('tls');
|
3 | import https = require('https');
|
4 | import http = require('http');
|
5 | import events = require('events');
|
6 | import Stream = require('stream');
|
7 |
|
8 | export interface UtilsJSONParseOptions {
|
9 | reviver?: Function;
|
10 | }
|
11 |
|
12 | export interface UtilsJSONStringifyOptions {
|
13 | replacer?: Function;
|
14 | }
|
15 |
|
16 | export declare class Utils {
|
17 |
|
18 | static response(error: JSONRPCError | undefined | null, result: JSONRPCResultLike | undefined | null, id: JSONRPCIDLike | null | undefined, version?: number): JSONRPCVersionTwoRequest;
|
19 | static response(error: JSONRPCError | undefined | null, result: JSONRPCResultLike | undefined | null, id: JSONRPCIDLike | null | undefined, version:2): JSONRPCVersionTwoRequest;
|
20 | static response(error: JSONRPCError | undefined | null, result: JSONRPCResultLike | undefined | null, id: JSONRPCIDLike | null | undefined, version:1): JSONRPCVersionOneRequest;
|
21 |
|
22 | static generateId(): string;
|
23 |
|
24 | static merge(...objs: object[]): object;
|
25 |
|
26 | static parseStream(stream:Stream, options:UtilsJSONParseOptions, onRequest: (err?:Error, data?:any) => void): void;
|
27 |
|
28 | static parseBody(stream:Stream, options:UtilsJSONParseOptions, callback: (err?:Error, obj?:any) => void): void;
|
29 |
|
30 | static getHttpListener(self:http.Server, server:Server): Function;
|
31 |
|
32 | static isContentType(request:http.IncomingMessage, typ:string): boolean;
|
33 |
|
34 | static isMethod(request:http.IncomingMessage, method:string): boolean;
|
35 |
|
36 | static walk(obj:object, key:string, fn: (key:string, value:any) => any): object;
|
37 |
|
38 | static JSON: UtilsJSON;
|
39 |
|
40 | static Request: UtilsRequest;
|
41 |
|
42 | static Response: UtilsResponse;
|
43 |
|
44 | }
|
45 |
|
46 | type UtilsJSON = {
|
47 | parse(str:string, options:UtilsJSONParseOptions | null | undefined, callback: (err?:Error, obj?:object) => void):void;
|
48 | stringify(obj:object, options:UtilsJSONStringifyOptions | null | undefined, callback: (err?:Error, str?:string) => void):void;
|
49 | }
|
50 |
|
51 | type UtilsRequest = {
|
52 | isBatch(request:any): boolean;
|
53 | isNotification(request:any): boolean;
|
54 | isValidVersionTwoRequest(request:any): boolean;
|
55 | isValidVersionOneRequest(request:any): boolean;
|
56 | isValidRequest(request:any, version?:number): boolean;
|
57 | }
|
58 |
|
59 | type UtilsResponse = {
|
60 | isValidError(error:any, version?:number): boolean;
|
61 | isValidResponse(response:any, version?:number): boolean;
|
62 | }
|
63 |
|
64 | export type RequestParamsLike = Array<any> | object;
|
65 |
|
66 | export interface JSONRPCError {
|
67 | code: number;
|
68 | message: string;
|
69 | data?: object;
|
70 | }
|
71 |
|
72 | export type JSONRPCErrorLike = Error | JSONRPCError;
|
73 |
|
74 | export interface JSONRPCVersionOneRequest {
|
75 | method: string;
|
76 | params: Array<any>;
|
77 | id: JSONRPCIDLike;
|
78 | }
|
79 |
|
80 | export interface JSONRPCVersionTwoRequest {
|
81 | jsonrpc: number;
|
82 | method: string;
|
83 | params: RequestParamsLike;
|
84 | id?: JSONRPCIDLike | null;
|
85 | }
|
86 |
|
87 | export type JSONRPCIDLike = number | string;
|
88 |
|
89 | export type JSONRPCRequest = JSONRPCVersionOneRequest | JSONRPCVersionTwoRequest;
|
90 |
|
91 | export type JSONRPCRequestLike = JSONRPCRequest | string;
|
92 |
|
93 | export type JSONRPCResultLike = any;
|
94 |
|
95 | export interface JSONRPCCallbackTypePlain {
|
96 | (err?: JSONRPCErrorLike | null, result?: JSONRPCResultLike): void
|
97 | }
|
98 |
|
99 | export interface JSONRPCCallbackTypeSugared {
|
100 | (err?: Error | null, error?: JSONRPCErrorLike, result?: JSONRPCResultLike): void
|
101 | }
|
102 |
|
103 | type JSONRPCCallbackType = JSONRPCCallbackTypePlain | JSONRPCCallbackTypeSugared;
|
104 |
|
105 | export interface JSONRPCCallbackTypeBatchPlain {
|
106 | (err: JSONRPCErrorLike, results?: Array<JSONRPCResultLike>): void
|
107 | }
|
108 |
|
109 | export interface JSONRPCCallbackTypeBatchSugared {
|
110 | (err: Error, errors?: Array<JSONRPCErrorLike>, results?: Array<JSONRPCResultLike>): void
|
111 | }
|
112 |
|
113 | type JSONRPCCallbackTypeBatch = JSONRPCCallbackTypeBatchPlain | JSONRPCCallbackTypeBatchSugared;
|
114 |
|
115 | export interface MethodHandler {
|
116 | (this:Server, args:RequestParamsLike, callback:JSONRPCCallbackTypePlain): void;
|
117 | }
|
118 |
|
119 | export interface MethodHandlerContext {
|
120 | (this:Server, args:RequestParamsLike, context:object, callback:JSONRPCCallbackTypePlain): void;
|
121 | }
|
122 |
|
123 | export type MethodHandlerType = MethodHandlerContext | MethodHandler;
|
124 | export type MethodOptionsParamsLike = Array<any> | Object | object;
|
125 |
|
126 | export interface MethodOptions {
|
127 | handler?: MethodHandlerType;
|
128 | useContext?: boolean;
|
129 | params?: MethodOptionsParamsLike;
|
130 | }
|
131 |
|
132 | export declare class Method {
|
133 | constructor(options: MethodOptions);
|
134 | constructor(handler?: MethodHandlerType, options?: MethodOptions);
|
135 |
|
136 | getHandler(): MethodHandlerType;
|
137 | setHandler(handler: MethodHandlerType): void;
|
138 | execute(server: Server, requestParams: RequestParamsLike, callback: JSONRPCCallbackType): any | Promise<any>;
|
139 | }
|
140 |
|
141 | export type MethodLike = Function | Method | Client
|
142 |
|
143 | export type ServerRouterFunction = (this: Server, method: string, params: RequestParamsLike) => MethodLike;
|
144 |
|
145 | export interface ServerOptions {
|
146 | useContext?: boolean;
|
147 | params?: MethodOptionsParamsLike;
|
148 | version?: number;
|
149 | reviver?: JSONParseReviver;
|
150 | replacer?: JSONStringifyReplacer;
|
151 | encoding?: string;
|
152 | router?: ServerRouterFunction;
|
153 | methodConstructor?: Function;
|
154 | }
|
155 |
|
156 | export interface MethodMap { [methodName:string]: Method }
|
157 |
|
158 | export declare class Server {
|
159 | constructor(methods?: {[methodName: string]: MethodLike}, options?: object);
|
160 |
|
161 | static errors: {[errorName: string]: number};
|
162 | static errorMessages: {[errorMessage: string]: string};
|
163 | static interfaces: {[interfaces: string]: Function};
|
164 |
|
165 | public _methods: MethodMap;
|
166 |
|
167 | http(options?: HttpServerOptions): HttpServer;
|
168 | https(options?: HttpsServerOptions): HttpsServer;
|
169 | tcp(options?: TcpServerOptions): TcpServer;
|
170 | tls(options?: TlsServerOptions): TlsServer;
|
171 | middleware(options?: MiddlewareServerOptions): Function;
|
172 |
|
173 | method(name: string, definition: MethodLike): void;
|
174 | methods(methods: {[methodName: string]: MethodLike}): void;
|
175 | hasMethod(name: string): boolean;
|
176 | removeMethod(name: string): void;
|
177 | getMethod(name: string): MethodLike;
|
178 | error(code?: number, message?: string, data?: object): JSONRPCError;
|
179 | call(request: JSONRPCRequestLike | Array<JSONRPCRequestLike>, originalCallback?: JSONRPCCallbackType): void;
|
180 | }
|
181 |
|
182 | export interface MiddlewareServerOptions extends ServerOptions {
|
183 | }
|
184 |
|
185 | export interface HttpServerOptions extends ServerOptions {
|
186 | }
|
187 |
|
188 | declare class HttpServer extends http.Server {
|
189 | constructor(server: Server, options?: HttpServerOptions);
|
190 | }
|
191 |
|
192 | export interface HttpsServerOptions extends ServerOptions, https.ServerOptions {
|
193 | }
|
194 |
|
195 | declare class HttpsServer extends https.Server {
|
196 | constructor(server: Server, options?: HttpsServerOptions);
|
197 | }
|
198 |
|
199 | export interface TcpServerOptions extends ServerOptions {
|
200 | }
|
201 |
|
202 | declare class TcpServer extends net.Server {
|
203 | constructor(server: Server, options?: TcpServerOptions);
|
204 | }
|
205 |
|
206 | export interface TlsServerOptions extends tls.TlsOptions {
|
207 | }
|
208 |
|
209 | declare class TlsServer extends tls.Server {
|
210 | constructor(server: Server, options?: TlsServerOptions);
|
211 | }
|
212 |
|
213 | type JSONParseReviver = (key: string, value: any) => any;
|
214 | type JSONStringifyReplacer = (key: string, value: any) => any;
|
215 |
|
216 | type IDGenerator = () => string;
|
217 |
|
218 | export interface ClientOptions {
|
219 | version?: number;
|
220 | reviver?: JSONParseReviver;
|
221 | replacer?: JSONStringifyReplacer;
|
222 | generator?: IDGenerator;
|
223 | }
|
224 |
|
225 | export interface HttpClientOptions extends ClientOptions, http.RequestOptions {
|
226 | }
|
227 |
|
228 | declare class HttpClient extends Client {
|
229 | constructor(options?: HttpClientOptions);
|
230 | }
|
231 |
|
232 | export interface TlsClientOptions extends ClientOptions, tls.ConnectionOptions {
|
233 | }
|
234 |
|
235 | declare class TlsClient extends Client {
|
236 | constructor(options?: TlsClientOptions);
|
237 | }
|
238 |
|
239 | export interface TcpClientOptions extends ClientOptions, net.TcpSocketConnectOpts {
|
240 | }
|
241 |
|
242 | declare class TcpClient extends Client {
|
243 | constructor(options?: TcpClientOptions);
|
244 | }
|
245 |
|
246 | export interface HttpsClientOptions extends ClientOptions, https.RequestOptions {
|
247 | }
|
248 |
|
249 | declare class HttpsClient extends Client {
|
250 | constructor(options?: HttpsClientOptions);
|
251 | }
|
252 |
|
253 | type ClientRequestShouldCall = JSONRPCCallbackType | false;
|
254 |
|
255 | export declare class Client extends events.EventEmitter {
|
256 | constructor(server: Server, options?: ClientOptions);
|
257 | constructor(options: ClientOptions);
|
258 |
|
259 | static http(options?: HttpClientOptions): HttpClient;
|
260 | static https(options?: HttpsClientOptions): HttpsClient;
|
261 | static tcp(options?: TcpClientOptions): TcpClient;
|
262 | static tls(options?: TlsClientOptions): TlsClient;
|
263 |
|
264 | request(method: string, params: RequestParamsLike, id?: string | null, callback?: JSONRPCCallbackType): JSONRPCRequest;
|
265 | request(method: string, params: RequestParamsLike, callback?: JSONRPCCallbackType): JSONRPCRequest;
|
266 | request(method: Array<JSONRPCRequestLike>, callback: JSONRPCCallbackTypeBatch): Array<JSONRPCRequest>;
|
267 | }
|