daveappserver 0.6.25 → 0.6.27

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/appserver.js CHANGED
@@ -1,4 +1,4 @@
1
- var myVersion = "0.6.25", myProductName = "daveAppServer";
1
+ var myVersion = "0.6.27", myProductName = "daveAppServer";
2
2
 
3
3
  exports.start = startup;
4
4
  exports.notifySocketSubscribers = notifySocketSubscribers;
@@ -66,6 +66,8 @@ var config = {
66
66
 
67
67
  flUseS3ForStorage: false, //2/15/23 by DW
68
68
 
69
+ flUseDatabaseForConfirmations: false, //8/14/23 by DW
70
+
69
71
  isUserInDatabase: function (screenname, callback) { //2/6/23 by DW
70
72
  callback (false);
71
73
  },
@@ -200,17 +202,24 @@ function getDomainName (clientIp, callback) { //11/14/15 by DW
200
202
  }
201
203
  }
202
204
  else {
203
- dns.reverse (clientIp, function (err, domains) {
204
- var name = clientIp;
205
- if (!err) {
206
- if (domains.length > 0) {
207
- name = domains [0];
205
+ try { //7/27/23 by DW
206
+ dns.reverse (clientIp, function (err, domains) {
207
+ var name = clientIp;
208
+ if (!err) {
209
+ if (domains.length > 0) {
210
+ name = domains [0];
211
+ }
208
212
  }
209
- }
213
+ if (callback !== undefined) {
214
+ callback (name);
215
+ }
216
+ });
217
+ }
218
+ catch (err) {
210
219
  if (callback !== undefined) {
211
220
  callback (name);
212
221
  }
213
- });
222
+ }
214
223
  }
215
224
  }
216
225
  function getDomainNameVerb (clientIp, callback) { //2/27/21 by DW
@@ -1006,6 +1015,115 @@ function cleanFileStats (stats) { //4/19/21 by DW
1006
1015
  });
1007
1016
  }
1008
1017
  //email registration -- 12/7/22 by DW
1018
+ function addPendingConfirmation (theConfirmation, callback) { //8/14/23 by DW
1019
+ if (config.flUseDatabaseForConfirmations) {
1020
+ theConfirmation.whenCreated = theConfirmation.when;
1021
+ delete theConfirmation.when;
1022
+
1023
+ var sqltext = "replace into pendingConfirmations " + davesql.encodeValues (theConfirmation);
1024
+ davesql.runSqltext (sqltext, function (err, result) {
1025
+ if (err) {
1026
+ callback (err);
1027
+ }
1028
+ else {
1029
+ console.log ("addPendingConfirmation: email == " + theConfirmation.email);
1030
+ callback (undefined);
1031
+ }
1032
+ });
1033
+ }
1034
+ else {
1035
+ stats.pendingConfirmations.push (theConfirmation);
1036
+ statsChanged ();
1037
+ callback (undefined); //no error
1038
+ }
1039
+ }
1040
+ function findPendingConfirmation (magicString, callback) { //8/14/23 by DW
1041
+ if (config.flUseDatabaseForConfirmations) {
1042
+ var sqltext = "select * from pendingConfirmations where magicString=" + davesql.encode (magicString) + ";";
1043
+ davesql.runSqltext (sqltext, function (err, result) {
1044
+ if (err) {
1045
+ console.log ("findPendingConfirmation: err.message == " + err.message);
1046
+ callback (err);
1047
+ }
1048
+ else {
1049
+ if (result.length == 0) {
1050
+ const message = "Can't find the pending confirmation.";
1051
+ callback ({message});
1052
+ }
1053
+ else {
1054
+ callback (undefined, result [0]);
1055
+ }
1056
+ }
1057
+ });
1058
+ }
1059
+ else {
1060
+ var flFoundConfirm = false;
1061
+ stats.pendingConfirmations.forEach (function (item) {
1062
+ if (item.magicString == emailConfirmCode) {
1063
+ callback (undefined, item);
1064
+ flFoundConfirm = true;
1065
+ }
1066
+ });
1067
+ if (!flFoundConfirm) {
1068
+ const message = "Can't find the pending confirmation.";
1069
+ callback ({message});
1070
+ }
1071
+ }
1072
+ }
1073
+ function deletePendingConfirmation (item, callback) { //8/14/23 by DW
1074
+ if (config.flUseDatabaseForConfirmations) {
1075
+ const sqltext = "update pendingConfirmations set flDeleted = true where magicString = " + enclode (item.magicString) + ";"
1076
+ davesql.runSqltext (sqltext, function (err, result) {
1077
+ if (err) {
1078
+ if (callback !== undefined) {
1079
+ callback (err);
1080
+ }
1081
+ }
1082
+ else {
1083
+ if (callback !== undefined) {
1084
+ callback (undefined);
1085
+ }
1086
+ }
1087
+ });
1088
+ }
1089
+ else {
1090
+ item.flDeleted = true;
1091
+ }
1092
+ }
1093
+ function checkPendingConfirmations (callback) { //8/14/23 by DW
1094
+ if (config.flUseDatabaseForConfirmations) {
1095
+ const sqltext = "delete from pendingConfirmations where flDeleted = true or whenCreated < now() - interval 1 hour;";
1096
+ davesql.runSqltext (sqltext, function (err, result) {
1097
+ if (err) {
1098
+ if (callback !== undefined) {
1099
+ callback (err);
1100
+ }
1101
+ }
1102
+ else {
1103
+ if (callback !== undefined) {
1104
+ callback (undefined);
1105
+ }
1106
+ }
1107
+ });
1108
+ }
1109
+ else {
1110
+ var flChanged = false;
1111
+ var newArray = new Array ();
1112
+ stats.pendingConfirmations.forEach (function (item) {
1113
+ if ((!item.flDeleted) && (utils.secondsSince (item.when) < config.confirmationExpiresAfter)) {
1114
+ newArray.push (item);
1115
+ }
1116
+ else {
1117
+ flChanged = true;
1118
+ }
1119
+ });
1120
+ if (flChanged) {
1121
+ stats.pendingConfirmations = newArray;
1122
+ statsChanged ();
1123
+ }
1124
+ }
1125
+ }
1126
+
1009
1127
  function sendConfirmingEmail (email, screenname, flNewUser=false, urlRedirect, callback) {
1010
1128
  email = utils.stringLower (email); //3/8/23 by DW
1011
1129
  function getScreenname (callback) {
@@ -1059,35 +1177,40 @@ function cleanFileStats (stats) { //4/19/21 by DW
1059
1177
  urlRedirect, //3/3/23 by DW
1060
1178
  when: new Date ()
1061
1179
  };
1062
- stats.pendingConfirmations.push (obj);
1063
- statsChanged ();
1064
- console.log ("sendConfirmingEmail: obj == " + utils.jsonStringify (obj));
1065
- var params = {
1066
- title: config.confirmEmailSubject,
1067
- operationToConfirm: config.operationToConfirm,
1068
- confirmationUrl: urlWebApp + "userconfirms?emailConfirmCode=" + encodeURIComponent (magicString)
1069
- };
1070
- fs.readFile (config.fnameEmailTemplate, function (err, emailTemplate) {
1180
+ addPendingConfirmation (obj, function (err) { //8/14/23 by DW
1071
1181
  if (err) {
1072
- const message = "Error reading email template.";
1073
- console.log ("sendConfirmingEmail: err.message == " + err.message);
1074
- callback ({message});
1182
+ callback (err);
1075
1183
  }
1076
1184
  else {
1077
- var mailtext = utils.multipleReplaceAll (emailTemplate.toString (), params, false, "[%", "%]");
1078
- mail.send (email, params.title, mailtext, config.mailSender, function (err, data) {
1185
+ console.log ("sendConfirmingEmail: obj == " + utils.jsonStringify (obj));
1186
+ var params = {
1187
+ title: config.confirmEmailSubject,
1188
+ operationToConfirm: config.operationToConfirm,
1189
+ confirmationUrl: urlWebApp + "userconfirms?emailConfirmCode=" + encodeURIComponent (magicString)
1190
+ };
1191
+ fs.readFile (config.fnameEmailTemplate, function (err, emailTemplate) {
1079
1192
  if (err) {
1080
- callback (err);
1193
+ const message = "Error reading email template.";
1194
+ console.log ("sendConfirmingEmail: err.message == " + err.message);
1195
+ callback ({message});
1081
1196
  }
1082
1197
  else {
1083
- callback (undefined, {message: "Please check your email."});
1198
+ var mailtext = utils.multipleReplaceAll (emailTemplate.toString (), params, false, "[%", "%]");
1199
+ mail.send (email, params.title, mailtext, config.mailSender, function (err, data) {
1200
+ if (err) {
1201
+ callback (err);
1202
+ }
1203
+ else {
1204
+ callback (undefined, {message: "Please check your email."});
1205
+ }
1206
+ });
1207
+ const f = config.dataFolder + "lastmail.html";
1208
+ utils.sureFilePath (f, function () {
1209
+ fs.writeFile (f, mailtext, function (err) {
1210
+ });
1211
+ });
1084
1212
  }
1085
1213
  });
1086
- const f = config.dataFolder + "lastmail.html";
1087
- utils.sureFilePath (f, function () {
1088
- fs.writeFile (f, mailtext, function (err) {
1089
- });
1090
- });
1091
1214
  }
1092
1215
  });
1093
1216
  }
@@ -1099,8 +1222,15 @@ function cleanFileStats (stats) { //4/19/21 by DW
1099
1222
  function encode (s) {
1100
1223
  return (encodeURIComponent (s));
1101
1224
  }
1102
- stats.pendingConfirmations.forEach (function (item) {
1103
- if (item.magicString == emailConfirmCode) {
1225
+
1226
+ findPendingConfirmation (emailConfirmCode, function (err, theConfirmation) {
1227
+ if (err) {
1228
+ if (urlRedirect === undefined) {
1229
+ urlRedirect = urlWebApp;
1230
+ }
1231
+ callback (urlRedirect + "?failedLogin=true&message=" + encode (err.message); );
1232
+ }
1233
+ else {
1104
1234
  if (config.addEmailToUserInDatabase !== undefined) {
1105
1235
  if (item.urlRedirect !== undefined) { //3/3/23 by DW
1106
1236
  urlWebApp = item.urlRedirect;
@@ -1111,36 +1241,13 @@ function cleanFileStats (stats) { //4/19/21 by DW
1111
1241
  }
1112
1242
  else {
1113
1243
  urlRedirect = urlWebApp + "?emailconfirmed=true&email=" + encode (item.email) + "&code=" + encode (emailSecret) + "&screenname=" + encode (item.screenname);
1114
- item.flDeleted = true;
1244
+ deletePendingConfirmation (item);
1115
1245
  }
1116
1246
  callback (urlRedirect);
1117
1247
  });
1118
1248
  }
1119
- flFoundConfirm = true;
1120
1249
  }
1121
1250
  });
1122
- if (!flFoundConfirm) {
1123
- if (urlRedirect === undefined) {
1124
- urlRedirect = urlWebApp + "?failedLogin=true&message=" + encode ("Can't find the pending confirmation.");
1125
- }
1126
- callback (urlRedirect);
1127
- }
1128
- }
1129
- function checkPendingConfirmations () {
1130
- var flChanged = false;
1131
- var newArray = new Array ();
1132
- stats.pendingConfirmations.forEach (function (item) {
1133
- if ((!item.flDeleted) && (utils.secondsSince (item.when) < config.confirmationExpiresAfter)) {
1134
- newArray.push (item);
1135
- }
1136
- else {
1137
- flChanged = true;
1138
- }
1139
- });
1140
- if (flChanged) {
1141
- stats.pendingConfirmations = newArray;
1142
- statsChanged ();
1143
- }
1144
1251
  }
1145
1252
 
1146
1253
  function startup (options, callback) {
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.25",
4
+ "version": "0.6.27",
5
5
  "main": "appserver.js",
6
6
  "repository": {
7
7
  "type" : "git",
package/readme.md DELETED
@@ -1,78 +0,0 @@
1
- # appserver
2
-
3
- A new release of nodeStorage that removes all the historical addons, streamlines configuration, only serves from the local file system, and serves the home page of the app. It could be thought of as v2.0 of nodeStorage except it is not backward compatible.
4
-
5
- ### Overview
6
-
7
- Over the years nodeStorage got a lot of appendages and add-ons that most apps don't use.
8
-
9
- The configuration file was confusing and not well documented.
10
-
11
- It was one of the first things I wrote for Node, and I learned a lot over time.
12
-
13
- FInally when one of my programming partners had difficulty understanding the code, I realizaed I did too, and decided to rebuild it from scratch using components I had already developed, such as davehttp, davetwitter, etc.
14
-
15
- I have converted Little Outliner to use this backend. The app was only modified in how it's configured. Everything else remains the same. It uses the same api glue file that it used to access nodeStorage.
16
-
17
- ### Configuring
18
-
19
- Below is an example of config.json file that goes in the same directory as the appserver.js file.
20
-
21
- ### Example
22
-
23
- ```json
24
-
25
- ### Explanation
26
-
27
- The first section provides configuration information about and to the app that's running on the server's home page.
28
-
29
- 1. productName is an id that can be used to identify the product. No specified use.
30
-
31
- 2. productNameForDisplay is what the app should use in its user interface.
32
-
33
- 3. urlServerHomePageSource is the address of an HTML page that is served through the home page of the server. It can contain macros that plug in values from the server.
34
-
35
- 4. prefsPath is the name of the prefs file for the app. This is the file you should read and save to, to maintain the state of the app for the user.
36
-
37
- 5. docsPath is where the documents for this app are stored in the user's storage on the server.
38
-
39
- 6. urlServerForClient is the address the app uses to call back to the server.
40
-
41
- 7. urlWebsocketServerForClient is the address of the web socket that reports on changes to files stored on the server. This address can be included in the app files so that apps that read the files can subbscribe to changes.
42
-
43
- The second section configures the HTTP server and the connection to Twitter for identity.
44
-
45
- 1. port is the port the HTTP server will run on, unless process.env.PORT is specified. It overrides the port choice in the config file.
46
-
47
- 2. flWebsocketEnabled, a boolean, determines whether or not we initialize the websocket server, if it's true then websocketPort is the port it's listening on.
48
-
49
- 3. myDomain, the domain assigned to this app. It's the domain you'd use to access the home page. If that includes a port, include the port in this value.
50
-
51
- 4. twitterConsumerKey and twitterConsumerSecret are the values that identify the app for Twitter, as assigned on developer.twitter.com.
52
-
53
- ### Updates
54
-
55
- #### v0.5.48 -- 3/18/22 by DW
56
-
57
- Exports three routines: fileExists, readWholeFile, writeWholeFile.
58
-
59
- #### v0.5.44 -- 3/18/22 by DW
60
-
61
- New callback, config.publishFile. If defined we call back with the file, screenname, relpath, flprivate, filetext and the url of the file if it's public.
62
-
63
- It's called when we handle a /publishfile or /writewholefile message.
64
-
65
- #### v0.5.32 -- 12/4/21 by DW
66
-
67
- Added a new callback, publicFileSaved, which is called when the user updates a public file.
68
-
69
- Fixed a bug where a user couldn't create a new file if their public files folder was empty.
70
-
71
- #### v0.5.32 -- 9/26/21 by DW
72
-
73
- Fixed the example app to require "../appserver.js" instead of "lib/daveappserver.js" which only exists on my development machine.
74
-
75
- #### v0.5.31 -- 9/15/21 by DW
76
-
77
- writeWholeFile was meant to be sure the path to the file exists before writing the file. It wasn't doing it, now it does.
78
-
package/readme.opml DELETED
@@ -1,67 +0,0 @@
1
- <?xml version="1.0" encoding="ISO-8859-1"?>
2
- <opml version="2.0">
3
- <head>
4
- <title>readme.md</title>
5
- <dateModified>Sat, 28 May 2022 18:55:48 GMT</dateModified>
6
- <expansionState></expansionState>
7
- <vertScrollState>1</vertScrollState>
8
- <windowTop>300</windowTop>
9
- <windowLeft>700</windowLeft>
10
- <windowBottom>900</windowBottom>
11
- <windowRight>1500</windowRight>
12
- </head>
13
- <body text="readme.md">
14
- <outline text="# appserver">
15
- <outline text="A new release of nodeStorage that removes all the historical addons, streamlines configuration, only serves from the local file system, and serves the home page of the app. It could be thought of as v2.0 of nodeStorage except it is not backward compatible. "></outline>
16
- </outline>
17
- <outline text="### Overview">
18
- <outline text="Over the years nodeStorage got a lot of appendages and add-ons that most apps don't use. "></outline>
19
- <outline text="The configuration file was confusing and not well documented."></outline>
20
- <outline text="It was one of the first things I wrote for Node, and I learned a lot over time."></outline>
21
- <outline text="FInally when one of my programming partners had difficulty understanding the code, I realizaed I did too, and decided to rebuild it from scratch using components I had already developed, such as davehttp, davetwitter, etc. "></outline>
22
- <outline text="I have converted Little Outliner to use this backend. The app was only modified in how it's configured. Everything else remains the same. It uses the same api glue file that it used to access nodeStorage. "></outline>
23
- </outline>
24
- <outline text="### Configuring">
25
- <outline text="Below is an example of config.json file that goes in the same directory as the appserver.js file."></outline>
26
- </outline>
27
- <outline text="### Example">
28
- <outline text="&lt;%includeExample (&quot;example1&quot;)%&gt;"></outline>
29
- </outline>
30
- <outline text="### Explanation">
31
- <outline text="The first section provides configuration information about and to the app that's running on the server's home page. ">
32
- <outline text="1. productName is an id that can be used to identify the product. No specified use."></outline>
33
- <outline text="2. productNameForDisplay is what the app should use in its user interface."></outline>
34
- <outline text="3. urlServerHomePageSource is the address of an HTML page that is served through the home page of the server. It can contain macros that plug in values from the server. "></outline>
35
- <outline text="4. prefsPath is the name of the prefs file for the app. This is the file you should read and save to, to maintain the state of the app for the user. "></outline>
36
- <outline text="5. docsPath is where the documents for this app are stored in the user's storage on the server."></outline>
37
- <outline text="6. urlServerForClient is the address the app uses to call back to the server. "></outline>
38
- <outline text="7. urlWebsocketServerForClient is the address of the web socket that reports on changes to files stored on the server. This address can be included in the app files so that apps that read the files can subbscribe to changes. "></outline>
39
- </outline>
40
- <outline text="The second section configures the HTTP server and the connection to Twitter for identity. ">
41
- <outline text="1. port is the port the HTTP server will run on, unless process.env.PORT is specified. It overrides the port choice in the config file."></outline>
42
- <outline text="2. flWebsocketEnabled, a boolean, determines whether or not we initialize the websocket server, if it's true then websocketPort is the port it's listening on. "></outline>
43
- <outline text="3. myDomain, the domain assigned to this app. It's the domain you'd use to access the home page. If that includes a port, include the port in this value."></outline>
44
- <outline text="4. twitterConsumerKey and twitterConsumerSecret are the values that identify the app for Twitter, as assigned on developer.twitter.com. "></outline>
45
- </outline>
46
- </outline>
47
- <outline created="Wed, 15 Sep 2021 14:19:02 GMT" text="### Updates">
48
- <outline created="Sat, 28 May 2022 18:54:36 GMT" text="#### v0.5.48 -- 3/18/22 by DW">
49
- <outline created="Sat, 28 May 2022 18:54:39 GMT" text="Exports three routines: fileExists, readWholeFile, writeWholeFile."></outline>
50
- </outline>
51
- <outline created="Fri, 18 Mar 2022 14:21:28 GMT" text="#### v0.5.44 -- 3/18/22 by DW">
52
- <outline created="Fri, 18 Mar 2022 14:21:59 GMT" text="New callback, config.publishFile. If defined we call back with the file, screenname, relpath, flprivate, filetext and the url of the file if it's public."></outline>
53
- <outline created="Fri, 18 Mar 2022 14:22:36 GMT" text="It's called when we handle a /publishfile or /writewholefile message."></outline>
54
- </outline>
55
- <outline created="Sat, 04 Dec 2021 21:29:57 GMT" text="#### v0.5.32 -- 12/4/21 by DW">
56
- <outline created="Sat, 04 Dec 2021 21:30:33 GMT" text="Added a new callback, publicFileSaved, which is called when the user updates a public file. "></outline>
57
- <outline created="Sat, 04 Dec 2021 21:29:58 GMT" text="Fixed a bug where a user couldn't create a new file if their public files folder was empty. "></outline>
58
- </outline>
59
- <outline created="Wed, 15 Sep 2021 14:19:10 GMT" text="#### v0.5.32 -- 9/26/21 by DW">
60
- <outline created="Wed, 15 Sep 2021 14:19:12 GMT" text="Fixed the example app to require &quot;../appserver.js&quot; instead of &quot;lib/daveappserver.js&quot; which only exists on my development machine. "></outline>
61
- </outline>
62
- <outline created="Wed, 15 Sep 2021 14:19:10 GMT" text="#### v0.5.31 -- 9/15/21 by DW">
63
- <outline created="Wed, 15 Sep 2021 14:19:12 GMT" text="writeWholeFile was meant to be sure the path to the file exists before writing the file. It wasn't doing it, now it does. "></outline>
64
- </outline>
65
- </outline>
66
- </body>
67
- </opml>