tlsd 2.6.2 → 2.6.5

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.
Files changed (2) hide show
  1. package/package.json +2 -2
  2. package/tlsd.js +39 -46
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tlsd",
3
- "version": "2.6.2",
3
+ "version": "2.6.5",
4
4
  "description": "A server for web app prototyping with HTTPS and Websockets",
5
5
  "main": "tlsd.js",
6
6
  "bin": {
@@ -20,7 +20,7 @@
20
20
  "g": "^2.0.1",
21
21
  "greenlock-express": "^4.0.3",
22
22
  "serve-static": "^1.15.0",
23
- "sleepless": "^5.10.1",
23
+ "sleepless": "^5.13.0",
24
24
  "tlsd": "^2.4.1",
25
25
  "websocket": "^1.0.34"
26
26
  }
package/tlsd.js CHANGED
@@ -1,9 +1,7 @@
1
1
 
2
2
  /*
3
-
4
3
  Copyright 2023 Sleepless Software Inc.
5
4
  All Rights Reserved
6
-
7
5
  */
8
6
 
9
7
  const { path, http, https, fs, crypto, tls, } = require( "allcore" );
@@ -12,8 +10,8 @@ const connect = require( "connect" );
12
10
  const websocket = require( "websocket" );
13
11
  const serveStatic = require( "serve-static" )
14
12
 
15
- const g = require( "g" );
16
- g( "sleepless" );
13
+ require( "sleepless" ).globalize();
14
+
17
15
  const L = log5.mkLog( "TLSD: " );
18
16
  const { D, V, I, W, E } = L;
19
17
 
@@ -34,7 +32,6 @@ function usage() {
34
32
  process.exit( 1 );
35
33
  }
36
34
 
37
-
38
35
  let seq = 0;
