doc
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
# Class: RPCServer <**SubResType**>
|
||||
|
||||
A Websocket-server-on-steroids with built-in RPC capabilities
|
||||
|
||||
## Type parameters
|
||||
|
||||
▪ **SubResType**
|
||||
@@ -41,17 +43,19 @@
|
||||
|
||||
### constructor
|
||||
|
||||
\+ **new RPCServer**(`port`: number, `exporters`: [Exporter](../interfaces/_interfaces_.exporter.md)‹SubResType›[], `conf`: T.SocketConf): *[RPCServer](_backend_.rpcserver.md)*
|
||||
\+ **new RPCServer**(`port`: number, `exporters`: [RPCExporter](../interfaces/_interfaces_.rpcexporter.md)‹SubResType›[], `conf`: T.SocketConf): *[RPCServer](_backend_.rpcserver.md)*
|
||||
|
||||
Defined in Backend.ts:19
|
||||
Defined in Backend.ts:22
|
||||
|
||||
**`throws`** On RPC with no name
|
||||
|
||||
**Parameters:**
|
||||
|
||||
Name | Type | Default |
|
||||
------ | ------ | ------ |
|
||||
`port` | number | - |
|
||||
`exporters` | [Exporter](../interfaces/_interfaces_.exporter.md)‹SubResType›[] | [] |
|
||||
`conf` | T.SocketConf | {} |
|
||||
Name | Type | Default | Description |
|
||||
------ | ------ | ------ | ------ |
|
||||
`port` | number | - | The port to listen on |
|
||||
`exporters` | [RPCExporter](../interfaces/_interfaces_.rpcexporter.md)‹SubResType›[] | [] | A list of [RPCExporter](../interfaces/_interfaces_.rpcexporter.md) to publish |
|
||||
`conf` | T.SocketConf | {} | A [SocketConf](../modules/_types_.md#socketconf) object with optional settings |
|
||||
|
||||
**Returns:** *[RPCServer](_backend_.rpcserver.md)*
|
||||
|
||||
@@ -61,7 +65,7 @@ Name | Type | Default |
|
||||
|
||||
• **closeHandler**: *T.CloseHandler*
|
||||
|
||||
Defined in Backend.ts:17
|
||||
Defined in Backend.ts:20
|
||||
|
||||
___
|
||||
|
||||
@@ -69,7 +73,7 @@ ___
|
||||
|
||||
• **connectionHandler**: *T.ConnectionHandler*
|
||||
|
||||
Defined in Backend.ts:19
|
||||
Defined in Backend.ts:22
|
||||
|
||||
___
|
||||
|
||||
@@ -77,15 +81,17 @@ ___
|
||||
|
||||
• **errorHandler**: *T.ErrorHandler*
|
||||
|
||||
Defined in Backend.ts:18
|
||||
Defined in Backend.ts:21
|
||||
|
||||
___
|
||||
|
||||
### `Private` exporters
|
||||
|
||||
• **exporters**: *[Exporter](../interfaces/_interfaces_.exporter.md)‹SubResType›[]*
|
||||
• **exporters**: *[RPCExporter](../interfaces/_interfaces_.rpcexporter.md)‹SubResType›[]*
|
||||
|
||||
Defined in Backend.ts:23
|
||||
Defined in Backend.ts:32
|
||||
|
||||
A list of [RPCExporter](../interfaces/_interfaces_.rpcexporter.md) to publish
|
||||
|
||||
___
|
||||
|
||||
@@ -93,7 +99,7 @@ ___
|
||||
|
||||
• **io**: *any* = bsock.createServer()
|
||||
|
||||
Defined in Backend.ts:15
|
||||
Defined in Backend.ts:18
|
||||
|
||||
___
|
||||
|
||||
@@ -101,7 +107,9 @@ ___
|
||||
|
||||
• **port**: *number*
|
||||
|
||||
Defined in Backend.ts:22
|
||||
Defined in Backend.ts:31
|
||||
|
||||
The port to listen on
|
||||
|
||||
___
|
||||
|
||||
@@ -109,7 +117,7 @@ ___
|
||||
|
||||
• **visibility**: *T.Visibility*
|
||||
|
||||
Defined in Backend.ts:16
|
||||
Defined in Backend.ts:19
|
||||
|
||||
___
|
||||
|
||||
@@ -117,7 +125,7 @@ ___
|
||||
|
||||
• **ws**: *Server* = http.createServer()
|
||||
|
||||
Defined in Backend.ts:14
|
||||
Defined in Backend.ts:17
|
||||
|
||||
## Methods
|
||||
|
||||
@@ -127,7 +135,7 @@ Defined in Backend.ts:14
|
||||
|
||||
*Implementation of [Destroyable](../interfaces/_interfaces_.destroyable.md)*
|
||||
|
||||
Defined in Backend.ts:80
|
||||
Defined in Backend.ts:89
|
||||
|
||||
**Returns:** *Promise‹void›*
|
||||
|
||||
@@ -137,7 +145,7 @@ ___
|
||||
|
||||
▸ **initRPCs**(`socket`: [Socket](../interfaces/_interfaces_.socket.md)): *void*
|
||||
|
||||
Defined in Backend.ts:73
|
||||
Defined in Backend.ts:82
|
||||
|
||||
**Parameters:**
|
||||
|
||||
@@ -153,6 +161,6 @@ ___
|
||||
|
||||
▸ **startWebsocket**(): *void*
|
||||
|
||||
Defined in Backend.ts:57
|
||||
Defined in Backend.ts:66
|
||||
|
||||
**Returns:** *void*
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
# Class: RPCSocket
|
||||
|
||||
A websocket-on-steroids with built-in RPC capabilities
|
||||
|
||||
## Hierarchy
|
||||
|
||||
* **RPCSocket**
|
||||
@@ -43,15 +45,15 @@
|
||||
|
||||
\+ **new RPCSocket**(`port`: number, `server`: string, `tls`: boolean): *[RPCSocket](_frontend_.rpcsocket.md)*
|
||||
|
||||
Defined in Frontend.ts:15
|
||||
Defined in Frontend.ts:20
|
||||
|
||||
**Parameters:**
|
||||
|
||||
Name | Type | Default |
|
||||
------ | ------ | ------ |
|
||||
`port` | number | - |
|
||||
`server` | string | - |
|
||||
`tls` | boolean | false |
|
||||
Name | Type | Default | Description |
|
||||
------ | ------ | ------ | ------ |
|
||||
`port` | number | - | Port to connect to |
|
||||
`server` | string | - | Server address |
|
||||
`tls` | boolean | false | @default false use TLS |
|
||||
|
||||
**Returns:** *[RPCSocket](_frontend_.rpcsocket.md)*
|
||||
|
||||
@@ -63,7 +65,9 @@ Name | Type | Default |
|
||||
|
||||
*Implementation of [Socket](../interfaces/_interfaces_.socket.md).[port](../interfaces/_interfaces_.socket.md#port)*
|
||||
|
||||
Defined in Frontend.ts:16
|
||||
Defined in Frontend.ts:28
|
||||
|
||||
Port to connect to
|
||||
|
||||
___
|
||||
|
||||
@@ -71,7 +75,9 @@ ___
|
||||
|
||||
• **server**: *string*
|
||||
|
||||
Defined in Frontend.ts:16
|
||||
Defined in Frontend.ts:28
|
||||
|
||||
Server address
|
||||
|
||||
___
|
||||
|
||||
@@ -79,7 +85,7 @@ ___
|
||||
|
||||
• **socket**: *[Socket](../interfaces/_interfaces_.socket.md)*
|
||||
|
||||
Defined in Frontend.ts:15
|
||||
Defined in Frontend.ts:20
|
||||
|
||||
___
|
||||
|
||||
@@ -87,7 +93,9 @@ ___
|
||||
|
||||
• **tls**: *boolean*
|
||||
|
||||
Defined in Frontend.ts:16
|
||||
Defined in Frontend.ts:28
|
||||
|
||||
@default false use TLS
|
||||
|
||||
## Methods
|
||||
|
||||
@@ -95,14 +103,16 @@ Defined in Frontend.ts:16
|
||||
|
||||
▸ **call**(`rpcname`: T.Name, ...`args`: T.Any[]): *Promise‹T.Any›*
|
||||
|
||||
Defined in Frontend.ts:40
|
||||
Defined in Frontend.ts:77
|
||||
|
||||
Trigger a hooked handler on the server
|
||||
|
||||
**Parameters:**
|
||||
|
||||
Name | Type |
|
||||
------ | ------ |
|
||||
`rpcname` | T.Name |
|
||||
`...args` | T.Any[] |
|
||||
Name | Type | Description |
|
||||
------ | ------ | ------ |
|
||||
`rpcname` | T.Name | The function to call |
|
||||
`...args` | T.Any[] | other arguments |
|
||||
|
||||
**Returns:** *Promise‹T.Any›*
|
||||
|
||||
@@ -112,14 +122,16 @@ ___
|
||||
|
||||
▸ **callGenerator**(`fnName`: T.Name, `fnArgs`: T.Arg[]): *T.AsyncFunction*
|
||||
|
||||
Defined in Frontend.ts:73
|
||||
Defined in Frontend.ts:126
|
||||
|
||||
Utility [AsyncFunction](../modules/_types_.md#asyncfunction) generator
|
||||
|
||||
**Parameters:**
|
||||
|
||||
Name | Type |
|
||||
------ | ------ |
|
||||
`fnName` | T.Name |
|
||||
`fnArgs` | T.Arg[] |
|
||||
Name | Type | Description |
|
||||
------ | ------ | ------ |
|
||||
`fnName` | T.Name | The function name |
|
||||
`fnArgs` | T.Arg[] | A string-list of parameters |
|
||||
|
||||
**Returns:** *T.AsyncFunction*
|
||||
|
||||
@@ -131,7 +143,9 @@ ___
|
||||
|
||||
*Implementation of [Socket](../interfaces/_interfaces_.socket.md)*
|
||||
|
||||
Defined in Frontend.ts:36
|
||||
Defined in Frontend.ts:68
|
||||
|
||||
Closes the socket. It may attempt to reconnect.
|
||||
|
||||
**Returns:** *void*
|
||||
|
||||
@@ -141,7 +155,9 @@ ___
|
||||
|
||||
▸ **connect**(): *Promise‹void›*
|
||||
|
||||
Defined in Frontend.ts:48
|
||||
Defined in Frontend.ts:93
|
||||
|
||||
Connects to the server and attaches available RPCs to this object
|
||||
|
||||
**Returns:** *Promise‹void›*
|
||||
|
||||
@@ -153,7 +169,9 @@ ___
|
||||
|
||||
*Implementation of [Socket](../interfaces/_interfaces_.socket.md)*
|
||||
|
||||
Defined in Frontend.ts:32
|
||||
Defined in Frontend.ts:61
|
||||
|
||||
Destroys the socket
|
||||
|
||||
**Returns:** *void*
|
||||
|
||||
@@ -161,18 +179,20 @@ ___
|
||||
|
||||
### fire
|
||||
|
||||
▸ **fire**(`rpcname`: T.Name, ...`args`: T.Any[]): *Promise‹T.Any›*
|
||||
▸ **fire**(`rpcname`: T.Name, ...`args`: T.Any[]): *Promise‹void›*
|
||||
|
||||
Defined in Frontend.ts:44
|
||||
Defined in Frontend.ts:86
|
||||
|
||||
An alternative to call that does not wait for confirmation and doesn't return a value.
|
||||
|
||||
**Parameters:**
|
||||
|
||||
Name | Type |
|
||||
------ | ------ |
|
||||
`rpcname` | T.Name |
|
||||
`...args` | T.Any[] |
|
||||
Name | Type | Description |
|
||||
------ | ------ | ------ |
|
||||
`rpcname` | T.Name | The function to call |
|
||||
`...args` | T.Any[] | other arguments |
|
||||
|
||||
**Returns:** *Promise‹T.Any›*
|
||||
**Returns:** *Promise‹void›*
|
||||
|
||||
___
|
||||
|
||||
@@ -180,14 +200,20 @@ ___
|
||||
|
||||
▸ **hook**(`name`: T.Name, `handler`: function): *[Socket](../interfaces/_interfaces_.socket.md)*
|
||||
|
||||
Defined in Frontend.ts:20
|
||||
Defined in Frontend.ts:37
|
||||
|
||||
Hooks a handler to a function name. Use [call](_frontend_.rpcsocket.md#call) to trigger it.
|
||||
|
||||
**Parameters:**
|
||||
|
||||
▪ **name**: *T.Name*
|
||||
|
||||
The function name to listen on
|
||||
|
||||
▪ **handler**: *function*
|
||||
|
||||
The handler to attach
|
||||
|
||||
▸ (...`args`: any[]): *any | Promise‹any›*
|
||||
|
||||
**Parameters:**
|
||||
@@ -204,14 +230,16 @@ ___
|
||||
|
||||
▸ **hookGenerator**(`fnName`: T.Name, `fnArgs`: T.Arg[]): *T.HookFunction*
|
||||
|
||||
Defined in Frontend.ts:79
|
||||
Defined in Frontend.ts:137
|
||||
|
||||
Utility [HookFunction](../modules/_types_.md#hookfunction) generator
|
||||
|
||||
**Parameters:**
|
||||
|
||||
Name | Type |
|
||||
------ | ------ |
|
||||
`fnName` | T.Name |
|
||||
`fnArgs` | T.Arg[] |
|
||||
Name | Type | Description |
|
||||
------ | ------ | ------ |
|
||||
`fnName` | T.Name | The function name |
|
||||
`fnArgs` | T.Arg[] | A string-list of parameters |
|
||||
|
||||
**Returns:** *T.HookFunction*
|
||||
|
||||
@@ -221,7 +249,9 @@ ___
|
||||
|
||||
▸ **info**(): *Promise‹any›*
|
||||
|
||||
Defined in Frontend.ts:69
|
||||
Defined in Frontend.ts:117
|
||||
|
||||
Get a list of available RPCs from the server
|
||||
|
||||
**Returns:** *Promise‹any›*
|
||||
|
||||
@@ -231,14 +261,20 @@ ___
|
||||
|
||||
▸ **on**(`type`: "error" | "close", `f`: function): *[Socket](../interfaces/_interfaces_.socket.md)*
|
||||
|
||||
Defined in Frontend.ts:28
|
||||
Defined in Frontend.ts:54
|
||||
|
||||
Attach a listener to error or close events
|
||||
|
||||
**Parameters:**
|
||||
|
||||
▪ **type**: *"error" | "close"*
|
||||
|
||||
'error' or 'close'
|
||||
|
||||
▪ **f**: *function*
|
||||
|
||||
The listener to attach
|
||||
|
||||
▸ (`e?`: any): *void*
|
||||
|
||||
**Parameters:**
|
||||
@@ -255,12 +291,14 @@ ___
|
||||
|
||||
▸ **unhook**(`name`: T.Name): *[Socket](../interfaces/_interfaces_.socket.md)*
|
||||
|
||||
Defined in Frontend.ts:24
|
||||
Defined in Frontend.ts:45
|
||||
|
||||
Removes a [hook](_frontend_.rpcsocket.md#hook) listener by name.
|
||||
|
||||
**Parameters:**
|
||||
|
||||
Name | Type |
|
||||
------ | ------ |
|
||||
`name` | T.Name |
|
||||
Name | Type | Description |
|
||||
------ | ------ | ------ |
|
||||
`name` | T.Name | The function name |
|
||||
|
||||
**Returns:** *[Socket](../interfaces/_interfaces_.socket.md)*
|
||||
|
||||
@@ -24,6 +24,6 @@
|
||||
|
||||
▸ **destroy**(): *void*
|
||||
|
||||
Defined in Interfaces.ts:20
|
||||
Defined in Interfaces.ts:26
|
||||
|
||||
**Returns:** *void*
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
[rpclibrary](../README.md) › [Globals](../globals.md) › ["Interfaces"](../modules/_interfaces_.md) › [Exporter](_interfaces_.exporter.md)
|
||||
|
||||
# Interface: Exporter <**T**>
|
||||
|
||||
## Type parameters
|
||||
|
||||
▪ **T**
|
||||
|
||||
## Hierarchy
|
||||
|
||||
* **Exporter**
|
||||
|
||||
## Index
|
||||
|
||||
### Properties
|
||||
|
||||
* [name](_interfaces_.exporter.md#name)
|
||||
|
||||
### Methods
|
||||
|
||||
* [exportRPCs](_interfaces_.exporter.md#exportrpcs)
|
||||
|
||||
## Properties
|
||||
|
||||
### name
|
||||
|
||||
• **name**: *T.Name*
|
||||
|
||||
Defined in Interfaces.ts:5
|
||||
|
||||
## Methods
|
||||
|
||||
### exportRPCs
|
||||
|
||||
▸ **exportRPCs**(): *T.RPC‹T›[]*
|
||||
|
||||
Defined in Interfaces.ts:6
|
||||
|
||||
**Returns:** *T.RPC‹T›[]*
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
# Interface: Socket
|
||||
|
||||
Generic socket interface that can apply to bsock as well as RPCSocket
|
||||
|
||||
## Hierarchy
|
||||
|
||||
* [Destroyable](_interfaces_.destroyable.md)
|
||||
@@ -34,7 +36,7 @@
|
||||
|
||||
• **call**: *function*
|
||||
|
||||
Defined in Interfaces.ts:13
|
||||
Defined in Interfaces.ts:19
|
||||
|
||||
#### Type declaration:
|
||||
|
||||
@@ -53,7 +55,7 @@ ___
|
||||
|
||||
• **fire**: *function*
|
||||
|
||||
Defined in Interfaces.ts:14
|
||||
Defined in Interfaces.ts:20
|
||||
|
||||
#### Type declaration:
|
||||
|
||||
@@ -72,7 +74,7 @@ ___
|
||||
|
||||
• **hook**: *function*
|
||||
|
||||
Defined in Interfaces.ts:11
|
||||
Defined in Interfaces.ts:17
|
||||
|
||||
#### Type declaration:
|
||||
|
||||
@@ -98,7 +100,7 @@ ___
|
||||
|
||||
• **on**: *T.OnFunction*
|
||||
|
||||
Defined in Interfaces.ts:15
|
||||
Defined in Interfaces.ts:21
|
||||
|
||||
___
|
||||
|
||||
@@ -106,7 +108,7 @@ ___
|
||||
|
||||
• **port**: *number*
|
||||
|
||||
Defined in Interfaces.ts:10
|
||||
Defined in Interfaces.ts:16
|
||||
|
||||
___
|
||||
|
||||
@@ -114,7 +116,7 @@ ___
|
||||
|
||||
• **unhook**: *function*
|
||||
|
||||
Defined in Interfaces.ts:12
|
||||
Defined in Interfaces.ts:18
|
||||
|
||||
#### Type declaration:
|
||||
|
||||
@@ -132,7 +134,7 @@ Name | Type |
|
||||
|
||||
▸ **close**(): *void*
|
||||
|
||||
Defined in Interfaces.ts:16
|
||||
Defined in Interfaces.ts:22
|
||||
|
||||
**Returns:** *void*
|
||||
|
||||
@@ -144,6 +146,6 @@ ___
|
||||
|
||||
*Inherited from [Destroyable](_interfaces_.destroyable.md).[destroy](_interfaces_.destroyable.md#destroy)*
|
||||
|
||||
Defined in Interfaces.ts:20
|
||||
Defined in Interfaces.ts:26
|
||||
|
||||
**Returns:** *void*
|
||||
|
||||
@@ -18,12 +18,14 @@
|
||||
|
||||
▸ **stripAfterEquals**(`str`: string): *string*
|
||||
|
||||
Defined in Frontend.ts:9
|
||||
Defined in Frontend.ts:12
|
||||
|
||||
Utility function to strip parameters like "a = 3" of their defaults
|
||||
|
||||
**Parameters:**
|
||||
|
||||
Name | Type |
|
||||
------ | ------ |
|
||||
`str` | string |
|
||||
Name | Type | Description |
|
||||
------ | ------ | ------ |
|
||||
`str` | string | The parameter to modify |
|
||||
|
||||
**Returns:** *string*
|
||||
|
||||
@@ -7,5 +7,5 @@
|
||||
### Interfaces
|
||||
|
||||
* [Destroyable](../interfaces/_interfaces_.destroyable.md)
|
||||
* [Exporter](../interfaces/_interfaces_.exporter.md)
|
||||
* [RPCExporter](../interfaces/_interfaces_.rpcexporter.md)
|
||||
* [Socket](../interfaces/_interfaces_.socket.md)
|
||||
|
||||
+23
-15
@@ -18,13 +18,15 @@
|
||||
|
||||
▸ **extractArgs**(`f`: Function): *T.Arg[]*
|
||||
|
||||
Defined in Utils.ts:102
|
||||
Defined in Utils.ts:110
|
||||
|
||||
Extract a string list of parameters from a function
|
||||
|
||||
**Parameters:**
|
||||
|
||||
Name | Type |
|
||||
------ | ------ |
|
||||
`f` | Function |
|
||||
Name | Type | Description |
|
||||
------ | ------ | ------ |
|
||||
`f` | Function | The source function |
|
||||
|
||||
**Returns:** *T.Arg[]*
|
||||
|
||||
@@ -34,16 +36,20 @@ ___
|
||||
|
||||
▸ **hookGenerator**(`rpc`: T.HookRPC‹any›): *function*
|
||||
|
||||
Defined in Utils.ts:81
|
||||
Defined in Utils.ts:85
|
||||
|
||||
Utility function to generate [HookFunction](_types_.md#hookfunction) from a RPC
|
||||
|
||||
**Parameters:**
|
||||
|
||||
Name | Type |
|
||||
------ | ------ |
|
||||
`rpc` | T.HookRPC‹any› |
|
||||
Name | Type | Description |
|
||||
------ | ------ | ------ |
|
||||
`rpc` | T.HookRPC‹any› | The RPC to transform |
|
||||
|
||||
**Returns:** *function*
|
||||
|
||||
A [HookFunction](_types_.md#hookfunction)
|
||||
|
||||
▸ (`socket?`: I.Socket): *[HookFunction](_types_.md#hookfunction)‹T›*
|
||||
|
||||
**Parameters:**
|
||||
@@ -58,13 +64,15 @@ ___
|
||||
|
||||
▸ **makeSubResponse**(`uuid?`: undefined | string): *[SubscriptionResponse](_types_.md#subscriptionresponse)*
|
||||
|
||||
Defined in Utils.ts:107
|
||||
Defined in Utils.ts:119
|
||||
|
||||
Simple utility function to create basic [SubscriptionResponse](_types_.md#subscriptionresponse)
|
||||
|
||||
**Parameters:**
|
||||
|
||||
Name | Type |
|
||||
------ | ------ |
|
||||
`uuid?` | undefined | string |
|
||||
Name | Type | Description |
|
||||
------ | ------ | ------ |
|
||||
`uuid?` | undefined | string | optional uuid to use, otherwise defaults to uuid/v4 |
|
||||
|
||||
**Returns:** *[SubscriptionResponse](_types_.md#subscriptionresponse)*
|
||||
|
||||
@@ -72,11 +80,11 @@ ___
|
||||
|
||||
### rpcHooker
|
||||
|
||||
▸ **rpcHooker**<**SubResT**>(`socket`: [Socket](../interfaces/_interfaces_.socket.md), `exporter`: [Exporter](../interfaces/_interfaces_.exporter.md)‹SubResT›, `makeUnique`: boolean): *T.ExtendedRpcInfo[]*
|
||||
▸ **rpcHooker**<**SubResT**>(`socket`: [Socket](../interfaces/_interfaces_.socket.md), `exporter`: [RPCExporter](../interfaces/_interfaces_.rpcexporter.md)‹SubResT›, `makeUnique`: boolean): *T.ExtendedRpcInfo[]*
|
||||
|
||||
Defined in Utils.ts:59
|
||||
|
||||
Utility function to apply the RPCs of an [Exporter](../interfaces/_interfaces_.exporter.md).
|
||||
Utility function to apply the RPCs of an [RPCExporter](../interfaces/_interfaces_.rpcexporter.md).
|
||||
|
||||
**Type parameters:**
|
||||
|
||||
@@ -87,7 +95,7 @@ Utility function to apply the RPCs of an [Exporter](../interfaces/_interfaces_.e
|
||||
Name | Type | Default | Description |
|
||||
------ | ------ | ------ | ------ |
|
||||
`socket` | [Socket](../interfaces/_interfaces_.socket.md) | - | The websocket (implementation: bsock) to hook on |
|
||||
`exporter` | [Exporter](../interfaces/_interfaces_.exporter.md)‹SubResT› | - | The exporter |
|
||||
`exporter` | [RPCExporter](../interfaces/_interfaces_.rpcexporter.md)‹SubResT› | - | The exporter |
|
||||
`makeUnique` | boolean | true | @default true Attach a suffix to RPC names |
|
||||
|
||||
**Returns:** *T.ExtendedRpcInfo[]*
|
||||
|
||||
+10
-1
@@ -7,6 +7,9 @@ import * as T from './Types';
|
||||
import * as U from './Utils';
|
||||
import * as I from './Interfaces';
|
||||
|
||||
/**
|
||||
* A Websocket-server-on-steroids with built-in RPC capabilities
|
||||
*/
|
||||
export class RPCServer<
|
||||
SubResType = {}
|
||||
> implements I.Destroyable{
|
||||
@@ -18,9 +21,15 @@ export class RPCServer<
|
||||
private errorHandler: T.ErrorHandler
|
||||
private connectionHandler: T.ConnectionHandler
|
||||
|
||||
/**
|
||||
* @throws On RPC with no name
|
||||
* @param port The port to listen on
|
||||
* @param exporters A list of {@link RPCExporter} to publish
|
||||
* @param conf A {@link SocketConf} object with optional settings
|
||||
*/
|
||||
constructor(
|
||||
private port:number,
|
||||
private exporters: I.Exporter<SubResType>[] = [],
|
||||
private exporters: I.RPCExporter<SubResType>[] = [],
|
||||
conf: T.SocketConf = {}
|
||||
){
|
||||
|
||||
|
||||
+63
-5
@@ -5,46 +5,91 @@ import bsock = require('bsock');
|
||||
import * as T from './Types';
|
||||
import * as I from './Interfaces';
|
||||
|
||||
//fix args with defaults like "force = true" -> "force"
|
||||
function stripAfterEquals(str:string){
|
||||
/**
|
||||
* Utility function to strip parameters like "a = 3" of their defaults
|
||||
* @param str The parameter to modify
|
||||
*/
|
||||
function stripAfterEquals(str:string):string{
|
||||
return str.split("=")[0]
|
||||
}
|
||||
|
||||
/**
|
||||
* A websocket-on-steroids with built-in RPC capabilities
|
||||
*/
|
||||
export class RPCSocket implements I.Socket{
|
||||
|
||||
private socket: I.Socket
|
||||
|
||||
/**
|
||||
*
|
||||
* @param port Port to connect to
|
||||
* @param server Server address
|
||||
* @param tls @default false use TLS
|
||||
*/
|
||||
constructor(public port:number, private server: string, private tls: boolean = false){
|
||||
Object.defineProperty(this, 'socket', {value: undefined, writable: true})
|
||||
}
|
||||
|
||||
/**
|
||||
* Hooks a handler to a function name. Use {@link call} to trigger it.
|
||||
* @param name The function name to listen on
|
||||
* @param handler The handler to attach
|
||||
*/
|
||||
public hook(name: T.Name, handler: (...args:any[]) => any | Promise<any>){
|
||||
return this.socket.hook(name, handler)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a {@link hook} listener by name.
|
||||
* @param name The function name
|
||||
*/
|
||||
public unhook(name: T.Name){
|
||||
return this.socket.unhook(name)
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach a listener to error or close events
|
||||
* @param type 'error' or 'close'
|
||||
* @param f The listener to attach
|
||||
*/
|
||||
public on(type: "error" | "close", f: (e?: any) => void){
|
||||
return this.socket.on(type, f)
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the socket
|
||||
*/
|
||||
public destroy(){
|
||||
return this.socket.destroy()
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the socket. It may attempt to reconnect.
|
||||
*/
|
||||
public close(){
|
||||
return this.socket.close()
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger a hooked handler on the server
|
||||
* @param rpcname The function to call
|
||||
* @param args other arguments
|
||||
*/
|
||||
public async call (rpcname: T.Name, ...args: T.Any[]) : Promise<T.Any>{
|
||||
return await this.socket.call.apply(this.socket, [rpcname, ...args])
|
||||
}
|
||||
|
||||
public async fire(rpcname: T.Name, ...args: T.Any[]) : Promise<T.Any>{
|
||||
return await this.socket.fire.apply(this.socket, [rpcname, ...args])
|
||||
/**
|
||||
* An alternative to call that does not wait for confirmation and doesn't return a value.
|
||||
* @param rpcname The function to call
|
||||
* @param args other arguments
|
||||
*/
|
||||
public async fire(rpcname: T.Name, ...args: T.Any[]) : Promise<void>{
|
||||
await this.socket.fire.apply(this.socket, [rpcname, ...args])
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to the server and attaches available RPCs to this object
|
||||
*/
|
||||
public async connect(){
|
||||
this.socket = await bsock.connect(this.port, this.server, this.tls)
|
||||
|
||||
@@ -66,16 +111,29 @@ export class RPCSocket implements I.Socket{
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of available RPCs from the server
|
||||
*/
|
||||
public async info(){
|
||||
return await this.socket.call('info')
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility {@link AsyncFunction} generator
|
||||
* @param fnName The function name
|
||||
* @param fnArgs A string-list of parameters
|
||||
*/
|
||||
private callGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.AsyncFunction{
|
||||
const headerArgs = fnArgs.join(",")
|
||||
const argParams = fnArgs.map(stripAfterEquals).join(",")
|
||||
return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", '+argParams+')} )()' )
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility {@link HookFunction} generator
|
||||
* @param fnName The function name
|
||||
* @param fnArgs A string-list of parameters
|
||||
*/
|
||||
private hookGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.HookFunction{
|
||||
const headerArgs = fnArgs.join(",")
|
||||
const argParams = fnArgs.map(stripAfterEquals).join(",")
|
||||
|
||||
+7
-1
@@ -1,11 +1,17 @@
|
||||
import * as T from "./Types";
|
||||
import * as I from "./Interfaces"
|
||||
|
||||
export interface Exporter<T = {}>{
|
||||
/**
|
||||
* Interface for all classes that may export RPCs
|
||||
*/
|
||||
export interface RPCExporter<T = {}>{
|
||||
name: T.Name
|
||||
exportRPCs() : T.RPC<T>[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic socket interface that can apply to bsock as well as RPCSocket
|
||||
*/
|
||||
export interface Socket extends Destroyable {
|
||||
port: number
|
||||
hook: (rpcname: T.Name, handler: (...args:any[]) => any | Promise<any>) => I.Socket
|
||||
|
||||
+16
-4
@@ -8,7 +8,7 @@ import { SubscriptionResponse } from "./Types";
|
||||
* Translate an RPC to RPCInfo for serialization.
|
||||
* @param rpc The RPC to transform
|
||||
* @param owner The owning RPC group's name
|
||||
* @throws {Errror} Error on RPC without name property
|
||||
* @throws Error on RPC without name property
|
||||
*/
|
||||
export const rpcToRpcinfo = <SubResT = {}>(rpc : T.RPC<SubResT>, owner: T.Owner):T.RpcInfo => {
|
||||
switch (typeof rpc){
|
||||
@@ -51,12 +51,12 @@ RPC did not provide a name.
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility function to apply the RPCs of an {@link Exporter}.
|
||||
* Utility function to apply the RPCs of an {@link RPCExporter}.
|
||||
* @param socket The websocket (implementation: bsock) to hook on
|
||||
* @param exporter The exporter
|
||||
* @param makeUnique @default true Attach a suffix to RPC names
|
||||
*/
|
||||
export function rpcHooker<SubResT = {}>(socket: I.Socket, exporter:I.Exporter<SubResT>, makeUnique = true):T.ExtendedRpcInfo[]{
|
||||
export function rpcHooker<SubResT = {}>(socket: I.Socket, exporter:I.RPCExporter<SubResT>, makeUnique = true):T.ExtendedRpcInfo[]{
|
||||
const owner = exporter.name
|
||||
const RPCs = [...exporter.exportRPCs()]
|
||||
const suffix = makeUnique?"-"+uuidv4().substr(0,4):""
|
||||
@@ -77,7 +77,11 @@ export function rpcHooker<SubResT = {}>(socket: I.Socket, exporter:I.Exporter<Su
|
||||
return ret
|
||||
})
|
||||
}
|
||||
//
|
||||
/**
|
||||
* Utility function to generate {@link HookFunction} from a RPC
|
||||
* @param rpc The RPC to transform
|
||||
* @returns A {@link HookFunction}
|
||||
*/
|
||||
const hookGenerator = (rpc:T.HookRPC<any>): T.HookInfo['generator'] => {
|
||||
const argsArr = extractArgs(rpc.hook)
|
||||
argsArr.pop()
|
||||
@@ -99,11 +103,19 @@ const hookGenerator = (rpc:T.HookRPC<any>): T.HookInfo['generator'] => {
|
||||
}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a string list of parameters from a function
|
||||
* @param f The source function
|
||||
*/
|
||||
const extractArgs = (f:Function):T.Arg[] => {
|
||||
let fn
|
||||
return (fn = String(f)).substr(0, fn.indexOf(")")).substr(fn.indexOf("(")+1).split(",")
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple utility function to create basic {@link SubscriptionResponse}
|
||||
* @param uuid optional uuid to use, otherwise defaults to uuid/v4
|
||||
*/
|
||||
export function makeSubResponse(uuid?:string):SubscriptionResponse{
|
||||
return {
|
||||
result: "Success",
|
||||
|
||||
Reference in New Issue
Block a user