daveappserver 0.6.9 → 0.6.11

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/appserver.js +87 -27
  2. package/package.json +2 -1
package/appserver.js CHANGED
@@ -1,4 +1,4 @@
1
- var myVersion = "0.6.9", myProductName = "daveAppServer";
1
+ var myVersion = "0.6.11", myProductName = "daveAppServer";
2
2
 
3
3
  exports.start = startup;
4
4
  exports.notifySocketSubscribers = notifySocketSubscribers;
@@ -23,6 +23,7 @@ const folderToJson = require ("foldertojson");
23
23
  const zip = require ("davezip");
24
24
  const qs = require ("querystring");
25
25
  const mail = require ("davemail");
26
+ const requireFromString = require ("require-from-string"); //2/10/23 by DW
26
27
 
27
28
  const whenStart = new Date ();
28
29
 
@@ -41,6 +42,7 @@ var config = {
41
42
  flEnableLogin: true, //user can log in via twitter
42
43
  blockedAddresses: [],
43
44
  flForceTwitterLogin: true,
45
+ flUseTwitterIdentity: false, //2/6/23 by DW
44
46
 
45
47
  flStorageEnabled: true,
46
48
  privateFilesPath: "privateFiles/users/",
@@ -57,7 +59,16 @@ var config = {
57
59
  operationToConfirm: "add your email address to your FeedLand user profile",
58
60
  mailSender: "dave@scripting.com",
59
61
  dataFolder: "data/",
60
- confirmationExpiresAfter: 60 * 60 //emails expire after an hour
62
+ confirmationExpiresAfter: 60 * 60, //emails expire after an hour
63
+
64
+ flSecureWebsocket: false, //2/8/23 by DW
65
+
66
+ isUserInDatabase: function (screenname, callback) { //2/6/23 by DW
67
+ callback (false);
68
+ },
69
+ getScreenNameFromEmail: function (screenname, callback) { //2/7/23 by DW
70
+ callback (undefined, screenname);
71
+ }
61
72
  };
62
73
  const fnameConfig = "config.json";
63
74
 
@@ -164,7 +175,7 @@ function checkPathForIllegalChars (path) {
164
175
  return (false);
165
176
  }
166
177
  switch (ch) {
167
- case "/": case "_": case "-": case ".": case " ": case "*":
178
+ case "/": case "_": case "-": case ".": case " ": case "*": case "@":
168
179
  return (false);
169
180
  }
170
181
  return (true);
@@ -237,6 +248,10 @@ function cleanFileStats (stats) { //4/19/21 by DW
237
248
  //sockets
238
249
  var theWsServer = undefined;
239
250
 
251
+ function getWsProtocol () { //2/8/23 by DW
252
+ const protocol = (utils.getBoolean (config.flSecureWebsocket)) ? "wss://" : "ws://";
253
+ return (protocol);
254
+ }
240
255
  function notifySocketSubscribers (verb, payload, flPayloadIsString, callbackToQualify) {
241
256
  if (theWsServer !== undefined) {
242
257
  var ctUpdates = 0, now = new Date (), ctTotalSockets = 0;
@@ -329,6 +344,7 @@ function cleanFileStats (stats) { //4/19/21 by DW
329
344
 
330
345
  conn.on ("text", function (s) {
331
346
  var words = s.split (" ");
347
+ console.log ("handleWebSocketConnection: s == " + s); //6/7/21 by DW
332
348
  if (words.length > 1) { //new protocol as of 11/29/15 by DW
333
349
  conn.appData.whenLastUpdate = now;
334
350
  conn.appData.lastVerb = words [0];
@@ -934,7 +950,15 @@ function cleanFileStats (stats) { //4/19/21 by DW
934
950
  function sendConfirmingEmail (email, screenname, flNewUser=false, callback) {
935
951
  function getScreenname (callback) {
936
952
  if (flNewUser) { //the caller had to provide it
937
- callback (undefined, screenname);
953
+ config.isUserInDatabase (screenname, function (flInDatabase) {
954
+ if (flInDatabase) {
955
+ const message = "Can't create the user \"" + screenname + "\" because there already is a user with that name."
956
+ callback ({message});
957
+ }
958
+ else {
959
+ callback (undefined, screenname);
960
+ }
961
+ });
938
962
  }
939
963
  else { //we have to look it up
940
964
  config.getScreenNameFromEmail (email, callback);
@@ -946,7 +970,7 @@ function cleanFileStats (stats) { //4/19/21 by DW
946
970
  }
947
971
  else {
948
972
  const magicString = utils.getRandomPassword (10);
949
- const urlWebApp = "http://" + config.myDomain + "/";
973
+ const urlWebApp = config.urlServerForEmail; //2/6/23 by DW
950
974
  console.log ("sendConfirmingEmail: email == " + email + ", urlWebApp == " + urlWebApp);
951
975
  var obj = {
952
976
  magicString: magicString,
@@ -991,7 +1015,8 @@ function cleanFileStats (stats) { //4/19/21 by DW
991
1015
  });
992
1016
  }
993
1017
  function receiveConfirmation (emailConfirmCode, callback) {
994
- const urlWebApp = "http://" + config.myDomain + "/";
1018
+ const urlWebApp = config.urlServerForClient; //2/5/23 by DW
1019
+
995
1020
  var urlRedirect = undefined, flFoundConfirm = false;
996
1021
  function encode (s) {
997
1022
  return (encodeURIComponent (s));
@@ -1004,7 +1029,7 @@ function cleanFileStats (stats) { //4/19/21 by DW
1004
1029
  urlRedirect = urlWebApp + "?failedLogin=true&message=" + encode (err.message);
1005
1030
  }
1006
1031
  else {
1007
- urlRedirect = urlWebApp + "?emailconfirmed=true&email=" + item.email + "&code=" + encode (emailSecret) + "&screenname=" + encode (item.screenname);
1032
+ urlRedirect = urlWebApp + "?emailconfirmed=true&email=" + encode (item.email) + "&code=" + encode (emailSecret) + "&screenname=" + encode (item.screenname);
1008
1033
  item.flDeleted = true;
1009
1034
  }
1010
1035
  callback (urlRedirect);
@@ -1059,6 +1084,26 @@ function startup (options, callback) {
1059
1084
  callback ();
1060
1085
  });
1061
1086
  }
1087
+ function readConfigJson (callback) { //2/8/23 by DW
1088
+ fs.readFile ("config.js", function (err, scripttext) {
1089
+ var flReadConfigJson = true;
1090
+ if (!err) {
1091
+ try {
1092
+ var jstruct = requireFromString (scripttext);
1093
+ for (var x in configJs) {
1094
+ config [x] = jstruct [x];
1095
+ }
1096
+ flReadConfigJson = false;
1097
+ callback ();
1098
+ }
1099
+ catch (err) {
1100
+ }
1101
+ }
1102
+ if (flReadConfigJson) {
1103
+ readConfig (fnameConfig, config, true, callback);
1104
+ }
1105
+ });
1106
+ }
1062
1107
  function startDavetwitter (httpRequestCallback) { //patch over a design problem in starting up davetwitter and davehttp -- 7/20/20 by DW
1063
1108
  if (config.twitter === undefined) {
1064
1109
  config.twitter = new Object ();
@@ -1188,7 +1233,9 @@ function startup (options, callback) {
1188
1233
  flEnableLogin: config.flEnableLogin,
1189
1234
  prefsPath: config.prefsPath,
1190
1235
  docsPath: config.docsPath,
1191
- idGitHubClient: config.githubClientId //11/9/21 by DW
1236
+ flUseTwitterIdentity: config.flUseTwitterIdentity, //2/6/23 by DW
1237
+ idGitHubClient: config.githubClientId, //11/9/21 by DW
1238
+ flWebsocketEnabled: config.flWebsocketEnabled //2/8/23 by DW
1192
1239
  };
1193
1240
  if (theRequest.addToPagetable !== undefined) { //3/9/21 by DW
1194
1241
  for (var x in theRequest.addToPagetable) {
@@ -1231,25 +1278,37 @@ function startup (options, callback) {
1231
1278
  }
1232
1279
  }
1233
1280
  function callWithScreenname (callback) {
1234
- if (config.getScreenname === undefined) {
1235
- davetwitter.getScreenName (token, secret, function (screenname) {
1236
- if (screenname === undefined) {
1237
- returnError ({message: "Can't do the thing you want because the accessToken is not valid."});
1238
- }
1239
- else {
1240
- callback (screenname);
1241
- }
1242
- });
1281
+
1282
+ if (config.flUseTwitterIdentity) { //2/6/23 by DW
1283
+ if (config.getScreenname === undefined) {
1284
+ davetwitter.getScreenName (token, secret, function (screenname) {
1285
+ if (screenname === undefined) {
1286
+ returnError ({message: "Can't do the thing you want because the accessToken is not valid."});
1287
+ }
1288
+ else {
1289
+ callback (screenname);
1290
+ }
1291
+ });
1292
+ }
1293
+ else {
1294
+ config.getScreenname (params, function (err, screenname) { //12/23/22 by DW
1295
+ if (err) {
1296
+ returnError (err);
1297
+ }
1298
+ else {
1299
+ callback (screenname);
1300
+ }
1301
+ });
1302
+ }
1243
1303
  }
1244
1304
  else {
1245
- config.getScreenname (params, function (err, screenname) { //12/23/22 by DW
1246
- if (err) {
1247
- returnError (err);
1248
- }
1249
- else {
1250
- callback (screenname);
1251
- }
1252
- });
1305
+ if ((params.emailaddress !== undefined) && (params.emailcode !== undefined)) {
1306
+ callback (params.emailaddress); //obviously we have to check if it's valid
1307
+ }
1308
+ else {
1309
+ const message = "Can't do what you wanted because the call is missing email authentication.";
1310
+ returnError ({message});
1311
+ }
1253
1312
  }
1254
1313
  }
1255
1314
 
@@ -1508,8 +1567,9 @@ function startup (options, callback) {
1508
1567
  }
1509
1568
  }
1510
1569
 
1570
+
1511
1571
  utils.copyScalars (options, config); //1/22/21 by DW
1512
- readConfig (fnameConfig, config, true, function () { //anything can be overridden by config.json
1572
+ readConfigJson (function () { //readConfig (fnameConfig, config, true, function () { //anything can be overridden by config.json
1513
1573
  readConfig (fnameStats, stats, false, function () {
1514
1574
  if (process.env.PORT !== undefined) { //8/6/20 by DW
1515
1575
  config.port = process.env.PORT;
@@ -1530,7 +1590,7 @@ function startup (options, callback) {
1530
1590
  config.urlServerForClient = "http://" + config.myDomain + "/";
1531
1591
  }
1532
1592
  if (config.urlWebsocketServerForClient === undefined) { //1/30/23 by DW
1533
- config.urlWebsocketServerForClient = "ws://" + utils.stringNthField (config.myDomain, ":", 1) + ":" + config.websocketPort + "/";
1593
+ config.urlWebsocketServerForClient = getWsProtocol () + utils.stringNthField (config.myDomain, ":", 1) + ":" + config.websocketPort + "/";
1534
1594
  }
1535
1595
  webSocketStartup ();
1536
1596
  setInterval (everySecond, 1000);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "daveappserver",
3
3
  "description": "Factored code that was appearing in all my servers.",
4
- "version": "0.6.9",
4
+ "version": "0.6.11",
5
5
  "main": "appserver.js",
6
6
  "repository": {
7
7
  "type" : "git",
@@ -15,6 +15,7 @@
15
15
  "querystring": "*",
16
16
  "request": "*",
17
17
  "nodejs-websocket": "*",
18
+ "require-from-string": "*",
18
19
  "daveutils": "*",
19
20
  "davefilesystem": "*",
20
21
  "foldertojson": "*",