catto.js 0.6.3 → 0.6.4
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 +8 -5
- package/cjs_client.js +4 -1
- package/package.json +1 -1
package/Server.js
CHANGED
|
@@ -59,9 +59,7 @@ class Server extends EventEmitter {
|
|
|
59
59
|
this.app.set("view engine", "ejs");
|
|
60
60
|
}
|
|
61
61
|
if (this.options.cjs) {
|
|
62
|
-
|
|
63
|
-
res.sendFile(path.join(__dirname, "cjs_client.js"));
|
|
64
|
-
});
|
|
62
|
+
Server.injectCJS(this.app);
|
|
65
63
|
}
|
|
66
64
|
if (this.options.secret) {
|
|
67
65
|
this.app.use(session({
|
|
@@ -155,14 +153,14 @@ class Server extends EventEmitter {
|
|
|
155
153
|
}
|
|
156
154
|
return this;
|
|
157
155
|
}
|
|
158
|
-
renderCJS(filepath, options, callback) {
|
|
156
|
+
static renderCJS(filepath, options, callback) {
|
|
159
157
|
var code = fs.readFileSync(filepath).toString("utf-8");
|
|
160
158
|
var doctype = code.startsWith("<!DOCTYPE html>");
|
|
161
159
|
if (doctype) {
|
|
162
160
|
code = code.replace(/^<!DOCTYPE html>(\r?\n)?/, "");
|
|
163
161
|
}
|
|
164
162
|
var parts = code.split(/<%s(?:erver)?(?!\*)(?!=) +(.+?) +%>/g);
|
|
165
|
-
var compile = `var __output = "${doctype ? "<!DOCTYPE html>\\n" : ""}<script src=\\"/_cattojs/cjs_client.js\\"></script>\\n";\nfunction __escape(str) {\n return str.split("<").join("<").split(">").join(">");\n}\n`;
|
|
163
|
+
var compile = `var __output = "${doctype ? "<!DOCTYPE html>\\n" : ""}<script src=\\"/_cattojs/cjs_client.js\\"></script>\\n";\nfunction __escape(str) {\n if (typeof str !== "string") {\n str = str.toString();\n }\n return str.split("<").join("<").split(">").join(">");\n}\n`;
|
|
166
164
|
parts.forEach((part, index) => {
|
|
167
165
|
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`);
|
|
168
166
|
});
|
|
@@ -170,6 +168,11 @@ class Server extends EventEmitter {
|
|
|
170
168
|
vm.runInContext(compile, context);
|
|
171
169
|
callback(null, context.__output);
|
|
172
170
|
}
|
|
171
|
+
static injectCJS(app) {
|
|
172
|
+
app.engine("cjs", Server.renderCJS).set("view engine", "cjs").get("/_cattojs/cjs_client.js", (req, res) => {
|
|
173
|
+
res.sendFile(path.join(__dirname, "cjs_client.js"));
|
|
174
|
+
});
|
|
175
|
+
}
|
|
173
176
|
static fa(text) {
|
|
174
177
|
return (req,res) => {
|
|
175
178
|
res.end(text);
|
package/cjs_client.js
CHANGED
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
compile += ((index + 1) % 2 < 1 ? `${part}\n` : `output += ${JSON.stringify(part)}.replace(/<%c(?:lient)?(\\*)?= +(.+?) +%>/g, (_, t, g) => __rpf(t, g, eval(g), !0)).replace(/<%c(?:lient)?(\\*)?- +(.+?) +%>/g, (_, t, g) => __rpf(t, g, eval(g), !1)).replace(/<%c(lient)\\*?# +(.+?) +%>/g, "");\n`);
|
|
15
15
|
});
|
|
16
16
|
window.__rpf = (t, g, result, r) => {
|
|
17
|
+
if (typeof result !== "string") {
|
|
18
|
+
result = result.toString();
|
|
19
|
+
}
|
|
17
20
|
if (r) {
|
|
18
21
|
result = result.split("<").join("<").split(">").join(">");
|
|
19
22
|
}
|
|
@@ -43,4 +46,4 @@
|
|
|
43
46
|
}
|
|
44
47
|
document.documentElement.innerHTML = render(document.documentElement.innerHTML);
|
|
45
48
|
});
|
|
46
|
-
})();
|
|
49
|
+
})();
|