39
36
  const next_seq = function() {
40
37
  seq += 1;
@@ -44,22 +41,23 @@ const next_seq = function() {
44
41
 
45
42
  // Handles incoming RPC msgs.
46
43
  // Tries to load the rpc handler module from root and if successful, passes the msg to it
47
- const rpc_handler = function( root, msg, _okay, _fail ) {
44
+ const rpc_handler = function( root, msg, xport, _okay, _fail ) {
48
45
 
49
- D( "----------->>> RPC "+o2j( msg, null, 2 ) );
46
+ V( "RPC " + o2j( msg ).abbr( 70 ) );
47
+ D( "----------->>> "+xport+" "+o2j( msg, null, 2 ) );
50
48
 
51
49
  const okay = data => {
52
- D( "<<<=========== RPC OKAY "+o2j( data, null, 2 ) );
50
+ D( "<<<=========== "+xport+" OKAY "+o2j( data, null, 2 ) );
53
51
  _okay( data );
54
52
  };
55
53
 
56
54
  const fail = error => {
57
- D( "<<<*********** RPC ERROR "+o2j( error, null, 2 ) );
55
+ D( "<<<*********** "+xport+" ERROR "+o2j( error, null, 2 ) );
58
56
  _fail( error );
59
57
  };
60
58
 
61
59
  const ouch = audit_error => {
62
- //E( root + ": " + audit_error );
60
+ E( root + ": " + audit_error );
63
61
  fail( "RPC error" ); // this is returned to browser
64
62
  };
65
63
 
@@ -68,11 +66,11 @@ const rpc_handler = function( root, msg, _okay, _fail ) {
68
66
  try {
69
67
  mod( msg, okay, ouch );
70
68
  } catch( err ) {
71
- E( err );
69
+ E( (err instanceof Error ) ? err.stack : err );
72
70
  ouch( "RPC handler exception" );
73
71
  }
74
72
  } catch( err ) {
75
- E( err );
73
+ E( (err instanceof Error ) ? err.stack : err );
76
74
  ouch( "Error loading RPC handler" );
77
75
  }
78
76
  };
@@ -81,10 +79,10 @@ const rpc_handler = function( root, msg, _okay, _fail ) {
81
79
  // Glue function to call rpc handler module and then return response
82
80
  // via the websockets msg object
83
81
  const ws_msg_handler = function( root, msg ) {
84
- rpc_handler( root, msg.msg, data => {
82
+ rpc_handler( root, msg.msg, "WS", data => {
85
83
  msg.reply( data );
86
84
  }, error => {
87
- E( "This shouldn't happen." );
85
+ E( "This shouldn't happen: "+o2j(error,null,2) );
88
86
  } );
89
87
  };
90
88
 
@@ -156,7 +154,7 @@ const basic_handler = function( root ) {
156
154
  const fail = ( error, body ) => { done( error, body ); };
157
155
 
158
156
  // Summon the rpc handler for the domain root.
159
- rpc_handler( root, input, okay, fail );
157
+ rpc_handler( root, input, "REST", okay, fail );
160
158
 
161
159
  } );
162
160
 
@@ -168,6 +166,8 @@ const basic_handler = function( root ) {
168
166
  // Serve static files for domain
169
167
  app.use( serveStatic( root + "/static" ) );
170
168
 
169
+ // finally, if serveStatic can't service the request,
170
+ // look for a 404/ dir and redirect to that if present
171
171
  app.use( function( req, res, next ) {
172
172
  // I can't just return a redirect here because if the /404 doesn't exist,
173
173
  // I will just go into a redirect loop, so I have to test to see if the
@@ -186,42 +186,35 @@ const basic_handler = function( root ) {
186
186
  }
187
187
 
188
188
 
189
- const rest_handler = function( root, req, res ) {
189
+ // Handle REST calls (as opposed to websocket messages)
190
+ const rest_handler = function( root, req, rsp ) {
190
191
 
191
192
  I( req.headers[ "host" ] + ": " + req.method + " " + req.url );
192
193
  D( "rest_handler root: " + root );
193
194
 
194
- const ouch = ( log_msg, error ) => {
195
- W( root + ": " + log_msg );
196
- if( error ) {
195
+ const call = function( handler ) {
196
+ try {
197
+ handler( req, rsp );
198
+ } catch( error ) {
199
+ W( root + ": REST handler exception" );
197
200
  E( error.stack );
201
+ try {
202
+ rsp.writeHead( 404 );
203
+ rsp.end();
204
+ } catch( err ) {}
198
205
  }
199
- try {
200
- res.writeHead( 404 );
201
- res.end();
202
- } catch( err ) {}
203
- };
206
+ }
204
207
 
208
+ // try loading custom handler first
205
209
  try {
206
-
207
210
  handler = require( root );
208
- D( "Custom handler loaded from "+root );
209
- try {
210
- handler( req, res );
211
- } catch( err ) {
212
- ouch( "Custom handler exception", err );
213
- }
214
-
211
+ D( "Using custom REST handler loaded from "+root );
212
+ call( handler );
215
213
  } catch( err ) {
216
-
217
- try {
218
- const handler = basic_handler( root );
219
- D( "Using basic handler" );
220
- handler( req, res );
221
- } catch( err ) {
222
- ouch( "Basic handler exception", err );
223
- }
224
-
214
+ // custom handler load attempt threw an exception
215
+ const handler = basic_handler( root );
216
+ D( "Using basic REST handler" );
217
+ call( handler );
225
218
  }
226
219
  };
227
220
 
@@ -231,7 +224,7 @@ const ws_attach = function( httpServer, msg_handler ) {
231
224
  const wsd = new websocket.server( { httpServer, autoAcceptConnections: false, } );
232
225
 
233
226
  wsd.on( "request", function( req ) {
234
- D( "WS: connection request from "+req.remoteAddress+" "+req.resource )
227
+ V( "WS: connection request from "+req.remoteAddress+" "+req.resource )
235
228
 
236
229
  const domain = req.httpRequest.headers[ "host" ];
237
230
 
@@ -244,7 +237,7 @@ const ws_attach = function( httpServer, msg_handler ) {
244
237
  if( msg.msg_id === undefined ) {
245
238
  msg.msg_id = "msg-id-" + next_seq(); // every message must have an id
246
239
  }
247
- D( name+" <-- WS --<< "+o2j( msg ) );
240
+ D( name+" <=== WS ===<< "+o2j( msg ) );
248
241
  socket.send( o2j( msg ) );
249
242
  };
250
243
 
@@ -260,7 +253,7 @@ const ws_attach = function( httpServer, msg_handler ) {
260
253
 
261
254
  // incoming msgs from client come through here
262
255
  socket.on( "message", function( x ) {
263
- D( name+" >>-- WS --> "+x.utf8Data );
256
+ D( name+" >>--- WS ---> "+x.utf8Data );
264
257
 
265
258
  const json = x.utf8Data; // raw message is a utf8 string
266
259
  const msg_in = j2o( json );
@@ -298,7 +291,6 @@ const ws_attach = function( httpServer, msg_handler ) {
298
291
 
299
292
  // -----------------------
300
293
 
301
-
302
294
  const argv = process.argv;
303
295
 
304
296
  if( argv.length == 2 ) {
@@ -317,7 +309,8 @@ if( argv.length == 2 ) {
317
309
  packageRoot: __dirname,
318
310
  maintainerEmail: MAINTAINER_EMAIL,
319
311
  configDir: "./greenlock.d",
320
- cluster: true
312
+ cluster: false,
313
+ //notify
321
314
  } ).ready( glx => {
322
315
 
323
316
  var httpd = glx.httpsServer();