Browse Source

doc

master
peter 5 years ago
parent
commit
2bab836faf

+ 27
- 19
docs/classes/_backend_.rpcserver.md View File

2
 
2
 
3
 # Class: RPCServer <**SubResType**>
3
 # Class: RPCServer <**SubResType**>
4
 
4
 
5
+A Websocket-server-on-steroids with built-in RPC capabilities
6
+
5
 ## Type parameters
7
 ## Type parameters
6
 
8
 
7
 ▪ **SubResType**
9
 ▪ **SubResType**
41
 
43
 
42
 ###  constructor
44
 ###  constructor
43
 
45
 
44
-\+ **new RPCServer**(`port`: number, `exporters`: [Exporter](../interfaces/_interfaces_.exporter.md)‹SubResType›[], `conf`: T.SocketConf): *[RPCServer](_backend_.rpcserver.md)*
46
+\+ **new RPCServer**(`port`: number, `exporters`: [RPCExporter](../interfaces/_interfaces_.rpcexporter.md)‹SubResType›[], `conf`: T.SocketConf): *[RPCServer](_backend_.rpcserver.md)*
45
 
47
 
46
-Defined in Backend.ts:19
48
+Defined in Backend.ts:22
49
+
50
+**`throws`** On RPC with no name
47
 
51
 
48
 **Parameters:**
52
 **Parameters:**
49
 
53
 
