notherbase-fs 4.0.23 → 4.1.1

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.
@@ -64,12 +64,31 @@ export default class Creation {
64
64
  main: req.main,
65
65
  query: req.query,
66
66
  route: req.path,
67
- requireUser: req.lock
67
+ requireUser: req.lock,
68
+ preprocessed: {}
68
69
  }
69
70
 
70
71
  if (req.session.currentUser) {
71
72
  context.user = await req.db.Spirit.recallOne("user", null, { username: req.session.currentUser });
72
- }
73
+ }
74
+
75
+ //preprocess
76
+ let preprocessScripts = fs.existsSync(req.preprocess) ? fs.readdirSync(req.preprocess) : [];
77
+ for (let preprocessScript of preprocessScripts) {
78
+ try {
79
+ let scriptPath = `${req.preprocess}/${preprocessScript}`;
80
+
81
+ if (fs.existsSync(scriptPath)) {
82
+ let script = await import(process.env.WINDOWS == "true" ? `file://${scriptPath}` : scriptPath);
83
+ let result = await script.default(req, context.user, this.io);
84
+ context.preprocessed[preprocessScript.split(".")[0]] = result;
85
+ }
86
+ else context.preprocessed[preprocessScript.split(".")[0]] = `Error: Script Not Found`;
87
+ } catch (error) {
88
+ console.log(error);
89
+ context.preprocessed[preprocessScript.split(".")[0]] = `Error: Server Error`;
90
+ }
91
+ }
73
92
 
74
93
  res.render(req.toRender, context);
75
94
  }
