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