50
-Name | Type | Default |
51
------- | ------ | ------ |
52
-`port` | number | - |
53
-`exporters` | [Exporter](../interfaces/_interfaces_.exporter.md)‹SubResType›[] |  [] |
54
-`conf` | T.SocketConf |   {} |
54
+Name | Type | Default | Description |
55
+------ | ------ | ------ | ------ |
56
+`port` | number | - | The port to listen on |
57
+`exporters` | [RPCExporter](../interfaces/_interfaces_.rpcexporter.md)‹SubResType›[] |  [] | A list of [RPCExporter](../interfaces/_interfaces_.rpcexporter.md) to publish |
58
+`conf` | T.SocketConf |   {} | A [SocketConf](../modules/_types_.md#socketconf) object with optional settings  |
55
 
59
 
56
 **Returns:** *[RPCServer](_backend_.rpcserver.md)*
60
 **Returns:** *[RPCServer](_backend_.rpcserver.md)*
57
 
61
 
61
 
65
 
62
 • **closeHandler**: *T.CloseHandler*
66
 • **closeHandler**: *T.CloseHandler*
63
 
67
 
64
-Defined in Backend.ts:17
68
+Defined in Backend.ts:20
65
 
69
 
66
 ___
70
 ___
67
 
71
 
69
 
73
 
70
 • **connectionHandler**: *T.ConnectionHandler*
74
 • **connectionHandler**: *T.ConnectionHandler*
71
 
75
 
72
-Defined in Backend.ts:19
76
+Defined in Backend.ts:22
73
 
77
 
74
 ___
78
 ___
75
 
79
 
77
 
81
 
78
 • **errorHandler**: *T.ErrorHandler*
82
 • **errorHandler**: *T.ErrorHandler*
79
 
83
 
80
-Defined in Backend.ts:18
84
+Defined in Backend.ts:21
81
 
85
 
82
 ___
86
 ___
83
 
87
 
84
 ### `Private` exporters
88
 ### `Private` exporters
85
 
89
 
86
-• **exporters**: *[Exporter](../interfaces/_interfaces_.exporter.md)‹SubResType›[]*
90
+• **exporters**: *[RPCExporter](../interfaces/_interfaces_.rpcexporter.md)‹SubResType›[]*
91
+
92
+Defined in Backend.ts:32
87
 
93
 
88
-Defined in Backend.ts:23
94
+A list of [RPCExporter](../interfaces/_interfaces_.rpcexporter.md) to publish
89
 
95
 
90
 ___
96
 ___
91
 
97
 
93
 
99
 
94
 • **io**: *any* =  bsock.createServer()
100
 • **io**: *any* =  bsock.createServer()
95
 
101
 
96
-Defined in Backend.ts:15
102
+Defined in Backend.ts:18
97
 
103
 
98
 ___
104
 ___
99
 
105
 
101
 
107
 
102
 • **port**: *number*
108
 • **port**: *number*
103
 
109
 
104
-Defined in Backend.ts:22
110
+Defined in Backend.ts:31
111
+
112
+The port to listen on
105
 
113
 
106
 ___
114
 ___
107
 
115
 
109
 
117
 
110
 • **visibility**: *T.Visibility*
118
 • **visibility**: *T.Visibility*
111
 
119
 
112
-Defined in Backend.ts:16
120
+Defined in Backend.ts:19
113
 
121
 
114
 ___
122
 ___
115
 
123
 
117
 
125
 
118
 • **ws**: *Server* =  http.createServer()
126
 • **ws**: *Server* =  http.createServer()
119
 
127
 
120
-Defined in Backend.ts:14
128
+Defined in Backend.ts:17
121
 
129
 
122
 ## Methods
130
 ## Methods
123
 
131
 
127
 
135
 
128
 *Implementation of [Destroyable](../interfaces/_interfaces_.destroyable.md)*
136
 *Implementation of [Destroyable](../interfaces/_interfaces_.destroyable.md)*
129
 
137
 
130
-Defined in Backend.ts:80
138
+Defined in Backend.ts:89
131
 
139
 
132
 **Returns:** *Promise‹void›*
140
 **Returns:** *Promise‹void›*
133
 
141
 
137
 
145
 
138
 ▸ **initRPCs**(`socket`: [Socket](../interfaces/_interfaces_.socket.md)): *void*
146
 ▸ **initRPCs**(`socket`: [Socket](../interfaces/_interfaces_.socket.md)): *void*
139
 
147
 
140
-Defined in Backend.ts:73
148
+Defined in Backend.ts:82
141
 
149
 
142
 **Parameters:**
150
 **Parameters:**
143
 
151
 
153
 
161
 
154
 ▸ **startWebsocket**(): *void*
162
 ▸ **startWebsocket**(): *void*
155
 
163
 
156
-Defined in Backend.ts:57
164
+Defined in Backend.ts:66
157
 
165
 
158
 **Returns:** *void*
166
 **Returns:** *void*

+ 80
- 42
docs/classes/_frontend_.rpcsocket.md View File

2
 
2
 
3
 # Class: RPCSocket
3
 # Class: RPCSocket
4
 
4
 
5
+A websocket-on-steroids with built-in RPC capabilities
6
+
5
 ## Hierarchy
7
 ## Hierarchy
6
 
8
 
7
 * **RPCSocket**
9
 * **RPCSocket**
43
 
45
 
44
 \+ **new RPCSocket**(`port`: number, `server`: string, `tls`: boolean): *[RPCSocket](_frontend_.rpcsocket.md)*
46
 \+ **new RPCSocket**(`port`: number, `server`: string, `tls`: boolean): *[RPCSocket](_frontend_.rpcsocket.md)*
45
 
47
 
46
-Defined in Frontend.ts:15
48
+Defined in Frontend.ts:20
47
 
49
 
48
 **Parameters:**
50
 **Parameters:**
49
 
51
 
50
-Name | Type | Default |
51
------- | ------ | ------ |
52
-`port` | number | - |
53
-`server` | string | - |
54
-`tls` | boolean | false |
52
+Name | Type | Default | Description |
53
+------ | ------ | ------ | ------ |
54
+`port` | number | - | Port to connect to |
55
+`server` | string | - | Server address |
56
+`tls` | boolean | false | @default false use TLS  |
55
 
57
 
56
 **Returns:** *[RPCSocket](_frontend_.rpcsocket.md)*
58
 **Returns:** *[RPCSocket](_frontend_.rpcsocket.md)*
57
 
59
 
63
 
65
 
64
 *Implementation of [Socket](../interfaces/_interfaces_.socket.md).[port](../interfaces/_interfaces_.socket.md#port)*
66
 *Implementation of [Socket](../interfaces/_interfaces_.socket.md).[port](../interfaces/_interfaces_.socket.md#port)*
65
 
67
 
66
-Defined in Frontend.ts:16
68
+Defined in Frontend.ts:28
69
+
70
+Port to connect to
67
 
71
 
68
 ___
72
 ___
69
 
73
 
71
 
75
 
72
 • **server**: *string*
76
 • **server**: *string*
73
 
77
 
74
-Defined in Frontend.ts:16
78
+Defined in Frontend.ts:28
79
+
80
+Server address
75
 
81
 
76
 ___
82
 ___
77
 
83
 
79
 
85
 
80
 • **socket**: *[Socket](../interfaces/_interfaces_.socket.md)*
86
 • **socket**: *[Socket](../interfaces/_interfaces_.socket.md)*
81
 
87
 
82
-Defined in Frontend.ts:15
88
+Defined in Frontend.ts:20
83
 
89
 
84
 ___
90
 ___
85
 
91
 
87
 
93
 
88
 • **tls**: *boolean*
94
 • **tls**: *boolean*
89
 
95
 
90
-Defined in Frontend.ts:16
96
+Defined in Frontend.ts:28
97
+
98
+@default false use TLS
91
 
99
 
92
 ## Methods
100
 ## Methods
93
 
101
 
95
 
103
 
96
 ▸ **call**(`rpcname`: T.Name, ...`args`: T.Any[]): *Promise‹T.Any›*
104
 ▸ **call**(`rpcname`: T.Name, ...`args`: T.Any[]): *Promise‹T.Any›*
97
 
105
 
98
-Defined in Frontend.ts:40
106
+Defined in Frontend.ts:77
107
+
108
+Trigger a hooked handler on the server
99
 
109
 
100
 **Parameters:**
110
 **Parameters:**
101
 
111
 
102
-Name | Type |
103
------- | ------ |
104
-`rpcname` | T.Name |
105
-`...args` | T.Any[] |
112
+Name | Type | Description |
113
+------ | ------ | ------ |
114
+`rpcname` | T.Name | The function to call |
115
+`...args` | T.Any[] | other arguments  |
106
 
116
 
107
 **Returns:** *Promise‹T.Any›*
117
 **Returns:** *Promise‹T.Any›*
108
 
118
 
112
 
122
 
113
 ▸ **callGenerator**(`fnName`: T.Name, `fnArgs`: T.Arg[]): *T.AsyncFunction*
123
 ▸ **callGenerator**(`fnName`: T.Name, `fnArgs`: T.Arg[]): *T.AsyncFunction*
114
 
124
 
115
-Defined in Frontend.ts:73
125
+Defined in Frontend.ts:126
126
+
127
+Utility [AsyncFunction](../modules/_types_.md#asyncfunction) generator
116
 
128
 
117
 **Parameters:**
129
 **Parameters:**
118
 
130
 
119
-Name | Type |
120
------- | ------ |
121
-`fnName` | T.Name |
122
-`fnArgs` | T.Arg[] |
131
+Name | Type | Description |
132
+------ | ------ | ------ |
133
+`fnName` | T.Name | The function name |
134
+`fnArgs` | T.Arg[] | A string-list of parameters  |
123
 
135
 
124
 **Returns:** *T.AsyncFunction*
136
 **Returns:** *T.AsyncFunction*
125
 
137
 
131
 
143
 
132
 *Implementation of [Socket](../interfaces/_interfaces_.socket.md)*
144
 *Implementation of [Socket](../interfaces/_interfaces_.socket.md)*
133
 
145
 
134
-Defined in Frontend.ts:36
146
+Defined in Frontend.ts:68
147
+
148
+Closes the socket. It may attempt to reconnect.
135
 
149
 
136
 **Returns:** *void*
150
 **Returns:** *void*
137
 
151
 
141
 
155
 
142
 ▸ **connect**(): *Promise‹void›*
156
 ▸ **connect**(): *Promise‹void›*
143
 
157
 
144
-Defined in Frontend.ts:48
158
+Defined in Frontend.ts:93
159
+
160
+Connects to the server and attaches available RPCs to this object
145
 
161
 
146
 **Returns:** *Promise‹void›*
162
 **Returns:** *Promise‹void›*
147
 
163
 
153
 
169
 
154
 *Implementation of [Socket](../interfaces/_interfaces_.socket.md)*
170
 *Implementation of [Socket](../interfaces/_interfaces_.socket.md)*
155
 
171
 
156
-Defined in Frontend.ts:32
172
+Defined in Frontend.ts:61
173
+
174
+Destroys the socket
157
 
175
 
158
 **Returns:** *void*
176
 **Returns:** *void*
159
 
177
 
161
 
179
 
162
 ###  fire
180
 ###  fire
163
 
181
 
164
-▸ **fire**(`rpcname`: T.Name, ...`args`: T.Any[]): *Promise‹T.Any›*
182
+▸ **fire**(`rpcname`: T.Name, ...`args`: T.Any[]): *Promise‹void›*
183
+
184
+Defined in Frontend.ts:86
165
 
185
 
166
-Defined in Frontend.ts:44
186
+An alternative to call that does not wait for confirmation and doesn't return a value.
167
 
187
 
168
 **Parameters:**
188
 **Parameters:**
169
 
189
 
170
-Name | Type |
171
------- | ------ |
172
-`rpcname` | T.Name |
173
-`...args` | T.Any[] |
190
+Name | Type | Description |
191
+------ | ------ | ------ |
192
+`rpcname` | T.Name | The function to call |
193
+`...args` | T.Any[] | other arguments  |
174
 
194
 
175
-**Returns:** *Promise‹T.Any›*
195
+**Returns:** *Promise‹void›*
176
 
196
 
177
 ___
197
 ___
178
 
198
 
180
 
200
 
181
 ▸ **hook**(`name`: T.Name, `handler`: function): *[Socket](../interfaces/_interfaces_.socket.md)*
201
 ▸ **hook**(`name`: T.Name, `handler`: function): *[Socket](../interfaces/_interfaces_.socket.md)*
182
 
202
 
183
-Defined in Frontend.ts:20
203
+Defined in Frontend.ts:37
204
+
205
+Hooks a handler to a function name. Use [call](_frontend_.rpcsocket.md#call) to trigger it.
184
 
206
 
185
 **Parameters:**
207
 **Parameters:**
186
 
208
 
187
 ▪ **name**: *T.Name*
209
 ▪ **name**: *T.Name*
188
 
210
 
211
+The function name to listen on
212
+
189
 ▪ **handler**: *function*
213
 ▪ **handler**: *function*
190
 
214
 
215
+The handler to attach
216
+
191
 ▸ (...`args`: any[]): *any | Promise‹any›*
217
 ▸ (...`args`: any[]): *any | Promise‹any›*
192
 
218
 
193
 **Parameters:**
219
 **Parameters:**
204
 
230
 
205
 ▸ **hookGenerator**(`fnName`: T.Name, `fnArgs`: T.Arg[]): *T.HookFunction*
231
 ▸ **hookGenerator**(`fnName`: T.Name, `fnArgs`: T.Arg[]): *T.HookFunction*
206
 
232
 
207
-Defined in Frontend.ts:79
233
+Defined in Frontend.ts:137
234
+
235
+Utility [HookFunction](../modules/_types_.md#hookfunction) generator
208
 
236
 
209
 **Parameters:**
237
 **Parameters:**
210
 
238
 
211
-Name | Type |
212
------- | ------ |
213
-`fnName` | T.Name |
214
-`fnArgs` | T.Arg[] |
239
+Name | Type | Description |
240
+------ | ------ | ------ |
241
+`fnName` | T.Name | The function name |
242
+`fnArgs` | T.Arg[] | A string-list of parameters  |
215
 
243
 
216
 **Returns:** *T.HookFunction*
244
 **Returns:** *T.HookFunction*
217
 
245
 
221
 
249
 
222
 ▸ **info**(): *Promise‹any›*
250
 ▸ **info**(): *Promise‹any›*
223
 
251
 
224
-Defined in Frontend.ts:69
252
+Defined in Frontend.ts:117
253
+
254
+Get a list of available RPCs from the server
225
 
255
 
226
 **Returns:** *Promise‹any›*
256
 **Returns:** *Promise‹any›*
227
 
257
 
231
 
261
 
232
 ▸ **on**(`type`: "error" | "close", `f`: function): *[Socket](../interfaces/_interfaces_.socket.md)*
262
 ▸ **on**(`type`: "error" | "close", `f`: function): *[Socket](../interfaces/_interfaces_.socket.md)*
233
 
263
 
234
-Defined in Frontend.ts:28
264
+Defined in Frontend.ts:54
265
+
266
+Attach a listener to error or close events
235
 
267
 
236
 **Parameters:**
268
 **Parameters:**
237
 
269
 
238
 ▪ **type**: *"error" | "close"*
270
 ▪ **type**: *"error" | "close"*
239
 
271
 
272
+'error' or 'close'
273
+
240
 ▪ **f**: *function*
274
 ▪ **f**: *function*
241
 
275
 
276
+The listener to attach
277
+
242
 ▸ (`e?`: any): *void*
278
 ▸ (`e?`: any): *void*
243
 
279
 
244
 **Parameters:**
280
 **Parameters:**
255
 
291
 
256
 ▸ **unhook**(`name`: T.Name): *[Socket](../interfaces/_interfaces_.socket.md)*
292
 ▸ **unhook**(`name`: T.Name): *[Socket](../interfaces/_interfaces_.socket.md)*
257
 
293
 
258
-Defined in Frontend.ts:24
294
+Defined in Frontend.ts:45
295
+
296
+Removes a [hook](_frontend_.rpcsocket.md#hook) listener by name.
259
 
297
 
260
 **Parameters:**
298
 **Parameters:**
261
 
299
 
262
-Name | Type |
263
------- | ------ |
264
-`name` | T.Name |
300
+Name | Type | Description |
301
+------ | ------ | ------ |
302
+`name` | T.Name | The function name  |
265
 
303
 
266
 **Returns:** *[Socket](../interfaces/_interfaces_.socket.md)*
304
 **Returns:** *[Socket](../interfaces/_interfaces_.socket.md)*

+ 1
- 1
docs/interfaces/_interfaces_.destroyable.md View File

24
 
24
 
25
 ▸ **destroy**(): *void*
25
 ▸ **destroy**(): *void*
26
 
26
 
27
-Defined in Interfaces.ts:20
27
+Defined in Interfaces.ts:26
28
 
28
 
29
 **Returns:** *void*
29
 **Returns:** *void*

+ 0
- 39
docs/interfaces/_interfaces_.exporter.md View File

1
-[rpclibrary](../README.md) › [Globals](../globals.md) › ["Interfaces"](../modules/_interfaces_.md) › [Exporter](_interfaces_.exporter.md)
2
-
3
-# Interface: Exporter <**T**>
4
-
5
-## Type parameters
6
-
7
-▪ **T**
8
-
9
-## Hierarchy
10
-
11
-* **Exporter**
12
-
13
-## Index
14
-
15
-### Properties
16
-
17
-* [name](_interfaces_.exporter.md#name)
18
-
19
-### Methods
20
-
21
-* [exportRPCs](_interfaces_.exporter.md#exportrpcs)
22
-
23
-## Properties
24
-
25
-###  name
26
-
27
-• **name**: *T.Name*
28
-
29
-Defined in Interfaces.ts:5
30
-
31
-## Methods
32
-
33
-###  exportRPCs
34
-
35
-▸ **exportRPCs**(): *T.RPC‹T›[]*
36
-
37
-Defined in Interfaces.ts:6
38
-
39
-**Returns:** *T.RPC‹T›[]*

+ 10
- 8
docs/interfaces/_interfaces_.socket.md View File

2
 
2
 
3
 # Interface: Socket
3
 # Interface: Socket
4
 
4
 
5
+Generic socket interface that can apply to bsock as well as RPCSocket
6
+
5
 ## Hierarchy
7
 ## Hierarchy
6
 
8
 
7
 * [Destroyable](_interfaces_.destroyable.md)
9
 * [Destroyable](_interfaces_.destroyable.md)
34
 
36
 
35
 • **call**: *function*
37
 • **call**: *function*
36
 
38
 
37
-Defined in Interfaces.ts:13
39
+Defined in Interfaces.ts:19
38
 
40
 
39
 #### Type declaration:
41
 #### Type declaration:
40
 
42
 
53
 
55
 
54
 • **fire**: *function*
56
 • **fire**: *function*
55
 
57
 
56
-Defined in Interfaces.ts:14
58
+Defined in Interfaces.ts:20
57
 
59
 
58
 #### Type declaration:
60
 #### Type declaration:
59
 
61
 
72
 
74
 
73
 • **hook**: *function*
75
 • **hook**: *function*
74
 
76
 
75
-Defined in Interfaces.ts:11
77
+Defined in Interfaces.ts:17
76
 
78
 
77
 #### Type declaration:
79
 #### Type declaration:
78
 
80
 
98
 
100
 
99
 • **on**: *T.OnFunction*
101
 • **on**: *T.OnFunction*
100
 
102
 
101
-Defined in Interfaces.ts:15
103
+Defined in Interfaces.ts:21
102
 
104
 
103
 ___
105
 ___
104
 
106
 
106
 
108
 
107
 • **port**: *number*
109
 • **port**: *number*
108
 
110
 
109
-Defined in Interfaces.ts:10
111
+Defined in Interfaces.ts:16
110
 
112
 
111
 ___
113
 ___
112
 
114
 
114
 
116
 
115
 • **unhook**: *function*
117
 • **unhook**: *function*
116
 
118
 
117
-Defined in Interfaces.ts:12
119
+Defined in Interfaces.ts:18
118
 
120
 
119
 #### Type declaration:
121
 #### Type declaration:
120
 
122
 
132
 
134
 
133
 ▸ **close**(): *void*
135
 ▸ **close**(): *void*
134
 
136
 
135
-Defined in Interfaces.ts:16
137
+Defined in Interfaces.ts:22
136
 
138
 
137
 **Returns:** *void*
139
 **Returns:** *void*
138
 
140
 
144
 
146
 
145
 *Inherited from [Destroyable](_interfaces_.destroyable.md).[destroy](_interfaces_.destroyable.md#destroy)*
147
 *Inherited from [Destroyable](_interfaces_.destroyable.md).[destroy](_interfaces_.destroyable.md#destroy)*
146
 
148
 
147
-Defined in Interfaces.ts:20
149
+Defined in Interfaces.ts:26
148
 
150
 
149
 **Returns:** *void*
151
 **Returns:** *void*

+ 6
- 4
docs/modules/_frontend_.md View File

18
 
18
 
19
 ▸ **stripAfterEquals**(`str`: string): *string*
19
 ▸ **stripAfterEquals**(`str`: string): *string*
20
 
20
 
21
-Defined in Frontend.ts:9
21
+Defined in Frontend.ts:12
22
+
23
+Utility function to strip parameters like "a = 3" of their defaults
22
 
24
 
23
 **Parameters:**
25
 **Parameters:**
24
 
26
 
25
-Name | Type |
26
------- | ------ |
27
-`str` | string |
27
+Name | Type | Description |
28
+------ | ------ | ------ |
29
+`str` | string | The parameter to modify  |
28
 
30
 
29
 **Returns:** *string*
31
 **Returns:** *string*

+ 1
- 1
docs/modules/_interfaces_.md View File

7
 ### Interfaces
7
 ### Interfaces
8
 
8
 
9
 * [Destroyable](../interfaces/_interfaces_.destroyable.md)
9
 * [Destroyable](../interfaces/_interfaces_.destroyable.md)
10
-* [Exporter](../interfaces/_interfaces_.exporter.md)
10
+* [RPCExporter](../interfaces/_interfaces_.rpcexporter.md)
11
 * [Socket](../interfaces/_interfaces_.socket.md)
11
 * [Socket](../interfaces/_interfaces_.socket.md)

+ 23
- 15
docs/modules/_utils_.md View File

18
 
18
 
19
 ▸ **extractArgs**(`f`: Function): *T.Arg[]*
19
 ▸ **extractArgs**(`f`: Function): *T.Arg[]*
20
 
20
 
21
-Defined in Utils.ts:102
21
+Defined in Utils.ts:110
22
+
23
+Extract a string list of parameters from a function
22
 
24
 
23
 **Parameters:**
25
 **Parameters:**
24
 
26
 
25
-Name | Type |
26
------- | ------ |
27
-`f` | Function |
27
+Name | Type | Description |
28
+------ | ------ | ------ |
29
+`f` | Function | The source function  |
28
 
30
 
29
 **Returns:** *T.Arg[]*
31
 **Returns:** *T.Arg[]*
30
 
32
 
34
 
36
 
35
 ▸ **hookGenerator**(`rpc`: T.HookRPC‹any›): *function*
37
 ▸ **hookGenerator**(`rpc`: T.HookRPC‹any›): *function*
36
 
38
 
37
-Defined in Utils.ts:81
39
+Defined in Utils.ts:85
40
+
41
+Utility function to generate [HookFunction](_types_.md#hookfunction) from a RPC
38
 
42
 
39
 **Parameters:**
43
 **Parameters:**
40
 
44
 
41
-Name | Type |
42
------- | ------ |
43
-`rpc` | T.HookRPC‹any› |
45
+Name | Type | Description |
46
+------ | ------ | ------ |
47
+`rpc` | T.HookRPC‹any› | The RPC to transform |
44
 
48
 
45
 **Returns:** *function*
49
 **Returns:** *function*
46
 
50
 
51
+A [HookFunction](_types_.md#hookfunction)
52
+
47
 ▸ (`socket?`: I.Socket): *[HookFunction](_types_.md#hookfunction)‹T›*
53
 ▸ (`socket?`: I.Socket): *[HookFunction](_types_.md#hookfunction)‹T›*
48
 
54
 
49
 **Parameters:**
55
 **Parameters:**
58
 
64
 
59
 ▸ **makeSubResponse**(`uuid?`: undefined | string): *[SubscriptionResponse](_types_.md#subscriptionresponse)*
65
 ▸ **makeSubResponse**(`uuid?`: undefined | string): *[SubscriptionResponse](_types_.md#subscriptionresponse)*
60
 
66
 
61
-Defined in Utils.ts:107
67
+Defined in Utils.ts:119
68
+
69
+Simple utility function to create basic [SubscriptionResponse](_types_.md#subscriptionresponse)
62
 
70
 
63
 **Parameters:**
71
 **Parameters:**
64
 
72
 
65
-Name | Type |
66
------- | ------ |
67
-`uuid?` | undefined &#124; string |
73
+Name | Type | Description |
74
+------ | ------ | ------ |
75
+`uuid?` | undefined &#124; string | optional uuid to use, otherwise defaults to uuid/v4  |
68
 
76
 
69
 **Returns:** *[SubscriptionResponse](_types_.md#subscriptionresponse)*
77
 **Returns:** *[SubscriptionResponse](_types_.md#subscriptionresponse)*
70
 
78
 
72
 
80
 
73
 ###  rpcHooker
81
 ###  rpcHooker
74
 
82
 
75
-▸ **rpcHooker**<**SubResT**>(`socket`: [Socket](../interfaces/_interfaces_.socket.md), `exporter`: [Exporter](../interfaces/_interfaces_.exporter.md)‹SubResT›, `makeUnique`: boolean): *T.ExtendedRpcInfo[]*
83
+▸ **rpcHooker**<**SubResT**>(`socket`: [Socket](../interfaces/_interfaces_.socket.md), `exporter`: [RPCExporter](../interfaces/_interfaces_.rpcexporter.md)‹SubResT›, `makeUnique`: boolean): *T.ExtendedRpcInfo[]*
76
 
84
 
77
 Defined in Utils.ts:59
85
 Defined in Utils.ts:59
78
 
86
 
79
-Utility function to apply the RPCs of an [Exporter](../interfaces/_interfaces_.exporter.md).
87
+Utility function to apply the RPCs of an [RPCExporter](../interfaces/_interfaces_.rpcexporter.md).
80
 
88
 
81
 **Type parameters:**
89
 **Type parameters:**
82
 
90
 
87
 Name | Type | Default | Description |
95
 Name | Type | Default | Description |
88
 ------ | ------ | ------ | ------ |
96
 ------ | ------ | ------ | ------ |
89
 `socket` | [Socket](../interfaces/_interfaces_.socket.md) | - | The websocket (implementation: bsock) to hook on |
97
 `socket` | [Socket](../interfaces/_interfaces_.socket.md) | - | The websocket (implementation: bsock) to hook on |
90
-`exporter` | [Exporter](../interfaces/_interfaces_.exporter.md)‹SubResT› | - | The exporter |
98
+`exporter` | [RPCExporter](../interfaces/_interfaces_.rpcexporter.md)‹SubResT› | - | The exporter |
91
 `makeUnique` | boolean | true | @default true Attach a suffix to RPC names  |
99
 `makeUnique` | boolean | true | @default true Attach a suffix to RPC names  |
92
 
100
 
93
 **Returns:** *T.ExtendedRpcInfo[]*
101
 **Returns:** *T.ExtendedRpcInfo[]*

+ 10
- 1
src/Backend.ts View File

7
 import * as U from './Utils'; 
7
 import * as U from './Utils'; 
8
 import * as I from './Interfaces';
8
 import * as I from './Interfaces';
9
 
9
 
10
+/**
11
+ * A Websocket-server-on-steroids with built-in RPC capabilities
12
+ */
10
 export class RPCServer<
13
 export class RPCServer<
11
     SubResType = {}
14
     SubResType = {}
12
 > implements I.Destroyable{
15
 > implements I.Destroyable{
18
     private errorHandler: T.ErrorHandler
21
     private errorHandler: T.ErrorHandler
19
     private connectionHandler: T.ConnectionHandler
22
     private connectionHandler: T.ConnectionHandler
20
 
23
 
24
+    /**
25
+     * @throws On RPC with no name
26
+     * @param port The port to listen on
27
+     * @param exporters A list of {@link RPCExporter} to publish
28
+     * @param conf A {@link SocketConf} object with optional settings
29
+     */
21
     constructor(
30
     constructor(
22
         private port:number,
31
         private port:number,
23
-        private exporters: I.Exporter<SubResType>[] = [],
32
+        private exporters: I.RPCExporter<SubResType>[] = [],
24
         conf: T.SocketConf =  {}
33
         conf: T.SocketConf =  {}
25
     ){
34
     ){
26
         
35
         

+ 63
- 5
src/Frontend.ts View File

5
 import * as T from './Types'; 
5
 import * as T from './Types'; 
6
 import * as I from './Interfaces';
6
 import * as I from './Interfaces';
7
 
7
 
8
-//fix args with defaults like "force = true" -> "force"
9
-function stripAfterEquals(str:string){
8
+/**
9
+ * Utility function to strip parameters like "a = 3" of their defaults
10
+ * @param str The parameter to modify
11
+ */
12
+function stripAfterEquals(str:string):string{
10
     return str.split("=")[0]
13
     return str.split("=")[0]
11
 }
14
 }
12
 
15
 
16
+/**
17
+ * A websocket-on-steroids with built-in RPC capabilities
18
+ */
13
 export class RPCSocket implements I.Socket{
19
 export class RPCSocket implements I.Socket{
14
-
15
     private socket: I.Socket
20
     private socket: I.Socket
21
+
22
+    /**
23
+     * 
24
+     * @param port Port to connect to
25
+     * @param server Server address
26
+     * @param tls @default false use TLS
27
+     */
16
     constructor(public port:number, private server: string, private tls: boolean = false){
28
     constructor(public port:number, private server: string, private tls: boolean = false){
17
         Object.defineProperty(this, 'socket', {value: undefined, writable: true})
29
         Object.defineProperty(this, 'socket', {value: undefined, writable: true})
18
     }
30
     }
19
 
31
 
32
+    /**
33
+     * Hooks a handler to a function name. Use {@link call} to trigger it.
34
+     * @param name The function name to listen on
35
+     * @param handler The handler to attach
36
+     */
20
     public hook(name: T.Name, handler: (...args:any[]) => any | Promise<any>){
37
     public hook(name: T.Name, handler: (...args:any[]) => any | Promise<any>){
21
         return this.socket.hook(name, handler)
38
         return this.socket.hook(name, handler)
22
     }
39
     }
23
 
40
 
41
+    /**
42
+     * Removes a {@link hook} listener by name.
43
+     * @param name The function name 
44
+     */
24
     public unhook(name: T.Name){
45
     public unhook(name: T.Name){
25
         return this.socket.unhook(name)
46
         return this.socket.unhook(name)
26
     }
47
     }
27
 
48
 
49
+    /**
50
+     * Attach a listener to error or close events
51
+     * @param type 'error' or 'close'
52
+     * @param f The listener to attach
53
+     */
28
     public on(type: "error" | "close", f: (e?: any) => void){
54
     public on(type: "error" | "close", f: (e?: any) => void){
29
         return this.socket.on(type, f)
55
         return this.socket.on(type, f)
30
     }
56
     }
31
 
57
 
58
+    /**
59
+     * Destroys the socket
60
+     */
32
     public destroy(){
61
     public destroy(){
33
         return this.socket.destroy()
62
         return this.socket.destroy()
34
     }
63
     }
35
 
64
 
65
+    /**
66
+     * Closes the socket. It may attempt to reconnect.
67
+     */
36
     public close(){
68
     public close(){
37
         return this.socket.close()
69
         return this.socket.close()
38
     }
70
     }
39
 
71
 
72
+    /**
73
+     * Trigger a hooked handler on the server
74
+     * @param rpcname The function to call
75
+     * @param args other arguments
76
+     */
40
     public async call (rpcname: T.Name, ...args: T.Any[]) : Promise<T.Any>{
77
     public async call (rpcname: T.Name, ...args: T.Any[]) : Promise<T.Any>{
41
         return await this.socket.call.apply(this.socket, [rpcname, ...args])
78
         return await this.socket.call.apply(this.socket, [rpcname, ...args])
42
     }
79
     }
43
 
80
 
44
-    public async fire(rpcname: T.Name, ...args: T.Any[]) : Promise<T.Any>{
45
-        return await this.socket.fire.apply(this.socket, [rpcname, ...args])
81
+    /**
82
+     * An alternative to call that does not wait for confirmation and doesn't return a value.
83
+     * @param rpcname The function to call
84
+     * @param args other arguments
85
+     */
86
+    public async fire(rpcname: T.Name, ...args: T.Any[]) : Promise<void>{
87
+        await this.socket.fire.apply(this.socket, [rpcname, ...args])
46
     }
88
     }
47
     
89
     
90
+    /**
91
+     * Connects to the server and attaches available RPCs to this object
92
+     */
48
     public async connect(){
93
     public async connect(){
49
         this.socket = await bsock.connect(this.port, this.server, this.tls)
94
         this.socket = await bsock.connect(this.port, this.server, this.tls)
50
 
95
 
66
         })
111
         })
67
     }
112
     }
68
 
113
 
114
+    /**
115
+     * Get a list of available RPCs from the server
116
+     */
69
     public async info(){
117
     public async info(){
70
         return await this.socket.call('info')
118
         return await this.socket.call('info')
71
     }
119
     }
72
 
120
 
121
+    /**
122
+     * Utility {@link AsyncFunction} generator
123
+     * @param fnName The function name
124
+     * @param fnArgs A string-list of parameters
125
+     */
73
     private callGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.AsyncFunction{
126
     private callGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.AsyncFunction{
74
         const headerArgs = fnArgs.join(",")
127
         const headerArgs = fnArgs.join(",")
75
         const argParams = fnArgs.map(stripAfterEquals).join(",")
128
         const argParams = fnArgs.map(stripAfterEquals).join(",")
76
         return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", '+argParams+')} )()' )
129
         return eval( '( () => async ('+headerArgs+') => { return await this.socket.call("'+fnName+'", '+argParams+')} )()' )
77
     }
130
     }
78
 
131
 
132
+    /**
133
+     * Utility {@link HookFunction} generator
134
+     * @param fnName The function name
135
+     * @param fnArgs A string-list of parameters
136
+     */
79
     private hookGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.HookFunction{
137
     private hookGenerator(fnName: T.Name, fnArgs:T.Arg[]): T.HookFunction{
80
         const headerArgs = fnArgs.join(",")
138
         const headerArgs = fnArgs.join(",")
81
         const argParams = fnArgs.map(stripAfterEquals).join(",")
139
         const argParams = fnArgs.map(stripAfterEquals).join(",")

+ 7
- 1
src/Interfaces.ts View File

1
 import * as T from "./Types";
1
 import * as T from "./Types";
2
 import * as I from "./Interfaces"
2
 import * as I from "./Interfaces"
3
 
3
 
4
-export interface Exporter<T = {}>{
4
+/**
5
+ * Interface for all classes that may export RPCs
6
+ */
7
+export interface RPCExporter<T = {}>{
5
     name: T.Name
8
     name: T.Name
6
     exportRPCs() : T.RPC<T>[]
9
     exportRPCs() : T.RPC<T>[]
7
 }
10
 }
8
 
11
 
12
+/**
13
+ * Generic socket interface that can apply to bsock as well as RPCSocket
14
+ */
9
 export interface Socket extends Destroyable {
15
 export interface Socket extends Destroyable {
10
     port: number
16
     port: number
11
     hook: (rpcname: T.Name, handler: (...args:any[]) => any | Promise<any>) => I.Socket
17
     hook: (rpcname: T.Name, handler: (...args:any[]) => any | Promise<any>) => I.Socket

+ 16
- 4
src/Utils.ts View File

8
  * Translate an RPC to RPCInfo for serialization.
8
  * Translate an RPC to RPCInfo for serialization.
9
  * @param rpc The RPC to transform
9
  * @param rpc The RPC to transform
10
  * @param owner The owning RPC group's name
10
  * @param owner The owning RPC group's name
11
- * @throws {Errror} Error on RPC without name property
11
+ * @throws Error on RPC without name property
12
  */
12
  */
13
 export const rpcToRpcinfo = <SubResT = {}>(rpc : T.RPC<SubResT>, owner: T.Owner):T.RpcInfo => {
13
 export const rpcToRpcinfo = <SubResT = {}>(rpc : T.RPC<SubResT>, owner: T.Owner):T.RpcInfo => {
14
     switch (typeof rpc){
14
     switch (typeof rpc){
51
 }
51
 }
52
 
52
 
53
 /**
53
 /**
54
- * Utility function to apply the RPCs of an {@link Exporter}.
54
+ * Utility function to apply the RPCs of an {@link RPCExporter}.
55
  * @param socket The websocket (implementation: bsock) to hook on
55
  * @param socket The websocket (implementation: bsock) to hook on
56
  * @param exporter The exporter
56
  * @param exporter The exporter
57
  * @param makeUnique @default true Attach a suffix to RPC names
57
  * @param makeUnique @default true Attach a suffix to RPC names
58
  */
58
  */
59
-export function rpcHooker<SubResT = {}>(socket: I.Socket, exporter:I.Exporter<SubResT>, makeUnique = true):T.ExtendedRpcInfo[]{
59
+export function rpcHooker<SubResT = {}>(socket: I.Socket, exporter:I.RPCExporter<SubResT>, makeUnique = true):T.ExtendedRpcInfo[]{
60
     const owner = exporter.name
60
     const owner = exporter.name
61
     const RPCs = [...exporter.exportRPCs()]
61
     const RPCs = [...exporter.exportRPCs()]
62
     const suffix = makeUnique?"-"+uuidv4().substr(0,4):""
62
     const suffix = makeUnique?"-"+uuidv4().substr(0,4):""
77
         return ret
77
         return ret
78
     })
78
     })
79
 }
79
 }
80
-//
80
+/**
81
+ * Utility function to generate {@link HookFunction} from a RPC
82
+ * @param rpc The RPC to transform
83
+ * @returns A {@link HookFunction}
84
+ */
81
 const hookGenerator = (rpc:T.HookRPC<any>): T.HookInfo['generator'] => { 
85
 const hookGenerator = (rpc:T.HookRPC<any>): T.HookInfo['generator'] => { 
82
     const argsArr = extractArgs(rpc.hook)
86
     const argsArr = extractArgs(rpc.hook)
83
     argsArr.pop()
87
     argsArr.pop()
99
     }`)
103
     }`)
100
 }
104
 }
101
 
105
 
106
+/**
107
+ * Extract a string list of parameters from a function
108
+ * @param f The source function
109
+ */
102
 const extractArgs = (f:Function):T.Arg[] => {
110
 const extractArgs = (f:Function):T.Arg[] => {
103
     let fn
111
     let fn
104
     return (fn = String(f)).substr(0, fn.indexOf(")")).substr(fn.indexOf("(")+1).split(",")
112
     return (fn = String(f)).substr(0, fn.indexOf(")")).substr(fn.indexOf("(")+1).split(",")
105
 }
113
 }
106
 
114
 
115
+/**
116
+ * Simple utility function to create basic {@link SubscriptionResponse}
117
+ * @param uuid optional uuid to use, otherwise defaults to uuid/v4
118
+ */
107
 export function makeSubResponse(uuid?:string):SubscriptionResponse{
119
 export function makeSubResponse(uuid?:string):SubscriptionResponse{
108
     return {
120
     return {
109
         result: "Success",
121
         result: "Success",

Loading…
Cancel
Save