@@ -89,6 +108,7 @@ export default class Creation {
89
108
  */
90
109
  front = async (req, res, next) => {
91
110
  req.main = req.contentPath + "/the-front/index";
111
+ req.preprocess = req.contentPath + "/the-front/_preprocess";
92
112
  req.siteTitle = this.bases[req.hosting].title;
93
113
  req.toRender = "explorer";
94
114
  next();
@@ -102,6 +122,7 @@ export default class Creation {
102
122
  */
103
123
  frontDetail = async (req, res, next) => {
104
124
  req.main = `${req.contentPath}/the-front/${req.params.frontDetail}/index`;
125
+ req.preprocess = `${req.contentPath}/the-front/${req.params.frontDetail}/_preprocess`;
105
126
  req.siteTitle = `${this.bases[req.hosting].title} - ${req.params.frontDetail}`;
106
127
  req.toRender = "explorer";
107
128
  next();
@@ -115,6 +136,7 @@ export default class Creation {
115
136
  */
116
137
  poi = async (req, res, next) => {
117
138
  req.main = `${req.contentPath}/${req.params.region}/${req.params.area}/${req.params.poi}/index`;
139
+ req.preprocess = `${req.contentPath}/${req.params.region}/${req.params.area}/${req.params.poi}/_preprocess`;
118
140
  req.siteTitle = `${this.bases[req.hosting].title} - ${req.params.poi}`;
119
141
  req.toRender = "explorer";
120
142
  next();
@@ -128,6 +150,7 @@ export default class Creation {
128
150
  */
129
151
  detail = async (req, res, next) => {
130
152
  req.main = `${req.contentPath}/${req.params.region}/${req.params.area}/${req.params.poi}/${req.params.detail}/index`;
153
+ req.preprocess = `${req.contentPath}/${req.params.region}/${req.params.area}/${req.params.poi}/${req.params.detail}/_preprocess`;
131
154
  req.siteTitle = `${this.bases[req.hosting].title} - ${req.params.detail}`;
132
155
  req.toRender = "explorer";
133
156
  next();
@@ -141,6 +164,7 @@ export default class Creation {
141
164
  */
142
165
  page = async (req, res, next) => {
143
166
  req.main = `${req.contentPath}/pages/${req.params.page}/index`;
167
+ req.preprocess = `${req.contentPath}/pages/${req.params.page}/_preprocess`;
144
168
  req.siteTitle = `${req.params.page}`;
145
169
  req.toRender = req.main;
146
170
  next();
@@ -19,10 +19,20 @@ export default class SpiritWorld {
19
19
 
20
20
  this.user = new User();
21
21
  this.router = express.Router();
22
- this.router.post("/loadAll", this.loadAll);
23
- this.router.post("/load", this.load);
24
- this.router.post("/serve", this.serve);
25
- this.router.use("/user", this.user.router);
22
+ this.router.post("/loadAll", this.catchErrors, this.loadAll);
23
+ this.router.post("/load", this.catchErrors, this.load);
24
+ this.router.post("/serve", this.catchErrors, this.serve);
25
+ this.router.use("/user", this.catchErrors, this.user.router);
26
+ }
27
+
28
+ // abstract out the try catch pattern
29
+ catchErrors = async (req, res, next) => {
30
+ try {
31
+ await next();
32
+ } catch (error) {
33
+ console.log(error);
34
+ fail(res, "Server error");
35
+ }
26
36
  }
27
37
 
28
38
  /**
@@ -91,29 +101,24 @@ export default class SpiritWorld {
91
101
  * @returns {Object} The requested spirits.
92
102
  */
93
103
  loadAll = async (req, res) => {
94
- try {
95
- let parent = null;
96
- let data = req.body.data ? req.body.data : {};
97
- let id = req.body.id ? req.body.id : null;
98
-
99
- // if the scope is local, the parent is the user's id
100
- if (req.body.scope === "local") {
101
- let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
102
- if (user?.memory?._id) parent = user.memory._id;
103
- else {
104
- fail(res, "User had no id on load()");
105
- return;
106
- }
107
- }
104
+ let parent = null;
105
+ let data = req.body.data ? req.body.data : {};
106
+ let id = req.body.id ? req.body.id : null;
107
+
108
+ // if the scope is local, the parent is the user's id
109
+ if (req.body.scope === "local") {
110
+ let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
111
+ if (user?.memory?._id) parent = user.memory._id;
112
+ else {
113
+ fail(res, "User had no id on load()");
114
+ return;
115
+ }
116
+ }
108
117
 
109
- // recall all spirits with the given service name and parent
110
- let spirits = await req.db.Spirit.recallAll(req.body.service, parent, data, id);
118
+ // recall all spirits with the given service name and parent
119
+ let spirits = await req.db.Spirit.recallAll(req.body.service, parent, data, id);
111
120
 
112
- res.send(spirits);
113
- } catch (error) {
114
- console.log(error);
115
- fail(res, "Server error");
116
- }
121
+ res.send(spirits);
117
122
  }
118
123
 
119
124
  /**
@@ -123,29 +128,24 @@ export default class SpiritWorld {
123
128
  * @returns {Object} The requested spirit.
124
129
  */
125
130
  load = async (req, res) => {
126
- try {
127
- let parent = null;
128
- let data = req.body.data ? req.body.data : {};
129
- let id = req.body.id ? req.body.id : null;
130
-
131
- // if the scope is local, the parent is the user's id
132
- if (req.body.scope === "local") {
133
- let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
134
- if (user?.memory?._id) parent = user.memory._id;
135
- else {
136
- fail(res, "User had no id on load()");
137
- return;
138
- }
139
- }
131
+ let parent = null;
132
+ let data = req.body.data ? req.body.data : {};
133
+ let id = req.body.id ? req.body.id : null;
134
+
135
+ // if the scope is local, the parent is the user's id
136
+ if (req.body.scope === "local") {
137
+ let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
138
+ if (user?.memory?._id) parent = user.memory._id;
139
+ else {
140
+ fail(res, "User had no id on load()");
141
+ return;
142
+ }
143
+ }
140
144
 
141
- // recall all spirits with the given service name and parent
142
- let spirit = await req.db.Spirit.recallOne(req.body.service, parent, data, id);
145
+ // recall all spirits with the given service name and parent
146
+ let spirit = await req.db.Spirit.recallOne(req.body.service, parent, data, id);
143
147
 
144
- res.send(spirit);
145
- } catch (error) {
146
- console.log(error);
147
- fail(res, "Server error");
148
- }
148
+ res.send(spirit);
149
149
  }
150
150
 
151
151
  /**
@@ -154,22 +154,17 @@ export default class SpiritWorld {
154
154
  * @param {Object} res
155
155
  */
156
156
  serve = async (req, res) => {
157
- try {
158
- let scriptPath = `${req.contentPath}${req.body.route}/${req.body.script}.js`;
159
-
160
- let script, result = null;
157
+ let scriptPath = `${req.contentPath}${req.body.route}/${req.body.script}.js`;
158
+
159
+ let script, result = null;
161
160
 
162
- if (fs.existsSync(scriptPath)) {
163
- let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
161
+ if (fs.existsSync(scriptPath)) {
162
+ let user = await req.db.Spirit.recallOne("user", null, { username: req.session?.currentUser });
164
163
 
165
- script = await import(process.env.WINDOWS == "true" ? `file://${scriptPath}` : scriptPath);
166
- result = await script.default(req, user, this.io);
167
- success(res, "Served.", result);
168
- }
169
- else fail(res, `Script not found: ${req.body.script} at ${scriptPath}`);
170
- } catch (error) {
171
- console.log(error);
172
- fail(res, "Server error");
164
+ script = await import(process.env.WINDOWS == "true" ? `file://${scriptPath}` : scriptPath);
165
+ result = await script.default(req, user, this.io);
166
+ success(res, "Served.", result);
173
167
  }
168
+ else fail(res, `Script not found: ${req.body.script} at ${scriptPath}`);
174
169
  }
175
170
  }
@@ -221,19 +221,21 @@ export default class User {
221
221
  let user = await req.db.Spirit.recallOne("user", null, { username: req.session.currentUser });
222
222
 
223
223
  if (check(res, user, "Account not found!")) {
224
- let passResult = await bcrypt.compare(req.body.password, user.memory.data.password);
225
-
226
- if (check(res, passResult, "Password doesn't match the username.")) {
227
- let data = JSON.parse(req.body.data);
228
- let imported = 0;
229
- for (let i = 0; i < data.length; i++) {
230
- if (data[i].parent != null) {
231
- let spirit = await req.db.Spirit.create(data[i].service, data[i].data, user.memory._id);
232
- if (spirit) imported++;
224
+ if (check(res, req.body.password, "Password error.")) {
225
+ let passResult = await bcrypt.compare(req.body.password, user.memory.data.password);
226
+
227
+ if (check(res, passResult, "Password doesn't match the username.")) {
228
+ let data = JSON.parse(req.body.data);
229
+ let imported = 0;
230
+ for (let i = 0; i < data.length; i++) {
231
+ if (data[i].parent != null) {
232
+ let spirit = await req.db.Spirit.create(data[i].service, data[i].data, user.memory._id);
233
+ if (spirit) imported++;
234
+ }
233
235
  }
234
- }
235
236
 
236
- success(res, "Data Imported", imported);
237
+ success(res, "Data Imported", imported);
238
+ }
237
239
  }
238
240
  }
239
241
  }
package/notherbase-fs.js CHANGED
@@ -26,8 +26,10 @@ class NotherBaseFS {
26
26
  this.spiritWorld = new SpiritWorld(this.io);
27
27
  this.creation = new Creation(bases);
28
28
 
29
- //set views path
29
+ //set view engine
30
30
  this.app.set("view engine", "ejs");
31
+
32
+ //set views path
31
33
  this.app.set("views", `${__dirname}/views`);
32
34
 
33
35
  // allows us to use post body data
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notherbase-fs",
3
- "version": "4.0.23",
3
+ "version": "4.1.1",
4
4
  "description": "Functions to help make developing for NotherBase easier.",
5
5
  "exports": "./notherbase-fs.js",
6
6
  "scripts": {
@@ -0,0 +1,9 @@
1
+ export default async (req, user, io) => {
2
+ if (user) {
3
+ let spirit = await req.db.Spirit.recallOne("test-save3", user.memory._id);
4
+
5
+ return spirit.memory.data.text;
6
+ }
7
+
8
+ return "Not Logged In";
9
+ }
@@ -12,6 +12,7 @@
12
12
  <button class="switch" onclick="base.do('flip')">Flip Me</button>
13
13
  <input type="text" id="test" placeholder="Loading...">
14
14
  <button onclick="saveInput()">Save</button>
15
+ <p>saved: <%= preprocessed.saved %></p>
15
16
 
16
17
  <hr>
17
18
 
@@ -1,5 +1,5 @@
1
1
  export default async (req, user, io) => {
2
- let spirit = await req.db.Spirit.recallOne("test-save3", user.memory._id);
2
+ let spirit = await req.db.Spirit.recallOrCreateOne("test-save3", user.memory._id);
3
3
  // console.log(req.body.text, spirit.memory);
4
4
 
5
5
  await spirit.commit({