Created
March 26, 2019 22:42
-
-
Save vinayb21/b6cead0cca3bc6391feb501aac28a5ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Factory for JSON-RPC proxy objects. | |
* | |
* A JSON-RPC proxy exposes the programmatic interface of an object through | |
* JSON-RPC. This allows remote programs to call methods of this objects by | |
* sending JSON-RPC requests. This takes place over a bi-directional stream, | |
* where both ends can expose an object and both can call methods each other's | |
* exposed object. | |
* | |
* For example, assuming we have an object of the following type on one end: | |
* | |
* class Foo { | |
* bar(baz: number): number { return baz + 1 } | |
* } | |
* | |
* which we want to expose through a JSON-RPC interface. We would do: | |
* | |
* let target = new Foo() | |
* let factory = new JsonRpcProxyFactory<Foo>('/foo', target) | |
* factory.onConnection(connection) | |
* | |
* The party at the other end of the `connection`, in order to remotely call | |
* methods on this object would do: | |
* | |
* let factory = new JsonRpcProxyFactory<Foo>('/foo') | |
* factory.onConnection(connection) | |
* let proxy = factory.createProxy(); | |
* let result = proxy.bar(42) | |
* // result is equal to 43 | |
* | |
* One the wire, it would look like this: | |
* | |
* --> {"jsonrpc": "2.0", "id": 0, "method": "bar", "params": {"baz": 42}} | |
* <-- {"jsonrpc": "2.0", "id": 0, "result": 43} | |
* | |
* Note that in the code of the caller, we didn't pass a target object to | |
* JsonRpcProxyFactory, because we don't want/need to expose an object. | |
* If we had passed a target object, the other side could've called methods on | |
* it. | |
* | |
* @param <T> - The type of the object to expose to JSON-RPC. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment