tlsd 2.2.2 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tlsd",
3
- "version": "2.2.2",
4
- "description": "An server for web app prototyping with HTTPS and Websockets",
3
+ "version": "2.4.0",
4
+ "description": "A server for web app prototyping with HTTPS and Websockets",
5
5
  "main": "tlsd.js",
6
6
  "bin": {
7
7
  "tlsd": "tlsd"
@@ -1,4 +1,13 @@
1
1
 
2
+ /*
3
+ This is the back end script that handles message from the front end.
4
+ The "input" object is the exact same that was given to RPC()
5
+ on the front end.
6
+ Use okay() to return a normal response, or fail() to return an error.
7
+ These correspond to the same okay()/fail() functions given to RPC() on
8
+ the front end.
9
+ */
10
+
2
11
  module.exports = ( input, okay, fail ) => {
3
12
 
4
13
  const action = input.action;
@@ -6,9 +6,20 @@
6
6
  <script src="/rpc/rpc.js"></script>
7
7
 
8
8
  <script>
9
- RPC.on_connect = evt => {
10
- RPC( { action: "hello" }, console.log, alert );
11
- };
9
+
10
+ /*
11
+ RPC is used to send msgs to the back end.
12
+ If you call RPC() directly, it will decide whether or not
13
+ to use Websocket or REST as transport based on size of object.
14
+ Otherwise, you can explicitly choose the transport by using
15
+ RPC.POST() or RPC.WS().
16
+ */
17
+
18
+ RPC.on_connect = function( evt ) {
19
+ // RPC is ready to be used
20
+ RPC( { action: "hello" }, console.log, alert )
21
+ }
22
+
12
23
  </script>
13
24
 
14
25
  </body>
@@ -190,21 +190,6 @@
190
190
  };
191
191
 
192
192
 
193
- ws_connect( msg => {
194
- DBG( "Server says: " + msg ); // msg initiated by server
195
- const fn = RPC[ "onmessage" ];
196
- if( typeof fn == "function" ) {
197
- fn( msg );
198
- }
199
- }, ( evt, detail ) => {
200
- DBG( "Event: " + evt );
201
- const fn = RPC[ "on_" + evt ];
202
- if( typeof fn == "function" ) {
203
- fn( detail );
204
- }
205
- }, "/" );
206
-
207
-
208
193
  const RPC_URL = "/rpc/";
209
194
 
210
195
  const via_post = function( msg, okay = ()=>{}, fail = ()=>{} ) {
@@ -230,6 +215,14 @@
230
215
  conn.send( { msg }, okay );
231
216
  };
232
217
 
218
+ const connect = function( cb ) {
219
+ if( connected ) {
220
+ cb();
221
+ } else {
222
+ RPC.on_connect = cb;
223
+ }
224
+ };
225
+
233
226
  const RPC = function( msg, okay = ()=>{}, fail = ()=>{} ) {
234
227
  const json = o2j( msg );
235
228
  if( json.length > 5000 ) {
@@ -241,9 +234,34 @@
241
234
  }
242
235
  };
243
236
 
237
+ RPC.connect = connect;
244
238
  RPC.POST = via_post;
245
239
  RPC.WS = via_ws;
246
240
  RPC.debug = false;
241
+ RPC.connected = false;
242
+
243
+ ws_connect( msg => {
244
+ // msg initiated by server
245
+ DBG( "Server says: " + msg );
246
+ const fn = RPC[ "onmessage" ];
247
+ if( typeof fn == "function" ) {
248
+ fn( msg );
249
+ }
250
+ }, ( evt, detail ) => {
251
+ // event occurred
252
+ DBG( "Event: " + evt );
253
+ if( evt === "connect" ) {
254
+ RPC.connected = true;
255
+ }
256
+ if( evt === "disconnect" ) {
257
+ RPC.connected = false;
258
+ }
259
+ const fn = RPC[ "on_" + evt ];
260
+ if( typeof fn == "function" ) {
261
+ fn( detail );
262
+ }
263
+ }, "/" );
264
+
247
265
 
248
266
  globalThis.RPC = RPC;
249
267