tlsd 2.7.0 → 2.8.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/README.md CHANGED
@@ -7,8 +7,9 @@ This is a node.js based webserver.
7
7
 
8
8
  - SSL/TLS magically via Greenlock
9
9
  - Websockets built in
10
- - Standardised RPC protocol for front/back end communcations
10
+ - Standardised Websockets/RPC protocol for front/back end communcations
11
11
  - Utility command 'tlsd'
12
+ - Browser reload on src changes
12
13
 
13
14
 
14
15
  ## Installing
@@ -70,7 +71,7 @@ All arguments are required
70
71
 
71
72
  Adds a domain to Greenlock for production.
72
73
 
73
- tlsd domain foo.bar
74
+ tlsd domain example.com
74
75
 
75
76
 
76
77
  ## tlsd version
@@ -94,5 +95,7 @@ You must also add your domain to greenlock before running nodes or it won't be r
94
95
 
95
96
  Production mode serves files with Letsencrypt SSL certs magically.
96
97
 
98
+ The browser reload feature does not operate in production mode.
99
+
97
100
 
98
101
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tlsd",
3
- "version": "2.7.0",
3
+ "version": "2.8.0",
4
4
  "description": "A server for web app prototyping with HTTPS and Websockets",
5
5
  "main": "tlsd.js",
6
6
  "bin": {
@@ -2,6 +2,9 @@
2
2
  <body>
3
3
 
4
4
  <h1> Hello </h1>
5
+ <script>
6
+ document.write( Date() );
7
+ </script>
5
8
 
6
9
  <script src="/rpc/rpc.js"></script>
7
10
 
@@ -15,6 +18,15 @@
15
18
  RPC.POST() or RPC.WS().
16
19
  */
17
20
 
21
+ RPC.debug = true; // enables debug output to browser console
22
+
23
+ RPC.onmessage = msg => {
24
+ // Message originating from server (not a response to client message)
25
+ // returning true will prevent tlsd from doing anything further
26
+ // with the message, such as try to detect hot-reload messages, etc.
27
+ return false;
28
+ };
29
+
18
30
  RPC.on_connect = function( evt ) {
19
31
  // RPC is ready to be used
20
32
  RPC( { action: "hello" }, console.log, alert )
@@ -147,7 +147,7 @@
147
147
  }
148
148
 
149
149
  if(typeof msg_in.msg !== "undefined") {
150
- // server initiated msg (not a reply)
150
+ // server initiated msg (not a reply to a client msg)
151
151
 
152
152
  msg_in.reply = function( response ) {
153
153
  send( { msg_id: msg_in.msg_id, response, } );
@@ -181,7 +181,7 @@
181
181
  }
182
182
 
183
183
  if( typeof msg_in.response !== "undefined" ) {
184
- // response to a client initiated msg
184
+ // normal response to a client initiated msg
185
185
 
186
186
  var x = waiting.rem( msg_in.msg_id );
187
187
  if( ! x ) {
@@ -257,11 +257,25 @@
257
257
 
258
258
  ws_connect( msg => {
259
259
  // msg initiated by server
260
- DBG( "Server says: " + msg );
260
+ //DBG( "Server says: " + msg );
261
+
262
+ // If there's a handler set for this, call that
261
263
  const fn = RPC[ "onmessage" ];
262
264
  if( typeof fn == "function" ) {
263
- fn( msg );
265
+ if( fn( msg ) ) {
266
+ // returning true means it was handled by client
267
+ return;
268
+ }
264
269
  }
270
+
271
+ // Server is telling browser to reload the page
272
+ // this is due to the dev-mode hot-reload feature
273
+ // server sends this when it detects any changes to the
274
+ // site root dir, which includes the static/ and rpc/ dirs.
275
+ if( msg.msg === "RELOAD" ) {
276
+ location.reload();
277
+ }
278
+
265
279
  }, ( evt, detail ) => {
266
280
  // event occurred
267
281
  DBG( "Event: " + evt );
package/tlsd CHANGED
@@ -24,7 +24,7 @@ if [ "$cmd" = "run" ] ; then
24
24
 
25
25
  echo "Using node.js `node -v`"
26
26
 
27
- if DOMAINS_ROOT="domains" MAINTAINER_EMAIL="$email" VERBOSITY=3 node "$home/tlsd.js" ; then
27
+ if DOMAINS_ROOT="$home/domains" MAINTAINER_EMAIL="$email" VERBOSITY=3 node "$home/tlsd.js" ; then
28
28
  echo "`date` ____________ Graceful exit "
29
29
  break
30
30
  else
package/tlsd.js CHANGED
@@ -220,7 +220,7 @@ const rest_handler = function( root, req, rsp ) {
220
220
  };
221
221
 
222
222
 
223
- const ws_attach = function( httpServer, msg_handler ) {
223
+ const ws_attach = function( httpServer, msg_handler, done ) {
224
224
 
225
225
  const wsd = new websocket.server( { httpServer, autoAcceptConnections: false, } );
226
226
 
@@ -281,6 +281,8 @@ const ws_attach = function( httpServer, msg_handler ) {
281
281
 
282
282
  D( "WS: connected: "+name );
283
283
 
284
+ done( conn );
285
+
284
286
  } );
285
287
 
286
288
  D( "WS: initialized" );
@@ -290,6 +292,39 @@ const ws_attach = function( httpServer, msg_handler ) {
290
292
  };
291
293
 
292
294
 
295
+ const enable_hot_reload = function( root, conn ) {
296
+ D( "Enabling hot-reload" );
297
+
298
+ let tid = null;
299
+ let count = 0;
300
+
301
+ function tick() {
302
+ count -= 1; // reduce the count by 1
303
+ if( count <= 0 ) {
304
+ // counter reached 0
305
+ clearInterval( tid ); // turn off the ticker
306
+ tid = null;
307
+ conn.send( { msg: "RELOAD" } ); // send the RELOAD msg
308
+ }
309
+ }
310
+
311
+ function see( evt, path ) {
312
+ // a change of some sort was seen
313
+ // hundreds of these can happen very rapidly, so the timer stuff
314
+ // above is used to delay sending the RELOAD command until
315
+ // a second or so has passed without any more calls to this function
316
+ // XXX use regex to only consider certain file extensions?
317
+ count = 5;
318
+ // If the ticker isn't already running, the start it
319
+ if( tid === null ) {
320
+ tid = setInterval( tick, 200 );
321
+ }
322
+ };
323
+
324
+ fs.watch( root, { recursive: true, }, see );
325
+ };
326
+
327
+
293
328
  // -----------------------
294
329
 
295
330
  const argv = process.argv;
@@ -319,7 +354,9 @@ if( argv.length == 2 ) {
319
354
  ws_attach( httpd, ( msg, conn, domain ) => {
320
355
  const root = path.resolve( DOMAINS_ROOT + "/" + domain );
321
356
  ws_msg_handler( root, msg );
322
- } );
357
+ }, conn => {
358
+ // attachment complete
359
+ } );
323
360
 
324
361
  glx.serveApp( ( req, res ) => {
325
362
  const root = path.resolve( DOMAINS_ROOT + "/" + req.headers[ "host" ] );
@@ -358,6 +395,10 @@ if( argv.length == 5 ) {
358
395
 
359
396
  ws_attach( httpd, ( msg, conn, domain ) => {
360
397
  ws_msg_handler( SITE_ROOT, msg );
398
+ }, conn => {
399
+ // attachment complete
400
+ enable_hot_reload( SITE_ROOT, conn );
401
+
361
402
  } );
362
403
 
363
404
  I( "Listening on " + PORT + " & serving from " + SITE_ROOT );