catto.js 0.7.9 → 0.8.0
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/Server.js +9 -4
- package/package.json +1 -1
- package/utils.js +9 -0
package/Server.js
CHANGED
|
@@ -156,7 +156,7 @@ class Server extends EventEmitter {
|
|
|
156
156
|
}
|
|
157
157
|
return this;
|
|
158
158
|
}
|
|
159
|
-
static renderCJS(filepath, options, callback) {
|
|
159
|
+
static renderCJS(app, filepath, options, callback) {
|
|
160
160
|
var code = fs.readFileSync(filepath).toString("utf-8");
|
|
161
161
|
var doctype = code.startsWith("<!DOCTYPE html>");
|
|
162
162
|
if (doctype) {
|
|
@@ -167,12 +167,17 @@ class Server extends EventEmitter {
|
|
|
167
167
|
parts.forEach((part, index) => {
|
|
168
168
|
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
169
|
});
|
|
170
|
-
var context = vm.createContext(
|
|
170
|
+
var context = vm.createContext(Object.assign({
|
|
171
|
+
"include": (filepath, options) => Server.renderCJS(app, path.join(app.get("views"), filepath.endsWith(".cjs") ? filepath : `${filepath}.cjs`), options)
|
|
172
|
+
}, options));
|
|
171
173
|
vm.runInContext(compile, context);
|
|
172
|
-
|
|
174
|
+
if (typeof callback === "function") {
|
|
175
|
+
callback(null, context.__output);
|
|
176
|
+
}
|
|
177
|
+
return context.__output;
|
|
173
178
|
}
|
|
174
179
|
static injectCJS(app) {
|
|
175
|
-
app.engine("cjs", Server.renderCJS).set("view engine", "cjs").get("/_cattojs/cjs_client.js", (req, res) => {
|
|
180
|
+
app.engine("cjs", Server.renderCJS.bind(null, app)).set("view engine", "cjs").get("/_cattojs/cjs_client.js", (req, res) => {
|
|
176
181
|
res.sendFile(path.join(__dirname, "cjs_client.js"));
|
|
177
182
|
});
|
|
178
183
|
}
|
package/package.json
CHANGED
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 => {
|