tlsd 2.6.1 → 2.6.3

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 +44 -46
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tlsd",
3
- "version": "2.6.1",
3
+ "version": "2.6.3",
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.7.0",
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,22 @@ 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
+ D( "----------->>> "+xport+" "+o2j( msg, null, 2 ) );
50
47
 
51
48
  const okay = data => {
52
- D( "<<<=========== RPC OKAY "+o2j( data, null, 2 ) );
49
+ D( "<<<=========== "+xport+" OKAY "+o2j( data, null, 2 ) );
53
50
  _okay( data );
54
51
  };
55
52
 
56
53
  const fail = error => {
57
- D( "<<<*********** RPC ERROR "+o2j( error, null, 2 ) );
54
+ D( "<<<*********** "+xport+" ERROR "+o2j( error, null, 2 ) );
58
55
  _fail( error );
59
56
  };
60
57
 
61
58
  const ouch = audit_error => {
62
- //E( root + ": " + audit_error );
59
+ E( root + ": " + audit_error );
63
60
  fail( "RPC error" ); // this is returned to browser
64
61
  };
65
62
 
@@ -81,10 +78,10 @@ const rpc_handler = function( root, msg, _okay, _fail ) {
81
78
  // Glue function to call rpc handler module and then return response
82
79
  // via the websockets msg object
83
80
  const ws_msg_handler = function( root, msg ) {
84
- rpc_handler( root, msg.msg, data => {
81
+ rpc_handler( root, msg.msg, "WS", data => {
85
82
  msg.reply( data );
86
83
  }, error => {
87
- E( "This shouldn't happen." );
84
+ E( "This shouldn't happen: "+o2j(error,null,2) );
88
85
  } );
89
86
  };
90
87
 
@@ -156,7 +153,7 @@ const basic_handler = function( root ) {
156
153
  const fail = ( error, body ) => { done( error, body ); };
157
154
 
158
155
  // Summon the rpc handler for the domain root.
159
- rpc_handler( root, input, okay, fail );
156
+ rpc_handler( root, input, "REST", okay, fail );
160
157
 
161
158
  } );
162
159
 
@@ -168,11 +165,19 @@ const basic_handler = function( root ) {
168
165
  // Serve static files for domain
169
166
  app.use( serveStatic( root + "/static" ) );
170
167
 
168
+ // finally, if serveStatic can't service the request,
169
+ // look for a 404/ dir and redirect to that if present
171
170
  app.use( function( req, res, next ) {
172
- res.writeHead( 302, {
173
- "Location": "/404/",
174
- });
175
- res.end();
171
+ // I can't just return a redirect here because if the /404 doesn't exist,
172
+ // I will just go into a redirect loop, so I have to test to see if the
173
+ // dir exists, and then if so, redirect to it, otherwise, just return 404.
174
+ is_file( root + "/404", so => {
175
+ if( so )
176
+ res.writeHead( 302, { "Location": "/404/", });
177
+ else
178
+ res.writeHead( 404 );
179
+ res.end();
180
+ } );
176
181
  } );
177
182
 
178
183
  return app;
@@ -180,42 +185,35 @@ const basic_handler = function( root ) {
180
185
  }
181
186
 
182
187
 
183
- const rest_handler = function( root, req, res ) {
188
+ // Handle REST calls (as opposed to websocket messages)
189
+ const rest_handler = function( root, req, rsp ) {
184
190
 
185
191
  I( req.headers[ "host" ] + ": " + req.method + " " + req.url );
186
192
  D( "rest_handler root: " + root );
187
193
 
188
- const ouch = ( log_msg, error ) => {
189
- W( root + ": " + log_msg );
190
- if( error ) {
194
+ const call = function( handler ) {
195
+ try {
196
+ handler( req, rsp );
197
+ } catch( error ) {
198
+ W( root + ": REST handler exception" );
191
199
  E( error.stack );
200
+ try {
201
+ rsp.writeHead( 404 );
202
+ rsp.end();
203
+ } catch( err ) {}
192
204
  }
193
- try {
194
- res.writeHead( 404 );
195
- res.end();
196
- } catch( err ) {}
197
- };
205
+ }
198
206
 
207
+ // try loading custom handler first
199
208
  try {
200
-
201
209
  handler = require( root );
202
- D( "Custom handler loaded from "+root );
203
- try {
204
- handler( req, res );
205
- } catch( err ) {
206
- ouch( "Custom handler exception", err );
207
- }
208
-
210
+ D( "Using custom REST handler loaded from "+root );
211
+ call( handler );
209
212
  } catch( err ) {
210
-
211
- try {
212
- const handler = basic_handler( root );
213
- D( "Using basic handler" );
214
- handler( req, res );
215
- } catch( err ) {
216
- ouch( "Basic handler exception", err );
217
- }
218
-
213
+ // custom handler load attempt threw an exception
214
+ const handler = basic_handler( root );
215
+ D( "Using basic REST handler" );
216
+ call( handler );
219
217
  }
220
218
  };
221
219
 
@@ -225,7 +223,7 @@ const ws_attach = function( httpServer, msg_handler ) {
225
223
  const wsd = new websocket.server( { httpServer, autoAcceptConnections: false, } );
226
224
 
227
225
  wsd.on( "request", function( req ) {
228
- D( "WS: connection request from "+req.remoteAddress+" "+req.resource )
226
+ V( "WS: connection request from "+req.remoteAddress+" "+req.resource )
229
227
 
230
228
  const domain = req.httpRequest.headers[ "host" ];
231
229
 
@@ -238,7 +236,7 @@ const ws_attach = function( httpServer, msg_handler ) {
238
236
  if( msg.msg_id === undefined ) {
239
237
  msg.msg_id = "msg-id-" + next_seq(); // every message must have an id
240
238
  }
241
- D( name+" <-- WS --<< "+o2j( msg ) );
239
+ D( name+" <=== WS ===<< "+o2j( msg ) );
242
240
  socket.send( o2j( msg ) );
243
241
  };
244
242
 
@@ -254,7 +252,7 @@ const ws_attach = function( httpServer, msg_handler ) {
254
252
 
255
253
  // incoming msgs from client come through here
256
254
  socket.on( "message", function( x ) {
257
- D( name+" >>-- WS --> "+x.utf8Data );
255
+ D( name+" >>--- WS ---> "+x.utf8Data );
258
256
 
259
257
  const json = x.utf8Data; // raw message is a utf8 string
260
258
  const msg_in = j2o( json );