UNPKG

6.73 kBTypeScriptView Raw
1import { Disposable } from '../disposable';
2import { Emitter, Event } from '../event';
3import { Channel } from '../message-rpc/channel';
4import { RequestHandler, RpcProtocol } from '../message-rpc/rpc-protocol';
5import { ConnectionHandler } from './handler';
6export declare type JsonRpcServer<Client> = Disposable & {
7 /**
8 * If this server is a proxy to a remote server then
9 * a client is used as a local object
10 * to handle JSON-RPC messages from the remote server.
11 */
12 setClient(client: Client | undefined): void;
13 getClient?(): Client | undefined;
14};
15export interface JsonRpcConnectionEventEmitter {
16 readonly onDidOpenConnection: Event<void>;
17 readonly onDidCloseConnection: Event<void>;
18}
19export declare type JsonRpcProxy<T> = T & JsonRpcConnectionEventEmitter;
20export declare class JsonRpcConnectionHandler<T extends object> implements ConnectionHandler {
21 readonly path: string;
22 readonly targetFactory: (proxy: JsonRpcProxy<T>) => any;
23 readonly factoryConstructor: new () => JsonRpcProxyFactory<T>;
24 constructor(path: string, targetFactory: (proxy: JsonRpcProxy<T>) => any, factoryConstructor?: new () => JsonRpcProxyFactory<T>);
25 onConnection(connection: Channel): void;
26}
27/**
28 * Factory for creating a new {@link RpcConnection} for a given chanel and {@link RequestHandler}.
29 */
30export declare type RpcConnectionFactory = (channel: Channel, requestHandler: RequestHandler) => RpcProtocol;
31/**
32 * Factory for JSON-RPC proxy objects.
33 *
34 * A JSON-RPC proxy exposes the programmatic interface of an object through
35 * JSON-RPC. This allows remote programs to call methods of this objects by
36 * sending JSON-RPC requests. This takes place over a bi-directional stream,
37 * where both ends can expose an object and both can call methods each other's
38 * exposed object.
39 *
40 * For example, assuming we have an object of the following type on one end:
41 *
42 * class Foo {
43 * bar(baz: number): number { return baz + 1 }
44 * }
45 *
46 * which we want to expose through a JSON-RPC interface. We would do:
47 *
48 * let target = new Foo()
49 * let factory = new JsonRpcProxyFactory<Foo>('/foo', target)
50 * factory.onConnection(connection)
51 *
52 * The party at the other end of the `connection`, in order to remotely call
53 * methods on this object would do:
54 *
55 * let factory = new JsonRpcProxyFactory<Foo>('/foo')
56 * factory.onConnection(connection)
57 * let proxy = factory.createProxy();
58 * let result = proxy.bar(42)
59 * // result is equal to 43
60 *
61 * One the wire, it would look like this:
62 *
63 * --> {"jsonrpc": "2.0", "id": 0, "method": "bar", "params": {"baz": 42}}
64 * <-- {"jsonrpc": "2.0", "id": 0, "result": 43}
65 *
66 * Note that in the code of the caller, we didn't pass a target object to
67 * JsonRpcProxyFactory, because we don't want/need to expose an object.
68 * If we had passed a target object, the other side could've called methods on
69 * it.
70 *
71 * @param <T> - The type of the object to expose to JSON-RPC.
72 */
73export declare class JsonRpcProxyFactory<T extends object> implements ProxyHandler<T> {
74 target?: any;
75 protected rpcConnectionFactory: RpcConnectionFactory;
76 protected readonly onDidOpenConnectionEmitter: Emitter<void>;
77 protected readonly onDidCloseConnectionEmitter: Emitter<void>;
78 protected connectionPromiseResolve: (connection: RpcProtocol) => void;
79 protected connectionPromise: Promise<RpcProtocol>;
80 /**
81 * Build a new JsonRpcProxyFactory.
82 *
83 * @param target - The object to expose to JSON-RPC methods calls. If this
84 * is omitted, the proxy won't be able to handle requests, only send them.
85 */
86 constructor(target?: any, rpcConnectionFactory?: RpcConnectionFactory);
87 protected waitForConnection(): void;
88 /**
89 * Connect a MessageConnection to the factory.
90 *
91 * This connection will be used to send/receive JSON-RPC requests and
92 * response.
93 */
94 listen(channel: Channel): void;
95 /**
96 * Process an incoming JSON-RPC method call.
97 *
98 * onRequest is called when the JSON-RPC connection received a method call
99 * request. It calls the corresponding method on [[target]].
100 *
101 * The return value is a Promise object that is resolved with the return
102 * value of the method call, if it is successful. The promise is rejected
103 * if the called method does not exist or if it throws.
104 *
105 * @returns A promise of the method call completion.
106 */
107 protected onRequest(method: string, ...args: any[]): Promise<any>;
108 /**
109 * Process an incoming JSON-RPC notification.
110 *
111 * Same as [[onRequest]], but called on incoming notifications rather than
112 * methods calls.
113 */
114 protected onNotification(method: string, ...args: any[]): void;
115 /**
116 * Create a Proxy exposing the interface of an object of type T. This Proxy
117 * can be used to do JSON-RPC method calls on the remote target object as
118 * if it was local.
119 *
120 * If `T` implements `JsonRpcServer` then a client is used as a target object for a remote target object.
121 */
122 createProxy(): JsonRpcProxy<T>;
123 /**
124 * Get a callable object that executes a JSON-RPC method call.
125 *
126 * Getting a property on the Proxy object returns a callable that, when
127 * called, executes a JSON-RPC call. The name of the property defines the
128 * method to be called. The callable takes a variable number of arguments,
129 * which are passed in the JSON-RPC method call.
130 *
131 * For example, if you have a Proxy object:
132 *
133 * let fooProxyFactory = JsonRpcProxyFactory<Foo>('/foo')
134 * let fooProxy = fooProxyFactory.createProxy()
135 *
136 * accessing `fooProxy.bar` will return a callable that, when called,
137 * executes a JSON-RPC method call to method `bar`. Therefore, doing
138 * `fooProxy.bar()` will call the `bar` method on the remote Foo object.
139 *
140 * @param target - unused.
141 * @param p - The property accessed on the Proxy object.
142 * @param receiver - unused.
143 * @returns A callable that executes the JSON-RPC call.
144 */
145 get(target: T, p: PropertyKey, receiver: any): any;
146 /**
147 * Return whether the given property represents a notification.
148 *
149 * A property leads to a notification rather than a method call if its name
150 * begins with `notify` or `on`.
151 *
152 * @param p - The property being called on the proxy.
153 * @return Whether `p` represents a notification.
154 */
155 protected isNotification(p: PropertyKey): boolean;
156 protected serializeError(e: any): any;
157 protected deserializeError(capturedError: Error, e: any): any;
158}
159//# sourceMappingURL=proxy-factory.d.ts.map
\No newline at end of file