catto.js 0.7.9 → 0.8.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.
Files changed (3) hide show
  1. package/Server.js +15 -7
  2. package/package.json +1 -1
  3. package/utils.js +9 -0
package/Server.js CHANGED
@@ -37,7 +37,8 @@ class Server extends EventEmitter {
37
37
  "ejs": !1,
38
38
  "cjs": !1,
39
39
  "proxies": 0,
40
- "bodyCompatible": !1
40
+ "bodyCompatible": !1,
41
+ "secureCookie": !0
41
42
  }, options || {});
42
43
  this.app = express();
43
44
  if (this.options.ssl) {
@@ -69,8 +70,10 @@ class Server extends EventEmitter {
69
70
  "store": new FileStore(this.options.storeOptions),
70
71
  "secret": this.options.secret,
71
72
  "cookie": {
72
- "secure": (this.options.ssl || this.options.sslProxy)
73
- }
73
+ "secure": this.options.secureCookie
74
+ },
75
+ "resave": !0,
76
+ "saveUninitialized": !1
74
77
  }));
75
78
  }
76
79
  }
@@ -156,7 +159,7 @@ class Server extends EventEmitter {
156
159
  }
157
160
  return this;
158
161
  }
159
- static renderCJS(filepath, options, callback) {
162
+ static renderCJS(app, filepath, options, callback) {
160
163
  var code = fs.readFileSync(filepath).toString("utf-8");
161
164
  var doctype = code.startsWith("<!DOCTYPE html>");
162
165
  if (doctype) {
@@ -167,12 +170,17 @@ class Server extends EventEmitter {
167
170
  parts.forEach((part, index) => {
168
171
  compile += ((index + 1) % 2 < 1 ? `${part}\n` : `__output += ${JSON.stringify(part)}.replace(/<%s(?:erver)?(?!\\*)= +(.+?) +%>/g, (_, g) => __escape(eval(g))).replace(/<%s(?:erver)?(?!\\*)- +(.+?) +%>/g, (_, g) => eval(g)).replace(/<%s(erver)\\*?# +(.+?) +%>/g, "");\n`);
169
172
  });
170
- var context = vm.createContext(options);
173
+ var context = vm.createContext(Object.assign({
174
+ "include": (filepath, options) => Server.renderCJS(app, path.join(app.get("views"), filepath.endsWith(".cjs") ? filepath : `${filepath}.cjs`), options)
175
+ }, options));
171
176
  vm.runInContext(compile, context);
172
- callback(null, context.__output);
177
+ if (typeof callback === "function") {
178
+ callback(null, context.__output);
179
+ }
180
+ return context.__output;
173
181
  }
174
182
  static injectCJS(app) {
175
- app.engine("cjs", Server.renderCJS).set("view engine", "cjs").get("/_cattojs/cjs_client.js", (req, res) => {
183
+ app.engine("cjs", Server.renderCJS.bind(null, app)).set("view engine", "cjs").get("/_cattojs/cjs_client.js", (req, res) => {
176
184
  res.sendFile(path.join(__dirname, "cjs_client.js"));
177
185
  });
178
186
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "catto.js",
3
- "version": "0.7.9",
3
+ "version": "0.8.1",
4
4
  "description": "Universal module for everything.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/utils.js CHANGED
@@ -4,6 +4,15 @@ Array.prototype.remove = function(index) {
4
4
  Array.prototype.has = function(data) {
5
5
  return this.includes(data);
6
6
  };
7
+ String.prototype.replaceAsync = async function(regex, asyncFn) {
8
+ var promises = [];
9
+ this.replace(regex, (full, ...args) => {
10
+ promises.push(asyncFn(full, ...args));
11
+ return full;
12
+ });
13
+ var data = await Promise.all(promises);
14
+ return this.replace(regex, () => data.shift());
15
+ };
7
16
  var utils = {};
8
17
  utils.waitFor = (obj, prop) => {
9
18
  return new Promise(r => {